wasm_loader.c 473 KB

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