wasm_loader.c 479 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. static void
  36. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  37. {
  38. if (error_buf != NULL) {
  39. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  40. string);
  41. }
  42. }
  43. static void
  44. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  45. {
  46. va_list args;
  47. char buf[128];
  48. if (error_buf != NULL) {
  49. va_start(args, format);
  50. vsnprintf(buf, sizeof(buf), format, args);
  51. va_end(args);
  52. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  53. }
  54. }
  55. static bool
  56. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  57. char *error_buf, uint32 error_buf_size)
  58. {
  59. if ((uintptr_t)buf + length < (uintptr_t)buf
  60. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  61. set_error_buf(error_buf, error_buf_size,
  62. "unexpected end of section or function");
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool
  68. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  69. char *error_buf, uint32 error_buf_size)
  70. {
  71. if ((uintptr_t)buf + length < (uintptr_t)buf
  72. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  73. set_error_buf(error_buf, error_buf_size, "unexpected end");
  74. return false;
  75. }
  76. return true;
  77. }
  78. #define CHECK_BUF(buf, buf_end, length) \
  79. do { \
  80. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  81. goto fail; \
  82. } \
  83. } while (0)
  84. #define CHECK_BUF1(buf, buf_end, length) \
  85. do { \
  86. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  87. goto fail; \
  88. } \
  89. } while (0)
  90. #define skip_leb(p) while (*p++ & 0x80)
  91. #define skip_leb_int64(p, p_end) skip_leb(p)
  92. #define skip_leb_uint32(p, p_end) skip_leb(p)
  93. #define skip_leb_int32(p, p_end) skip_leb(p)
  94. static bool
  95. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  96. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  97. {
  98. const uint8 *buf = *p_buf;
  99. uint64 result = 0;
  100. uint32 shift = 0;
  101. uint32 offset = 0, bcnt = 0;
  102. uint64 byte;
  103. while (true) {
  104. /* uN or SN must not exceed ceil(N/7) bytes */
  105. if (bcnt + 1 > (maxbits + 6) / 7) {
  106. set_error_buf(error_buf, error_buf_size,
  107. "integer representation too long");
  108. return false;
  109. }
  110. CHECK_BUF(buf, buf_end, offset + 1);
  111. byte = buf[offset];
  112. offset += 1;
  113. result |= ((byte & 0x7f) << shift);
  114. shift += 7;
  115. bcnt += 1;
  116. if ((byte & 0x80) == 0) {
  117. break;
  118. }
  119. }
  120. if (!sign && maxbits == 32 && shift >= maxbits) {
  121. /* The top bits set represent values > 32 bits */
  122. if (((uint8)byte) & 0xf0)
  123. goto fail_integer_too_large;
  124. }
  125. else if (sign && maxbits == 32) {
  126. if (shift < maxbits) {
  127. /* Sign extend, second highest bit is the sign bit */
  128. if ((uint8)byte & 0x40)
  129. result |= (~((uint64)0)) << shift;
  130. }
  131. else {
  132. /* The top bits should be a sign-extension of the sign bit */
  133. bool sign_bit_set = ((uint8)byte) & 0x8;
  134. int top_bits = ((uint8)byte) & 0xf0;
  135. if ((sign_bit_set && top_bits != 0x70)
  136. || (!sign_bit_set && top_bits != 0))
  137. goto fail_integer_too_large;
  138. }
  139. }
  140. else if (sign && maxbits == 64) {
  141. if (shift < maxbits) {
  142. /* Sign extend, second highest bit is the sign bit */
  143. if ((uint8)byte & 0x40)
  144. result |= (~((uint64)0)) << shift;
  145. }
  146. else {
  147. /* The top bits should be a sign-extension of the sign bit */
  148. bool sign_bit_set = ((uint8)byte) & 0x1;
  149. int top_bits = ((uint8)byte) & 0xfe;
  150. if ((sign_bit_set && top_bits != 0x7e)
  151. || (!sign_bit_set && top_bits != 0))
  152. goto fail_integer_too_large;
  153. }
  154. }
  155. *p_buf += offset;
  156. *p_result = result;
  157. return true;
  158. fail_integer_too_large:
  159. set_error_buf(error_buf, error_buf_size, "integer too large");
  160. fail:
  161. return false;
  162. }
  163. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  164. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  165. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  166. #define read_leb_int64(p, p_end, res) \
  167. do { \
  168. uint64 res64; \
  169. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  170. error_buf_size)) \
  171. goto fail; \
  172. res = (int64)res64; \
  173. } while (0)
  174. #define read_leb_uint32(p, p_end, res) \
  175. do { \
  176. uint64 res64; \
  177. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  178. error_buf_size)) \
  179. goto fail; \
  180. res = (uint32)res64; \
  181. } while (0)
  182. #define read_leb_int32(p, p_end, res) \
  183. do { \
  184. uint64 res64; \
  185. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  186. error_buf_size)) \
  187. goto fail; \
  188. res = (int32)res64; \
  189. } while (0)
  190. static char *
  191. type2str(uint8 type)
  192. {
  193. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  194. #if WASM_ENABLE_GC != 0
  195. char *type_str_ref[] = { "nullref", "arrayref", "structref",
  196. "nullexternref", "nullfuncref", "i31ref",
  197. "(ref ht)", "(ref null ht)", "eqref",
  198. "anyref", "externref", "funcref" };
  199. #endif
  200. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  201. return type_str[type - VALUE_TYPE_V128];
  202. #if WASM_ENABLE_GC != 0
  203. else if (wasm_is_type_reftype(type))
  204. return type_str_ref[type - REF_TYPE_NULLREF];
  205. #endif
  206. else if (type == VALUE_TYPE_FUNCREF)
  207. return "funcref";
  208. else if (type == VALUE_TYPE_EXTERNREF)
  209. return "externref";
  210. else
  211. return "unknown type";
  212. }
  213. static bool
  214. is_32bit_type(uint8 type)
  215. {
  216. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  217. #if WASM_ENABLE_GC != 0
  218. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  219. #elif WASM_ENABLE_REF_TYPES != 0
  220. /* For reference types, we use uint32 index to represent
  221. the funcref and externref */
  222. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  223. #endif
  224. )
  225. return true;
  226. return false;
  227. }
  228. static bool
  229. is_64bit_type(uint8 type)
  230. {
  231. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  232. #if WASM_ENABLE_GC != 0
  233. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  234. #endif
  235. )
  236. return true;
  237. return false;
  238. }
  239. static bool
  240. is_value_type(uint8 type)
  241. {
  242. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  243. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  244. #if WASM_ENABLE_GC != 0
  245. /* reference types, 0x65 to 0x70 */
  246. || wasm_is_type_reftype(type)
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  249. #endif
  250. #if WASM_ENABLE_SIMD != 0
  251. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  252. || type == VALUE_TYPE_V128 /* 0x7B */
  253. #endif
  254. #endif
  255. )
  256. return true;
  257. return false;
  258. }
  259. #if WASM_ENABLE_GC != 0
  260. static bool
  261. is_packed_type(uint8 type)
  262. {
  263. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  264. }
  265. #endif
  266. static bool
  267. is_byte_a_type(uint8 type)
  268. {
  269. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  270. }
  271. #if WASM_ENABLE_SIMD != 0
  272. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  273. static V128
  274. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  275. {
  276. V128 result;
  277. uint8 i;
  278. for (i = 0; i != 16; ++i) {
  279. result.i8x16[i] = read_uint8(p_buf);
  280. }
  281. return result;
  282. }
  283. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  284. #endif /* end of WASM_ENABLE_SIMD */
  285. static void *
  286. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  287. {
  288. void *mem;
  289. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  290. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  291. return NULL;
  292. }
  293. memset(mem, 0, (uint32)size);
  294. return mem;
  295. }
  296. static bool
  297. check_utf8_str(const uint8 *str, uint32 len)
  298. {
  299. /* The valid ranges are taken from page 125, below link
  300. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  301. const uint8 *p = str, *p_end = str + len;
  302. uint8 chr;
  303. while (p < p_end) {
  304. chr = *p;
  305. if (chr < 0x80) {
  306. p++;
  307. }
  308. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  309. if (p[1] < 0x80 || p[1] > 0xBF) {
  310. return false;
  311. }
  312. p += 2;
  313. }
  314. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  315. if (chr == 0xE0) {
  316. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  317. return false;
  318. }
  319. }
  320. else if (chr == 0xED) {
  321. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  322. return false;
  323. }
  324. }
  325. else if (chr >= 0xE1 && chr <= 0xEF) {
  326. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  327. return false;
  328. }
  329. }
  330. p += 3;
  331. }
  332. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  333. if (chr == 0xF0) {
  334. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  335. || p[3] < 0x80 || p[3] > 0xBF) {
  336. return false;
  337. }
  338. }
  339. else if (chr >= 0xF1 && chr <= 0xF3) {
  340. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  341. || p[3] < 0x80 || p[3] > 0xBF) {
  342. return false;
  343. }
  344. }
  345. else if (chr == 0xF4) {
  346. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  347. || p[3] < 0x80 || p[3] > 0xBF) {
  348. return false;
  349. }
  350. }
  351. p += 4;
  352. }
  353. else {
  354. return false;
  355. }
  356. }
  357. return (p == p_end);
  358. }
  359. static char *
  360. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  361. bool is_load_from_file_buf, char *error_buf,
  362. uint32 error_buf_size)
  363. {
  364. StringNode *node, *node_next;
  365. if (!check_utf8_str(str, len)) {
  366. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  367. return NULL;
  368. }
  369. if (len == 0) {
  370. return "";
  371. }
  372. else if (is_load_from_file_buf) {
  373. /* As the file buffer can be referred to after loading, we use
  374. the previous byte of leb encoded size to adjust the string:
  375. move string 1 byte backward and then append '\0' */
  376. char *c_str = (char *)str - 1;
  377. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  378. c_str[len] = '\0';
  379. return c_str;
  380. }
  381. /* Search const str list */
  382. node = module->const_str_list;
  383. while (node) {
  384. node_next = node->next;
  385. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  386. break;
  387. node = node_next;
  388. }
  389. if (node) {
  390. return node->str;
  391. }
  392. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  393. error_buf_size))) {
  394. return NULL;
  395. }
  396. node->str = ((char *)node) + sizeof(StringNode);
  397. bh_memcpy_s(node->str, len + 1, str, len);
  398. node->str[len] = '\0';
  399. if (!module->const_str_list) {
  400. /* set as head */
  401. module->const_str_list = node;
  402. node->next = NULL;
  403. }
  404. else {
  405. /* insert it */
  406. node->next = module->const_str_list;
  407. module->const_str_list = node;
  408. }
  409. return node->str;
  410. }
  411. #if WASM_ENABLE_GC != 0
  412. static bool
  413. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  414. uint32 error_buf_size)
  415. {
  416. if (type_index >= module->type_count) {
  417. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  418. type_index);
  419. return false;
  420. }
  421. return true;
  422. }
  423. static bool
  424. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  425. uint32 error_buf_size)
  426. {
  427. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  428. return false;
  429. }
  430. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  431. set_error_buf(error_buf, error_buf_size, "unkown array type");
  432. return false;
  433. }
  434. return true;
  435. }
  436. #endif
  437. static bool
  438. check_function_index(const WASMModule *module, uint32 function_index,
  439. char *error_buf, uint32 error_buf_size)
  440. {
  441. if (function_index
  442. >= module->import_function_count + module->function_count) {
  443. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  444. function_index);
  445. return false;
  446. }
  447. return true;
  448. }
  449. static bool
  450. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  451. InitializerExpression *init_expr, uint8 type, void *ref_type,
  452. char *error_buf, uint32 error_buf_size)
  453. {
  454. const uint8 *p = *p_buf, *p_end = buf_end;
  455. uint8 flag, end_byte, *p_float;
  456. uint32 i;
  457. #if WASM_ENABLE_GC != 0
  458. uint32 type_idx;
  459. #endif
  460. CHECK_BUF(p, p_end, 1);
  461. init_expr->init_expr_type = read_uint8(p);
  462. flag = init_expr->init_expr_type;
  463. switch (flag) {
  464. /* i32.const */
  465. case INIT_EXPR_TYPE_I32_CONST:
  466. read_leb_int32(p, p_end, init_expr->u.i32);
  467. #if WASM_ENABLE_GC == 0
  468. if (type != VALUE_TYPE_I32)
  469. goto fail_type_mismatch;
  470. #else
  471. if (type != VALUE_TYPE_I32) {
  472. if (!wasm_reftype_is_subtype_of(
  473. type, (WASMRefType *)ref_type, REF_TYPE_I31REF, NULL,
  474. module->types, module->type_count)) {
  475. goto fail_type_mismatch;
  476. }
  477. else {
  478. CHECK_BUF(p, p_end, 2);
  479. if (*p == WASM_OP_GC_PREFIX
  480. && *(p + 1) == WASM_OP_I31_NEW) {
  481. p += 2;
  482. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  483. }
  484. else
  485. goto fail_type_mismatch;
  486. }
  487. }
  488. #endif
  489. break;
  490. /* i64.const */
  491. case INIT_EXPR_TYPE_I64_CONST:
  492. if (type != VALUE_TYPE_I64)
  493. goto fail_type_mismatch;
  494. read_leb_int64(p, p_end, init_expr->u.i64);
  495. break;
  496. /* f32.const */
  497. case INIT_EXPR_TYPE_F32_CONST:
  498. if (type != VALUE_TYPE_F32)
  499. goto fail_type_mismatch;
  500. CHECK_BUF(p, p_end, 4);
  501. p_float = (uint8 *)&init_expr->u.f32;
  502. for (i = 0; i < sizeof(float32); i++)
  503. *p_float++ = *p++;
  504. break;
  505. /* f64.const */
  506. case INIT_EXPR_TYPE_F64_CONST:
  507. if (type != VALUE_TYPE_F64)
  508. goto fail_type_mismatch;
  509. CHECK_BUF(p, p_end, 8);
  510. p_float = (uint8 *)&init_expr->u.f64;
  511. for (i = 0; i < sizeof(float64); i++)
  512. *p_float++ = *p++;
  513. break;
  514. #if WASM_ENABLE_SIMD != 0
  515. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  516. /* v128.const */
  517. case INIT_EXPR_TYPE_V128_CONST:
  518. {
  519. uint64 high, low;
  520. if (type != VALUE_TYPE_V128)
  521. goto fail_type_mismatch;
  522. flag = read_uint8(p);
  523. (void)flag;
  524. CHECK_BUF(p, p_end, 16);
  525. wasm_runtime_read_v128(p, &high, &low);
  526. p += 16;
  527. init_expr->u.v128.i64x2[0] = high;
  528. init_expr->u.v128.i64x2[1] = low;
  529. break;
  530. }
  531. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  532. #endif /* end of WASM_ENABLE_SIMD */
  533. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  534. /* ref.func */
  535. case INIT_EXPR_TYPE_FUNCREF_CONST:
  536. {
  537. #if WASM_ENABLE_GC == 0
  538. if (type != VALUE_TYPE_FUNCREF)
  539. goto fail_type_mismatch;
  540. #else
  541. if (!wasm_reftype_is_subtype_of(type, (WASMRefType *)ref_type,
  542. VALUE_TYPE_FUNCREF, NULL,
  543. module->types, module->type_count))
  544. goto fail_type_mismatch;
  545. #endif
  546. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  547. if (!check_function_index(module, init_expr->u.ref_index, error_buf,
  548. error_buf_size)) {
  549. return false;
  550. }
  551. break;
  552. }
  553. /* ref.null */
  554. case INIT_EXPR_TYPE_REFNULL_CONST:
  555. {
  556. uint8 type1;
  557. #if WASM_ENABLE_GC == 0
  558. CHECK_BUF(p, p_end, 1);
  559. type1 = read_uint8(p);
  560. if (type1 != type)
  561. goto fail_type_mismatch;
  562. init_expr->u.ref_index = NULL_REF;
  563. #else
  564. WASMRefType ref_type1 = { 0 };
  565. type1 = read_uint8(p);
  566. if (!is_byte_a_type(type1)) {
  567. p--;
  568. read_leb_uint32(p, p_end, type_idx);
  569. if (!check_type_index(module, type_idx, error_buf,
  570. error_buf_size))
  571. return false;
  572. wasm_set_refheaptype_typeidx(&ref_type1.ref_ht_typeidx, false,
  573. type_idx);
  574. type1 = ref_type1.ref_type;
  575. }
  576. if (!wasm_reftype_is_subtype_of(type1, &ref_type1, type, ref_type,
  577. module->types,
  578. module->type_count)) {
  579. goto fail_type_mismatch;
  580. }
  581. /* Use UINT32_MAX to indicate that it is an null reference */
  582. init_expr->u.ref_index = UINT32_MAX;
  583. #endif
  584. break;
  585. }
  586. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  587. /* get_global */
  588. case INIT_EXPR_TYPE_GET_GLOBAL:
  589. {
  590. uint32 global_idx;
  591. read_leb_uint32(p, p_end, init_expr->u.global_index);
  592. global_idx = init_expr->u.global_index;
  593. #if WASM_ENABLE_GC == 0
  594. if (global_idx >= module->import_global_count) {
  595. /**
  596. * Currently, constant expressions occurring as initializers
  597. * of globals are further constrained in that contained
  598. * global.get instructions are
  599. * only allowed to refer to imported globals.
  600. */
  601. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  602. global_idx);
  603. return false;
  604. }
  605. if (module->import_globals[global_idx].u.global.is_mutable) {
  606. set_error_buf_v(error_buf, error_buf_size,
  607. "constant expression required");
  608. return false;
  609. }
  610. #else
  611. if (global_idx
  612. >= module->import_global_count + module->global_count) {
  613. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  614. global_idx);
  615. return false;
  616. }
  617. if (global_idx < module->import_global_count
  618. && module->import_globals[global_idx].u.global.is_mutable) {
  619. set_error_buf_v(error_buf, error_buf_size,
  620. "constant expression required");
  621. return false;
  622. }
  623. #endif
  624. break;
  625. }
  626. #if WASM_ENABLE_GC != 0
  627. /* struct.new and array.new */
  628. case WASM_OP_GC_PREFIX:
  629. {
  630. uint8 opcode1;
  631. CHECK_BUF(p, p_end, 1);
  632. opcode1 = read_uint8(p);
  633. switch (opcode1) {
  634. case WASM_OP_STRUCT_NEW_CANON:
  635. {
  636. set_error_buf(
  637. error_buf, error_buf_size,
  638. "unsuppoted constant expression of struct.new_canon");
  639. return false;
  640. }
  641. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  642. {
  643. init_expr->init_expr_type =
  644. INIT_EXPR_TYPE_STRUCT_NEW_CANON_DEFAULT;
  645. read_leb_uint32(p, p_end, init_expr->u.type_index);
  646. type_idx = init_expr->u.type_index;
  647. if (!check_type_index(module, type_idx, error_buf,
  648. error_buf_size)) {
  649. return false;
  650. }
  651. if (module->types[type_idx]->type_flag
  652. != WASM_TYPE_STRUCT) {
  653. set_error_buf(error_buf, error_buf_size,
  654. "unkown struct type");
  655. return false;
  656. }
  657. break;
  658. }
  659. case WASM_OP_ARRAY_NEW_CANON:
  660. {
  661. set_error_buf(
  662. error_buf, error_buf_size,
  663. "unsuppoted constant expression of array.new_canon");
  664. return false;
  665. }
  666. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  667. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  668. {
  669. init_expr->init_expr_type =
  670. (opcode1 == WASM_OP_ARRAY_NEW_CANON_DEFAULT)
  671. ? INIT_EXPR_TYPE_ARRAY_NEW_CANON_DEFAULT
  672. : INIT_EXPR_TYPE_ARRAY_NEW_CANON_FIXED;
  673. read_leb_uint32(p, p_end, init_expr->u.type_index);
  674. type_idx = init_expr->u.type_index;
  675. if (!check_array_type(module, type_idx, error_buf,
  676. error_buf_size)) {
  677. return false;
  678. }
  679. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  680. read_leb_uint32(p, p_end,
  681. init_expr->u.array_new_canon_fixed.N);
  682. }
  683. break;
  684. }
  685. case WASM_OP_EXTERN_INTERNALIZE:
  686. {
  687. set_error_buf(
  688. error_buf, error_buf_size,
  689. "unsuppoted constant expression of extern.internalize");
  690. return false;
  691. }
  692. case WASM_OP_EXTERN_EXTERNALIZE:
  693. {
  694. set_error_buf(
  695. error_buf, error_buf_size,
  696. "unsuppoted constant expression of extern.externalize");
  697. return false;
  698. }
  699. default:
  700. goto fail_type_mismatch;
  701. }
  702. break;
  703. }
  704. #endif /* end of WASM_ENABLE_GC != 0 */
  705. default:
  706. {
  707. set_error_buf(error_buf, error_buf_size,
  708. "illegal opcode "
  709. "or constant expression required "
  710. "or type mismatch");
  711. goto fail;
  712. }
  713. }
  714. CHECK_BUF(p, p_end, 1);
  715. end_byte = read_uint8(p);
  716. if (end_byte != 0x0b)
  717. goto fail_type_mismatch;
  718. *p_buf = p;
  719. return true;
  720. fail_type_mismatch:
  721. set_error_buf(error_buf, error_buf_size,
  722. "type mismatch or constant expression required");
  723. fail:
  724. return false;
  725. }
  726. static bool
  727. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  728. {
  729. if (mutable >= 2) {
  730. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  731. return false;
  732. }
  733. return true;
  734. }
  735. #if WASM_ENABLE_GC != 0
  736. static void
  737. destroy_func_type(WASMFuncType *type)
  738. {
  739. /* Destroy the reference type hash set */
  740. if (type->ref_type_maps)
  741. wasm_runtime_free(type->ref_type_maps);
  742. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  743. && WASM_ENABLE_LAZY_JIT != 0
  744. if (type->call_to_llvm_jit_from_fast_jit)
  745. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  746. #endif
  747. /* Free the type */
  748. wasm_runtime_free(type);
  749. }
  750. static void
  751. destroy_struct_type(WASMStructType *type)
  752. {
  753. if (type->ref_type_maps)
  754. wasm_runtime_free(type->ref_type_maps);
  755. wasm_runtime_free(type);
  756. }
  757. static void
  758. destroy_array_type(WASMArrayType *type)
  759. {
  760. wasm_runtime_free(type);
  761. }
  762. static void
  763. destroy_wasm_type(WASMType *type)
  764. {
  765. if (type->type_flag == WASM_TYPE_FUNC)
  766. destroy_func_type((WASMFuncType *)type);
  767. else if (type->type_flag == WASM_TYPE_STRUCT)
  768. destroy_struct_type((WASMStructType *)type);
  769. else if (type->type_flag == WASM_TYPE_ARRAY)
  770. destroy_array_type((WASMArrayType *)type);
  771. else {
  772. bh_assert(0);
  773. }
  774. }
  775. /* Resolve (ref null ht) or (ref ht) */
  776. static bool
  777. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  778. WASMModule *module, bool nullable, WASMRefType *ref_type,
  779. char *error_buf, uint32 error_buf_size)
  780. {
  781. const uint8 *p = *p_buf, *p_end = buf_end;
  782. ref_type->ref_type =
  783. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  784. ref_type->ref_ht_common.nullable = nullable;
  785. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  786. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  787. /* heap type is (type i), i : typeidx, >= 0 */
  788. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  789. error_buf, error_buf_size)) {
  790. return false;
  791. }
  792. }
  793. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  794. /* heap type is func, extern, any, eq, i31 or data */
  795. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  796. return false;
  797. }
  798. *p_buf = p;
  799. return true;
  800. fail:
  801. return false;
  802. }
  803. static bool
  804. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  805. WASMModule *module, bool *p_need_ref_type_map,
  806. WASMRefType *ref_type, bool allow_packed_type,
  807. char *error_buf, uint32 error_buf_size)
  808. {
  809. const uint8 *p = *p_buf, *p_end = buf_end;
  810. uint8 type;
  811. memset(ref_type, 0, sizeof(WASMRefType));
  812. CHECK_BUF(p, p_end, 1);
  813. type = read_uint8(p);
  814. if (wasm_is_reftype_htref_nullable(type)) {
  815. /* (ref null ht) */
  816. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  817. error_buf_size))
  818. return false;
  819. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  820. *p_need_ref_type_map = true;
  821. else {
  822. /* For (ref null func/extern/any/eq/i31/data), they are same as
  823. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  824. multi-byte type to one-byte type to reduce the footprint and
  825. the complexity of type equal/subtype checking */
  826. ref_type->ref_type =
  827. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  828. *p_need_ref_type_map = false;
  829. }
  830. }
  831. else if (wasm_is_reftype_htref_non_nullable(type)) {
  832. /* (ref ht) */
  833. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  834. error_buf, error_buf_size))
  835. return false;
  836. *p_need_ref_type_map = true;
  837. }
  838. else {
  839. /* type which can be represented by one byte */
  840. if (!is_value_type(type)
  841. && !(allow_packed_type && is_packed_type(type))) {
  842. set_error_buf(error_buf, error_buf_size, "type mismatch");
  843. return false;
  844. }
  845. ref_type->ref_type = type;
  846. *p_need_ref_type_map = false;
  847. }
  848. *p_buf = p;
  849. return true;
  850. fail:
  851. return false;
  852. }
  853. static WASMRefType *
  854. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  855. char *error_buf, uint32 error_buf_size)
  856. {
  857. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  858. if (!ret) {
  859. set_error_buf(error_buf, error_buf_size,
  860. "insert ref type to hash set failed");
  861. }
  862. return ret;
  863. }
  864. static bool
  865. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  866. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  867. {
  868. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  869. uint32 param_count, result_count, i, j = 0;
  870. uint32 param_cell_num, ret_cell_num;
  871. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  872. uint64 total_size;
  873. bool need_ref_type_map;
  874. WASMRefType ref_type;
  875. WASMFuncType *type = NULL;
  876. /* Parse first time to resolve param count, result count and
  877. ref type map count */
  878. read_leb_uint32(p, p_end, param_count);
  879. p_org = p;
  880. for (i = 0; i < param_count; i++) {
  881. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  882. &ref_type, false, error_buf, error_buf_size)) {
  883. return false;
  884. }
  885. if (need_ref_type_map)
  886. ref_type_map_count++;
  887. }
  888. read_leb_uint32(p, p_end, result_count);
  889. for (i = 0; i < result_count; i++) {
  890. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  891. &ref_type, false, error_buf, error_buf_size)) {
  892. return false;
  893. }
  894. if (need_ref_type_map) {
  895. ref_type_map_count++;
  896. result_ref_type_map_count++;
  897. }
  898. }
  899. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  900. "ref type map count: %d\n",
  901. type_idx, param_count, result_count, ref_type_map_count);
  902. /* Parse second time to resolve param types, result types and
  903. ref type map info */
  904. p = p_org;
  905. total_size = offsetof(WASMFuncType, types)
  906. + sizeof(uint8) * (uint64)(param_count + result_count);
  907. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  908. return false;
  909. }
  910. if (ref_type_map_count > 0) {
  911. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  912. if (!(type->ref_type_maps =
  913. loader_malloc(total_size, error_buf, error_buf_size))) {
  914. goto fail;
  915. }
  916. }
  917. type->base_type.type_flag = WASM_TYPE_FUNC;
  918. type->param_count = param_count;
  919. type->result_count = result_count;
  920. type->ref_type_map_count = ref_type_map_count;
  921. type->result_ref_type_maps =
  922. type->ref_type_maps + ref_type_map_count - result_ref_type_map_count;
  923. for (i = 0; i < param_count; i++) {
  924. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  925. &ref_type, false, error_buf, error_buf_size)) {
  926. goto fail;
  927. }
  928. type->types[i] = ref_type.ref_type;
  929. if (need_ref_type_map) {
  930. type->ref_type_maps[j].index = i;
  931. if (!(type->ref_type_maps[j++].ref_type =
  932. reftype_set_insert(module->ref_type_set, &ref_type,
  933. error_buf, error_buf_size))) {
  934. goto fail;
  935. }
  936. }
  937. }
  938. read_leb_uint32(p, p_end, result_count);
  939. for (i = 0; i < result_count; i++) {
  940. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  941. &ref_type, false, error_buf, error_buf_size)) {
  942. goto fail;
  943. }
  944. type->types[param_count + i] = ref_type.ref_type;
  945. if (need_ref_type_map) {
  946. type->ref_type_maps[j].index = param_count + i;
  947. if (!(type->ref_type_maps[j++].ref_type =
  948. reftype_set_insert(module->ref_type_set, &ref_type,
  949. error_buf, error_buf_size))) {
  950. goto fail;
  951. }
  952. }
  953. }
  954. bh_assert(j == type->ref_type_map_count);
  955. #if TRACE_WASM_LOADER != 0
  956. os_printf("type %d = ", type_idx);
  957. wasm_dump_func_type(type);
  958. #endif
  959. param_cell_num = wasm_get_cell_num(type->types, param_count);
  960. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  961. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  962. set_error_buf(error_buf, error_buf_size,
  963. "param count or result count too large");
  964. goto fail;
  965. }
  966. type->param_cell_num = (uint16)param_cell_num;
  967. type->ret_cell_num = (uint16)ret_cell_num;
  968. /* Calculate the minimal type index of the type equal to this type */
  969. type->min_type_idx_normalized = type_idx;
  970. for (i = 0; i < type_idx; i++) {
  971. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  972. if (func_type->base_type.type_flag == WASM_TYPE_FUNC
  973. && wasm_func_type_equal(type, func_type, module->types,
  974. type_idx + 1)) {
  975. type->min_type_idx_normalized = i;
  976. break;
  977. }
  978. }
  979. *p_buf = p;
  980. module->types[type_idx] = (WASMType *)type;
  981. return true;
  982. fail:
  983. if (type)
  984. destroy_func_type(type);
  985. return false;
  986. }
  987. static bool
  988. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  989. WASMModule *module, uint32 type_idx, char *error_buf,
  990. uint32 error_buf_size)
  991. {
  992. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  993. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  994. uint32 i, j = 0, offset;
  995. uint16 *reference_table;
  996. uint64 total_size;
  997. uint8 mutable;
  998. bool need_ref_type_map;
  999. WASMRefType ref_type;
  1000. WASMStructType *type = NULL;
  1001. /* Parse first time to resolve field count and ref type map count */
  1002. read_leb_uint32(p, p_end, field_count);
  1003. p_org = p;
  1004. for (i = 0; i < field_count; i++) {
  1005. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1006. &ref_type, true, error_buf, error_buf_size)) {
  1007. return false;
  1008. }
  1009. if (need_ref_type_map)
  1010. ref_type_map_count++;
  1011. if (wasm_is_type_reftype(ref_type.ref_type))
  1012. ref_field_count++;
  1013. CHECK_BUF(p, p_end, 1);
  1014. mutable = read_uint8(p);
  1015. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1016. return false;
  1017. }
  1018. }
  1019. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d\n",
  1020. type_idx, field_count, ref_type_map_count);
  1021. /* Parse second time to resolve field types and ref type map info */
  1022. p = p_org;
  1023. total_size = offsetof(WASMStructType, fields)
  1024. + sizeof(WASMStructFieldType) * (uint64)field_count
  1025. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1026. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1027. return false;
  1028. }
  1029. if (ref_type_map_count > 0) {
  1030. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1031. if (!(type->ref_type_maps =
  1032. loader_malloc(total_size, error_buf, error_buf_size))) {
  1033. goto fail;
  1034. }
  1035. }
  1036. type->reference_table = reference_table =
  1037. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1038. + sizeof(WASMStructFieldType) * field_count);
  1039. *reference_table++ = ref_field_count;
  1040. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1041. type->field_count = field_count;
  1042. type->ref_type_map_count = ref_type_map_count;
  1043. offset = (uint32)sizeof(WASMStructObject);
  1044. for (i = 0; i < field_count; i++) {
  1045. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1046. &ref_type, true, error_buf, error_buf_size)) {
  1047. goto fail;
  1048. }
  1049. type->fields[i].field_type = ref_type.ref_type;
  1050. if (need_ref_type_map) {
  1051. type->ref_type_maps[j].index = i;
  1052. if (!(type->ref_type_maps[j++].ref_type =
  1053. reftype_set_insert(module->ref_type_set, &ref_type,
  1054. error_buf, error_buf_size))) {
  1055. goto fail;
  1056. }
  1057. }
  1058. type->fields[i].field_flags = read_uint8(p);
  1059. type->fields[i].field_size =
  1060. (uint8)wasm_reftype_size(ref_type.ref_type);
  1061. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1062. || defined(BUILD_TARGET_X86_32))
  1063. if (type->fields[i].field_size == 2)
  1064. offset = align_uint(offset, 2);
  1065. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1066. offset = align_uint(offset, 4);
  1067. #endif
  1068. type->fields[i].field_offset = offset;
  1069. if (wasm_is_type_reftype(ref_type.ref_type))
  1070. *reference_table++ = offset;
  1071. offset += type->fields[i].field_size;
  1072. }
  1073. type->total_size = offset;
  1074. bh_assert(j == type->ref_type_map_count);
  1075. #if TRACE_WASM_LOADER != 0
  1076. os_printf("type %d = ", type_idx);
  1077. wasm_dump_struct_type(type);
  1078. #endif
  1079. *p_buf = p;
  1080. module->types[type_idx] = (WASMType *)type;
  1081. return true;
  1082. fail:
  1083. if (type)
  1084. destroy_struct_type(type);
  1085. return false;
  1086. }
  1087. static bool
  1088. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1089. WASMModule *module, uint32 type_idx, char *error_buf,
  1090. uint32 error_buf_size)
  1091. {
  1092. const uint8 *p = *p_buf, *p_end = buf_end;
  1093. uint8 mutable;
  1094. bool need_ref_type_map;
  1095. WASMRefType ref_type;
  1096. WASMArrayType *type = NULL;
  1097. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1098. true, error_buf, error_buf_size)) {
  1099. return false;
  1100. }
  1101. CHECK_BUF(p, p_end, 1);
  1102. mutable = read_uint8(p);
  1103. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1104. return false;
  1105. }
  1106. LOG_VERBOSE("type %u: array\n", type_idx);
  1107. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1108. error_buf_size))) {
  1109. return false;
  1110. }
  1111. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1112. type->elem_flags = mutable;
  1113. type->elem_type = ref_type.ref_type;
  1114. if (need_ref_type_map) {
  1115. if (!(type->elem_ref_type =
  1116. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1117. error_buf_size))) {
  1118. goto fail;
  1119. }
  1120. }
  1121. #if TRACE_WASM_LOADER != 0
  1122. os_printf("type %d = ", type_idx);
  1123. wasm_dump_array_type(type);
  1124. #endif
  1125. *p_buf = p;
  1126. module->types[type_idx] = (WASMType *)type;
  1127. return true;
  1128. fail:
  1129. if (type)
  1130. destroy_array_type(type);
  1131. return false;
  1132. }
  1133. #else /* else of WASM_ENABLE_GC != 0 */
  1134. static void
  1135. destroy_wasm_type(WASMType *type)
  1136. {
  1137. if (type->ref_count > 1) {
  1138. /* The type is referenced by other types
  1139. of current wasm module */
  1140. type->ref_count--;
  1141. return;
  1142. }
  1143. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1144. && WASM_ENABLE_LAZY_JIT != 0
  1145. if (type->call_to_llvm_jit_from_fast_jit)
  1146. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1147. #endif
  1148. wasm_runtime_free(type);
  1149. }
  1150. #endif /* end of WASM_ENABLE_GC != 0 */
  1151. static bool
  1152. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1153. char *error_buf, uint32 error_buf_size)
  1154. {
  1155. const uint8 *p = buf, *p_end = buf_end;
  1156. uint32 type_count, i;
  1157. uint64 total_size;
  1158. uint8 flag;
  1159. read_leb_uint32(p, p_end, type_count);
  1160. if (type_count) {
  1161. module->type_count = type_count;
  1162. total_size = sizeof(WASMFuncType *) * (uint64)type_count;
  1163. if (!(module->types =
  1164. loader_malloc(total_size, error_buf, error_buf_size))) {
  1165. return false;
  1166. }
  1167. #if WASM_ENABLE_GC == 0
  1168. for (i = 0; i < type_count; i++) {
  1169. WASMFuncType *type;
  1170. const uint8 *p_org;
  1171. uint32 param_count, result_count, j;
  1172. uint32 param_cell_num, ret_cell_num;
  1173. CHECK_BUF(p, p_end, 1);
  1174. flag = read_uint8(p);
  1175. if (flag != 0x60) {
  1176. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1177. return false;
  1178. }
  1179. read_leb_uint32(p, p_end, param_count);
  1180. /* Resolve param count and result count firstly */
  1181. p_org = p;
  1182. CHECK_BUF(p, p_end, param_count);
  1183. p += param_count;
  1184. read_leb_uint32(p, p_end, result_count);
  1185. CHECK_BUF(p, p_end, result_count);
  1186. p = p_org;
  1187. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1188. set_error_buf(error_buf, error_buf_size,
  1189. "param count or result count too large");
  1190. return false;
  1191. }
  1192. total_size = offsetof(WASMFuncType, types)
  1193. + sizeof(uint8) * (uint64)(param_count + result_count);
  1194. if (!(type = module->types[i] =
  1195. loader_malloc(total_size, error_buf, error_buf_size))) {
  1196. return false;
  1197. }
  1198. /* Resolve param types and result types */
  1199. type->ref_count = 1;
  1200. type->param_count = (uint16)param_count;
  1201. type->result_count = (uint16)result_count;
  1202. for (j = 0; j < param_count; j++) {
  1203. CHECK_BUF(p, p_end, 1);
  1204. type->types[j] = read_uint8(p);
  1205. }
  1206. read_leb_uint32(p, p_end, result_count);
  1207. for (j = 0; j < result_count; j++) {
  1208. CHECK_BUF(p, p_end, 1);
  1209. type->types[param_count + j] = read_uint8(p);
  1210. }
  1211. for (j = 0; j < param_count + result_count; j++) {
  1212. if (!is_value_type(type->types[j])) {
  1213. set_error_buf(error_buf, error_buf_size,
  1214. "unknown value type");
  1215. return false;
  1216. }
  1217. }
  1218. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1219. ret_cell_num =
  1220. wasm_get_cell_num(type->types + param_count, result_count);
  1221. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1222. set_error_buf(error_buf, error_buf_size,
  1223. "param count or result count too large");
  1224. return false;
  1225. }
  1226. type->param_cell_num = (uint16)param_cell_num;
  1227. type->ret_cell_num = (uint16)ret_cell_num;
  1228. /* If there is already a same type created, use it instead */
  1229. for (j = 0; j < i; j++) {
  1230. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1231. if (module->types[j]->ref_count == UINT16_MAX) {
  1232. set_error_buf(error_buf, error_buf_size,
  1233. "wasm type's ref count too large");
  1234. return false;
  1235. }
  1236. destroy_wasm_type(type);
  1237. module->types[i] = module->types[j];
  1238. module->types[j]->ref_count++;
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. #else /* else of WASM_ENABLE_GC == 0 */
  1244. for (i = 0; i < type_count; i++) {
  1245. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1246. bool is_sub_final = true;
  1247. CHECK_BUF(p, p_end, 1);
  1248. flag = read_uint8(p);
  1249. if (flag == DEFINED_TYPE_SUB || flag == DEFINED_TYPE_SUB_FINAL) {
  1250. read_leb_uint32(p, p_end, super_type_count);
  1251. if (super_type_count > 1) {
  1252. set_error_buf(error_buf, error_buf_size,
  1253. "super type count too large");
  1254. return false;
  1255. }
  1256. if (super_type_count > 0) {
  1257. read_leb_uint32(p, p_end, parent_type_idx);
  1258. if (parent_type_idx >= i) {
  1259. set_error_buf_v(error_buf, error_buf_size,
  1260. "unknown type %d", parent_type_idx);
  1261. return false;
  1262. }
  1263. if (module->types[parent_type_idx]->is_sub_final) {
  1264. set_error_buf(error_buf, error_buf_size,
  1265. "sub type can not inherit from "
  1266. "a final super type");
  1267. return false;
  1268. }
  1269. }
  1270. if (flag == DEFINED_TYPE_SUB)
  1271. is_sub_final = false;
  1272. CHECK_BUF(p, p_end, 1);
  1273. flag = read_uint8(p);
  1274. }
  1275. #if WASM_ENABLE_GC_BINARYEN != 0
  1276. else {
  1277. is_sub_final = false;
  1278. }
  1279. #endif
  1280. if (flag == DEFINED_TYPE_FUNC) {
  1281. if (!resolve_func_type(&p, buf_end, module, i, error_buf,
  1282. error_buf_size)) {
  1283. return false;
  1284. }
  1285. }
  1286. else if (flag == DEFINED_TYPE_STRUCT) {
  1287. if (!resolve_struct_type(&p, buf_end, module, i, error_buf,
  1288. error_buf_size)) {
  1289. return false;
  1290. }
  1291. }
  1292. else if (flag == DEFINED_TYPE_ARRAY) {
  1293. if (!resolve_array_type(&p, buf_end, module, i, error_buf,
  1294. error_buf_size)) {
  1295. return false;
  1296. }
  1297. }
  1298. else {
  1299. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1300. return false;
  1301. }
  1302. if (parent_type_idx != (uint32)-1) { /* has parent */
  1303. WASMType *parent_type = module->types[parent_type_idx];
  1304. if (!wasm_type_is_subtype_of(module->types[i], parent_type,
  1305. module->types, i)) {
  1306. set_error_buf(error_buf, error_buf_size,
  1307. "sub type does not match super type");
  1308. return false;
  1309. }
  1310. module->types[i]->parent_type = parent_type;
  1311. module->types[i]->root_type = parent_type->root_type;
  1312. module->types[i]->inherit_depth =
  1313. parent_type->inherit_depth + 1;
  1314. }
  1315. else {
  1316. module->types[i]->parent_type = NULL;
  1317. module->types[i]->root_type = module->types[i];
  1318. module->types[i]->inherit_depth = 0;
  1319. }
  1320. module->types[i]->parent_type_idx = parent_type_idx;
  1321. module->types[i]->is_sub_final = is_sub_final;
  1322. }
  1323. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1324. * module->type_count,
  1325. error_buf, error_buf_size))) {
  1326. return false;
  1327. }
  1328. #endif /* end of WASM_ENABLE_GC == 0 */
  1329. }
  1330. if (p != p_end) {
  1331. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1332. return false;
  1333. }
  1334. LOG_VERBOSE("Load type section success.\n");
  1335. return true;
  1336. fail:
  1337. return false;
  1338. }
  1339. static void
  1340. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1341. {
  1342. uint32 default_max_size =
  1343. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  1344. if (max_size_flag) {
  1345. /* module defines the table limitation */
  1346. bh_assert(init_size <= *max_size);
  1347. if (init_size < *max_size) {
  1348. *max_size =
  1349. *max_size < default_max_size ? *max_size : default_max_size;
  1350. }
  1351. }
  1352. else {
  1353. /* partial defined table limitation, gives a default value */
  1354. *max_size = default_max_size;
  1355. }
  1356. }
  1357. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1358. /**
  1359. * Find export item of a module with export info:
  1360. * module name, field name and export kind
  1361. */
  1362. static WASMExport *
  1363. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1364. const char *field_name, uint8 export_kind,
  1365. char *error_buf, uint32 error_buf_size)
  1366. {
  1367. WASMExport *export;
  1368. uint32 i;
  1369. for (i = 0, export = module->exports; i < module->export_count;
  1370. ++i, ++export) {
  1371. /**
  1372. * need to consider a scenario that different kinds of exports
  1373. * may have the same name, like
  1374. * (table (export "m1" "exported") 10 funcref)
  1375. * (memory (export "m1" "exported") 10)
  1376. **/
  1377. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  1378. break;
  1379. }
  1380. }
  1381. if (i == module->export_count) {
  1382. LOG_DEBUG("can not find an export %d named %s in the module %s",
  1383. export_kind, field_name, module_name);
  1384. set_error_buf(error_buf, error_buf_size,
  1385. "unknown import or incompatible import type");
  1386. return NULL;
  1387. }
  1388. (void)module_name;
  1389. /* since there is a validation in load_export_section(), it is for sure
  1390. * export->index is valid*/
  1391. return export;
  1392. }
  1393. #endif
  1394. #if WASM_ENABLE_MULTI_MODULE != 0
  1395. static WASMFunction *
  1396. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1397. const WASMFuncType *expected_function_type,
  1398. char *error_buf, uint32 error_buf_size)
  1399. {
  1400. WASMModuleCommon *module_reg;
  1401. WASMFunction *function = NULL;
  1402. WASMExport *export = NULL;
  1403. WASMModule *module = NULL;
  1404. WASMFuncType *target_function_type = NULL;
  1405. module_reg = wasm_runtime_find_module_registered(module_name);
  1406. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1407. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1408. function_name);
  1409. set_error_buf(error_buf, error_buf_size, "unknown import");
  1410. return NULL;
  1411. }
  1412. module = (WASMModule *)module_reg;
  1413. export =
  1414. wasm_loader_find_export(module, module_name, function_name,
  1415. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  1416. if (!export) {
  1417. return NULL;
  1418. }
  1419. /* resolve function type and function */
  1420. if (export->index < module->import_function_count) {
  1421. target_function_type =
  1422. module->import_functions[export->index].u.function.func_type;
  1423. function = module->import_functions[export->index]
  1424. .u.function.import_func_linked;
  1425. }
  1426. else {
  1427. target_function_type =
  1428. module->functions[export->index - module->import_function_count]
  1429. ->func_type;
  1430. function =
  1431. module->functions[export->index - module->import_function_count];
  1432. }
  1433. /* check function type */
  1434. if (!wasm_type_equal((WASMType *)expected_function_type,
  1435. (WASMType *)target_function_type, module->types,
  1436. module->type_count)) {
  1437. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  1438. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1439. return NULL;
  1440. }
  1441. return function;
  1442. }
  1443. static WASMTable *
  1444. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  1445. uint32 init_size, uint32 max_size, char *error_buf,
  1446. uint32 error_buf_size)
  1447. {
  1448. WASMModuleCommon *module_reg;
  1449. WASMTable *table = NULL;
  1450. WASMExport *export = NULL;
  1451. WASMModule *module = NULL;
  1452. module_reg = wasm_runtime_find_module_registered(module_name);
  1453. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1454. LOG_DEBUG("can not find a module named %s for table", module_name);
  1455. set_error_buf(error_buf, error_buf_size, "unknown import");
  1456. return NULL;
  1457. }
  1458. module = (WASMModule *)module_reg;
  1459. export =
  1460. wasm_loader_find_export(module, module_name, table_name,
  1461. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  1462. if (!export) {
  1463. return NULL;
  1464. }
  1465. /* resolve table and check the init/max size */
  1466. if (export->index < module->import_table_count) {
  1467. table =
  1468. module->import_tables[export->index].u.table.import_table_linked;
  1469. }
  1470. else {
  1471. table = &(module->tables[export->index - module->import_table_count]);
  1472. }
  1473. if (table->init_size < init_size || table->max_size > max_size) {
  1474. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1475. module_name, table_name, table->init_size, table->max_size,
  1476. init_size, max_size);
  1477. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1478. return NULL;
  1479. }
  1480. return table;
  1481. }
  1482. static WASMMemory *
  1483. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  1484. uint32 init_page_count, uint32 max_page_count,
  1485. char *error_buf, uint32 error_buf_size)
  1486. {
  1487. WASMModuleCommon *module_reg;
  1488. WASMMemory *memory = NULL;
  1489. WASMExport *export = NULL;
  1490. WASMModule *module = NULL;
  1491. module_reg = wasm_runtime_find_module_registered(module_name);
  1492. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1493. LOG_DEBUG("can not find a module named %s for memory", module_name);
  1494. set_error_buf(error_buf, error_buf_size, "unknown import");
  1495. return NULL;
  1496. }
  1497. module = (WASMModule *)module_reg;
  1498. export =
  1499. wasm_loader_find_export(module, module_name, memory_name,
  1500. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  1501. if (!export) {
  1502. return NULL;
  1503. }
  1504. /* resolve memory and check the init/max page count */
  1505. if (export->index < module->import_memory_count) {
  1506. memory = module->import_memories[export->index]
  1507. .u.memory.import_memory_linked;
  1508. }
  1509. else {
  1510. memory =
  1511. &(module->memories[export->index - module->import_memory_count]);
  1512. }
  1513. if (memory->init_page_count < init_page_count
  1514. || memory->max_page_count > max_page_count) {
  1515. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1516. module_name, memory_name, memory->init_page_count,
  1517. memory->max_page_count, init_page_count, max_page_count);
  1518. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1519. return NULL;
  1520. }
  1521. return memory;
  1522. }
  1523. static WASMGlobal *
  1524. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  1525. uint8 type, bool is_mutable, char *error_buf,
  1526. uint32 error_buf_size)
  1527. {
  1528. WASMModuleCommon *module_reg;
  1529. WASMGlobal *global = NULL;
  1530. WASMExport *export = NULL;
  1531. WASMModule *module = NULL;
  1532. module_reg = wasm_runtime_find_module_registered(module_name);
  1533. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1534. LOG_DEBUG("can not find a module named %s for global", module_name);
  1535. set_error_buf(error_buf, error_buf_size, "unknown import");
  1536. return NULL;
  1537. }
  1538. module = (WASMModule *)module_reg;
  1539. export =
  1540. wasm_loader_find_export(module, module_name, global_name,
  1541. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  1542. if (!export) {
  1543. return NULL;
  1544. }
  1545. /* resolve and check the global */
  1546. if (export->index < module->import_global_count) {
  1547. global =
  1548. module->import_globals[export->index].u.global.import_global_linked;
  1549. }
  1550. else {
  1551. global =
  1552. &(module->globals[export->index - module->import_global_count]);
  1553. }
  1554. if (global->type != type || global->is_mutable != is_mutable) {
  1555. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  1556. module_name, global_name, global->type, global->is_mutable,
  1557. type, is_mutable);
  1558. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1559. return NULL;
  1560. }
  1561. return global;
  1562. }
  1563. static WASMModule *
  1564. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  1565. {
  1566. WASMRegisteredModule *node =
  1567. bh_list_first_elem(parent_module->import_module_list);
  1568. while (node && strcmp(sub_module_name, node->module_name)) {
  1569. node = bh_list_elem_next(node);
  1570. }
  1571. return node ? (WASMModule *)node->module : NULL;
  1572. }
  1573. static bool
  1574. register_sub_module(const WASMModule *parent_module,
  1575. const char *sub_module_name, WASMModule *sub_module)
  1576. {
  1577. /* register sub_module into its parent sub module list */
  1578. WASMRegisteredModule *node = NULL;
  1579. bh_list_status ret;
  1580. if (search_sub_module(parent_module, sub_module_name)) {
  1581. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  1582. return true;
  1583. }
  1584. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  1585. if (!node) {
  1586. return false;
  1587. }
  1588. node->module_name = sub_module_name;
  1589. node->module = (WASMModuleCommon *)sub_module;
  1590. ret = bh_list_insert(parent_module->import_module_list, node);
  1591. bh_assert(BH_LIST_SUCCESS == ret);
  1592. (void)ret;
  1593. return true;
  1594. }
  1595. static WASMModule *
  1596. load_depended_module(const WASMModule *parent_module,
  1597. const char *sub_module_name, char *error_buf,
  1598. uint32 error_buf_size)
  1599. {
  1600. WASMModule *sub_module = NULL;
  1601. bool ret = false;
  1602. uint8 *buffer = NULL;
  1603. uint32 buffer_size = 0;
  1604. const module_reader reader = wasm_runtime_get_module_reader();
  1605. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  1606. /* check the registered module list of the parent */
  1607. sub_module = search_sub_module(parent_module, sub_module_name);
  1608. if (sub_module) {
  1609. LOG_DEBUG("%s has been loaded before", sub_module_name);
  1610. return sub_module;
  1611. }
  1612. /* check the global registered module list */
  1613. sub_module =
  1614. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  1615. if (sub_module) {
  1616. LOG_DEBUG("%s has been loaded", sub_module_name);
  1617. goto register_sub_module;
  1618. }
  1619. LOG_VERBOSE("loading %s", sub_module_name);
  1620. if (!reader) {
  1621. set_error_buf_v(error_buf, error_buf_size,
  1622. "no sub module reader to load %s", sub_module_name);
  1623. return NULL;
  1624. }
  1625. /* start to maintain a loading module list */
  1626. ret = wasm_runtime_is_loading_module(sub_module_name);
  1627. if (ret) {
  1628. set_error_buf_v(error_buf, error_buf_size,
  1629. "found circular dependency on %s", sub_module_name);
  1630. return NULL;
  1631. }
  1632. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  1633. error_buf_size);
  1634. if (!ret) {
  1635. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  1636. return NULL;
  1637. }
  1638. ret = reader(sub_module_name, &buffer, &buffer_size);
  1639. if (!ret) {
  1640. LOG_DEBUG("read the file of %s failed", sub_module_name);
  1641. set_error_buf_v(error_buf, error_buf_size, "unknown import",
  1642. sub_module_name);
  1643. goto delete_loading_module;
  1644. }
  1645. sub_module =
  1646. wasm_loader_load(buffer, buffer_size, false, error_buf, error_buf_size);
  1647. if (!sub_module) {
  1648. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  1649. /* others will be destroyed in runtime_destroy() */
  1650. goto destroy_file_buffer;
  1651. }
  1652. wasm_runtime_delete_loading_module(sub_module_name);
  1653. /* register on a global list */
  1654. ret = wasm_runtime_register_module_internal(
  1655. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  1656. error_buf, error_buf_size);
  1657. if (!ret) {
  1658. LOG_DEBUG("error: can not register module %s globally\n",
  1659. sub_module_name);
  1660. /* others will be unloaded in runtime_destroy() */
  1661. goto unload_module;
  1662. }
  1663. /* register into its parent list */
  1664. register_sub_module:
  1665. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  1666. if (!ret) {
  1667. set_error_buf_v(error_buf, error_buf_size,
  1668. "failed to register sub module %s", sub_module_name);
  1669. /* since it is in the global module list, no need to
  1670. * unload the module. the runtime_destroy() will do it
  1671. */
  1672. return NULL;
  1673. }
  1674. return sub_module;
  1675. unload_module:
  1676. wasm_loader_unload(sub_module);
  1677. destroy_file_buffer:
  1678. if (destroyer) {
  1679. destroyer(buffer, buffer_size);
  1680. }
  1681. else {
  1682. LOG_WARNING("need to release the reading buffer of %s manually",
  1683. sub_module_name);
  1684. }
  1685. delete_loading_module:
  1686. wasm_runtime_delete_loading_module(sub_module_name);
  1687. return NULL;
  1688. }
  1689. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  1690. static bool
  1691. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  1692. const WASMModule *parent_module,
  1693. const char *sub_module_name, const char *function_name,
  1694. WASMFunctionImport *function, char *error_buf,
  1695. uint32 error_buf_size)
  1696. {
  1697. const uint8 *p = *p_buf, *p_end = buf_end;
  1698. uint32 declare_type_index = 0;
  1699. WASMFuncType *declare_func_type = NULL;
  1700. WASMFunction *linked_func = NULL;
  1701. #if WASM_ENABLE_MULTI_MODULE != 0
  1702. WASMModule *sub_module = NULL;
  1703. #endif
  1704. const char *linked_signature = NULL;
  1705. void *linked_attachment = NULL;
  1706. bool linked_call_conv_raw = false;
  1707. bool is_native_symbol = false;
  1708. read_leb_uint32(p, p_end, declare_type_index);
  1709. *p_buf = p;
  1710. if (declare_type_index >= parent_module->type_count) {
  1711. set_error_buf(error_buf, error_buf_size, "unknown type");
  1712. return false;
  1713. }
  1714. #if WASM_ENABLE_GC != 0
  1715. function->type_idx = declare_type_index;
  1716. #endif
  1717. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1718. declare_type_index = wasm_get_smallest_type_idx(
  1719. parent_module->types, parent_module->type_count, declare_type_index);
  1720. #endif
  1721. declare_func_type =
  1722. (WASMFuncType *)parent_module->types[declare_type_index];
  1723. /* lookup registered native symbols first */
  1724. linked_func = wasm_native_resolve_symbol(
  1725. sub_module_name, function_name, declare_func_type, &linked_signature,
  1726. &linked_attachment, &linked_call_conv_raw);
  1727. if (linked_func) {
  1728. is_native_symbol = true;
  1729. }
  1730. #if WASM_ENABLE_MULTI_MODULE != 0
  1731. else {
  1732. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1733. sub_module = load_depended_module(parent_module, sub_module_name,
  1734. error_buf, error_buf_size);
  1735. if (!sub_module) {
  1736. return false;
  1737. }
  1738. }
  1739. linked_func = wasm_loader_resolve_function(
  1740. sub_module_name, function_name, declare_func_type, error_buf,
  1741. error_buf_size);
  1742. }
  1743. #endif
  1744. function->module_name = (char *)sub_module_name;
  1745. function->field_name = (char *)function_name;
  1746. function->func_type = declare_func_type;
  1747. /* func_ptr_linked is for native registered symbol */
  1748. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  1749. function->signature = linked_signature;
  1750. function->attachment = linked_attachment;
  1751. function->call_conv_raw = linked_call_conv_raw;
  1752. #if WASM_ENABLE_MULTI_MODULE != 0
  1753. function->import_module = is_native_symbol ? NULL : sub_module;
  1754. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  1755. #endif
  1756. return true;
  1757. fail:
  1758. return false;
  1759. }
  1760. static bool
  1761. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1762. uint32 error_buf_size)
  1763. {
  1764. if (max_size < init_size) {
  1765. set_error_buf(error_buf, error_buf_size,
  1766. "size minimum must not be greater than maximum");
  1767. return false;
  1768. }
  1769. return true;
  1770. }
  1771. static bool
  1772. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  1773. WASMModule *parent_module, const char *sub_module_name,
  1774. const char *table_name, WASMTableImport *table,
  1775. char *error_buf, uint32 error_buf_size)
  1776. {
  1777. const uint8 *p = *p_buf, *p_end = buf_end;
  1778. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  1779. declare_init_size = 0, declare_max_size = 0;
  1780. #if WASM_ENABLE_MULTI_MODULE != 0
  1781. WASMModule *sub_module = NULL;
  1782. WASMTable *linked_table = NULL;
  1783. #endif
  1784. #if WASM_ENABLE_GC != 0
  1785. WASMRefType ref_type;
  1786. bool need_ref_type_map;
  1787. #endif
  1788. #if WASM_ENABLE_GC == 0
  1789. CHECK_BUF(p, p_end, 1);
  1790. /* 0x70 or 0x6F */
  1791. declare_elem_type = read_uint8(p);
  1792. if (VALUE_TYPE_FUNCREF != declare_elem_type
  1793. #if WASM_ENABLE_REF_TYPES != 0
  1794. && VALUE_TYPE_EXTERNREF != declare_elem_type
  1795. #endif
  1796. ) {
  1797. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1798. return false;
  1799. }
  1800. #else /* else of WASM_ENABLE_GC == 0 */
  1801. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  1802. &ref_type, false, error_buf, error_buf_size)) {
  1803. return false;
  1804. }
  1805. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  1806. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1807. return false;
  1808. }
  1809. declare_elem_type = ref_type.ref_type;
  1810. if (need_ref_type_map) {
  1811. if (!(table->elem_ref_type =
  1812. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  1813. error_buf, error_buf_size))) {
  1814. return false;
  1815. }
  1816. }
  1817. #if TRACE_WASM_LOADER != 0
  1818. os_printf("import table type: ");
  1819. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  1820. os_printf("\n");
  1821. #endif
  1822. #endif /* end of WASM_ENABLE_GC == 0 */
  1823. read_leb_uint32(p, p_end, declare_max_size_flag);
  1824. if (declare_max_size_flag > 1) {
  1825. set_error_buf(error_buf, error_buf_size, "integer too large");
  1826. return false;
  1827. }
  1828. read_leb_uint32(p, p_end, declare_init_size);
  1829. if (declare_max_size_flag) {
  1830. read_leb_uint32(p, p_end, declare_max_size);
  1831. if (!check_table_max_size(declare_init_size, declare_max_size,
  1832. error_buf, error_buf_size))
  1833. return false;
  1834. }
  1835. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1836. &declare_max_size);
  1837. *p_buf = p;
  1838. #if WASM_ENABLE_MULTI_MODULE != 0
  1839. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1840. sub_module = load_depended_module(parent_module, sub_module_name,
  1841. error_buf, error_buf_size);
  1842. if (!sub_module) {
  1843. return false;
  1844. }
  1845. linked_table = wasm_loader_resolve_table(
  1846. sub_module_name, table_name, declare_init_size, declare_max_size,
  1847. error_buf, error_buf_size);
  1848. if (!linked_table) {
  1849. return false;
  1850. }
  1851. /* reset with linked table limit */
  1852. declare_elem_type = linked_table->elem_type;
  1853. declare_init_size = linked_table->init_size;
  1854. declare_max_size = linked_table->max_size;
  1855. declare_max_size_flag = linked_table->flags;
  1856. table->import_table_linked = linked_table;
  1857. table->import_module = sub_module;
  1858. }
  1859. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1860. /* (table (export "table") 10 20 funcref) */
  1861. /* we need this section working in wamrc */
  1862. if (!strcmp("spectest", sub_module_name)) {
  1863. const uint32 spectest_table_init_size = 10;
  1864. const uint32 spectest_table_max_size = 20;
  1865. if (strcmp("table", table_name)) {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "incompatible import type or unknown import");
  1868. return false;
  1869. }
  1870. if (declare_init_size > spectest_table_init_size
  1871. || declare_max_size < spectest_table_max_size) {
  1872. set_error_buf(error_buf, error_buf_size,
  1873. "incompatible import type");
  1874. return false;
  1875. }
  1876. declare_init_size = spectest_table_init_size;
  1877. declare_max_size = spectest_table_max_size;
  1878. }
  1879. /* now we believe all declaration are ok */
  1880. table->elem_type = declare_elem_type;
  1881. table->init_size = declare_init_size;
  1882. table->flags = declare_max_size_flag;
  1883. table->max_size = declare_max_size;
  1884. (void)parent_module;
  1885. return true;
  1886. fail:
  1887. return false;
  1888. }
  1889. static bool
  1890. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1891. {
  1892. if (init_size > DEFAULT_MAX_PAGES) {
  1893. set_error_buf(error_buf, error_buf_size,
  1894. "memory size must be at most 65536 pages (4GiB)");
  1895. return false;
  1896. }
  1897. return true;
  1898. }
  1899. static bool
  1900. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1901. uint32 error_buf_size)
  1902. {
  1903. if (max_size < init_size) {
  1904. set_error_buf(error_buf, error_buf_size,
  1905. "size minimum must not be greater than maximum");
  1906. return false;
  1907. }
  1908. if (max_size > DEFAULT_MAX_PAGES) {
  1909. set_error_buf(error_buf, error_buf_size,
  1910. "memory size must be at most 65536 pages (4GiB)");
  1911. return false;
  1912. }
  1913. return true;
  1914. }
  1915. static bool
  1916. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1917. WASMModule *parent_module, const char *sub_module_name,
  1918. const char *memory_name, WASMMemoryImport *memory,
  1919. char *error_buf, uint32 error_buf_size)
  1920. {
  1921. const uint8 *p = *p_buf, *p_end = buf_end;
  1922. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1923. uint32 pool_size = wasm_runtime_memory_pool_size();
  1924. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1925. / DEFAULT_NUM_BYTES_PER_PAGE;
  1926. #else
  1927. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1928. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1929. uint32 declare_max_page_count_flag = 0;
  1930. uint32 declare_init_page_count = 0;
  1931. uint32 declare_max_page_count = 0;
  1932. #if WASM_ENABLE_MULTI_MODULE != 0
  1933. WASMModule *sub_module = NULL;
  1934. WASMMemory *linked_memory = NULL;
  1935. #endif
  1936. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1937. read_leb_uint32(p, p_end, declare_init_page_count);
  1938. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1939. error_buf_size)) {
  1940. return false;
  1941. }
  1942. if (declare_max_page_count_flag & 1) {
  1943. read_leb_uint32(p, p_end, declare_max_page_count);
  1944. if (!check_memory_max_size(declare_init_page_count,
  1945. declare_max_page_count, error_buf,
  1946. error_buf_size)) {
  1947. return false;
  1948. }
  1949. if (declare_max_page_count > max_page_count) {
  1950. declare_max_page_count = max_page_count;
  1951. }
  1952. }
  1953. else {
  1954. /* Limit the maximum memory size to max_page_count */
  1955. declare_max_page_count = max_page_count;
  1956. }
  1957. #if WASM_ENABLE_MULTI_MODULE != 0
  1958. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1959. sub_module = load_depended_module(parent_module, sub_module_name,
  1960. error_buf, error_buf_size);
  1961. if (!sub_module) {
  1962. return false;
  1963. }
  1964. linked_memory = wasm_loader_resolve_memory(
  1965. sub_module_name, memory_name, declare_init_page_count,
  1966. declare_max_page_count, error_buf, error_buf_size);
  1967. if (!linked_memory) {
  1968. return false;
  1969. }
  1970. /**
  1971. * reset with linked memory limit
  1972. */
  1973. memory->import_module = sub_module;
  1974. memory->import_memory_linked = linked_memory;
  1975. declare_init_page_count = linked_memory->init_page_count;
  1976. declare_max_page_count = linked_memory->max_page_count;
  1977. }
  1978. #endif
  1979. /* (memory (export "memory") 1 2) */
  1980. if (!strcmp("spectest", sub_module_name)) {
  1981. uint32 spectest_memory_init_page = 1;
  1982. uint32 spectest_memory_max_page = 2;
  1983. if (strcmp("memory", memory_name)) {
  1984. set_error_buf(error_buf, error_buf_size,
  1985. "incompatible import type or unknown import");
  1986. return false;
  1987. }
  1988. if (declare_init_page_count > spectest_memory_init_page
  1989. || declare_max_page_count < spectest_memory_max_page) {
  1990. set_error_buf(error_buf, error_buf_size,
  1991. "incompatible import type");
  1992. return false;
  1993. }
  1994. declare_init_page_count = spectest_memory_init_page;
  1995. declare_max_page_count = spectest_memory_max_page;
  1996. }
  1997. /* now we believe all declaration are ok */
  1998. memory->flags = declare_max_page_count_flag;
  1999. memory->init_page_count = declare_init_page_count;
  2000. memory->max_page_count = declare_max_page_count;
  2001. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2002. *p_buf = p;
  2003. (void)parent_module;
  2004. return true;
  2005. fail:
  2006. return false;
  2007. }
  2008. static bool
  2009. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2010. WASMModule *parent_module, char *sub_module_name,
  2011. char *global_name, WASMGlobalImport *global, char *error_buf,
  2012. uint32 error_buf_size)
  2013. {
  2014. const uint8 *p = *p_buf, *p_end = buf_end;
  2015. uint8 declare_type = 0;
  2016. uint8 declare_mutable = 0;
  2017. #if WASM_ENABLE_MULTI_MODULE != 0
  2018. WASMModule *sub_module = NULL;
  2019. WASMGlobal *linked_global = NULL;
  2020. #endif
  2021. #if WASM_ENABLE_GC != 0
  2022. WASMRefType ref_type;
  2023. bool need_ref_type_map;
  2024. #endif
  2025. bool ret = false;
  2026. #if WASM_ENABLE_GC == 0
  2027. CHECK_BUF(p, p_end, 2);
  2028. declare_type = read_uint8(p);
  2029. declare_mutable = read_uint8(p);
  2030. #else
  2031. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2032. &ref_type, false, error_buf, error_buf_size)) {
  2033. return false;
  2034. }
  2035. declare_type = ref_type.ref_type;
  2036. if (need_ref_type_map) {
  2037. if (!(global->ref_type =
  2038. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2039. error_buf, error_buf_size))) {
  2040. return false;
  2041. }
  2042. }
  2043. #if TRACE_WASM_LOADER != 0
  2044. os_printf("import global type: ");
  2045. wasm_dump_value_type(declare_type, global->ref_type);
  2046. os_printf("\n");
  2047. #endif
  2048. CHECK_BUF(p, p_end, 1);
  2049. declare_mutable = read_uint8(p);
  2050. #endif /* end of WASM_ENABLE_GC == 0 */
  2051. *p_buf = p;
  2052. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2053. return false;
  2054. }
  2055. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2056. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2057. global);
  2058. if (ret) {
  2059. if (global->type != declare_type
  2060. || global->is_mutable != declare_mutable) {
  2061. set_error_buf(error_buf, error_buf_size,
  2062. "incompatible import type");
  2063. return false;
  2064. }
  2065. global->is_linked = true;
  2066. }
  2067. #endif
  2068. #if WASM_ENABLE_MULTI_MODULE != 0
  2069. if (!global->is_linked
  2070. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2071. sub_module = load_depended_module(parent_module, sub_module_name,
  2072. error_buf, error_buf_size);
  2073. if (!sub_module) {
  2074. return false;
  2075. }
  2076. /* check sub modules */
  2077. linked_global = wasm_loader_resolve_global(
  2078. sub_module_name, global_name, declare_type, declare_mutable,
  2079. error_buf, error_buf_size);
  2080. if (linked_global) {
  2081. global->import_module = sub_module;
  2082. global->import_global_linked = linked_global;
  2083. global->is_linked = true;
  2084. }
  2085. }
  2086. #endif
  2087. global->module_name = sub_module_name;
  2088. global->field_name = global_name;
  2089. global->type = declare_type;
  2090. global->is_mutable = (declare_mutable == 1);
  2091. (void)parent_module;
  2092. (void)ret;
  2093. return true;
  2094. fail:
  2095. return false;
  2096. }
  2097. static bool
  2098. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2099. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2100. {
  2101. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2102. #if WASM_ENABLE_GC != 0
  2103. WASMRefType ref_type;
  2104. bool need_ref_type_map;
  2105. #endif
  2106. #if WASM_ENABLE_GC == 0
  2107. CHECK_BUF(p, p_end, 1);
  2108. /* 0x70 or 0x6F */
  2109. table->elem_type = read_uint8(p);
  2110. if (VALUE_TYPE_FUNCREF != table->elem_type
  2111. #if WASM_ENABLE_REF_TYPES != 0
  2112. && VALUE_TYPE_EXTERNREF != table->elem_type
  2113. #endif
  2114. ) {
  2115. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2116. return false;
  2117. }
  2118. #else /* else of WASM_ENABLE_GC == 0 */
  2119. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  2120. false, error_buf, error_buf_size)) {
  2121. return false;
  2122. }
  2123. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2124. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2125. return false;
  2126. }
  2127. table->elem_type = ref_type.ref_type;
  2128. if (need_ref_type_map) {
  2129. if (!(table->elem_ref_type =
  2130. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2131. error_buf_size))) {
  2132. return false;
  2133. }
  2134. }
  2135. #if TRACE_WASM_LOADER != 0
  2136. os_printf("table type: ");
  2137. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2138. os_printf("\n");
  2139. #endif
  2140. #endif /* end of WASM_ENABLE_GC == 0 */
  2141. p_org = p;
  2142. read_leb_uint32(p, p_end, table->flags);
  2143. #if WASM_ENABLE_SHARED_MEMORY == 0
  2144. if (p - p_org > 1) {
  2145. set_error_buf(error_buf, error_buf_size,
  2146. "integer representation too long");
  2147. return false;
  2148. }
  2149. if (table->flags > 1) {
  2150. set_error_buf(error_buf, error_buf_size, "integer too large");
  2151. return false;
  2152. }
  2153. #else
  2154. if (p - p_org > 1) {
  2155. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2156. return false;
  2157. }
  2158. if (table->flags == 2) {
  2159. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2160. return false;
  2161. }
  2162. if (table->flags > 1) {
  2163. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2164. return false;
  2165. }
  2166. #endif
  2167. read_leb_uint32(p, p_end, table->init_size);
  2168. if (table->flags) {
  2169. read_leb_uint32(p, p_end, table->max_size);
  2170. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2171. error_buf_size))
  2172. return false;
  2173. }
  2174. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2175. *p_buf = p;
  2176. return true;
  2177. fail:
  2178. return false;
  2179. }
  2180. static bool
  2181. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2182. char *error_buf, uint32 error_buf_size)
  2183. {
  2184. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2185. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2186. uint32 pool_size = wasm_runtime_memory_pool_size();
  2187. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2188. / DEFAULT_NUM_BYTES_PER_PAGE;
  2189. #else
  2190. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2191. #endif
  2192. p_org = p;
  2193. read_leb_uint32(p, p_end, memory->flags);
  2194. #if WASM_ENABLE_SHARED_MEMORY == 0
  2195. if (p - p_org > 1) {
  2196. set_error_buf(error_buf, error_buf_size,
  2197. "integer representation too long");
  2198. return false;
  2199. }
  2200. if (memory->flags > 1) {
  2201. set_error_buf(error_buf, error_buf_size, "integer too large");
  2202. return false;
  2203. }
  2204. #else
  2205. if (p - p_org > 1) {
  2206. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2207. return false;
  2208. }
  2209. if (memory->flags > 3) {
  2210. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2211. return false;
  2212. }
  2213. else if (memory->flags == 2) {
  2214. set_error_buf(error_buf, error_buf_size,
  2215. "shared memory must have maximum");
  2216. return false;
  2217. }
  2218. #endif
  2219. read_leb_uint32(p, p_end, memory->init_page_count);
  2220. if (!check_memory_init_size(memory->init_page_count, error_buf,
  2221. error_buf_size))
  2222. return false;
  2223. if (memory->flags & 1) {
  2224. read_leb_uint32(p, p_end, memory->max_page_count);
  2225. if (!check_memory_max_size(memory->init_page_count,
  2226. memory->max_page_count, error_buf,
  2227. error_buf_size))
  2228. return false;
  2229. if (memory->max_page_count > max_page_count)
  2230. memory->max_page_count = max_page_count;
  2231. }
  2232. else {
  2233. /* Limit the maximum memory size to max_page_count */
  2234. memory->max_page_count = max_page_count;
  2235. }
  2236. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2237. *p_buf = p;
  2238. return true;
  2239. fail:
  2240. return false;
  2241. }
  2242. static bool
  2243. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2244. bool is_load_from_file_buf, char *error_buf,
  2245. uint32 error_buf_size)
  2246. {
  2247. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2248. uint32 import_count, name_len, type_index, i, u32, flags;
  2249. uint64 total_size;
  2250. WASMImport *import;
  2251. WASMImport *import_functions = NULL, *import_tables = NULL;
  2252. WASMImport *import_memories = NULL, *import_globals = NULL;
  2253. char *sub_module_name, *field_name;
  2254. uint8 u8, kind, global_type;
  2255. read_leb_uint32(p, p_end, import_count);
  2256. if (import_count) {
  2257. module->import_count = import_count;
  2258. total_size = sizeof(WASMImport) * (uint64)import_count;
  2259. if (!(module->imports =
  2260. loader_malloc(total_size, error_buf, error_buf_size))) {
  2261. return false;
  2262. }
  2263. p_old = p;
  2264. /* Scan firstly to get import count of each type */
  2265. for (i = 0; i < import_count; i++) {
  2266. /* module name */
  2267. read_leb_uint32(p, p_end, name_len);
  2268. CHECK_BUF(p, p_end, name_len);
  2269. p += name_len;
  2270. /* field name */
  2271. read_leb_uint32(p, p_end, name_len);
  2272. CHECK_BUF(p, p_end, name_len);
  2273. p += name_len;
  2274. CHECK_BUF(p, p_end, 1);
  2275. /* 0x00/0x01/0x02/0x03 */
  2276. kind = read_uint8(p);
  2277. switch (kind) {
  2278. case IMPORT_KIND_FUNC: /* import function */
  2279. read_leb_uint32(p, p_end, type_index);
  2280. module->import_function_count++;
  2281. break;
  2282. case IMPORT_KIND_TABLE: /* import table */
  2283. CHECK_BUF(p, p_end, 1);
  2284. /* 0x70 */
  2285. u8 = read_uint8(p);
  2286. read_leb_uint32(p, p_end, flags);
  2287. read_leb_uint32(p, p_end, u32);
  2288. if (flags & 1)
  2289. read_leb_uint32(p, p_end, u32);
  2290. module->import_table_count++;
  2291. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2292. if (module->import_table_count > 1) {
  2293. set_error_buf(error_buf, error_buf_size,
  2294. "multiple tables");
  2295. return false;
  2296. }
  2297. #endif
  2298. break;
  2299. case IMPORT_KIND_MEMORY: /* import memory */
  2300. read_leb_uint32(p, p_end, flags);
  2301. read_leb_uint32(p, p_end, u32);
  2302. if (flags & 1)
  2303. read_leb_uint32(p, p_end, u32);
  2304. module->import_memory_count++;
  2305. if (module->import_memory_count > 1) {
  2306. set_error_buf(error_buf, error_buf_size,
  2307. "multiple memories");
  2308. return false;
  2309. }
  2310. break;
  2311. case IMPORT_KIND_GLOBAL: /* import global */
  2312. #if WASM_ENABLE_GC != 0
  2313. /* valtype */
  2314. CHECK_BUF(p, p_end, 1);
  2315. global_type = read_uint8(p);
  2316. if (wasm_is_type_multi_byte_type(global_type)) {
  2317. int32 heap_type;
  2318. read_leb_int32(p, p_end, heap_type);
  2319. (void)heap_type;
  2320. }
  2321. /* mutability */
  2322. CHECK_BUF(p, p_end, 1);
  2323. p += 1;
  2324. #else
  2325. CHECK_BUF(p, p_end, 2);
  2326. p += 2;
  2327. #endif
  2328. (void)global_type;
  2329. module->import_global_count++;
  2330. break;
  2331. default:
  2332. set_error_buf(error_buf, error_buf_size,
  2333. "invalid import kind");
  2334. return false;
  2335. }
  2336. }
  2337. if (module->import_function_count)
  2338. import_functions = module->import_functions = module->imports;
  2339. if (module->import_table_count)
  2340. import_tables = module->import_tables =
  2341. module->imports + module->import_function_count;
  2342. if (module->import_memory_count)
  2343. import_memories = module->import_memories =
  2344. module->imports + module->import_function_count
  2345. + module->import_table_count;
  2346. if (module->import_global_count)
  2347. import_globals = module->import_globals =
  2348. module->imports + module->import_function_count
  2349. + module->import_table_count + module->import_memory_count;
  2350. p = p_old;
  2351. /* Scan again to resolve the data */
  2352. for (i = 0; i < import_count; i++) {
  2353. /* load module name */
  2354. read_leb_uint32(p, p_end, name_len);
  2355. CHECK_BUF(p, p_end, name_len);
  2356. if (!(sub_module_name = const_str_list_insert(
  2357. p, name_len, module, is_load_from_file_buf, error_buf,
  2358. error_buf_size))) {
  2359. return false;
  2360. }
  2361. p += name_len;
  2362. /* load field name */
  2363. read_leb_uint32(p, p_end, name_len);
  2364. CHECK_BUF(p, p_end, name_len);
  2365. if (!(field_name = const_str_list_insert(
  2366. p, name_len, module, is_load_from_file_buf, error_buf,
  2367. error_buf_size))) {
  2368. return false;
  2369. }
  2370. p += name_len;
  2371. CHECK_BUF(p, p_end, 1);
  2372. /* 0x00/0x01/0x02/0x03 */
  2373. kind = read_uint8(p);
  2374. switch (kind) {
  2375. case IMPORT_KIND_FUNC: /* import function */
  2376. bh_assert(import_functions);
  2377. import = import_functions++;
  2378. if (!load_function_import(
  2379. &p, p_end, module, sub_module_name, field_name,
  2380. &import->u.function, error_buf, error_buf_size)) {
  2381. return false;
  2382. }
  2383. break;
  2384. case IMPORT_KIND_TABLE: /* import table */
  2385. bh_assert(import_tables);
  2386. import = import_tables++;
  2387. if (!load_table_import(&p, p_end, module, sub_module_name,
  2388. field_name, &import->u.table,
  2389. error_buf, error_buf_size)) {
  2390. LOG_DEBUG("can not import such a table (%s,%s)",
  2391. sub_module_name, field_name);
  2392. return false;
  2393. }
  2394. break;
  2395. case IMPORT_KIND_MEMORY: /* import memory */
  2396. bh_assert(import_memories);
  2397. import = import_memories++;
  2398. if (!load_memory_import(&p, p_end, module, sub_module_name,
  2399. field_name, &import->u.memory,
  2400. error_buf, error_buf_size)) {
  2401. return false;
  2402. }
  2403. break;
  2404. case IMPORT_KIND_GLOBAL: /* import global */
  2405. bh_assert(import_globals);
  2406. import = import_globals++;
  2407. if (!load_global_import(&p, p_end, module, sub_module_name,
  2408. field_name, &import->u.global,
  2409. error_buf, error_buf_size)) {
  2410. return false;
  2411. }
  2412. break;
  2413. default:
  2414. set_error_buf(error_buf, error_buf_size,
  2415. "invalid import kind");
  2416. return false;
  2417. }
  2418. import->kind = kind;
  2419. import->u.names.module_name = sub_module_name;
  2420. import->u.names.field_name = field_name;
  2421. }
  2422. #if WASM_ENABLE_LIBC_WASI != 0
  2423. import = module->import_functions;
  2424. for (i = 0; i < module->import_function_count; i++, import++) {
  2425. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  2426. || !strcmp(import->u.names.module_name,
  2427. "wasi_snapshot_preview1")) {
  2428. module->import_wasi_api = true;
  2429. break;
  2430. }
  2431. }
  2432. #endif
  2433. }
  2434. if (p != p_end) {
  2435. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2436. return false;
  2437. }
  2438. LOG_VERBOSE("Load import section success.\n");
  2439. (void)u8;
  2440. (void)u32;
  2441. (void)type_index;
  2442. return true;
  2443. fail:
  2444. return false;
  2445. }
  2446. static bool
  2447. init_function_local_offsets(WASMFunction *func, char *error_buf,
  2448. uint32 error_buf_size)
  2449. {
  2450. WASMFuncType *param_type = func->func_type;
  2451. uint32 param_count = param_type->param_count;
  2452. uint8 *param_types = param_type->types;
  2453. uint32 local_count = func->local_count;
  2454. uint8 *local_types = func->local_types;
  2455. uint32 i, local_offset = 0;
  2456. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  2457. /*
  2458. * Only allocate memory when total_size is not 0,
  2459. * or the return value of malloc(0) might be NULL on some platforms,
  2460. * which causes wasm loader return false.
  2461. */
  2462. if (total_size > 0
  2463. && !(func->local_offsets =
  2464. loader_malloc(total_size, error_buf, error_buf_size))) {
  2465. return false;
  2466. }
  2467. for (i = 0; i < param_count; i++) {
  2468. func->local_offsets[i] = (uint16)local_offset;
  2469. local_offset += wasm_value_type_cell_num(param_types[i]);
  2470. }
  2471. for (i = 0; i < local_count; i++) {
  2472. func->local_offsets[param_count + i] = (uint16)local_offset;
  2473. local_offset += wasm_value_type_cell_num(local_types[i]);
  2474. }
  2475. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  2476. return true;
  2477. }
  2478. static bool
  2479. load_function_section(const uint8 *buf, const uint8 *buf_end,
  2480. const uint8 *buf_code, const uint8 *buf_code_end,
  2481. WASMModule *module, char *error_buf,
  2482. uint32 error_buf_size)
  2483. {
  2484. const uint8 *p = buf, *p_end = buf_end;
  2485. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  2486. uint32 func_count;
  2487. uint64 total_size;
  2488. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  2489. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  2490. uint8 type;
  2491. WASMFunction *func;
  2492. #if WASM_ENABLE_GC != 0
  2493. bool need_ref_type_map;
  2494. WASMRefType ref_type;
  2495. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  2496. #endif
  2497. read_leb_uint32(p, p_end, func_count);
  2498. if (buf_code)
  2499. read_leb_uint32(p_code, buf_code_end, code_count);
  2500. if (func_count != code_count) {
  2501. set_error_buf(error_buf, error_buf_size,
  2502. "function and code section have inconsistent lengths or "
  2503. "unexpected end");
  2504. return false;
  2505. }
  2506. if (func_count) {
  2507. module->function_count = func_count;
  2508. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  2509. if (!(module->functions =
  2510. loader_malloc(total_size, error_buf, error_buf_size))) {
  2511. return false;
  2512. }
  2513. for (i = 0; i < func_count; i++) {
  2514. /* Resolve function type */
  2515. read_leb_uint32(p, p_end, type_index);
  2516. if (type_index >= module->type_count) {
  2517. set_error_buf(error_buf, error_buf_size, "unknown type");
  2518. return false;
  2519. }
  2520. #if WASM_ENABLE_GC != 0
  2521. type_index_org = type_index;
  2522. #endif
  2523. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2524. type_index = wasm_get_smallest_type_idx(
  2525. module->types, module->type_count, type_index);
  2526. #endif
  2527. read_leb_uint32(p_code, buf_code_end, code_size);
  2528. if (code_size == 0 || p_code + code_size > buf_code_end) {
  2529. set_error_buf(error_buf, error_buf_size,
  2530. "invalid function code size");
  2531. return false;
  2532. }
  2533. /* Resolve local set count */
  2534. p_code_end = p_code + code_size;
  2535. local_count = 0;
  2536. read_leb_uint32(p_code, buf_code_end, local_set_count);
  2537. p_code_save = p_code;
  2538. #if WASM_ENABLE_GC != 0
  2539. ref_type_map_count = 0;
  2540. #endif
  2541. /* Calculate total local count */
  2542. for (j = 0; j < local_set_count; j++) {
  2543. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  2544. if (sub_local_count > UINT32_MAX - local_count) {
  2545. set_error_buf(error_buf, error_buf_size, "too many locals");
  2546. return false;
  2547. }
  2548. #if WASM_ENABLE_GC == 0
  2549. CHECK_BUF(p_code, buf_code_end, 1);
  2550. /* 0x7F/0x7E/0x7D/0x7C */
  2551. type = read_uint8(p_code);
  2552. local_count += sub_local_count;
  2553. #else
  2554. if (!resolve_value_type(&p_code, buf_code_end, module,
  2555. &need_ref_type_map, &ref_type, false,
  2556. error_buf, error_buf_size)) {
  2557. return false;
  2558. }
  2559. local_count += sub_local_count;
  2560. if (need_ref_type_map)
  2561. ref_type_map_count += sub_local_count;
  2562. #endif
  2563. }
  2564. /* Alloc memory, layout: function structure + local types */
  2565. code_size = (uint32)(p_code_end - p_code);
  2566. total_size = sizeof(WASMFunction) + (uint64)local_count;
  2567. if (!(func = module->functions[i] =
  2568. loader_malloc(total_size, error_buf, error_buf_size))) {
  2569. return false;
  2570. }
  2571. #if WASM_ENABLE_GC != 0
  2572. if (ref_type_map_count > 0) {
  2573. total_size =
  2574. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  2575. if (!(func->local_ref_type_maps = loader_malloc(
  2576. total_size, error_buf, error_buf_size))) {
  2577. return false;
  2578. }
  2579. func->local_ref_type_map_count = ref_type_map_count;
  2580. }
  2581. #endif
  2582. /* Set function type, local count, code size and code body */
  2583. func->func_type = (WASMFuncType *)module->types[type_index];
  2584. func->local_count = local_count;
  2585. if (local_count > 0)
  2586. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  2587. func->code_size = code_size;
  2588. /*
  2589. * we shall make a copy of code body [p_code, p_code + code_size]
  2590. * when we are worrying about inappropriate releasing behaviour.
  2591. * all code bodies are actually in a buffer which user allocates in
  2592. * his embedding environment and we don't have power on them.
  2593. * it will be like:
  2594. * code_body_cp = malloc(code_size);
  2595. * memcpy(code_body_cp, p_code, code_size);
  2596. * func->code = code_body_cp;
  2597. */
  2598. func->code = (uint8 *)p_code;
  2599. #if WASM_ENABLE_GC != 0
  2600. func->type_idx = type_index_org;
  2601. #endif
  2602. #if WASM_ENABLE_GC != 0
  2603. t = 0;
  2604. #endif
  2605. /* Load each local type */
  2606. p_code = p_code_save;
  2607. local_type_index = 0;
  2608. for (j = 0; j < local_set_count; j++) {
  2609. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  2610. /* Note: sub_local_count is allowed to be 0 */
  2611. if (local_type_index > UINT32_MAX - sub_local_count
  2612. || local_type_index + sub_local_count > local_count) {
  2613. set_error_buf(error_buf, error_buf_size,
  2614. "invalid local count");
  2615. return false;
  2616. }
  2617. #if WASM_ENABLE_GC == 0
  2618. CHECK_BUF(p_code, buf_code_end, 1);
  2619. /* 0x7F/0x7E/0x7D/0x7C */
  2620. type = read_uint8(p_code);
  2621. if (!is_value_type(type)) {
  2622. if (type == VALUE_TYPE_V128)
  2623. set_error_buf(error_buf, error_buf_size,
  2624. "v128 value type requires simd feature");
  2625. else if (type == VALUE_TYPE_FUNCREF
  2626. || type == VALUE_TYPE_EXTERNREF)
  2627. set_error_buf(error_buf, error_buf_size,
  2628. "ref value type requires "
  2629. "reference types feature");
  2630. else
  2631. set_error_buf_v(error_buf, error_buf_size,
  2632. "invalid local type 0x%02X", type);
  2633. return false;
  2634. }
  2635. #else
  2636. if (!resolve_value_type(&p_code, buf_code_end, module,
  2637. &need_ref_type_map, &ref_type, false,
  2638. error_buf, error_buf_size)) {
  2639. return false;
  2640. }
  2641. if (need_ref_type_map) {
  2642. WASMRefType *ref_type_tmp;
  2643. if (!(ref_type_tmp = reftype_set_insert(
  2644. module->ref_type_set, &ref_type, error_buf,
  2645. error_buf_size))) {
  2646. return false;
  2647. }
  2648. for (k = 0; k < sub_local_count; k++) {
  2649. func->local_ref_type_maps[t + k].ref_type =
  2650. ref_type_tmp;
  2651. func->local_ref_type_maps[t + k].index =
  2652. local_type_index + k;
  2653. }
  2654. t += sub_local_count;
  2655. }
  2656. type = ref_type.ref_type;
  2657. #endif
  2658. for (k = 0; k < sub_local_count; k++) {
  2659. func->local_types[local_type_index++] = type;
  2660. }
  2661. }
  2662. bh_assert(local_type_index == func->local_count);
  2663. #if WASM_ENABLE_GC != 0
  2664. bh_assert(t == func->local_ref_type_map_count);
  2665. #if TRACE_WASM_LOADER != 0
  2666. os_printf("func %u, local types: [", i);
  2667. k = 0;
  2668. for (j = 0; j < func->local_count; j++) {
  2669. WASMRefType *ref_type_tmp = NULL;
  2670. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  2671. bh_assert(j == func->local_ref_type_maps[k].index);
  2672. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  2673. }
  2674. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  2675. if (j < func->local_count - 1)
  2676. os_printf(" ");
  2677. }
  2678. os_printf("]\n");
  2679. #endif
  2680. #endif
  2681. func->param_cell_num = func->func_type->param_cell_num;
  2682. func->ret_cell_num = func->func_type->ret_cell_num;
  2683. local_cell_num =
  2684. wasm_get_cell_num(func->local_types, func->local_count);
  2685. if (local_cell_num > UINT16_MAX) {
  2686. set_error_buf(error_buf, error_buf_size,
  2687. "local count too large");
  2688. return false;
  2689. }
  2690. func->local_cell_num = (uint16)local_cell_num;
  2691. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  2692. return false;
  2693. p_code = p_code_end;
  2694. }
  2695. }
  2696. if (p != p_end) {
  2697. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2698. return false;
  2699. }
  2700. LOG_VERBOSE("Load function section success.\n");
  2701. return true;
  2702. fail:
  2703. return false;
  2704. }
  2705. static bool
  2706. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2707. char *error_buf, uint32 error_buf_size)
  2708. {
  2709. const uint8 *p = buf, *p_end = buf_end;
  2710. uint32 table_count, i;
  2711. uint64 total_size;
  2712. WASMTable *table;
  2713. read_leb_uint32(p, p_end, table_count);
  2714. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2715. if (module->import_table_count + table_count > 1) {
  2716. /* a total of one table is allowed */
  2717. set_error_buf(error_buf, error_buf_size, "multiple tables");
  2718. return false;
  2719. }
  2720. #endif
  2721. if (table_count) {
  2722. module->table_count = table_count;
  2723. total_size = sizeof(WASMTable) * (uint64)table_count;
  2724. if (!(module->tables =
  2725. loader_malloc(total_size, error_buf, error_buf_size))) {
  2726. return false;
  2727. }
  2728. /* load each table */
  2729. table = module->tables;
  2730. for (i = 0; i < table_count; i++, table++)
  2731. if (!load_table(&p, p_end, module, table, error_buf,
  2732. error_buf_size))
  2733. return false;
  2734. }
  2735. if (p != p_end) {
  2736. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2737. return false;
  2738. }
  2739. LOG_VERBOSE("Load table section success.\n");
  2740. return true;
  2741. fail:
  2742. return false;
  2743. }
  2744. static bool
  2745. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2746. char *error_buf, uint32 error_buf_size)
  2747. {
  2748. const uint8 *p = buf, *p_end = buf_end;
  2749. uint32 memory_count, i;
  2750. uint64 total_size;
  2751. WASMMemory *memory;
  2752. read_leb_uint32(p, p_end, memory_count);
  2753. /* a total of one memory is allowed */
  2754. if (module->import_memory_count + memory_count > 1) {
  2755. set_error_buf(error_buf, error_buf_size, "multiple memories");
  2756. return false;
  2757. }
  2758. if (memory_count) {
  2759. module->memory_count = memory_count;
  2760. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  2761. if (!(module->memories =
  2762. loader_malloc(total_size, error_buf, error_buf_size))) {
  2763. return false;
  2764. }
  2765. /* load each memory */
  2766. memory = module->memories;
  2767. for (i = 0; i < memory_count; i++, memory++)
  2768. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  2769. return false;
  2770. }
  2771. if (p != p_end) {
  2772. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2773. return false;
  2774. }
  2775. LOG_VERBOSE("Load memory section success.\n");
  2776. return true;
  2777. fail:
  2778. return false;
  2779. }
  2780. static bool
  2781. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2782. char *error_buf, uint32 error_buf_size)
  2783. {
  2784. const uint8 *p = buf, *p_end = buf_end;
  2785. uint32 global_count, i;
  2786. uint64 total_size;
  2787. WASMGlobal *global;
  2788. uint8 mutable;
  2789. #if WASM_ENABLE_GC != 0
  2790. bool need_ref_type_map;
  2791. WASMRefType ref_type;
  2792. #endif
  2793. read_leb_uint32(p, p_end, global_count);
  2794. if (global_count) {
  2795. module->global_count = global_count;
  2796. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  2797. if (!(module->globals =
  2798. loader_malloc(total_size, error_buf, error_buf_size))) {
  2799. return false;
  2800. }
  2801. global = module->globals;
  2802. for (i = 0; i < global_count; i++, global++) {
  2803. #if WASM_ENABLE_GC == 0
  2804. CHECK_BUF(p, p_end, 2);
  2805. global->type = read_uint8(p);
  2806. mutable = read_uint8(p);
  2807. #else
  2808. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  2809. &ref_type, false, error_buf,
  2810. error_buf_size)) {
  2811. return false;
  2812. }
  2813. global->type = ref_type.ref_type;
  2814. CHECK_BUF(p, p_end, 1);
  2815. mutable = read_uint8(p);
  2816. #endif /* end of WASM_ENABLE_GC */
  2817. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  2818. return false;
  2819. }
  2820. global->is_mutable = mutable ? true : false;
  2821. /* initialize expression */
  2822. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  2823. global->type,
  2824. #if WASM_ENABLE_GC == 0
  2825. NULL,
  2826. #else
  2827. &ref_type,
  2828. #endif
  2829. error_buf, error_buf_size))
  2830. return false;
  2831. #if WASM_ENABLE_GC != 0
  2832. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  2833. uint8 global_type;
  2834. WASMRefType *global_ref_type;
  2835. uint32 global_idx = global->init_expr.u.global_index;
  2836. if (global->init_expr.u.global_index
  2837. >= module->import_global_count + i) {
  2838. set_error_buf(error_buf, error_buf_size, "unknown global");
  2839. return false;
  2840. }
  2841. if (global_idx < module->import_global_count) {
  2842. global_type =
  2843. module->import_globals[global_idx].u.global.type;
  2844. global_ref_type =
  2845. module->import_globals[global_idx].u.global.ref_type;
  2846. }
  2847. else {
  2848. global_type = module->globals[global_idx].type;
  2849. global_ref_type = module->globals[global_idx].ref_type;
  2850. }
  2851. if (!wasm_reftype_is_subtype_of(
  2852. global_type, global_ref_type, global->type,
  2853. global->ref_type, module->types, module->type_count)) {
  2854. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2855. return false;
  2856. }
  2857. }
  2858. if (need_ref_type_map) {
  2859. if (!(global->ref_type =
  2860. reftype_set_insert(module->ref_type_set, &ref_type,
  2861. error_buf, error_buf_size))) {
  2862. return false;
  2863. }
  2864. }
  2865. #if TRACE_WASM_LOADER != 0
  2866. os_printf("global type: ");
  2867. wasm_dump_value_type(global->type, global->ref_type);
  2868. os_printf("\n");
  2869. #endif
  2870. #endif
  2871. }
  2872. }
  2873. if (p != p_end) {
  2874. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2875. return false;
  2876. }
  2877. LOG_VERBOSE("Load global section success.\n");
  2878. return true;
  2879. fail:
  2880. return false;
  2881. }
  2882. static bool
  2883. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2884. bool is_load_from_file_buf, char *error_buf,
  2885. uint32 error_buf_size)
  2886. {
  2887. const uint8 *p = buf, *p_end = buf_end;
  2888. uint32 export_count, i, j, index;
  2889. uint64 total_size;
  2890. uint32 str_len;
  2891. WASMExport *export;
  2892. const char *name;
  2893. read_leb_uint32(p, p_end, export_count);
  2894. if (export_count) {
  2895. module->export_count = export_count;
  2896. total_size = sizeof(WASMExport) * (uint64)export_count;
  2897. if (!(module->exports =
  2898. loader_malloc(total_size, error_buf, error_buf_size))) {
  2899. return false;
  2900. }
  2901. export = module->exports;
  2902. for (i = 0; i < export_count; i++, export ++) {
  2903. #if WASM_ENABLE_THREAD_MGR == 0
  2904. if (p == p_end) {
  2905. /* export section with inconsistent count:
  2906. n export declared, but less than n given */
  2907. set_error_buf(error_buf, error_buf_size,
  2908. "length out of bounds");
  2909. return false;
  2910. }
  2911. #endif
  2912. read_leb_uint32(p, p_end, str_len);
  2913. CHECK_BUF(p, p_end, str_len);
  2914. for (j = 0; j < i; j++) {
  2915. name = module->exports[j].name;
  2916. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  2917. set_error_buf(error_buf, error_buf_size,
  2918. "duplicate export name");
  2919. return false;
  2920. }
  2921. }
  2922. if (!(export->name = const_str_list_insert(
  2923. p, str_len, module, is_load_from_file_buf, error_buf,
  2924. error_buf_size))) {
  2925. return false;
  2926. }
  2927. p += str_len;
  2928. CHECK_BUF(p, p_end, 1);
  2929. export->kind = read_uint8(p);
  2930. read_leb_uint32(p, p_end, index);
  2931. export->index = index;
  2932. switch (export->kind) {
  2933. /* function index */
  2934. case EXPORT_KIND_FUNC:
  2935. if (index >= module->function_count
  2936. + module->import_function_count) {
  2937. set_error_buf(error_buf, error_buf_size,
  2938. "unknown function");
  2939. return false;
  2940. }
  2941. #if WASM_ENABLE_SIMD != 0
  2942. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2943. /* TODO: check func type, if it has v128 param or result,
  2944. report error */
  2945. #endif
  2946. #endif
  2947. break;
  2948. /* table index */
  2949. case EXPORT_KIND_TABLE:
  2950. if (index
  2951. >= module->table_count + module->import_table_count) {
  2952. set_error_buf(error_buf, error_buf_size,
  2953. "unknown table");
  2954. return false;
  2955. }
  2956. break;
  2957. /* memory index */
  2958. case EXPORT_KIND_MEMORY:
  2959. if (index
  2960. >= module->memory_count + module->import_memory_count) {
  2961. set_error_buf(error_buf, error_buf_size,
  2962. "unknown memory");
  2963. return false;
  2964. }
  2965. break;
  2966. /* global index */
  2967. case EXPORT_KIND_GLOBAL:
  2968. if (index
  2969. >= module->global_count + module->import_global_count) {
  2970. set_error_buf(error_buf, error_buf_size,
  2971. "unknown global");
  2972. return false;
  2973. }
  2974. break;
  2975. default:
  2976. set_error_buf(error_buf, error_buf_size,
  2977. "invalid export kind");
  2978. return false;
  2979. }
  2980. }
  2981. }
  2982. if (p != p_end) {
  2983. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2984. return false;
  2985. }
  2986. LOG_VERBOSE("Load export section success.\n");
  2987. return true;
  2988. fail:
  2989. return false;
  2990. }
  2991. static bool
  2992. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  2993. uint32 error_buf_size)
  2994. {
  2995. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2996. if (table_index != 0) {
  2997. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  2998. return false;
  2999. }
  3000. #endif
  3001. if (table_index >= module->import_table_count + module->table_count) {
  3002. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3003. table_index);
  3004. return false;
  3005. }
  3006. return true;
  3007. }
  3008. static bool
  3009. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3010. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3011. {
  3012. const uint8 *p = *p_buf, *p_end = buf_end;
  3013. uint32 table_index;
  3014. read_leb_uint32(p, p_end, table_index);
  3015. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3016. return false;
  3017. }
  3018. *p_table_index = table_index;
  3019. *p_buf = p;
  3020. return true;
  3021. fail:
  3022. return false;
  3023. }
  3024. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3025. static bool
  3026. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3027. uint32 *p_elem_type,
  3028. #if WASM_ENABLE_GC != 0
  3029. WASMRefType **p_elem_ref_type,
  3030. #endif
  3031. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3032. {
  3033. const uint8 *p = *p_buf, *p_end = buf_end;
  3034. uint8 elem_type;
  3035. #if WASM_ENABLE_GC != 0
  3036. WASMRefType elem_ref_type;
  3037. bool need_ref_type_map;
  3038. #endif
  3039. CHECK_BUF(p, p_end, 1);
  3040. elem_type = read_uint8(p);
  3041. if (elemkind_zero) {
  3042. if (elem_type != 0) {
  3043. set_error_buf(error_buf, error_buf_size,
  3044. "invalid reference type or unknown type");
  3045. return false;
  3046. }
  3047. else {
  3048. *p_elem_type = VALUE_TYPE_FUNCREF;
  3049. *p_buf = p;
  3050. return true;
  3051. }
  3052. }
  3053. #if WASM_ENABLE_GC == 0
  3054. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3055. set_error_buf(error_buf, error_buf_size,
  3056. "invalid reference type or unknown type");
  3057. return false;
  3058. }
  3059. *p_elem_type = elem_type;
  3060. #else
  3061. p--;
  3062. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3063. &need_ref_type_map, &elem_ref_type, false,
  3064. error_buf, error_buf_size)) {
  3065. return false;
  3066. }
  3067. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3068. set_error_buf(error_buf, error_buf_size,
  3069. "invalid reference type or unknown type");
  3070. return false;
  3071. }
  3072. *p_elem_type = elem_ref_type.ref_type;
  3073. if (need_ref_type_map) {
  3074. if (!(*p_elem_ref_type =
  3075. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3076. error_buf, error_buf_size))) {
  3077. return false;
  3078. }
  3079. }
  3080. #endif
  3081. *p_buf = p;
  3082. return true;
  3083. fail:
  3084. return false;
  3085. }
  3086. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3087. static bool
  3088. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3089. WASMModule *module, WASMTableSeg *table_segment,
  3090. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  3091. {
  3092. const uint8 *p = *p_buf, *p_end = buf_end;
  3093. uint32 function_count, function_index = 0, i;
  3094. uint64 total_size;
  3095. read_leb_uint32(p, p_end, function_count);
  3096. table_segment->function_count = function_count;
  3097. total_size = sizeof(uintptr_t) * (uint64)function_count;
  3098. if (total_size > 0
  3099. && !(table_segment->func_indexes = (uintptr_t *)loader_malloc(
  3100. total_size, error_buf, error_buf_size))) {
  3101. return false;
  3102. }
  3103. for (i = 0; i < function_count; i++) {
  3104. InitializerExpression init_expr = { 0 };
  3105. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3106. if (!use_init_expr) {
  3107. read_leb_uint32(p, p_end, function_index);
  3108. }
  3109. else {
  3110. if (!load_init_expr(module, &p, p_end, &init_expr,
  3111. table_segment->elem_type,
  3112. #if WASM_ENABLE_GC == 0
  3113. NULL,
  3114. #else
  3115. table_segment->elem_ref_type,
  3116. #endif
  3117. error_buf, error_buf_size))
  3118. return false;
  3119. function_index = init_expr.u.ref_index;
  3120. }
  3121. #else
  3122. read_leb_uint32(p, p_end, function_index);
  3123. #endif
  3124. /* since we are using -1 to indicate ref.null */
  3125. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  3126. && !check_function_index(module, function_index, error_buf,
  3127. error_buf_size)) {
  3128. return false;
  3129. }
  3130. table_segment->func_indexes[i] = function_index;
  3131. }
  3132. *p_buf = p;
  3133. return true;
  3134. fail:
  3135. return false;
  3136. }
  3137. static bool
  3138. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3139. WASMModule *module, char *error_buf,
  3140. uint32 error_buf_size)
  3141. {
  3142. const uint8 *p = buf, *p_end = buf_end;
  3143. uint32 table_segment_count, i;
  3144. uint64 total_size;
  3145. WASMTableSeg *table_segment;
  3146. read_leb_uint32(p, p_end, table_segment_count);
  3147. if (table_segment_count) {
  3148. module->table_seg_count = table_segment_count;
  3149. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3150. if (!(module->table_segments =
  3151. loader_malloc(total_size, error_buf, error_buf_size))) {
  3152. return false;
  3153. }
  3154. table_segment = module->table_segments;
  3155. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3156. if (p >= p_end) {
  3157. set_error_buf(error_buf, error_buf_size,
  3158. "invalid value type or "
  3159. "invalid elements segment kind");
  3160. return false;
  3161. }
  3162. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3163. read_leb_uint32(p, p_end, table_segment->mode);
  3164. /* last three bits */
  3165. table_segment->mode = table_segment->mode & 0x07;
  3166. switch (table_segment->mode) {
  3167. /* elemkind/elemtype + active */
  3168. case 0:
  3169. case 4:
  3170. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3171. table_segment->table_index = 0;
  3172. if (!check_table_index(module, table_segment->table_index,
  3173. error_buf, error_buf_size))
  3174. return false;
  3175. if (!load_init_expr(
  3176. module, &p, p_end, &table_segment->base_offset,
  3177. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3178. return false;
  3179. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3180. table_segment->mode == 0 ? false
  3181. : true,
  3182. error_buf, error_buf_size))
  3183. return false;
  3184. break;
  3185. /* elemkind + passive/declarative */
  3186. case 1:
  3187. case 3:
  3188. if (!load_elem_type(module, &p, p_end,
  3189. &table_segment->elem_type,
  3190. #if WASM_ENABLE_GC != 0
  3191. &table_segment->elem_ref_type,
  3192. #endif
  3193. true, error_buf, error_buf_size))
  3194. return false;
  3195. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3196. false, error_buf, error_buf_size))
  3197. return false;
  3198. break;
  3199. /* elemkind/elemtype + table_idx + active */
  3200. case 2:
  3201. case 6:
  3202. if (!load_table_index(&p, p_end, module,
  3203. &table_segment->table_index,
  3204. error_buf, error_buf_size))
  3205. return false;
  3206. if (!load_init_expr(
  3207. module, &p, p_end, &table_segment->base_offset,
  3208. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3209. return false;
  3210. if (!load_elem_type(module, &p, p_end,
  3211. &table_segment->elem_type,
  3212. #if WASM_ENABLE_GC != 0
  3213. &table_segment->elem_ref_type,
  3214. #endif
  3215. table_segment->mode == 2 ? true : false,
  3216. error_buf, error_buf_size))
  3217. return false;
  3218. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3219. table_segment->mode == 2 ? false
  3220. : true,
  3221. error_buf, error_buf_size))
  3222. return false;
  3223. break;
  3224. case 5:
  3225. case 7:
  3226. if (!load_elem_type(module, &p, p_end,
  3227. &table_segment->elem_type,
  3228. #if WASM_ENABLE_GC != 0
  3229. &table_segment->elem_ref_type,
  3230. #endif
  3231. false, error_buf, error_buf_size))
  3232. return false;
  3233. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3234. true, error_buf, error_buf_size))
  3235. return false;
  3236. break;
  3237. default:
  3238. set_error_buf(error_buf, error_buf_size,
  3239. "unknown element segment kind");
  3240. return false;
  3241. }
  3242. #else
  3243. /*
  3244. * like: 00 41 05 0b 04 00 01 00 01
  3245. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  3246. */
  3247. if (!load_table_index(&p, p_end, module,
  3248. &table_segment->table_index, error_buf,
  3249. error_buf_size))
  3250. return false;
  3251. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  3252. VALUE_TYPE_I32, NULL, error_buf,
  3253. error_buf_size))
  3254. return false;
  3255. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  3256. error_buf, error_buf_size))
  3257. return false;
  3258. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  3259. }
  3260. }
  3261. if (p != p_end) {
  3262. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3263. return false;
  3264. }
  3265. LOG_VERBOSE("Load table segment section success.\n");
  3266. return true;
  3267. fail:
  3268. return false;
  3269. }
  3270. static bool
  3271. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  3272. WASMModule *module, char *error_buf,
  3273. uint32 error_buf_size)
  3274. {
  3275. const uint8 *p = buf, *p_end = buf_end;
  3276. uint32 data_seg_count, i, mem_index, data_seg_len;
  3277. uint64 total_size;
  3278. WASMDataSeg *dataseg;
  3279. InitializerExpression init_expr;
  3280. #if WASM_ENABLE_BULK_MEMORY != 0
  3281. bool is_passive = false;
  3282. uint32 mem_flag;
  3283. #endif
  3284. read_leb_uint32(p, p_end, data_seg_count);
  3285. #if WASM_ENABLE_BULK_MEMORY != 0
  3286. if ((module->data_seg_count1 != 0)
  3287. && (data_seg_count != module->data_seg_count1)) {
  3288. set_error_buf(error_buf, error_buf_size,
  3289. "data count and data section have inconsistent lengths");
  3290. return false;
  3291. }
  3292. #endif
  3293. if (data_seg_count) {
  3294. module->data_seg_count = data_seg_count;
  3295. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  3296. if (!(module->data_segments =
  3297. loader_malloc(total_size, error_buf, error_buf_size))) {
  3298. return false;
  3299. }
  3300. for (i = 0; i < data_seg_count; i++) {
  3301. read_leb_uint32(p, p_end, mem_index);
  3302. #if WASM_ENABLE_BULK_MEMORY != 0
  3303. is_passive = false;
  3304. mem_flag = mem_index & 0x03;
  3305. switch (mem_flag) {
  3306. case 0x01:
  3307. is_passive = true;
  3308. break;
  3309. case 0x00:
  3310. /* no memory index, treat index as 0 */
  3311. mem_index = 0;
  3312. goto check_mem_index;
  3313. case 0x02:
  3314. /* read following memory index */
  3315. read_leb_uint32(p, p_end, mem_index);
  3316. check_mem_index:
  3317. if (mem_index
  3318. >= module->import_memory_count + module->memory_count) {
  3319. set_error_buf_v(error_buf, error_buf_size,
  3320. "unknown memory %d", mem_index);
  3321. return false;
  3322. }
  3323. break;
  3324. case 0x03:
  3325. default:
  3326. set_error_buf(error_buf, error_buf_size, "unknown memory");
  3327. return false;
  3328. break;
  3329. }
  3330. #else
  3331. if (mem_index
  3332. >= module->import_memory_count + module->memory_count) {
  3333. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  3334. mem_index);
  3335. return false;
  3336. }
  3337. #endif /* WASM_ENABLE_BULK_MEMORY */
  3338. #if WASM_ENABLE_BULK_MEMORY != 0
  3339. if (!is_passive)
  3340. #endif
  3341. if (!load_init_expr(module, &p, p_end, &init_expr,
  3342. VALUE_TYPE_I32, NULL, error_buf,
  3343. error_buf_size))
  3344. return false;
  3345. read_leb_uint32(p, p_end, data_seg_len);
  3346. if (!(dataseg = module->data_segments[i] = loader_malloc(
  3347. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  3348. return false;
  3349. }
  3350. #if WASM_ENABLE_BULK_MEMORY != 0
  3351. dataseg->is_passive = is_passive;
  3352. if (!is_passive)
  3353. #endif
  3354. {
  3355. bh_memcpy_s(&dataseg->base_offset,
  3356. sizeof(InitializerExpression), &init_expr,
  3357. sizeof(InitializerExpression));
  3358. dataseg->memory_index = mem_index;
  3359. }
  3360. dataseg->data_length = data_seg_len;
  3361. CHECK_BUF(p, p_end, data_seg_len);
  3362. dataseg->data = (uint8 *)p;
  3363. p += data_seg_len;
  3364. }
  3365. }
  3366. if (p != p_end) {
  3367. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3368. return false;
  3369. }
  3370. LOG_VERBOSE("Load data segment section success.\n");
  3371. return true;
  3372. fail:
  3373. return false;
  3374. }
  3375. #if WASM_ENABLE_BULK_MEMORY != 0
  3376. static bool
  3377. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  3378. WASMModule *module, char *error_buf,
  3379. uint32 error_buf_size)
  3380. {
  3381. const uint8 *p = buf, *p_end = buf_end;
  3382. uint32 data_seg_count1 = 0;
  3383. read_leb_uint32(p, p_end, data_seg_count1);
  3384. module->data_seg_count1 = data_seg_count1;
  3385. if (p != p_end) {
  3386. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3387. return false;
  3388. }
  3389. LOG_VERBOSE("Load datacount section success.\n");
  3390. return true;
  3391. fail:
  3392. return false;
  3393. }
  3394. #endif
  3395. static bool
  3396. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  3397. const uint8 *buf_func_end, WASMModule *module,
  3398. char *error_buf, uint32 error_buf_size)
  3399. {
  3400. const uint8 *p = buf, *p_end = buf_end;
  3401. const uint8 *p_func = buf_func;
  3402. uint32 func_count = 0, code_count;
  3403. /* code has been loaded in function section, so pass it here, just check
  3404. * whether function and code section have inconsistent lengths */
  3405. read_leb_uint32(p, p_end, code_count);
  3406. if (buf_func)
  3407. read_leb_uint32(p_func, buf_func_end, func_count);
  3408. if (func_count != code_count) {
  3409. set_error_buf(error_buf, error_buf_size,
  3410. "function and code section have inconsistent lengths");
  3411. return false;
  3412. }
  3413. LOG_VERBOSE("Load code segment section success.\n");
  3414. (void)module;
  3415. return true;
  3416. fail:
  3417. return false;
  3418. }
  3419. static bool
  3420. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3421. char *error_buf, uint32 error_buf_size)
  3422. {
  3423. const uint8 *p = buf, *p_end = buf_end;
  3424. WASMFuncType *type;
  3425. uint32 start_function;
  3426. read_leb_uint32(p, p_end, start_function);
  3427. if (start_function
  3428. >= module->function_count + module->import_function_count) {
  3429. set_error_buf(error_buf, error_buf_size, "unknown function");
  3430. return false;
  3431. }
  3432. if (start_function < module->import_function_count)
  3433. type = module->import_functions[start_function].u.function.func_type;
  3434. else
  3435. type = module->functions[start_function - module->import_function_count]
  3436. ->func_type;
  3437. if (type->param_count != 0 || type->result_count != 0) {
  3438. set_error_buf(error_buf, error_buf_size, "invalid start function");
  3439. return false;
  3440. }
  3441. module->start_function = start_function;
  3442. if (p != p_end) {
  3443. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3444. return false;
  3445. }
  3446. LOG_VERBOSE("Load start section success.\n");
  3447. return true;
  3448. fail:
  3449. return false;
  3450. }
  3451. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3452. static bool
  3453. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3454. bool is_load_from_file_buf, char *error_buf,
  3455. uint32 error_buf_size)
  3456. {
  3457. const uint8 *p = buf, *p_end = buf_end;
  3458. uint32 name_type, subsection_size;
  3459. uint32 previous_name_type = 0;
  3460. uint32 num_func_name;
  3461. uint32 func_index;
  3462. uint32 previous_func_index = ~0U;
  3463. uint32 func_name_len;
  3464. uint32 name_index;
  3465. int i = 0;
  3466. if (p >= p_end) {
  3467. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3468. return false;
  3469. }
  3470. while (p < p_end) {
  3471. read_leb_uint32(p, p_end, name_type);
  3472. if (i != 0) {
  3473. if (name_type == previous_name_type) {
  3474. set_error_buf(error_buf, error_buf_size,
  3475. "duplicate sub-section");
  3476. return false;
  3477. }
  3478. if (name_type < previous_name_type) {
  3479. set_error_buf(error_buf, error_buf_size,
  3480. "out-of-order sub-section");
  3481. return false;
  3482. }
  3483. }
  3484. previous_name_type = name_type;
  3485. read_leb_uint32(p, p_end, subsection_size);
  3486. CHECK_BUF(p, p_end, subsection_size);
  3487. switch (name_type) {
  3488. case SUB_SECTION_TYPE_FUNC:
  3489. if (subsection_size) {
  3490. read_leb_uint32(p, p_end, num_func_name);
  3491. for (name_index = 0; name_index < num_func_name;
  3492. name_index++) {
  3493. read_leb_uint32(p, p_end, func_index);
  3494. if (func_index == previous_func_index) {
  3495. set_error_buf(error_buf, error_buf_size,
  3496. "duplicate function name");
  3497. return false;
  3498. }
  3499. if (func_index < previous_func_index
  3500. && previous_func_index != ~0U) {
  3501. set_error_buf(error_buf, error_buf_size,
  3502. "out-of-order function index ");
  3503. return false;
  3504. }
  3505. previous_func_index = func_index;
  3506. read_leb_uint32(p, p_end, func_name_len);
  3507. CHECK_BUF(p, p_end, func_name_len);
  3508. /* Skip the import functions */
  3509. if (func_index >= module->import_function_count) {
  3510. func_index -= module->import_function_count;
  3511. if (func_index >= module->function_count) {
  3512. set_error_buf(error_buf, error_buf_size,
  3513. "out-of-range function index");
  3514. return false;
  3515. }
  3516. if (!(module->functions[func_index]->field_name =
  3517. const_str_list_insert(
  3518. p, func_name_len, module,
  3519. is_load_from_file_buf, error_buf,
  3520. error_buf_size))) {
  3521. return false;
  3522. }
  3523. }
  3524. p += func_name_len;
  3525. }
  3526. }
  3527. break;
  3528. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  3529. */
  3530. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  3531. default:
  3532. p = p + subsection_size;
  3533. break;
  3534. }
  3535. i++;
  3536. }
  3537. return true;
  3538. fail:
  3539. return false;
  3540. }
  3541. #endif
  3542. static bool
  3543. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3544. bool is_load_from_file_buf, char *error_buf,
  3545. uint32 error_buf_size)
  3546. {
  3547. const uint8 *p = buf, *p_end = buf_end;
  3548. char section_name[32];
  3549. uint32 name_len, buffer_len;
  3550. if (p >= p_end) {
  3551. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3552. return false;
  3553. }
  3554. read_leb_uint32(p, p_end, name_len);
  3555. if (name_len == 0 || p + name_len > p_end) {
  3556. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3557. return false;
  3558. }
  3559. if (!check_utf8_str(p, name_len)) {
  3560. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  3561. return false;
  3562. }
  3563. buffer_len = sizeof(section_name);
  3564. memset(section_name, 0, buffer_len);
  3565. if (name_len < buffer_len) {
  3566. bh_memcpy_s(section_name, buffer_len, p, name_len);
  3567. }
  3568. else {
  3569. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  3570. memset(section_name + buffer_len - 4, '.', 3);
  3571. }
  3572. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3573. if (memcmp(p, "name", 4) == 0) {
  3574. module->name_section_buf = buf;
  3575. module->name_section_buf_end = buf_end;
  3576. p += name_len;
  3577. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  3578. error_buf_size);
  3579. LOG_VERBOSE("Load custom name section success.");
  3580. return true;
  3581. }
  3582. #endif
  3583. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3584. {
  3585. WASMCustomSection *section =
  3586. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  3587. if (!section) {
  3588. return false;
  3589. }
  3590. section->name_addr = (char *)p;
  3591. section->name_len = name_len;
  3592. section->content_addr = (uint8 *)(p + name_len);
  3593. section->content_len = (uint32)(p_end - p - name_len);
  3594. section->next = module->custom_section_list;
  3595. module->custom_section_list = section;
  3596. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  3597. return true;
  3598. }
  3599. #endif
  3600. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  3601. (void)is_load_from_file_buf;
  3602. (void)module;
  3603. return true;
  3604. fail:
  3605. return false;
  3606. }
  3607. static void
  3608. calculate_global_data_offset(WASMModule *module)
  3609. {
  3610. uint32 i, data_offset;
  3611. data_offset = 0;
  3612. for (i = 0; i < module->import_global_count; i++) {
  3613. WASMGlobalImport *import_global =
  3614. &((module->import_globals + i)->u.global);
  3615. #if WASM_ENABLE_FAST_JIT != 0
  3616. import_global->data_offset = data_offset;
  3617. #endif
  3618. data_offset += wasm_value_type_size(import_global->type);
  3619. }
  3620. for (i = 0; i < module->global_count; i++) {
  3621. WASMGlobal *global = module->globals + i;
  3622. #if WASM_ENABLE_FAST_JIT != 0
  3623. global->data_offset = data_offset;
  3624. #endif
  3625. data_offset += wasm_value_type_size(global->type);
  3626. }
  3627. module->global_data_size = data_offset;
  3628. }
  3629. #if WASM_ENABLE_FAST_JIT != 0
  3630. static bool
  3631. init_fast_jit_functions(WASMModule *module, char *error_buf,
  3632. uint32 error_buf_size)
  3633. {
  3634. #if WASM_ENABLE_LAZY_JIT != 0
  3635. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  3636. #endif
  3637. uint32 i;
  3638. if (!module->function_count)
  3639. return true;
  3640. if (!(module->fast_jit_func_ptrs =
  3641. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  3642. error_buf_size))) {
  3643. return false;
  3644. }
  3645. #if WASM_ENABLE_LAZY_JIT != 0
  3646. for (i = 0; i < module->function_count; i++) {
  3647. module->fast_jit_func_ptrs[i] =
  3648. jit_globals->compile_fast_jit_and_then_call;
  3649. }
  3650. #endif
  3651. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  3652. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  3653. set_error_buf(error_buf, error_buf_size,
  3654. "init fast jit thread lock failed");
  3655. return false;
  3656. }
  3657. module->fast_jit_thread_locks_inited[i] = true;
  3658. }
  3659. return true;
  3660. }
  3661. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  3662. #if WASM_ENABLE_JIT != 0
  3663. static bool
  3664. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  3665. uint32 error_buf_size)
  3666. {
  3667. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  3668. AOTCompOption option = { 0 };
  3669. char *aot_last_error;
  3670. uint64 size;
  3671. bool gc_enabled =
  3672. #if WASM_ENABLE_GC != 0
  3673. true
  3674. #else
  3675. false
  3676. #endif
  3677. ;
  3678. if (module->function_count == 0)
  3679. return true;
  3680. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3681. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  3682. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  3683. return false;
  3684. }
  3685. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  3686. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  3687. os_mutex_destroy(&module->tierup_wait_lock);
  3688. return false;
  3689. }
  3690. module->tierup_wait_lock_inited = true;
  3691. #endif
  3692. size = sizeof(void *) * (uint64)module->function_count
  3693. + sizeof(bool) * (uint64)module->function_count;
  3694. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  3695. return false;
  3696. }
  3697. module->func_ptrs_compiled =
  3698. (bool *)((uint8 *)module->func_ptrs
  3699. + sizeof(void *) * module->function_count);
  3700. module->comp_data = aot_create_comp_data(module, gc_enabled);
  3701. if (!module->comp_data) {
  3702. aot_last_error = aot_get_last_error();
  3703. bh_assert(aot_last_error != NULL);
  3704. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3705. return false;
  3706. }
  3707. option.is_jit_mode = true;
  3708. llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  3709. option.opt_level = llvm_jit_options.opt_level;
  3710. option.size_level = llvm_jit_options.size_level;
  3711. option.segue_flags = llvm_jit_options.segue_flags;
  3712. #if WASM_ENABLE_BULK_MEMORY != 0
  3713. option.enable_bulk_memory = true;
  3714. #endif
  3715. #if WASM_ENABLE_THREAD_MGR != 0
  3716. option.enable_thread_mgr = true;
  3717. #endif
  3718. #if WASM_ENABLE_TAIL_CALL != 0
  3719. option.enable_tail_call = true;
  3720. #endif
  3721. #if WASM_ENABLE_SIMD != 0
  3722. option.enable_simd = true;
  3723. #endif
  3724. #if WASM_ENABLE_REF_TYPES != 0
  3725. option.enable_ref_types = true;
  3726. #endif
  3727. option.enable_aux_stack_check = true;
  3728. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  3729. option.enable_aux_stack_frame = true;
  3730. #endif
  3731. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3732. option.enable_stack_estimation = true;
  3733. #endif
  3734. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  3735. if (!module->comp_ctx) {
  3736. aot_last_error = aot_get_last_error();
  3737. bh_assert(aot_last_error != NULL);
  3738. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3739. return false;
  3740. }
  3741. return true;
  3742. }
  3743. static bool
  3744. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  3745. uint32 error_buf_size)
  3746. {
  3747. char *aot_last_error;
  3748. uint32 i;
  3749. if (module->function_count == 0)
  3750. return true;
  3751. if (!aot_compile_wasm(module->comp_ctx)) {
  3752. aot_last_error = aot_get_last_error();
  3753. bh_assert(aot_last_error != NULL);
  3754. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3755. return false;
  3756. }
  3757. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3758. if (module->orcjit_stop_compiling)
  3759. return false;
  3760. #endif
  3761. bh_print_time("Begin to lookup llvm jit functions");
  3762. for (i = 0; i < module->function_count; i++) {
  3763. LLVMOrcJITTargetAddress func_addr = 0;
  3764. LLVMErrorRef error;
  3765. char func_name[48];
  3766. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  3767. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  3768. func_name);
  3769. if (error != LLVMErrorSuccess) {
  3770. char *err_msg = LLVMGetErrorMessage(error);
  3771. set_error_buf_v(error_buf, error_buf_size,
  3772. "failed to compile llvm jit function: %s", err_msg);
  3773. LLVMDisposeErrorMessage(err_msg);
  3774. return false;
  3775. }
  3776. /**
  3777. * No need to lock the func_ptr[func_idx] here as it is basic
  3778. * data type, the load/store for it can be finished by one cpu
  3779. * instruction, and there can be only one cpu instruction
  3780. * loading/storing at the same time.
  3781. */
  3782. module->func_ptrs[i] = (void *)func_addr;
  3783. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3784. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  3785. if (module->orcjit_stop_compiling)
  3786. return false;
  3787. #endif
  3788. }
  3789. bh_print_time("End lookup llvm jit functions");
  3790. return true;
  3791. }
  3792. #endif /* end of WASM_ENABLE_JIT != 0 */
  3793. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3794. && WASM_ENABLE_LAZY_JIT != 0
  3795. static void *
  3796. init_llvm_jit_functions_stage2_callback(void *arg)
  3797. {
  3798. WASMModule *module = (WASMModule *)arg;
  3799. char error_buf[128];
  3800. uint32 error_buf_size = (uint32)sizeof(error_buf);
  3801. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  3802. module->orcjit_stop_compiling = true;
  3803. return NULL;
  3804. }
  3805. os_mutex_lock(&module->tierup_wait_lock);
  3806. module->llvm_jit_inited = true;
  3807. os_cond_broadcast(&module->tierup_wait_cond);
  3808. os_mutex_unlock(&module->tierup_wait_lock);
  3809. return NULL;
  3810. }
  3811. #endif
  3812. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3813. /* The callback function to compile jit functions */
  3814. static void *
  3815. orcjit_thread_callback(void *arg)
  3816. {
  3817. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  3818. #if WASM_ENABLE_JIT != 0
  3819. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  3820. #endif
  3821. WASMModule *module = thread_arg->module;
  3822. uint32 group_idx = thread_arg->group_idx;
  3823. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  3824. uint32 func_count = module->function_count;
  3825. uint32 i;
  3826. #if WASM_ENABLE_FAST_JIT != 0
  3827. /* Compile fast jit funcitons of this group */
  3828. for (i = group_idx; i < func_count; i += group_stride) {
  3829. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  3830. os_printf("failed to compile fast jit function %u\n", i);
  3831. break;
  3832. }
  3833. if (module->orcjit_stop_compiling) {
  3834. return NULL;
  3835. }
  3836. }
  3837. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3838. os_mutex_lock(&module->tierup_wait_lock);
  3839. module->fast_jit_ready_groups++;
  3840. os_mutex_unlock(&module->tierup_wait_lock);
  3841. #endif
  3842. #endif
  3843. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3844. && WASM_ENABLE_LAZY_JIT != 0
  3845. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  3846. for (i = group_idx; i < func_count;
  3847. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  3848. uint32 j;
  3849. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  3850. if (i + j * group_stride < func_count) {
  3851. if (!jit_compiler_set_call_to_fast_jit(
  3852. module,
  3853. i + j * group_stride + module->import_function_count)) {
  3854. os_printf(
  3855. "failed to compile call_to_fast_jit for func %u\n",
  3856. i + j * group_stride + module->import_function_count);
  3857. module->orcjit_stop_compiling = true;
  3858. return NULL;
  3859. }
  3860. }
  3861. if (module->orcjit_stop_compiling) {
  3862. return NULL;
  3863. }
  3864. }
  3865. }
  3866. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  3867. fast jit functions are compiled */
  3868. os_mutex_lock(&module->tierup_wait_lock);
  3869. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  3870. && module->fast_jit_ready_groups >= group_stride)) {
  3871. os_cond_reltimedwait(&module->tierup_wait_cond,
  3872. &module->tierup_wait_lock, 10000);
  3873. if (module->orcjit_stop_compiling) {
  3874. /* init_llvm_jit_functions_stage2 failed */
  3875. os_mutex_unlock(&module->tierup_wait_lock);
  3876. return NULL;
  3877. }
  3878. }
  3879. os_mutex_unlock(&module->tierup_wait_lock);
  3880. #endif
  3881. #if WASM_ENABLE_JIT != 0
  3882. /* Compile llvm jit functions of this group */
  3883. for (i = group_idx; i < func_count;
  3884. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  3885. LLVMOrcJITTargetAddress func_addr = 0;
  3886. LLVMErrorRef error;
  3887. char func_name[48];
  3888. typedef void (*F)(void);
  3889. union {
  3890. F f;
  3891. void *v;
  3892. } u;
  3893. uint32 j;
  3894. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  3895. "_wrapper");
  3896. LOG_DEBUG("compile llvm jit func %s", func_name);
  3897. error =
  3898. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  3899. if (error != LLVMErrorSuccess) {
  3900. char *err_msg = LLVMGetErrorMessage(error);
  3901. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  3902. LLVMDisposeErrorMessage(err_msg);
  3903. break;
  3904. }
  3905. /* Call the jit wrapper function to trigger its compilation, so as
  3906. to compile the actual jit functions, since we add the latter to
  3907. function list in the PartitionFunction callback */
  3908. u.v = (void *)func_addr;
  3909. u.f();
  3910. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  3911. if (i + j * group_stride < func_count) {
  3912. module->func_ptrs_compiled[i + j * group_stride] = true;
  3913. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3914. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  3915. i + j * group_stride);
  3916. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  3917. func_name);
  3918. if (error != LLVMErrorSuccess) {
  3919. char *err_msg = LLVMGetErrorMessage(error);
  3920. os_printf("failed to compile llvm jit function %u: %s", i,
  3921. err_msg);
  3922. LLVMDisposeErrorMessage(err_msg);
  3923. /* Ignore current llvm jit func, as its func ptr is
  3924. previous set to call_to_fast_jit, which also works */
  3925. continue;
  3926. }
  3927. jit_compiler_set_llvm_jit_func_ptr(
  3928. module,
  3929. i + j * group_stride + module->import_function_count,
  3930. (void *)func_addr);
  3931. /* Try to switch to call this llvm jit funtion instead of
  3932. fast jit function from fast jit jitted code */
  3933. jit_compiler_set_call_to_llvm_jit(
  3934. module,
  3935. i + j * group_stride + module->import_function_count);
  3936. #endif
  3937. }
  3938. }
  3939. if (module->orcjit_stop_compiling) {
  3940. break;
  3941. }
  3942. }
  3943. #endif
  3944. return NULL;
  3945. }
  3946. static void
  3947. orcjit_stop_compile_threads(WASMModule *module)
  3948. {
  3949. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  3950. / sizeof(OrcJitThreadArg));
  3951. module->orcjit_stop_compiling = true;
  3952. for (i = 0; i < thread_num; i++) {
  3953. if (module->orcjit_threads[i])
  3954. os_thread_join(module->orcjit_threads[i], NULL);
  3955. }
  3956. }
  3957. static bool
  3958. compile_jit_functions(WASMModule *module, char *error_buf,
  3959. uint32 error_buf_size)
  3960. {
  3961. uint32 thread_num =
  3962. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  3963. uint32 i, j;
  3964. bh_print_time("Begin to compile jit functions");
  3965. /* Create threads to compile the jit functions */
  3966. for (i = 0; i < thread_num && i < module->function_count; i++) {
  3967. #if WASM_ENABLE_JIT != 0
  3968. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  3969. #endif
  3970. module->orcjit_thread_args[i].module = module;
  3971. module->orcjit_thread_args[i].group_idx = i;
  3972. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  3973. (void *)&module->orcjit_thread_args[i],
  3974. APP_THREAD_STACK_SIZE_DEFAULT)
  3975. != 0) {
  3976. set_error_buf(error_buf, error_buf_size,
  3977. "create orcjit compile thread failed");
  3978. /* Terminate the threads created */
  3979. module->orcjit_stop_compiling = true;
  3980. for (j = 0; j < i; j++) {
  3981. os_thread_join(module->orcjit_threads[j], NULL);
  3982. }
  3983. return false;
  3984. }
  3985. }
  3986. #if WASM_ENABLE_LAZY_JIT == 0
  3987. /* Wait until all jit functions are compiled for eager mode */
  3988. for (i = 0; i < thread_num; i++) {
  3989. if (module->orcjit_threads[i])
  3990. os_thread_join(module->orcjit_threads[i], NULL);
  3991. }
  3992. #if WASM_ENABLE_FAST_JIT != 0
  3993. /* Ensure all the fast-jit functions are compiled */
  3994. for (i = 0; i < module->function_count; i++) {
  3995. if (!jit_compiler_is_compiled(module,
  3996. i + module->import_function_count)) {
  3997. set_error_buf(error_buf, error_buf_size,
  3998. "failed to compile fast jit function");
  3999. return false;
  4000. }
  4001. }
  4002. #endif
  4003. #if WASM_ENABLE_JIT != 0
  4004. /* Ensure all the llvm-jit functions are compiled */
  4005. for (i = 0; i < module->function_count; i++) {
  4006. if (!module->func_ptrs_compiled[i]) {
  4007. set_error_buf(error_buf, error_buf_size,
  4008. "failed to compile llvm jit function");
  4009. return false;
  4010. }
  4011. }
  4012. #endif
  4013. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  4014. bh_print_time("End compile jit functions");
  4015. return true;
  4016. }
  4017. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  4018. static bool
  4019. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4020. uint32 cur_func_idx, char *error_buf,
  4021. uint32 error_buf_size);
  4022. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4023. void **
  4024. wasm_interp_get_handle_table();
  4025. static void **handle_table;
  4026. #endif
  4027. static bool
  4028. load_from_sections(WASMModule *module, WASMSection *sections,
  4029. bool is_load_from_file_buf, char *error_buf,
  4030. uint32 error_buf_size)
  4031. {
  4032. WASMExport *export;
  4033. WASMSection *section = sections;
  4034. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  4035. *buf_func = NULL, *buf_func_end = NULL;
  4036. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  4037. WASMGlobal *aux_stack_top_global = NULL, *global;
  4038. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  4039. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  4040. uint32 aux_data_end_global_index = (uint32)-1;
  4041. uint32 aux_heap_base_global_index = (uint32)-1;
  4042. WASMFuncType *func_type;
  4043. /* Find code and function sections if have */
  4044. while (section) {
  4045. if (section->section_type == SECTION_TYPE_CODE) {
  4046. buf_code = section->section_body;
  4047. buf_code_end = buf_code + section->section_body_size;
  4048. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  4049. module->buf_code = (uint8 *)buf_code;
  4050. module->buf_code_size = section->section_body_size;
  4051. #endif
  4052. }
  4053. else if (section->section_type == SECTION_TYPE_FUNC) {
  4054. buf_func = section->section_body;
  4055. buf_func_end = buf_func + section->section_body_size;
  4056. }
  4057. section = section->next;
  4058. }
  4059. section = sections;
  4060. while (section) {
  4061. buf = section->section_body;
  4062. buf_end = buf + section->section_body_size;
  4063. switch (section->section_type) {
  4064. case SECTION_TYPE_USER:
  4065. /* unsupported user section, ignore it. */
  4066. if (!load_user_section(buf, buf_end, module,
  4067. is_load_from_file_buf, error_buf,
  4068. error_buf_size))
  4069. return false;
  4070. break;
  4071. case SECTION_TYPE_TYPE:
  4072. if (!load_type_section(buf, buf_end, module, error_buf,
  4073. error_buf_size))
  4074. return false;
  4075. break;
  4076. case SECTION_TYPE_IMPORT:
  4077. if (!load_import_section(buf, buf_end, module,
  4078. is_load_from_file_buf, error_buf,
  4079. error_buf_size))
  4080. return false;
  4081. break;
  4082. case SECTION_TYPE_FUNC:
  4083. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  4084. module, error_buf, error_buf_size))
  4085. return false;
  4086. break;
  4087. case SECTION_TYPE_TABLE:
  4088. if (!load_table_section(buf, buf_end, module, error_buf,
  4089. error_buf_size))
  4090. return false;
  4091. break;
  4092. case SECTION_TYPE_MEMORY:
  4093. if (!load_memory_section(buf, buf_end, module, error_buf,
  4094. error_buf_size))
  4095. return false;
  4096. break;
  4097. case SECTION_TYPE_GLOBAL:
  4098. if (!load_global_section(buf, buf_end, module, error_buf,
  4099. error_buf_size))
  4100. return false;
  4101. break;
  4102. case SECTION_TYPE_EXPORT:
  4103. if (!load_export_section(buf, buf_end, module,
  4104. is_load_from_file_buf, error_buf,
  4105. error_buf_size))
  4106. return false;
  4107. break;
  4108. case SECTION_TYPE_START:
  4109. if (!load_start_section(buf, buf_end, module, error_buf,
  4110. error_buf_size))
  4111. return false;
  4112. break;
  4113. case SECTION_TYPE_ELEM:
  4114. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  4115. error_buf_size))
  4116. return false;
  4117. break;
  4118. case SECTION_TYPE_CODE:
  4119. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  4120. module, error_buf, error_buf_size))
  4121. return false;
  4122. break;
  4123. case SECTION_TYPE_DATA:
  4124. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  4125. error_buf_size))
  4126. return false;
  4127. break;
  4128. #if WASM_ENABLE_BULK_MEMORY != 0
  4129. case SECTION_TYPE_DATACOUNT:
  4130. if (!load_datacount_section(buf, buf_end, module, error_buf,
  4131. error_buf_size))
  4132. return false;
  4133. break;
  4134. #endif
  4135. default:
  4136. set_error_buf(error_buf, error_buf_size, "invalid section id");
  4137. return false;
  4138. }
  4139. section = section->next;
  4140. }
  4141. module->aux_data_end_global_index = (uint32)-1;
  4142. module->aux_heap_base_global_index = (uint32)-1;
  4143. module->aux_stack_top_global_index = (uint32)-1;
  4144. /* Resolve auxiliary data/stack/heap info and reset memory info */
  4145. export = module->exports;
  4146. for (i = 0; i < module->export_count; i++, export ++) {
  4147. if (export->kind == EXPORT_KIND_GLOBAL) {
  4148. if (!strcmp(export->name, "__heap_base")) {
  4149. global_index = export->index - module->import_global_count;
  4150. global = module->globals + global_index;
  4151. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4152. && global->init_expr.init_expr_type
  4153. == INIT_EXPR_TYPE_I32_CONST) {
  4154. aux_heap_base_global = global;
  4155. aux_heap_base = global->init_expr.u.i32;
  4156. aux_heap_base_global_index = export->index;
  4157. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  4158. aux_heap_base);
  4159. }
  4160. }
  4161. else if (!strcmp(export->name, "__data_end")) {
  4162. global_index = export->index - module->import_global_count;
  4163. global = module->globals + global_index;
  4164. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4165. && global->init_expr.init_expr_type
  4166. == INIT_EXPR_TYPE_I32_CONST) {
  4167. aux_data_end_global = global;
  4168. aux_data_end = global->init_expr.u.i32;
  4169. aux_data_end_global_index = export->index;
  4170. LOG_VERBOSE("Found aux __data_end global, value: %d",
  4171. aux_data_end);
  4172. aux_data_end = align_uint(aux_data_end, 16);
  4173. }
  4174. }
  4175. /* For module compiled with -pthread option, the global is:
  4176. [0] stack_top <-- 0
  4177. [1] tls_pointer
  4178. [2] tls_size
  4179. [3] data_end <-- 3
  4180. [4] global_base
  4181. [5] heap_base <-- 5
  4182. [6] dso_handle
  4183. For module compiled without -pthread option:
  4184. [0] stack_top <-- 0
  4185. [1] data_end <-- 1
  4186. [2] global_base
  4187. [3] heap_base <-- 3
  4188. [4] dso_handle
  4189. */
  4190. if (aux_data_end_global && aux_heap_base_global
  4191. && aux_data_end <= aux_heap_base) {
  4192. module->aux_data_end_global_index = aux_data_end_global_index;
  4193. module->aux_data_end = aux_data_end;
  4194. module->aux_heap_base_global_index = aux_heap_base_global_index;
  4195. module->aux_heap_base = aux_heap_base;
  4196. /* Resolve aux stack top global */
  4197. for (global_index = 0; global_index < module->global_count;
  4198. global_index++) {
  4199. global = module->globals + global_index;
  4200. if (global->is_mutable /* heap_base and data_end is
  4201. not mutable */
  4202. && global->type == VALUE_TYPE_I32
  4203. && global->init_expr.init_expr_type
  4204. == INIT_EXPR_TYPE_I32_CONST
  4205. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  4206. aux_stack_top_global = global;
  4207. aux_stack_top = (uint32)global->init_expr.u.i32;
  4208. module->aux_stack_top_global_index =
  4209. module->import_global_count + global_index;
  4210. module->aux_stack_bottom = aux_stack_top;
  4211. module->aux_stack_size =
  4212. aux_stack_top > aux_data_end
  4213. ? aux_stack_top - aux_data_end
  4214. : aux_stack_top;
  4215. LOG_VERBOSE("Found aux stack top global, value: %d, "
  4216. "global index: %d, stack size: %d",
  4217. aux_stack_top, global_index,
  4218. module->aux_stack_size);
  4219. break;
  4220. }
  4221. }
  4222. if (!aux_stack_top_global) {
  4223. /* Auxiliary stack global isn't found, it must be unused
  4224. in the wasm app, as if it is used, the global must be
  4225. defined. Here we set it to __heap_base global and set
  4226. its size to 0. */
  4227. aux_stack_top_global = aux_heap_base_global;
  4228. aux_stack_top = aux_heap_base;
  4229. module->aux_stack_top_global_index =
  4230. module->aux_heap_base_global_index;
  4231. module->aux_stack_bottom = aux_stack_top;
  4232. module->aux_stack_size = 0;
  4233. }
  4234. break;
  4235. }
  4236. }
  4237. }
  4238. module->malloc_function = (uint32)-1;
  4239. module->free_function = (uint32)-1;
  4240. module->retain_function = (uint32)-1;
  4241. /* Resolve malloc/free function exported by wasm module */
  4242. export = module->exports;
  4243. for (i = 0; i < module->export_count; i++, export ++) {
  4244. if (export->kind == EXPORT_KIND_FUNC) {
  4245. if (!strcmp(export->name, "malloc")
  4246. && export->index >= module->import_function_count) {
  4247. func_index = export->index - module->import_function_count;
  4248. func_type = module->functions[func_index]->func_type;
  4249. if (func_type->param_count == 1 && func_type->result_count == 1
  4250. && func_type->types[0] == VALUE_TYPE_I32
  4251. && func_type->types[1] == VALUE_TYPE_I32) {
  4252. bh_assert(module->malloc_function == (uint32)-1);
  4253. module->malloc_function = export->index;
  4254. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  4255. export->name, export->index);
  4256. }
  4257. }
  4258. else if (!strcmp(export->name, "__new")
  4259. && export->index >= module->import_function_count) {
  4260. /* __new && __pin for AssemblyScript */
  4261. func_index = export->index - module->import_function_count;
  4262. func_type = module->functions[func_index]->func_type;
  4263. if (func_type->param_count == 2 && func_type->result_count == 1
  4264. && func_type->types[0] == VALUE_TYPE_I32
  4265. && func_type->types[1] == VALUE_TYPE_I32
  4266. && func_type->types[2] == VALUE_TYPE_I32) {
  4267. uint32 j;
  4268. WASMExport *export_tmp;
  4269. bh_assert(module->malloc_function == (uint32)-1);
  4270. module->malloc_function = export->index;
  4271. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  4272. export->name, export->index);
  4273. /* resolve retain function.
  4274. If not found, reset malloc function index */
  4275. export_tmp = module->exports;
  4276. for (j = 0; j < module->export_count; j++, export_tmp++) {
  4277. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  4278. && (!strcmp(export_tmp->name, "__retain")
  4279. || (!strcmp(export_tmp->name, "__pin")))
  4280. && (export_tmp->index
  4281. >= module->import_function_count)) {
  4282. func_index = export_tmp->index
  4283. - module->import_function_count;
  4284. func_type =
  4285. module->functions[func_index]->func_type;
  4286. if (func_type->param_count == 1
  4287. && func_type->result_count == 1
  4288. && func_type->types[0] == VALUE_TYPE_I32
  4289. && func_type->types[1] == VALUE_TYPE_I32) {
  4290. bh_assert(module->retain_function
  4291. == (uint32)-1);
  4292. module->retain_function = export_tmp->index;
  4293. LOG_VERBOSE("Found retain function, name: %s, "
  4294. "index: %u",
  4295. export_tmp->name,
  4296. export_tmp->index);
  4297. break;
  4298. }
  4299. }
  4300. }
  4301. if (j == module->export_count) {
  4302. module->malloc_function = (uint32)-1;
  4303. LOG_VERBOSE("Can't find retain function,"
  4304. "reset malloc function index to -1");
  4305. }
  4306. }
  4307. }
  4308. else if (((!strcmp(export->name, "free"))
  4309. || (!strcmp(export->name, "__release"))
  4310. || (!strcmp(export->name, "__unpin")))
  4311. && export->index >= module->import_function_count) {
  4312. func_index = export->index - module->import_function_count;
  4313. func_type = module->functions[func_index]->func_type;
  4314. if (func_type->param_count == 1 && func_type->result_count == 0
  4315. && func_type->types[0] == VALUE_TYPE_I32) {
  4316. bh_assert(module->free_function == (uint32)-1);
  4317. module->free_function = export->index;
  4318. LOG_VERBOSE("Found free function, name: %s, index: %u",
  4319. export->name, export->index);
  4320. }
  4321. }
  4322. }
  4323. }
  4324. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4325. handle_table = wasm_interp_get_handle_table();
  4326. #endif
  4327. for (i = 0; i < module->function_count; i++) {
  4328. WASMFunction *func = module->functions[i];
  4329. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  4330. error_buf_size)) {
  4331. return false;
  4332. }
  4333. if (i == module->function_count - 1
  4334. && func->code + func->code_size != buf_code_end) {
  4335. set_error_buf(error_buf, error_buf_size,
  4336. "code section size mismatch");
  4337. return false;
  4338. }
  4339. }
  4340. if (!module->possible_memory_grow) {
  4341. WASMMemoryImport *memory_import;
  4342. WASMMemory *memory;
  4343. if (aux_data_end_global && aux_heap_base_global
  4344. && aux_stack_top_global) {
  4345. uint64 init_memory_size;
  4346. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  4347. if (module->import_memory_count) {
  4348. memory_import = &module->import_memories[0].u.memory;
  4349. init_memory_size = (uint64)memory_import->num_bytes_per_page
  4350. * memory_import->init_page_count;
  4351. if (shrunk_memory_size <= init_memory_size) {
  4352. /* Reset memory info to decrease memory usage */
  4353. memory_import->num_bytes_per_page = shrunk_memory_size;
  4354. memory_import->init_page_count = 1;
  4355. LOG_VERBOSE("Shrink import memory size to %d",
  4356. shrunk_memory_size);
  4357. }
  4358. }
  4359. if (module->memory_count) {
  4360. memory = &module->memories[0];
  4361. init_memory_size = (uint64)memory->num_bytes_per_page
  4362. * memory->init_page_count;
  4363. if (shrunk_memory_size <= init_memory_size) {
  4364. /* Reset memory info to decrease memory usage */
  4365. memory->num_bytes_per_page = shrunk_memory_size;
  4366. memory->init_page_count = 1;
  4367. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  4368. }
  4369. }
  4370. }
  4371. #if WASM_ENABLE_MULTI_MODULE == 0
  4372. if (module->import_memory_count) {
  4373. memory_import = &module->import_memories[0].u.memory;
  4374. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  4375. memory_import->num_bytes_per_page *=
  4376. memory_import->init_page_count;
  4377. else
  4378. memory_import->num_bytes_per_page = UINT32_MAX;
  4379. if (memory_import->init_page_count > 0)
  4380. memory_import->init_page_count = memory_import->max_page_count =
  4381. 1;
  4382. else
  4383. memory_import->init_page_count = memory_import->max_page_count =
  4384. 0;
  4385. }
  4386. if (module->memory_count) {
  4387. memory = &module->memories[0];
  4388. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  4389. memory->num_bytes_per_page *= memory->init_page_count;
  4390. else
  4391. memory->num_bytes_per_page = UINT32_MAX;
  4392. if (memory->init_page_count > 0)
  4393. memory->init_page_count = memory->max_page_count = 1;
  4394. else
  4395. memory->init_page_count = memory->max_page_count = 0;
  4396. }
  4397. #endif
  4398. }
  4399. calculate_global_data_offset(module);
  4400. #if WASM_ENABLE_FAST_JIT != 0
  4401. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  4402. return false;
  4403. }
  4404. #endif
  4405. #if WASM_ENABLE_JIT != 0
  4406. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  4407. return false;
  4408. }
  4409. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  4410. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4411. return false;
  4412. }
  4413. #else
  4414. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  4415. thread fast jit execution, since applying llvm optimizations in
  4416. aot_compile_wasm may cost a lot of time.
  4417. Create thread with enough native stack to apply llvm optimizations */
  4418. if (os_thread_create(&module->llvm_jit_init_thread,
  4419. init_llvm_jit_functions_stage2_callback,
  4420. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  4421. != 0) {
  4422. set_error_buf(error_buf, error_buf_size,
  4423. "create orcjit compile thread failed");
  4424. return false;
  4425. }
  4426. #endif
  4427. #endif
  4428. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4429. /* Create threads to compile the jit functions */
  4430. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  4431. return false;
  4432. }
  4433. #endif
  4434. #if WASM_ENABLE_MEMORY_TRACING != 0
  4435. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  4436. #endif
  4437. return true;
  4438. }
  4439. static WASMModule *
  4440. create_module(char *error_buf, uint32 error_buf_size)
  4441. {
  4442. WASMModule *module =
  4443. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  4444. bh_list_status ret;
  4445. if (!module) {
  4446. return NULL;
  4447. }
  4448. module->module_type = Wasm_Module_Bytecode;
  4449. /* Set start_function to -1, means no start function */
  4450. module->start_function = (uint32)-1;
  4451. #if WASM_ENABLE_FAST_INTERP == 0
  4452. module->br_table_cache_list = &module->br_table_cache_list_head;
  4453. ret = bh_list_init(module->br_table_cache_list);
  4454. bh_assert(ret == BH_LIST_SUCCESS);
  4455. #endif
  4456. #if WASM_ENABLE_MULTI_MODULE != 0
  4457. module->import_module_list = &module->import_module_list_head;
  4458. ret = bh_list_init(module->import_module_list);
  4459. bh_assert(ret == BH_LIST_SUCCESS);
  4460. #endif
  4461. #if WASM_ENABLE_DEBUG_INTERP != 0
  4462. ret = bh_list_init(&module->fast_opcode_list);
  4463. bh_assert(ret == BH_LIST_SUCCESS);
  4464. #endif
  4465. #if WASM_ENABLE_GC != 0
  4466. if (!(module->ref_type_set =
  4467. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  4468. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  4469. goto fail1;
  4470. }
  4471. if (os_mutex_init(&module->rtt_type_lock)) {
  4472. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  4473. goto fail2;
  4474. }
  4475. #endif
  4476. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4477. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4478. && WASM_ENABLE_LAZY_JIT != 0)
  4479. if (os_mutex_init(&module->instance_list_lock) != 0) {
  4480. set_error_buf(error_buf, error_buf_size,
  4481. "init instance list lock failed");
  4482. goto fail3;
  4483. }
  4484. #endif
  4485. (void)ret;
  4486. return module;
  4487. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4488. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  4489. && WASM_ENABLE_LAZY_JIT != 0)
  4490. fail3:
  4491. #endif
  4492. #if WASM_ENABLE_GC != 0
  4493. os_mutex_destroy(&module->rtt_type_lock);
  4494. fail2:
  4495. bh_hash_map_destroy(module->ref_type_set);
  4496. fail1:
  4497. #endif
  4498. wasm_runtime_free(module);
  4499. return NULL;
  4500. }
  4501. #if WASM_ENABLE_DEBUG_INTERP != 0
  4502. static bool
  4503. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  4504. uint32 error_buf_size)
  4505. {
  4506. WASMFastOPCodeNode *fast_op =
  4507. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  4508. if (fast_op) {
  4509. fast_op->offset = pos - module->load_addr;
  4510. fast_op->orig_op = orig_op;
  4511. bh_list_insert(&module->fast_opcode_list, fast_op);
  4512. }
  4513. return fast_op ? true : false;
  4514. }
  4515. #endif
  4516. WASMModule *
  4517. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  4518. uint32 error_buf_size)
  4519. {
  4520. WASMModule *module = create_module(error_buf, error_buf_size);
  4521. if (!module)
  4522. return NULL;
  4523. if (!load_from_sections(module, section_list, false, error_buf,
  4524. error_buf_size)) {
  4525. wasm_loader_unload(module);
  4526. return NULL;
  4527. }
  4528. LOG_VERBOSE("Load module from sections success.\n");
  4529. return module;
  4530. }
  4531. static void
  4532. destroy_sections(WASMSection *section_list)
  4533. {
  4534. WASMSection *section = section_list, *next;
  4535. while (section) {
  4536. next = section->next;
  4537. wasm_runtime_free(section);
  4538. section = next;
  4539. }
  4540. }
  4541. /* clang-format off */
  4542. static uint8 section_ids[] = {
  4543. SECTION_TYPE_USER,
  4544. SECTION_TYPE_TYPE,
  4545. SECTION_TYPE_IMPORT,
  4546. SECTION_TYPE_FUNC,
  4547. SECTION_TYPE_TABLE,
  4548. SECTION_TYPE_MEMORY,
  4549. SECTION_TYPE_GLOBAL,
  4550. SECTION_TYPE_EXPORT,
  4551. SECTION_TYPE_START,
  4552. SECTION_TYPE_ELEM,
  4553. #if WASM_ENABLE_BULK_MEMORY != 0
  4554. SECTION_TYPE_DATACOUNT,
  4555. #endif
  4556. SECTION_TYPE_CODE,
  4557. SECTION_TYPE_DATA
  4558. };
  4559. /* clang-format on */
  4560. static uint8
  4561. get_section_index(uint8 section_type)
  4562. {
  4563. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  4564. for (uint8 i = 0; i < max_id; i++) {
  4565. if (section_type == section_ids[i])
  4566. return i;
  4567. }
  4568. return (uint8)-1;
  4569. }
  4570. static bool
  4571. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  4572. char *error_buf, uint32 error_buf_size)
  4573. {
  4574. WASMSection *section_list_end = NULL, *section;
  4575. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  4576. uint8 section_type, section_index, last_section_index = (uint8)-1;
  4577. uint32 section_size;
  4578. bh_assert(!*p_section_list);
  4579. p += 8;
  4580. while (p < p_end) {
  4581. CHECK_BUF(p, p_end, 1);
  4582. section_type = read_uint8(p);
  4583. section_index = get_section_index(section_type);
  4584. if (section_index != (uint8)-1) {
  4585. if (section_type != SECTION_TYPE_USER) {
  4586. /* Custom sections may be inserted at any place,
  4587. while other sections must occur at most once
  4588. and in prescribed order. */
  4589. if (last_section_index != (uint8)-1
  4590. && (section_index <= last_section_index)) {
  4591. set_error_buf(error_buf, error_buf_size,
  4592. "unexpected content after last section or "
  4593. "junk after last section");
  4594. return false;
  4595. }
  4596. last_section_index = section_index;
  4597. }
  4598. read_leb_uint32(p, p_end, section_size);
  4599. CHECK_BUF1(p, p_end, section_size);
  4600. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  4601. error_buf_size))) {
  4602. return false;
  4603. }
  4604. section->section_type = section_type;
  4605. section->section_body = (uint8 *)p;
  4606. section->section_body_size = section_size;
  4607. if (!section_list_end)
  4608. *p_section_list = section_list_end = section;
  4609. else {
  4610. section_list_end->next = section;
  4611. section_list_end = section;
  4612. }
  4613. p += section_size;
  4614. }
  4615. else {
  4616. set_error_buf(error_buf, error_buf_size, "invalid section id");
  4617. return false;
  4618. }
  4619. }
  4620. return true;
  4621. fail:
  4622. return false;
  4623. }
  4624. static void
  4625. exchange32(uint8 *p_data)
  4626. {
  4627. uint8 value = *p_data;
  4628. *p_data = *(p_data + 3);
  4629. *(p_data + 3) = value;
  4630. value = *(p_data + 1);
  4631. *(p_data + 1) = *(p_data + 2);
  4632. *(p_data + 2) = value;
  4633. }
  4634. static union {
  4635. int a;
  4636. char b;
  4637. } __ue = { .a = 1 };
  4638. #define is_little_endian() (__ue.b == 1)
  4639. static bool
  4640. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  4641. uint32 error_buf_size)
  4642. {
  4643. const uint8 *buf_end = buf + size;
  4644. const uint8 *p = buf, *p_end = buf_end;
  4645. uint32 magic_number, version;
  4646. WASMSection *section_list = NULL;
  4647. CHECK_BUF1(p, p_end, sizeof(uint32));
  4648. magic_number = read_uint32(p);
  4649. if (!is_little_endian())
  4650. exchange32((uint8 *)&magic_number);
  4651. if (magic_number != WASM_MAGIC_NUMBER) {
  4652. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  4653. return false;
  4654. }
  4655. CHECK_BUF1(p, p_end, sizeof(uint32));
  4656. version = read_uint32(p);
  4657. if (!is_little_endian())
  4658. exchange32((uint8 *)&version);
  4659. if (version != WASM_CURRENT_VERSION) {
  4660. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  4661. return false;
  4662. }
  4663. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  4664. || !load_from_sections(module, section_list, true, error_buf,
  4665. error_buf_size)) {
  4666. destroy_sections(section_list);
  4667. return false;
  4668. }
  4669. destroy_sections(section_list);
  4670. return true;
  4671. fail:
  4672. return false;
  4673. }
  4674. #if WASM_ENABLE_LIBC_WASI != 0
  4675. /**
  4676. * refer to
  4677. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  4678. */
  4679. static bool
  4680. check_wasi_abi_compatibility(const WASMModule *module,
  4681. #if WASM_ENABLE_MULTI_MODULE != 0
  4682. bool main_module,
  4683. #endif
  4684. char *error_buf, uint32 error_buf_size)
  4685. {
  4686. /**
  4687. * be careful with:
  4688. * wasi compatiable modules(command/reactor) which don't import any wasi
  4689. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  4690. * reactor can depend on nothing. At the same time, each has its own entry
  4691. * point.
  4692. *
  4693. * observations:
  4694. * - clang always injects `_start` into a command
  4695. * - clang always injects `_initialize` into a reactor
  4696. * - `iwasm -f` allows to run a function in the reactor
  4697. *
  4698. * strong assumptions:
  4699. * - no one will define either `_start` or `_initialize` on purpose
  4700. * - `_start` should always be `void _start(void)`
  4701. * - `_initialize` should always be `void _initialize(void)`
  4702. *
  4703. */
  4704. /* clang-format off */
  4705. /**
  4706. *
  4707. * | | import_wasi_api True | | import_wasi_api False | |
  4708. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  4709. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  4710. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  4711. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  4712. */
  4713. /* clang-format on */
  4714. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  4715. /* (func (export "_start") (...) */
  4716. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  4717. error_buf, error_buf_size);
  4718. if (start) {
  4719. WASMFuncType *func_type =
  4720. module->functions[start->index - module->import_function_count]
  4721. ->func_type;
  4722. if (func_type->param_count || func_type->result_count) {
  4723. set_error_buf(error_buf, error_buf_size,
  4724. "the signature of builtin _start function is wrong");
  4725. return false;
  4726. }
  4727. }
  4728. /* (func (export "_initialize") (...) */
  4729. initialize = wasm_loader_find_export(
  4730. module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
  4731. if (initialize) {
  4732. WASMFuncType *func_type =
  4733. module->functions[initialize->index - module->import_function_count]
  4734. ->func_type;
  4735. if (func_type->param_count || func_type->result_count) {
  4736. set_error_buf(
  4737. error_buf, error_buf_size,
  4738. "the signature of builtin _initialize function is wrong");
  4739. return false;
  4740. }
  4741. }
  4742. /* filter out non-wasi compatiable modules */
  4743. if (!module->import_wasi_api && !start && !initialize) {
  4744. return true;
  4745. }
  4746. /* should have one at least */
  4747. if (module->import_wasi_api && !start && !initialize) {
  4748. LOG_WARNING("warning: a module with WASI apis should be either "
  4749. "a command or a reactor");
  4750. }
  4751. /*
  4752. * there is at least one of `_start` and `_initialize` in below cases.
  4753. * according to the assumption, they should be all wasi compatiable
  4754. */
  4755. /* always can not have both at the same time */
  4756. if (start && initialize) {
  4757. set_error_buf(
  4758. error_buf, error_buf_size,
  4759. "neither a command nor a reactor can both have _start function "
  4760. "and _initialize function at the same time");
  4761. return false;
  4762. }
  4763. #if WASM_ENABLE_MULTI_MODULE != 0
  4764. /* filter out commands (with `_start`) cases */
  4765. if (start && !main_module) {
  4766. set_error_buf(
  4767. error_buf, error_buf_size,
  4768. "a command (with _start function) can not be a sub-module");
  4769. return false;
  4770. }
  4771. #endif
  4772. /*
  4773. * it is ok a reactor acts as a main module,
  4774. * so skip the check about (with `_initialize`)
  4775. */
  4776. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  4777. error_buf, error_buf_size);
  4778. if (!memory
  4779. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  4780. /*
  4781. * with wasi-threads, it's still an open question if a memory
  4782. * should be exported.
  4783. *
  4784. * https://github.com/WebAssembly/wasi-threads/issues/22
  4785. * https://github.com/WebAssembly/WASI/issues/502
  4786. *
  4787. * Note: this code assumes the number of memories is at most 1.
  4788. */
  4789. && module->import_memory_count == 0
  4790. #endif
  4791. ) {
  4792. set_error_buf(error_buf, error_buf_size,
  4793. "a module with WASI apis must export memory by default");
  4794. return false;
  4795. }
  4796. return true;
  4797. }
  4798. #endif
  4799. WASMModule *
  4800. wasm_loader_load(uint8 *buf, uint32 size,
  4801. #if WASM_ENABLE_MULTI_MODULE != 0
  4802. bool main_module,
  4803. #endif
  4804. char *error_buf, uint32 error_buf_size)
  4805. {
  4806. WASMModule *module = create_module(error_buf, error_buf_size);
  4807. if (!module) {
  4808. return NULL;
  4809. }
  4810. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
  4811. module->load_addr = (uint8 *)buf;
  4812. module->load_size = size;
  4813. #endif
  4814. if (!load(buf, size, module, error_buf, error_buf_size)) {
  4815. goto fail;
  4816. }
  4817. #if WASM_ENABLE_LIBC_WASI != 0
  4818. /* Check the WASI application ABI */
  4819. if (!check_wasi_abi_compatibility(module,
  4820. #if WASM_ENABLE_MULTI_MODULE != 0
  4821. main_module,
  4822. #endif
  4823. error_buf, error_buf_size)) {
  4824. goto fail;
  4825. }
  4826. #endif
  4827. LOG_VERBOSE("Load module success.\n");
  4828. return module;
  4829. fail:
  4830. wasm_loader_unload(module);
  4831. return NULL;
  4832. }
  4833. void
  4834. wasm_loader_unload(WASMModule *module)
  4835. {
  4836. uint32 i;
  4837. if (!module)
  4838. return;
  4839. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4840. && WASM_ENABLE_LAZY_JIT != 0
  4841. module->orcjit_stop_compiling = true;
  4842. if (module->llvm_jit_init_thread)
  4843. os_thread_join(module->llvm_jit_init_thread, NULL);
  4844. #endif
  4845. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4846. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  4847. module internal data after they were freed */
  4848. orcjit_stop_compile_threads(module);
  4849. #endif
  4850. #if WASM_ENABLE_JIT != 0
  4851. if (module->func_ptrs)
  4852. wasm_runtime_free(module->func_ptrs);
  4853. if (module->comp_ctx)
  4854. aot_destroy_comp_context(module->comp_ctx);
  4855. if (module->comp_data)
  4856. aot_destroy_comp_data(module->comp_data);
  4857. #endif
  4858. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4859. && WASM_ENABLE_LAZY_JIT != 0
  4860. if (module->tierup_wait_lock_inited) {
  4861. os_mutex_destroy(&module->tierup_wait_lock);
  4862. os_cond_destroy(&module->tierup_wait_cond);
  4863. }
  4864. #endif
  4865. if (module->types) {
  4866. for (i = 0; i < module->type_count; i++) {
  4867. if (module->types[i])
  4868. destroy_wasm_type(module->types[i]);
  4869. }
  4870. wasm_runtime_free(module->types);
  4871. }
  4872. if (module->imports)
  4873. wasm_runtime_free(module->imports);
  4874. if (module->functions) {
  4875. for (i = 0; i < module->function_count; i++) {
  4876. if (module->functions[i]) {
  4877. if (module->functions[i]->local_offsets)
  4878. wasm_runtime_free(module->functions[i]->local_offsets);
  4879. #if WASM_ENABLE_FAST_INTERP != 0
  4880. if (module->functions[i]->code_compiled)
  4881. wasm_runtime_free(module->functions[i]->code_compiled);
  4882. if (module->functions[i]->consts)
  4883. wasm_runtime_free(module->functions[i]->consts);
  4884. #endif
  4885. #if WASM_ENABLE_FAST_JIT != 0
  4886. if (module->functions[i]->fast_jit_jitted_code) {
  4887. jit_code_cache_free(
  4888. module->functions[i]->fast_jit_jitted_code);
  4889. }
  4890. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4891. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  4892. jit_code_cache_free(
  4893. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  4894. }
  4895. #endif
  4896. #endif
  4897. #if WASM_ENABLE_GC != 0
  4898. if (module->functions[i]->local_ref_type_maps) {
  4899. wasm_runtime_free(
  4900. module->functions[i]->local_ref_type_maps);
  4901. }
  4902. #endif
  4903. wasm_runtime_free(module->functions[i]);
  4904. }
  4905. }
  4906. wasm_runtime_free(module->functions);
  4907. }
  4908. if (module->tables)
  4909. wasm_runtime_free(module->tables);
  4910. if (module->memories)
  4911. wasm_runtime_free(module->memories);
  4912. if (module->globals)
  4913. wasm_runtime_free(module->globals);
  4914. if (module->exports)
  4915. wasm_runtime_free(module->exports);
  4916. if (module->table_segments) {
  4917. for (i = 0; i < module->table_seg_count; i++) {
  4918. if (module->table_segments[i].func_indexes)
  4919. wasm_runtime_free(module->table_segments[i].func_indexes);
  4920. }
  4921. wasm_runtime_free(module->table_segments);
  4922. }
  4923. if (module->data_segments) {
  4924. for (i = 0; i < module->data_seg_count; i++) {
  4925. if (module->data_segments[i])
  4926. wasm_runtime_free(module->data_segments[i]);
  4927. }
  4928. wasm_runtime_free(module->data_segments);
  4929. }
  4930. if (module->const_str_list) {
  4931. StringNode *node = module->const_str_list, *node_next;
  4932. while (node) {
  4933. node_next = node->next;
  4934. wasm_runtime_free(node);
  4935. node = node_next;
  4936. }
  4937. }
  4938. #if WASM_ENABLE_FAST_INTERP == 0
  4939. if (module->br_table_cache_list) {
  4940. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  4941. BrTableCache *node_next;
  4942. while (node) {
  4943. node_next = bh_list_elem_next(node);
  4944. wasm_runtime_free(node);
  4945. node = node_next;
  4946. }
  4947. }
  4948. #endif
  4949. #if WASM_ENABLE_MULTI_MODULE != 0
  4950. /* just release the sub module list */
  4951. if (module->import_module_list) {
  4952. WASMRegisteredModule *node =
  4953. bh_list_first_elem(module->import_module_list);
  4954. while (node) {
  4955. WASMRegisteredModule *next = bh_list_elem_next(node);
  4956. bh_list_remove(module->import_module_list, node);
  4957. /*
  4958. * unload(sub_module) will be trigged during runtime_destroy().
  4959. * every module in the global module list will be unloaded one by
  4960. * one. so don't worry.
  4961. */
  4962. wasm_runtime_free(node);
  4963. /*
  4964. * the module file reading buffer will be released
  4965. * in runtime_destroy()
  4966. */
  4967. node = next;
  4968. }
  4969. }
  4970. #endif
  4971. #if WASM_ENABLE_DEBUG_INTERP != 0
  4972. WASMFastOPCodeNode *fast_opcode =
  4973. bh_list_first_elem(&module->fast_opcode_list);
  4974. while (fast_opcode) {
  4975. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  4976. wasm_runtime_free(fast_opcode);
  4977. fast_opcode = next;
  4978. }
  4979. #endif
  4980. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4981. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4982. && WASM_ENABLE_LAZY_JIT != 0)
  4983. os_mutex_destroy(&module->instance_list_lock);
  4984. #endif
  4985. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4986. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  4987. #endif
  4988. #if WASM_ENABLE_FAST_JIT != 0
  4989. if (module->fast_jit_func_ptrs) {
  4990. wasm_runtime_free(module->fast_jit_func_ptrs);
  4991. }
  4992. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4993. if (module->fast_jit_thread_locks_inited[i]) {
  4994. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  4995. }
  4996. }
  4997. #endif
  4998. #if WASM_ENABLE_GC != 0
  4999. os_mutex_destroy(&module->rtt_type_lock);
  5000. bh_hash_map_destroy(module->ref_type_set);
  5001. if (module->rtt_types) {
  5002. for (i = 0; i < module->type_count; i++) {
  5003. if (module->rtt_types[i])
  5004. wasm_runtime_free(module->rtt_types[i]);
  5005. }
  5006. wasm_runtime_free(module->rtt_types);
  5007. }
  5008. #endif
  5009. wasm_runtime_free(module);
  5010. }
  5011. bool
  5012. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  5013. const uint8 *start_addr, const uint8 *code_end_addr,
  5014. uint8 label_type, uint8 **p_else_addr,
  5015. uint8 **p_end_addr)
  5016. {
  5017. const uint8 *p = start_addr, *p_end = code_end_addr;
  5018. uint8 *else_addr = NULL;
  5019. char error_buf[128];
  5020. uint32 block_nested_depth = 1, count, i, j, t;
  5021. uint32 error_buf_size = sizeof(error_buf);
  5022. uint8 opcode, u8;
  5023. BlockAddr block_stack[16] = { { 0 } }, *block;
  5024. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  5025. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  5026. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  5027. if (block[j].start_addr == start_addr) {
  5028. /* Cache hit */
  5029. *p_else_addr = block[j].else_addr;
  5030. *p_end_addr = block[j].end_addr;
  5031. return true;
  5032. }
  5033. }
  5034. /* Cache unhit */
  5035. block_stack[0].start_addr = start_addr;
  5036. while (p < code_end_addr) {
  5037. opcode = *p++;
  5038. #if WASM_ENABLE_DEBUG_INTERP != 0
  5039. op_break_retry:
  5040. #endif
  5041. switch (opcode) {
  5042. case WASM_OP_UNREACHABLE:
  5043. case WASM_OP_NOP:
  5044. break;
  5045. case WASM_OP_BLOCK:
  5046. case WASM_OP_LOOP:
  5047. case WASM_OP_IF:
  5048. {
  5049. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  5050. u8 = read_uint8(p);
  5051. if (is_byte_a_type(u8)) {
  5052. #if WASM_ENABLE_GC != 0
  5053. if (wasm_is_type_multi_byte_type(u8)) {
  5054. /* the possible extra bytes of GC ref type have been
  5055. modified to OP_NOP, no need to resolve them again */
  5056. }
  5057. #endif
  5058. }
  5059. else {
  5060. p--;
  5061. skip_leb_uint32(p, p_end);
  5062. }
  5063. if (block_nested_depth
  5064. < sizeof(block_stack) / sizeof(BlockAddr)) {
  5065. block_stack[block_nested_depth].start_addr = p;
  5066. block_stack[block_nested_depth].else_addr = NULL;
  5067. }
  5068. block_nested_depth++;
  5069. break;
  5070. }
  5071. case EXT_OP_BLOCK:
  5072. case EXT_OP_LOOP:
  5073. case EXT_OP_IF:
  5074. /* block type */
  5075. skip_leb_uint32(p, p_end);
  5076. if (block_nested_depth
  5077. < sizeof(block_stack) / sizeof(BlockAddr)) {
  5078. block_stack[block_nested_depth].start_addr = p;
  5079. block_stack[block_nested_depth].else_addr = NULL;
  5080. }
  5081. block_nested_depth++;
  5082. break;
  5083. case WASM_OP_ELSE:
  5084. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  5085. else_addr = (uint8 *)(p - 1);
  5086. if (block_nested_depth - 1
  5087. < sizeof(block_stack) / sizeof(BlockAddr))
  5088. block_stack[block_nested_depth - 1].else_addr =
  5089. (uint8 *)(p - 1);
  5090. break;
  5091. case WASM_OP_END:
  5092. if (block_nested_depth == 1) {
  5093. if (label_type == LABEL_TYPE_IF)
  5094. *p_else_addr = else_addr;
  5095. *p_end_addr = (uint8 *)(p - 1);
  5096. block_stack[0].end_addr = (uint8 *)(p - 1);
  5097. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  5098. t++) {
  5099. start_addr = block_stack[t].start_addr;
  5100. if (start_addr) {
  5101. i = ((uintptr_t)start_addr)
  5102. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  5103. block =
  5104. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  5105. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  5106. if (!block[j].start_addr)
  5107. break;
  5108. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  5109. memmove(block + 1, block,
  5110. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  5111. * sizeof(BlockAddr));
  5112. j = 0;
  5113. }
  5114. block[j].start_addr = block_stack[t].start_addr;
  5115. block[j].else_addr = block_stack[t].else_addr;
  5116. block[j].end_addr = block_stack[t].end_addr;
  5117. }
  5118. else
  5119. break;
  5120. }
  5121. return true;
  5122. }
  5123. else {
  5124. block_nested_depth--;
  5125. if (block_nested_depth
  5126. < sizeof(block_stack) / sizeof(BlockAddr))
  5127. block_stack[block_nested_depth].end_addr =
  5128. (uint8 *)(p - 1);
  5129. }
  5130. break;
  5131. case WASM_OP_BR:
  5132. case WASM_OP_BR_IF:
  5133. skip_leb_uint32(p, p_end); /* labelidx */
  5134. break;
  5135. case WASM_OP_BR_TABLE:
  5136. read_leb_uint32(p, p_end, count); /* lable num */
  5137. #if WASM_ENABLE_FAST_INTERP != 0
  5138. for (i = 0; i <= count; i++) /* lableidxs */
  5139. skip_leb_uint32(p, p_end);
  5140. #else
  5141. p += count + 1;
  5142. while (*p == WASM_OP_NOP)
  5143. p++;
  5144. #endif
  5145. break;
  5146. #if WASM_ENABLE_FAST_INTERP == 0
  5147. case EXT_OP_BR_TABLE_CACHE:
  5148. read_leb_uint32(p, p_end, count); /* lable num */
  5149. while (*p == WASM_OP_NOP)
  5150. p++;
  5151. break;
  5152. #endif
  5153. case WASM_OP_RETURN:
  5154. break;
  5155. case WASM_OP_CALL:
  5156. #if WASM_ENABLE_TAIL_CALL != 0
  5157. case WASM_OP_RETURN_CALL:
  5158. #endif
  5159. skip_leb_uint32(p, p_end); /* funcidx */
  5160. break;
  5161. case WASM_OP_CALL_INDIRECT:
  5162. #if WASM_ENABLE_TAIL_CALL != 0
  5163. case WASM_OP_RETURN_CALL_INDIRECT:
  5164. #endif
  5165. skip_leb_uint32(p, p_end); /* typeidx */
  5166. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5167. u8 = read_uint8(p); /* 0x00 */
  5168. #else
  5169. skip_leb_uint32(p, p_end); /* talbeidx */
  5170. #endif
  5171. break;
  5172. #if WASM_ENABLE_GC != 0
  5173. case WASM_OP_CALL_REF:
  5174. case WASM_OP_RETURN_CALL_REF:
  5175. skip_leb_uint32(p, p_end); /* typeidx */
  5176. break;
  5177. #endif
  5178. case WASM_OP_DROP:
  5179. case WASM_OP_SELECT:
  5180. case WASM_OP_DROP_64:
  5181. case WASM_OP_SELECT_64:
  5182. break;
  5183. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  5184. case WASM_OP_SELECT_T:
  5185. {
  5186. skip_leb_uint32(p, p_end); /* vec length */
  5187. u8 = read_uint8(p); /* typeidx */
  5188. /* the possible extra bytes of GC ref type have been
  5189. modified to OP_NOP, no need to resolve them again */
  5190. break;
  5191. }
  5192. case WASM_OP_TABLE_GET:
  5193. case WASM_OP_TABLE_SET:
  5194. skip_leb_uint32(p, p_end); /* table index */
  5195. break;
  5196. case WASM_OP_REF_NULL:
  5197. {
  5198. u8 = read_uint8(p); /* type */
  5199. if (is_byte_a_type(u8)) {
  5200. #if WASM_ENABLE_GC != 0
  5201. if (wasm_is_type_multi_byte_type(u8)) {
  5202. /* the possible extra bytes of GC ref type have been
  5203. modified to OP_NOP, no need to resolve them again */
  5204. }
  5205. #endif
  5206. }
  5207. else {
  5208. p--;
  5209. skip_leb_uint32(p, p_end);
  5210. }
  5211. break;
  5212. }
  5213. case WASM_OP_REF_IS_NULL:
  5214. break;
  5215. case WASM_OP_REF_FUNC:
  5216. skip_leb_uint32(p, p_end); /* func index */
  5217. break;
  5218. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  5219. #if WASM_ENABLE_GC != 0
  5220. case WASM_OP_REF_AS_NON_NULL:
  5221. case WASM_OP_REF_EQ:
  5222. break;
  5223. case WASM_OP_BR_ON_NULL:
  5224. case WASM_OP_BR_ON_NON_NULL:
  5225. skip_leb_uint32(p, p_end); /* label index */
  5226. break;
  5227. #endif /* end of WASM_ENABLE_GC != 0 */
  5228. case WASM_OP_GET_LOCAL:
  5229. case WASM_OP_SET_LOCAL:
  5230. case WASM_OP_TEE_LOCAL:
  5231. case WASM_OP_GET_GLOBAL:
  5232. case WASM_OP_SET_GLOBAL:
  5233. case WASM_OP_GET_GLOBAL_64:
  5234. case WASM_OP_SET_GLOBAL_64:
  5235. case WASM_OP_SET_GLOBAL_AUX_STACK:
  5236. skip_leb_uint32(p, p_end); /* local index */
  5237. break;
  5238. case EXT_OP_GET_LOCAL_FAST:
  5239. case EXT_OP_SET_LOCAL_FAST:
  5240. case EXT_OP_TEE_LOCAL_FAST:
  5241. CHECK_BUF(p, p_end, 1);
  5242. p++;
  5243. break;
  5244. case WASM_OP_I32_LOAD:
  5245. case WASM_OP_I64_LOAD:
  5246. case WASM_OP_F32_LOAD:
  5247. case WASM_OP_F64_LOAD:
  5248. case WASM_OP_I32_LOAD8_S:
  5249. case WASM_OP_I32_LOAD8_U:
  5250. case WASM_OP_I32_LOAD16_S:
  5251. case WASM_OP_I32_LOAD16_U:
  5252. case WASM_OP_I64_LOAD8_S:
  5253. case WASM_OP_I64_LOAD8_U:
  5254. case WASM_OP_I64_LOAD16_S:
  5255. case WASM_OP_I64_LOAD16_U:
  5256. case WASM_OP_I64_LOAD32_S:
  5257. case WASM_OP_I64_LOAD32_U:
  5258. case WASM_OP_I32_STORE:
  5259. case WASM_OP_I64_STORE:
  5260. case WASM_OP_F32_STORE:
  5261. case WASM_OP_F64_STORE:
  5262. case WASM_OP_I32_STORE8:
  5263. case WASM_OP_I32_STORE16:
  5264. case WASM_OP_I64_STORE8:
  5265. case WASM_OP_I64_STORE16:
  5266. case WASM_OP_I64_STORE32:
  5267. skip_leb_uint32(p, p_end); /* align */
  5268. skip_leb_uint32(p, p_end); /* offset */
  5269. break;
  5270. case WASM_OP_MEMORY_SIZE:
  5271. case WASM_OP_MEMORY_GROW:
  5272. skip_leb_uint32(p, p_end); /* 0x00 */
  5273. break;
  5274. case WASM_OP_I32_CONST:
  5275. skip_leb_int32(p, p_end);
  5276. break;
  5277. case WASM_OP_I64_CONST:
  5278. skip_leb_int64(p, p_end);
  5279. break;
  5280. case WASM_OP_F32_CONST:
  5281. p += sizeof(float32);
  5282. break;
  5283. case WASM_OP_F64_CONST:
  5284. p += sizeof(float64);
  5285. break;
  5286. case WASM_OP_I32_EQZ:
  5287. case WASM_OP_I32_EQ:
  5288. case WASM_OP_I32_NE:
  5289. case WASM_OP_I32_LT_S:
  5290. case WASM_OP_I32_LT_U:
  5291. case WASM_OP_I32_GT_S:
  5292. case WASM_OP_I32_GT_U:
  5293. case WASM_OP_I32_LE_S:
  5294. case WASM_OP_I32_LE_U:
  5295. case WASM_OP_I32_GE_S:
  5296. case WASM_OP_I32_GE_U:
  5297. case WASM_OP_I64_EQZ:
  5298. case WASM_OP_I64_EQ:
  5299. case WASM_OP_I64_NE:
  5300. case WASM_OP_I64_LT_S:
  5301. case WASM_OP_I64_LT_U:
  5302. case WASM_OP_I64_GT_S:
  5303. case WASM_OP_I64_GT_U:
  5304. case WASM_OP_I64_LE_S:
  5305. case WASM_OP_I64_LE_U:
  5306. case WASM_OP_I64_GE_S:
  5307. case WASM_OP_I64_GE_U:
  5308. case WASM_OP_F32_EQ:
  5309. case WASM_OP_F32_NE:
  5310. case WASM_OP_F32_LT:
  5311. case WASM_OP_F32_GT:
  5312. case WASM_OP_F32_LE:
  5313. case WASM_OP_F32_GE:
  5314. case WASM_OP_F64_EQ:
  5315. case WASM_OP_F64_NE:
  5316. case WASM_OP_F64_LT:
  5317. case WASM_OP_F64_GT:
  5318. case WASM_OP_F64_LE:
  5319. case WASM_OP_F64_GE:
  5320. case WASM_OP_I32_CLZ:
  5321. case WASM_OP_I32_CTZ:
  5322. case WASM_OP_I32_POPCNT:
  5323. case WASM_OP_I32_ADD:
  5324. case WASM_OP_I32_SUB:
  5325. case WASM_OP_I32_MUL:
  5326. case WASM_OP_I32_DIV_S:
  5327. case WASM_OP_I32_DIV_U:
  5328. case WASM_OP_I32_REM_S:
  5329. case WASM_OP_I32_REM_U:
  5330. case WASM_OP_I32_AND:
  5331. case WASM_OP_I32_OR:
  5332. case WASM_OP_I32_XOR:
  5333. case WASM_OP_I32_SHL:
  5334. case WASM_OP_I32_SHR_S:
  5335. case WASM_OP_I32_SHR_U:
  5336. case WASM_OP_I32_ROTL:
  5337. case WASM_OP_I32_ROTR:
  5338. case WASM_OP_I64_CLZ:
  5339. case WASM_OP_I64_CTZ:
  5340. case WASM_OP_I64_POPCNT:
  5341. case WASM_OP_I64_ADD:
  5342. case WASM_OP_I64_SUB:
  5343. case WASM_OP_I64_MUL:
  5344. case WASM_OP_I64_DIV_S:
  5345. case WASM_OP_I64_DIV_U:
  5346. case WASM_OP_I64_REM_S:
  5347. case WASM_OP_I64_REM_U:
  5348. case WASM_OP_I64_AND:
  5349. case WASM_OP_I64_OR:
  5350. case WASM_OP_I64_XOR:
  5351. case WASM_OP_I64_SHL:
  5352. case WASM_OP_I64_SHR_S:
  5353. case WASM_OP_I64_SHR_U:
  5354. case WASM_OP_I64_ROTL:
  5355. case WASM_OP_I64_ROTR:
  5356. case WASM_OP_F32_ABS:
  5357. case WASM_OP_F32_NEG:
  5358. case WASM_OP_F32_CEIL:
  5359. case WASM_OP_F32_FLOOR:
  5360. case WASM_OP_F32_TRUNC:
  5361. case WASM_OP_F32_NEAREST:
  5362. case WASM_OP_F32_SQRT:
  5363. case WASM_OP_F32_ADD:
  5364. case WASM_OP_F32_SUB:
  5365. case WASM_OP_F32_MUL:
  5366. case WASM_OP_F32_DIV:
  5367. case WASM_OP_F32_MIN:
  5368. case WASM_OP_F32_MAX:
  5369. case WASM_OP_F32_COPYSIGN:
  5370. case WASM_OP_F64_ABS:
  5371. case WASM_OP_F64_NEG:
  5372. case WASM_OP_F64_CEIL:
  5373. case WASM_OP_F64_FLOOR:
  5374. case WASM_OP_F64_TRUNC:
  5375. case WASM_OP_F64_NEAREST:
  5376. case WASM_OP_F64_SQRT:
  5377. case WASM_OP_F64_ADD:
  5378. case WASM_OP_F64_SUB:
  5379. case WASM_OP_F64_MUL:
  5380. case WASM_OP_F64_DIV:
  5381. case WASM_OP_F64_MIN:
  5382. case WASM_OP_F64_MAX:
  5383. case WASM_OP_F64_COPYSIGN:
  5384. case WASM_OP_I32_WRAP_I64:
  5385. case WASM_OP_I32_TRUNC_S_F32:
  5386. case WASM_OP_I32_TRUNC_U_F32:
  5387. case WASM_OP_I32_TRUNC_S_F64:
  5388. case WASM_OP_I32_TRUNC_U_F64:
  5389. case WASM_OP_I64_EXTEND_S_I32:
  5390. case WASM_OP_I64_EXTEND_U_I32:
  5391. case WASM_OP_I64_TRUNC_S_F32:
  5392. case WASM_OP_I64_TRUNC_U_F32:
  5393. case WASM_OP_I64_TRUNC_S_F64:
  5394. case WASM_OP_I64_TRUNC_U_F64:
  5395. case WASM_OP_F32_CONVERT_S_I32:
  5396. case WASM_OP_F32_CONVERT_U_I32:
  5397. case WASM_OP_F32_CONVERT_S_I64:
  5398. case WASM_OP_F32_CONVERT_U_I64:
  5399. case WASM_OP_F32_DEMOTE_F64:
  5400. case WASM_OP_F64_CONVERT_S_I32:
  5401. case WASM_OP_F64_CONVERT_U_I32:
  5402. case WASM_OP_F64_CONVERT_S_I64:
  5403. case WASM_OP_F64_CONVERT_U_I64:
  5404. case WASM_OP_F64_PROMOTE_F32:
  5405. case WASM_OP_I32_REINTERPRET_F32:
  5406. case WASM_OP_I64_REINTERPRET_F64:
  5407. case WASM_OP_F32_REINTERPRET_I32:
  5408. case WASM_OP_F64_REINTERPRET_I64:
  5409. case WASM_OP_I32_EXTEND8_S:
  5410. case WASM_OP_I32_EXTEND16_S:
  5411. case WASM_OP_I64_EXTEND8_S:
  5412. case WASM_OP_I64_EXTEND16_S:
  5413. case WASM_OP_I64_EXTEND32_S:
  5414. break;
  5415. #if WASM_ENABLE_GC != 0
  5416. case WASM_OP_GC_PREFIX:
  5417. {
  5418. uint32 opcode1;
  5419. read_leb_uint32(p, p_end, opcode1);
  5420. switch (opcode1) {
  5421. case WASM_OP_STRUCT_NEW_CANON:
  5422. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  5423. skip_leb_uint32(p, p_end); /* typeidx */
  5424. break;
  5425. case WASM_OP_STRUCT_GET:
  5426. case WASM_OP_STRUCT_GET_S:
  5427. case WASM_OP_STRUCT_GET_U:
  5428. case WASM_OP_STRUCT_SET:
  5429. skip_leb_uint32(p, p_end); /* typeidx */
  5430. skip_leb_uint32(p, p_end); /* fieldidx */
  5431. break;
  5432. case WASM_OP_ARRAY_NEW_CANON:
  5433. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  5434. case WASM_OP_ARRAY_GET:
  5435. case WASM_OP_ARRAY_GET_S:
  5436. case WASM_OP_ARRAY_GET_U:
  5437. case WASM_OP_ARRAY_SET:
  5438. skip_leb_uint32(p, p_end); /* typeidx */
  5439. break;
  5440. #if WASM_ENABLE_GC_BINARYEN != 0
  5441. case WASM_OP_ARRAY_COPY:
  5442. skip_leb_uint32(p, p_end); /* typeidx1 */
  5443. skip_leb_uint32(p, p_end); /* typeidx2 */
  5444. break;
  5445. #endif
  5446. case WASM_OP_ARRAY_LEN:
  5447. break;
  5448. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  5449. case WASM_OP_ARRAY_NEW_CANON_DATA:
  5450. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  5451. skip_leb_uint32(p, p_end); /* typeidx */
  5452. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  5453. break;
  5454. case WASM_OP_I31_NEW:
  5455. case WASM_OP_I31_GET_S:
  5456. case WASM_OP_I31_GET_U:
  5457. break;
  5458. case WASM_OP_REF_TEST:
  5459. case WASM_OP_REF_CAST:
  5460. case WASM_OP_REF_TEST_NULLABLE:
  5461. case WASM_OP_REF_CAST_NULLABLE:
  5462. skip_leb_int32(p, p_end); /* heaptype */
  5463. break;
  5464. case WASM_OP_BR_ON_CAST:
  5465. case WASM_OP_BR_ON_CAST_FAIL:
  5466. case WASM_OP_BR_ON_CAST_NULLABLE:
  5467. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  5468. skip_leb_uint32(p, p_end); /* labelidx */
  5469. skip_leb_int32(p, p_end); /* heaptype */
  5470. break;
  5471. case WASM_OP_EXTERN_INTERNALIZE:
  5472. case WASM_OP_EXTERN_EXTERNALIZE:
  5473. break;
  5474. default:
  5475. return false;
  5476. }
  5477. break;
  5478. }
  5479. #endif /* end of WASM_ENABLE_GC != 0 */
  5480. case WASM_OP_MISC_PREFIX:
  5481. {
  5482. uint32 opcode1;
  5483. read_leb_uint32(p, p_end, opcode1);
  5484. switch (opcode1) {
  5485. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5486. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5487. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5488. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5489. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5490. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5491. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5492. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5493. break;
  5494. #if WASM_ENABLE_BULK_MEMORY != 0
  5495. case WASM_OP_MEMORY_INIT:
  5496. skip_leb_uint32(p, p_end);
  5497. /* skip memory idx */
  5498. p++;
  5499. break;
  5500. case WASM_OP_DATA_DROP:
  5501. skip_leb_uint32(p, p_end);
  5502. break;
  5503. case WASM_OP_MEMORY_COPY:
  5504. /* skip two memory idx */
  5505. p += 2;
  5506. break;
  5507. case WASM_OP_MEMORY_FILL:
  5508. /* skip memory idx */
  5509. p++;
  5510. break;
  5511. #endif /* WASM_ENABLE_BULK_MEMORY */
  5512. #if WASM_ENABLE_REF_TYPES != 0
  5513. case WASM_OP_TABLE_INIT:
  5514. case WASM_OP_TABLE_COPY:
  5515. /* tableidx */
  5516. skip_leb_uint32(p, p_end);
  5517. /* elemidx */
  5518. skip_leb_uint32(p, p_end);
  5519. break;
  5520. case WASM_OP_ELEM_DROP:
  5521. /* elemidx */
  5522. skip_leb_uint32(p, p_end);
  5523. break;
  5524. case WASM_OP_TABLE_SIZE:
  5525. case WASM_OP_TABLE_GROW:
  5526. case WASM_OP_TABLE_FILL:
  5527. skip_leb_uint32(p, p_end); /* table idx */
  5528. break;
  5529. #endif /* WASM_ENABLE_REF_TYPES */
  5530. default:
  5531. return false;
  5532. }
  5533. break;
  5534. }
  5535. #if WASM_ENABLE_SIMD != 0
  5536. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5537. case WASM_OP_SIMD_PREFIX:
  5538. {
  5539. /* TODO: shall we ceate a table to be friendly to branch
  5540. * prediction */
  5541. opcode = read_uint8(p);
  5542. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  5543. */
  5544. switch (opcode) {
  5545. case SIMD_v128_load:
  5546. case SIMD_v128_load8x8_s:
  5547. case SIMD_v128_load8x8_u:
  5548. case SIMD_v128_load16x4_s:
  5549. case SIMD_v128_load16x4_u:
  5550. case SIMD_v128_load32x2_s:
  5551. case SIMD_v128_load32x2_u:
  5552. case SIMD_v128_load8_splat:
  5553. case SIMD_v128_load16_splat:
  5554. case SIMD_v128_load32_splat:
  5555. case SIMD_v128_load64_splat:
  5556. case SIMD_v128_store:
  5557. /* memarg align */
  5558. skip_leb_uint32(p, p_end);
  5559. /* memarg offset*/
  5560. skip_leb_uint32(p, p_end);
  5561. break;
  5562. case SIMD_v128_const:
  5563. case SIMD_v8x16_shuffle:
  5564. /* immByte[16] immLaneId[16] */
  5565. CHECK_BUF1(p, p_end, 16);
  5566. p += 16;
  5567. break;
  5568. case SIMD_i8x16_extract_lane_s:
  5569. case SIMD_i8x16_extract_lane_u:
  5570. case SIMD_i8x16_replace_lane:
  5571. case SIMD_i16x8_extract_lane_s:
  5572. case SIMD_i16x8_extract_lane_u:
  5573. case SIMD_i16x8_replace_lane:
  5574. case SIMD_i32x4_extract_lane:
  5575. case SIMD_i32x4_replace_lane:
  5576. case SIMD_i64x2_extract_lane:
  5577. case SIMD_i64x2_replace_lane:
  5578. case SIMD_f32x4_extract_lane:
  5579. case SIMD_f32x4_replace_lane:
  5580. case SIMD_f64x2_extract_lane:
  5581. case SIMD_f64x2_replace_lane:
  5582. /* ImmLaneId */
  5583. CHECK_BUF(p, p_end, 1);
  5584. p++;
  5585. break;
  5586. case SIMD_v128_load8_lane:
  5587. case SIMD_v128_load16_lane:
  5588. case SIMD_v128_load32_lane:
  5589. case SIMD_v128_load64_lane:
  5590. case SIMD_v128_store8_lane:
  5591. case SIMD_v128_store16_lane:
  5592. case SIMD_v128_store32_lane:
  5593. case SIMD_v128_store64_lane:
  5594. /* memarg align */
  5595. skip_leb_uint32(p, p_end);
  5596. /* memarg offset*/
  5597. skip_leb_uint32(p, p_end);
  5598. /* ImmLaneId */
  5599. CHECK_BUF(p, p_end, 1);
  5600. p++;
  5601. break;
  5602. case SIMD_v128_load32_zero:
  5603. case SIMD_v128_load64_zero:
  5604. /* memarg align */
  5605. skip_leb_uint32(p, p_end);
  5606. /* memarg offset*/
  5607. skip_leb_uint32(p, p_end);
  5608. break;
  5609. default:
  5610. /*
  5611. * since latest SIMD specific used almost every value
  5612. * from 0x00 to 0xff, the default branch will present
  5613. * all opcodes without imm
  5614. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  5615. */
  5616. break;
  5617. }
  5618. break;
  5619. }
  5620. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  5621. #endif /* end of WASM_ENABLE_SIMD */
  5622. #if WASM_ENABLE_SHARED_MEMORY != 0
  5623. case WASM_OP_ATOMIC_PREFIX:
  5624. {
  5625. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  5626. opcode = read_uint8(p);
  5627. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5628. skip_leb_uint32(p, p_end); /* align */
  5629. skip_leb_uint32(p, p_end); /* offset */
  5630. }
  5631. else {
  5632. /* atomic.fence doesn't have memarg */
  5633. p++;
  5634. }
  5635. break;
  5636. }
  5637. #endif
  5638. #if WASM_ENABLE_DEBUG_INTERP != 0
  5639. case DEBUG_OP_BREAK:
  5640. {
  5641. WASMDebugInstance *debug_instance =
  5642. wasm_exec_env_get_instance(exec_env);
  5643. char orignal_opcode[1];
  5644. uint64 size = 1;
  5645. WASMModuleInstance *module_inst =
  5646. (WASMModuleInstance *)exec_env->module_inst;
  5647. uint64 offset = (p - 1) >= module_inst->module->load_addr
  5648. ? (p - 1) - module_inst->module->load_addr
  5649. : ~0;
  5650. if (debug_instance) {
  5651. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  5652. orignal_opcode, &size)
  5653. && size == 1) {
  5654. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  5655. "with %02x: ",
  5656. orignal_opcode[0]);
  5657. opcode = orignal_opcode[0];
  5658. goto op_break_retry;
  5659. }
  5660. }
  5661. break;
  5662. }
  5663. #endif
  5664. default:
  5665. return false;
  5666. }
  5667. }
  5668. (void)u8;
  5669. (void)exec_env;
  5670. return false;
  5671. fail:
  5672. return false;
  5673. }
  5674. #if WASM_ENABLE_FAST_INTERP != 0
  5675. #if WASM_DEBUG_PREPROCESSOR != 0
  5676. #define LOG_OP(...) os_printf(__VA_ARGS__)
  5677. #else
  5678. #define LOG_OP(...) (void)0
  5679. #endif
  5680. #define PATCH_ELSE 0
  5681. #define PATCH_END 1
  5682. typedef struct BranchBlockPatch {
  5683. struct BranchBlockPatch *next;
  5684. uint8 patch_type;
  5685. uint8 *code_compiled;
  5686. } BranchBlockPatch;
  5687. #endif
  5688. typedef struct BranchBlock {
  5689. uint8 label_type;
  5690. BlockType block_type;
  5691. uint8 *start_addr;
  5692. uint8 *else_addr;
  5693. uint8 *end_addr;
  5694. uint32 stack_cell_num;
  5695. #if WASM_ENABLE_GC != 0
  5696. uint32 reftype_map_num;
  5697. #endif
  5698. #if WASM_ENABLE_FAST_INTERP != 0
  5699. uint16 dynamic_offset;
  5700. uint8 *code_compiled;
  5701. BranchBlockPatch *patch_list;
  5702. /* This is used to save params frame_offset of of if block */
  5703. int16 *param_frame_offsets;
  5704. #endif
  5705. /* Indicate the operand stack is in polymorphic state.
  5706. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  5707. * to polymorphic state until the block's 'end' opcode is processed.
  5708. * If stack is in polymorphic state and stack is empty, instruction can
  5709. * pop any type of value directly without decreasing stack top pointer
  5710. * and stack cell num. */
  5711. bool is_stack_polymorphic;
  5712. } BranchBlock;
  5713. typedef struct WASMLoaderContext {
  5714. /* frame ref stack */
  5715. uint8 *frame_ref;
  5716. uint8 *frame_ref_bottom;
  5717. uint8 *frame_ref_boundary;
  5718. uint32 frame_ref_size;
  5719. uint32 stack_cell_num;
  5720. uint32 max_stack_cell_num;
  5721. #if WASM_ENABLE_GC != 0
  5722. /* frame reftype map stack */
  5723. WASMRefTypeMap *frame_reftype_map;
  5724. WASMRefTypeMap *frame_reftype_map_bottom;
  5725. WASMRefTypeMap *frame_reftype_map_boundary;
  5726. uint32 frame_reftype_map_size;
  5727. uint32 reftype_map_num;
  5728. uint32 max_reftype_map_num;
  5729. /* Current module */
  5730. WASMModule *module;
  5731. /* Current module's ref_type_set */
  5732. HashMap *ref_type_set;
  5733. /* Always point to local variable ref_type of
  5734. wasm_loader_prepare_bytecode */
  5735. WASMRefType *ref_type_tmp;
  5736. #endif
  5737. /* frame csp stack */
  5738. BranchBlock *frame_csp;
  5739. BranchBlock *frame_csp_bottom;
  5740. BranchBlock *frame_csp_boundary;
  5741. uint32 frame_csp_size;
  5742. uint32 csp_num;
  5743. uint32 max_csp_num;
  5744. #if WASM_ENABLE_FAST_INTERP != 0
  5745. /* frame offset stack */
  5746. int16 *frame_offset;
  5747. int16 *frame_offset_bottom;
  5748. int16 *frame_offset_boundary;
  5749. uint32 frame_offset_size;
  5750. int16 dynamic_offset;
  5751. int16 start_dynamic_offset;
  5752. int16 max_dynamic_offset;
  5753. /* preserved local offset */
  5754. int16 preserved_local_offset;
  5755. /* const buffer */
  5756. uint8 *const_buf;
  5757. uint16 num_const;
  5758. uint16 const_cell_num;
  5759. uint32 const_buf_size;
  5760. /* processed code */
  5761. uint8 *p_code_compiled;
  5762. uint8 *p_code_compiled_end;
  5763. uint32 code_compiled_size;
  5764. /* If the last opcode will be dropped, the peak memory usage will be larger
  5765. * than the final code_compiled_size, we record the peak size to ensure
  5766. * there will not be invalid memory access during second traverse */
  5767. uint32 code_compiled_peak_size;
  5768. #endif
  5769. } WASMLoaderContext;
  5770. typedef struct Const {
  5771. WASMValue value;
  5772. uint16 slot_index;
  5773. uint8 value_type;
  5774. } Const;
  5775. static void *
  5776. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  5777. uint32 error_buf_size)
  5778. {
  5779. uint8 *mem_new;
  5780. bh_assert(size_new > size_old);
  5781. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  5782. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  5783. memset(mem_new + size_old, 0, size_new - size_old);
  5784. wasm_runtime_free(mem_old);
  5785. }
  5786. return mem_new;
  5787. }
  5788. #define MEM_REALLOC(mem, size_old, size_new) \
  5789. do { \
  5790. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  5791. error_buf_size); \
  5792. if (!mem_new) \
  5793. goto fail; \
  5794. mem = mem_new; \
  5795. } while (0)
  5796. #define CHECK_CSP_PUSH() \
  5797. do { \
  5798. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  5799. MEM_REALLOC( \
  5800. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  5801. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  5802. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  5803. ctx->frame_csp_boundary = \
  5804. ctx->frame_csp_bottom \
  5805. + ctx->frame_csp_size / sizeof(BranchBlock); \
  5806. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  5807. } \
  5808. } while (0)
  5809. #define CHECK_CSP_POP() \
  5810. do { \
  5811. if (ctx->csp_num < 1) { \
  5812. set_error_buf(error_buf, error_buf_size, \
  5813. "type mismatch: " \
  5814. "expect data but block stack was empty"); \
  5815. goto fail; \
  5816. } \
  5817. } while (0)
  5818. #if WASM_ENABLE_FAST_INTERP != 0
  5819. static bool
  5820. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  5821. uint32 error_buf_size)
  5822. {
  5823. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  5824. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  5825. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  5826. ctx->frame_offset_size + 16);
  5827. ctx->frame_offset_size += 16;
  5828. ctx->frame_offset_boundary =
  5829. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  5830. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  5831. }
  5832. return true;
  5833. fail:
  5834. return false;
  5835. }
  5836. static bool
  5837. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  5838. {
  5839. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  5840. return false;
  5841. return true;
  5842. }
  5843. static void
  5844. free_label_patch_list(BranchBlock *frame_csp)
  5845. {
  5846. BranchBlockPatch *label_patch = frame_csp->patch_list;
  5847. BranchBlockPatch *next;
  5848. while (label_patch != NULL) {
  5849. next = label_patch->next;
  5850. wasm_runtime_free(label_patch);
  5851. label_patch = next;
  5852. }
  5853. frame_csp->patch_list = NULL;
  5854. }
  5855. static void
  5856. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  5857. {
  5858. BranchBlock *tmp_csp = frame_csp;
  5859. for (uint32 i = 0; i < csp_num; i++) {
  5860. free_label_patch_list(tmp_csp);
  5861. tmp_csp++;
  5862. }
  5863. }
  5864. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5865. static void
  5866. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  5867. {
  5868. if (ctx) {
  5869. if (ctx->frame_ref_bottom)
  5870. wasm_runtime_free(ctx->frame_ref_bottom);
  5871. #if WASM_ENABLE_GC != 0
  5872. if (ctx->frame_reftype_map_bottom)
  5873. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  5874. #endif
  5875. if (ctx->frame_csp_bottom) {
  5876. #if WASM_ENABLE_FAST_INTERP != 0
  5877. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  5878. #endif
  5879. wasm_runtime_free(ctx->frame_csp_bottom);
  5880. }
  5881. #if WASM_ENABLE_FAST_INTERP != 0
  5882. if (ctx->frame_offset_bottom)
  5883. wasm_runtime_free(ctx->frame_offset_bottom);
  5884. if (ctx->const_buf)
  5885. wasm_runtime_free(ctx->const_buf);
  5886. #endif
  5887. wasm_runtime_free(ctx);
  5888. }
  5889. }
  5890. static WASMLoaderContext *
  5891. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  5892. {
  5893. WASMLoaderContext *loader_ctx =
  5894. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  5895. if (!loader_ctx)
  5896. return NULL;
  5897. loader_ctx->frame_ref_size = 32;
  5898. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  5899. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  5900. goto fail;
  5901. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  5902. #if WASM_ENABLE_GC != 0
  5903. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  5904. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  5905. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  5906. error_buf_size)))
  5907. goto fail;
  5908. loader_ctx->frame_reftype_map_boundary =
  5909. loader_ctx->frame_reftype_map_bottom + 16;
  5910. #endif
  5911. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  5912. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  5913. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  5914. goto fail;
  5915. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  5916. #if WASM_ENABLE_FAST_INTERP != 0
  5917. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  5918. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  5919. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  5920. error_buf_size)))
  5921. goto fail;
  5922. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  5923. loader_ctx->num_const = 0;
  5924. loader_ctx->const_buf_size = sizeof(Const) * 8;
  5925. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  5926. error_buf, error_buf_size)))
  5927. goto fail;
  5928. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  5929. set_error_buf(error_buf, error_buf_size,
  5930. "fast interpreter offset overflow");
  5931. goto fail;
  5932. }
  5933. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  5934. loader_ctx->max_dynamic_offset =
  5935. func->param_cell_num + func->local_cell_num;
  5936. #endif
  5937. return loader_ctx;
  5938. fail:
  5939. wasm_loader_ctx_destroy(loader_ctx);
  5940. return NULL;
  5941. }
  5942. static bool
  5943. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  5944. uint32 error_buf_size)
  5945. {
  5946. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  5947. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  5948. /* Increase the frame ref stack */
  5949. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  5950. ctx->frame_ref_size + 16);
  5951. ctx->frame_ref_size += 16;
  5952. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  5953. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  5954. }
  5955. #if WASM_ENABLE_GC != 0
  5956. if (wasm_is_type_multi_byte_type(type)
  5957. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  5958. /* Increase the frame reftype map stack */
  5959. bh_assert(
  5960. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  5961. * sizeof(WASMRefTypeMap))
  5962. == ctx->frame_reftype_map_size);
  5963. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  5964. ctx->frame_reftype_map_size
  5965. + (uint32)sizeof(WASMRefTypeMap) * 8);
  5966. ctx->frame_reftype_map =
  5967. ctx->frame_reftype_map_bottom
  5968. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  5969. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  5970. ctx->frame_reftype_map_boundary =
  5971. ctx->frame_reftype_map_bottom
  5972. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  5973. }
  5974. #endif
  5975. return true;
  5976. fail:
  5977. return false;
  5978. }
  5979. static bool
  5980. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  5981. uint32 error_buf_size)
  5982. {
  5983. uint32 type_cell_num = wasm_value_type_cell_num(type);
  5984. uint32 i;
  5985. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  5986. return false;
  5987. #if WASM_ENABLE_GC != 0
  5988. if (wasm_is_type_multi_byte_type(type)) {
  5989. WASMRefType *ref_type;
  5990. if (!(ref_type =
  5991. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  5992. error_buf, error_buf_size))) {
  5993. return false;
  5994. }
  5995. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  5996. /* Increase the frame reftype map stack */
  5997. bh_assert((uint32)((ctx->frame_reftype_map
  5998. - ctx->frame_reftype_map_bottom)
  5999. * sizeof(WASMRefTypeMap))
  6000. == ctx->frame_reftype_map_size);
  6001. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  6002. ctx->frame_reftype_map_size,
  6003. ctx->frame_reftype_map_size
  6004. + (uint32)sizeof(WASMRefTypeMap) * 8);
  6005. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  6006. + ctx->frame_reftype_map_size
  6007. / ((uint32)sizeof(WASMRefTypeMap));
  6008. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  6009. ctx->frame_reftype_map_boundary =
  6010. ctx->frame_reftype_map_bottom
  6011. + ctx->frame_reftype_map_size
  6012. / ((uint32)sizeof(WASMRefTypeMap));
  6013. }
  6014. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  6015. ctx->frame_reftype_map->ref_type = ref_type;
  6016. ctx->frame_reftype_map++;
  6017. ctx->reftype_map_num++;
  6018. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  6019. ctx->max_reftype_map_num = ctx->reftype_map_num;
  6020. }
  6021. #endif
  6022. for (i = 0; i < type_cell_num; i++)
  6023. *ctx->frame_ref++ = type;
  6024. ctx->stack_cell_num += type_cell_num;
  6025. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  6026. ctx->max_stack_cell_num = ctx->stack_cell_num;
  6027. if (ctx->max_stack_cell_num > UINT16_MAX) {
  6028. set_error_buf(error_buf, error_buf_size,
  6029. "operand stack depth limit exceeded");
  6030. return false;
  6031. }
  6032. }
  6033. return true;
  6034. #if WASM_ENABLE_GC != 0
  6035. fail:
  6036. return false;
  6037. #endif
  6038. }
  6039. static bool
  6040. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  6041. int32 stack_cell_num,
  6042. #if WASM_ENABLE_GC != 0
  6043. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  6044. #endif
  6045. uint8 type,
  6046. #if WASM_ENABLE_GC != 0
  6047. WASMRefType *ref_type,
  6048. #endif
  6049. char *error_buf, uint32 error_buf_size)
  6050. {
  6051. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  6052. #if WASM_ENABLE_GC != 0
  6053. WASMRefType *frame_reftype = NULL;
  6054. #endif
  6055. if (stack_cell_num < type_cell_num) {
  6056. set_error_buf(error_buf, error_buf_size,
  6057. "type mismatch: expect data but stack was empty");
  6058. return false;
  6059. }
  6060. #if WASM_ENABLE_GC == 0
  6061. for (i = 0; i < type_cell_num; i++) {
  6062. if (*(frame_ref - 1 - i) != type) {
  6063. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6064. "type mismatch: expect ", type2str(type),
  6065. " but got other");
  6066. return false;
  6067. }
  6068. }
  6069. #else
  6070. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  6071. bh_assert(reftype_map_num > 0);
  6072. frame_reftype = (frame_reftype_map - 1)->ref_type;
  6073. }
  6074. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  6075. ref_type, ctx->module->types,
  6076. ctx->module->type_count)) {
  6077. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6078. "type mismatch: expect ", type2str(type),
  6079. " but got other");
  6080. return false;
  6081. }
  6082. for (i = 0; i < type_cell_num - 1; i++) {
  6083. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  6084. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6085. "type mismatch: expect ", type2str(type),
  6086. " but got other");
  6087. return false;
  6088. }
  6089. }
  6090. #endif
  6091. return true;
  6092. }
  6093. static bool
  6094. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  6095. uint32 error_buf_size)
  6096. {
  6097. int32 block_stack_cell_num =
  6098. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  6099. #if WASM_ENABLE_GC != 0
  6100. int32 reftype_map_num =
  6101. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  6102. #endif
  6103. if (block_stack_cell_num > 0) {
  6104. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  6105. /* the stack top is a value of any type, return success */
  6106. return true;
  6107. }
  6108. #if WASM_ENABLE_GC != 0
  6109. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  6110. uint8 stack_top_type = *(ctx->frame_ref - 1);
  6111. WASMRefType *stack_top_ref_type = NULL;
  6112. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  6113. bh_assert(reftype_map_num > 0);
  6114. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  6115. }
  6116. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  6117. ctx->ref_type_tmp, ctx->module->types,
  6118. ctx->module->type_count)) {
  6119. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  6120. uint32 ref_type_struct_size =
  6121. wasm_reftype_struct_size(stack_top_ref_type);
  6122. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  6123. stack_top_ref_type, ref_type_struct_size);
  6124. }
  6125. return true;
  6126. }
  6127. }
  6128. #endif
  6129. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  6130. #if WASM_ENABLE_GC != 0
  6131. ctx->frame_reftype_map, reftype_map_num,
  6132. #endif
  6133. type,
  6134. #if WASM_ENABLE_GC != 0
  6135. ctx->ref_type_tmp,
  6136. #endif
  6137. error_buf, error_buf_size)) {
  6138. return false;
  6139. }
  6140. return true;
  6141. }
  6142. static bool
  6143. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  6144. uint32 error_buf_size)
  6145. {
  6146. BranchBlock *cur_block = ctx->frame_csp - 1;
  6147. int32 available_stack_cell =
  6148. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6149. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  6150. /* Directly return success if current block is in stack
  6151. polymorphic state while stack is empty. */
  6152. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  6153. return true;
  6154. if (type == VALUE_TYPE_VOID)
  6155. return true;
  6156. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  6157. return false;
  6158. bh_assert(available_stack_cell > 0);
  6159. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  6160. type = VALUE_TYPE_ANY;
  6161. cell_num_to_pop = 1;
  6162. }
  6163. ctx->frame_ref -= cell_num_to_pop;
  6164. ctx->stack_cell_num -= cell_num_to_pop;
  6165. #if WASM_ENABLE_GC != 0
  6166. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  6167. ctx->frame_reftype_map--;
  6168. ctx->reftype_map_num--;
  6169. }
  6170. #endif
  6171. return true;
  6172. }
  6173. #if WASM_ENABLE_GC != 0
  6174. /* Get the stack top element of current block */
  6175. static bool
  6176. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  6177. WASMRefType **p_ref_type, char *error_buf,
  6178. uint32 error_buf_size)
  6179. {
  6180. BranchBlock *cur_block = ctx->frame_csp - 1;
  6181. int32 available_stack_cell =
  6182. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6183. if (available_stack_cell <= 0) {
  6184. /* Directly return success if current block is in stack
  6185. polymorphic state while stack is empty. */
  6186. if (cur_block->is_stack_polymorphic) {
  6187. *p_type = VALUE_TYPE_ANY;
  6188. return true;
  6189. }
  6190. else {
  6191. set_error_buf(
  6192. error_buf, error_buf_size,
  6193. "type mismatch: expect data but block stack was empty");
  6194. return false;
  6195. }
  6196. }
  6197. *p_type = *(ctx->frame_ref - 1);
  6198. if (wasm_is_type_multi_byte_type(*p_type)) {
  6199. int32 available_reftype_map =
  6200. (int32)(ctx->reftype_map_num
  6201. - (ctx->frame_csp - 1)->reftype_map_num);
  6202. bh_assert(available_reftype_map > 0);
  6203. (void)available_reftype_map;
  6204. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  6205. }
  6206. return true;
  6207. }
  6208. #if WASM_ENABLE_FAST_INTERP != 0
  6209. static bool
  6210. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  6211. char *error_buf, uint32 error_buf_size);
  6212. #endif
  6213. /* Check whether the stack top elem is a heap object, and if yes,
  6214. pop and return it */
  6215. static bool
  6216. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  6217. WASMRefType *ref_ht_ret, char *error_buf,
  6218. uint32 error_buf_size)
  6219. {
  6220. uint8 type = 0;
  6221. WASMRefType *ref_type = NULL;
  6222. /* Get stack top element */
  6223. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  6224. error_buf_size)) {
  6225. return false;
  6226. }
  6227. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  6228. /* stack top isn't a ref type */
  6229. && !wasm_is_type_reftype(type)) {
  6230. set_error_buf(error_buf, error_buf_size,
  6231. "type mismatch: expect heap object but got others");
  6232. return false;
  6233. }
  6234. /* POP stack top */
  6235. if (wasm_is_type_multi_byte_type(type)) {
  6236. bh_assert(ref_type);
  6237. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  6238. wasm_reftype_struct_size(ref_type));
  6239. }
  6240. #if WASM_ENABLE_FAST_INTERP != 0
  6241. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  6242. error_buf_size)) {
  6243. return false;
  6244. }
  6245. #else
  6246. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  6247. return false;
  6248. }
  6249. #endif
  6250. if (p_type)
  6251. *p_type = type;
  6252. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  6253. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  6254. wasm_reftype_struct_size(ref_type));
  6255. }
  6256. return true;
  6257. }
  6258. /* Check whether the stack top elem is subtype of (ref null ht),
  6259. and if yes, pop it and return the converted (ref ht) */
  6260. static bool
  6261. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  6262. WASMRefType *ref_ht_ret, char *error_buf,
  6263. uint32 error_buf_size)
  6264. {
  6265. uint8 type = 0;
  6266. WASMRefType ref_type = { 0 };
  6267. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  6268. error_buf_size)) {
  6269. return false;
  6270. }
  6271. /* Convert to related (ref ht) and return */
  6272. if ((type >= REF_TYPE_EQREF && type <= REF_TYPE_FUNCREF)
  6273. || (type >= REF_TYPE_NULLREF && type <= REF_TYPE_I31REF)) {
  6274. /* Return (ref func/extern/any/eq/i31/nofunc/noextern/struct/array/none)
  6275. */
  6276. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  6277. HEAP_TYPE_FUNC + (type - REF_TYPE_FUNCREF));
  6278. type = ref_ht_ret->ref_type;
  6279. }
  6280. else if (wasm_is_reftype_htref_nullable(type)
  6281. || wasm_is_reftype_htref_non_nullable(type)) {
  6282. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  6283. wasm_reftype_struct_size(&ref_type));
  6284. /* Convert to (ref ht) */
  6285. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  6286. ref_ht_ret->ref_ht_common.nullable = false;
  6287. type = ref_ht_ret->ref_type;
  6288. }
  6289. *p_type = type;
  6290. return true;
  6291. }
  6292. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  6293. and if yes, pop it and return the type_idx */
  6294. static bool
  6295. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  6296. uint32 *p_type_idx, char *error_buf,
  6297. uint32 error_buf_size)
  6298. {
  6299. uint8 type = 0;
  6300. int32 type_idx = -1;
  6301. WASMRefType *ref_type = NULL;
  6302. /* Get stack top element */
  6303. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  6304. error_buf_size)) {
  6305. return false;
  6306. }
  6307. if (type != VALUE_TYPE_ANY) {
  6308. /* stack top isn't (ref null $t) */
  6309. if (!((wasm_is_reftype_htref_nullable(type)
  6310. || wasm_is_reftype_htref_non_nullable(type))
  6311. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  6312. set_error_buf(error_buf, error_buf_size,
  6313. "type mismatch: expect (ref null $t) but got others");
  6314. return false;
  6315. }
  6316. type_idx = ref_type->ref_ht_typeidx.type_idx;
  6317. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  6318. wasm_reftype_struct_size(ref_type));
  6319. }
  6320. /* POP stack top */
  6321. #if WASM_ENABLE_FAST_INTERP != 0
  6322. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  6323. error_buf_size)) {
  6324. return false;
  6325. }
  6326. #else
  6327. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  6328. return false;
  6329. }
  6330. #endif
  6331. /* Convert to type_idx and return */
  6332. *p_type = type;
  6333. if (type != VALUE_TYPE_ANY)
  6334. *p_type_idx = (uint32)type_idx;
  6335. return true;
  6336. }
  6337. #endif /* WASM_ENABLE_GC != 0 */
  6338. static bool
  6339. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  6340. uint8 type_push, uint8 type_pop, char *error_buf,
  6341. uint32 error_buf_size)
  6342. {
  6343. for (int i = 0; i < pop_cnt; i++) {
  6344. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  6345. error_buf_size))
  6346. return false;
  6347. }
  6348. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  6349. return false;
  6350. return true;
  6351. }
  6352. static bool
  6353. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  6354. BlockType block_type, uint8 *start_addr,
  6355. char *error_buf, uint32 error_buf_size)
  6356. {
  6357. CHECK_CSP_PUSH();
  6358. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  6359. ctx->frame_csp->label_type = label_type;
  6360. ctx->frame_csp->block_type = block_type;
  6361. ctx->frame_csp->start_addr = start_addr;
  6362. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  6363. #if WASM_ENABLE_GC != 0
  6364. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  6365. #endif
  6366. #if WASM_ENABLE_FAST_INTERP != 0
  6367. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  6368. ctx->frame_csp->patch_list = NULL;
  6369. #endif
  6370. ctx->frame_csp++;
  6371. ctx->csp_num++;
  6372. if (ctx->csp_num > ctx->max_csp_num) {
  6373. ctx->max_csp_num = ctx->csp_num;
  6374. if (ctx->max_csp_num > UINT16_MAX) {
  6375. set_error_buf(error_buf, error_buf_size,
  6376. "label stack depth limit exceeded");
  6377. return false;
  6378. }
  6379. }
  6380. return true;
  6381. fail:
  6382. return false;
  6383. }
  6384. static bool
  6385. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  6386. uint32 error_buf_size)
  6387. {
  6388. CHECK_CSP_POP();
  6389. #if WASM_ENABLE_FAST_INTERP != 0
  6390. if ((ctx->frame_csp - 1)->param_frame_offsets)
  6391. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  6392. #endif
  6393. ctx->frame_csp--;
  6394. ctx->csp_num--;
  6395. return true;
  6396. fail:
  6397. return false;
  6398. }
  6399. #if WASM_ENABLE_FAST_INTERP != 0
  6400. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6401. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6402. #define emit_label(opcode) \
  6403. do { \
  6404. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  6405. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6406. } while (0)
  6407. #define skip_label() \
  6408. do { \
  6409. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  6410. LOG_OP("\ndelete last op\n"); \
  6411. } while (0)
  6412. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6413. #define emit_label(opcode) \
  6414. do { \
  6415. int32 offset = \
  6416. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  6417. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  6418. set_error_buf(error_buf, error_buf_size, \
  6419. "pre-compiled label offset out of range"); \
  6420. goto fail; \
  6421. } \
  6422. wasm_loader_emit_int16(loader_ctx, offset); \
  6423. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6424. } while (0)
  6425. #define skip_label() \
  6426. do { \
  6427. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  6428. LOG_OP("\ndelete last op\n"); \
  6429. } while (0)
  6430. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6431. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6432. #define emit_label(opcode) \
  6433. do { \
  6434. wasm_loader_emit_uint8(loader_ctx, opcode); \
  6435. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6436. } while (0)
  6437. #define skip_label() \
  6438. do { \
  6439. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  6440. LOG_OP("\ndelete last op\n"); \
  6441. } while (0)
  6442. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6443. #define emit_empty_label_addr_and_frame_ip(type) \
  6444. do { \
  6445. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  6446. loader_ctx->p_code_compiled, error_buf, \
  6447. error_buf_size)) \
  6448. goto fail; \
  6449. /* label address, to be patched */ \
  6450. wasm_loader_emit_ptr(loader_ctx, NULL); \
  6451. } while (0)
  6452. #define emit_br_info(frame_csp) \
  6453. do { \
  6454. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  6455. error_buf_size)) \
  6456. goto fail; \
  6457. } while (0)
  6458. #define LAST_OP_OUTPUT_I32() \
  6459. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  6460. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  6461. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  6462. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  6463. || (last_op >= WASM_OP_I32_WRAP_I64 \
  6464. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  6465. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  6466. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  6467. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  6468. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  6469. || (last_op == EXT_OP_COPY_STACK_TOP)
  6470. #define LAST_OP_OUTPUT_I64() \
  6471. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  6472. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  6473. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  6474. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  6475. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  6476. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  6477. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  6478. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  6479. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  6480. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  6481. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  6482. #define GET_CONST_OFFSET(type, val) \
  6483. do { \
  6484. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  6485. &operand_offset, error_buf, \
  6486. error_buf_size))) \
  6487. goto fail; \
  6488. } while (0)
  6489. #define GET_CONST_F32_OFFSET(type, fval) \
  6490. do { \
  6491. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  6492. &operand_offset, error_buf, \
  6493. error_buf_size))) \
  6494. goto fail; \
  6495. } while (0)
  6496. #define GET_CONST_F64_OFFSET(type, fval) \
  6497. do { \
  6498. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  6499. &operand_offset, error_buf, \
  6500. error_buf_size))) \
  6501. goto fail; \
  6502. } while (0)
  6503. #define emit_operand(ctx, offset) \
  6504. do { \
  6505. wasm_loader_emit_int16(ctx, offset); \
  6506. LOG_OP("%d\t", offset); \
  6507. } while (0)
  6508. #define emit_byte(ctx, byte) \
  6509. do { \
  6510. wasm_loader_emit_uint8(ctx, byte); \
  6511. LOG_OP("%d\t", byte); \
  6512. } while (0)
  6513. #define emit_uint32(ctx, value) \
  6514. do { \
  6515. wasm_loader_emit_uint32(ctx, value); \
  6516. LOG_OP("%d\t", value); \
  6517. } while (0)
  6518. #define emit_uint64(ctx, value) \
  6519. do { \
  6520. wasm_loader_emit_const(ctx, &value, false); \
  6521. LOG_OP("%lld\t", value); \
  6522. } while (0)
  6523. #define emit_float32(ctx, value) \
  6524. do { \
  6525. wasm_loader_emit_const(ctx, &value, true); \
  6526. LOG_OP("%f\t", value); \
  6527. } while (0)
  6528. #define emit_float64(ctx, value) \
  6529. do { \
  6530. wasm_loader_emit_const(ctx, &value, false); \
  6531. LOG_OP("%f\t", value); \
  6532. } while (0)
  6533. static bool
  6534. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  6535. {
  6536. if (!(ctx->p_code_compiled =
  6537. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  6538. return false;
  6539. ctx->p_code_compiled_end =
  6540. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  6541. /* clean up frame ref */
  6542. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  6543. ctx->frame_ref = ctx->frame_ref_bottom;
  6544. ctx->stack_cell_num = 0;
  6545. #if WASM_ENABLE_GC != 0
  6546. /* clean up reftype map */
  6547. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  6548. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  6549. ctx->reftype_map_num = 0;
  6550. #endif
  6551. /* clean up frame csp */
  6552. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  6553. ctx->frame_csp = ctx->frame_csp_bottom;
  6554. ctx->csp_num = 0;
  6555. ctx->max_csp_num = 0;
  6556. /* clean up frame offset */
  6557. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  6558. ctx->frame_offset = ctx->frame_offset_bottom;
  6559. ctx->dynamic_offset = ctx->start_dynamic_offset;
  6560. /* init preserved local offsets */
  6561. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  6562. /* const buf is reserved */
  6563. return true;
  6564. }
  6565. static void
  6566. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  6567. {
  6568. ctx->code_compiled_size += size;
  6569. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  6570. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  6571. }
  6572. }
  6573. static void
  6574. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  6575. {
  6576. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  6577. if (ctx->p_code_compiled) {
  6578. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6579. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6580. #endif
  6581. bh_memcpy_s(ctx->p_code_compiled,
  6582. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  6583. value, size);
  6584. ctx->p_code_compiled += size;
  6585. }
  6586. else {
  6587. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6588. bh_assert((ctx->code_compiled_size & 1) == 0);
  6589. #endif
  6590. increase_compiled_code_space(ctx, size);
  6591. }
  6592. }
  6593. static void
  6594. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  6595. {
  6596. if (ctx->p_code_compiled) {
  6597. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6598. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6599. #endif
  6600. STORE_U32(ctx->p_code_compiled, value);
  6601. ctx->p_code_compiled += sizeof(uint32);
  6602. }
  6603. else {
  6604. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6605. bh_assert((ctx->code_compiled_size & 1) == 0);
  6606. #endif
  6607. increase_compiled_code_space(ctx, sizeof(uint32));
  6608. }
  6609. }
  6610. static void
  6611. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  6612. {
  6613. if (ctx->p_code_compiled) {
  6614. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6615. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6616. #endif
  6617. STORE_U16(ctx->p_code_compiled, (uint16)value);
  6618. ctx->p_code_compiled += sizeof(int16);
  6619. }
  6620. else {
  6621. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6622. bh_assert((ctx->code_compiled_size & 1) == 0);
  6623. #endif
  6624. increase_compiled_code_space(ctx, sizeof(uint16));
  6625. }
  6626. }
  6627. static void
  6628. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  6629. {
  6630. if (ctx->p_code_compiled) {
  6631. *(ctx->p_code_compiled) = value;
  6632. ctx->p_code_compiled += sizeof(uint8);
  6633. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6634. ctx->p_code_compiled++;
  6635. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6636. #endif
  6637. }
  6638. else {
  6639. increase_compiled_code_space(ctx, sizeof(uint8));
  6640. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6641. increase_compiled_code_space(ctx, sizeof(uint8));
  6642. bh_assert((ctx->code_compiled_size & 1) == 0);
  6643. #endif
  6644. }
  6645. }
  6646. static void
  6647. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  6648. {
  6649. if (ctx->p_code_compiled) {
  6650. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6651. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6652. #endif
  6653. STORE_PTR(ctx->p_code_compiled, value);
  6654. ctx->p_code_compiled += sizeof(void *);
  6655. }
  6656. else {
  6657. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6658. bh_assert((ctx->code_compiled_size & 1) == 0);
  6659. #endif
  6660. increase_compiled_code_space(ctx, sizeof(void *));
  6661. }
  6662. }
  6663. static void
  6664. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  6665. {
  6666. if (ctx->p_code_compiled) {
  6667. ctx->p_code_compiled -= size;
  6668. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6669. if (size == sizeof(uint8)) {
  6670. ctx->p_code_compiled--;
  6671. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6672. }
  6673. #endif
  6674. }
  6675. else {
  6676. ctx->code_compiled_size -= size;
  6677. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6678. if (size == sizeof(uint8)) {
  6679. ctx->code_compiled_size--;
  6680. bh_assert((ctx->code_compiled_size & 1) == 0);
  6681. }
  6682. #endif
  6683. }
  6684. }
  6685. static bool
  6686. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  6687. uint32 local_index, uint32 local_type,
  6688. bool *preserved, char *error_buf,
  6689. uint32 error_buf_size)
  6690. {
  6691. uint32 i = 0;
  6692. int16 preserved_offset = (int16)local_index;
  6693. *preserved = false;
  6694. while (i < loader_ctx->stack_cell_num) {
  6695. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  6696. /* move previous local into dynamic space before a set/tee_local opcode
  6697. */
  6698. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  6699. if (!(*preserved)) {
  6700. *preserved = true;
  6701. skip_label();
  6702. preserved_offset = loader_ctx->preserved_local_offset;
  6703. if (loader_ctx->p_code_compiled) {
  6704. bh_assert(preserved_offset != (int16)local_index);
  6705. }
  6706. if (is_32bit_type(local_type)) {
  6707. /* Only increase preserve offset in the second traversal */
  6708. if (loader_ctx->p_code_compiled)
  6709. loader_ctx->preserved_local_offset++;
  6710. emit_label(EXT_OP_COPY_STACK_TOP);
  6711. }
  6712. else {
  6713. if (loader_ctx->p_code_compiled)
  6714. loader_ctx->preserved_local_offset += 2;
  6715. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  6716. }
  6717. emit_operand(loader_ctx, local_index);
  6718. emit_operand(loader_ctx, preserved_offset);
  6719. emit_label(opcode);
  6720. }
  6721. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  6722. }
  6723. if (is_32bit_type(cur_type))
  6724. i++;
  6725. else
  6726. i += 2;
  6727. }
  6728. (void)error_buf;
  6729. (void)error_buf_size;
  6730. return true;
  6731. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6732. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6733. fail:
  6734. return false;
  6735. #endif
  6736. #endif
  6737. }
  6738. static bool
  6739. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  6740. char *error_buf, uint32 error_buf_size)
  6741. {
  6742. uint32 i = 0;
  6743. bool preserve_local;
  6744. /* preserve locals before blocks to ensure that "tee/set_local" inside
  6745. blocks will not influence the value of these locals */
  6746. while (i < loader_ctx->stack_cell_num) {
  6747. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  6748. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  6749. if ((cur_offset < loader_ctx->start_dynamic_offset)
  6750. && (cur_offset >= 0)) {
  6751. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  6752. cur_type, &preserve_local,
  6753. error_buf, error_buf_size)))
  6754. return false;
  6755. }
  6756. if (is_32bit_type(cur_type)) {
  6757. i++;
  6758. }
  6759. else {
  6760. i += 2;
  6761. }
  6762. }
  6763. return true;
  6764. }
  6765. static bool
  6766. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  6767. uint8 *p_code_compiled, char *error_buf,
  6768. uint32 error_buf_size)
  6769. {
  6770. BranchBlockPatch *patch =
  6771. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  6772. if (!patch) {
  6773. return false;
  6774. }
  6775. patch->patch_type = patch_type;
  6776. patch->code_compiled = p_code_compiled;
  6777. if (!frame_csp->patch_list) {
  6778. frame_csp->patch_list = patch;
  6779. patch->next = NULL;
  6780. }
  6781. else {
  6782. patch->next = frame_csp->patch_list;
  6783. frame_csp->patch_list = patch;
  6784. }
  6785. return true;
  6786. }
  6787. static void
  6788. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  6789. {
  6790. BranchBlock *frame_csp = ctx->frame_csp - depth;
  6791. BranchBlockPatch *node = frame_csp->patch_list;
  6792. BranchBlockPatch *node_prev = NULL, *node_next;
  6793. if (!ctx->p_code_compiled)
  6794. return;
  6795. while (node) {
  6796. node_next = node->next;
  6797. if (node->patch_type == patch_type) {
  6798. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  6799. if (node_prev == NULL) {
  6800. frame_csp->patch_list = node_next;
  6801. }
  6802. else {
  6803. node_prev->next = node_next;
  6804. }
  6805. wasm_runtime_free(node);
  6806. }
  6807. else {
  6808. node_prev = node;
  6809. }
  6810. node = node_next;
  6811. }
  6812. }
  6813. static bool
  6814. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  6815. char *error_buf, uint32 error_buf_size)
  6816. {
  6817. /* br info layout:
  6818. * a) arity of target block
  6819. * b) total cell num of arity values
  6820. * c) each arity value's cell num
  6821. * d) each arity value's src frame offset
  6822. * e) each arity values's dst dynamic offset
  6823. * f) branch target address
  6824. *
  6825. * Note: b-e are omitted when arity is 0 so that
  6826. * interpreter can recover the br info quickly.
  6827. */
  6828. BlockType *block_type = &frame_csp->block_type;
  6829. uint8 *types = NULL, cell;
  6830. #if WASM_ENABLE_GC != 0
  6831. WASMRefTypeMap *reftype_maps;
  6832. uint32 reftype_map_count;
  6833. #endif
  6834. uint32 arity = 0;
  6835. int32 i;
  6836. int16 *frame_offset = ctx->frame_offset;
  6837. uint16 dynamic_offset;
  6838. /* Note: loop's arity is different from if and block. loop's arity is
  6839. * its parameter count while if and block arity is result count.
  6840. */
  6841. #if WASM_ENABLE_GC == 0
  6842. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  6843. arity = block_type_get_param_types(block_type, &types);
  6844. else
  6845. arity = block_type_get_result_types(block_type, &types);
  6846. #else
  6847. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  6848. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  6849. &reftype_map_count);
  6850. else
  6851. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  6852. &reftype_map_count);
  6853. #endif
  6854. /* Part a */
  6855. emit_uint32(ctx, arity);
  6856. if (arity) {
  6857. /* Part b */
  6858. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  6859. /* Part c */
  6860. for (i = (int32)arity - 1; i >= 0; i--) {
  6861. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6862. emit_byte(ctx, cell);
  6863. }
  6864. /* Part d */
  6865. for (i = (int32)arity - 1; i >= 0; i--) {
  6866. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6867. frame_offset -= cell;
  6868. emit_operand(ctx, *(int16 *)(frame_offset));
  6869. }
  6870. /* Part e */
  6871. dynamic_offset =
  6872. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  6873. for (i = (int32)arity - 1; i >= 0; i--) {
  6874. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6875. dynamic_offset -= cell;
  6876. emit_operand(ctx, dynamic_offset);
  6877. }
  6878. }
  6879. /* Part f */
  6880. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  6881. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  6882. }
  6883. else {
  6884. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  6885. error_buf, error_buf_size))
  6886. return false;
  6887. /* label address, to be patched */
  6888. wasm_loader_emit_ptr(ctx, NULL);
  6889. }
  6890. return true;
  6891. }
  6892. static bool
  6893. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  6894. bool disable_emit, int16 operand_offset,
  6895. char *error_buf, uint32 error_buf_size)
  6896. {
  6897. if (type == VALUE_TYPE_VOID)
  6898. return true;
  6899. /* only check memory overflow in first traverse */
  6900. if (ctx->p_code_compiled == NULL) {
  6901. if (!check_offset_push(ctx, error_buf, error_buf_size))
  6902. return false;
  6903. }
  6904. if (disable_emit)
  6905. *(ctx->frame_offset)++ = operand_offset;
  6906. else {
  6907. emit_operand(ctx, ctx->dynamic_offset);
  6908. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  6909. ctx->dynamic_offset++;
  6910. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  6911. ctx->max_dynamic_offset = ctx->dynamic_offset;
  6912. if (ctx->max_dynamic_offset >= INT16_MAX) {
  6913. goto fail;
  6914. }
  6915. }
  6916. }
  6917. if (is_32bit_type(type))
  6918. return true;
  6919. if (ctx->p_code_compiled == NULL) {
  6920. if (!check_offset_push(ctx, error_buf, error_buf_size))
  6921. return false;
  6922. }
  6923. ctx->frame_offset++;
  6924. if (!disable_emit) {
  6925. ctx->dynamic_offset++;
  6926. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  6927. ctx->max_dynamic_offset = ctx->dynamic_offset;
  6928. if (ctx->max_dynamic_offset >= INT16_MAX) {
  6929. goto fail;
  6930. }
  6931. }
  6932. }
  6933. return true;
  6934. fail:
  6935. set_error_buf(error_buf, error_buf_size,
  6936. "fast interpreter offset overflow");
  6937. return false;
  6938. }
  6939. /* This function should be in front of wasm_loader_pop_frame_ref
  6940. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  6941. will be modified by wasm_loader_pop_frame_ref */
  6942. static bool
  6943. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  6944. char *error_buf, uint32 error_buf_size)
  6945. {
  6946. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  6947. then current block is the function block */
  6948. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  6949. BranchBlock *cur_block = ctx->frame_csp - depth;
  6950. int32 available_stack_cell =
  6951. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6952. /* Directly return success if current block is in stack
  6953. * polymorphic state while stack is empty. */
  6954. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  6955. return true;
  6956. if (type == VALUE_TYPE_VOID)
  6957. return true;
  6958. if (is_32bit_type(type)) {
  6959. /* Check the offset stack bottom to ensure the frame offset
  6960. stack will not go underflow. But we don't thrown error
  6961. and return true here, because the error msg should be
  6962. given in wasm_loader_pop_frame_ref */
  6963. if (!check_offset_pop(ctx, 1))
  6964. return true;
  6965. ctx->frame_offset -= 1;
  6966. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  6967. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  6968. ctx->dynamic_offset -= 1;
  6969. }
  6970. else {
  6971. if (!check_offset_pop(ctx, 2))
  6972. return true;
  6973. ctx->frame_offset -= 2;
  6974. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  6975. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  6976. ctx->dynamic_offset -= 2;
  6977. }
  6978. emit_operand(ctx, *(ctx->frame_offset));
  6979. (void)error_buf;
  6980. (void)error_buf_size;
  6981. return true;
  6982. }
  6983. static bool
  6984. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  6985. uint8 type_push, uint8 type_pop,
  6986. bool disable_emit, int16 operand_offset,
  6987. char *error_buf, uint32 error_buf_size)
  6988. {
  6989. uint8 i;
  6990. for (i = 0; i < pop_cnt; i++) {
  6991. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  6992. error_buf_size))
  6993. return false;
  6994. }
  6995. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  6996. operand_offset, error_buf,
  6997. error_buf_size))
  6998. return false;
  6999. return true;
  7000. }
  7001. static bool
  7002. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7003. bool disable_emit, int16 operand_offset,
  7004. char *error_buf, uint32 error_buf_size)
  7005. {
  7006. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  7007. error_buf, error_buf_size)))
  7008. return false;
  7009. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  7010. return false;
  7011. return true;
  7012. }
  7013. static bool
  7014. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7015. char *error_buf, uint32 error_buf_size)
  7016. {
  7017. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  7018. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  7019. return false;
  7020. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  7021. return false;
  7022. return true;
  7023. }
  7024. static bool
  7025. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  7026. uint8 type_push, uint8 type_pop,
  7027. bool disable_emit, int16 operand_offset,
  7028. char *error_buf, uint32 error_buf_size)
  7029. {
  7030. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  7031. disable_emit, operand_offset,
  7032. error_buf, error_buf_size))
  7033. return false;
  7034. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  7035. error_buf, error_buf_size))
  7036. return false;
  7037. return true;
  7038. }
  7039. static bool
  7040. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  7041. int16 *offset, char *error_buf,
  7042. uint32 error_buf_size)
  7043. {
  7044. int8 bytes_to_increase;
  7045. int16 operand_offset = 0;
  7046. Const *c;
  7047. /* Search existing constant */
  7048. for (c = (Const *)ctx->const_buf;
  7049. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  7050. /* TODO: handle v128 type? */
  7051. if ((type == c->value_type)
  7052. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  7053. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  7054. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  7055. || (type == VALUE_TYPE_FUNCREF
  7056. && *(int32 *)value == c->value.i32)
  7057. || (type == VALUE_TYPE_EXTERNREF
  7058. && *(int32 *)value == c->value.i32)
  7059. #endif
  7060. || (type == VALUE_TYPE_F64
  7061. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  7062. || (type == VALUE_TYPE_F32
  7063. && (0
  7064. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  7065. operand_offset = c->slot_index;
  7066. break;
  7067. }
  7068. if (is_32bit_type(c->value_type))
  7069. operand_offset += 1;
  7070. else
  7071. operand_offset += 2;
  7072. }
  7073. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  7074. /* New constant, append to the const buffer */
  7075. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  7076. bytes_to_increase = 2;
  7077. }
  7078. else {
  7079. bytes_to_increase = 1;
  7080. }
  7081. /* The max cell num of const buffer is 32768 since the valid index range
  7082. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  7083. * full */
  7084. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  7085. *offset = 0;
  7086. return true;
  7087. }
  7088. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  7089. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  7090. ctx->const_buf_size + 4 * sizeof(Const));
  7091. ctx->const_buf_size += 4 * sizeof(Const);
  7092. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  7093. }
  7094. c->value_type = type;
  7095. switch (type) {
  7096. case VALUE_TYPE_F64:
  7097. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  7098. sizeof(float64));
  7099. ctx->const_cell_num += 2;
  7100. /* The const buf will be reversed, we use the second cell */
  7101. /* of the i64/f64 const so the finnal offset is corrent */
  7102. operand_offset++;
  7103. break;
  7104. case VALUE_TYPE_I64:
  7105. c->value.i64 = *(int64 *)value;
  7106. ctx->const_cell_num += 2;
  7107. operand_offset++;
  7108. break;
  7109. case VALUE_TYPE_F32:
  7110. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  7111. sizeof(float32));
  7112. ctx->const_cell_num++;
  7113. break;
  7114. case VALUE_TYPE_I32:
  7115. c->value.i32 = *(int32 *)value;
  7116. ctx->const_cell_num++;
  7117. break;
  7118. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  7119. case VALUE_TYPE_EXTERNREF:
  7120. case VALUE_TYPE_FUNCREF:
  7121. c->value.i32 = *(int32 *)value;
  7122. ctx->const_cell_num++;
  7123. break;
  7124. #endif
  7125. default:
  7126. break;
  7127. }
  7128. c->slot_index = operand_offset;
  7129. ctx->num_const++;
  7130. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  7131. (int64)c->value.i64);
  7132. }
  7133. /* use negetive index for const */
  7134. operand_offset = -(operand_offset + 1);
  7135. *offset = operand_offset;
  7136. return true;
  7137. fail:
  7138. return false;
  7139. }
  7140. /*
  7141. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  7142. -- Mostly used for the binary / compare operation
  7143. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  7144. -- Mostly used in block / control instructions
  7145. The POP will always emit the offset on the top of the frame_offset stack
  7146. PUSH can be used in two ways:
  7147. 1. directly PUSH:
  7148. PUSH_XXX();
  7149. will allocate a dynamic space and emit
  7150. 2. silent PUSH:
  7151. operand_offset = xxx; disable_emit = true;
  7152. PUSH_XXX();
  7153. only push the frame_offset stack, no emit
  7154. */
  7155. #define TEMPLATE_PUSH(Type) \
  7156. do { \
  7157. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  7158. disable_emit, operand_offset, \
  7159. error_buf, error_buf_size)) \
  7160. goto fail; \
  7161. } while (0)
  7162. #define TEMPLATE_PUSH_REF(Type) \
  7163. do { \
  7164. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  7165. operand_offset, error_buf, \
  7166. error_buf_size)) \
  7167. goto fail; \
  7168. } while (0)
  7169. #define TEMPLATE_POP(Type) \
  7170. do { \
  7171. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  7172. error_buf, error_buf_size)) \
  7173. goto fail; \
  7174. } while (0)
  7175. #define TEMPLATE_POP_REF(Type) \
  7176. do { \
  7177. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  7178. error_buf_size)) \
  7179. goto fail; \
  7180. } while (0)
  7181. #define PUSH_OFFSET_TYPE(type) \
  7182. do { \
  7183. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  7184. operand_offset, error_buf, \
  7185. error_buf_size))) \
  7186. goto fail; \
  7187. } while (0)
  7188. #define POP_OFFSET_TYPE(type) \
  7189. do { \
  7190. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  7191. error_buf_size))) \
  7192. goto fail; \
  7193. } while (0)
  7194. #define POP_AND_PUSH(type_pop, type_push) \
  7195. do { \
  7196. if (!(wasm_loader_push_pop_frame_ref_offset( \
  7197. loader_ctx, 1, type_push, type_pop, disable_emit, \
  7198. operand_offset, error_buf, error_buf_size))) \
  7199. goto fail; \
  7200. } while (0)
  7201. /* type of POPs should be the same */
  7202. #define POP2_AND_PUSH(type_pop, type_push) \
  7203. do { \
  7204. if (!(wasm_loader_push_pop_frame_ref_offset( \
  7205. loader_ctx, 2, type_push, type_pop, disable_emit, \
  7206. operand_offset, error_buf, error_buf_size))) \
  7207. goto fail; \
  7208. } while (0)
  7209. #else /* WASM_ENABLE_FAST_INTERP */
  7210. #define TEMPLATE_PUSH(Type) \
  7211. do { \
  7212. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  7213. error_buf, error_buf_size))) \
  7214. goto fail; \
  7215. } while (0)
  7216. #define TEMPLATE_PUSH_REF(Type) \
  7217. do { \
  7218. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  7219. error_buf_size))) \
  7220. goto fail; \
  7221. } while (0)
  7222. #define TEMPLATE_POP(Type) \
  7223. do { \
  7224. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  7225. error_buf, error_buf_size))) \
  7226. goto fail; \
  7227. } while (0)
  7228. #define TEMPLATE_POP_REF(Type) \
  7229. do { \
  7230. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  7231. error_buf_size))) \
  7232. goto fail; \
  7233. } while (0)
  7234. #define POP_AND_PUSH(type_pop, type_push) \
  7235. do { \
  7236. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  7237. type_pop, error_buf, \
  7238. error_buf_size))) \
  7239. goto fail; \
  7240. } while (0)
  7241. /* type of POPs should be the same */
  7242. #define POP2_AND_PUSH(type_pop, type_push) \
  7243. do { \
  7244. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  7245. type_pop, error_buf, \
  7246. error_buf_size))) \
  7247. goto fail; \
  7248. } while (0)
  7249. #endif /* WASM_ENABLE_FAST_INTERP */
  7250. #define PUSH_I32() TEMPLATE_PUSH(I32)
  7251. #define PUSH_F32() TEMPLATE_PUSH(F32)
  7252. #define PUSH_I64() TEMPLATE_PUSH(I64)
  7253. #define PUSH_F64() TEMPLATE_PUSH(F64)
  7254. #define PUSH_V128() TEMPLATE_PUSH(V128)
  7255. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  7256. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  7257. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  7258. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  7259. #define POP_I32() TEMPLATE_POP(I32)
  7260. #define POP_F32() TEMPLATE_POP(F32)
  7261. #define POP_I64() TEMPLATE_POP(I64)
  7262. #define POP_F64() TEMPLATE_POP(F64)
  7263. #define POP_V128() TEMPLATE_POP(V128)
  7264. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  7265. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  7266. #if WASM_ENABLE_FAST_INTERP != 0
  7267. static bool
  7268. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  7269. bool disable_emit, char *error_buf, uint32 error_buf_size)
  7270. {
  7271. int16 operand_offset = 0;
  7272. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  7273. : loader_ctx->frame_csp;
  7274. BlockType *block_type = &block->block_type;
  7275. uint8 *return_types = NULL;
  7276. #if WASM_ENABLE_GC != 0
  7277. WASMRefTypeMap *reftype_maps = NULL;
  7278. uint32 reftype_map_count;
  7279. #endif
  7280. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  7281. int32 i = 0;
  7282. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  7283. *frame_offset_org = NULL;
  7284. #if WASM_ENABLE_GC == 0
  7285. return_count = block_type_get_result_types(block_type, &return_types);
  7286. #else
  7287. return_count = block_type_get_result_types(
  7288. block_type, &return_types, &reftype_maps, &reftype_map_count);
  7289. #endif
  7290. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  7291. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  7292. if (return_count == 1) {
  7293. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  7294. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  7295. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  7296. /* insert op_copy before else opcode */
  7297. if (opcode == WASM_OP_ELSE)
  7298. skip_label();
  7299. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  7300. : EXT_OP_COPY_STACK_TOP_I64);
  7301. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  7302. emit_operand(loader_ctx, block->dynamic_offset);
  7303. if (opcode == WASM_OP_ELSE) {
  7304. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  7305. }
  7306. else {
  7307. loader_ctx->frame_offset -= cell;
  7308. loader_ctx->dynamic_offset = block->dynamic_offset;
  7309. PUSH_OFFSET_TYPE(return_types[0]);
  7310. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7311. }
  7312. if (opcode == WASM_OP_ELSE)
  7313. emit_label(opcode);
  7314. }
  7315. return true;
  7316. }
  7317. /* Copy stack top values to block's results which are in dynamic space.
  7318. * The instruction format:
  7319. * Part a: values count
  7320. * Part b: all values total cell num
  7321. * Part c: each value's cell_num, src offset and dst offset
  7322. * Part d: each value's src offset and dst offset
  7323. * Part e: each value's dst offset
  7324. */
  7325. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  7326. dynamic_offset = dynamic_offset_org =
  7327. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  7328. /* First traversal to get the count of values needed to be copied. */
  7329. for (i = (int32)return_count - 1; i >= 0; i--) {
  7330. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  7331. frame_offset -= cells;
  7332. dynamic_offset -= cells;
  7333. if (dynamic_offset != *frame_offset) {
  7334. value_count++;
  7335. total_cel_num += cells;
  7336. }
  7337. }
  7338. if (value_count) {
  7339. uint32 j = 0;
  7340. uint8 *emit_data = NULL, *cells = NULL;
  7341. int16 *src_offsets = NULL;
  7342. uint16 *dst_offsets = NULL;
  7343. uint64 size =
  7344. (uint64)value_count
  7345. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  7346. /* Allocate memory for the emit data */
  7347. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  7348. return false;
  7349. cells = emit_data;
  7350. src_offsets = (int16 *)(cells + value_count);
  7351. dst_offsets = (uint16 *)(src_offsets + value_count);
  7352. /* insert op_copy before else opcode */
  7353. if (opcode == WASM_OP_ELSE)
  7354. skip_label();
  7355. emit_label(EXT_OP_COPY_STACK_VALUES);
  7356. /* Part a) */
  7357. emit_uint32(loader_ctx, value_count);
  7358. /* Part b) */
  7359. emit_uint32(loader_ctx, total_cel_num);
  7360. /* Second traversal to get each value's cell num, src offset and dst
  7361. * offset. */
  7362. frame_offset = frame_offset_org;
  7363. dynamic_offset = dynamic_offset_org;
  7364. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  7365. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  7366. frame_offset -= cell;
  7367. dynamic_offset -= cell;
  7368. if (dynamic_offset != *frame_offset) {
  7369. /* cell num */
  7370. cells[j] = cell;
  7371. /* src offset */
  7372. src_offsets[j] = *frame_offset;
  7373. /* dst offset */
  7374. dst_offsets[j] = dynamic_offset;
  7375. j++;
  7376. }
  7377. if (opcode == WASM_OP_ELSE) {
  7378. *frame_offset = dynamic_offset;
  7379. }
  7380. else {
  7381. loader_ctx->frame_offset = frame_offset;
  7382. loader_ctx->dynamic_offset = dynamic_offset;
  7383. PUSH_OFFSET_TYPE(return_types[i]);
  7384. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7385. loader_ctx->frame_offset = frame_offset_org;
  7386. loader_ctx->dynamic_offset = dynamic_offset_org;
  7387. }
  7388. }
  7389. bh_assert(j == value_count);
  7390. /* Emit the cells, src_offsets and dst_offsets */
  7391. for (j = 0; j < value_count; j++)
  7392. emit_byte(loader_ctx, cells[j]);
  7393. for (j = 0; j < value_count; j++)
  7394. emit_operand(loader_ctx, src_offsets[j]);
  7395. for (j = 0; j < value_count; j++)
  7396. emit_operand(loader_ctx, dst_offsets[j]);
  7397. if (opcode == WASM_OP_ELSE)
  7398. emit_label(opcode);
  7399. wasm_runtime_free(emit_data);
  7400. }
  7401. return true;
  7402. fail:
  7403. return false;
  7404. }
  7405. #endif /* WASM_ENABLE_FAST_INTERP */
  7406. #define RESERVE_BLOCK_RET() \
  7407. do { \
  7408. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  7409. error_buf_size)) \
  7410. goto fail; \
  7411. } while (0)
  7412. #define PUSH_TYPE(type) \
  7413. do { \
  7414. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  7415. error_buf_size))) \
  7416. goto fail; \
  7417. } while (0)
  7418. #define POP_TYPE(type) \
  7419. do { \
  7420. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  7421. error_buf_size))) \
  7422. goto fail; \
  7423. } while (0)
  7424. #define PUSH_CSP(label_type, block_type, _start_addr) \
  7425. do { \
  7426. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  7427. _start_addr, error_buf, \
  7428. error_buf_size)) \
  7429. goto fail; \
  7430. } while (0)
  7431. #define POP_CSP() \
  7432. do { \
  7433. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  7434. goto fail; \
  7435. } while (0)
  7436. #if WASM_ENABLE_GC == 0
  7437. #define GET_LOCAL_REFTYPE() (void)0
  7438. #else
  7439. #define GET_LOCAL_REFTYPE() \
  7440. do { \
  7441. if (wasm_is_type_multi_byte_type(local_type)) { \
  7442. WASMRefType *_ref_type; \
  7443. if (local_idx < param_count) \
  7444. _ref_type = wasm_reftype_map_find( \
  7445. param_reftype_maps, param_reftype_map_count, local_idx); \
  7446. else \
  7447. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  7448. local_reftype_map_count, \
  7449. local_idx - param_count); \
  7450. bh_assert(_ref_type); \
  7451. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  7452. wasm_reftype_struct_size(_ref_type)); \
  7453. } \
  7454. } while (0)
  7455. #endif
  7456. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  7457. do { \
  7458. read_leb_uint32(p, p_end, local_idx); \
  7459. if (local_idx >= param_count + local_count) { \
  7460. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  7461. goto fail; \
  7462. } \
  7463. local_type = local_idx < param_count \
  7464. ? param_types[local_idx] \
  7465. : local_types[local_idx - param_count]; \
  7466. local_offset = local_offsets[local_idx]; \
  7467. GET_LOCAL_REFTYPE(); \
  7468. } while (0)
  7469. #define CHECK_BR(depth) \
  7470. do { \
  7471. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  7472. error_buf_size)) \
  7473. goto fail; \
  7474. } while (0)
  7475. static bool
  7476. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  7477. {
  7478. if (module->memory_count == 0 && module->import_memory_count == 0) {
  7479. set_error_buf(error_buf, error_buf_size, "unknown memory");
  7480. return false;
  7481. }
  7482. return true;
  7483. }
  7484. #define CHECK_MEMORY() \
  7485. do { \
  7486. if (!check_memory(module, error_buf, error_buf_size)) \
  7487. goto fail; \
  7488. } while (0)
  7489. static bool
  7490. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  7491. uint32 error_buf_size)
  7492. {
  7493. uint8 mem_access_aligns[] = {
  7494. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  7495. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  7496. };
  7497. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  7498. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  7499. set_error_buf(error_buf, error_buf_size,
  7500. "alignment must not be larger than natural");
  7501. return false;
  7502. }
  7503. return true;
  7504. }
  7505. #if WASM_ENABLE_SIMD != 0
  7506. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7507. static bool
  7508. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  7509. uint32 error_buf_size)
  7510. {
  7511. uint8 mem_access_aligns[] = {
  7512. 4, /* load */
  7513. 3, 3, 3, 3, 3, 3, /* load and extend */
  7514. 0, 1, 2, 3, /* load and splat */
  7515. 4, /* store */
  7516. };
  7517. uint8 mem_access_aligns_load_lane[] = {
  7518. 0, 1, 2, 3, /* load lane */
  7519. 0, 1, 2, 3, /* store lane */
  7520. 2, 3 /* store zero */
  7521. };
  7522. if (!((opcode <= SIMD_v128_store)
  7523. || (SIMD_v128_load8_lane <= opcode
  7524. && opcode <= SIMD_v128_load64_zero))) {
  7525. set_error_buf(error_buf, error_buf_size,
  7526. "the opcode doesn't include memarg");
  7527. return false;
  7528. }
  7529. if ((opcode <= SIMD_v128_store
  7530. && align > mem_access_aligns[opcode - SIMD_v128_load])
  7531. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  7532. && align > mem_access_aligns_load_lane[opcode
  7533. - SIMD_v128_load8_lane])) {
  7534. set_error_buf(error_buf, error_buf_size,
  7535. "alignment must not be larger than natural");
  7536. return false;
  7537. }
  7538. return true;
  7539. }
  7540. static bool
  7541. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  7542. uint32 error_buf_size)
  7543. {
  7544. switch (opcode) {
  7545. case SIMD_i8x16_extract_lane_s:
  7546. case SIMD_i8x16_extract_lane_u:
  7547. case SIMD_i8x16_replace_lane:
  7548. if (lane >= 16) {
  7549. goto fail;
  7550. }
  7551. break;
  7552. case SIMD_i16x8_extract_lane_s:
  7553. case SIMD_i16x8_extract_lane_u:
  7554. case SIMD_i16x8_replace_lane:
  7555. if (lane >= 8) {
  7556. goto fail;
  7557. }
  7558. break;
  7559. case SIMD_i32x4_extract_lane:
  7560. case SIMD_i32x4_replace_lane:
  7561. case SIMD_f32x4_extract_lane:
  7562. case SIMD_f32x4_replace_lane:
  7563. if (lane >= 4) {
  7564. goto fail;
  7565. }
  7566. break;
  7567. case SIMD_i64x2_extract_lane:
  7568. case SIMD_i64x2_replace_lane:
  7569. case SIMD_f64x2_extract_lane:
  7570. case SIMD_f64x2_replace_lane:
  7571. if (lane >= 2) {
  7572. goto fail;
  7573. }
  7574. break;
  7575. case SIMD_v128_load8_lane:
  7576. case SIMD_v128_load16_lane:
  7577. case SIMD_v128_load32_lane:
  7578. case SIMD_v128_load64_lane:
  7579. case SIMD_v128_store8_lane:
  7580. case SIMD_v128_store16_lane:
  7581. case SIMD_v128_store32_lane:
  7582. case SIMD_v128_store64_lane:
  7583. case SIMD_v128_load32_zero:
  7584. case SIMD_v128_load64_zero:
  7585. {
  7586. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  7587. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  7588. goto fail;
  7589. }
  7590. break;
  7591. }
  7592. default:
  7593. goto fail;
  7594. }
  7595. return true;
  7596. fail:
  7597. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  7598. return false;
  7599. }
  7600. static bool
  7601. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  7602. {
  7603. uint8 i;
  7604. for (i = 0; i != 16; ++i) {
  7605. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  7606. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  7607. return false;
  7608. }
  7609. }
  7610. return true;
  7611. }
  7612. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7613. #endif /* end of WASM_ENABLE_SIMD */
  7614. #if WASM_ENABLE_SHARED_MEMORY != 0
  7615. static bool
  7616. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  7617. uint32 error_buf_size)
  7618. {
  7619. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  7620. uint8 mem_access_aligns[] = {
  7621. 2, 3, 0, 1, 0, 1, 2,
  7622. };
  7623. uint8 expect;
  7624. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  7625. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  7626. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  7627. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  7628. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  7629. }
  7630. else {
  7631. /* 7 opcodes in every group */
  7632. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  7633. }
  7634. if (align != expect) {
  7635. set_error_buf(error_buf, error_buf_size,
  7636. "alignment isn't equal to natural");
  7637. return false;
  7638. }
  7639. return true;
  7640. }
  7641. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  7642. static bool
  7643. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  7644. bool is_br_table, char *error_buf, uint32 error_buf_size)
  7645. {
  7646. BranchBlock *target_block, *cur_block;
  7647. BlockType *target_block_type;
  7648. uint8 type, *types = NULL, *frame_ref;
  7649. uint32 arity = 0;
  7650. int32 i, available_stack_cell;
  7651. uint16 cell_num;
  7652. #if WASM_ENABLE_GC != 0
  7653. WASMRefTypeMap *frame_reftype_map;
  7654. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  7655. WASMRefType *ref_type;
  7656. uint32 reftype_map_count = 0;
  7657. int32 available_reftype_map;
  7658. bool is_type_multi_byte;
  7659. #endif
  7660. if (loader_ctx->csp_num < depth + 1) {
  7661. set_error_buf(error_buf, error_buf_size,
  7662. "unknown label, "
  7663. "unexpected end of section or function");
  7664. return false;
  7665. }
  7666. cur_block = loader_ctx->frame_csp - 1;
  7667. target_block = loader_ctx->frame_csp - (depth + 1);
  7668. target_block_type = &target_block->block_type;
  7669. frame_ref = loader_ctx->frame_ref;
  7670. #if WASM_ENABLE_GC != 0
  7671. frame_reftype_map = loader_ctx->frame_reftype_map;
  7672. #endif
  7673. /* Note: loop's arity is different from if and block. loop's arity is
  7674. * its parameter count while if and block arity is result count.
  7675. */
  7676. #if WASM_ENABLE_GC == 0
  7677. if (target_block->label_type == LABEL_TYPE_LOOP)
  7678. arity = block_type_get_param_types(target_block_type, &types);
  7679. else
  7680. arity = block_type_get_result_types(target_block_type, &types);
  7681. #else
  7682. if (target_block->label_type == LABEL_TYPE_LOOP)
  7683. arity = block_type_get_param_types(target_block_type, &types,
  7684. &reftype_maps, &reftype_map_count);
  7685. else
  7686. arity = block_type_get_result_types(target_block_type, &types,
  7687. &reftype_maps, &reftype_map_count);
  7688. #endif
  7689. /* If the stack is in polymorphic state, just clear the stack
  7690. * and then re-push the values to make the stack top values
  7691. * match block type. */
  7692. if (cur_block->is_stack_polymorphic && !is_br_table) {
  7693. #if WASM_ENABLE_GC != 0
  7694. int32 j = reftype_map_count - 1;
  7695. #endif
  7696. for (i = (int32)arity - 1; i >= 0; i--) {
  7697. #if WASM_ENABLE_GC != 0
  7698. if (wasm_is_type_multi_byte_type(types[i])) {
  7699. bh_assert(reftype_maps[j].index == i);
  7700. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7701. reftype_maps[j].ref_type,
  7702. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  7703. j--;
  7704. }
  7705. #endif
  7706. #if WASM_ENABLE_FAST_INTERP != 0
  7707. POP_OFFSET_TYPE(types[i]);
  7708. #endif
  7709. POP_TYPE(types[i]);
  7710. }
  7711. #if WASM_ENABLE_GC != 0
  7712. j = 0;
  7713. #endif
  7714. for (i = 0; i < (int32)arity; i++) {
  7715. #if WASM_ENABLE_GC != 0
  7716. if (wasm_is_type_multi_byte_type(types[i])) {
  7717. bh_assert(reftype_maps[j].index == i);
  7718. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7719. reftype_maps[j].ref_type,
  7720. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  7721. j++;
  7722. }
  7723. #endif
  7724. #if WASM_ENABLE_FAST_INTERP != 0
  7725. bool disable_emit = true;
  7726. int16 operand_offset = 0;
  7727. PUSH_OFFSET_TYPE(types[i]);
  7728. #endif
  7729. PUSH_TYPE(types[i]);
  7730. }
  7731. return true;
  7732. }
  7733. available_stack_cell =
  7734. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  7735. #if WASM_ENABLE_GC != 0
  7736. available_reftype_map =
  7737. (int32)(loader_ctx->reftype_map_num
  7738. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  7739. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  7740. #endif
  7741. /* Check stack top values match target block type */
  7742. for (i = (int32)arity - 1; i >= 0; i--) {
  7743. type = types[i];
  7744. #if WASM_ENABLE_GC != 0
  7745. ref_type = NULL;
  7746. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  7747. if (is_type_multi_byte) {
  7748. bh_assert(reftype_map);
  7749. ref_type = reftype_map->ref_type;
  7750. }
  7751. #endif
  7752. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7753. break;
  7754. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  7755. #if WASM_ENABLE_GC != 0
  7756. frame_reftype_map, available_reftype_map,
  7757. #endif
  7758. type,
  7759. #if WASM_ENABLE_GC != 0
  7760. ref_type,
  7761. #endif
  7762. error_buf, error_buf_size)) {
  7763. return false;
  7764. }
  7765. cell_num = wasm_value_type_cell_num(types[i]);
  7766. frame_ref -= cell_num;
  7767. available_stack_cell -= cell_num;
  7768. #if WASM_ENABLE_GC != 0
  7769. if (is_type_multi_byte) {
  7770. frame_reftype_map--;
  7771. available_reftype_map--;
  7772. reftype_map--;
  7773. }
  7774. #endif
  7775. }
  7776. return true;
  7777. fail:
  7778. return false;
  7779. }
  7780. static BranchBlock *
  7781. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  7782. bool is_br_table, char *error_buf, uint32 error_buf_size)
  7783. {
  7784. uint8 *p = *p_buf, *p_end = buf_end;
  7785. BranchBlock *frame_csp_tmp;
  7786. uint32 depth;
  7787. read_leb_uint32(p, p_end, depth);
  7788. if (!wasm_loader_check_br(loader_ctx, depth, is_br_table, error_buf,
  7789. error_buf_size)) {
  7790. goto fail;
  7791. }
  7792. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  7793. #if WASM_ENABLE_FAST_INTERP != 0
  7794. emit_br_info(frame_csp_tmp);
  7795. #endif
  7796. *p_buf = p;
  7797. return frame_csp_tmp;
  7798. fail:
  7799. return NULL;
  7800. }
  7801. static bool
  7802. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  7803. char *error_buf, uint32 error_buf_size)
  7804. {
  7805. BlockType *block_type = &block->block_type;
  7806. uint8 *return_types = NULL;
  7807. uint32 return_count = 0;
  7808. int32 available_stack_cell, return_cell_num, i;
  7809. uint8 *frame_ref = NULL;
  7810. #if WASM_ENABLE_GC != 0
  7811. WASMRefTypeMap *frame_reftype_map;
  7812. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  7813. WASMRefType *ref_type;
  7814. uint32 param_count, return_reftype_map_count = 0;
  7815. int32 available_reftype_map =
  7816. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  7817. #endif
  7818. available_stack_cell =
  7819. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  7820. #if WASM_ENABLE_GC == 0
  7821. return_count = block_type_get_result_types(block_type, &return_types);
  7822. #else
  7823. return_count = block_type_get_result_types(block_type, &return_types,
  7824. &return_reftype_maps,
  7825. &return_reftype_map_count);
  7826. param_count =
  7827. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  7828. (void)param_count;
  7829. #endif
  7830. return_cell_num =
  7831. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  7832. /* If the stack is in polymorphic state, just clear the stack
  7833. * and then re-push the values to make the stack top values
  7834. * match block type. */
  7835. if (block->is_stack_polymorphic) {
  7836. #if WASM_ENABLE_GC != 0
  7837. int32 j = return_reftype_map_count - 1;
  7838. #endif
  7839. for (i = (int32)return_count - 1; i >= 0; i--) {
  7840. #if WASM_ENABLE_GC != 0
  7841. if (wasm_is_type_multi_byte_type(return_types[i])) {
  7842. bh_assert(return_reftype_maps[j].index == i + param_count);
  7843. bh_memcpy_s(
  7844. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7845. return_reftype_maps[j].ref_type,
  7846. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  7847. j--;
  7848. }
  7849. #endif
  7850. #if WASM_ENABLE_FAST_INTERP != 0
  7851. POP_OFFSET_TYPE(return_types[i]);
  7852. #endif
  7853. POP_TYPE(return_types[i]);
  7854. }
  7855. /* Check stack is empty */
  7856. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  7857. set_error_buf(
  7858. error_buf, error_buf_size,
  7859. "type mismatch: stack size does not match block type");
  7860. goto fail;
  7861. }
  7862. #if WASM_ENABLE_GC != 0
  7863. j = 0;
  7864. #endif
  7865. for (i = 0; i < (int32)return_count; i++) {
  7866. #if WASM_ENABLE_GC != 0
  7867. if (wasm_is_type_multi_byte_type(return_types[i])) {
  7868. bh_assert(return_reftype_maps[j].index == i + param_count);
  7869. bh_memcpy_s(
  7870. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7871. return_reftype_maps[j].ref_type,
  7872. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  7873. j++;
  7874. }
  7875. #endif
  7876. #if WASM_ENABLE_FAST_INTERP != 0
  7877. bool disable_emit = true;
  7878. int16 operand_offset = 0;
  7879. PUSH_OFFSET_TYPE(return_types[i]);
  7880. #endif
  7881. PUSH_TYPE(return_types[i]);
  7882. }
  7883. return true;
  7884. }
  7885. /* Check stack cell num equals return cell num */
  7886. if (available_stack_cell != return_cell_num) {
  7887. set_error_buf(error_buf, error_buf_size,
  7888. "type mismatch: stack size does not match block type");
  7889. goto fail;
  7890. }
  7891. /* Check stack values match return types */
  7892. frame_ref = loader_ctx->frame_ref;
  7893. #if WASM_ENABLE_GC != 0
  7894. frame_reftype_map = loader_ctx->frame_reftype_map;
  7895. return_reftype_map =
  7896. return_reftype_map_count
  7897. ? return_reftype_maps + return_reftype_map_count - 1
  7898. : NULL;
  7899. #endif
  7900. for (i = (int32)return_count - 1; i >= 0; i--) {
  7901. uint8 type = return_types[i];
  7902. #if WASM_ENABLE_GC != 0
  7903. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  7904. ref_type = NULL;
  7905. if (is_type_multi_byte) {
  7906. bh_assert(return_reftype_map);
  7907. ref_type = return_reftype_map->ref_type;
  7908. }
  7909. #endif
  7910. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  7911. #if WASM_ENABLE_GC != 0
  7912. frame_reftype_map, available_reftype_map,
  7913. #endif
  7914. type,
  7915. #if WASM_ENABLE_GC != 0
  7916. ref_type,
  7917. #endif
  7918. error_buf, error_buf_size))
  7919. return false;
  7920. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  7921. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  7922. #if WASM_ENABLE_GC != 0
  7923. if (is_type_multi_byte) {
  7924. frame_reftype_map--;
  7925. available_reftype_map--;
  7926. return_reftype_map--;
  7927. }
  7928. #endif
  7929. }
  7930. return true;
  7931. fail:
  7932. return false;
  7933. }
  7934. #if WASM_ENABLE_FAST_INTERP != 0
  7935. /* Copy parameters to dynamic space.
  7936. * 1) POP original parameter out;
  7937. * 2) Push and copy original values to dynamic space.
  7938. * The copy instruction format:
  7939. * Part a: param count
  7940. * Part b: all param total cell num
  7941. * Part c: each param's cell_num, src offset and dst offset
  7942. * Part d: each param's src offset
  7943. * Part e: each param's dst offset
  7944. */
  7945. static bool
  7946. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  7947. char *error_buf, uint32 error_buf_size)
  7948. {
  7949. bool ret = false;
  7950. int16 *frame_offset = NULL;
  7951. uint8 *cells = NULL, cell;
  7952. int16 *src_offsets = NULL;
  7953. uint8 *emit_data = NULL;
  7954. uint32 i;
  7955. BranchBlock *block = loader_ctx->frame_csp - 1;
  7956. BlockType *block_type = &block->block_type;
  7957. WASMFuncType *wasm_type = block_type->u.type;
  7958. uint32 param_count = block_type->u.type->param_count;
  7959. int16 condition_offset = 0;
  7960. bool disable_emit = false;
  7961. int16 operand_offset = 0;
  7962. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  7963. /* For if block, we also need copy the condition operand offset. */
  7964. if (is_if_block)
  7965. size += sizeof(*cells) + sizeof(*src_offsets);
  7966. /* Allocate memory for the emit data */
  7967. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  7968. return false;
  7969. cells = emit_data;
  7970. src_offsets = (int16 *)(cells + param_count);
  7971. if (is_if_block)
  7972. condition_offset = *loader_ctx->frame_offset;
  7973. /* POP original parameter out */
  7974. for (i = 0; i < param_count; i++) {
  7975. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  7976. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7977. }
  7978. frame_offset = loader_ctx->frame_offset;
  7979. /* Get each param's cell num and src offset */
  7980. for (i = 0; i < param_count; i++) {
  7981. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  7982. cells[i] = cell;
  7983. src_offsets[i] = *frame_offset;
  7984. frame_offset += cell;
  7985. }
  7986. /* emit copy instruction */
  7987. emit_label(EXT_OP_COPY_STACK_VALUES);
  7988. /* Part a) */
  7989. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  7990. /* Part b) */
  7991. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  7992. : wasm_type->param_cell_num);
  7993. /* Part c) */
  7994. for (i = 0; i < param_count; i++)
  7995. emit_byte(loader_ctx, cells[i]);
  7996. if (is_if_block)
  7997. emit_byte(loader_ctx, 1);
  7998. /* Part d) */
  7999. for (i = 0; i < param_count; i++)
  8000. emit_operand(loader_ctx, src_offsets[i]);
  8001. if (is_if_block)
  8002. emit_operand(loader_ctx, condition_offset);
  8003. /* Part e) */
  8004. /* Push to dynamic space. The push will emit the dst offset. */
  8005. for (i = 0; i < param_count; i++)
  8006. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  8007. if (is_if_block)
  8008. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  8009. ret = true;
  8010. fail:
  8011. /* Free the emit data */
  8012. wasm_runtime_free(emit_data);
  8013. return ret;
  8014. }
  8015. #endif
  8016. #if WASM_ENABLE_GC == 0
  8017. #define RESET_REFTYPE_MAP_STACK() (void)0
  8018. #else
  8019. #define RESET_REFTYPE_MAP_STACK() \
  8020. do { \
  8021. loader_ctx->reftype_map_num = \
  8022. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  8023. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  8024. + loader_ctx->reftype_map_num; \
  8025. } while (0)
  8026. #endif
  8027. /* reset the stack to the state of before entering the last block */
  8028. #if WASM_ENABLE_FAST_INTERP != 0
  8029. #define RESET_STACK() \
  8030. do { \
  8031. loader_ctx->stack_cell_num = \
  8032. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  8033. loader_ctx->frame_ref = \
  8034. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  8035. loader_ctx->frame_offset = \
  8036. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  8037. RESET_REFTYPE_MAP_STACK(); \
  8038. } while (0)
  8039. #else
  8040. #define RESET_STACK() \
  8041. do { \
  8042. loader_ctx->stack_cell_num = \
  8043. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  8044. loader_ctx->frame_ref = \
  8045. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  8046. RESET_REFTYPE_MAP_STACK(); \
  8047. } while (0)
  8048. #endif
  8049. /* set current block's stack polymorphic state */
  8050. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  8051. do { \
  8052. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  8053. _cur_block->is_stack_polymorphic = flag; \
  8054. } while (0)
  8055. #define BLOCK_HAS_PARAM(block_type) \
  8056. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  8057. #define PRESERVE_LOCAL_FOR_BLOCK() \
  8058. do { \
  8059. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  8060. error_buf_size))) { \
  8061. goto fail; \
  8062. } \
  8063. } while (0)
  8064. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  8065. static bool
  8066. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  8067. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  8068. uint32 error_buf_size)
  8069. {
  8070. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  8071. return false;
  8072. }
  8073. if (table_idx < module->import_table_count) {
  8074. if (p_elem_type)
  8075. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  8076. #if WASM_ENABLE_GC != 0
  8077. if (p_ref_type)
  8078. *((WASMRefType **)p_ref_type) =
  8079. module->import_tables[table_idx].u.table.elem_ref_type;
  8080. #endif
  8081. }
  8082. else {
  8083. if (p_elem_type)
  8084. *p_elem_type =
  8085. module->tables[module->import_table_count + table_idx]
  8086. .elem_type;
  8087. #if WASM_ENABLE_GC != 0
  8088. if (p_ref_type)
  8089. *((WASMRefType **)p_ref_type) =
  8090. module->tables[module->import_table_count + table_idx]
  8091. .elem_ref_type;
  8092. #endif
  8093. }
  8094. return true;
  8095. }
  8096. static bool
  8097. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  8098. uint8 *p_elem_type, void **p_elem_ref_type,
  8099. char *error_buf, uint32 error_buf_size)
  8100. {
  8101. if (table_seg_idx >= module->table_seg_count) {
  8102. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  8103. table_seg_idx);
  8104. return false;
  8105. }
  8106. if (p_elem_type) {
  8107. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  8108. }
  8109. #if WASM_ENABLE_GC != 0
  8110. if (p_elem_ref_type)
  8111. *((WASMRefType **)p_elem_ref_type) =
  8112. module->table_segments[table_seg_idx].elem_ref_type;
  8113. #endif
  8114. return true;
  8115. }
  8116. #endif
  8117. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  8118. const uint8 *
  8119. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  8120. uint32 *len)
  8121. {
  8122. WASMCustomSection *section = module->custom_section_list;
  8123. while (section) {
  8124. if ((section->name_len == strlen(name))
  8125. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  8126. if (len) {
  8127. *len = section->content_len;
  8128. }
  8129. return section->content_addr;
  8130. }
  8131. section = section->next;
  8132. }
  8133. return false;
  8134. }
  8135. #endif
  8136. static bool
  8137. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  8138. uint32 cur_func_idx, char *error_buf,
  8139. uint32 error_buf_size)
  8140. {
  8141. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  8142. uint32 param_count, local_count, global_count;
  8143. uint8 *param_types, *local_types, local_type, global_type;
  8144. BlockType func_block_type;
  8145. uint16 *local_offsets, local_offset;
  8146. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  8147. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  8148. int32 i32_const = 0;
  8149. int64 i64_const;
  8150. uint8 opcode;
  8151. bool return_value = false;
  8152. WASMLoaderContext *loader_ctx;
  8153. BranchBlock *frame_csp_tmp;
  8154. #if WASM_ENABLE_GC != 0
  8155. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  8156. uint32 param_reftype_map_count, local_reftype_map_count;
  8157. int32 heap_type;
  8158. WASMRefType wasm_ref_type = { 0 };
  8159. bool need_ref_type_map;
  8160. #endif
  8161. #if WASM_ENABLE_FAST_INTERP != 0
  8162. uint8 *func_const_end, *func_const = NULL;
  8163. int16 operand_offset = 0;
  8164. uint8 last_op = 0;
  8165. bool disable_emit, preserve_local = false;
  8166. float32 f32_const;
  8167. float64 f64_const;
  8168. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  8169. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  8170. #endif
  8171. global_count = module->import_global_count + module->global_count;
  8172. param_count = func->func_type->param_count;
  8173. param_types = func->func_type->types;
  8174. func_block_type.is_value_type = false;
  8175. func_block_type.u.type = func->func_type;
  8176. local_count = func->local_count;
  8177. local_types = func->local_types;
  8178. local_offsets = func->local_offsets;
  8179. #if WASM_ENABLE_GC != 0
  8180. param_reftype_maps = func->func_type->ref_type_maps;
  8181. param_reftype_map_count = func->func_type->ref_type_map_count;
  8182. local_reftype_maps = func->local_ref_type_maps;
  8183. local_reftype_map_count = func->local_ref_type_map_count;
  8184. #endif
  8185. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  8186. goto fail;
  8187. }
  8188. #if WASM_ENABLE_GC != 0
  8189. loader_ctx->module = module;
  8190. loader_ctx->ref_type_set = module->ref_type_set;
  8191. loader_ctx->ref_type_tmp = &wasm_ref_type;
  8192. #endif
  8193. #if WASM_ENABLE_FAST_INTERP != 0
  8194. /* For the first traverse, the initial value of preserved_local_offset has
  8195. * not been determined, we use the INT16_MAX to represent that a slot has
  8196. * been copied to preserve space. For second traverse, this field will be
  8197. * set to the appropriate value in wasm_loader_ctx_reinit.
  8198. * This is for Issue #1230,
  8199. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  8200. * drop opcodes need to know which slots are preserved, so those slots will
  8201. * not be treated as dynamically allocated slots */
  8202. loader_ctx->preserved_local_offset = INT16_MAX;
  8203. re_scan:
  8204. if (loader_ctx->code_compiled_size > 0) {
  8205. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  8206. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  8207. goto fail;
  8208. }
  8209. p = func->code;
  8210. func->code_compiled = loader_ctx->p_code_compiled;
  8211. func->code_compiled_size = loader_ctx->code_compiled_size;
  8212. }
  8213. #endif
  8214. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  8215. while (p < p_end) {
  8216. opcode = *p++;
  8217. #if WASM_ENABLE_FAST_INTERP != 0
  8218. p_org = p;
  8219. disable_emit = false;
  8220. emit_label(opcode);
  8221. #endif
  8222. switch (opcode) {
  8223. case WASM_OP_UNREACHABLE:
  8224. RESET_STACK();
  8225. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8226. break;
  8227. case WASM_OP_NOP:
  8228. #if WASM_ENABLE_FAST_INTERP != 0
  8229. skip_label();
  8230. #endif
  8231. break;
  8232. case WASM_OP_IF:
  8233. #if WASM_ENABLE_FAST_INTERP != 0
  8234. PRESERVE_LOCAL_FOR_BLOCK();
  8235. #endif
  8236. POP_I32();
  8237. goto handle_op_block_and_loop;
  8238. case WASM_OP_BLOCK:
  8239. case WASM_OP_LOOP:
  8240. #if WASM_ENABLE_FAST_INTERP != 0
  8241. PRESERVE_LOCAL_FOR_BLOCK();
  8242. #endif
  8243. handle_op_block_and_loop:
  8244. {
  8245. uint8 value_type;
  8246. BlockType block_type;
  8247. CHECK_BUF(p, p_end, 1);
  8248. value_type = read_uint8(p);
  8249. if (is_byte_a_type(value_type)) {
  8250. /* If the first byte is one of these special values:
  8251. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  8252. * the single return value. */
  8253. block_type.is_value_type = true;
  8254. block_type.u.value_type.type = value_type;
  8255. #if WASM_ENABLE_GC != 0
  8256. if (value_type != VALUE_TYPE_VOID) {
  8257. p_org = p;
  8258. p--;
  8259. if (!resolve_value_type((const uint8 **)&p, p_end,
  8260. module, &need_ref_type_map,
  8261. &wasm_ref_type, false,
  8262. error_buf, error_buf_size)) {
  8263. goto fail;
  8264. }
  8265. if (need_ref_type_map) {
  8266. block_type.u.value_type.ref_type_map.index = 0;
  8267. if (!(block_type.u.value_type.ref_type_map
  8268. .ref_type = reftype_set_insert(
  8269. module->ref_type_set, &wasm_ref_type,
  8270. error_buf, error_buf_size))) {
  8271. goto fail;
  8272. }
  8273. }
  8274. /* Set again as the type might be changed, e.g.
  8275. (ref null any) to anyref */
  8276. block_type.u.value_type.type = wasm_ref_type.ref_type;
  8277. #if WASM_ENABLE_FAST_INTERP == 0
  8278. while (p_org < p) {
  8279. #if WASM_ENABLE_DEBUG_INTERP != 0
  8280. if (!record_fast_op(module, p_org, *p_org,
  8281. error_buf, error_buf_size)) {
  8282. goto fail;
  8283. }
  8284. #endif
  8285. /* Ignore extra bytes for interpreter */
  8286. *p_org++ = WASM_OP_NOP;
  8287. }
  8288. #endif
  8289. }
  8290. #endif /* end of WASM_ENABLE_GC != 0 */
  8291. }
  8292. else {
  8293. uint32 type_index;
  8294. /* Resolve the leb128 encoded type index as block type */
  8295. p--;
  8296. p_org = p - 1;
  8297. read_leb_uint32(p, p_end, type_index);
  8298. if (type_index >= module->type_count) {
  8299. set_error_buf(error_buf, error_buf_size,
  8300. "unknown type");
  8301. goto fail;
  8302. }
  8303. block_type.is_value_type = false;
  8304. block_type.u.type =
  8305. (WASMFuncType *)module->types[type_index];
  8306. #if WASM_ENABLE_FAST_INTERP == 0
  8307. /* If block use type index as block type, change the opcode
  8308. * to new extended opcode so that interpreter can resolve
  8309. * the block quickly.
  8310. */
  8311. #if WASM_ENABLE_DEBUG_INTERP != 0
  8312. if (!record_fast_op(module, p_org, *p_org, error_buf,
  8313. error_buf_size)) {
  8314. goto fail;
  8315. }
  8316. #endif
  8317. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  8318. #endif
  8319. }
  8320. /* Pop block parameters from stack */
  8321. if (BLOCK_HAS_PARAM(block_type)) {
  8322. WASMFuncType *func_type = block_type.u.type;
  8323. for (i = 0; i < block_type.u.type->param_count; i++)
  8324. POP_TYPE(
  8325. func_type->types[func_type->param_count - i - 1]);
  8326. }
  8327. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  8328. block_type, p);
  8329. /* Pass parameters to block */
  8330. if (BLOCK_HAS_PARAM(block_type)) {
  8331. for (i = 0; i < block_type.u.type->param_count; i++)
  8332. PUSH_TYPE(block_type.u.type->types[i]);
  8333. }
  8334. #if WASM_ENABLE_FAST_INTERP != 0
  8335. if (opcode == WASM_OP_BLOCK) {
  8336. skip_label();
  8337. }
  8338. else if (opcode == WASM_OP_LOOP) {
  8339. skip_label();
  8340. if (BLOCK_HAS_PARAM(block_type)) {
  8341. /* Make sure params are in dynamic space */
  8342. if (!copy_params_to_dynamic_space(
  8343. loader_ctx, false, error_buf, error_buf_size))
  8344. goto fail;
  8345. }
  8346. (loader_ctx->frame_csp - 1)->code_compiled =
  8347. loader_ctx->p_code_compiled;
  8348. }
  8349. else if (opcode == WASM_OP_IF) {
  8350. /* If block has parameters, we should make sure they are in
  8351. * dynamic space. Otherwise, when else branch is missing,
  8352. * the later opcode may consume incorrect operand offset.
  8353. * Spec case:
  8354. * (func (export "params-id") (param i32) (result i32)
  8355. * (i32.const 1)
  8356. * (i32.const 2)
  8357. * (if (param i32 i32) (result i32 i32) (local.get 0)
  8358. * (then)) (i32.add)
  8359. * )
  8360. *
  8361. * So we should emit a copy instruction before the if.
  8362. *
  8363. * And we also need to save the parameter offsets and
  8364. * recover them before entering else branch.
  8365. *
  8366. */
  8367. if (BLOCK_HAS_PARAM(block_type)) {
  8368. BranchBlock *block = loader_ctx->frame_csp - 1;
  8369. uint64 size;
  8370. /* skip the if condition operand offset */
  8371. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8372. /* skip the if label */
  8373. skip_label();
  8374. /* Emit a copy instruction */
  8375. if (!copy_params_to_dynamic_space(
  8376. loader_ctx, true, error_buf, error_buf_size))
  8377. goto fail;
  8378. /* Emit the if instruction */
  8379. emit_label(opcode);
  8380. /* Emit the new condition operand offset */
  8381. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  8382. /* Save top param_count values of frame_offset stack, so
  8383. * that we can recover it before executing else branch
  8384. */
  8385. size = sizeof(int16)
  8386. * (uint64)block_type.u.type->param_cell_num;
  8387. if (!(block->param_frame_offsets = loader_malloc(
  8388. size, error_buf, error_buf_size)))
  8389. goto fail;
  8390. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  8391. loader_ctx->frame_offset
  8392. - size / sizeof(int16),
  8393. (uint32)size);
  8394. }
  8395. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  8396. emit_empty_label_addr_and_frame_ip(PATCH_END);
  8397. }
  8398. #endif
  8399. break;
  8400. }
  8401. case WASM_OP_ELSE:
  8402. {
  8403. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  8404. if (loader_ctx->csp_num < 2
  8405. || (loader_ctx->frame_csp - 1)->label_type
  8406. != LABEL_TYPE_IF) {
  8407. set_error_buf(
  8408. error_buf, error_buf_size,
  8409. "opcode else found without matched opcode if");
  8410. goto fail;
  8411. }
  8412. /* check whether if branch's stack matches its result type */
  8413. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  8414. error_buf, error_buf_size))
  8415. goto fail;
  8416. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  8417. #if WASM_ENABLE_FAST_INTERP != 0
  8418. /* if the result of if branch is in local or const area, add a
  8419. * copy op */
  8420. RESERVE_BLOCK_RET();
  8421. emit_empty_label_addr_and_frame_ip(PATCH_END);
  8422. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  8423. #endif
  8424. RESET_STACK();
  8425. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  8426. /* Pass parameters to if-false branch */
  8427. if (BLOCK_HAS_PARAM(block_type)) {
  8428. for (i = 0; i < block_type.u.type->param_count; i++)
  8429. PUSH_TYPE(block_type.u.type->types[i]);
  8430. }
  8431. #if WASM_ENABLE_FAST_INTERP != 0
  8432. /* Recover top param_count values of frame_offset stack */
  8433. if (BLOCK_HAS_PARAM((block_type))) {
  8434. uint32 size;
  8435. BranchBlock *block = loader_ctx->frame_csp - 1;
  8436. size = sizeof(int16) * block_type.u.type->param_cell_num;
  8437. bh_memcpy_s(loader_ctx->frame_offset, size,
  8438. block->param_frame_offsets, size);
  8439. loader_ctx->frame_offset += (size / sizeof(int16));
  8440. }
  8441. #endif
  8442. break;
  8443. }
  8444. case WASM_OP_END:
  8445. {
  8446. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8447. /* check whether block stack matches its result type */
  8448. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  8449. error_buf_size))
  8450. goto fail;
  8451. /* if no else branch, and return types do not match param types,
  8452. report failure */
  8453. if (cur_block->label_type == LABEL_TYPE_IF
  8454. && !cur_block->else_addr) {
  8455. uint32 block_param_count = 0, block_ret_count = 0;
  8456. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  8457. BlockType *cur_block_type = &cur_block->block_type;
  8458. #if WASM_ENABLE_GC != 0
  8459. uint32 block_param_reftype_map_count;
  8460. uint32 block_ret_reftype_map_count;
  8461. WASMRefTypeMap *block_param_reftype_maps;
  8462. WASMRefTypeMap *block_ret_reftype_maps;
  8463. #endif
  8464. block_param_count = block_type_get_param_types(
  8465. cur_block_type, &block_param_types
  8466. #if WASM_ENABLE_GC != 0
  8467. ,
  8468. &block_param_reftype_maps,
  8469. &block_param_reftype_map_count
  8470. #endif
  8471. );
  8472. block_ret_count = block_type_get_result_types(
  8473. cur_block_type, &block_ret_types
  8474. #if WASM_ENABLE_GC != 0
  8475. ,
  8476. &block_ret_reftype_maps, &block_ret_reftype_map_count
  8477. #endif
  8478. );
  8479. if (block_param_count != block_ret_count
  8480. || (block_param_count
  8481. && memcmp(block_param_types, block_ret_types,
  8482. block_param_count))) {
  8483. set_error_buf(error_buf, error_buf_size,
  8484. "type mismatch: else branch missing");
  8485. goto fail;
  8486. }
  8487. #if WASM_ENABLE_GC != 0
  8488. if (block_param_reftype_map_count
  8489. != block_ret_reftype_map_count
  8490. || (block_param_reftype_map_count
  8491. && memcmp(block_param_reftype_maps,
  8492. block_ret_reftype_maps,
  8493. sizeof(WASMRefTypeMap)
  8494. * block_param_reftype_map_count))) {
  8495. set_error_buf(error_buf, error_buf_size,
  8496. "type mismatch: else branch missing");
  8497. goto fail;
  8498. }
  8499. #endif
  8500. }
  8501. POP_CSP();
  8502. #if WASM_ENABLE_FAST_INTERP != 0
  8503. skip_label();
  8504. /* copy the result to the block return address */
  8505. RESERVE_BLOCK_RET();
  8506. apply_label_patch(loader_ctx, 0, PATCH_END);
  8507. free_label_patch_list(loader_ctx->frame_csp);
  8508. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  8509. int32 idx;
  8510. uint8 ret_type;
  8511. emit_label(WASM_OP_RETURN);
  8512. for (idx = (int32)func->func_type->result_count - 1;
  8513. idx >= 0; idx--) {
  8514. ret_type = *(func->func_type->types
  8515. + func->func_type->param_count + idx);
  8516. POP_OFFSET_TYPE(ret_type);
  8517. }
  8518. }
  8519. #endif
  8520. if (loader_ctx->csp_num > 0) {
  8521. loader_ctx->frame_csp->end_addr = p - 1;
  8522. }
  8523. else {
  8524. /* end of function block, function will return */
  8525. if (p < p_end) {
  8526. set_error_buf(error_buf, error_buf_size,
  8527. "section size mismatch");
  8528. goto fail;
  8529. }
  8530. }
  8531. break;
  8532. }
  8533. case WASM_OP_BR:
  8534. {
  8535. if (!(frame_csp_tmp =
  8536. check_branch_block(loader_ctx, &p, p_end, false,
  8537. error_buf, error_buf_size)))
  8538. goto fail;
  8539. RESET_STACK();
  8540. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8541. break;
  8542. }
  8543. case WASM_OP_BR_IF:
  8544. {
  8545. POP_I32();
  8546. if (!(frame_csp_tmp =
  8547. check_branch_block(loader_ctx, &p, p_end, false,
  8548. error_buf, error_buf_size)))
  8549. goto fail;
  8550. break;
  8551. }
  8552. case WASM_OP_BR_TABLE:
  8553. {
  8554. uint32 depth, default_arity, arity = 0;
  8555. BranchBlock *target_block;
  8556. BlockType *target_block_type;
  8557. #if WASM_ENABLE_FAST_INTERP == 0
  8558. BrTableCache *br_table_cache = NULL;
  8559. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  8560. uint32 j;
  8561. #endif
  8562. read_leb_uint32(p, p_end, count);
  8563. #if WASM_ENABLE_FAST_INTERP != 0
  8564. emit_uint32(loader_ctx, count);
  8565. #endif
  8566. POP_I32();
  8567. /* Get the default depth and check it */
  8568. p_org = p;
  8569. for (i = 0; i <= count; i++) {
  8570. read_leb_uint32(p, p_end, depth);
  8571. }
  8572. if (loader_ctx->csp_num < depth + 1) {
  8573. set_error_buf(error_buf, error_buf_size,
  8574. "unknown label, "
  8575. "unexpected end of section or function");
  8576. goto fail;
  8577. }
  8578. p = p_org;
  8579. /* Get the default block's arity */
  8580. target_block = loader_ctx->frame_csp - (depth + 1);
  8581. target_block_type = &target_block->block_type;
  8582. default_arity = block_type_get_arity(target_block_type,
  8583. target_block->label_type);
  8584. #if WASM_ENABLE_FAST_INTERP == 0
  8585. p_depth_begin = p_depth = p;
  8586. #endif
  8587. for (i = 0; i <= count; i++) {
  8588. p_org = p;
  8589. read_leb_uint32(p, p_end, depth);
  8590. if (loader_ctx->csp_num < depth + 1) {
  8591. set_error_buf(error_buf, error_buf_size,
  8592. "unknown label, "
  8593. "unexpected end of section or function");
  8594. goto fail;
  8595. }
  8596. p = p_org;
  8597. /* Get the target block's arity and check it */
  8598. target_block = loader_ctx->frame_csp - (depth + 1);
  8599. target_block_type = &target_block->block_type;
  8600. arity = block_type_get_arity(target_block_type,
  8601. target_block->label_type);
  8602. if (arity != default_arity) {
  8603. set_error_buf(error_buf, error_buf_size,
  8604. "type mismatch: br_table targets must "
  8605. "all use same result type");
  8606. goto fail;
  8607. }
  8608. if (!(frame_csp_tmp =
  8609. check_branch_block(loader_ctx, &p, p_end, true,
  8610. error_buf, error_buf_size))) {
  8611. goto fail;
  8612. }
  8613. #if WASM_ENABLE_FAST_INTERP == 0
  8614. if (br_table_cache) {
  8615. br_table_cache->br_depths[i] = depth;
  8616. }
  8617. else {
  8618. if (depth > 255) {
  8619. /* The depth cannot be stored in one byte,
  8620. create br_table cache to store each depth */
  8621. #if WASM_ENABLE_DEBUG_INTERP != 0
  8622. if (!record_fast_op(module, p_opcode, *p_opcode,
  8623. error_buf, error_buf_size)) {
  8624. goto fail;
  8625. }
  8626. #endif
  8627. if (!(br_table_cache = loader_malloc(
  8628. offsetof(BrTableCache, br_depths)
  8629. + sizeof(uint32)
  8630. * (uint64)(count + 1),
  8631. error_buf, error_buf_size))) {
  8632. goto fail;
  8633. }
  8634. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  8635. br_table_cache->br_table_op_addr = p_opcode;
  8636. br_table_cache->br_count = count;
  8637. /* Copy previous depths which are one byte */
  8638. for (j = 0; j < i; j++) {
  8639. br_table_cache->br_depths[j] = p_depth_begin[j];
  8640. }
  8641. br_table_cache->br_depths[i] = depth;
  8642. bh_list_insert(module->br_table_cache_list,
  8643. br_table_cache);
  8644. }
  8645. else {
  8646. /* The depth can be stored in one byte, use the
  8647. byte of the leb to store it */
  8648. *p_depth++ = (uint8)depth;
  8649. }
  8650. }
  8651. #endif
  8652. }
  8653. #if WASM_ENABLE_FAST_INTERP == 0
  8654. /* Set the tailing bytes to nop */
  8655. if (br_table_cache)
  8656. p_depth = p_depth_begin;
  8657. while (p_depth < p)
  8658. *p_depth++ = WASM_OP_NOP;
  8659. #endif
  8660. RESET_STACK();
  8661. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8662. break;
  8663. }
  8664. case WASM_OP_RETURN:
  8665. {
  8666. int32 idx;
  8667. uint8 ret_type;
  8668. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  8669. idx--) {
  8670. ret_type = *(func->func_type->types
  8671. + func->func_type->param_count + idx);
  8672. POP_TYPE(ret_type);
  8673. #if WASM_ENABLE_FAST_INTERP != 0
  8674. /* emit the offset after return opcode */
  8675. POP_OFFSET_TYPE(ret_type);
  8676. #endif
  8677. }
  8678. RESET_STACK();
  8679. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8680. break;
  8681. }
  8682. case WASM_OP_CALL:
  8683. #if WASM_ENABLE_TAIL_CALL != 0
  8684. case WASM_OP_RETURN_CALL:
  8685. #endif
  8686. #if WASM_ENABLE_GC != 0
  8687. case WASM_OP_CALL_REF:
  8688. case WASM_OP_RETURN_CALL_REF:
  8689. #endif
  8690. {
  8691. WASMFuncType *func_type;
  8692. uint8 type;
  8693. int32 idx;
  8694. #if WASM_ENABLE_GC != 0
  8695. WASMRefType *ref_type;
  8696. uint32 type_idx1;
  8697. int32 j;
  8698. #endif
  8699. #if WASM_ENABLE_GC != 0
  8700. if (opcode == WASM_OP_CALL_REF
  8701. || opcode == WASM_OP_RETURN_CALL_REF) {
  8702. read_leb_uint32(p, p_end, type_idx1);
  8703. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  8704. &type_idx, error_buf,
  8705. error_buf_size)) {
  8706. goto fail;
  8707. }
  8708. if (type == VALUE_TYPE_ANY) {
  8709. type_idx = type_idx1;
  8710. }
  8711. if (!check_type_index(module, type_idx, error_buf,
  8712. error_buf_size)) {
  8713. goto fail;
  8714. }
  8715. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  8716. set_error_buf(error_buf, error_buf_size,
  8717. "unkown function type");
  8718. goto fail;
  8719. }
  8720. if (type_idx != type_idx1) {
  8721. set_error_buf(error_buf, error_buf_size,
  8722. "function type mismatch");
  8723. goto fail;
  8724. }
  8725. func_type = (WASMFuncType *)module->types[type_idx];
  8726. }
  8727. else
  8728. #endif
  8729. {
  8730. read_leb_uint32(p, p_end, func_idx);
  8731. #if WASM_ENABLE_FAST_INTERP != 0
  8732. /* we need to emit func_idx before arguments */
  8733. emit_uint32(loader_ctx, func_idx);
  8734. #endif
  8735. if (!check_function_index(module, func_idx, error_buf,
  8736. error_buf_size)) {
  8737. goto fail;
  8738. }
  8739. if (func_idx < module->import_function_count)
  8740. func_type = module->import_functions[func_idx]
  8741. .u.function.func_type;
  8742. else
  8743. func_type =
  8744. module
  8745. ->functions[func_idx
  8746. - module->import_function_count]
  8747. ->func_type;
  8748. }
  8749. if (func_type->param_count > 0) {
  8750. #if WASM_ENABLE_GC != 0
  8751. j = (int32)(func_type->result_ref_type_maps
  8752. - func_type->ref_type_maps - 1);
  8753. #endif
  8754. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  8755. idx--) {
  8756. #if WASM_ENABLE_GC != 0
  8757. if (wasm_is_type_multi_byte_type(
  8758. func_type->types[idx])) {
  8759. ref_type = func_type->ref_type_maps[j].ref_type;
  8760. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  8761. ref_type,
  8762. wasm_reftype_struct_size(ref_type));
  8763. j--;
  8764. }
  8765. #endif
  8766. POP_TYPE(func_type->types[idx]);
  8767. #if WASM_ENABLE_FAST_INTERP != 0
  8768. POP_OFFSET_TYPE(func_type->types[idx]);
  8769. #endif
  8770. }
  8771. }
  8772. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  8773. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  8774. #endif
  8775. #if WASM_ENABLE_GC != 0
  8776. j = (int32)(func_type->result_ref_type_maps
  8777. - func_type->ref_type_maps);
  8778. #endif
  8779. for (i = 0; i < func_type->result_count; i++) {
  8780. #if WASM_ENABLE_GC != 0
  8781. if (wasm_is_type_multi_byte_type(
  8782. func_type->types[func_type->param_count + i])) {
  8783. ref_type = func_type->ref_type_maps[j].ref_type;
  8784. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  8785. ref_type,
  8786. wasm_reftype_struct_size(ref_type));
  8787. j++;
  8788. }
  8789. #endif
  8790. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  8791. #if WASM_ENABLE_FAST_INTERP != 0
  8792. /* Here we emit each return value's dynamic_offset. But
  8793. * in fact these offsets are continuous, so interpreter
  8794. * only need to get the first return value's offset.
  8795. */
  8796. PUSH_OFFSET_TYPE(
  8797. func_type->types[func_type->param_count + i]);
  8798. #endif
  8799. }
  8800. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  8801. }
  8802. else {
  8803. #if WASM_ENABLE_GC == 0
  8804. if (func_type->result_count
  8805. != func->func_type->result_count) {
  8806. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  8807. "type mismatch: expect ",
  8808. func->func_type->result_count,
  8809. " return values but got other");
  8810. goto fail;
  8811. }
  8812. for (i = 0; i < func_type->result_count; i++) {
  8813. type = func->func_type
  8814. ->types[func->func_type->param_count + i];
  8815. if (func_type->types[func_type->param_count + i]
  8816. != type) {
  8817. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8818. "type mismatch: expect ",
  8819. type2str(type), " but got other");
  8820. goto fail;
  8821. }
  8822. }
  8823. #else
  8824. if (!wasm_func_type_result_is_subtype_of(
  8825. func_type, func->func_type, module->types,
  8826. module->type_count)) {
  8827. set_error_buf(
  8828. error_buf, error_buf_size,
  8829. "type mismatch: invalid func result types");
  8830. goto fail;
  8831. }
  8832. #endif
  8833. RESET_STACK();
  8834. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8835. }
  8836. #endif
  8837. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  8838. || WASM_ENABLE_WAMR_COMPILER != 0
  8839. func->has_op_func_call = true;
  8840. #endif
  8841. (void)type;
  8842. break;
  8843. }
  8844. /*
  8845. * if disable reference type: call_indirect typeidx, 0x00
  8846. * if enable reference type: call_indirect typeidx, tableidx
  8847. */
  8848. case WASM_OP_CALL_INDIRECT:
  8849. #if WASM_ENABLE_TAIL_CALL != 0
  8850. case WASM_OP_RETURN_CALL_INDIRECT:
  8851. #endif
  8852. {
  8853. int32 idx;
  8854. WASMFuncType *func_type;
  8855. read_leb_uint32(p, p_end, type_idx);
  8856. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  8857. read_leb_uint32(p, p_end, table_idx);
  8858. #else
  8859. CHECK_BUF(p, p_end, 1);
  8860. table_idx = read_uint8(p);
  8861. #endif
  8862. if (!check_table_index(module, table_idx, error_buf,
  8863. error_buf_size)) {
  8864. goto fail;
  8865. }
  8866. #if WASM_ENABLE_FAST_INTERP != 0
  8867. /* we need to emit before arguments */
  8868. #if WASM_ENABLE_TAIL_CALL != 0
  8869. emit_byte(loader_ctx, opcode);
  8870. #endif
  8871. emit_uint32(loader_ctx, type_idx);
  8872. emit_uint32(loader_ctx, table_idx);
  8873. #endif
  8874. /* skip elem idx */
  8875. POP_I32();
  8876. if (type_idx >= module->type_count) {
  8877. set_error_buf(error_buf, error_buf_size, "unknown type");
  8878. goto fail;
  8879. }
  8880. func_type = (WASMFuncType *)module->types[type_idx];
  8881. if (func_type->param_count > 0) {
  8882. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  8883. idx--) {
  8884. POP_TYPE(func_type->types[idx]);
  8885. #if WASM_ENABLE_FAST_INTERP != 0
  8886. POP_OFFSET_TYPE(func_type->types[idx]);
  8887. #endif
  8888. }
  8889. }
  8890. #if WASM_ENABLE_TAIL_CALL != 0
  8891. if (opcode == WASM_OP_CALL_INDIRECT) {
  8892. #endif
  8893. for (i = 0; i < func_type->result_count; i++) {
  8894. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  8895. #if WASM_ENABLE_FAST_INTERP != 0
  8896. PUSH_OFFSET_TYPE(
  8897. func_type->types[func_type->param_count + i]);
  8898. #endif
  8899. }
  8900. #if WASM_ENABLE_TAIL_CALL != 0
  8901. }
  8902. else {
  8903. uint8 type;
  8904. if (func_type->result_count
  8905. != func->func_type->result_count) {
  8906. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  8907. "type mismatch: expect ",
  8908. func->func_type->result_count,
  8909. " return values but got other");
  8910. goto fail;
  8911. }
  8912. for (i = 0; i < func_type->result_count; i++) {
  8913. type = func->func_type
  8914. ->types[func->func_type->param_count + i];
  8915. if (func_type->types[func_type->param_count + i]
  8916. != type) {
  8917. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8918. "type mismatch: expect ",
  8919. type2str(type), " but got other");
  8920. goto fail;
  8921. }
  8922. }
  8923. RESET_STACK();
  8924. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8925. }
  8926. #endif
  8927. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  8928. || WASM_ENABLE_WAMR_COMPILER != 0
  8929. func->has_op_func_call = true;
  8930. #endif
  8931. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8932. func->has_op_call_indirect = true;
  8933. #endif
  8934. break;
  8935. }
  8936. case WASM_OP_DROP:
  8937. {
  8938. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8939. int32 available_stack_cell =
  8940. (int32)(loader_ctx->stack_cell_num
  8941. - cur_block->stack_cell_num);
  8942. if (available_stack_cell <= 0
  8943. && !cur_block->is_stack_polymorphic) {
  8944. set_error_buf(error_buf, error_buf_size,
  8945. "type mismatch, opcode drop was found "
  8946. "but stack was empty");
  8947. goto fail;
  8948. }
  8949. if (available_stack_cell > 0) {
  8950. #if WASM_ENABLE_GC != 0
  8951. if (wasm_is_type_multi_byte_type(
  8952. *(loader_ctx->frame_ref - 1))) {
  8953. bh_assert((int32)(loader_ctx->reftype_map_num
  8954. - cur_block->reftype_map_num)
  8955. > 0);
  8956. loader_ctx->frame_reftype_map--;
  8957. loader_ctx->reftype_map_num--;
  8958. }
  8959. #endif
  8960. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  8961. loader_ctx->frame_ref--;
  8962. loader_ctx->stack_cell_num--;
  8963. #if WASM_ENABLE_FAST_INTERP != 0
  8964. skip_label();
  8965. loader_ctx->frame_offset--;
  8966. if ((*(loader_ctx->frame_offset)
  8967. > loader_ctx->start_dynamic_offset)
  8968. && (*(loader_ctx->frame_offset)
  8969. < loader_ctx->max_dynamic_offset))
  8970. loader_ctx->dynamic_offset--;
  8971. #endif
  8972. }
  8973. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  8974. loader_ctx->frame_ref -= 2;
  8975. loader_ctx->stack_cell_num -= 2;
  8976. #if WASM_ENABLE_FAST_INTERP == 0
  8977. *(p - 1) = WASM_OP_DROP_64;
  8978. #endif
  8979. #if WASM_ENABLE_FAST_INTERP != 0
  8980. skip_label();
  8981. loader_ctx->frame_offset -= 2;
  8982. if ((*(loader_ctx->frame_offset)
  8983. > loader_ctx->start_dynamic_offset)
  8984. && (*(loader_ctx->frame_offset)
  8985. < loader_ctx->max_dynamic_offset))
  8986. loader_ctx->dynamic_offset -= 2;
  8987. #endif
  8988. }
  8989. #if WASM_ENABLE_SIMD != 0
  8990. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8991. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  8992. loader_ctx->frame_ref -= 4;
  8993. loader_ctx->stack_cell_num -= 4;
  8994. }
  8995. #endif
  8996. #endif
  8997. else {
  8998. set_error_buf(error_buf, error_buf_size,
  8999. "type mismatch");
  9000. goto fail;
  9001. }
  9002. }
  9003. else {
  9004. #if WASM_ENABLE_FAST_INTERP != 0
  9005. skip_label();
  9006. #endif
  9007. }
  9008. break;
  9009. }
  9010. case WASM_OP_SELECT:
  9011. {
  9012. uint8 ref_type;
  9013. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9014. int32 available_stack_cell;
  9015. POP_I32();
  9016. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  9017. - cur_block->stack_cell_num);
  9018. if (available_stack_cell <= 0
  9019. && !cur_block->is_stack_polymorphic) {
  9020. set_error_buf(error_buf, error_buf_size,
  9021. "type mismatch or invalid result arity, "
  9022. "opcode select was found "
  9023. "but stack was empty");
  9024. goto fail;
  9025. }
  9026. if (available_stack_cell > 0) {
  9027. switch (*(loader_ctx->frame_ref - 1)) {
  9028. case VALUE_TYPE_I32:
  9029. case VALUE_TYPE_F32:
  9030. break;
  9031. case VALUE_TYPE_I64:
  9032. case VALUE_TYPE_F64:
  9033. #if WASM_ENABLE_FAST_INTERP == 0
  9034. *(p - 1) = WASM_OP_SELECT_64;
  9035. #endif
  9036. #if WASM_ENABLE_FAST_INTERP != 0
  9037. if (loader_ctx->p_code_compiled) {
  9038. uint8 opcode_tmp = WASM_OP_SELECT_64;
  9039. uint8 *p_code_compiled_tmp =
  9040. loader_ctx->p_code_compiled - 2;
  9041. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  9042. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9043. *(void **)(p_code_compiled_tmp
  9044. - sizeof(void *)) =
  9045. handle_table[opcode_tmp];
  9046. #else
  9047. int32 offset =
  9048. (int32)((uint8 *)handle_table[opcode_tmp]
  9049. - (uint8 *)handle_table[0]);
  9050. if (!(offset >= INT16_MIN
  9051. && offset < INT16_MAX)) {
  9052. set_error_buf(error_buf, error_buf_size,
  9053. "pre-compiled label offset "
  9054. "out of range");
  9055. goto fail;
  9056. }
  9057. *(int16 *)(p_code_compiled_tmp
  9058. - sizeof(int16)) = (int16)offset;
  9059. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9060. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  9061. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9062. *(p_code_compiled_tmp - 1) = opcode_tmp;
  9063. #else
  9064. *(p_code_compiled_tmp - 2) = opcode_tmp;
  9065. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9066. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  9067. }
  9068. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9069. break;
  9070. #if WASM_ENABLE_SIMD != 0
  9071. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  9072. case VALUE_TYPE_V128:
  9073. break;
  9074. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9075. #endif /* WASM_ENABLE_SIMD != 0 */
  9076. default:
  9077. {
  9078. set_error_buf(error_buf, error_buf_size,
  9079. "type mismatch");
  9080. goto fail;
  9081. }
  9082. }
  9083. ref_type = *(loader_ctx->frame_ref - 1);
  9084. #if WASM_ENABLE_FAST_INTERP != 0
  9085. POP_OFFSET_TYPE(ref_type);
  9086. POP_TYPE(ref_type);
  9087. POP_OFFSET_TYPE(ref_type);
  9088. POP_TYPE(ref_type);
  9089. PUSH_OFFSET_TYPE(ref_type);
  9090. PUSH_TYPE(ref_type);
  9091. #else
  9092. POP2_AND_PUSH(ref_type, ref_type);
  9093. #endif
  9094. }
  9095. else {
  9096. #if WASM_ENABLE_FAST_INTERP != 0
  9097. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  9098. #endif
  9099. PUSH_TYPE(VALUE_TYPE_ANY);
  9100. }
  9101. break;
  9102. }
  9103. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9104. case WASM_OP_SELECT_T:
  9105. {
  9106. uint8 vec_len, type;
  9107. #if WASM_ENABLE_GC != 0
  9108. WASMRefType *ref_type = NULL;
  9109. #endif
  9110. read_leb_uint32(p, p_end, vec_len);
  9111. if (vec_len != 1) {
  9112. /* typed select must have exactly one result */
  9113. set_error_buf(error_buf, error_buf_size,
  9114. "invalid result arity");
  9115. goto fail;
  9116. }
  9117. #if WASM_ENABLE_GC == 0
  9118. CHECK_BUF(p, p_end, 1);
  9119. type = read_uint8(p);
  9120. if (!is_value_type(type)) {
  9121. set_error_buf(error_buf, error_buf_size,
  9122. "unknown value type");
  9123. goto fail;
  9124. }
  9125. #else
  9126. p_org = p + 1;
  9127. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  9128. &need_ref_type_map, &wasm_ref_type,
  9129. false, error_buf, error_buf_size)) {
  9130. goto fail;
  9131. }
  9132. type = wasm_ref_type.ref_type;
  9133. if (need_ref_type_map) {
  9134. if (!(ref_type = reftype_set_insert(
  9135. module->ref_type_set, &wasm_ref_type, error_buf,
  9136. error_buf_size))) {
  9137. goto fail;
  9138. }
  9139. }
  9140. #if WASM_ENABLE_FAST_INTERP == 0
  9141. while (p_org < p) {
  9142. #if WASM_ENABLE_DEBUG_INTERP != 0
  9143. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9144. error_buf_size)) {
  9145. goto fail;
  9146. }
  9147. #endif
  9148. /* Ignore extra bytes for interpreter */
  9149. *p_org++ = WASM_OP_NOP;
  9150. }
  9151. #endif
  9152. #endif /* end of WASM_ENABLE_GC == 0 */
  9153. POP_I32();
  9154. #if WASM_ENABLE_FAST_INTERP != 0
  9155. if (loader_ctx->p_code_compiled) {
  9156. uint8 opcode_tmp = WASM_OP_SELECT;
  9157. uint8 *p_code_compiled_tmp =
  9158. loader_ctx->p_code_compiled - 2;
  9159. if (type == VALUE_TYPE_V128) {
  9160. #if (WASM_ENABLE_SIMD == 0) \
  9161. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  9162. set_error_buf(error_buf, error_buf_size,
  9163. "SIMD v128 type isn't supported");
  9164. goto fail;
  9165. #endif
  9166. }
  9167. else {
  9168. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  9169. opcode_tmp = WASM_OP_SELECT_64;
  9170. #if WASM_ENABLE_GC != 0
  9171. if (wasm_is_type_reftype(type))
  9172. opcode_tmp = WASM_OP_SELECT_T;
  9173. #endif
  9174. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  9175. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9176. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  9177. handle_table[opcode_tmp];
  9178. #else
  9179. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  9180. - (uint8 *)handle_table[0]);
  9181. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  9182. set_error_buf(
  9183. error_buf, error_buf_size,
  9184. "pre-compiled label offset out of range");
  9185. goto fail;
  9186. }
  9187. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  9188. (int16)offset;
  9189. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9190. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  9191. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9192. *(p_code_compiled_tmp - 1) = opcode_tmp;
  9193. #else
  9194. *(p_code_compiled_tmp - 2) = opcode_tmp;
  9195. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9196. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  9197. }
  9198. }
  9199. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  9200. POP_REF(type);
  9201. #if WASM_ENABLE_GC != 0
  9202. if (need_ref_type_map) {
  9203. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9204. wasm_reftype_struct_size(ref_type));
  9205. }
  9206. #endif
  9207. POP_REF(type);
  9208. #if WASM_ENABLE_GC != 0
  9209. if (need_ref_type_map) {
  9210. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9211. wasm_reftype_struct_size(ref_type));
  9212. }
  9213. #endif
  9214. PUSH_REF(type);
  9215. (void)vec_len;
  9216. break;
  9217. }
  9218. /* table.get x. tables[x]. [i32] -> [t] */
  9219. /* table.set x. tables[x]. [i32 t] -> [] */
  9220. case WASM_OP_TABLE_GET:
  9221. case WASM_OP_TABLE_SET:
  9222. {
  9223. uint8 decl_ref_type;
  9224. #if WASM_ENABLE_GC != 0
  9225. WASMRefType *ref_type;
  9226. #endif
  9227. read_leb_uint32(p, p_end, table_idx);
  9228. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  9229. #if WASM_ENABLE_GC != 0
  9230. (void **)&ref_type,
  9231. #else
  9232. NULL,
  9233. #endif
  9234. error_buf, error_buf_size))
  9235. goto fail;
  9236. #if WASM_ENABLE_GC != 0
  9237. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  9238. bh_assert(ref_type);
  9239. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  9240. ref_type, wasm_reftype_struct_size(ref_type));
  9241. }
  9242. #endif
  9243. #if WASM_ENABLE_FAST_INTERP != 0
  9244. emit_uint32(loader_ctx, table_idx);
  9245. #endif
  9246. if (opcode == WASM_OP_TABLE_GET) {
  9247. POP_I32();
  9248. #if WASM_ENABLE_FAST_INTERP != 0
  9249. PUSH_OFFSET_TYPE(decl_ref_type);
  9250. #endif
  9251. PUSH_TYPE(decl_ref_type);
  9252. }
  9253. else {
  9254. #if WASM_ENABLE_FAST_INTERP != 0
  9255. POP_OFFSET_TYPE(decl_ref_type);
  9256. #endif
  9257. POP_TYPE(decl_ref_type);
  9258. POP_I32();
  9259. }
  9260. break;
  9261. }
  9262. case WASM_OP_REF_NULL:
  9263. {
  9264. uint8 ref_type;
  9265. CHECK_BUF(p, p_end, 1);
  9266. ref_type = read_uint8(p);
  9267. #if WASM_ENABLE_GC == 0
  9268. if (ref_type != VALUE_TYPE_FUNCREF
  9269. && ref_type != VALUE_TYPE_EXTERNREF) {
  9270. set_error_buf(error_buf, error_buf_size, "type mismatch");
  9271. goto fail;
  9272. }
  9273. #else
  9274. p--;
  9275. if (is_byte_a_type(ref_type)) {
  9276. p_org = p + 1;
  9277. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  9278. &need_ref_type_map, &wasm_ref_type,
  9279. false, error_buf, error_buf_size)) {
  9280. goto fail;
  9281. }
  9282. ref_type = wasm_ref_type.ref_type;
  9283. #if WASM_ENABLE_FAST_INTERP == 0
  9284. while (p_org < p) {
  9285. #if WASM_ENABLE_DEBUG_INTERP != 0
  9286. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9287. error_buf_size)) {
  9288. goto fail;
  9289. }
  9290. #endif
  9291. /* Ignore extra bytes for interpreter */
  9292. *p_org++ = WASM_OP_NOP;
  9293. }
  9294. #endif
  9295. }
  9296. else {
  9297. read_leb_uint32(p, p_end, type_idx);
  9298. if (!check_type_index(module, type_idx, error_buf,
  9299. error_buf_size)) {
  9300. goto fail;
  9301. }
  9302. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  9303. true, type_idx);
  9304. ref_type = wasm_ref_type.ref_type;
  9305. }
  9306. #endif /* end of WASM_ENABLE_GC == 0 */
  9307. #if WASM_ENABLE_FAST_INTERP != 0
  9308. PUSH_OFFSET_TYPE(ref_type);
  9309. #endif
  9310. PUSH_TYPE(ref_type);
  9311. break;
  9312. }
  9313. case WASM_OP_REF_IS_NULL:
  9314. {
  9315. #if WASM_ENABLE_GC == 0
  9316. #if WASM_ENABLE_FAST_INTERP != 0
  9317. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  9318. VALUE_TYPE_FUNCREF,
  9319. error_buf, error_buf_size)
  9320. && !wasm_loader_pop_frame_ref_offset(
  9321. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  9322. error_buf_size)) {
  9323. goto fail;
  9324. }
  9325. #else
  9326. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  9327. error_buf, error_buf_size)
  9328. && !wasm_loader_pop_frame_ref(loader_ctx,
  9329. VALUE_TYPE_EXTERNREF,
  9330. error_buf, error_buf_size)) {
  9331. goto fail;
  9332. }
  9333. #endif
  9334. #else /* else of WASM_ENABLE_GC == 0 */
  9335. uint8 type;
  9336. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  9337. error_buf, error_buf_size)) {
  9338. goto fail;
  9339. }
  9340. #endif
  9341. PUSH_I32();
  9342. break;
  9343. }
  9344. case WASM_OP_REF_FUNC:
  9345. {
  9346. read_leb_uint32(p, p_end, func_idx);
  9347. if (!check_function_index(module, func_idx, error_buf,
  9348. error_buf_size)) {
  9349. goto fail;
  9350. }
  9351. /* Refer to a forward-declared function */
  9352. if (func_idx >= cur_func_idx + module->import_function_count) {
  9353. WASMTableSeg *table_seg = module->table_segments;
  9354. bool func_declared = false;
  9355. uint32 j;
  9356. /* Check whether the function is declared in table segs */
  9357. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  9358. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  9359. && wasm_elem_is_declarative(table_seg->mode)) {
  9360. for (j = 0; j < table_seg->function_count; j++) {
  9361. if (table_seg->func_indexes[j] == func_idx) {
  9362. func_declared = true;
  9363. break;
  9364. }
  9365. }
  9366. }
  9367. }
  9368. if (!func_declared) {
  9369. /* Check whether the function is exported */
  9370. for (i = 0; i < module->export_count; i++) {
  9371. if (module->exports[i].kind == EXPORT_KIND_FUNC
  9372. && module->exports[i].index == func_idx) {
  9373. func_declared = true;
  9374. break;
  9375. }
  9376. }
  9377. }
  9378. if (!func_declared) {
  9379. set_error_buf(error_buf, error_buf_size,
  9380. "undeclared function reference");
  9381. goto fail;
  9382. }
  9383. }
  9384. #if WASM_ENABLE_FAST_INTERP != 0
  9385. emit_uint32(loader_ctx, func_idx);
  9386. #endif
  9387. #if WASM_ENABLE_GC == 0
  9388. PUSH_FUNCREF();
  9389. #else
  9390. if (func_idx < module->import_function_count)
  9391. type_idx =
  9392. module->import_functions[func_idx].u.function.type_idx;
  9393. else
  9394. type_idx = module
  9395. ->functions[func_idx
  9396. - module->import_function_count]
  9397. ->type_idx;
  9398. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  9399. false, type_idx);
  9400. PUSH_REF(wasm_ref_type.ref_type);
  9401. #endif
  9402. break;
  9403. }
  9404. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  9405. #if WASM_ENABLE_GC != 0
  9406. case WASM_OP_REF_AS_NON_NULL:
  9407. case WASM_OP_BR_ON_NULL:
  9408. {
  9409. uint8 type;
  9410. WASMRefType ref_type;
  9411. /* POP (ref null ht) and get the converted (ref ht) */
  9412. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  9413. error_buf, error_buf_size)) {
  9414. goto fail;
  9415. }
  9416. if (opcode == WASM_OP_BR_ON_NULL) {
  9417. if (!(frame_csp_tmp =
  9418. check_branch_block(loader_ctx, &p, p_end, false,
  9419. error_buf, error_buf_size))) {
  9420. goto fail;
  9421. }
  9422. }
  9423. #if WASM_ENABLE_FAST_INTERP != 0
  9424. disable_emit = true;
  9425. #endif
  9426. /* PUSH the converted (ref ht) */
  9427. if (type != VALUE_TYPE_ANY) {
  9428. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  9429. sizeof(WASMRefType));
  9430. }
  9431. PUSH_REF(type);
  9432. break;
  9433. }
  9434. case WASM_OP_BR_ON_NON_NULL:
  9435. {
  9436. uint8 type;
  9437. WASMRefType ref_type;
  9438. uint32 available_stack_cell =
  9439. loader_ctx->stack_cell_num
  9440. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  9441. /* POP (ref null ht) and get the converted (ref ht) */
  9442. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  9443. error_buf, error_buf_size)) {
  9444. goto fail;
  9445. }
  9446. #if WASM_ENABLE_FAST_INTERP != 0
  9447. disable_emit = true;
  9448. #endif
  9449. /* Temporarily PUSH back (ref ht), check brach block and
  9450. then POP it */
  9451. if (available_stack_cell
  9452. > 0) { /* stack isn't in polymorphic state */
  9453. if (type != VALUE_TYPE_ANY) {
  9454. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  9455. &ref_type, sizeof(WASMRefType));
  9456. }
  9457. PUSH_REF(type);
  9458. }
  9459. if (!(frame_csp_tmp =
  9460. check_branch_block(loader_ctx, &p, p_end, false,
  9461. error_buf, error_buf_size))) {
  9462. goto fail;
  9463. }
  9464. if (available_stack_cell
  9465. > 0) { /* stack isn't in polymorphic state */
  9466. POP_REF(type);
  9467. #if WASM_ENABLE_FAST_INTERP != 0
  9468. /* Erase the opnd offset emitted by POP_REF() */
  9469. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  9470. #endif
  9471. }
  9472. break;
  9473. }
  9474. case WASM_OP_REF_EQ:
  9475. POP_REF(REF_TYPE_EQREF);
  9476. POP_REF(REF_TYPE_EQREF);
  9477. PUSH_I32();
  9478. break;
  9479. #endif /* end of WASM_ENABLE_GC != 0 */
  9480. case WASM_OP_GET_LOCAL:
  9481. {
  9482. p_org = p - 1;
  9483. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9484. PUSH_TYPE(local_type);
  9485. #if WASM_ENABLE_GC != 0
  9486. /* Cannot get a non-nullable and unset local */
  9487. if (local_idx >= param_count
  9488. && wasm_is_reftype_htref_non_nullable(local_type)) {
  9489. set_error_buf(error_buf, error_buf_size,
  9490. "uninitialized local");
  9491. return false;
  9492. }
  9493. #endif
  9494. #if WASM_ENABLE_FAST_INTERP != 0
  9495. /* Get Local is optimized out */
  9496. skip_label();
  9497. disable_emit = true;
  9498. operand_offset = local_offset;
  9499. PUSH_OFFSET_TYPE(local_type);
  9500. #else
  9501. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9502. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9503. if (local_offset < 0x80
  9504. #if WASM_ENABLE_GC != 0
  9505. && !wasm_is_type_reftype(local_type)
  9506. #endif
  9507. ) {
  9508. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  9509. if (is_32bit_type(local_type)) {
  9510. *p_org++ = (uint8)local_offset;
  9511. }
  9512. else {
  9513. *p_org++ = (uint8)(local_offset | 0x80);
  9514. }
  9515. while (p_org < p) {
  9516. *p_org++ = WASM_OP_NOP;
  9517. }
  9518. }
  9519. #endif
  9520. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9521. break;
  9522. }
  9523. case WASM_OP_SET_LOCAL:
  9524. {
  9525. p_org = p - 1;
  9526. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9527. POP_TYPE(local_type);
  9528. #if WASM_ENABLE_FAST_INTERP != 0
  9529. if (!(preserve_referenced_local(
  9530. loader_ctx, opcode, local_offset, local_type,
  9531. &preserve_local, error_buf, error_buf_size)))
  9532. goto fail;
  9533. if (local_offset < 256
  9534. #if WASM_ENABLE_GC != 0
  9535. && !wasm_is_type_reftype(local_type)
  9536. #endif
  9537. ) {
  9538. skip_label();
  9539. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  9540. if (loader_ctx->p_code_compiled)
  9541. STORE_U16(loader_ctx->p_code_compiled - 2,
  9542. local_offset);
  9543. loader_ctx->frame_offset--;
  9544. loader_ctx->dynamic_offset--;
  9545. }
  9546. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  9547. if (loader_ctx->p_code_compiled)
  9548. STORE_U16(loader_ctx->p_code_compiled - 2,
  9549. local_offset);
  9550. loader_ctx->frame_offset -= 2;
  9551. loader_ctx->dynamic_offset -= 2;
  9552. }
  9553. else {
  9554. if (is_32bit_type(local_type)) {
  9555. emit_label(EXT_OP_SET_LOCAL_FAST);
  9556. emit_byte(loader_ctx, (uint8)local_offset);
  9557. }
  9558. else {
  9559. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  9560. emit_byte(loader_ctx, (uint8)local_offset);
  9561. }
  9562. POP_OFFSET_TYPE(local_type);
  9563. }
  9564. }
  9565. else { /* local index larger than 255, reserve leb */
  9566. emit_uint32(loader_ctx, local_idx);
  9567. POP_OFFSET_TYPE(local_type);
  9568. }
  9569. #else
  9570. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9571. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9572. if (local_offset < 0x80
  9573. #if WASM_ENABLE_GC != 0
  9574. && !wasm_is_type_reftype(local_type)
  9575. #endif
  9576. ) {
  9577. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  9578. if (is_32bit_type(local_type)) {
  9579. *p_org++ = (uint8)local_offset;
  9580. }
  9581. else {
  9582. *p_org++ = (uint8)(local_offset | 0x80);
  9583. }
  9584. while (p_org < p) {
  9585. *p_org++ = WASM_OP_NOP;
  9586. }
  9587. }
  9588. #endif
  9589. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9590. break;
  9591. }
  9592. case WASM_OP_TEE_LOCAL:
  9593. {
  9594. p_org = p - 1;
  9595. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9596. #if WASM_ENABLE_FAST_INTERP != 0
  9597. /* If the stack is in polymorphic state, do fake pop and push on
  9598. offset stack to keep the depth of offset stack to be the
  9599. same with ref stack */
  9600. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9601. if (cur_block->is_stack_polymorphic) {
  9602. POP_OFFSET_TYPE(local_type);
  9603. PUSH_OFFSET_TYPE(local_type);
  9604. }
  9605. #endif
  9606. POP_TYPE(local_type);
  9607. PUSH_TYPE(local_type);
  9608. #if WASM_ENABLE_FAST_INTERP != 0
  9609. if (!(preserve_referenced_local(
  9610. loader_ctx, opcode, local_offset, local_type,
  9611. &preserve_local, error_buf, error_buf_size)))
  9612. goto fail;
  9613. if (local_offset < 256
  9614. #if WASM_ENABLE_GC != 0
  9615. && !wasm_is_type_reftype(local_type)
  9616. #endif
  9617. ) {
  9618. skip_label();
  9619. if (is_32bit_type(local_type)) {
  9620. emit_label(EXT_OP_TEE_LOCAL_FAST);
  9621. emit_byte(loader_ctx, (uint8)local_offset);
  9622. }
  9623. else {
  9624. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  9625. emit_byte(loader_ctx, (uint8)local_offset);
  9626. }
  9627. }
  9628. else { /* local index larger than 255, reserve leb */
  9629. emit_uint32(loader_ctx, local_idx);
  9630. }
  9631. emit_operand(loader_ctx,
  9632. *(loader_ctx->frame_offset
  9633. - wasm_value_type_cell_num(local_type)));
  9634. #else
  9635. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9636. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9637. if (local_offset < 0x80
  9638. #if WASM_ENABLE_GC != 0
  9639. && !wasm_is_type_reftype(local_type)
  9640. #endif
  9641. ) {
  9642. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  9643. if (is_32bit_type(local_type)) {
  9644. *p_org++ = (uint8)local_offset;
  9645. }
  9646. else {
  9647. *p_org++ = (uint8)(local_offset | 0x80);
  9648. }
  9649. while (p_org < p) {
  9650. *p_org++ = WASM_OP_NOP;
  9651. }
  9652. }
  9653. #endif
  9654. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9655. break;
  9656. }
  9657. case WASM_OP_GET_GLOBAL:
  9658. {
  9659. #if WASM_ENABLE_GC != 0
  9660. WASMRefType *ref_type;
  9661. #endif
  9662. p_org = p - 1;
  9663. read_leb_uint32(p, p_end, global_idx);
  9664. if (global_idx >= global_count) {
  9665. set_error_buf(error_buf, error_buf_size, "unknown global");
  9666. goto fail;
  9667. }
  9668. global_type =
  9669. global_idx < module->import_global_count
  9670. ? module->import_globals[global_idx].u.global.type
  9671. : module
  9672. ->globals[global_idx
  9673. - module->import_global_count]
  9674. .type;
  9675. #if WASM_ENABLE_GC != 0
  9676. ref_type =
  9677. global_idx < module->import_global_count
  9678. ? module->import_globals[global_idx].u.global.ref_type
  9679. : module
  9680. ->globals[global_idx
  9681. - module->import_global_count]
  9682. .ref_type;
  9683. if (wasm_is_type_multi_byte_type(global_type)) {
  9684. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9685. wasm_reftype_struct_size(ref_type));
  9686. }
  9687. #endif
  9688. PUSH_TYPE(global_type);
  9689. #if WASM_ENABLE_FAST_INTERP == 0
  9690. if (global_type == VALUE_TYPE_I64
  9691. || global_type == VALUE_TYPE_F64) {
  9692. #if WASM_ENABLE_DEBUG_INTERP != 0
  9693. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9694. error_buf_size)) {
  9695. goto fail;
  9696. }
  9697. #endif
  9698. *p_org = WASM_OP_GET_GLOBAL_64;
  9699. }
  9700. #else /* else of WASM_ENABLE_FAST_INTERP */
  9701. if (global_type == VALUE_TYPE_I64
  9702. || global_type == VALUE_TYPE_F64) {
  9703. skip_label();
  9704. emit_label(WASM_OP_GET_GLOBAL_64);
  9705. }
  9706. emit_uint32(loader_ctx, global_idx);
  9707. PUSH_OFFSET_TYPE(global_type);
  9708. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9709. break;
  9710. }
  9711. case WASM_OP_SET_GLOBAL:
  9712. {
  9713. bool is_mutable = false;
  9714. #if WASM_ENABLE_GC != 0
  9715. WASMRefType *ref_type;
  9716. #endif
  9717. p_org = p - 1;
  9718. read_leb_uint32(p, p_end, global_idx);
  9719. if (global_idx >= global_count) {
  9720. set_error_buf(error_buf, error_buf_size, "unknown global");
  9721. goto fail;
  9722. }
  9723. is_mutable =
  9724. global_idx < module->import_global_count
  9725. ? module->import_globals[global_idx].u.global.is_mutable
  9726. : module
  9727. ->globals[global_idx
  9728. - module->import_global_count]
  9729. .is_mutable;
  9730. if (!is_mutable) {
  9731. #if WASM_ENABLE_GC == 0
  9732. set_error_buf(error_buf, error_buf_size,
  9733. "global is immutable");
  9734. #else
  9735. set_error_buf(error_buf, error_buf_size,
  9736. "immutable global");
  9737. #endif
  9738. goto fail;
  9739. }
  9740. global_type =
  9741. global_idx < module->import_global_count
  9742. ? module->import_globals[global_idx].u.global.type
  9743. : module
  9744. ->globals[global_idx
  9745. - module->import_global_count]
  9746. .type;
  9747. #if WASM_ENABLE_GC != 0
  9748. ref_type =
  9749. global_idx < module->import_global_count
  9750. ? module->import_globals[global_idx].u.global.ref_type
  9751. : module
  9752. ->globals[global_idx
  9753. - module->import_global_count]
  9754. .ref_type;
  9755. if (wasm_is_type_multi_byte_type(global_type)) {
  9756. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9757. wasm_reftype_struct_size(ref_type));
  9758. }
  9759. #endif
  9760. POP_TYPE(global_type);
  9761. #if WASM_ENABLE_FAST_INTERP == 0
  9762. if (global_type == VALUE_TYPE_I64
  9763. || global_type == VALUE_TYPE_F64) {
  9764. #if WASM_ENABLE_DEBUG_INTERP != 0
  9765. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9766. error_buf_size)) {
  9767. goto fail;
  9768. }
  9769. #endif
  9770. *p_org = WASM_OP_SET_GLOBAL_64;
  9771. }
  9772. else if (module->aux_stack_size > 0
  9773. && global_idx == module->aux_stack_top_global_index) {
  9774. #if WASM_ENABLE_DEBUG_INTERP != 0
  9775. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9776. error_buf_size)) {
  9777. goto fail;
  9778. }
  9779. #endif
  9780. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  9781. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9782. func->has_op_set_global_aux_stack = true;
  9783. #endif
  9784. }
  9785. #else /* else of WASM_ENABLE_FAST_INTERP */
  9786. if (global_type == VALUE_TYPE_I64
  9787. || global_type == VALUE_TYPE_F64) {
  9788. skip_label();
  9789. emit_label(WASM_OP_SET_GLOBAL_64);
  9790. }
  9791. else if (module->aux_stack_size > 0
  9792. && global_idx == module->aux_stack_top_global_index) {
  9793. skip_label();
  9794. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  9795. }
  9796. emit_uint32(loader_ctx, global_idx);
  9797. POP_OFFSET_TYPE(global_type);
  9798. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9799. break;
  9800. }
  9801. /* load */
  9802. case WASM_OP_I32_LOAD:
  9803. case WASM_OP_I32_LOAD8_S:
  9804. case WASM_OP_I32_LOAD8_U:
  9805. case WASM_OP_I32_LOAD16_S:
  9806. case WASM_OP_I32_LOAD16_U:
  9807. case WASM_OP_I64_LOAD:
  9808. case WASM_OP_I64_LOAD8_S:
  9809. case WASM_OP_I64_LOAD8_U:
  9810. case WASM_OP_I64_LOAD16_S:
  9811. case WASM_OP_I64_LOAD16_U:
  9812. case WASM_OP_I64_LOAD32_S:
  9813. case WASM_OP_I64_LOAD32_U:
  9814. case WASM_OP_F32_LOAD:
  9815. case WASM_OP_F64_LOAD:
  9816. /* store */
  9817. case WASM_OP_I32_STORE:
  9818. case WASM_OP_I32_STORE8:
  9819. case WASM_OP_I32_STORE16:
  9820. case WASM_OP_I64_STORE:
  9821. case WASM_OP_I64_STORE8:
  9822. case WASM_OP_I64_STORE16:
  9823. case WASM_OP_I64_STORE32:
  9824. case WASM_OP_F32_STORE:
  9825. case WASM_OP_F64_STORE:
  9826. {
  9827. #if WASM_ENABLE_FAST_INTERP != 0
  9828. /* change F32/F64 into I32/I64 */
  9829. if (opcode == WASM_OP_F32_LOAD) {
  9830. skip_label();
  9831. emit_label(WASM_OP_I32_LOAD);
  9832. }
  9833. else if (opcode == WASM_OP_F64_LOAD) {
  9834. skip_label();
  9835. emit_label(WASM_OP_I64_LOAD);
  9836. }
  9837. else if (opcode == WASM_OP_F32_STORE) {
  9838. skip_label();
  9839. emit_label(WASM_OP_I32_STORE);
  9840. }
  9841. else if (opcode == WASM_OP_F64_STORE) {
  9842. skip_label();
  9843. emit_label(WASM_OP_I64_STORE);
  9844. }
  9845. #endif
  9846. CHECK_MEMORY();
  9847. read_leb_uint32(p, p_end, align); /* align */
  9848. read_leb_uint32(p, p_end, mem_offset); /* offset */
  9849. if (!check_memory_access_align(opcode, align, error_buf,
  9850. error_buf_size)) {
  9851. goto fail;
  9852. }
  9853. #if WASM_ENABLE_FAST_INTERP != 0
  9854. emit_uint32(loader_ctx, mem_offset);
  9855. #endif
  9856. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9857. func->has_memory_operations = true;
  9858. #endif
  9859. switch (opcode) {
  9860. /* load */
  9861. case WASM_OP_I32_LOAD:
  9862. case WASM_OP_I32_LOAD8_S:
  9863. case WASM_OP_I32_LOAD8_U:
  9864. case WASM_OP_I32_LOAD16_S:
  9865. case WASM_OP_I32_LOAD16_U:
  9866. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9867. break;
  9868. case WASM_OP_I64_LOAD:
  9869. case WASM_OP_I64_LOAD8_S:
  9870. case WASM_OP_I64_LOAD8_U:
  9871. case WASM_OP_I64_LOAD16_S:
  9872. case WASM_OP_I64_LOAD16_U:
  9873. case WASM_OP_I64_LOAD32_S:
  9874. case WASM_OP_I64_LOAD32_U:
  9875. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  9876. break;
  9877. case WASM_OP_F32_LOAD:
  9878. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  9879. break;
  9880. case WASM_OP_F64_LOAD:
  9881. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  9882. break;
  9883. /* store */
  9884. case WASM_OP_I32_STORE:
  9885. case WASM_OP_I32_STORE8:
  9886. case WASM_OP_I32_STORE16:
  9887. POP_I32();
  9888. POP_I32();
  9889. break;
  9890. case WASM_OP_I64_STORE:
  9891. case WASM_OP_I64_STORE8:
  9892. case WASM_OP_I64_STORE16:
  9893. case WASM_OP_I64_STORE32:
  9894. POP_I64();
  9895. POP_I32();
  9896. break;
  9897. case WASM_OP_F32_STORE:
  9898. POP_F32();
  9899. POP_I32();
  9900. break;
  9901. case WASM_OP_F64_STORE:
  9902. POP_F64();
  9903. POP_I32();
  9904. break;
  9905. default:
  9906. break;
  9907. }
  9908. break;
  9909. }
  9910. case WASM_OP_MEMORY_SIZE:
  9911. CHECK_MEMORY();
  9912. /* reserved byte 0x00 */
  9913. if (*p++ != 0x00) {
  9914. set_error_buf(error_buf, error_buf_size,
  9915. "zero byte expected");
  9916. goto fail;
  9917. }
  9918. PUSH_I32();
  9919. module->possible_memory_grow = true;
  9920. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9921. func->has_memory_operations = true;
  9922. #endif
  9923. break;
  9924. case WASM_OP_MEMORY_GROW:
  9925. CHECK_MEMORY();
  9926. /* reserved byte 0x00 */
  9927. if (*p++ != 0x00) {
  9928. set_error_buf(error_buf, error_buf_size,
  9929. "zero byte expected");
  9930. goto fail;
  9931. }
  9932. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9933. module->possible_memory_grow = true;
  9934. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  9935. || WASM_ENABLE_WAMR_COMPILER != 0
  9936. func->has_op_memory_grow = true;
  9937. #endif
  9938. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9939. func->has_memory_operations = true;
  9940. #endif
  9941. break;
  9942. case WASM_OP_I32_CONST:
  9943. read_leb_int32(p, p_end, i32_const);
  9944. #if WASM_ENABLE_FAST_INTERP != 0
  9945. skip_label();
  9946. disable_emit = true;
  9947. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  9948. if (operand_offset == 0) {
  9949. disable_emit = false;
  9950. emit_label(WASM_OP_I32_CONST);
  9951. emit_uint32(loader_ctx, i32_const);
  9952. }
  9953. #else
  9954. (void)i32_const;
  9955. #endif
  9956. PUSH_I32();
  9957. break;
  9958. case WASM_OP_I64_CONST:
  9959. read_leb_int64(p, p_end, i64_const);
  9960. #if WASM_ENABLE_FAST_INTERP != 0
  9961. skip_label();
  9962. disable_emit = true;
  9963. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  9964. if (operand_offset == 0) {
  9965. disable_emit = false;
  9966. emit_label(WASM_OP_I64_CONST);
  9967. emit_uint64(loader_ctx, i64_const);
  9968. }
  9969. #endif
  9970. PUSH_I64();
  9971. break;
  9972. case WASM_OP_F32_CONST:
  9973. p += sizeof(float32);
  9974. #if WASM_ENABLE_FAST_INTERP != 0
  9975. skip_label();
  9976. disable_emit = true;
  9977. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  9978. sizeof(float32));
  9979. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  9980. if (operand_offset == 0) {
  9981. disable_emit = false;
  9982. emit_label(WASM_OP_F32_CONST);
  9983. emit_float32(loader_ctx, f32_const);
  9984. }
  9985. #endif
  9986. PUSH_F32();
  9987. break;
  9988. case WASM_OP_F64_CONST:
  9989. p += sizeof(float64);
  9990. #if WASM_ENABLE_FAST_INTERP != 0
  9991. skip_label();
  9992. disable_emit = true;
  9993. /* Some MCU may require 8-byte align */
  9994. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  9995. sizeof(float64));
  9996. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  9997. if (operand_offset == 0) {
  9998. disable_emit = false;
  9999. emit_label(WASM_OP_F64_CONST);
  10000. emit_float64(loader_ctx, f64_const);
  10001. }
  10002. #endif
  10003. PUSH_F64();
  10004. break;
  10005. case WASM_OP_I32_EQZ:
  10006. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10007. break;
  10008. case WASM_OP_I32_EQ:
  10009. case WASM_OP_I32_NE:
  10010. case WASM_OP_I32_LT_S:
  10011. case WASM_OP_I32_LT_U:
  10012. case WASM_OP_I32_GT_S:
  10013. case WASM_OP_I32_GT_U:
  10014. case WASM_OP_I32_LE_S:
  10015. case WASM_OP_I32_LE_U:
  10016. case WASM_OP_I32_GE_S:
  10017. case WASM_OP_I32_GE_U:
  10018. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10019. break;
  10020. case WASM_OP_I64_EQZ:
  10021. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10022. break;
  10023. case WASM_OP_I64_EQ:
  10024. case WASM_OP_I64_NE:
  10025. case WASM_OP_I64_LT_S:
  10026. case WASM_OP_I64_LT_U:
  10027. case WASM_OP_I64_GT_S:
  10028. case WASM_OP_I64_GT_U:
  10029. case WASM_OP_I64_LE_S:
  10030. case WASM_OP_I64_LE_U:
  10031. case WASM_OP_I64_GE_S:
  10032. case WASM_OP_I64_GE_U:
  10033. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10034. break;
  10035. case WASM_OP_F32_EQ:
  10036. case WASM_OP_F32_NE:
  10037. case WASM_OP_F32_LT:
  10038. case WASM_OP_F32_GT:
  10039. case WASM_OP_F32_LE:
  10040. case WASM_OP_F32_GE:
  10041. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10042. break;
  10043. case WASM_OP_F64_EQ:
  10044. case WASM_OP_F64_NE:
  10045. case WASM_OP_F64_LT:
  10046. case WASM_OP_F64_GT:
  10047. case WASM_OP_F64_LE:
  10048. case WASM_OP_F64_GE:
  10049. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10050. break;
  10051. case WASM_OP_I32_CLZ:
  10052. case WASM_OP_I32_CTZ:
  10053. case WASM_OP_I32_POPCNT:
  10054. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10055. break;
  10056. case WASM_OP_I32_ADD:
  10057. case WASM_OP_I32_SUB:
  10058. case WASM_OP_I32_MUL:
  10059. case WASM_OP_I32_DIV_S:
  10060. case WASM_OP_I32_DIV_U:
  10061. case WASM_OP_I32_REM_S:
  10062. case WASM_OP_I32_REM_U:
  10063. case WASM_OP_I32_AND:
  10064. case WASM_OP_I32_OR:
  10065. case WASM_OP_I32_XOR:
  10066. case WASM_OP_I32_SHL:
  10067. case WASM_OP_I32_SHR_S:
  10068. case WASM_OP_I32_SHR_U:
  10069. case WASM_OP_I32_ROTL:
  10070. case WASM_OP_I32_ROTR:
  10071. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10072. break;
  10073. case WASM_OP_I64_CLZ:
  10074. case WASM_OP_I64_CTZ:
  10075. case WASM_OP_I64_POPCNT:
  10076. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10077. break;
  10078. case WASM_OP_I64_ADD:
  10079. case WASM_OP_I64_SUB:
  10080. case WASM_OP_I64_MUL:
  10081. case WASM_OP_I64_DIV_S:
  10082. case WASM_OP_I64_DIV_U:
  10083. case WASM_OP_I64_REM_S:
  10084. case WASM_OP_I64_REM_U:
  10085. case WASM_OP_I64_AND:
  10086. case WASM_OP_I64_OR:
  10087. case WASM_OP_I64_XOR:
  10088. case WASM_OP_I64_SHL:
  10089. case WASM_OP_I64_SHR_S:
  10090. case WASM_OP_I64_SHR_U:
  10091. case WASM_OP_I64_ROTL:
  10092. case WASM_OP_I64_ROTR:
  10093. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10094. break;
  10095. case WASM_OP_F32_ABS:
  10096. case WASM_OP_F32_NEG:
  10097. case WASM_OP_F32_CEIL:
  10098. case WASM_OP_F32_FLOOR:
  10099. case WASM_OP_F32_TRUNC:
  10100. case WASM_OP_F32_NEAREST:
  10101. case WASM_OP_F32_SQRT:
  10102. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  10103. break;
  10104. case WASM_OP_F32_ADD:
  10105. case WASM_OP_F32_SUB:
  10106. case WASM_OP_F32_MUL:
  10107. case WASM_OP_F32_DIV:
  10108. case WASM_OP_F32_MIN:
  10109. case WASM_OP_F32_MAX:
  10110. case WASM_OP_F32_COPYSIGN:
  10111. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  10112. break;
  10113. case WASM_OP_F64_ABS:
  10114. case WASM_OP_F64_NEG:
  10115. case WASM_OP_F64_CEIL:
  10116. case WASM_OP_F64_FLOOR:
  10117. case WASM_OP_F64_TRUNC:
  10118. case WASM_OP_F64_NEAREST:
  10119. case WASM_OP_F64_SQRT:
  10120. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  10121. break;
  10122. case WASM_OP_F64_ADD:
  10123. case WASM_OP_F64_SUB:
  10124. case WASM_OP_F64_MUL:
  10125. case WASM_OP_F64_DIV:
  10126. case WASM_OP_F64_MIN:
  10127. case WASM_OP_F64_MAX:
  10128. case WASM_OP_F64_COPYSIGN:
  10129. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  10130. break;
  10131. case WASM_OP_I32_WRAP_I64:
  10132. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10133. break;
  10134. case WASM_OP_I32_TRUNC_S_F32:
  10135. case WASM_OP_I32_TRUNC_U_F32:
  10136. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10137. break;
  10138. case WASM_OP_I32_TRUNC_S_F64:
  10139. case WASM_OP_I32_TRUNC_U_F64:
  10140. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10141. break;
  10142. case WASM_OP_I64_EXTEND_S_I32:
  10143. case WASM_OP_I64_EXTEND_U_I32:
  10144. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  10145. break;
  10146. case WASM_OP_I64_TRUNC_S_F32:
  10147. case WASM_OP_I64_TRUNC_U_F32:
  10148. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  10149. break;
  10150. case WASM_OP_I64_TRUNC_S_F64:
  10151. case WASM_OP_I64_TRUNC_U_F64:
  10152. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10153. break;
  10154. case WASM_OP_F32_CONVERT_S_I32:
  10155. case WASM_OP_F32_CONVERT_U_I32:
  10156. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  10157. break;
  10158. case WASM_OP_F32_CONVERT_S_I64:
  10159. case WASM_OP_F32_CONVERT_U_I64:
  10160. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  10161. break;
  10162. case WASM_OP_F32_DEMOTE_F64:
  10163. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  10164. break;
  10165. case WASM_OP_F64_CONVERT_S_I32:
  10166. case WASM_OP_F64_CONVERT_U_I32:
  10167. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  10168. break;
  10169. case WASM_OP_F64_CONVERT_S_I64:
  10170. case WASM_OP_F64_CONVERT_U_I64:
  10171. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  10172. break;
  10173. case WASM_OP_F64_PROMOTE_F32:
  10174. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  10175. break;
  10176. case WASM_OP_I32_REINTERPRET_F32:
  10177. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10178. break;
  10179. case WASM_OP_I64_REINTERPRET_F64:
  10180. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10181. break;
  10182. case WASM_OP_F32_REINTERPRET_I32:
  10183. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  10184. break;
  10185. case WASM_OP_F64_REINTERPRET_I64:
  10186. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  10187. break;
  10188. case WASM_OP_I32_EXTEND8_S:
  10189. case WASM_OP_I32_EXTEND16_S:
  10190. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10191. break;
  10192. case WASM_OP_I64_EXTEND8_S:
  10193. case WASM_OP_I64_EXTEND16_S:
  10194. case WASM_OP_I64_EXTEND32_S:
  10195. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10196. break;
  10197. #if WASM_ENABLE_GC != 0
  10198. case WASM_OP_GC_PREFIX:
  10199. {
  10200. uint32 opcode1;
  10201. read_leb_uint32(p, p_end, opcode1);
  10202. #if WASM_ENABLE_FAST_INTERP != 0
  10203. emit_byte(loader_ctx, ((uint8)opcode1));
  10204. #endif
  10205. switch (opcode1) {
  10206. case WASM_OP_STRUCT_NEW_CANON:
  10207. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  10208. {
  10209. read_leb_uint32(p, p_end, type_idx);
  10210. #if WASM_ENABLE_FAST_INTERP != 0
  10211. emit_uint32(loader_ctx, type_idx);
  10212. #endif
  10213. if (!check_type_index(module, type_idx, error_buf,
  10214. error_buf_size)) {
  10215. goto fail;
  10216. }
  10217. if (module->types[type_idx]->type_flag
  10218. != WASM_TYPE_STRUCT) {
  10219. set_error_buf(error_buf, error_buf_size,
  10220. "unkown struct type");
  10221. goto fail;
  10222. }
  10223. if (opcode1 == WASM_OP_STRUCT_NEW_CANON) {
  10224. int32 j, k;
  10225. uint8 value_type;
  10226. uint32 ref_type_struct_size;
  10227. WASMStructType *struct_type =
  10228. (WASMStructType *)module->types[type_idx];
  10229. k = struct_type->ref_type_map_count - 1;
  10230. for (j = struct_type->field_count - 1; j >= 0;
  10231. j--) {
  10232. value_type = struct_type->fields[j].field_type;
  10233. if (wasm_is_type_reftype(value_type)) {
  10234. if (wasm_is_type_multi_byte_type(
  10235. value_type)) {
  10236. ref_type_struct_size =
  10237. wasm_reftype_struct_size(
  10238. struct_type->ref_type_maps[k]
  10239. .ref_type);
  10240. bh_memcpy_s(
  10241. &wasm_ref_type,
  10242. (uint32)sizeof(WASMRefType),
  10243. struct_type->ref_type_maps[k]
  10244. .ref_type,
  10245. ref_type_struct_size);
  10246. k--;
  10247. }
  10248. POP_REF(value_type);
  10249. }
  10250. else {
  10251. switch (value_type) {
  10252. case VALUE_TYPE_I32:
  10253. case PACKED_TYPE_I8:
  10254. case PACKED_TYPE_I16:
  10255. POP_I32();
  10256. break;
  10257. case VALUE_TYPE_I64:
  10258. POP_I64();
  10259. break;
  10260. case VALUE_TYPE_F32:
  10261. POP_F32();
  10262. break;
  10263. case VALUE_TYPE_F64:
  10264. POP_F64();
  10265. break;
  10266. default:
  10267. set_error_buf(error_buf,
  10268. error_buf_size,
  10269. "unknown type");
  10270. goto fail;
  10271. }
  10272. }
  10273. }
  10274. }
  10275. /* PUSH struct obj, (ref $t) */
  10276. wasm_set_refheaptype_typeidx(
  10277. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  10278. PUSH_REF(wasm_ref_type.ref_type);
  10279. break;
  10280. }
  10281. case WASM_OP_STRUCT_GET:
  10282. case WASM_OP_STRUCT_GET_S:
  10283. case WASM_OP_STRUCT_GET_U:
  10284. case WASM_OP_STRUCT_SET:
  10285. {
  10286. WASMStructType *struct_type;
  10287. WASMRefType *ref_type = NULL;
  10288. uint32 field_idx;
  10289. uint8 field_type;
  10290. read_leb_uint32(p, p_end, type_idx);
  10291. #if WASM_ENABLE_FAST_INTERP != 0
  10292. emit_uint32(loader_ctx, type_idx);
  10293. #endif
  10294. if (!check_type_index(module, type_idx, error_buf,
  10295. error_buf_size)) {
  10296. goto fail;
  10297. }
  10298. if (module->types[type_idx]->type_flag
  10299. != WASM_TYPE_STRUCT) {
  10300. set_error_buf(error_buf, error_buf_size,
  10301. "unknown struct type");
  10302. goto fail;
  10303. }
  10304. struct_type = (WASMStructType *)module->types[type_idx];
  10305. read_leb_uint32(p, p_end, field_idx);
  10306. #if WASM_ENABLE_FAST_INTERP != 0
  10307. emit_uint32(loader_ctx, field_idx);
  10308. #endif
  10309. if (field_idx >= struct_type->field_count) {
  10310. set_error_buf(error_buf, error_buf_size,
  10311. "unknown struct field");
  10312. goto fail;
  10313. }
  10314. if (opcode1 == WASM_OP_STRUCT_SET
  10315. && !(struct_type->fields[field_idx].field_flags
  10316. & 1)) {
  10317. set_error_buf(error_buf, error_buf_size,
  10318. "field is immutable");
  10319. goto fail;
  10320. }
  10321. field_type = struct_type->fields[field_idx].field_type;
  10322. if (is_packed_type(field_type)) {
  10323. if (opcode1 != WASM_OP_STRUCT_GET_S
  10324. && opcode1 != WASM_OP_STRUCT_GET_U) {
  10325. set_error_buf(error_buf, error_buf_size,
  10326. "type mismatch");
  10327. goto fail;
  10328. }
  10329. else {
  10330. field_type = VALUE_TYPE_I32;
  10331. }
  10332. }
  10333. if (wasm_is_type_multi_byte_type(field_type)) {
  10334. ref_type = wasm_reftype_map_find(
  10335. struct_type->ref_type_maps,
  10336. struct_type->ref_type_map_count, field_idx);
  10337. bh_assert(ref_type);
  10338. }
  10339. if (opcode1 == WASM_OP_STRUCT_SET) {
  10340. /* POP field */
  10341. if (wasm_is_type_multi_byte_type(field_type)) {
  10342. bh_memcpy_s(&wasm_ref_type,
  10343. (uint32)sizeof(WASMRefType),
  10344. ref_type,
  10345. wasm_reftype_struct_size(ref_type));
  10346. }
  10347. POP_REF(field_type);
  10348. /* POP struct obj, (ref null $t) */
  10349. wasm_set_refheaptype_typeidx(
  10350. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10351. POP_REF(wasm_ref_type.ref_type);
  10352. }
  10353. else {
  10354. /* POP struct obj, (ref null $t) */
  10355. wasm_set_refheaptype_typeidx(
  10356. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10357. POP_REF(wasm_ref_type.ref_type);
  10358. /* PUSH field */
  10359. if (wasm_is_type_multi_byte_type(field_type)) {
  10360. bh_memcpy_s(&wasm_ref_type,
  10361. (uint32)sizeof(WASMRefType),
  10362. ref_type,
  10363. wasm_reftype_struct_size(ref_type));
  10364. }
  10365. PUSH_REF(field_type);
  10366. }
  10367. break;
  10368. }
  10369. case WASM_OP_ARRAY_NEW_CANON:
  10370. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  10371. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  10372. case WASM_OP_ARRAY_NEW_CANON_DATA:
  10373. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  10374. {
  10375. WASMArrayType *array_type;
  10376. uint8 elem_type;
  10377. uint32 u32 = 0;
  10378. read_leb_uint32(p, p_end, type_idx);
  10379. #if WASM_ENABLE_FAST_INTERP != 0
  10380. emit_uint32(loader_ctx, type_idx);
  10381. #endif
  10382. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED
  10383. || opcode1 == WASM_OP_ARRAY_NEW_CANON_DATA
  10384. || opcode1 == WASM_OP_ARRAY_NEW_CANON_ELEM) {
  10385. read_leb_uint32(p, p_end, u32);
  10386. #if WASM_ENABLE_FAST_INTERP != 0
  10387. emit_uint32(loader_ctx, u32);
  10388. #endif
  10389. }
  10390. if (!check_array_type(module, type_idx, error_buf,
  10391. error_buf_size)) {
  10392. goto fail;
  10393. }
  10394. if (opcode1 != WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10395. /* length */
  10396. POP_I32();
  10397. }
  10398. array_type = (WASMArrayType *)module->types[type_idx];
  10399. elem_type = array_type->elem_type;
  10400. if (opcode1 == WASM_OP_ARRAY_NEW_CANON
  10401. || opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10402. if (wasm_is_type_multi_byte_type(elem_type)) {
  10403. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10404. array_type->elem_ref_type,
  10405. wasm_reftype_struct_size(
  10406. array_type->elem_ref_type));
  10407. }
  10408. if (is_packed_type(elem_type)) {
  10409. elem_type = VALUE_TYPE_I32;
  10410. }
  10411. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10412. uint32 N = u32;
  10413. for (i = 0; i < N; i++)
  10414. POP_REF(elem_type);
  10415. }
  10416. else
  10417. POP_REF(elem_type);
  10418. }
  10419. else if (opcode1 == WASM_OP_ARRAY_NEW_CANON_DATA) {
  10420. /* offset of data segment */
  10421. POP_I32();
  10422. if (u32 >= module->data_seg_count) {
  10423. set_error_buf(error_buf, error_buf_size,
  10424. "unknown data segement");
  10425. goto fail;
  10426. }
  10427. if (wasm_is_type_reftype(elem_type)) {
  10428. set_error_buf(error_buf, error_buf_size,
  10429. "array elem type mismatch");
  10430. goto fail;
  10431. }
  10432. }
  10433. else if (opcode1 == WASM_OP_ARRAY_NEW_CANON_ELEM) {
  10434. WASMTableSeg *table_seg =
  10435. module->table_segments + u32;
  10436. /* offset of element segment */
  10437. POP_I32();
  10438. if (u32 >= module->table_seg_count) {
  10439. set_error_buf(error_buf, error_buf_size,
  10440. "unknown element segement");
  10441. goto fail;
  10442. }
  10443. if (!wasm_reftype_is_subtype_of(
  10444. table_seg->elem_type,
  10445. table_seg->elem_ref_type, elem_type,
  10446. array_type->elem_ref_type, module->types,
  10447. module->type_count)) {
  10448. set_error_buf(error_buf, error_buf_size,
  10449. "array elem type mismatch");
  10450. goto fail;
  10451. }
  10452. }
  10453. /* PUSH array obj, (ref $t) */
  10454. wasm_set_refheaptype_typeidx(
  10455. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  10456. PUSH_REF(wasm_ref_type.ref_type);
  10457. break;
  10458. }
  10459. case WASM_OP_ARRAY_GET:
  10460. case WASM_OP_ARRAY_GET_S:
  10461. case WASM_OP_ARRAY_GET_U:
  10462. case WASM_OP_ARRAY_SET:
  10463. {
  10464. uint8 elem_type;
  10465. WASMArrayType *array_type;
  10466. WASMRefType *ref_type = NULL;
  10467. read_leb_uint32(p, p_end, type_idx);
  10468. #if WASM_ENABLE_FAST_INTERP != 0
  10469. emit_uint32(loader_ctx, type_idx);
  10470. #endif
  10471. if (!check_array_type(module, type_idx, error_buf,
  10472. error_buf_size)) {
  10473. goto fail;
  10474. }
  10475. array_type = (WASMArrayType *)module->types[type_idx];
  10476. if (opcode1 == WASM_OP_ARRAY_SET
  10477. && !(array_type->elem_flags & 1)) {
  10478. set_error_buf(error_buf, error_buf_size,
  10479. "array is immutable");
  10480. goto fail;
  10481. }
  10482. elem_type = array_type->elem_type;
  10483. if (is_packed_type(elem_type)) {
  10484. if (opcode1 != WASM_OP_ARRAY_GET_S
  10485. && opcode1 != WASM_OP_ARRAY_GET_U
  10486. && opcode1 != WASM_OP_ARRAY_SET) {
  10487. set_error_buf(error_buf, error_buf_size,
  10488. "type mismatch");
  10489. goto fail;
  10490. }
  10491. else {
  10492. elem_type = VALUE_TYPE_I32;
  10493. }
  10494. }
  10495. ref_type = array_type->elem_ref_type;
  10496. if (opcode1 == WASM_OP_ARRAY_SET) {
  10497. /* POP elem to set */
  10498. if (wasm_is_type_multi_byte_type(elem_type)) {
  10499. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10500. ref_type,
  10501. wasm_reftype_struct_size(ref_type));
  10502. }
  10503. POP_REF(elem_type);
  10504. }
  10505. /* elem idx */
  10506. POP_I32();
  10507. /* POP array obj, (ref null $t) */
  10508. wasm_set_refheaptype_typeidx(
  10509. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10510. POP_REF(wasm_ref_type.ref_type);
  10511. if (opcode1 != WASM_OP_ARRAY_SET) {
  10512. /* PUSH elem */
  10513. if (wasm_is_type_multi_byte_type(elem_type)) {
  10514. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10515. ref_type,
  10516. wasm_reftype_struct_size(ref_type));
  10517. }
  10518. PUSH_REF(elem_type);
  10519. }
  10520. break;
  10521. }
  10522. #if WASM_ENABLE_GC_BINARYEN != 0
  10523. case WASM_OP_ARRAY_COPY:
  10524. {
  10525. uint32 src_type_idx;
  10526. WASMRefType src_ref_type = { 0 };
  10527. WASMRefType dst_ref_type = { 0 };
  10528. WASMArrayType *array_type;
  10529. /* typeidx1 */
  10530. read_leb_uint32(p, p_end, type_idx);
  10531. #if WASM_ENABLE_FAST_INTERP != 0
  10532. emit_uint32(loader_ctx, type_idx);
  10533. #endif
  10534. /* typeidx2 */
  10535. read_leb_uint32(p, p_end, src_type_idx);
  10536. #if WASM_ENABLE_FAST_INTERP != 0
  10537. emit_uint32(loader_ctx, src_type_idx);
  10538. #endif
  10539. if (!check_array_type(module, type_idx, error_buf,
  10540. error_buf_size)) {
  10541. goto fail;
  10542. }
  10543. if (!check_array_type(module, src_type_idx, error_buf,
  10544. error_buf_size)) {
  10545. goto fail;
  10546. }
  10547. POP_I32();
  10548. POP_I32();
  10549. /* POP array obj, (ref null $t) */
  10550. wasm_set_refheaptype_typeidx(
  10551. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  10552. POP_REF(wasm_ref_type.ref_type);
  10553. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  10554. &wasm_ref_type,
  10555. wasm_reftype_struct_size(&wasm_ref_type));
  10556. POP_I32();
  10557. /* POP array obj, (ref null $t) */
  10558. wasm_set_refheaptype_typeidx(
  10559. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10560. POP_REF(wasm_ref_type.ref_type);
  10561. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  10562. &wasm_ref_type,
  10563. wasm_reftype_struct_size(&wasm_ref_type));
  10564. if (!wasm_type_is_subtype_of(
  10565. module->types[src_type_idx],
  10566. module->types[type_idx], module->types,
  10567. module->type_count)) {
  10568. set_error_buf(error_buf, error_buf_size,
  10569. "type mismatch");
  10570. goto fail;
  10571. }
  10572. array_type = (WASMArrayType *)module->types[type_idx];
  10573. if (!(array_type->elem_flags & 1)) {
  10574. set_error_buf(error_buf, error_buf_size,
  10575. "array is immutable");
  10576. goto fail;
  10577. }
  10578. break;
  10579. }
  10580. #endif
  10581. case WASM_OP_ARRAY_LEN:
  10582. {
  10583. POP_REF(REF_TYPE_ARRAYREF);
  10584. /* length */
  10585. PUSH_I32();
  10586. break;
  10587. }
  10588. case WASM_OP_I31_NEW:
  10589. {
  10590. POP_I32();
  10591. wasm_set_refheaptype_common(
  10592. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  10593. PUSH_REF(wasm_ref_type.ref_type);
  10594. break;
  10595. }
  10596. case WASM_OP_I31_GET_S:
  10597. case WASM_OP_I31_GET_U:
  10598. {
  10599. POP_REF(REF_TYPE_I31REF);
  10600. PUSH_I32();
  10601. break;
  10602. }
  10603. case WASM_OP_REF_TEST:
  10604. case WASM_OP_REF_CAST:
  10605. case WASM_OP_REF_TEST_NULLABLE:
  10606. case WASM_OP_REF_CAST_NULLABLE:
  10607. {
  10608. uint8 type;
  10609. read_leb_int32(p, p_end, heap_type);
  10610. #if WASM_ENABLE_FAST_INTERP != 0
  10611. emit_uint32(loader_ctx, (uint32)heap_type);
  10612. #endif
  10613. if (heap_type >= 0) {
  10614. if (!check_type_index(module, heap_type, error_buf,
  10615. error_buf_size)) {
  10616. goto fail;
  10617. }
  10618. }
  10619. else {
  10620. if (heap_type > HEAP_TYPE_FUNC
  10621. || heap_type < HEAP_TYPE_NONE) {
  10622. set_error_buf(error_buf, error_buf_size,
  10623. "unknown type");
  10624. goto fail;
  10625. }
  10626. }
  10627. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10628. &wasm_ref_type, error_buf,
  10629. error_buf_size)) {
  10630. goto fail;
  10631. }
  10632. if (opcode1 == WASM_OP_REF_TEST
  10633. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  10634. PUSH_I32();
  10635. else {
  10636. bool nullable =
  10637. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  10638. : false;
  10639. wasm_set_refheaptype_typeidx(
  10640. &wasm_ref_type.ref_ht_typeidx, nullable,
  10641. heap_type);
  10642. PUSH_REF(wasm_ref_type.ref_type);
  10643. }
  10644. break;
  10645. }
  10646. case WASM_OP_BR_ON_CAST:
  10647. case WASM_OP_BR_ON_CAST_NULLABLE:
  10648. {
  10649. WASMRefType ref_type_tmp;
  10650. uint8 type_tmp;
  10651. uint32 depth;
  10652. bool nullable;
  10653. nullable = (opcode1 == WASM_OP_BR_ON_CAST_NULLABLE)
  10654. ? true
  10655. : false;
  10656. p_org = p;
  10657. read_leb_uint32(p, p_end, depth);
  10658. read_leb_int32(p, p_end, heap_type);
  10659. #if WASM_ENABLE_FAST_INTERP != 0
  10660. /* Emit heap_type firstly */
  10661. emit_uint32(loader_ctx, (uint32)heap_type);
  10662. #endif
  10663. (void)depth;
  10664. /* Pop and backup the stack top's ref type */
  10665. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  10666. &ref_type_tmp, error_buf,
  10667. error_buf_size)) {
  10668. goto fail;
  10669. }
  10670. if (heap_type >= 0) {
  10671. if (!check_type_index(module, heap_type, error_buf,
  10672. error_buf_size)) {
  10673. goto fail;
  10674. }
  10675. wasm_set_refheaptype_typeidx(
  10676. &wasm_ref_type.ref_ht_typeidx, nullable,
  10677. heap_type);
  10678. }
  10679. else {
  10680. if (heap_type > HEAP_TYPE_FUNC
  10681. || heap_type < HEAP_TYPE_NONE) {
  10682. set_error_buf(error_buf, error_buf_size,
  10683. "unknown type");
  10684. goto fail;
  10685. }
  10686. wasm_set_refheaptype_common(
  10687. &wasm_ref_type.ref_ht_common, nullable,
  10688. heap_type);
  10689. }
  10690. p = p_org;
  10691. /* Push ref type casted for branch block check */
  10692. PUSH_REF(wasm_ref_type.ref_type);
  10693. if (!(frame_csp_tmp = check_branch_block(
  10694. loader_ctx, &p, p_end, false, error_buf,
  10695. error_buf_size))) {
  10696. goto fail;
  10697. }
  10698. /* Ignore heap_type */
  10699. skip_leb_uint32(p, p_end);
  10700. /* Restore the original stack top's ref type */
  10701. POP_REF(wasm_ref_type.ref_type);
  10702. #if WASM_ENABLE_FAST_INTERP != 0
  10703. /* Erase the opnd offset emitted by POP_REF() */
  10704. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10705. #endif
  10706. if (wasm_is_type_multi_byte_type(type_tmp)) {
  10707. bh_memcpy_s(
  10708. &wasm_ref_type,
  10709. wasm_reftype_struct_size(&ref_type_tmp),
  10710. &ref_type_tmp,
  10711. wasm_reftype_struct_size(&ref_type_tmp));
  10712. }
  10713. PUSH_REF(type_tmp);
  10714. #if WASM_ENABLE_FAST_INTERP != 0
  10715. /* Erase the opnd offset emitted by PUSH_REF() */
  10716. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10717. #endif
  10718. break;
  10719. }
  10720. case WASM_OP_BR_ON_CAST_FAIL:
  10721. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  10722. {
  10723. uint8 type_tmp;
  10724. uint32 depth;
  10725. bool nullable;
  10726. nullable = (opcode1 == WASM_OP_BR_ON_CAST_FAIL_NULLABLE)
  10727. ? true
  10728. : false;
  10729. p_org = p;
  10730. read_leb_uint32(p, p_end, depth);
  10731. read_leb_int32(p, p_end, heap_type);
  10732. #if WASM_ENABLE_FAST_INTERP != 0
  10733. /* Emit heap_type firstly */
  10734. emit_uint32(loader_ctx, (uint32)heap_type);
  10735. #endif
  10736. (void)depth;
  10737. /* Pop and push the stack top's ref type so fast-interp
  10738. can get the opnd offset */
  10739. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  10740. &wasm_ref_type, error_buf,
  10741. error_buf_size)) {
  10742. goto fail;
  10743. }
  10744. p = p_org;
  10745. PUSH_REF(type_tmp);
  10746. if (!(frame_csp_tmp = check_branch_block(
  10747. loader_ctx, &p, p_end, false, error_buf,
  10748. error_buf_size))) {
  10749. goto fail;
  10750. }
  10751. /* Ignore heap_type */
  10752. skip_leb_uint32(p, p_end);
  10753. /* Pop the original ref type, and push the casted
  10754. ref type */
  10755. POP_REF(type_tmp);
  10756. #if WASM_ENABLE_FAST_INTERP != 0
  10757. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10758. #endif
  10759. if (heap_type >= 0) {
  10760. if (!check_type_index(module, heap_type, error_buf,
  10761. error_buf_size)) {
  10762. goto fail;
  10763. }
  10764. wasm_set_refheaptype_typeidx(
  10765. &wasm_ref_type.ref_ht_typeidx, nullable,
  10766. heap_type);
  10767. }
  10768. else {
  10769. if (heap_type > HEAP_TYPE_FUNC
  10770. || heap_type < HEAP_TYPE_NONE) {
  10771. set_error_buf(error_buf, error_buf_size,
  10772. "unknown type");
  10773. goto fail;
  10774. }
  10775. wasm_set_refheaptype_common(
  10776. &wasm_ref_type.ref_ht_common, nullable,
  10777. heap_type);
  10778. }
  10779. PUSH_REF(wasm_ref_type.ref_type);
  10780. #if WASM_ENABLE_FAST_INTERP != 0
  10781. /* Erase the opnd offset emitted by PUSH_REF() */
  10782. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10783. #endif
  10784. break;
  10785. }
  10786. case WASM_OP_EXTERN_INTERNALIZE:
  10787. {
  10788. uint8 type;
  10789. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10790. &wasm_ref_type, error_buf,
  10791. error_buf_size)) {
  10792. goto fail;
  10793. }
  10794. if (!(type == REF_TYPE_EXTERNREF
  10795. || (type == REF_TYPE_HT_NON_NULLABLE
  10796. && wasm_ref_type.ref_ht_common.heap_type
  10797. == HEAP_TYPE_EXTERN)
  10798. || type == VALUE_TYPE_ANY)) {
  10799. set_error_buf(error_buf, error_buf_size,
  10800. "type mismatch");
  10801. goto fail;
  10802. }
  10803. if (type == REF_TYPE_EXTERNREF)
  10804. type = REF_TYPE_ANYREF;
  10805. else {
  10806. wasm_ref_type.ref_ht_common.heap_type =
  10807. HEAP_TYPE_ANY;
  10808. }
  10809. PUSH_REF(type);
  10810. break;
  10811. }
  10812. case WASM_OP_EXTERN_EXTERNALIZE:
  10813. {
  10814. uint8 type;
  10815. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10816. &wasm_ref_type, error_buf,
  10817. error_buf_size)) {
  10818. goto fail;
  10819. }
  10820. if (type == REF_TYPE_EXTERNREF
  10821. || ((type == REF_TYPE_HT_NULLABLE
  10822. || type == REF_TYPE_HT_NON_NULLABLE)
  10823. && wasm_ref_type.ref_ht_common.heap_type
  10824. == HEAP_TYPE_EXTERN)) {
  10825. set_error_buf(error_buf, error_buf_size,
  10826. "type mismatch");
  10827. goto fail;
  10828. }
  10829. if (type != REF_TYPE_HT_NON_NULLABLE) {
  10830. /* push (ref null extern) */
  10831. type = REF_TYPE_EXTERNREF;
  10832. }
  10833. else {
  10834. /* push (ref extern) */
  10835. type = REF_TYPE_HT_NON_NULLABLE;
  10836. wasm_set_refheaptype_common(
  10837. &wasm_ref_type.ref_ht_common, false,
  10838. HEAP_TYPE_EXTERN);
  10839. }
  10840. PUSH_REF(type);
  10841. break;
  10842. }
  10843. default:
  10844. set_error_buf_v(error_buf, error_buf_size,
  10845. "%s %02x %02x", "unsupported opcode",
  10846. 0xfb, opcode1);
  10847. goto fail;
  10848. }
  10849. break;
  10850. }
  10851. #endif /* end of WASM_ENABLE_GC != 0 */
  10852. case WASM_OP_MISC_PREFIX:
  10853. {
  10854. uint32 opcode1;
  10855. read_leb_uint32(p, p_end, opcode1);
  10856. #if WASM_ENABLE_FAST_INTERP != 0
  10857. emit_byte(loader_ctx, ((uint8)opcode1));
  10858. #endif
  10859. switch (opcode1) {
  10860. case WASM_OP_I32_TRUNC_SAT_S_F32:
  10861. case WASM_OP_I32_TRUNC_SAT_U_F32:
  10862. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10863. break;
  10864. case WASM_OP_I32_TRUNC_SAT_S_F64:
  10865. case WASM_OP_I32_TRUNC_SAT_U_F64:
  10866. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10867. break;
  10868. case WASM_OP_I64_TRUNC_SAT_S_F32:
  10869. case WASM_OP_I64_TRUNC_SAT_U_F32:
  10870. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  10871. break;
  10872. case WASM_OP_I64_TRUNC_SAT_S_F64:
  10873. case WASM_OP_I64_TRUNC_SAT_U_F64:
  10874. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10875. break;
  10876. #if WASM_ENABLE_BULK_MEMORY != 0
  10877. case WASM_OP_MEMORY_INIT:
  10878. {
  10879. read_leb_uint32(p, p_end, data_seg_idx);
  10880. #if WASM_ENABLE_FAST_INTERP != 0
  10881. emit_uint32(loader_ctx, data_seg_idx);
  10882. #endif
  10883. if (module->import_memory_count == 0
  10884. && module->memory_count == 0)
  10885. goto fail_unknown_memory;
  10886. if (*p++ != 0x00)
  10887. goto fail_zero_byte_expected;
  10888. if (data_seg_idx >= module->data_seg_count) {
  10889. set_error_buf_v(error_buf, error_buf_size,
  10890. "unknown data segment %d",
  10891. data_seg_idx);
  10892. goto fail;
  10893. }
  10894. if (module->data_seg_count1 == 0)
  10895. goto fail_data_cnt_sec_require;
  10896. POP_I32();
  10897. POP_I32();
  10898. POP_I32();
  10899. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10900. func->has_memory_operations = true;
  10901. #endif
  10902. break;
  10903. }
  10904. case WASM_OP_DATA_DROP:
  10905. {
  10906. read_leb_uint32(p, p_end, data_seg_idx);
  10907. #if WASM_ENABLE_FAST_INTERP != 0
  10908. emit_uint32(loader_ctx, data_seg_idx);
  10909. #endif
  10910. if (data_seg_idx >= module->data_seg_count) {
  10911. set_error_buf(error_buf, error_buf_size,
  10912. "unknown data segment");
  10913. goto fail;
  10914. }
  10915. if (module->data_seg_count1 == 0)
  10916. goto fail_data_cnt_sec_require;
  10917. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10918. func->has_memory_operations = true;
  10919. #endif
  10920. break;
  10921. }
  10922. case WASM_OP_MEMORY_COPY:
  10923. {
  10924. /* both src and dst memory index should be 0 */
  10925. if (*(int16 *)p != 0x0000)
  10926. goto fail_zero_byte_expected;
  10927. p += 2;
  10928. if (module->import_memory_count == 0
  10929. && module->memory_count == 0)
  10930. goto fail_unknown_memory;
  10931. POP_I32();
  10932. POP_I32();
  10933. POP_I32();
  10934. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10935. func->has_memory_operations = true;
  10936. #endif
  10937. break;
  10938. }
  10939. case WASM_OP_MEMORY_FILL:
  10940. {
  10941. if (*p++ != 0x00) {
  10942. goto fail_zero_byte_expected;
  10943. }
  10944. if (module->import_memory_count == 0
  10945. && module->memory_count == 0) {
  10946. goto fail_unknown_memory;
  10947. }
  10948. POP_I32();
  10949. POP_I32();
  10950. POP_I32();
  10951. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10952. func->has_memory_operations = true;
  10953. #endif
  10954. break;
  10955. }
  10956. fail_zero_byte_expected:
  10957. set_error_buf(error_buf, error_buf_size,
  10958. "zero byte expected");
  10959. goto fail;
  10960. fail_unknown_memory:
  10961. set_error_buf(error_buf, error_buf_size,
  10962. "unknown memory 0");
  10963. goto fail;
  10964. fail_data_cnt_sec_require:
  10965. set_error_buf(error_buf, error_buf_size,
  10966. "data count section required");
  10967. goto fail;
  10968. #endif /* WASM_ENABLE_BULK_MEMORY */
  10969. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10970. case WASM_OP_TABLE_INIT:
  10971. {
  10972. uint8 seg_type = 0, tbl_type = 0;
  10973. #if WASM_ENABLE_GC != 0
  10974. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  10975. #endif
  10976. read_leb_uint32(p, p_end, table_seg_idx);
  10977. read_leb_uint32(p, p_end, table_idx);
  10978. if (!get_table_elem_type(module, table_idx, &tbl_type,
  10979. #if WASM_ENABLE_GC != 0
  10980. (void **)&tbl_ref_type,
  10981. #else
  10982. NULL,
  10983. #endif
  10984. error_buf, error_buf_size))
  10985. goto fail;
  10986. if (!get_table_seg_elem_type(module, table_seg_idx,
  10987. &seg_type,
  10988. #if WASM_ENABLE_GC != 0
  10989. (void **)&seg_ref_type,
  10990. #else
  10991. NULL,
  10992. #endif
  10993. error_buf, error_buf_size))
  10994. goto fail;
  10995. #if WASM_ENABLE_GC == 0
  10996. if (seg_type != tbl_type) {
  10997. set_error_buf(error_buf, error_buf_size,
  10998. "type mismatch");
  10999. goto fail;
  11000. }
  11001. #else
  11002. if (!wasm_reftype_is_subtype_of(
  11003. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  11004. module->types, module->type_count)) {
  11005. set_error_buf(error_buf, error_buf_size,
  11006. "type mismatch");
  11007. goto fail;
  11008. }
  11009. #endif
  11010. #if WASM_ENABLE_FAST_INTERP != 0
  11011. emit_uint32(loader_ctx, table_seg_idx);
  11012. emit_uint32(loader_ctx, table_idx);
  11013. #endif
  11014. POP_I32();
  11015. POP_I32();
  11016. POP_I32();
  11017. break;
  11018. }
  11019. case WASM_OP_ELEM_DROP:
  11020. {
  11021. read_leb_uint32(p, p_end, table_seg_idx);
  11022. if (!get_table_seg_elem_type(module, table_seg_idx,
  11023. NULL, NULL, error_buf,
  11024. error_buf_size))
  11025. goto fail;
  11026. #if WASM_ENABLE_FAST_INTERP != 0
  11027. emit_uint32(loader_ctx, table_seg_idx);
  11028. #endif
  11029. break;
  11030. }
  11031. case WASM_OP_TABLE_COPY:
  11032. {
  11033. uint8 src_type, dst_type;
  11034. #if WASM_ENABLE_GC != 0
  11035. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  11036. #endif
  11037. uint32 src_tbl_idx, dst_tbl_idx;
  11038. read_leb_uint32(p, p_end, dst_tbl_idx);
  11039. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  11040. #if WASM_ENABLE_GC != 0
  11041. (void **)&dst_ref_type,
  11042. #else
  11043. NULL,
  11044. #endif
  11045. error_buf, error_buf_size))
  11046. goto fail;
  11047. read_leb_uint32(p, p_end, src_tbl_idx);
  11048. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  11049. #if WASM_ENABLE_GC != 0
  11050. (void **)&src_ref_type,
  11051. #else
  11052. NULL,
  11053. #endif
  11054. error_buf, error_buf_size))
  11055. goto fail;
  11056. #if WASM_ENABLE_GC == 0
  11057. if (src_type != dst_type) {
  11058. set_error_buf(error_buf, error_buf_size,
  11059. "type mismatch");
  11060. goto fail;
  11061. }
  11062. #else
  11063. if (!wasm_reftype_is_subtype_of(
  11064. src_type, src_ref_type, dst_type, dst_ref_type,
  11065. module->types, module->type_count)) {
  11066. set_error_buf(error_buf, error_buf_size,
  11067. "type mismatch");
  11068. goto fail;
  11069. }
  11070. #endif
  11071. #if WASM_ENABLE_FAST_INTERP != 0
  11072. emit_uint32(loader_ctx, dst_tbl_idx);
  11073. emit_uint32(loader_ctx, src_tbl_idx);
  11074. #endif
  11075. POP_I32();
  11076. POP_I32();
  11077. POP_I32();
  11078. break;
  11079. }
  11080. case WASM_OP_TABLE_SIZE:
  11081. {
  11082. read_leb_uint32(p, p_end, table_idx);
  11083. /* TODO: shall we create a new function to check
  11084. table idx instead of using below function? */
  11085. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  11086. error_buf, error_buf_size))
  11087. goto fail;
  11088. #if WASM_ENABLE_FAST_INTERP != 0
  11089. emit_uint32(loader_ctx, table_idx);
  11090. #endif
  11091. PUSH_I32();
  11092. break;
  11093. }
  11094. case WASM_OP_TABLE_GROW:
  11095. case WASM_OP_TABLE_FILL:
  11096. {
  11097. uint8 decl_type;
  11098. #if WASM_ENABLE_GC != 0
  11099. WASMRefType *ref_type = NULL;
  11100. #endif
  11101. read_leb_uint32(p, p_end, table_idx);
  11102. if (!get_table_elem_type(module, table_idx, &decl_type,
  11103. #if WASM_ENABLE_GC != 0
  11104. (void **)&ref_type,
  11105. #else
  11106. NULL,
  11107. #endif
  11108. error_buf, error_buf_size))
  11109. goto fail;
  11110. #if WASM_ENABLE_GC != 0
  11111. if (wasm_is_type_multi_byte_type(decl_type)) {
  11112. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11113. ref_type,
  11114. wasm_reftype_struct_size(ref_type));
  11115. }
  11116. #endif
  11117. if (opcode1 == WASM_OP_TABLE_GROW) {
  11118. if (table_idx < module->import_table_count) {
  11119. module->import_tables[table_idx]
  11120. .u.table.possible_grow = true;
  11121. }
  11122. else {
  11123. module
  11124. ->tables[table_idx
  11125. - module->import_table_count]
  11126. .possible_grow = true;
  11127. }
  11128. }
  11129. #if WASM_ENABLE_FAST_INTERP != 0
  11130. emit_uint32(loader_ctx, table_idx);
  11131. #endif
  11132. POP_I32();
  11133. #if WASM_ENABLE_FAST_INTERP != 0
  11134. POP_OFFSET_TYPE(decl_type);
  11135. #endif
  11136. POP_TYPE(decl_type);
  11137. if (opcode1 == WASM_OP_TABLE_GROW)
  11138. PUSH_I32();
  11139. else
  11140. POP_I32();
  11141. break;
  11142. }
  11143. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11144. default:
  11145. set_error_buf_v(error_buf, error_buf_size,
  11146. "%s %02x %02x", "unsupported opcode",
  11147. 0xfc, opcode1);
  11148. goto fail;
  11149. }
  11150. break;
  11151. }
  11152. #if WASM_ENABLE_SIMD != 0
  11153. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11154. case WASM_OP_SIMD_PREFIX:
  11155. {
  11156. uint32 opcode1;
  11157. opcode1 = read_uint8(p);
  11158. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  11159. */
  11160. switch (opcode1) {
  11161. /* memory instruction */
  11162. case SIMD_v128_load:
  11163. case SIMD_v128_load8x8_s:
  11164. case SIMD_v128_load8x8_u:
  11165. case SIMD_v128_load16x4_s:
  11166. case SIMD_v128_load16x4_u:
  11167. case SIMD_v128_load32x2_s:
  11168. case SIMD_v128_load32x2_u:
  11169. case SIMD_v128_load8_splat:
  11170. case SIMD_v128_load16_splat:
  11171. case SIMD_v128_load32_splat:
  11172. case SIMD_v128_load64_splat:
  11173. {
  11174. CHECK_MEMORY();
  11175. read_leb_uint32(p, p_end, align); /* align */
  11176. if (!check_simd_memory_access_align(
  11177. opcode1, align, error_buf, error_buf_size)) {
  11178. goto fail;
  11179. }
  11180. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11181. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  11182. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11183. func->has_memory_operations = true;
  11184. #endif
  11185. break;
  11186. }
  11187. case SIMD_v128_store:
  11188. {
  11189. CHECK_MEMORY();
  11190. read_leb_uint32(p, p_end, align); /* align */
  11191. if (!check_simd_memory_access_align(
  11192. opcode1, align, error_buf, error_buf_size)) {
  11193. goto fail;
  11194. }
  11195. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11196. POP_V128();
  11197. POP_I32();
  11198. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11199. func->has_memory_operations = true;
  11200. #endif
  11201. break;
  11202. }
  11203. /* basic operation */
  11204. case SIMD_v128_const:
  11205. {
  11206. CHECK_BUF1(p, p_end, 16);
  11207. p += 16;
  11208. PUSH_V128();
  11209. break;
  11210. }
  11211. case SIMD_v8x16_shuffle:
  11212. {
  11213. V128 mask;
  11214. CHECK_BUF1(p, p_end, 16);
  11215. mask = read_i8x16(p, error_buf, error_buf_size);
  11216. p += 16;
  11217. if (!check_simd_shuffle_mask(mask, error_buf,
  11218. error_buf_size)) {
  11219. goto fail;
  11220. }
  11221. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11222. break;
  11223. }
  11224. case SIMD_v8x16_swizzle:
  11225. {
  11226. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11227. break;
  11228. }
  11229. /* splat operation */
  11230. case SIMD_i8x16_splat:
  11231. case SIMD_i16x8_splat:
  11232. case SIMD_i32x4_splat:
  11233. case SIMD_i64x2_splat:
  11234. case SIMD_f32x4_splat:
  11235. case SIMD_f64x2_splat:
  11236. {
  11237. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  11238. VALUE_TYPE_I32, VALUE_TYPE_I64,
  11239. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  11240. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  11241. VALUE_TYPE_V128);
  11242. break;
  11243. }
  11244. /* lane operation */
  11245. case SIMD_i8x16_extract_lane_s:
  11246. case SIMD_i8x16_extract_lane_u:
  11247. case SIMD_i8x16_replace_lane:
  11248. case SIMD_i16x8_extract_lane_s:
  11249. case SIMD_i16x8_extract_lane_u:
  11250. case SIMD_i16x8_replace_lane:
  11251. case SIMD_i32x4_extract_lane:
  11252. case SIMD_i32x4_replace_lane:
  11253. case SIMD_i64x2_extract_lane:
  11254. case SIMD_i64x2_replace_lane:
  11255. case SIMD_f32x4_extract_lane:
  11256. case SIMD_f32x4_replace_lane:
  11257. case SIMD_f64x2_extract_lane:
  11258. case SIMD_f64x2_replace_lane:
  11259. {
  11260. uint8 lane;
  11261. /* clang-format off */
  11262. uint8 replace[] = {
  11263. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  11264. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  11265. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  11266. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  11267. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  11268. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  11269. };
  11270. uint8 push_type[] = {
  11271. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  11272. VALUE_TYPE_V128,
  11273. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  11274. VALUE_TYPE_V128,
  11275. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  11276. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  11277. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  11278. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  11279. };
  11280. /* clang-format on */
  11281. CHECK_BUF(p, p_end, 1);
  11282. lane = read_uint8(p);
  11283. if (!check_simd_access_lane(opcode1, lane, error_buf,
  11284. error_buf_size)) {
  11285. goto fail;
  11286. }
  11287. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  11288. if (!(wasm_loader_pop_frame_ref(
  11289. loader_ctx,
  11290. replace[opcode1
  11291. - SIMD_i8x16_extract_lane_s],
  11292. error_buf, error_buf_size)))
  11293. goto fail;
  11294. }
  11295. POP_AND_PUSH(
  11296. VALUE_TYPE_V128,
  11297. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  11298. break;
  11299. }
  11300. /* i8x16 compare operation */
  11301. case SIMD_i8x16_eq:
  11302. case SIMD_i8x16_ne:
  11303. case SIMD_i8x16_lt_s:
  11304. case SIMD_i8x16_lt_u:
  11305. case SIMD_i8x16_gt_s:
  11306. case SIMD_i8x16_gt_u:
  11307. case SIMD_i8x16_le_s:
  11308. case SIMD_i8x16_le_u:
  11309. case SIMD_i8x16_ge_s:
  11310. case SIMD_i8x16_ge_u:
  11311. /* i16x8 compare operation */
  11312. case SIMD_i16x8_eq:
  11313. case SIMD_i16x8_ne:
  11314. case SIMD_i16x8_lt_s:
  11315. case SIMD_i16x8_lt_u:
  11316. case SIMD_i16x8_gt_s:
  11317. case SIMD_i16x8_gt_u:
  11318. case SIMD_i16x8_le_s:
  11319. case SIMD_i16x8_le_u:
  11320. case SIMD_i16x8_ge_s:
  11321. case SIMD_i16x8_ge_u:
  11322. /* i32x4 compare operation */
  11323. case SIMD_i32x4_eq:
  11324. case SIMD_i32x4_ne:
  11325. case SIMD_i32x4_lt_s:
  11326. case SIMD_i32x4_lt_u:
  11327. case SIMD_i32x4_gt_s:
  11328. case SIMD_i32x4_gt_u:
  11329. case SIMD_i32x4_le_s:
  11330. case SIMD_i32x4_le_u:
  11331. case SIMD_i32x4_ge_s:
  11332. case SIMD_i32x4_ge_u:
  11333. /* f32x4 compare operation */
  11334. case SIMD_f32x4_eq:
  11335. case SIMD_f32x4_ne:
  11336. case SIMD_f32x4_lt:
  11337. case SIMD_f32x4_gt:
  11338. case SIMD_f32x4_le:
  11339. case SIMD_f32x4_ge:
  11340. /* f64x2 compare operation */
  11341. case SIMD_f64x2_eq:
  11342. case SIMD_f64x2_ne:
  11343. case SIMD_f64x2_lt:
  11344. case SIMD_f64x2_gt:
  11345. case SIMD_f64x2_le:
  11346. case SIMD_f64x2_ge:
  11347. {
  11348. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11349. break;
  11350. }
  11351. /* v128 operation */
  11352. case SIMD_v128_not:
  11353. {
  11354. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11355. break;
  11356. }
  11357. case SIMD_v128_and:
  11358. case SIMD_v128_andnot:
  11359. case SIMD_v128_or:
  11360. case SIMD_v128_xor:
  11361. {
  11362. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11363. break;
  11364. }
  11365. case SIMD_v128_bitselect:
  11366. {
  11367. POP_V128();
  11368. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11369. break;
  11370. }
  11371. case SIMD_v128_any_true:
  11372. {
  11373. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11374. break;
  11375. }
  11376. /* Load Lane Operation */
  11377. case SIMD_v128_load8_lane:
  11378. case SIMD_v128_load16_lane:
  11379. case SIMD_v128_load32_lane:
  11380. case SIMD_v128_load64_lane:
  11381. case SIMD_v128_store8_lane:
  11382. case SIMD_v128_store16_lane:
  11383. case SIMD_v128_store32_lane:
  11384. case SIMD_v128_store64_lane:
  11385. {
  11386. uint8 lane;
  11387. CHECK_MEMORY();
  11388. read_leb_uint32(p, p_end, align); /* align */
  11389. if (!check_simd_memory_access_align(
  11390. opcode1, align, error_buf, error_buf_size)) {
  11391. goto fail;
  11392. }
  11393. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11394. CHECK_BUF(p, p_end, 1);
  11395. lane = read_uint8(p);
  11396. if (!check_simd_access_lane(opcode1, lane, error_buf,
  11397. error_buf_size)) {
  11398. goto fail;
  11399. }
  11400. POP_V128();
  11401. POP_I32();
  11402. if (opcode1 < SIMD_v128_store8_lane) {
  11403. PUSH_V128();
  11404. }
  11405. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11406. func->has_memory_operations = true;
  11407. #endif
  11408. break;
  11409. }
  11410. case SIMD_v128_load32_zero:
  11411. case SIMD_v128_load64_zero:
  11412. {
  11413. CHECK_MEMORY();
  11414. read_leb_uint32(p, p_end, align); /* align */
  11415. if (!check_simd_memory_access_align(
  11416. opcode1, align, error_buf, error_buf_size)) {
  11417. goto fail;
  11418. }
  11419. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11420. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  11421. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11422. func->has_memory_operations = true;
  11423. #endif
  11424. break;
  11425. }
  11426. /* Float conversion */
  11427. case SIMD_f32x4_demote_f64x2_zero:
  11428. case SIMD_f64x2_promote_low_f32x4_zero:
  11429. {
  11430. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11431. break;
  11432. }
  11433. /* i8x16 Operation */
  11434. case SIMD_i8x16_abs:
  11435. case SIMD_i8x16_neg:
  11436. case SIMD_i8x16_popcnt:
  11437. {
  11438. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11439. break;
  11440. }
  11441. case SIMD_i8x16_all_true:
  11442. case SIMD_i8x16_bitmask:
  11443. {
  11444. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11445. break;
  11446. }
  11447. case SIMD_i8x16_narrow_i16x8_s:
  11448. case SIMD_i8x16_narrow_i16x8_u:
  11449. {
  11450. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11451. break;
  11452. }
  11453. case SIMD_f32x4_ceil:
  11454. case SIMD_f32x4_floor:
  11455. case SIMD_f32x4_trunc:
  11456. case SIMD_f32x4_nearest:
  11457. {
  11458. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11459. break;
  11460. }
  11461. case SIMD_i8x16_shl:
  11462. case SIMD_i8x16_shr_s:
  11463. case SIMD_i8x16_shr_u:
  11464. {
  11465. POP_I32();
  11466. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11467. break;
  11468. }
  11469. case SIMD_i8x16_add:
  11470. case SIMD_i8x16_add_sat_s:
  11471. case SIMD_i8x16_add_sat_u:
  11472. case SIMD_i8x16_sub:
  11473. case SIMD_i8x16_sub_sat_s:
  11474. case SIMD_i8x16_sub_sat_u:
  11475. {
  11476. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11477. break;
  11478. }
  11479. case SIMD_f64x2_ceil:
  11480. case SIMD_f64x2_floor:
  11481. {
  11482. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11483. break;
  11484. }
  11485. case SIMD_i8x16_min_s:
  11486. case SIMD_i8x16_min_u:
  11487. case SIMD_i8x16_max_s:
  11488. case SIMD_i8x16_max_u:
  11489. {
  11490. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11491. break;
  11492. }
  11493. case SIMD_f64x2_trunc:
  11494. {
  11495. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11496. break;
  11497. }
  11498. case SIMD_i8x16_avgr_u:
  11499. {
  11500. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11501. break;
  11502. }
  11503. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  11504. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  11505. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  11506. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  11507. /* i16x8 operation */
  11508. case SIMD_i16x8_abs:
  11509. case SIMD_i16x8_neg:
  11510. {
  11511. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11512. break;
  11513. }
  11514. case SIMD_i16x8_q15mulr_sat_s:
  11515. {
  11516. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11517. break;
  11518. }
  11519. case SIMD_i16x8_all_true:
  11520. case SIMD_i16x8_bitmask:
  11521. {
  11522. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11523. break;
  11524. }
  11525. case SIMD_i16x8_narrow_i32x4_s:
  11526. case SIMD_i16x8_narrow_i32x4_u:
  11527. {
  11528. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11529. break;
  11530. }
  11531. case SIMD_i16x8_extend_low_i8x16_s:
  11532. case SIMD_i16x8_extend_high_i8x16_s:
  11533. case SIMD_i16x8_extend_low_i8x16_u:
  11534. case SIMD_i16x8_extend_high_i8x16_u:
  11535. {
  11536. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11537. break;
  11538. }
  11539. case SIMD_i16x8_shl:
  11540. case SIMD_i16x8_shr_s:
  11541. case SIMD_i16x8_shr_u:
  11542. {
  11543. POP_I32();
  11544. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11545. break;
  11546. }
  11547. case SIMD_i16x8_add:
  11548. case SIMD_i16x8_add_sat_s:
  11549. case SIMD_i16x8_add_sat_u:
  11550. case SIMD_i16x8_sub:
  11551. case SIMD_i16x8_sub_sat_s:
  11552. case SIMD_i16x8_sub_sat_u:
  11553. {
  11554. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11555. break;
  11556. }
  11557. case SIMD_f64x2_nearest:
  11558. {
  11559. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11560. break;
  11561. }
  11562. case SIMD_i16x8_mul:
  11563. case SIMD_i16x8_min_s:
  11564. case SIMD_i16x8_min_u:
  11565. case SIMD_i16x8_max_s:
  11566. case SIMD_i16x8_max_u:
  11567. case SIMD_i16x8_avgr_u:
  11568. case SIMD_i16x8_extmul_low_i8x16_s:
  11569. case SIMD_i16x8_extmul_high_i8x16_s:
  11570. case SIMD_i16x8_extmul_low_i8x16_u:
  11571. case SIMD_i16x8_extmul_high_i8x16_u:
  11572. {
  11573. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11574. break;
  11575. }
  11576. /* i32x4 operation */
  11577. case SIMD_i32x4_abs:
  11578. case SIMD_i32x4_neg:
  11579. {
  11580. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11581. break;
  11582. }
  11583. case SIMD_i32x4_all_true:
  11584. case SIMD_i32x4_bitmask:
  11585. {
  11586. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11587. break;
  11588. }
  11589. case SIMD_i32x4_narrow_i64x2_s:
  11590. case SIMD_i32x4_narrow_i64x2_u:
  11591. {
  11592. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11593. break;
  11594. }
  11595. case SIMD_i32x4_extend_low_i16x8_s:
  11596. case SIMD_i32x4_extend_high_i16x8_s:
  11597. case SIMD_i32x4_extend_low_i16x8_u:
  11598. case SIMD_i32x4_extend_high_i16x8_u:
  11599. {
  11600. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11601. break;
  11602. }
  11603. case SIMD_i32x4_shl:
  11604. case SIMD_i32x4_shr_s:
  11605. case SIMD_i32x4_shr_u:
  11606. {
  11607. POP_I32();
  11608. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11609. break;
  11610. }
  11611. case SIMD_i32x4_add:
  11612. case SIMD_i32x4_sub:
  11613. case SIMD_i32x4_mul:
  11614. case SIMD_i32x4_min_s:
  11615. case SIMD_i32x4_min_u:
  11616. case SIMD_i32x4_max_s:
  11617. case SIMD_i32x4_max_u:
  11618. case SIMD_i32x4_dot_i16x8_s:
  11619. case SIMD_i32x4_avgr_u:
  11620. case SIMD_i32x4_extmul_low_i16x8_s:
  11621. case SIMD_i32x4_extmul_high_i16x8_s:
  11622. case SIMD_i32x4_extmul_low_i16x8_u:
  11623. case SIMD_i32x4_extmul_high_i16x8_u:
  11624. {
  11625. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11626. break;
  11627. }
  11628. /* i64x2 operation */
  11629. case SIMD_i64x2_abs:
  11630. case SIMD_i64x2_neg:
  11631. {
  11632. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11633. break;
  11634. }
  11635. case SIMD_i64x2_all_true:
  11636. case SIMD_i64x2_bitmask:
  11637. {
  11638. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11639. break;
  11640. }
  11641. case SIMD_i64x2_extend_low_i32x4_s:
  11642. case SIMD_i64x2_extend_high_i32x4_s:
  11643. case SIMD_i64x2_extend_low_i32x4_u:
  11644. case SIMD_i64x2_extend_high_i32x4_u:
  11645. {
  11646. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11647. break;
  11648. }
  11649. case SIMD_i64x2_shl:
  11650. case SIMD_i64x2_shr_s:
  11651. case SIMD_i64x2_shr_u:
  11652. {
  11653. POP_I32();
  11654. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11655. break;
  11656. }
  11657. case SIMD_i64x2_add:
  11658. case SIMD_i64x2_sub:
  11659. case SIMD_i64x2_mul:
  11660. case SIMD_i64x2_eq:
  11661. case SIMD_i64x2_ne:
  11662. case SIMD_i64x2_lt_s:
  11663. case SIMD_i64x2_gt_s:
  11664. case SIMD_i64x2_le_s:
  11665. case SIMD_i64x2_ge_s:
  11666. case SIMD_i64x2_extmul_low_i32x4_s:
  11667. case SIMD_i64x2_extmul_high_i32x4_s:
  11668. case SIMD_i64x2_extmul_low_i32x4_u:
  11669. case SIMD_i64x2_extmul_high_i32x4_u:
  11670. {
  11671. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11672. break;
  11673. }
  11674. /* f32x4 operation */
  11675. case SIMD_f32x4_abs:
  11676. case SIMD_f32x4_neg:
  11677. case SIMD_f32x4_round:
  11678. case SIMD_f32x4_sqrt:
  11679. {
  11680. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11681. break;
  11682. }
  11683. case SIMD_f32x4_add:
  11684. case SIMD_f32x4_sub:
  11685. case SIMD_f32x4_mul:
  11686. case SIMD_f32x4_div:
  11687. case SIMD_f32x4_min:
  11688. case SIMD_f32x4_max:
  11689. case SIMD_f32x4_pmin:
  11690. case SIMD_f32x4_pmax:
  11691. {
  11692. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11693. break;
  11694. }
  11695. /* f64x2 operation */
  11696. case SIMD_f64x2_abs:
  11697. case SIMD_f64x2_neg:
  11698. case SIMD_f64x2_round:
  11699. case SIMD_f64x2_sqrt:
  11700. {
  11701. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11702. break;
  11703. }
  11704. case SIMD_f64x2_add:
  11705. case SIMD_f64x2_sub:
  11706. case SIMD_f64x2_mul:
  11707. case SIMD_f64x2_div:
  11708. case SIMD_f64x2_min:
  11709. case SIMD_f64x2_max:
  11710. case SIMD_f64x2_pmin:
  11711. case SIMD_f64x2_pmax:
  11712. {
  11713. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11714. break;
  11715. }
  11716. case SIMD_i32x4_trunc_sat_f32x4_s:
  11717. case SIMD_i32x4_trunc_sat_f32x4_u:
  11718. case SIMD_f32x4_convert_i32x4_s:
  11719. case SIMD_f32x4_convert_i32x4_u:
  11720. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  11721. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  11722. case SIMD_f64x2_convert_low_i32x4_s:
  11723. case SIMD_f64x2_convert_low_i32x4_u:
  11724. {
  11725. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11726. break;
  11727. }
  11728. default:
  11729. {
  11730. if (error_buf != NULL) {
  11731. snprintf(error_buf, error_buf_size,
  11732. "WASM module load failed: "
  11733. "invalid opcode 0xfd %02x.",
  11734. opcode1);
  11735. }
  11736. goto fail;
  11737. }
  11738. }
  11739. break;
  11740. }
  11741. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11742. #endif /* end of WASM_ENABLE_SIMD */
  11743. #if WASM_ENABLE_SHARED_MEMORY != 0
  11744. case WASM_OP_ATOMIC_PREFIX:
  11745. {
  11746. uint32 opcode1;
  11747. opcode1 = read_uint8(p);
  11748. #if WASM_ENABLE_FAST_INTERP != 0
  11749. emit_byte(loader_ctx, opcode1);
  11750. #endif
  11751. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  11752. CHECK_MEMORY();
  11753. read_leb_uint32(p, p_end, align); /* align */
  11754. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11755. if (!check_memory_align_equal(opcode1, align, error_buf,
  11756. error_buf_size)) {
  11757. goto fail;
  11758. }
  11759. #if WASM_ENABLE_FAST_INTERP != 0
  11760. emit_uint32(loader_ctx, mem_offset);
  11761. #endif
  11762. }
  11763. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11764. func->has_memory_operations = true;
  11765. #endif
  11766. switch (opcode1) {
  11767. case WASM_OP_ATOMIC_NOTIFY:
  11768. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11769. break;
  11770. case WASM_OP_ATOMIC_WAIT32:
  11771. POP_I64();
  11772. POP_I32();
  11773. POP_I32();
  11774. PUSH_I32();
  11775. break;
  11776. case WASM_OP_ATOMIC_WAIT64:
  11777. POP_I64();
  11778. POP_I64();
  11779. POP_I32();
  11780. PUSH_I32();
  11781. break;
  11782. case WASM_OP_ATOMIC_FENCE:
  11783. /* reserved byte 0x00 */
  11784. if (*p++ != 0x00) {
  11785. set_error_buf(error_buf, error_buf_size,
  11786. "zero byte expected");
  11787. goto fail;
  11788. }
  11789. break;
  11790. case WASM_OP_ATOMIC_I32_LOAD:
  11791. case WASM_OP_ATOMIC_I32_LOAD8_U:
  11792. case WASM_OP_ATOMIC_I32_LOAD16_U:
  11793. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11794. break;
  11795. case WASM_OP_ATOMIC_I32_STORE:
  11796. case WASM_OP_ATOMIC_I32_STORE8:
  11797. case WASM_OP_ATOMIC_I32_STORE16:
  11798. POP_I32();
  11799. POP_I32();
  11800. break;
  11801. case WASM_OP_ATOMIC_I64_LOAD:
  11802. case WASM_OP_ATOMIC_I64_LOAD8_U:
  11803. case WASM_OP_ATOMIC_I64_LOAD16_U:
  11804. case WASM_OP_ATOMIC_I64_LOAD32_U:
  11805. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11806. break;
  11807. case WASM_OP_ATOMIC_I64_STORE:
  11808. case WASM_OP_ATOMIC_I64_STORE8:
  11809. case WASM_OP_ATOMIC_I64_STORE16:
  11810. case WASM_OP_ATOMIC_I64_STORE32:
  11811. POP_I64();
  11812. POP_I32();
  11813. break;
  11814. case WASM_OP_ATOMIC_RMW_I32_ADD:
  11815. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  11816. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  11817. case WASM_OP_ATOMIC_RMW_I32_SUB:
  11818. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  11819. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  11820. case WASM_OP_ATOMIC_RMW_I32_AND:
  11821. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  11822. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  11823. case WASM_OP_ATOMIC_RMW_I32_OR:
  11824. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  11825. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  11826. case WASM_OP_ATOMIC_RMW_I32_XOR:
  11827. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  11828. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  11829. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  11830. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  11831. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  11832. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11833. break;
  11834. case WASM_OP_ATOMIC_RMW_I64_ADD:
  11835. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  11836. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  11837. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  11838. case WASM_OP_ATOMIC_RMW_I64_SUB:
  11839. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  11840. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  11841. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  11842. case WASM_OP_ATOMIC_RMW_I64_AND:
  11843. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  11844. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  11845. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  11846. case WASM_OP_ATOMIC_RMW_I64_OR:
  11847. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  11848. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  11849. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  11850. case WASM_OP_ATOMIC_RMW_I64_XOR:
  11851. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  11852. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  11853. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  11854. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  11855. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  11856. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  11857. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  11858. POP_I64();
  11859. POP_I32();
  11860. PUSH_I64();
  11861. break;
  11862. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  11863. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  11864. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  11865. POP_I32();
  11866. POP_I32();
  11867. POP_I32();
  11868. PUSH_I32();
  11869. break;
  11870. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  11871. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  11872. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  11873. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  11874. POP_I64();
  11875. POP_I64();
  11876. POP_I32();
  11877. PUSH_I64();
  11878. break;
  11879. default:
  11880. set_error_buf_v(error_buf, error_buf_size,
  11881. "%s %02x %02x", "unsupported opcode",
  11882. 0xfe, opcode1);
  11883. goto fail;
  11884. }
  11885. break;
  11886. }
  11887. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  11888. default:
  11889. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  11890. "unsupported opcode", opcode);
  11891. goto fail;
  11892. }
  11893. #if WASM_ENABLE_FAST_INTERP != 0
  11894. last_op = opcode;
  11895. #endif
  11896. }
  11897. if (loader_ctx->csp_num > 0) {
  11898. if (cur_func_idx < module->function_count - 1)
  11899. /* Function with missing end marker (between two functions) */
  11900. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  11901. else
  11902. /* Function with missing end marker
  11903. (at EOF or end of code sections) */
  11904. set_error_buf(error_buf, error_buf_size,
  11905. "unexpected end of section or function, "
  11906. "or section size mismatch");
  11907. goto fail;
  11908. }
  11909. #if WASM_ENABLE_FAST_INTERP != 0
  11910. if (loader_ctx->p_code_compiled == NULL)
  11911. goto re_scan;
  11912. func->const_cell_num = loader_ctx->const_cell_num;
  11913. if (func->const_cell_num > 0) {
  11914. int32 j;
  11915. if (!(func->consts = func_const = loader_malloc(
  11916. func->const_cell_num * 4, error_buf, error_buf_size)))
  11917. goto fail;
  11918. func_const_end = func->consts + func->const_cell_num * 4;
  11919. /* reverse the const buf */
  11920. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  11921. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  11922. if (c->value_type == VALUE_TYPE_F64
  11923. || c->value_type == VALUE_TYPE_I64) {
  11924. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  11925. &(c->value.f64), (uint32)sizeof(int64));
  11926. func_const += sizeof(int64);
  11927. }
  11928. else {
  11929. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  11930. &(c->value.f32), (uint32)sizeof(int32));
  11931. func_const += sizeof(int32);
  11932. }
  11933. }
  11934. }
  11935. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  11936. - loader_ctx->start_dynamic_offset + 1;
  11937. #else
  11938. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  11939. #endif
  11940. func->max_block_num = loader_ctx->max_csp_num;
  11941. return_value = true;
  11942. fail:
  11943. wasm_loader_ctx_destroy(loader_ctx);
  11944. (void)table_idx;
  11945. (void)table_seg_idx;
  11946. (void)data_seg_idx;
  11947. (void)i64_const;
  11948. (void)local_offset;
  11949. (void)p_org;
  11950. (void)mem_offset;
  11951. (void)align;
  11952. return return_value;
  11953. }