wasm_mini_loader.c 318 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723
  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. #include "wasm_loader_common.h"
  14. #if WASM_ENABLE_FAST_JIT != 0
  15. #include "../fast-jit/jit_compiler.h"
  16. #include "../fast-jit/jit_codecache.h"
  17. #endif
  18. #if WASM_ENABLE_JIT != 0
  19. #include "../compilation/aot_llvm.h"
  20. #endif
  21. /* Read a value of given type from the address pointed to by the given
  22. pointer and increase the pointer to the position just after the
  23. value being read. */
  24. #define TEMPLATE_READ_VALUE(Type, p) \
  25. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  26. #if WASM_ENABLE_MEMORY64 != 0
  27. static bool
  28. has_module_memory64(WASMModule *module)
  29. {
  30. /* TODO: multi-memories for now assuming the memory idx type is consistent
  31. * across multi-memories */
  32. if (module->import_memory_count > 0)
  33. return !!(module->import_memories[0].u.memory.mem_type.flags
  34. & MEMORY64_FLAG);
  35. else if (module->memory_count > 0)
  36. return !!(module->memories[0].flags & MEMORY64_FLAG);
  37. return false;
  38. }
  39. static bool
  40. is_table_64bit(WASMModule *module, uint32 table_idx)
  41. {
  42. if (table_idx < module->import_table_count)
  43. return !!(module->import_tables[table_idx].u.table.table_type.flags
  44. & TABLE64_FLAG);
  45. else
  46. return !!(module->tables[table_idx - module->import_table_count]
  47. .table_type.flags
  48. & TABLE64_FLAG);
  49. return false;
  50. }
  51. #endif
  52. static void
  53. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  54. {
  55. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  56. }
  57. #define CHECK_BUF(buf, buf_end, length) \
  58. do { \
  59. bh_assert(buf + length >= buf && buf + length <= buf_end); \
  60. } while (0)
  61. #define CHECK_BUF1(buf, buf_end, length) \
  62. do { \
  63. bh_assert(buf + length >= buf && buf + length <= buf_end); \
  64. } while (0)
  65. #define skip_leb(p) while (*p++ & 0x80)
  66. #define skip_leb_int64(p, p_end) skip_leb(p)
  67. #define skip_leb_uint32(p, p_end) skip_leb(p)
  68. #define skip_leb_int32(p, p_end) skip_leb(p)
  69. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  70. #define skip_leb_memidx(p, p_end) skip_leb(p)
  71. #if WASM_ENABLE_MULTI_MEMORY == 0
  72. #define skip_leb_align(p, p_end) skip_leb(p)
  73. #else
  74. /* Skip the following memidx if applicable */
  75. #define skip_leb_align(p, p_end) \
  76. do { \
  77. if (*p++ & OPT_MEMIDX_FLAG) \
  78. skip_leb_uint32(p, p_end); \
  79. } while (0)
  80. #endif
  81. static bool
  82. is_32bit_type(uint8 type)
  83. {
  84. if (type == VALUE_TYPE_I32
  85. || type == VALUE_TYPE_F32
  86. /* the operand stack is in polymorphic state */
  87. || type == VALUE_TYPE_ANY
  88. #if WASM_ENABLE_REF_TYPES != 0
  89. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  90. #endif
  91. )
  92. return true;
  93. return false;
  94. }
  95. static bool
  96. is_64bit_type(uint8 type)
  97. {
  98. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  99. return true;
  100. return false;
  101. }
  102. static bool
  103. is_byte_a_type(uint8 type)
  104. {
  105. return is_valid_value_type_for_interpreter(type)
  106. || (type == VALUE_TYPE_VOID);
  107. }
  108. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  109. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  110. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  111. #define read_leb_int64(p, p_end, res) \
  112. do { \
  113. uint64 res64; \
  114. read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  115. error_buf_size); \
  116. res = (int64)res64; \
  117. } while (0)
  118. #define read_leb_uint32(p, p_end, res) \
  119. do { \
  120. uint64 res64; \
  121. read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  122. error_buf_size); \
  123. res = (uint32)res64; \
  124. } while (0)
  125. #define read_leb_int32(p, p_end, res) \
  126. do { \
  127. uint64 res64; \
  128. read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  129. error_buf_size); \
  130. res = (int32)res64; \
  131. } while (0)
  132. #if WASM_ENABLE_MEMORY64 != 0
  133. #define read_leb_mem_offset(p, p_end, res) \
  134. do { \
  135. uint64 res64; \
  136. read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, &res64, \
  137. error_buf, error_buf_size); \
  138. res = (mem_offset_t)res64; \
  139. } while (0)
  140. #else
  141. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  142. #endif
  143. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  144. #if WASM_ENABLE_MULTI_MEMORY != 0
  145. #define check_memidx(module, memidx) \
  146. do { \
  147. bh_assert(memidx \
  148. < module->import_memory_count + module->memory_count); \
  149. (void)memidx; \
  150. } while (0)
  151. /* Bit 6 indicating the optional memidx, and reset bit 6 for
  152. * alignment check */
  153. #define read_leb_memarg(p, p_end, res) \
  154. do { \
  155. read_leb_uint32(p, p_end, res); \
  156. if (res & OPT_MEMIDX_FLAG) { \
  157. res &= ~OPT_MEMIDX_FLAG; \
  158. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  159. check_memidx(module, memidx); \
  160. } \
  161. } while (0)
  162. #else
  163. /* reserved byte 0x00 */
  164. #define check_memidx(module, memidx) \
  165. do { \
  166. (void)module; \
  167. bh_assert(memidx == 0); \
  168. (void)memidx; \
  169. } while (0)
  170. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  171. #endif
  172. static void *
  173. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  174. {
  175. void *mem;
  176. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  177. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  178. return NULL;
  179. }
  180. memset(mem, 0, (uint32)size);
  181. return mem;
  182. }
  183. static void *
  184. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  185. uint32 error_buf_size)
  186. {
  187. uint8 *mem_new;
  188. bh_assert(size_new > size_old);
  189. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  190. memset(mem_new + size_old, 0, size_new - size_old);
  191. return mem_new;
  192. }
  193. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  194. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  195. wasm_runtime_free(mem_old);
  196. }
  197. return mem_new;
  198. }
  199. #define MEM_REALLOC(mem, size_old, size_new) \
  200. do { \
  201. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  202. error_buf_size); \
  203. if (!mem_new) \
  204. goto fail; \
  205. mem = mem_new; \
  206. } while (0)
  207. static void
  208. destroy_wasm_type(WASMFuncType *type)
  209. {
  210. if (type->ref_count > 1) {
  211. /* The type is referenced by other types
  212. of current wasm module */
  213. type->ref_count--;
  214. return;
  215. }
  216. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  217. && WASM_ENABLE_LAZY_JIT != 0
  218. if (type->call_to_llvm_jit_from_fast_jit)
  219. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  220. #endif
  221. wasm_runtime_free(type);
  222. }
  223. static bool
  224. check_function_index(const WASMModule *module, uint32 function_index,
  225. char *error_buf, uint32 error_buf_size)
  226. {
  227. return (function_index
  228. < module->import_function_count + module->function_count);
  229. }
  230. typedef struct InitValue {
  231. uint8 type;
  232. uint8 flag;
  233. WASMValue value;
  234. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  235. InitializerExpression *expr;
  236. #endif
  237. } InitValue;
  238. typedef struct ConstExprContext {
  239. uint32 sp;
  240. uint32 size;
  241. WASMModule *module;
  242. InitValue *stack;
  243. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  244. } ConstExprContext;
  245. static void
  246. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  247. {
  248. ctx->sp = 0;
  249. ctx->module = module;
  250. ctx->stack = ctx->data;
  251. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  252. }
  253. static bool
  254. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  255. WASMValue *value,
  256. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  257. InitializerExpression *expr,
  258. #endif
  259. char *error_buf, uint32 error_buf_size)
  260. {
  261. InitValue *cur_value;
  262. if (ctx->sp >= ctx->size) {
  263. if (ctx->stack != ctx->data) {
  264. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  265. (ctx->size + 4) * sizeof(InitValue));
  266. }
  267. else {
  268. if (!(ctx->stack =
  269. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  270. error_buf, error_buf_size))) {
  271. return false;
  272. }
  273. }
  274. ctx->size += 4;
  275. }
  276. cur_value = &ctx->stack[ctx->sp++];
  277. cur_value->type = type;
  278. cur_value->flag = flag;
  279. cur_value->value = *value;
  280. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  281. cur_value->expr = expr;
  282. #endif
  283. return true;
  284. fail:
  285. return false;
  286. }
  287. static bool
  288. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  289. WASMValue *p_value,
  290. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  291. InitializerExpression **p_expr,
  292. #endif
  293. char *error_buf, uint32 error_buf_size)
  294. {
  295. InitValue *cur_value;
  296. if (ctx->sp == 0) {
  297. return false;
  298. }
  299. cur_value = &ctx->stack[--ctx->sp];
  300. if (cur_value->type != type) {
  301. return false;
  302. }
  303. if (p_flag)
  304. *p_flag = cur_value->flag;
  305. if (p_value)
  306. *p_value = cur_value->value;
  307. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  308. if (p_expr)
  309. *p_expr = cur_value->expr;
  310. #endif
  311. return true;
  312. }
  313. static void
  314. destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
  315. {
  316. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  317. if (free_exprs) {
  318. for (uint32 j = 0; j < ctx->sp; j++) {
  319. if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
  320. destroy_init_expr_recursive(ctx->stack[j].expr);
  321. ctx->stack[j].expr = NULL;
  322. }
  323. }
  324. }
  325. #endif
  326. if (ctx->stack != ctx->data) {
  327. wasm_runtime_free(ctx->stack);
  328. }
  329. }
  330. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  331. static void
  332. destroy_init_expr(InitializerExpression *expr)
  333. {
  334. // free left expr and right exprs for binary oprand
  335. if (is_expr_binary_op(expr->init_expr_type)) {
  336. return;
  337. }
  338. if (expr->u.binary.l_expr) {
  339. destroy_init_expr_recursive(expr->u.binary.l_expr);
  340. }
  341. if (expr->u.binary.r_expr) {
  342. destroy_init_expr_recursive(expr->u.binary.r_expr);
  343. }
  344. expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
  345. }
  346. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
  347. static bool
  348. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  349. InitializerExpression *init_expr, uint8 type, char *error_buf,
  350. uint32 error_buf_size)
  351. {
  352. const uint8 *p = *p_buf, *p_end = buf_end;
  353. uint8 flag, *p_float;
  354. uint32 i;
  355. ConstExprContext const_expr_ctx = { 0 };
  356. WASMValue cur_value = { 0 };
  357. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  358. InitializerExpression *cur_expr = NULL;
  359. #endif
  360. init_const_expr_stack(&const_expr_ctx, module);
  361. CHECK_BUF(p, p_end, 1);
  362. flag = read_uint8(p);
  363. while (flag != WASM_OP_END) {
  364. switch (flag) {
  365. /* i32.const */
  366. case INIT_EXPR_TYPE_I32_CONST:
  367. read_leb_int32(p, p_end, cur_value.i32);
  368. if (!push_const_expr_stack(&const_expr_ctx, flag,
  369. VALUE_TYPE_I32, &cur_value,
  370. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  371. NULL,
  372. #endif
  373. error_buf, error_buf_size)) {
  374. goto fail;
  375. }
  376. break;
  377. /* i64.const */
  378. case INIT_EXPR_TYPE_I64_CONST:
  379. read_leb_int64(p, p_end, cur_value.i64);
  380. if (!push_const_expr_stack(&const_expr_ctx, flag,
  381. VALUE_TYPE_I64, &cur_value,
  382. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  383. NULL,
  384. #endif
  385. error_buf, error_buf_size)) {
  386. goto fail;
  387. }
  388. break;
  389. /* f32.const */
  390. case INIT_EXPR_TYPE_F32_CONST:
  391. CHECK_BUF(p, p_end, 4);
  392. p_float = (uint8 *)&cur_value.f32;
  393. for (i = 0; i < sizeof(float32); i++)
  394. *p_float++ = *p++;
  395. if (!push_const_expr_stack(&const_expr_ctx, flag,
  396. VALUE_TYPE_F32, &cur_value,
  397. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  398. NULL,
  399. #endif
  400. error_buf, error_buf_size)) {
  401. goto fail;
  402. }
  403. break;
  404. /* f64.const */
  405. case INIT_EXPR_TYPE_F64_CONST:
  406. CHECK_BUF(p, p_end, 8);
  407. p_float = (uint8 *)&cur_value.f64;
  408. for (i = 0; i < sizeof(float64); i++)
  409. *p_float++ = *p++;
  410. if (!push_const_expr_stack(&const_expr_ctx, flag,
  411. VALUE_TYPE_F64, &cur_value,
  412. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  413. NULL,
  414. #endif
  415. error_buf, error_buf_size)) {
  416. goto fail;
  417. }
  418. break;
  419. #if WASM_ENABLE_REF_TYPES != 0
  420. /* ref.func */
  421. case INIT_EXPR_TYPE_FUNCREF_CONST:
  422. {
  423. uint32 func_idx;
  424. read_leb_uint32(p, p_end, func_idx);
  425. cur_value.ref_index = func_idx;
  426. if (!check_function_index(module, func_idx, error_buf,
  427. error_buf_size)) {
  428. goto fail;
  429. }
  430. if (!push_const_expr_stack(&const_expr_ctx, flag,
  431. VALUE_TYPE_FUNCREF, &cur_value,
  432. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  433. NULL,
  434. #endif
  435. error_buf, error_buf_size)) {
  436. goto fail;
  437. }
  438. break;
  439. }
  440. /* ref.null */
  441. case INIT_EXPR_TYPE_REFNULL_CONST:
  442. {
  443. uint8 type1;
  444. CHECK_BUF(p, p_end, 1);
  445. type1 = read_uint8(p);
  446. cur_value.ref_index = UINT32_MAX;
  447. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  448. &cur_value,
  449. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  450. NULL,
  451. #endif
  452. error_buf, error_buf_size)) {
  453. goto fail;
  454. }
  455. break;
  456. }
  457. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  458. /* get_global */
  459. case INIT_EXPR_TYPE_GET_GLOBAL:
  460. {
  461. uint32 global_idx;
  462. uint8 global_type;
  463. read_leb_uint32(p, p_end, cur_value.global_index);
  464. global_idx = cur_value.global_index;
  465. bh_assert(global_idx < module->import_global_count);
  466. bh_assert(!module->import_globals[global_idx]
  467. .u.global.type.is_mutable);
  468. if (global_idx < module->import_global_count) {
  469. global_type = module->import_globals[global_idx]
  470. .u.global.type.val_type;
  471. }
  472. else {
  473. global_type =
  474. module
  475. ->globals[global_idx - module->import_global_count]
  476. .type.val_type;
  477. }
  478. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  479. &cur_value,
  480. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  481. NULL,
  482. #endif
  483. error_buf, error_buf_size))
  484. goto fail;
  485. break;
  486. }
  487. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  488. case INIT_EXPR_TYPE_I32_ADD:
  489. case INIT_EXPR_TYPE_I64_ADD:
  490. case INIT_EXPR_TYPE_I32_SUB:
  491. case INIT_EXPR_TYPE_I64_SUB:
  492. case INIT_EXPR_TYPE_I32_MUL:
  493. case INIT_EXPR_TYPE_I64_MUL:
  494. {
  495. InitializerExpression *l_expr, *r_expr;
  496. WASMValue l_value, r_value;
  497. uint8 l_flag, r_flag;
  498. uint8 value_type;
  499. if (flag == INIT_EXPR_TYPE_I32_ADD
  500. || flag == INIT_EXPR_TYPE_I32_SUB
  501. || flag == INIT_EXPR_TYPE_I32_MUL) {
  502. value_type = VALUE_TYPE_I32;
  503. }
  504. else {
  505. value_type = VALUE_TYPE_I64;
  506. }
  507. /* If right flag indicates a binary operation, right expr will
  508. * be popped from stack. Otherwise, allocate a new expr for
  509. * right expr. Same for left expr.
  510. */
  511. if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
  512. &r_value, &r_expr, error_buf,
  513. error_buf_size))) {
  514. goto fail;
  515. }
  516. if (!is_expr_binary_op(r_flag)) {
  517. if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
  518. error_buf, error_buf_size))) {
  519. goto fail;
  520. }
  521. r_expr->init_expr_type = r_flag;
  522. r_expr->u.unary.v = r_value;
  523. }
  524. if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
  525. &l_value, &l_expr, error_buf,
  526. error_buf_size))) {
  527. destroy_init_expr_recursive(r_expr);
  528. goto fail;
  529. }
  530. if (!is_expr_binary_op(l_flag)) {
  531. if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
  532. error_buf, error_buf_size))) {
  533. destroy_init_expr_recursive(r_expr);
  534. goto fail;
  535. }
  536. l_expr->init_expr_type = l_flag;
  537. l_expr->u.unary.v = l_value;
  538. }
  539. if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
  540. error_buf, error_buf_size))) {
  541. destroy_init_expr_recursive(l_expr);
  542. destroy_init_expr_recursive(r_expr);
  543. goto fail;
  544. }
  545. cur_expr->init_expr_type = flag;
  546. cur_expr->u.binary.l_expr = l_expr;
  547. cur_expr->u.binary.r_expr = r_expr;
  548. if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
  549. &cur_value, cur_expr, error_buf,
  550. error_buf_size)) {
  551. destroy_init_expr_recursive(cur_expr);
  552. goto fail;
  553. }
  554. break;
  555. }
  556. #endif
  557. default:
  558. {
  559. goto fail;
  560. }
  561. }
  562. CHECK_BUF(p, p_end, 1);
  563. flag = read_uint8(p);
  564. }
  565. /* There should be only one value left on the init value stack */
  566. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type, &cur_value,
  567. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  568. &cur_expr,
  569. #endif
  570. error_buf, error_buf_size)) {
  571. goto fail;
  572. }
  573. if (const_expr_ctx.sp != 0) {
  574. set_error_buf(error_buf, error_buf_size,
  575. "type mismatch: illegal constant opcode sequence");
  576. goto fail;
  577. }
  578. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  579. if (cur_expr != NULL) {
  580. bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
  581. sizeof(InitializerExpression));
  582. wasm_runtime_free(cur_expr);
  583. }
  584. else {
  585. init_expr->init_expr_type = flag;
  586. init_expr->u.unary.v = cur_value;
  587. }
  588. #else
  589. init_expr->init_expr_type = flag;
  590. init_expr->u.unary.v = cur_value;
  591. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
  592. *p_buf = p;
  593. destroy_const_expr_stack(&const_expr_ctx, false);
  594. return true;
  595. fail:
  596. destroy_const_expr_stack(&const_expr_ctx, true);
  597. return false;
  598. }
  599. static bool
  600. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  601. char *error_buf, uint32 error_buf_size)
  602. {
  603. const uint8 *p = buf, *p_end = buf_end, *p_org;
  604. uint32 type_count, param_count, result_count, i, j;
  605. uint32 param_cell_num, ret_cell_num;
  606. uint64 total_size;
  607. uint8 flag;
  608. WASMFuncType *type;
  609. read_leb_uint32(p, p_end, type_count);
  610. if (type_count) {
  611. module->type_count = type_count;
  612. total_size = sizeof(WASMFuncType *) * (uint64)type_count;
  613. if (!(module->types =
  614. loader_malloc(total_size, error_buf, error_buf_size))) {
  615. return false;
  616. }
  617. for (i = 0; i < type_count; i++) {
  618. CHECK_BUF(p, p_end, 1);
  619. flag = read_uint8(p);
  620. bh_assert(flag == 0x60);
  621. read_leb_uint32(p, p_end, param_count);
  622. /* Resolve param count and result count firstly */
  623. p_org = p;
  624. CHECK_BUF(p, p_end, param_count);
  625. p += param_count;
  626. read_leb_uint32(p, p_end, result_count);
  627. CHECK_BUF(p, p_end, result_count);
  628. p = p_org;
  629. bh_assert(param_count <= UINT16_MAX && result_count <= UINT16_MAX);
  630. total_size = offsetof(WASMFuncType, types)
  631. + sizeof(uint8) * (uint64)(param_count + result_count);
  632. if (!(type = module->types[i] =
  633. loader_malloc(total_size, error_buf, error_buf_size))) {
  634. return false;
  635. }
  636. /* Resolve param types and result types */
  637. type->ref_count = 1;
  638. type->param_count = (uint16)param_count;
  639. type->result_count = (uint16)result_count;
  640. for (j = 0; j < param_count; j++) {
  641. CHECK_BUF(p, p_end, 1);
  642. type->types[j] = read_uint8(p);
  643. }
  644. read_leb_uint32(p, p_end, result_count);
  645. for (j = 0; j < result_count; j++) {
  646. CHECK_BUF(p, p_end, 1);
  647. type->types[param_count + j] = read_uint8(p);
  648. }
  649. for (j = 0; j < param_count + result_count; j++) {
  650. bh_assert(is_valid_value_type_for_interpreter(type->types[j]));
  651. }
  652. param_cell_num = wasm_get_cell_num(type->types, param_count);
  653. ret_cell_num =
  654. wasm_get_cell_num(type->types + param_count, result_count);
  655. bh_assert(param_cell_num <= UINT16_MAX
  656. && ret_cell_num <= UINT16_MAX);
  657. type->param_cell_num = (uint16)param_cell_num;
  658. type->ret_cell_num = (uint16)ret_cell_num;
  659. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  660. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  661. #endif
  662. /* If there is already a same type created, use it instead */
  663. for (j = 0; j < i; ++j) {
  664. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  665. bh_assert(module->types[j]->ref_count != UINT16_MAX);
  666. destroy_wasm_type(type);
  667. module->types[i] = module->types[j];
  668. module->types[j]->ref_count++;
  669. break;
  670. }
  671. }
  672. }
  673. }
  674. bh_assert(p == p_end);
  675. LOG_VERBOSE("Load type section success.\n");
  676. (void)flag;
  677. return true;
  678. }
  679. static void
  680. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  681. uint32 *max_size)
  682. {
  683. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  684. ? init_size * 2
  685. : WASM_TABLE_MAX_SIZE;
  686. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  687. * ABI unchanged */
  688. (void)is_table64;
  689. if (max_size_flag) {
  690. /* module defines the table limitation */
  691. bh_assert(init_size <= *max_size);
  692. if (init_size < *max_size) {
  693. *max_size =
  694. *max_size < default_max_size ? *max_size : default_max_size;
  695. }
  696. }
  697. else {
  698. /* partial defined table limitation, gives a default value */
  699. *max_size = default_max_size;
  700. }
  701. }
  702. static bool
  703. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  704. const WASMModule *parent_module,
  705. const char *sub_module_name, const char *function_name,
  706. WASMFunctionImport *function, char *error_buf,
  707. uint32 error_buf_size)
  708. {
  709. const uint8 *p = *p_buf, *p_end = buf_end;
  710. uint32 declare_type_index = 0;
  711. WASMFuncType *declare_func_type = NULL;
  712. WASMFunction *linked_func = NULL;
  713. const char *linked_signature = NULL;
  714. void *linked_attachment = NULL;
  715. bool linked_call_conv_raw = false;
  716. read_leb_uint32(p, p_end, declare_type_index);
  717. *p_buf = p;
  718. bh_assert(declare_type_index < parent_module->type_count);
  719. declare_func_type = parent_module->types[declare_type_index];
  720. /* check built-in modules */
  721. linked_func = wasm_native_resolve_symbol(
  722. sub_module_name, function_name, declare_func_type, &linked_signature,
  723. &linked_attachment, &linked_call_conv_raw);
  724. function->module_name = (char *)sub_module_name;
  725. function->field_name = (char *)function_name;
  726. function->func_type = declare_func_type;
  727. function->func_ptr_linked = linked_func;
  728. function->signature = linked_signature;
  729. function->attachment = linked_attachment;
  730. function->call_conv_raw = linked_call_conv_raw;
  731. return true;
  732. }
  733. static bool
  734. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  735. WASMModule *parent_module, const char *sub_module_name,
  736. const char *table_name, WASMTableImport *table,
  737. char *error_buf, uint32 error_buf_size)
  738. {
  739. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  740. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  741. declare_max_size = 0;
  742. CHECK_BUF(p, p_end, 1);
  743. /* 0x70 or 0x6F */
  744. declare_elem_type = read_uint8(p);
  745. bh_assert(VALUE_TYPE_FUNCREF == declare_elem_type
  746. #if WASM_ENABLE_REF_TYPES != 0
  747. || VALUE_TYPE_EXTERNREF == declare_elem_type
  748. #endif
  749. );
  750. /* the table flag can't exceed one byte, only check in debug build given
  751. * the nature of mini-loader */
  752. p_org = p;
  753. read_leb_uint32(p, p_end, table_flag);
  754. bh_assert(p - p_org <= 1);
  755. (void)p_org;
  756. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  757. return false;
  758. }
  759. read_leb_uint32(p, p_end, declare_init_size);
  760. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  761. read_leb_uint32(p, p_end, declare_max_size);
  762. bh_assert(table->table_type.init_size <= table->table_type.max_size);
  763. }
  764. adjust_table_max_size(table_flag & TABLE64_FLAG, declare_init_size,
  765. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  766. *p_buf = p;
  767. bh_assert(!((table_flag & MAX_TABLE_SIZE_FLAG)
  768. && declare_init_size > declare_max_size));
  769. /* now we believe all declaration are ok */
  770. table->table_type.elem_type = declare_elem_type;
  771. table->table_type.init_size = declare_init_size;
  772. table->table_type.flags = table_flag;
  773. table->table_type.max_size = declare_max_size;
  774. return true;
  775. }
  776. static bool
  777. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  778. WASMModule *parent_module, const char *sub_module_name,
  779. const char *memory_name, WASMMemoryImport *memory,
  780. char *error_buf, uint32 error_buf_size)
  781. {
  782. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  783. #if WASM_ENABLE_APP_FRAMEWORK != 0
  784. uint32 pool_size = wasm_runtime_memory_pool_size();
  785. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  786. / DEFAULT_NUM_BYTES_PER_PAGE;
  787. #else
  788. uint32 max_page_count;
  789. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  790. uint32 mem_flag = 0;
  791. bool is_memory64 = false;
  792. uint32 declare_init_page_count = 0;
  793. uint32 declare_max_page_count = 0;
  794. /* the memory flag can't exceed one byte, only check in debug build given
  795. * the nature of mini-loader */
  796. p_org = p;
  797. read_leb_uint32(p, p_end, mem_flag);
  798. bh_assert(p - p_org <= 1);
  799. (void)p_org;
  800. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  801. return false;
  802. }
  803. #if WASM_ENABLE_APP_FRAMEWORK == 0
  804. is_memory64 = mem_flag & MEMORY64_FLAG;
  805. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  806. #endif
  807. read_leb_uint32(p, p_end, declare_init_page_count);
  808. bh_assert(declare_init_page_count <= max_page_count);
  809. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  810. read_leb_uint32(p, p_end, declare_max_page_count);
  811. bh_assert(declare_init_page_count <= declare_max_page_count);
  812. bh_assert(declare_max_page_count <= max_page_count);
  813. if (declare_max_page_count > max_page_count) {
  814. declare_max_page_count = max_page_count;
  815. }
  816. }
  817. else {
  818. /* Limit the maximum memory size to max_page_count */
  819. declare_max_page_count = max_page_count;
  820. }
  821. /* now we believe all declaration are ok */
  822. memory->mem_type.flags = mem_flag;
  823. memory->mem_type.init_page_count = declare_init_page_count;
  824. memory->mem_type.max_page_count = declare_max_page_count;
  825. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  826. *p_buf = p;
  827. return true;
  828. }
  829. static bool
  830. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  831. const WASMModule *parent_module, char *sub_module_name,
  832. char *global_name, WASMGlobalImport *global, char *error_buf,
  833. uint32 error_buf_size)
  834. {
  835. const uint8 *p = *p_buf, *p_end = buf_end;
  836. uint8 declare_type = 0;
  837. uint8 declare_mutable = 0;
  838. bool is_mutable = false;
  839. bool ret = false;
  840. CHECK_BUF(p, p_end, 2);
  841. declare_type = read_uint8(p);
  842. declare_mutable = read_uint8(p);
  843. *p_buf = p;
  844. bh_assert(declare_mutable < 2);
  845. is_mutable = declare_mutable & 1 ? true : false;
  846. #if WASM_ENABLE_LIBC_BUILTIN != 0
  847. /* check built-in modules */
  848. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  849. global);
  850. if (ret) {
  851. bh_assert(global->type.val_type == declare_type
  852. && global->type.is_mutable != declare_mutable);
  853. }
  854. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  855. global->is_linked = ret;
  856. global->module_name = sub_module_name;
  857. global->field_name = global_name;
  858. global->type.val_type = declare_type;
  859. global->type.is_mutable = is_mutable;
  860. (void)p_end;
  861. return true;
  862. }
  863. static bool
  864. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  865. char *error_buf, uint32 error_buf_size)
  866. {
  867. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  868. CHECK_BUF(p, p_end, 1);
  869. /* 0x70 or 0x6F */
  870. table->table_type.elem_type = read_uint8(p);
  871. bh_assert((VALUE_TYPE_FUNCREF == table->table_type.elem_type)
  872. #if WASM_ENABLE_REF_TYPES != 0
  873. || VALUE_TYPE_EXTERNREF == table->table_type.elem_type
  874. #endif
  875. );
  876. /* the table flag can't exceed one byte, only check in debug build given
  877. * the nature of mini-loader */
  878. p_org = p;
  879. read_leb_uint32(p, p_end, table->table_type.flags);
  880. bh_assert(p - p_org <= 1);
  881. (void)p_org;
  882. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  883. error_buf_size, false)) {
  884. return false;
  885. }
  886. read_leb_uint32(p, p_end, table->table_type.init_size);
  887. if (table->table_type.flags == MAX_TABLE_SIZE_FLAG) {
  888. read_leb_uint32(p, p_end, table->table_type.max_size);
  889. bh_assert(table->table_type.init_size <= table->table_type.max_size);
  890. }
  891. adjust_table_max_size(table->table_type.flags & TABLE64_FLAG,
  892. table->table_type.init_size,
  893. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  894. &table->table_type.max_size);
  895. *p_buf = p;
  896. return true;
  897. }
  898. static bool
  899. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  900. char *error_buf, uint32 error_buf_size)
  901. {
  902. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  903. #if WASM_ENABLE_APP_FRAMEWORK != 0
  904. uint32 pool_size = wasm_runtime_memory_pool_size();
  905. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  906. / DEFAULT_NUM_BYTES_PER_PAGE;
  907. #else
  908. uint32 max_page_count;
  909. bool is_memory64 = false;
  910. #endif
  911. /* the memory flag can't exceed one byte, only check in debug build given
  912. * the nature of mini-loader */
  913. p_org = p;
  914. read_leb_uint32(p, p_end, memory->flags);
  915. bh_assert(p - p_org <= 1);
  916. (void)p_org;
  917. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  918. false)) {
  919. return false;
  920. }
  921. #if WASM_ENABLE_APP_FRAMEWORK == 0
  922. is_memory64 = memory->flags & MEMORY64_FLAG;
  923. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  924. #endif
  925. read_leb_uint32(p, p_end, memory->init_page_count);
  926. bh_assert(memory->init_page_count <= max_page_count);
  927. if (memory->flags & 1) {
  928. read_leb_uint32(p, p_end, memory->max_page_count);
  929. bh_assert(memory->init_page_count <= memory->max_page_count);
  930. bh_assert(memory->max_page_count <= max_page_count);
  931. if (memory->max_page_count > max_page_count)
  932. memory->max_page_count = max_page_count;
  933. }
  934. else {
  935. /* Limit the maximum memory size to max_page_count */
  936. memory->max_page_count = max_page_count;
  937. }
  938. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  939. *p_buf = p;
  940. return true;
  941. }
  942. static bool
  943. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  944. bool is_load_from_file_buf, char *error_buf,
  945. uint32 error_buf_size)
  946. {
  947. const uint8 *p = buf, *p_end = buf_end, *p_old;
  948. uint32 import_count, name_len, type_index, i, u32, flags;
  949. uint64 total_size;
  950. WASMImport *import;
  951. WASMImport *import_functions = NULL, *import_tables = NULL;
  952. WASMImport *import_memories = NULL, *import_globals = NULL;
  953. char *sub_module_name, *field_name;
  954. uint8 u8, kind;
  955. read_leb_uint32(p, p_end, import_count);
  956. if (import_count) {
  957. module->import_count = import_count;
  958. total_size = sizeof(WASMImport) * (uint64)import_count;
  959. if (!(module->imports =
  960. loader_malloc(total_size, error_buf, error_buf_size))) {
  961. return false;
  962. }
  963. p_old = p;
  964. /* Scan firstly to get import count of each type */
  965. for (i = 0; i < import_count; i++) {
  966. /* module name */
  967. read_leb_uint32(p, p_end, name_len);
  968. CHECK_BUF(p, p_end, name_len);
  969. p += name_len;
  970. /* field name */
  971. read_leb_uint32(p, p_end, name_len);
  972. CHECK_BUF(p, p_end, name_len);
  973. p += name_len;
  974. CHECK_BUF(p, p_end, 1);
  975. /* 0x00/0x01/0x02/0x03 */
  976. kind = read_uint8(p);
  977. switch (kind) {
  978. case IMPORT_KIND_FUNC: /* import function */
  979. read_leb_uint32(p, p_end, type_index);
  980. module->import_function_count++;
  981. break;
  982. case IMPORT_KIND_TABLE: /* import table */
  983. CHECK_BUF(p, p_end, 1);
  984. /* 0x70 */
  985. u8 = read_uint8(p);
  986. read_leb_uint32(p, p_end, flags);
  987. read_leb_uint32(p, p_end, u32);
  988. if (flags & 1)
  989. read_leb_uint32(p, p_end, u32);
  990. module->import_table_count++;
  991. #if WASM_ENABLE_REF_TYPES == 0
  992. bh_assert(module->import_table_count <= 1);
  993. #endif
  994. break;
  995. case IMPORT_KIND_MEMORY: /* import memory */
  996. read_leb_uint32(p, p_end, flags);
  997. read_leb_uint32(p, p_end, u32);
  998. if (flags & 1)
  999. read_leb_uint32(p, p_end, u32);
  1000. module->import_memory_count++;
  1001. #if WASM_ENABLE_MULTI_MEMORY != 0
  1002. bh_assert(module->import_memory_count <= 1);
  1003. #endif
  1004. break;
  1005. case IMPORT_KIND_GLOBAL: /* import global */
  1006. CHECK_BUF(p, p_end, 2);
  1007. p += 2;
  1008. module->import_global_count++;
  1009. break;
  1010. default:
  1011. bh_assert(0);
  1012. break;
  1013. }
  1014. }
  1015. if (module->import_function_count)
  1016. import_functions = module->import_functions = module->imports;
  1017. if (module->import_table_count)
  1018. import_tables = module->import_tables =
  1019. module->imports + module->import_function_count;
  1020. if (module->import_memory_count)
  1021. import_memories = module->import_memories =
  1022. module->imports + module->import_function_count
  1023. + module->import_table_count;
  1024. if (module->import_global_count)
  1025. import_globals = module->import_globals =
  1026. module->imports + module->import_function_count
  1027. + module->import_table_count + module->import_memory_count;
  1028. p = p_old;
  1029. /* Scan again to resolve the data */
  1030. for (i = 0; i < import_count; i++) {
  1031. WASMModule *sub_module = NULL;
  1032. /* load module name */
  1033. read_leb_uint32(p, p_end, name_len);
  1034. CHECK_BUF(p, p_end, name_len);
  1035. if (!(sub_module_name = wasm_const_str_list_insert(
  1036. p, name_len, module, is_load_from_file_buf, error_buf,
  1037. error_buf_size))) {
  1038. return false;
  1039. }
  1040. p += name_len;
  1041. /* load field name */
  1042. read_leb_uint32(p, p_end, name_len);
  1043. CHECK_BUF(p, p_end, name_len);
  1044. if (!(field_name = wasm_const_str_list_insert(
  1045. p, name_len, module, is_load_from_file_buf, error_buf,
  1046. error_buf_size))) {
  1047. return false;
  1048. }
  1049. p += name_len;
  1050. CHECK_BUF(p, p_end, 1);
  1051. /* 0x00/0x01/0x02/0x03 */
  1052. kind = read_uint8(p);
  1053. LOG_DEBUG("import #%d: (%s, %s), kind: %d", i, sub_module_name,
  1054. field_name, kind);
  1055. switch (kind) {
  1056. case IMPORT_KIND_FUNC: /* import function */
  1057. bh_assert(import_functions);
  1058. import = import_functions++;
  1059. if (!load_function_import(
  1060. &p, p_end, module, sub_module_name, field_name,
  1061. &import->u.function, error_buf, error_buf_size)) {
  1062. return false;
  1063. }
  1064. break;
  1065. case IMPORT_KIND_TABLE: /* import table */
  1066. bh_assert(import_tables);
  1067. import = import_tables++;
  1068. if (!load_table_import(&p, p_end, module, sub_module_name,
  1069. field_name, &import->u.table,
  1070. error_buf, error_buf_size)) {
  1071. LOG_DEBUG("can not import such a table (%s,%s)",
  1072. sub_module_name, field_name);
  1073. return false;
  1074. }
  1075. break;
  1076. case IMPORT_KIND_MEMORY: /* import memory */
  1077. bh_assert(import_memories);
  1078. import = import_memories++;
  1079. if (!load_memory_import(&p, p_end, module, sub_module_name,
  1080. field_name, &import->u.memory,
  1081. error_buf, error_buf_size)) {
  1082. return false;
  1083. }
  1084. break;
  1085. case IMPORT_KIND_GLOBAL: /* import global */
  1086. bh_assert(import_globals);
  1087. import = import_globals++;
  1088. if (!load_global_import(&p, p_end, module, sub_module_name,
  1089. field_name, &import->u.global,
  1090. error_buf, error_buf_size)) {
  1091. return false;
  1092. }
  1093. break;
  1094. default:
  1095. bh_assert(0);
  1096. import = NULL;
  1097. break;
  1098. }
  1099. import->kind = kind;
  1100. import->u.names.module_name = sub_module_name;
  1101. import->u.names.field_name = field_name;
  1102. (void)sub_module;
  1103. }
  1104. #if WASM_ENABLE_LIBC_WASI != 0
  1105. import = module->import_functions;
  1106. for (i = 0; i < module->import_function_count; i++, import++) {
  1107. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  1108. || !strcmp(import->u.names.module_name,
  1109. "wasi_snapshot_preview1")) {
  1110. module->import_wasi_api = true;
  1111. break;
  1112. }
  1113. }
  1114. #endif
  1115. }
  1116. bh_assert(p == p_end);
  1117. LOG_VERBOSE("Load import section success.\n");
  1118. (void)u8;
  1119. (void)u32;
  1120. (void)type_index;
  1121. return true;
  1122. }
  1123. static bool
  1124. init_function_local_offsets(WASMFunction *func, char *error_buf,
  1125. uint32 error_buf_size)
  1126. {
  1127. WASMFuncType *param_type = func->func_type;
  1128. uint32 param_count = param_type->param_count;
  1129. uint8 *param_types = param_type->types;
  1130. uint32 local_count = func->local_count;
  1131. uint8 *local_types = func->local_types;
  1132. uint32 i, local_offset = 0;
  1133. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  1134. if (total_size > 0
  1135. && !(func->local_offsets =
  1136. loader_malloc(total_size, error_buf, error_buf_size))) {
  1137. return false;
  1138. }
  1139. for (i = 0; i < param_count; i++) {
  1140. func->local_offsets[i] = (uint16)local_offset;
  1141. local_offset += wasm_value_type_cell_num(param_types[i]);
  1142. }
  1143. for (i = 0; i < local_count; i++) {
  1144. func->local_offsets[param_count + i] = (uint16)local_offset;
  1145. local_offset += wasm_value_type_cell_num(local_types[i]);
  1146. }
  1147. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  1148. return true;
  1149. }
  1150. static bool
  1151. load_function_section(const uint8 *buf, const uint8 *buf_end,
  1152. const uint8 *buf_code, const uint8 *buf_code_end,
  1153. WASMModule *module, char *error_buf,
  1154. uint32 error_buf_size)
  1155. {
  1156. const uint8 *p = buf, *p_end = buf_end;
  1157. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  1158. uint32 func_count;
  1159. uint64 total_size;
  1160. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  1161. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  1162. uint8 type;
  1163. WASMFunction *func;
  1164. read_leb_uint32(p, p_end, func_count);
  1165. if (buf_code)
  1166. read_leb_uint32(p_code, buf_code_end, code_count);
  1167. bh_assert(func_count == code_count);
  1168. bh_assert(module->import_function_count <= UINT32_MAX - func_count);
  1169. if (func_count) {
  1170. module->function_count = func_count;
  1171. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  1172. if (!(module->functions =
  1173. loader_malloc(total_size, error_buf, error_buf_size))) {
  1174. return false;
  1175. }
  1176. for (i = 0; i < func_count; i++) {
  1177. /* Resolve function type */
  1178. read_leb_uint32(p, p_end, type_index);
  1179. bh_assert(type_index < module->type_count);
  1180. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1181. type_index = wasm_get_smallest_type_idx(
  1182. module->types, module->type_count, type_index);
  1183. #endif
  1184. read_leb_uint32(p_code, buf_code_end, code_size);
  1185. bh_assert(code_size > 0 && p_code + code_size <= buf_code_end);
  1186. /* Resolve local set count */
  1187. p_code_end = p_code + code_size;
  1188. local_count = 0;
  1189. read_leb_uint32(p_code, buf_code_end, local_set_count);
  1190. p_code_save = p_code;
  1191. /* Calculate total local count */
  1192. for (j = 0; j < local_set_count; j++) {
  1193. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1194. bh_assert(sub_local_count <= UINT32_MAX - local_count);
  1195. CHECK_BUF(p_code, buf_code_end, 1);
  1196. /* 0x7F/0x7E/0x7D/0x7C */
  1197. type = read_uint8(p_code);
  1198. local_count += sub_local_count;
  1199. }
  1200. bh_assert(p_code_end > p_code && *(p_code_end - 1) == WASM_OP_END);
  1201. /* Alloc memory, layout: function structure + local types */
  1202. code_size = (uint32)(p_code_end - p_code);
  1203. total_size = sizeof(WASMFunction) + (uint64)local_count;
  1204. if (!(func = module->functions[i] =
  1205. loader_malloc(total_size, error_buf, error_buf_size))) {
  1206. return false;
  1207. }
  1208. /* Set function type, local count, code size and code body */
  1209. func->func_type = module->types[type_index];
  1210. func->local_count = local_count;
  1211. if (local_count > 0)
  1212. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  1213. func->code_size = code_size;
  1214. /*
  1215. * we shall make a copy of code body [p_code, p_code + code_size]
  1216. * when we are worrying about inappropriate releasing behaviour.
  1217. * all code bodies are actually in a buffer which user allocates in
  1218. * their embedding environment and we don't have power over them.
  1219. * it will be like:
  1220. * code_body_cp = malloc(code_size);
  1221. * memcpy(code_body_cp, p_code, code_size);
  1222. * func->code = code_body_cp;
  1223. */
  1224. func->code = (uint8 *)p_code;
  1225. /* Load each local type */
  1226. p_code = p_code_save;
  1227. local_type_index = 0;
  1228. for (j = 0; j < local_set_count; j++) {
  1229. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1230. /* Note: sub_local_count is allowed to be 0 */
  1231. bh_assert(local_type_index <= UINT32_MAX - sub_local_count
  1232. && local_type_index + sub_local_count <= local_count);
  1233. CHECK_BUF(p_code, buf_code_end, 1);
  1234. /* 0x7F/0x7E/0x7D/0x7C */
  1235. type = read_uint8(p_code);
  1236. bh_assert(is_valid_value_type_for_interpreter(type));
  1237. for (k = 0; k < sub_local_count; k++) {
  1238. func->local_types[local_type_index++] = type;
  1239. }
  1240. }
  1241. func->param_cell_num = func->func_type->param_cell_num;
  1242. func->ret_cell_num = func->func_type->ret_cell_num;
  1243. local_cell_num =
  1244. wasm_get_cell_num(func->local_types, func->local_count);
  1245. bh_assert(local_cell_num <= UINT16_MAX);
  1246. func->local_cell_num = (uint16)local_cell_num;
  1247. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  1248. return false;
  1249. p_code = p_code_end;
  1250. }
  1251. }
  1252. bh_assert(p == p_end);
  1253. LOG_VERBOSE("Load function section success.\n");
  1254. (void)code_count;
  1255. return true;
  1256. }
  1257. static bool
  1258. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1259. char *error_buf, uint32 error_buf_size)
  1260. {
  1261. const uint8 *p = buf, *p_end = buf_end;
  1262. uint32 table_count, i;
  1263. uint64 total_size;
  1264. WASMTable *table;
  1265. read_leb_uint32(p, p_end, table_count);
  1266. #if WASM_ENABLE_REF_TYPES == 0
  1267. bh_assert(module->import_table_count + table_count <= 1);
  1268. #endif
  1269. if (table_count) {
  1270. module->table_count = table_count;
  1271. total_size = sizeof(WASMTable) * (uint64)table_count;
  1272. if (!(module->tables =
  1273. loader_malloc(total_size, error_buf, error_buf_size))) {
  1274. return false;
  1275. }
  1276. /* load each table */
  1277. table = module->tables;
  1278. for (i = 0; i < table_count; i++, table++)
  1279. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  1280. return false;
  1281. }
  1282. bh_assert(p == p_end);
  1283. LOG_VERBOSE("Load table section success.\n");
  1284. return true;
  1285. }
  1286. static bool
  1287. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1288. char *error_buf, uint32 error_buf_size)
  1289. {
  1290. const uint8 *p = buf, *p_end = buf_end;
  1291. uint32 memory_count, i;
  1292. uint64 total_size;
  1293. WASMMemory *memory;
  1294. read_leb_uint32(p, p_end, memory_count);
  1295. #if WASM_ENABLE_MULTI_MEMORY != 0
  1296. bh_assert(module->import_memory_count + memory_count <= 1);
  1297. #endif
  1298. if (memory_count) {
  1299. module->memory_count = memory_count;
  1300. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  1301. if (!(module->memories =
  1302. loader_malloc(total_size, error_buf, error_buf_size))) {
  1303. return false;
  1304. }
  1305. /* load each memory */
  1306. memory = module->memories;
  1307. for (i = 0; i < memory_count; i++, memory++)
  1308. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  1309. return false;
  1310. }
  1311. bh_assert(p == p_end);
  1312. LOG_VERBOSE("Load memory section success.\n");
  1313. return true;
  1314. }
  1315. static bool
  1316. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1317. char *error_buf, uint32 error_buf_size)
  1318. {
  1319. const uint8 *p = buf, *p_end = buf_end;
  1320. uint32 global_count, i;
  1321. uint64 total_size;
  1322. WASMGlobal *global;
  1323. uint8 mutable;
  1324. read_leb_uint32(p, p_end, global_count);
  1325. bh_assert(module->import_global_count <= UINT32_MAX - global_count);
  1326. module->global_count = 0;
  1327. if (global_count) {
  1328. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  1329. if (!(module->globals =
  1330. loader_malloc(total_size, error_buf, error_buf_size))) {
  1331. return false;
  1332. }
  1333. global = module->globals;
  1334. for (i = 0; i < global_count; i++, global++) {
  1335. CHECK_BUF(p, p_end, 2);
  1336. global->type.val_type = read_uint8(p);
  1337. mutable = read_uint8(p);
  1338. bh_assert(mutable < 2);
  1339. global->type.is_mutable = mutable ? true : false;
  1340. /* initialize expression */
  1341. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  1342. global->type.val_type, error_buf,
  1343. error_buf_size))
  1344. return false;
  1345. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1346. /**
  1347. * Currently, constant expressions occurring as initializers
  1348. * of globals are further constrained in that contained
  1349. * global.get instructions are
  1350. * only allowed to refer to imported globals.
  1351. */
  1352. uint32 target_global_index =
  1353. global->init_expr.u.unary.v.global_index;
  1354. bh_assert(target_global_index < module->import_global_count);
  1355. (void)target_global_index;
  1356. }
  1357. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  1358. == global->init_expr.init_expr_type) {
  1359. bh_assert(global->init_expr.u.unary.v.ref_index
  1360. < module->import_function_count
  1361. + module->function_count);
  1362. }
  1363. module->global_count++;
  1364. }
  1365. bh_assert(module->global_count == global_count);
  1366. }
  1367. bh_assert(p == p_end);
  1368. LOG_VERBOSE("Load global section success.\n");
  1369. return true;
  1370. }
  1371. static bool
  1372. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1373. bool is_load_from_file_buf, char *error_buf,
  1374. uint32 error_buf_size)
  1375. {
  1376. const uint8 *p = buf, *p_end = buf_end;
  1377. uint32 export_count, i, j, index;
  1378. uint64 total_size;
  1379. uint32 str_len;
  1380. WASMExport *export;
  1381. const char *name;
  1382. read_leb_uint32(p, p_end, export_count);
  1383. if (export_count) {
  1384. module->export_count = export_count;
  1385. total_size = sizeof(WASMExport) * (uint64)export_count;
  1386. if (!(module->exports =
  1387. loader_malloc(total_size, error_buf, error_buf_size))) {
  1388. return false;
  1389. }
  1390. export = module->exports;
  1391. for (i = 0; i < export_count; i++, export ++) {
  1392. read_leb_uint32(p, p_end, str_len);
  1393. CHECK_BUF(p, p_end, str_len);
  1394. for (j = 0; j < i; j++) {
  1395. name = module->exports[j].name;
  1396. bh_assert(!(strlen(name) == str_len
  1397. && memcmp(name, p, str_len) == 0));
  1398. }
  1399. if (!(export->name = wasm_const_str_list_insert(
  1400. p, str_len, module, is_load_from_file_buf, error_buf,
  1401. error_buf_size))) {
  1402. return false;
  1403. }
  1404. p += str_len;
  1405. CHECK_BUF(p, p_end, 1);
  1406. export->kind = read_uint8(p);
  1407. read_leb_uint32(p, p_end, index);
  1408. export->index = index;
  1409. switch (export->kind) {
  1410. /* function index */
  1411. case EXPORT_KIND_FUNC:
  1412. bh_assert(index < module->function_count
  1413. + module->import_function_count);
  1414. break;
  1415. /* table index */
  1416. case EXPORT_KIND_TABLE:
  1417. bh_assert(index < module->table_count
  1418. + module->import_table_count);
  1419. break;
  1420. /* memory index */
  1421. case EXPORT_KIND_MEMORY:
  1422. bh_assert(index < module->memory_count
  1423. + module->import_memory_count);
  1424. break;
  1425. /* global index */
  1426. case EXPORT_KIND_GLOBAL:
  1427. bh_assert(index < module->global_count
  1428. + module->import_global_count);
  1429. break;
  1430. default:
  1431. bh_assert(0);
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. bh_assert(p == p_end);
  1437. LOG_VERBOSE("Load export section success.\n");
  1438. (void)name;
  1439. return true;
  1440. }
  1441. static bool
  1442. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  1443. uint32 error_buf_size)
  1444. {
  1445. #if WASM_ENABLE_REF_TYPES == 0
  1446. if (table_index != 0) {
  1447. return false;
  1448. }
  1449. #endif
  1450. if (table_index >= module->import_table_count + module->table_count) {
  1451. return false;
  1452. }
  1453. return true;
  1454. }
  1455. static bool
  1456. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1457. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  1458. {
  1459. const uint8 *p = *p_buf, *p_end = buf_end;
  1460. uint32 table_index;
  1461. read_leb_uint32(p, p_end, table_index);
  1462. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  1463. return false;
  1464. }
  1465. *p_table_index = table_index;
  1466. *p_buf = p;
  1467. return true;
  1468. }
  1469. #if WASM_ENABLE_REF_TYPES != 0
  1470. static bool
  1471. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  1472. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  1473. {
  1474. const uint8 *p = *p_buf, *p_end = buf_end;
  1475. uint8 elem_type;
  1476. CHECK_BUF(p, p_end, 1);
  1477. elem_type = read_uint8(p);
  1478. if ((elemkind_zero && elem_type != 0)
  1479. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  1480. && elem_type != VALUE_TYPE_EXTERNREF)) {
  1481. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  1482. return false;
  1483. }
  1484. if (elemkind_zero)
  1485. *p_elem_type = VALUE_TYPE_FUNCREF;
  1486. else
  1487. *p_elem_type = elem_type;
  1488. *p_buf = p;
  1489. (void)p_end;
  1490. return true;
  1491. }
  1492. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  1493. static bool
  1494. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  1495. WASMModule *module, WASMTableSeg *table_segment,
  1496. char *error_buf, uint32 error_buf_size)
  1497. {
  1498. const uint8 *p = *p_buf, *p_end = buf_end;
  1499. uint32 function_count, function_index = 0, i;
  1500. uint64 total_size;
  1501. read_leb_uint32(p, p_end, function_count);
  1502. table_segment->value_count = function_count;
  1503. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  1504. if (total_size > 0
  1505. && !(table_segment->init_values =
  1506. (InitializerExpression *)loader_malloc(total_size, error_buf,
  1507. error_buf_size))) {
  1508. return false;
  1509. }
  1510. for (i = 0; i < function_count; i++) {
  1511. InitializerExpression *init_expr = &table_segment->init_values[i];
  1512. read_leb_uint32(p, p_end, function_index);
  1513. if (!check_function_index(module, function_index, error_buf,
  1514. error_buf_size)) {
  1515. return false;
  1516. }
  1517. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  1518. init_expr->u.unary.v.ref_index = function_index;
  1519. }
  1520. *p_buf = p;
  1521. return true;
  1522. }
  1523. #if WASM_ENABLE_REF_TYPES != 0
  1524. static bool
  1525. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  1526. WASMModule *module, WASMTableSeg *table_segment,
  1527. char *error_buf, uint32 error_buf_size)
  1528. {
  1529. const uint8 *p = *p_buf, *p_end = buf_end;
  1530. uint32 ref_count, i;
  1531. uint64 total_size;
  1532. read_leb_uint32(p, p_end, ref_count);
  1533. table_segment->value_count = ref_count;
  1534. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  1535. if (total_size > 0
  1536. && !(table_segment->init_values =
  1537. (InitializerExpression *)loader_malloc(total_size, error_buf,
  1538. error_buf_size))) {
  1539. return false;
  1540. }
  1541. for (i = 0; i < ref_count; i++) {
  1542. InitializerExpression *init_expr = &table_segment->init_values[i];
  1543. if (!load_init_expr(module, &p, p_end, init_expr,
  1544. table_segment->elem_type, error_buf,
  1545. error_buf_size))
  1546. return false;
  1547. bh_assert(
  1548. (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  1549. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  1550. || (init_expr->init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST));
  1551. }
  1552. *p_buf = p;
  1553. return true;
  1554. }
  1555. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  1556. static bool
  1557. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  1558. WASMModule *module, char *error_buf,
  1559. uint32 error_buf_size)
  1560. {
  1561. const uint8 *p = buf, *p_end = buf_end;
  1562. uint8 table_elem_idx_type;
  1563. uint32 table_segment_count, i, table_index, function_count;
  1564. uint64 total_size;
  1565. WASMTableSeg *table_segment;
  1566. read_leb_uint32(p, p_end, table_segment_count);
  1567. if (table_segment_count) {
  1568. module->table_seg_count = table_segment_count;
  1569. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1570. if (!(module->table_segments =
  1571. loader_malloc(total_size, error_buf, error_buf_size))) {
  1572. return false;
  1573. }
  1574. table_segment = module->table_segments;
  1575. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1576. bh_assert(p < p_end);
  1577. table_elem_idx_type = VALUE_TYPE_I32;
  1578. #if WASM_ENABLE_REF_TYPES != 0
  1579. read_leb_uint32(p, p_end, table_segment->mode);
  1580. /* last three bits */
  1581. table_segment->mode = table_segment->mode & 0x07;
  1582. switch (table_segment->mode) {
  1583. /* elemkind/elemtype + active */
  1584. case 0:
  1585. case 4:
  1586. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  1587. table_segment->table_index = 0;
  1588. if (!check_table_index(module, table_segment->table_index,
  1589. error_buf, error_buf_size))
  1590. return false;
  1591. #if WASM_ENABLE_MEMORY64 != 0
  1592. table_elem_idx_type =
  1593. is_table_64bit(module, table_segment->table_index)
  1594. ? VALUE_TYPE_I64
  1595. : VALUE_TYPE_I32;
  1596. #endif
  1597. if (!load_init_expr(
  1598. module, &p, p_end, &table_segment->base_offset,
  1599. table_elem_idx_type, error_buf, error_buf_size))
  1600. return false;
  1601. if (table_segment->mode == 0) {
  1602. /* vec(funcidx) */
  1603. if (!load_func_index_vec(&p, p_end, module,
  1604. table_segment, error_buf,
  1605. error_buf_size))
  1606. return false;
  1607. }
  1608. else {
  1609. /* vec(expr) */
  1610. if (!load_init_expr_vec(&p, p_end, module,
  1611. table_segment, error_buf,
  1612. error_buf_size))
  1613. return false;
  1614. }
  1615. break;
  1616. /* elemkind + passive/declarative */
  1617. case 1:
  1618. case 3:
  1619. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1620. true, error_buf, error_buf_size))
  1621. return false;
  1622. /* vec(funcidx) */
  1623. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1624. error_buf, error_buf_size))
  1625. return false;
  1626. break;
  1627. /* elemkind/elemtype + table_idx + active */
  1628. case 2:
  1629. case 6:
  1630. if (!load_table_index(&p, p_end, module,
  1631. &table_segment->table_index,
  1632. error_buf, error_buf_size))
  1633. return false;
  1634. #if WASM_ENABLE_MEMORY64 != 0
  1635. table_elem_idx_type =
  1636. is_table_64bit(module, table_segment->table_index)
  1637. ? VALUE_TYPE_I64
  1638. : VALUE_TYPE_I32;
  1639. #endif
  1640. if (!load_init_expr(
  1641. module, &p, p_end, &table_segment->base_offset,
  1642. table_elem_idx_type, error_buf, error_buf_size))
  1643. return false;
  1644. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1645. table_segment->mode == 2 ? true : false,
  1646. error_buf, error_buf_size))
  1647. return false;
  1648. if (table_segment->mode == 2) {
  1649. /* vec(funcidx) */
  1650. if (!load_func_index_vec(&p, p_end, module,
  1651. table_segment, error_buf,
  1652. error_buf_size))
  1653. return false;
  1654. }
  1655. else {
  1656. /* vec(expr) */
  1657. if (!load_init_expr_vec(&p, p_end, module,
  1658. table_segment, error_buf,
  1659. error_buf_size))
  1660. return false;
  1661. }
  1662. break;
  1663. case 5:
  1664. case 7:
  1665. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1666. false, error_buf, error_buf_size))
  1667. return false;
  1668. /* vec(expr) */
  1669. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  1670. error_buf, error_buf_size))
  1671. return false;
  1672. break;
  1673. default:
  1674. return false;
  1675. }
  1676. #else
  1677. /*
  1678. * like: 00 41 05 0b 04 00 01 00 01
  1679. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  1680. */
  1681. if (!load_table_index(&p, p_end, module,
  1682. &table_segment->table_index, error_buf,
  1683. error_buf_size))
  1684. return false;
  1685. #if WASM_ENABLE_MEMORY64 != 0
  1686. table_elem_idx_type =
  1687. is_table_64bit(module, table_segment->table_index)
  1688. ? VALUE_TYPE_I64
  1689. : VALUE_TYPE_I32;
  1690. #endif
  1691. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  1692. table_elem_idx_type, error_buf, error_buf_size))
  1693. return false;
  1694. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1695. error_buf, error_buf_size))
  1696. return false;
  1697. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1698. #if WASM_ENABLE_MEMORY64 != 0
  1699. if (table_elem_idx_type == VALUE_TYPE_I64
  1700. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  1701. set_error_buf(error_buf, error_buf_size,
  1702. "In table64, table base offset can't be "
  1703. "larger than UINT32_MAX");
  1704. return false;
  1705. }
  1706. #endif
  1707. }
  1708. }
  1709. (void)table_index;
  1710. (void)function_count;
  1711. bh_assert(p == p_end);
  1712. LOG_VERBOSE("Load table segment section success.\n");
  1713. return true;
  1714. }
  1715. static bool
  1716. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1717. WASMModule *module,
  1718. #if WASM_ENABLE_BULK_MEMORY != 0
  1719. bool has_datacount_section,
  1720. #endif
  1721. bool clone_data_seg, char *error_buf,
  1722. uint32 error_buf_size)
  1723. {
  1724. const uint8 *p = buf, *p_end = buf_end;
  1725. uint32 data_seg_count, i, mem_index, data_seg_len;
  1726. uint64 total_size;
  1727. WASMDataSeg *dataseg;
  1728. InitializerExpression init_expr;
  1729. #if WASM_ENABLE_BULK_MEMORY != 0
  1730. bool is_passive = false;
  1731. uint32 mem_flag;
  1732. #endif
  1733. uint8 mem_offset_type = VALUE_TYPE_I32;
  1734. read_leb_uint32(p, p_end, data_seg_count);
  1735. #if WASM_ENABLE_BULK_MEMORY != 0
  1736. bh_assert(!has_datacount_section
  1737. || data_seg_count == module->data_seg_count1);
  1738. #endif
  1739. if (data_seg_count) {
  1740. module->data_seg_count = data_seg_count;
  1741. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  1742. if (!(module->data_segments =
  1743. loader_malloc(total_size, error_buf, error_buf_size))) {
  1744. return false;
  1745. }
  1746. for (i = 0; i < data_seg_count; i++) {
  1747. read_leb_uint32(p, p_end, mem_index);
  1748. #if WASM_ENABLE_BULK_MEMORY != 0
  1749. is_passive = false;
  1750. mem_flag = mem_index & 0x03;
  1751. switch (mem_flag) {
  1752. case 0x01:
  1753. is_passive = true;
  1754. break;
  1755. case 0x00:
  1756. /* no memory index, treat index as 0 */
  1757. mem_index = 0;
  1758. goto check_mem_index;
  1759. case 0x02:
  1760. /* read following memory index */
  1761. read_leb_uint32(p, p_end, mem_index);
  1762. check_mem_index:
  1763. bh_assert(mem_index < module->import_memory_count
  1764. + module->memory_count);
  1765. break;
  1766. case 0x03:
  1767. default:
  1768. bh_assert(0);
  1769. break;
  1770. }
  1771. #else
  1772. bh_assert(mem_index
  1773. < module->import_memory_count + module->memory_count);
  1774. #endif /* WASM_ENABLE_BULK_MEMORY */
  1775. #if WASM_ENABLE_BULK_MEMORY != 0
  1776. if (!is_passive)
  1777. #endif /* WASM_ENABLE_BULK_MEMORY */
  1778. {
  1779. #if WASM_ENABLE_MEMORY64 != 0
  1780. /* This memory_flag is from memory instead of data segment */
  1781. uint8 memory_flag;
  1782. if (module->import_memory_count > 0) {
  1783. memory_flag = module->import_memories[mem_index]
  1784. .u.memory.mem_type.flags;
  1785. }
  1786. else {
  1787. memory_flag =
  1788. module
  1789. ->memories[mem_index - module->import_memory_count]
  1790. .flags;
  1791. }
  1792. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  1793. : VALUE_TYPE_I32;
  1794. #else
  1795. mem_offset_type = VALUE_TYPE_I32;
  1796. #endif /* WASM_ENABLE_MEMORY64 */
  1797. }
  1798. #if WASM_ENABLE_BULK_MEMORY != 0
  1799. if (!is_passive)
  1800. #endif
  1801. if (!load_init_expr(module, &p, p_end, &init_expr,
  1802. mem_offset_type, error_buf, error_buf_size))
  1803. return false;
  1804. read_leb_uint32(p, p_end, data_seg_len);
  1805. if (!(dataseg = module->data_segments[i] = loader_malloc(
  1806. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  1807. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1808. destroy_init_expr(&init_expr);
  1809. #endif
  1810. return false;
  1811. }
  1812. #if WASM_ENABLE_BULK_MEMORY != 0
  1813. dataseg->is_passive = is_passive;
  1814. if (!is_passive)
  1815. #endif
  1816. {
  1817. bh_memcpy_s(&dataseg->base_offset,
  1818. sizeof(InitializerExpression), &init_expr,
  1819. sizeof(InitializerExpression));
  1820. dataseg->memory_index = mem_index;
  1821. }
  1822. dataseg->data_length = data_seg_len;
  1823. CHECK_BUF(p, p_end, data_seg_len);
  1824. if (clone_data_seg) {
  1825. if (!(dataseg->data = loader_malloc(
  1826. dataseg->data_length, error_buf, error_buf_size))) {
  1827. return false;
  1828. }
  1829. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  1830. data_seg_len);
  1831. }
  1832. else {
  1833. dataseg->data = (uint8 *)p;
  1834. }
  1835. dataseg->is_data_cloned = clone_data_seg;
  1836. p += data_seg_len;
  1837. }
  1838. }
  1839. bh_assert(p == p_end);
  1840. LOG_VERBOSE("Load data segment section success.\n");
  1841. return true;
  1842. }
  1843. #if WASM_ENABLE_BULK_MEMORY != 0
  1844. static bool
  1845. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  1846. WASMModule *module, char *error_buf,
  1847. uint32 error_buf_size)
  1848. {
  1849. const uint8 *p = buf, *p_end = buf_end;
  1850. uint32 data_seg_count1 = 0;
  1851. read_leb_uint32(p, p_end, data_seg_count1);
  1852. module->data_seg_count1 = data_seg_count1;
  1853. bh_assert(p == p_end);
  1854. LOG_VERBOSE("Load datacount section success.\n");
  1855. return true;
  1856. }
  1857. #endif
  1858. static bool
  1859. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  1860. const uint8 *buf_func_end, WASMModule *module,
  1861. char *error_buf, uint32 error_buf_size)
  1862. {
  1863. const uint8 *p = buf, *p_end = buf_end;
  1864. const uint8 *p_func = buf_func;
  1865. uint32 func_count = 0, code_count;
  1866. /* code has been loaded in function section, so pass it here, just check
  1867. * whether function and code section have inconsistent lengths */
  1868. read_leb_uint32(p, p_end, code_count);
  1869. if (buf_func)
  1870. read_leb_uint32(p_func, buf_func_end, func_count);
  1871. bh_assert(func_count == code_count);
  1872. LOG_VERBOSE("Load code segment section success.\n");
  1873. (void)code_count;
  1874. (void)func_count;
  1875. return true;
  1876. }
  1877. static bool
  1878. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1879. char *error_buf, uint32 error_buf_size)
  1880. {
  1881. const uint8 *p = buf, *p_end = buf_end;
  1882. WASMFuncType *type;
  1883. uint32 start_function;
  1884. read_leb_uint32(p, p_end, start_function);
  1885. bh_assert(start_function
  1886. < module->function_count + module->import_function_count);
  1887. if (start_function < module->import_function_count)
  1888. type = module->import_functions[start_function].u.function.func_type;
  1889. else
  1890. type = module->functions[start_function - module->import_function_count]
  1891. ->func_type;
  1892. bh_assert(type->param_count == 0 && type->result_count == 0);
  1893. module->start_function = start_function;
  1894. bh_assert(p == p_end);
  1895. LOG_VERBOSE("Load start section success.\n");
  1896. (void)type;
  1897. return true;
  1898. }
  1899. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1900. static bool
  1901. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1902. bool is_load_from_file_buf, char *error_buf,
  1903. uint32 error_buf_size)
  1904. {
  1905. const uint8 *p = buf, *p_end = buf_end;
  1906. uint32 name_type, subsection_size;
  1907. uint32 previous_name_type = 0;
  1908. uint32 num_func_name;
  1909. uint32 func_index;
  1910. uint32 previous_func_index = ~0U;
  1911. uint32 func_name_len;
  1912. uint32 name_index;
  1913. int i = 0;
  1914. bh_assert(p < p_end);
  1915. while (p < p_end) {
  1916. read_leb_uint32(p, p_end, name_type);
  1917. if (i != 0) {
  1918. bh_assert(name_type > previous_name_type);
  1919. }
  1920. previous_name_type = name_type;
  1921. read_leb_uint32(p, p_end, subsection_size);
  1922. CHECK_BUF(p, p_end, subsection_size);
  1923. switch (name_type) {
  1924. case SUB_SECTION_TYPE_FUNC:
  1925. if (subsection_size) {
  1926. read_leb_uint32(p, p_end, num_func_name);
  1927. for (name_index = 0; name_index < num_func_name;
  1928. name_index++) {
  1929. read_leb_uint32(p, p_end, func_index);
  1930. bh_assert(func_index > previous_func_index);
  1931. previous_func_index = func_index;
  1932. read_leb_uint32(p, p_end, func_name_len);
  1933. CHECK_BUF(p, p_end, func_name_len);
  1934. /* Skip the import functions */
  1935. if (func_index >= module->import_function_count) {
  1936. func_index -= module->import_function_count;
  1937. bh_assert(func_index < module->function_count);
  1938. if (!(module->functions[func_index]->field_name =
  1939. wasm_const_str_list_insert(
  1940. p, func_name_len, module,
  1941. is_load_from_file_buf, error_buf,
  1942. error_buf_size))) {
  1943. return false;
  1944. }
  1945. }
  1946. p += func_name_len;
  1947. }
  1948. }
  1949. break;
  1950. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  1951. */
  1952. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  1953. default:
  1954. p = p + subsection_size;
  1955. break;
  1956. }
  1957. i++;
  1958. }
  1959. (void)previous_name_type;
  1960. (void)previous_func_index;
  1961. return true;
  1962. }
  1963. #endif
  1964. static bool
  1965. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1966. bool is_load_from_file_buf, char *error_buf,
  1967. uint32 error_buf_size)
  1968. {
  1969. const uint8 *p = buf, *p_end = buf_end;
  1970. uint32 name_len;
  1971. bh_assert(p < p_end);
  1972. read_leb_uint32(p, p_end, name_len);
  1973. bh_assert(name_len > 0 && p + name_len <= p_end);
  1974. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1975. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  1976. p += name_len;
  1977. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  1978. error_buf, error_buf_size)) {
  1979. return false;
  1980. }
  1981. }
  1982. #endif
  1983. LOG_VERBOSE("Load custom section success.\n");
  1984. (void)name_len;
  1985. return true;
  1986. }
  1987. static void
  1988. calculate_global_data_offset(WASMModule *module)
  1989. {
  1990. uint32 i, data_offset;
  1991. data_offset = 0;
  1992. for (i = 0; i < module->import_global_count; i++) {
  1993. WASMGlobalImport *import_global =
  1994. &((module->import_globals + i)->u.global);
  1995. #if WASM_ENABLE_FAST_JIT != 0
  1996. import_global->data_offset = data_offset;
  1997. #endif
  1998. data_offset += wasm_value_type_size(import_global->type.val_type);
  1999. }
  2000. for (i = 0; i < module->global_count; i++) {
  2001. WASMGlobal *global = module->globals + i;
  2002. #if WASM_ENABLE_FAST_JIT != 0
  2003. global->data_offset = data_offset;
  2004. #endif
  2005. data_offset += wasm_value_type_size(global->type.val_type);
  2006. }
  2007. module->global_data_size = data_offset;
  2008. }
  2009. #if WASM_ENABLE_FAST_JIT != 0
  2010. static bool
  2011. init_fast_jit_functions(WASMModule *module, char *error_buf,
  2012. uint32 error_buf_size)
  2013. {
  2014. #if WASM_ENABLE_LAZY_JIT != 0
  2015. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  2016. #endif
  2017. uint32 i;
  2018. if (!module->function_count)
  2019. return true;
  2020. if (!(module->fast_jit_func_ptrs =
  2021. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  2022. error_buf_size))) {
  2023. return false;
  2024. }
  2025. #if WASM_ENABLE_LAZY_JIT != 0
  2026. for (i = 0; i < module->function_count; i++) {
  2027. module->fast_jit_func_ptrs[i] =
  2028. jit_globals->compile_fast_jit_and_then_call;
  2029. }
  2030. #endif
  2031. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  2032. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  2033. set_error_buf(error_buf, error_buf_size,
  2034. "init fast jit thread lock failed");
  2035. return false;
  2036. }
  2037. module->fast_jit_thread_locks_inited[i] = true;
  2038. }
  2039. return true;
  2040. }
  2041. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2042. #if WASM_ENABLE_JIT != 0
  2043. static bool
  2044. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  2045. uint32 error_buf_size)
  2046. {
  2047. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  2048. AOTCompOption option = { 0 };
  2049. char *aot_last_error;
  2050. uint64 size;
  2051. bool gc_enabled = false; /* GC hasn't been enabled in mini loader */
  2052. if (module->function_count == 0)
  2053. return true;
  2054. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2055. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  2056. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  2057. return false;
  2058. }
  2059. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  2060. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  2061. os_mutex_destroy(&module->tierup_wait_lock);
  2062. return false;
  2063. }
  2064. module->tierup_wait_lock_inited = true;
  2065. #endif
  2066. size = sizeof(void *) * (uint64)module->function_count
  2067. + sizeof(bool) * (uint64)module->function_count;
  2068. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  2069. return false;
  2070. }
  2071. module->func_ptrs_compiled =
  2072. (bool *)((uint8 *)module->func_ptrs
  2073. + sizeof(void *) * module->function_count);
  2074. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  2075. if (!module->comp_data) {
  2076. aot_last_error = aot_get_last_error();
  2077. bh_assert(aot_last_error != NULL);
  2078. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2079. return false;
  2080. }
  2081. option.is_jit_mode = true;
  2082. option.opt_level = llvm_jit_options->opt_level;
  2083. option.size_level = llvm_jit_options->size_level;
  2084. option.segue_flags = llvm_jit_options->segue_flags;
  2085. option.quick_invoke_c_api_import =
  2086. llvm_jit_options->quick_invoke_c_api_import;
  2087. #if WASM_ENABLE_BULK_MEMORY != 0
  2088. option.enable_bulk_memory = true;
  2089. #endif
  2090. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  2091. option.enable_bulk_memory_opt = true;
  2092. #endif
  2093. #if WASM_ENABLE_THREAD_MGR != 0
  2094. option.enable_thread_mgr = true;
  2095. #endif
  2096. #if WASM_ENABLE_TAIL_CALL != 0
  2097. option.enable_tail_call = true;
  2098. #endif
  2099. #if WASM_ENABLE_SIMD != 0
  2100. option.enable_simd = true;
  2101. #endif
  2102. #if WASM_ENABLE_REF_TYPES != 0
  2103. option.enable_ref_types = true;
  2104. #endif
  2105. #if WASM_ENABLE_CALL_INDIRECT_OVERLONG != 0
  2106. option.enable_call_indirect_overlong = true;
  2107. #endif
  2108. option.enable_aux_stack_check = true;
  2109. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  2110. || WASM_ENABLE_AOT_STACK_FRAME != 0
  2111. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  2112. aot_call_stack_features_init_default(&option.call_stack_features);
  2113. #endif
  2114. #if WASM_ENABLE_PERF_PROFILING != 0
  2115. option.enable_perf_profiling = true;
  2116. #endif
  2117. #if WASM_ENABLE_MEMORY_PROFILING != 0
  2118. option.enable_memory_profiling = true;
  2119. option.enable_stack_estimation = true;
  2120. #endif
  2121. #if WASM_ENABLE_SHARED_HEAP != 0
  2122. option.enable_shared_heap = true;
  2123. #endif
  2124. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  2125. if (!module->comp_ctx) {
  2126. aot_last_error = aot_get_last_error();
  2127. bh_assert(aot_last_error != NULL);
  2128. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2129. return false;
  2130. }
  2131. return true;
  2132. }
  2133. static bool
  2134. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  2135. uint32 error_buf_size)
  2136. {
  2137. char *aot_last_error;
  2138. uint32 i;
  2139. if (module->function_count == 0)
  2140. return true;
  2141. if (!aot_compile_wasm(module->comp_ctx)) {
  2142. aot_last_error = aot_get_last_error();
  2143. bh_assert(aot_last_error != NULL);
  2144. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2145. return false;
  2146. }
  2147. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2148. if (module->orcjit_stop_compiling)
  2149. return false;
  2150. #endif
  2151. bh_print_time("Begin to lookup llvm jit functions");
  2152. for (i = 0; i < module->function_count; i++) {
  2153. LLVMOrcJITTargetAddress func_addr = 0;
  2154. LLVMErrorRef error;
  2155. char func_name[48];
  2156. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2157. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  2158. func_name);
  2159. if (error != LLVMErrorSuccess) {
  2160. char *err_msg = LLVMGetErrorMessage(error);
  2161. char buf[96];
  2162. snprintf(buf, sizeof(buf),
  2163. "failed to compile llvm jit function: %s", err_msg);
  2164. set_error_buf(error_buf, error_buf_size, buf);
  2165. LLVMDisposeErrorMessage(err_msg);
  2166. return false;
  2167. }
  2168. /**
  2169. * No need to lock the func_ptr[func_idx] here as it is basic
  2170. * data type, the load/store for it can be finished by one cpu
  2171. * instruction, and there can be only one cpu instruction
  2172. * loading/storing at the same time.
  2173. */
  2174. module->func_ptrs[i] = (void *)func_addr;
  2175. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2176. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  2177. if (module->orcjit_stop_compiling)
  2178. return false;
  2179. #endif
  2180. }
  2181. bh_print_time("End lookup llvm jit functions");
  2182. return true;
  2183. }
  2184. #endif /* end of WASM_ENABLE_JIT != 0 */
  2185. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2186. && WASM_ENABLE_LAZY_JIT != 0
  2187. static void *
  2188. init_llvm_jit_functions_stage2_callback(void *arg)
  2189. {
  2190. WASMModule *module = (WASMModule *)arg;
  2191. char error_buf[128];
  2192. uint32 error_buf_size = (uint32)sizeof(error_buf);
  2193. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  2194. module->orcjit_stop_compiling = true;
  2195. return NULL;
  2196. }
  2197. os_mutex_lock(&module->tierup_wait_lock);
  2198. module->llvm_jit_inited = true;
  2199. os_cond_broadcast(&module->tierup_wait_cond);
  2200. os_mutex_unlock(&module->tierup_wait_lock);
  2201. return NULL;
  2202. }
  2203. #endif
  2204. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2205. /* The callback function to compile jit functions */
  2206. static void *
  2207. orcjit_thread_callback(void *arg)
  2208. {
  2209. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  2210. #if WASM_ENABLE_JIT != 0
  2211. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  2212. #endif
  2213. WASMModule *module = thread_arg->module;
  2214. uint32 group_idx = thread_arg->group_idx;
  2215. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  2216. uint32 func_count = module->function_count;
  2217. uint32 i;
  2218. #if WASM_ENABLE_FAST_JIT != 0
  2219. /* Compile fast jit functions of this group */
  2220. for (i = group_idx; i < func_count; i += group_stride) {
  2221. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  2222. LOG_ERROR("failed to compile fast jit function %u\n", i);
  2223. break;
  2224. }
  2225. if (module->orcjit_stop_compiling) {
  2226. return NULL;
  2227. }
  2228. }
  2229. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2230. os_mutex_lock(&module->tierup_wait_lock);
  2231. module->fast_jit_ready_groups++;
  2232. os_mutex_unlock(&module->tierup_wait_lock);
  2233. #endif
  2234. #endif
  2235. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2236. && WASM_ENABLE_LAZY_JIT != 0
  2237. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  2238. for (i = group_idx; i < func_count;
  2239. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  2240. uint32 j;
  2241. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  2242. if (i + j * group_stride < func_count) {
  2243. if (!jit_compiler_set_call_to_fast_jit(
  2244. module,
  2245. i + j * group_stride + module->import_function_count)) {
  2246. LOG_ERROR(
  2247. "failed to compile call_to_fast_jit for func %u\n",
  2248. i + j * group_stride + module->import_function_count);
  2249. module->orcjit_stop_compiling = true;
  2250. return NULL;
  2251. }
  2252. }
  2253. if (module->orcjit_stop_compiling) {
  2254. return NULL;
  2255. }
  2256. }
  2257. }
  2258. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  2259. fast jit functions are compiled */
  2260. os_mutex_lock(&module->tierup_wait_lock);
  2261. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  2262. && module->fast_jit_ready_groups >= group_stride)) {
  2263. os_cond_reltimedwait(&module->tierup_wait_cond,
  2264. &module->tierup_wait_lock, 10000);
  2265. if (module->orcjit_stop_compiling) {
  2266. /* init_llvm_jit_functions_stage2 failed */
  2267. os_mutex_unlock(&module->tierup_wait_lock);
  2268. return NULL;
  2269. }
  2270. }
  2271. os_mutex_unlock(&module->tierup_wait_lock);
  2272. #endif
  2273. #if WASM_ENABLE_JIT != 0
  2274. /* Compile llvm jit functions of this group */
  2275. for (i = group_idx; i < func_count;
  2276. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  2277. LLVMOrcJITTargetAddress func_addr = 0;
  2278. LLVMErrorRef error;
  2279. char func_name[48];
  2280. typedef void (*F)(void);
  2281. union {
  2282. F f;
  2283. void *v;
  2284. } u;
  2285. uint32 j;
  2286. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  2287. "_wrapper");
  2288. LOG_DEBUG("compile llvm jit func %s", func_name);
  2289. error =
  2290. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  2291. if (error != LLVMErrorSuccess) {
  2292. char *err_msg = LLVMGetErrorMessage(error);
  2293. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  2294. LLVMDisposeErrorMessage(err_msg);
  2295. break;
  2296. }
  2297. /* Call the jit wrapper function to trigger its compilation, so as
  2298. to compile the actual jit functions, since we add the latter to
  2299. function list in the PartitionFunction callback */
  2300. u.v = (void *)func_addr;
  2301. u.f();
  2302. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  2303. if (i + j * group_stride < func_count) {
  2304. module->func_ptrs_compiled[i + j * group_stride] = true;
  2305. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2306. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  2307. i + j * group_stride);
  2308. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  2309. func_name);
  2310. if (error != LLVMErrorSuccess) {
  2311. char *err_msg = LLVMGetErrorMessage(error);
  2312. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  2313. err_msg);
  2314. LLVMDisposeErrorMessage(err_msg);
  2315. /* Ignore current llvm jit func, as its func ptr is
  2316. previous set to call_to_fast_jit, which also works */
  2317. continue;
  2318. }
  2319. jit_compiler_set_llvm_jit_func_ptr(
  2320. module,
  2321. i + j * group_stride + module->import_function_count,
  2322. (void *)func_addr);
  2323. /* Try to switch to call this llvm jit function instead of
  2324. fast jit function from fast jit jitted code */
  2325. jit_compiler_set_call_to_llvm_jit(
  2326. module,
  2327. i + j * group_stride + module->import_function_count);
  2328. #endif
  2329. }
  2330. }
  2331. if (module->orcjit_stop_compiling) {
  2332. break;
  2333. }
  2334. }
  2335. #endif
  2336. return NULL;
  2337. }
  2338. static void
  2339. orcjit_stop_compile_threads(WASMModule *module)
  2340. {
  2341. #if WASM_ENABLE_LAZY_JIT != 0
  2342. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  2343. / sizeof(OrcJitThreadArg));
  2344. module->orcjit_stop_compiling = true;
  2345. for (i = 0; i < thread_num; i++) {
  2346. if (module->orcjit_threads[i])
  2347. os_thread_join(module->orcjit_threads[i], NULL);
  2348. }
  2349. #endif
  2350. }
  2351. static bool
  2352. compile_jit_functions(WASMModule *module, char *error_buf,
  2353. uint32 error_buf_size)
  2354. {
  2355. uint32 thread_num =
  2356. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  2357. uint32 i, j;
  2358. bh_print_time("Begin to compile jit functions");
  2359. /* Create threads to compile the jit functions */
  2360. for (i = 0; i < thread_num && i < module->function_count; i++) {
  2361. #if WASM_ENABLE_JIT != 0
  2362. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  2363. #endif
  2364. module->orcjit_thread_args[i].module = module;
  2365. module->orcjit_thread_args[i].group_idx = i;
  2366. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  2367. (void *)&module->orcjit_thread_args[i],
  2368. APP_THREAD_STACK_SIZE_DEFAULT)
  2369. != 0) {
  2370. set_error_buf(error_buf, error_buf_size,
  2371. "create orcjit compile thread failed");
  2372. /* Terminate the threads created */
  2373. module->orcjit_stop_compiling = true;
  2374. for (j = 0; j < i; j++) {
  2375. os_thread_join(module->orcjit_threads[j], NULL);
  2376. }
  2377. return false;
  2378. }
  2379. }
  2380. #if WASM_ENABLE_LAZY_JIT == 0
  2381. /* Wait until all jit functions are compiled for eager mode */
  2382. for (i = 0; i < thread_num; i++) {
  2383. if (module->orcjit_threads[i])
  2384. os_thread_join(module->orcjit_threads[i], NULL);
  2385. }
  2386. #if WASM_ENABLE_FAST_JIT != 0
  2387. /* Ensure all the fast-jit functions are compiled */
  2388. for (i = 0; i < module->function_count; i++) {
  2389. if (!jit_compiler_is_compiled(module,
  2390. i + module->import_function_count)) {
  2391. set_error_buf(error_buf, error_buf_size,
  2392. "failed to compile fast jit function");
  2393. return false;
  2394. }
  2395. }
  2396. #endif
  2397. #if WASM_ENABLE_JIT != 0
  2398. /* Ensure all the llvm-jit functions are compiled */
  2399. for (i = 0; i < module->function_count; i++) {
  2400. if (!module->func_ptrs_compiled[i]) {
  2401. set_error_buf(error_buf, error_buf_size,
  2402. "failed to compile llvm jit function");
  2403. return false;
  2404. }
  2405. }
  2406. #endif
  2407. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  2408. bh_print_time("End compile jit functions");
  2409. return true;
  2410. }
  2411. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  2412. #if WASM_ENABLE_REF_TYPES != 0
  2413. static bool
  2414. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  2415. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  2416. {
  2417. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  2418. return false;
  2419. }
  2420. if (p_elem_type) {
  2421. if (table_idx < module->import_table_count)
  2422. *p_elem_type =
  2423. module->import_tables[table_idx].u.table.table_type.elem_type;
  2424. else
  2425. *p_elem_type =
  2426. module->tables[table_idx - module->import_table_count]
  2427. .table_type.elem_type;
  2428. }
  2429. return true;
  2430. }
  2431. static bool
  2432. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  2433. uint8 *p_elem_type, char *error_buf,
  2434. uint32 error_buf_size)
  2435. {
  2436. if (table_seg_idx >= module->table_seg_count) {
  2437. return false;
  2438. }
  2439. if (p_elem_type) {
  2440. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  2441. }
  2442. return true;
  2443. }
  2444. #endif
  2445. static bool
  2446. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2447. uint32 cur_func_idx, char *error_buf,
  2448. uint32 error_buf_size);
  2449. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2450. void **
  2451. wasm_interp_get_handle_table(void);
  2452. static void **handle_table;
  2453. #endif
  2454. static bool
  2455. load_from_sections(WASMModule *module, WASMSection *sections,
  2456. bool is_load_from_file_buf, bool wasm_binary_freeable,
  2457. char *error_buf, uint32 error_buf_size)
  2458. {
  2459. WASMExport *export;
  2460. WASMSection *section = sections;
  2461. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  2462. *buf_func = NULL, *buf_func_end = NULL;
  2463. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  2464. WASMGlobal *aux_stack_top_global = NULL, *global;
  2465. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  2466. aux_stack_top = (uint64)-1LL;
  2467. uint32 global_index, func_index, i;
  2468. uint32 aux_data_end_global_index = (uint32)-1;
  2469. uint32 aux_heap_base_global_index = (uint32)-1;
  2470. WASMFuncType *func_type;
  2471. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  2472. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  2473. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  2474. #if WASM_ENABLE_BULK_MEMORY != 0
  2475. bool has_datacount_section = false;
  2476. #endif
  2477. /* Find code and function sections if have */
  2478. while (section) {
  2479. if (section->section_type == SECTION_TYPE_CODE) {
  2480. buf_code = section->section_body;
  2481. buf_code_end = buf_code + section->section_body_size;
  2482. }
  2483. else if (section->section_type == SECTION_TYPE_FUNC) {
  2484. buf_func = section->section_body;
  2485. buf_func_end = buf_func + section->section_body_size;
  2486. }
  2487. section = section->next;
  2488. }
  2489. section = sections;
  2490. while (section) {
  2491. buf = section->section_body;
  2492. buf_end = buf + section->section_body_size;
  2493. LOG_DEBUG("load section, type: %d", section->section_type);
  2494. switch (section->section_type) {
  2495. case SECTION_TYPE_USER:
  2496. /* unsupported user section, ignore it. */
  2497. if (!load_user_section(buf, buf_end, module,
  2498. reuse_const_strings, error_buf,
  2499. error_buf_size))
  2500. return false;
  2501. break;
  2502. case SECTION_TYPE_TYPE:
  2503. if (!load_type_section(buf, buf_end, module, error_buf,
  2504. error_buf_size))
  2505. return false;
  2506. break;
  2507. case SECTION_TYPE_IMPORT:
  2508. if (!load_import_section(buf, buf_end, module,
  2509. reuse_const_strings, error_buf,
  2510. error_buf_size))
  2511. return false;
  2512. break;
  2513. case SECTION_TYPE_FUNC:
  2514. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2515. module, error_buf, error_buf_size))
  2516. return false;
  2517. break;
  2518. case SECTION_TYPE_TABLE:
  2519. if (!load_table_section(buf, buf_end, module, error_buf,
  2520. error_buf_size))
  2521. return false;
  2522. break;
  2523. case SECTION_TYPE_MEMORY:
  2524. if (!load_memory_section(buf, buf_end, module, error_buf,
  2525. error_buf_size))
  2526. return false;
  2527. break;
  2528. case SECTION_TYPE_GLOBAL:
  2529. if (!load_global_section(buf, buf_end, module, error_buf,
  2530. error_buf_size))
  2531. return false;
  2532. break;
  2533. case SECTION_TYPE_EXPORT:
  2534. if (!load_export_section(buf, buf_end, module,
  2535. reuse_const_strings, error_buf,
  2536. error_buf_size))
  2537. return false;
  2538. break;
  2539. case SECTION_TYPE_START:
  2540. if (!load_start_section(buf, buf_end, module, error_buf,
  2541. error_buf_size))
  2542. return false;
  2543. break;
  2544. case SECTION_TYPE_ELEM:
  2545. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  2546. error_buf_size))
  2547. return false;
  2548. break;
  2549. case SECTION_TYPE_CODE:
  2550. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2551. module, error_buf, error_buf_size))
  2552. return false;
  2553. break;
  2554. case SECTION_TYPE_DATA:
  2555. if (!load_data_segment_section(buf, buf_end, module,
  2556. #if WASM_ENABLE_BULK_MEMORY != 0
  2557. has_datacount_section,
  2558. #endif
  2559. clone_data_seg, error_buf,
  2560. error_buf_size))
  2561. return false;
  2562. break;
  2563. #if WASM_ENABLE_BULK_MEMORY != 0
  2564. case SECTION_TYPE_DATACOUNT:
  2565. if (!load_datacount_section(buf, buf_end, module, error_buf,
  2566. error_buf_size))
  2567. return false;
  2568. has_datacount_section = true;
  2569. break;
  2570. #endif
  2571. default:
  2572. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2573. return false;
  2574. }
  2575. section = section->next;
  2576. }
  2577. #if WASM_ENABLE_BULK_MEMORY != 0
  2578. bh_assert(!has_datacount_section
  2579. || module->data_seg_count == module->data_seg_count1);
  2580. #endif
  2581. module->aux_data_end_global_index = (uint32)-1;
  2582. module->aux_heap_base_global_index = (uint32)-1;
  2583. module->aux_stack_top_global_index = (uint32)-1;
  2584. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2585. export = module->exports;
  2586. for (i = 0; i < module->export_count; i++, export ++) {
  2587. if (export->kind == EXPORT_KIND_GLOBAL) {
  2588. if (!strcmp(export->name, "__heap_base")) {
  2589. if (export->index < module->import_global_count) {
  2590. LOG_DEBUG("Skip the process if __heap_base is imported "
  2591. "instead of being a local global");
  2592. continue;
  2593. }
  2594. global_index = export->index - module->import_global_count;
  2595. global = module->globals + global_index;
  2596. if (global->type.val_type == VALUE_TYPE_I32
  2597. && !global->type.is_mutable
  2598. && global->init_expr.init_expr_type
  2599. == INIT_EXPR_TYPE_I32_CONST) {
  2600. aux_heap_base_global = global;
  2601. aux_heap_base =
  2602. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  2603. aux_heap_base_global_index = export->index;
  2604. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  2605. aux_heap_base);
  2606. }
  2607. }
  2608. else if (!strcmp(export->name, "__data_end")) {
  2609. if (export->index < module->import_global_count) {
  2610. LOG_DEBUG("Skip the process if __data_end is imported "
  2611. "instead of being a local global");
  2612. continue;
  2613. }
  2614. global_index = export->index - module->import_global_count;
  2615. global = module->globals + global_index;
  2616. if (global->type.val_type == VALUE_TYPE_I32
  2617. && !global->type.is_mutable
  2618. && global->init_expr.init_expr_type
  2619. == INIT_EXPR_TYPE_I32_CONST) {
  2620. aux_data_end_global = global;
  2621. aux_data_end =
  2622. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  2623. aux_data_end_global_index = export->index;
  2624. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  2625. aux_data_end);
  2626. aux_data_end = align_uint64(aux_data_end, 16);
  2627. }
  2628. }
  2629. /* For module compiled with -pthread option, the global is:
  2630. [0] stack_top <-- 0
  2631. [1] tls_pointer
  2632. [2] tls_size
  2633. [3] data_end <-- 3
  2634. [4] global_base
  2635. [5] heap_base <-- 5
  2636. [6] dso_handle
  2637. For module compiled without -pthread option:
  2638. [0] stack_top <-- 0
  2639. [1] data_end <-- 1
  2640. [2] global_base
  2641. [3] heap_base <-- 3
  2642. [4] dso_handle
  2643. */
  2644. if (aux_data_end_global && aux_heap_base_global
  2645. && aux_data_end <= aux_heap_base) {
  2646. module->aux_data_end_global_index = aux_data_end_global_index;
  2647. module->aux_data_end = aux_data_end;
  2648. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2649. module->aux_heap_base = aux_heap_base;
  2650. /* Resolve aux stack top global */
  2651. for (global_index = 0; global_index < module->global_count;
  2652. global_index++) {
  2653. global = module->globals + global_index;
  2654. if (global->type.is_mutable /* heap_base and data_end is
  2655. not mutable */
  2656. && global->type.val_type == VALUE_TYPE_I32
  2657. && global->init_expr.init_expr_type
  2658. == INIT_EXPR_TYPE_I32_CONST
  2659. && (uint64)(uint32)global->init_expr.u.unary.v.i32
  2660. <= aux_heap_base) {
  2661. aux_stack_top_global = global;
  2662. aux_stack_top =
  2663. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  2664. module->aux_stack_top_global_index =
  2665. module->import_global_count + global_index;
  2666. module->aux_stack_bottom = aux_stack_top;
  2667. module->aux_stack_size =
  2668. aux_stack_top > aux_data_end
  2669. ? (uint32)(aux_stack_top - aux_data_end)
  2670. : (uint32)aux_stack_top;
  2671. LOG_VERBOSE(
  2672. "Found aux stack top global, value: %" PRIu64 ", "
  2673. "global index: %d, stack size: %d",
  2674. aux_stack_top, global_index,
  2675. module->aux_stack_size);
  2676. break;
  2677. }
  2678. }
  2679. if (!aux_stack_top_global) {
  2680. /* Auxiliary stack global isn't found, it must be unused
  2681. in the wasm app, as if it is used, the global must be
  2682. defined. Here we set it to __heap_base global and set
  2683. its size to 0. */
  2684. aux_stack_top_global = aux_heap_base_global;
  2685. aux_stack_top = aux_heap_base;
  2686. module->aux_stack_top_global_index =
  2687. module->aux_heap_base_global_index;
  2688. module->aux_stack_bottom = aux_stack_top;
  2689. module->aux_stack_size = 0;
  2690. }
  2691. break;
  2692. }
  2693. }
  2694. }
  2695. module->malloc_function = (uint32)-1;
  2696. module->free_function = (uint32)-1;
  2697. module->retain_function = (uint32)-1;
  2698. /* Resolve malloc/free function exported by wasm module */
  2699. #if WASM_ENABLE_MEMORY64 != 0
  2700. if (has_module_memory64(module))
  2701. malloc_free_io_type = VALUE_TYPE_I64;
  2702. #endif
  2703. export = module->exports;
  2704. for (i = 0; i < module->export_count; i++, export ++) {
  2705. if (export->kind == EXPORT_KIND_FUNC) {
  2706. if (!strcmp(export->name, "malloc")
  2707. && export->index >= module->import_function_count) {
  2708. func_index = export->index - module->import_function_count;
  2709. func_type = module->functions[func_index]->func_type;
  2710. if (func_type->param_count == 1 && func_type->result_count == 1
  2711. && func_type->types[0] == malloc_free_io_type
  2712. && func_type->types[1] == malloc_free_io_type) {
  2713. bh_assert(module->malloc_function == (uint32)-1);
  2714. module->malloc_function = export->index;
  2715. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2716. export->name, export->index);
  2717. }
  2718. }
  2719. else if (!strcmp(export->name, "__new")
  2720. && export->index >= module->import_function_count) {
  2721. /* __new && __pin for AssemblyScript */
  2722. func_index = export->index - module->import_function_count;
  2723. func_type = module->functions[func_index]->func_type;
  2724. if (func_type->param_count == 2 && func_type->result_count == 1
  2725. && func_type->types[0] == malloc_free_io_type
  2726. && func_type->types[1] == VALUE_TYPE_I32
  2727. && func_type->types[2] == malloc_free_io_type) {
  2728. uint32 j;
  2729. WASMExport *export_tmp;
  2730. bh_assert(module->malloc_function == (uint32)-1);
  2731. module->malloc_function = export->index;
  2732. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2733. export->name, export->index);
  2734. /* resolve retain function.
  2735. If not found, reset malloc function index */
  2736. export_tmp = module->exports;
  2737. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2738. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2739. && (!strcmp(export_tmp->name, "__retain")
  2740. || !strcmp(export_tmp->name, "__pin"))
  2741. && (export_tmp->index
  2742. >= module->import_function_count)) {
  2743. func_index = export_tmp->index
  2744. - module->import_function_count;
  2745. func_type =
  2746. module->functions[func_index]->func_type;
  2747. if (func_type->param_count == 1
  2748. && func_type->result_count == 1
  2749. && func_type->types[0] == malloc_free_io_type
  2750. && func_type->types[1] == malloc_free_io_type) {
  2751. bh_assert(module->retain_function
  2752. == (uint32)-1);
  2753. module->retain_function = export_tmp->index;
  2754. LOG_VERBOSE("Found retain function, name: %s, "
  2755. "index: %u",
  2756. export_tmp->name,
  2757. export_tmp->index);
  2758. break;
  2759. }
  2760. }
  2761. }
  2762. if (j == module->export_count) {
  2763. module->malloc_function = (uint32)-1;
  2764. LOG_VERBOSE("Can't find retain function,"
  2765. "reset malloc function index to -1");
  2766. }
  2767. }
  2768. }
  2769. else if (((!strcmp(export->name, "free"))
  2770. || (!strcmp(export->name, "__release"))
  2771. || (!strcmp(export->name, "__unpin")))
  2772. && export->index >= module->import_function_count) {
  2773. func_index = export->index - module->import_function_count;
  2774. func_type = module->functions[func_index]->func_type;
  2775. if (func_type->param_count == 1 && func_type->result_count == 0
  2776. && func_type->types[0] == malloc_free_io_type) {
  2777. bh_assert(module->free_function == (uint32)-1);
  2778. module->free_function = export->index;
  2779. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2780. export->name, export->index);
  2781. }
  2782. }
  2783. }
  2784. }
  2785. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2786. handle_table = wasm_interp_get_handle_table();
  2787. #endif
  2788. for (i = 0; i < module->function_count; i++) {
  2789. WASMFunction *func = module->functions[i];
  2790. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  2791. error_buf_size)) {
  2792. return false;
  2793. }
  2794. if (i == module->function_count - 1) {
  2795. bh_assert(func->code + func->code_size == buf_code_end);
  2796. }
  2797. }
  2798. if (!module->possible_memory_grow) {
  2799. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  2800. if (aux_data_end_global && aux_heap_base_global
  2801. && aux_stack_top_global) {
  2802. uint64 init_memory_size;
  2803. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  2804. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  2805. * valid range of uint32 */
  2806. if (shrunk_memory_size <= UINT32_MAX) {
  2807. if (module->import_memory_count) {
  2808. WASMMemoryImport *memory_import =
  2809. &module->import_memories[0].u.memory;
  2810. init_memory_size =
  2811. (uint64)memory_import->mem_type.num_bytes_per_page
  2812. * memory_import->mem_type.init_page_count;
  2813. if (shrunk_memory_size <= init_memory_size) {
  2814. /* Reset memory info to decrease memory usage */
  2815. memory_import->mem_type.num_bytes_per_page =
  2816. shrunk_memory_size;
  2817. memory_import->mem_type.init_page_count = 1;
  2818. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  2819. shrunk_memory_size);
  2820. }
  2821. }
  2822. if (module->memory_count) {
  2823. WASMMemory *memory = &module->memories[0];
  2824. init_memory_size = (uint64)memory->num_bytes_per_page
  2825. * memory->init_page_count;
  2826. if (shrunk_memory_size <= init_memory_size) {
  2827. /* Reset memory info to decrease memory usage */
  2828. memory->num_bytes_per_page = shrunk_memory_size;
  2829. memory->init_page_count = 1;
  2830. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  2831. shrunk_memory_size);
  2832. }
  2833. }
  2834. }
  2835. }
  2836. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  2837. if (module->import_memory_count) {
  2838. WASMMemoryImport *memory_import =
  2839. &module->import_memories[0].u.memory;
  2840. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  2841. memory_import->mem_type.num_bytes_per_page *=
  2842. memory_import->mem_type.init_page_count;
  2843. if (memory_import->mem_type.init_page_count > 0)
  2844. memory_import->mem_type.init_page_count =
  2845. memory_import->mem_type.max_page_count = 1;
  2846. else
  2847. memory_import->mem_type.init_page_count =
  2848. memory_import->mem_type.max_page_count = 0;
  2849. }
  2850. }
  2851. if (module->memory_count) {
  2852. WASMMemory *memory = &module->memories[0];
  2853. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  2854. memory->num_bytes_per_page *= memory->init_page_count;
  2855. if (memory->init_page_count > 0)
  2856. memory->init_page_count = memory->max_page_count = 1;
  2857. else
  2858. memory->init_page_count = memory->max_page_count = 0;
  2859. }
  2860. }
  2861. }
  2862. #if WASM_ENABLE_MEMORY64 != 0
  2863. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  2864. false))
  2865. return false;
  2866. #endif
  2867. calculate_global_data_offset(module);
  2868. #if WASM_ENABLE_FAST_JIT != 0
  2869. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  2870. return false;
  2871. }
  2872. #endif
  2873. #if WASM_ENABLE_JIT != 0
  2874. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  2875. return false;
  2876. }
  2877. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  2878. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  2879. return false;
  2880. }
  2881. #else
  2882. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  2883. thread fast jit execution, since applying llvm optimizations in
  2884. aot_compile_wasm may cost a lot of time.
  2885. Create thread with enough native stack to apply llvm optimizations */
  2886. if (os_thread_create(&module->llvm_jit_init_thread,
  2887. init_llvm_jit_functions_stage2_callback,
  2888. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  2889. != 0) {
  2890. set_error_buf(error_buf, error_buf_size,
  2891. "create orcjit compile thread failed");
  2892. return false;
  2893. }
  2894. #endif
  2895. #endif
  2896. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2897. /* Create threads to compile the jit functions */
  2898. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  2899. return false;
  2900. }
  2901. #endif
  2902. #if WASM_ENABLE_MEMORY_TRACING != 0
  2903. wasm_runtime_dump_module_mem_consumption(module);
  2904. #endif
  2905. return true;
  2906. }
  2907. static WASMModule *
  2908. create_module(char *name, char *error_buf, uint32 error_buf_size)
  2909. {
  2910. WASMModule *module =
  2911. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  2912. bh_list_status ret;
  2913. if (!module) {
  2914. return NULL;
  2915. }
  2916. module->module_type = Wasm_Module_Bytecode;
  2917. /* Set start_function to -1, means no start function */
  2918. module->start_function = (uint32)-1;
  2919. module->name = name;
  2920. module->is_binary_freeable = false;
  2921. #if WASM_ENABLE_FAST_INTERP == 0
  2922. module->br_table_cache_list = &module->br_table_cache_list_head;
  2923. ret = bh_list_init(module->br_table_cache_list);
  2924. bh_assert(ret == BH_LIST_SUCCESS);
  2925. #endif
  2926. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2927. && WASM_ENABLE_LAZY_JIT != 0
  2928. if (os_mutex_init(&module->instance_list_lock) != 0) {
  2929. set_error_buf(error_buf, error_buf_size,
  2930. "init instance list lock failed");
  2931. wasm_runtime_free(module);
  2932. return NULL;
  2933. }
  2934. #endif
  2935. #if WASM_ENABLE_LIBC_WASI != 0
  2936. wasi_args_set_defaults(&module->wasi_args);
  2937. #endif /* WASM_ENABLE_LIBC_WASI != 0 */
  2938. (void)ret;
  2939. return module;
  2940. }
  2941. WASMModule *
  2942. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  2943. uint32 error_buf_size)
  2944. {
  2945. WASMModule *module = create_module("", error_buf, error_buf_size);
  2946. if (!module)
  2947. return NULL;
  2948. if (!load_from_sections(module, section_list, false, true, error_buf,
  2949. error_buf_size)) {
  2950. wasm_loader_unload(module);
  2951. return NULL;
  2952. }
  2953. LOG_VERBOSE("Load module from sections success.\n");
  2954. return module;
  2955. }
  2956. static void
  2957. destroy_sections(WASMSection *section_list)
  2958. {
  2959. WASMSection *section = section_list, *next;
  2960. while (section) {
  2961. next = section->next;
  2962. wasm_runtime_free(section);
  2963. section = next;
  2964. }
  2965. }
  2966. /* clang-format off */
  2967. static uint8 section_ids[] = {
  2968. SECTION_TYPE_USER,
  2969. SECTION_TYPE_TYPE,
  2970. SECTION_TYPE_IMPORT,
  2971. SECTION_TYPE_FUNC,
  2972. SECTION_TYPE_TABLE,
  2973. SECTION_TYPE_MEMORY,
  2974. SECTION_TYPE_GLOBAL,
  2975. SECTION_TYPE_EXPORT,
  2976. SECTION_TYPE_START,
  2977. SECTION_TYPE_ELEM,
  2978. #if WASM_ENABLE_BULK_MEMORY != 0
  2979. SECTION_TYPE_DATACOUNT,
  2980. #endif
  2981. SECTION_TYPE_CODE,
  2982. SECTION_TYPE_DATA
  2983. };
  2984. /* clang-format on */
  2985. static uint8
  2986. get_section_index(uint8 section_type)
  2987. {
  2988. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  2989. for (uint8 i = 0; i < max_id; i++) {
  2990. if (section_type == section_ids[i])
  2991. return i;
  2992. }
  2993. return (uint8)-1;
  2994. }
  2995. static bool
  2996. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  2997. char *error_buf, uint32 error_buf_size)
  2998. {
  2999. WASMSection *section_list_end = NULL, *section;
  3000. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  3001. uint8 section_type, section_index, last_section_index = (uint8)-1;
  3002. uint32 section_size;
  3003. bh_assert(!*p_section_list);
  3004. p += 8;
  3005. while (p < p_end) {
  3006. CHECK_BUF(p, p_end, 1);
  3007. section_type = read_uint8(p);
  3008. section_index = get_section_index(section_type);
  3009. if (section_index != (uint8)-1) {
  3010. if (section_type != SECTION_TYPE_USER) {
  3011. /* Custom sections may be inserted at any place,
  3012. while other sections must occur at most once
  3013. and in prescribed order. */
  3014. bh_assert(last_section_index == (uint8)-1
  3015. || last_section_index < section_index);
  3016. last_section_index = section_index;
  3017. }
  3018. read_leb_uint32(p, p_end, section_size);
  3019. CHECK_BUF1(p, p_end, section_size);
  3020. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  3021. error_buf_size))) {
  3022. return false;
  3023. }
  3024. section->section_type = section_type;
  3025. section->section_body = (uint8 *)p;
  3026. section->section_body_size = section_size;
  3027. if (!*p_section_list)
  3028. *p_section_list = section_list_end = section;
  3029. else {
  3030. section_list_end->next = section;
  3031. section_list_end = section;
  3032. }
  3033. p += section_size;
  3034. }
  3035. else {
  3036. bh_assert(0);
  3037. }
  3038. }
  3039. (void)last_section_index;
  3040. return true;
  3041. }
  3042. static void
  3043. exchange32(uint8 *p_data)
  3044. {
  3045. uint8 value = *p_data;
  3046. *p_data = *(p_data + 3);
  3047. *(p_data + 3) = value;
  3048. value = *(p_data + 1);
  3049. *(p_data + 1) = *(p_data + 2);
  3050. *(p_data + 2) = value;
  3051. }
  3052. static union {
  3053. int a;
  3054. char b;
  3055. } __ue = { .a = 1 };
  3056. #define is_little_endian() (__ue.b == 1)
  3057. static bool
  3058. load(const uint8 *buf, uint32 size, WASMModule *module,
  3059. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  3060. {
  3061. const uint8 *buf_end = buf + size;
  3062. const uint8 *p = buf, *p_end = buf_end;
  3063. uint32 magic_number, version;
  3064. WASMSection *section_list = NULL;
  3065. CHECK_BUF1(p, p_end, sizeof(uint32));
  3066. magic_number = read_uint32(p);
  3067. if (!is_little_endian())
  3068. exchange32((uint8 *)&magic_number);
  3069. bh_assert(magic_number == WASM_MAGIC_NUMBER);
  3070. CHECK_BUF1(p, p_end, sizeof(uint32));
  3071. version = read_uint32(p);
  3072. if (!is_little_endian())
  3073. exchange32((uint8 *)&version);
  3074. if (version != WASM_CURRENT_VERSION) {
  3075. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  3076. return false;
  3077. }
  3078. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  3079. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  3080. error_buf, error_buf_size)) {
  3081. destroy_sections(section_list);
  3082. return false;
  3083. }
  3084. destroy_sections(section_list);
  3085. (void)p_end;
  3086. return true;
  3087. }
  3088. WASMModule *
  3089. wasm_loader_load(uint8 *buf, uint32 size,
  3090. #if WASM_ENABLE_MULTI_MODULE != 0
  3091. bool main_module,
  3092. #endif
  3093. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  3094. {
  3095. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  3096. if (!module) {
  3097. return NULL;
  3098. }
  3099. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  3100. || WASM_ENABLE_JIT != 0
  3101. module->load_addr = (uint8 *)buf;
  3102. module->load_size = size;
  3103. #endif
  3104. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  3105. error_buf_size)) {
  3106. goto fail;
  3107. }
  3108. #if WASM_ENABLE_MULTI_MODULE != 0
  3109. (void)main_module;
  3110. #endif
  3111. LOG_VERBOSE("Load module success.\n");
  3112. return module;
  3113. fail:
  3114. wasm_loader_unload(module);
  3115. return NULL;
  3116. }
  3117. void
  3118. wasm_loader_unload(WASMModule *module)
  3119. {
  3120. uint32 i;
  3121. if (!module)
  3122. return;
  3123. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3124. && WASM_ENABLE_LAZY_JIT != 0
  3125. module->orcjit_stop_compiling = true;
  3126. if (module->llvm_jit_init_thread)
  3127. os_thread_join(module->llvm_jit_init_thread, NULL);
  3128. #endif
  3129. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3130. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  3131. module internal data after they were freed */
  3132. orcjit_stop_compile_threads(module);
  3133. #endif
  3134. #if WASM_ENABLE_JIT != 0
  3135. if (module->func_ptrs)
  3136. wasm_runtime_free(module->func_ptrs);
  3137. if (module->comp_ctx)
  3138. aot_destroy_comp_context(module->comp_ctx);
  3139. if (module->comp_data)
  3140. aot_destroy_comp_data(module->comp_data);
  3141. #endif
  3142. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3143. && WASM_ENABLE_LAZY_JIT != 0
  3144. if (module->tierup_wait_lock_inited) {
  3145. os_mutex_destroy(&module->tierup_wait_lock);
  3146. os_cond_destroy(&module->tierup_wait_cond);
  3147. }
  3148. #endif
  3149. if (module->types) {
  3150. for (i = 0; i < module->type_count; i++) {
  3151. if (module->types[i])
  3152. destroy_wasm_type(module->types[i]);
  3153. }
  3154. wasm_runtime_free(module->types);
  3155. }
  3156. if (module->imports)
  3157. wasm_runtime_free(module->imports);
  3158. if (module->functions) {
  3159. for (i = 0; i < module->function_count; i++) {
  3160. if (module->functions[i]) {
  3161. if (module->functions[i]->local_offsets)
  3162. wasm_runtime_free(module->functions[i]->local_offsets);
  3163. #if WASM_ENABLE_FAST_INTERP != 0
  3164. if (module->functions[i]->code_compiled)
  3165. wasm_runtime_free(module->functions[i]->code_compiled);
  3166. if (module->functions[i]->consts)
  3167. wasm_runtime_free(module->functions[i]->consts);
  3168. #endif
  3169. #if WASM_ENABLE_FAST_JIT != 0
  3170. if (module->functions[i]->fast_jit_jitted_code) {
  3171. jit_code_cache_free(
  3172. module->functions[i]->fast_jit_jitted_code);
  3173. }
  3174. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3175. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  3176. jit_code_cache_free(
  3177. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  3178. }
  3179. #endif
  3180. #endif
  3181. wasm_runtime_free(module->functions[i]);
  3182. }
  3183. }
  3184. wasm_runtime_free(module->functions);
  3185. }
  3186. if (module->tables)
  3187. wasm_runtime_free(module->tables);
  3188. if (module->memories)
  3189. wasm_runtime_free(module->memories);
  3190. if (module->globals) {
  3191. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  3192. for (i = 0; i < module->global_count; i++) {
  3193. destroy_init_expr(&module->globals[i].init_expr);
  3194. }
  3195. #endif
  3196. wasm_runtime_free(module->globals);
  3197. }
  3198. if (module->exports)
  3199. wasm_runtime_free(module->exports);
  3200. if (module->table_segments) {
  3201. for (i = 0; i < module->table_seg_count; i++) {
  3202. if (module->table_segments[i].init_values)
  3203. wasm_runtime_free(module->table_segments[i].init_values);
  3204. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  3205. destroy_init_expr(&module->table_segments[i].base_offset);
  3206. #endif
  3207. }
  3208. wasm_runtime_free(module->table_segments);
  3209. }
  3210. if (module->data_segments) {
  3211. for (i = 0; i < module->data_seg_count; i++) {
  3212. if (module->data_segments[i]) {
  3213. if (module->data_segments[i]->is_data_cloned)
  3214. wasm_runtime_free(module->data_segments[i]->data);
  3215. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  3216. destroy_init_expr(&module->data_segments[i]->base_offset);
  3217. #endif
  3218. wasm_runtime_free(module->data_segments[i]);
  3219. }
  3220. }
  3221. wasm_runtime_free(module->data_segments);
  3222. }
  3223. if (module->const_str_list) {
  3224. StringNode *node = module->const_str_list, *node_next;
  3225. while (node) {
  3226. node_next = node->next;
  3227. wasm_runtime_free(node);
  3228. node = node_next;
  3229. }
  3230. }
  3231. #if WASM_ENABLE_FAST_INTERP == 0
  3232. if (module->br_table_cache_list) {
  3233. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  3234. BrTableCache *node_next;
  3235. while (node) {
  3236. node_next = bh_list_elem_next(node);
  3237. wasm_runtime_free(node);
  3238. node = node_next;
  3239. }
  3240. }
  3241. #endif
  3242. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3243. && WASM_ENABLE_LAZY_JIT != 0
  3244. os_mutex_destroy(&module->instance_list_lock);
  3245. #endif
  3246. #if WASM_ENABLE_FAST_JIT != 0
  3247. if (module->fast_jit_func_ptrs) {
  3248. wasm_runtime_free(module->fast_jit_func_ptrs);
  3249. }
  3250. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  3251. if (module->fast_jit_thread_locks_inited[i]) {
  3252. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  3253. }
  3254. }
  3255. #endif
  3256. wasm_runtime_free(module);
  3257. }
  3258. bool
  3259. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  3260. const uint8 *start_addr, const uint8 *code_end_addr,
  3261. uint8 label_type, uint8 **p_else_addr,
  3262. uint8 **p_end_addr)
  3263. {
  3264. const uint8 *p = start_addr, *p_end = code_end_addr;
  3265. uint8 *else_addr = NULL;
  3266. char error_buf[128];
  3267. uint32 block_nested_depth = 1, count, i, j, t;
  3268. uint32 error_buf_size = sizeof(error_buf);
  3269. uint8 opcode, u8;
  3270. BlockAddr block_stack[16] = { 0 }, *block;
  3271. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3272. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3273. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  3274. if (block[j].start_addr == start_addr) {
  3275. /* Cache hit */
  3276. *p_else_addr = block[j].else_addr;
  3277. *p_end_addr = block[j].end_addr;
  3278. return true;
  3279. }
  3280. }
  3281. /* Cache unhit */
  3282. block_stack[0].start_addr = start_addr;
  3283. while (p < code_end_addr) {
  3284. opcode = *p++;
  3285. switch (opcode) {
  3286. case WASM_OP_UNREACHABLE:
  3287. case WASM_OP_NOP:
  3288. break;
  3289. case WASM_OP_BLOCK:
  3290. case WASM_OP_LOOP:
  3291. case WASM_OP_IF:
  3292. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  3293. u8 = read_uint8(p);
  3294. if (block_nested_depth
  3295. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3296. block_stack[block_nested_depth].start_addr = p;
  3297. block_stack[block_nested_depth].else_addr = NULL;
  3298. }
  3299. block_nested_depth++;
  3300. break;
  3301. case EXT_OP_BLOCK:
  3302. case EXT_OP_LOOP:
  3303. case EXT_OP_IF:
  3304. /* block type */
  3305. skip_leb_int32(p, p_end);
  3306. if (block_nested_depth
  3307. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3308. block_stack[block_nested_depth].start_addr = p;
  3309. block_stack[block_nested_depth].else_addr = NULL;
  3310. }
  3311. block_nested_depth++;
  3312. break;
  3313. case WASM_OP_ELSE:
  3314. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  3315. else_addr = (uint8 *)(p - 1);
  3316. if (block_nested_depth - 1
  3317. < sizeof(block_stack) / sizeof(BlockAddr))
  3318. block_stack[block_nested_depth - 1].else_addr =
  3319. (uint8 *)(p - 1);
  3320. break;
  3321. case WASM_OP_END:
  3322. if (block_nested_depth == 1) {
  3323. if (label_type == LABEL_TYPE_IF)
  3324. *p_else_addr = else_addr;
  3325. *p_end_addr = (uint8 *)(p - 1);
  3326. block_stack[0].end_addr = (uint8 *)(p - 1);
  3327. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  3328. t++) {
  3329. start_addr = block_stack[t].start_addr;
  3330. if (start_addr) {
  3331. i = ((uintptr_t)start_addr)
  3332. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3333. block =
  3334. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3335. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  3336. if (!block[j].start_addr)
  3337. break;
  3338. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  3339. memmove(block + 1, block,
  3340. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  3341. * sizeof(BlockAddr));
  3342. j = 0;
  3343. }
  3344. block[j].start_addr = block_stack[t].start_addr;
  3345. block[j].else_addr = block_stack[t].else_addr;
  3346. block[j].end_addr = block_stack[t].end_addr;
  3347. }
  3348. else
  3349. break;
  3350. }
  3351. return true;
  3352. }
  3353. else {
  3354. block_nested_depth--;
  3355. if (block_nested_depth
  3356. < sizeof(block_stack) / sizeof(BlockAddr))
  3357. block_stack[block_nested_depth].end_addr =
  3358. (uint8 *)(p - 1);
  3359. }
  3360. break;
  3361. case WASM_OP_BR:
  3362. case WASM_OP_BR_IF:
  3363. skip_leb_uint32(p, p_end); /* labelidx */
  3364. break;
  3365. case WASM_OP_BR_TABLE:
  3366. read_leb_uint32(p, p_end, count); /* lable num */
  3367. #if WASM_ENABLE_FAST_INTERP != 0
  3368. for (i = 0; i <= count; i++) /* lableidxs */
  3369. skip_leb_uint32(p, p_end);
  3370. #else
  3371. p += count + 1;
  3372. while (*p == WASM_OP_NOP)
  3373. p++;
  3374. #endif
  3375. break;
  3376. #if WASM_ENABLE_FAST_INTERP == 0
  3377. case EXT_OP_BR_TABLE_CACHE:
  3378. read_leb_uint32(p, p_end, count); /* lable num */
  3379. while (*p == WASM_OP_NOP)
  3380. p++;
  3381. break;
  3382. #endif
  3383. case WASM_OP_RETURN:
  3384. break;
  3385. case WASM_OP_CALL:
  3386. #if WASM_ENABLE_TAIL_CALL != 0
  3387. case WASM_OP_RETURN_CALL:
  3388. #endif
  3389. skip_leb_uint32(p, p_end); /* funcidx */
  3390. break;
  3391. case WASM_OP_CALL_INDIRECT:
  3392. #if WASM_ENABLE_TAIL_CALL != 0
  3393. case WASM_OP_RETURN_CALL_INDIRECT:
  3394. #endif
  3395. skip_leb_uint32(p, p_end); /* typeidx */
  3396. #if WASM_ENABLE_CALL_INDIRECT_OVERLONG != 0
  3397. skip_leb_uint32(p, p_end); /* tableidx */
  3398. #else
  3399. u8 = read_uint8(p); /* 0x00 */
  3400. #endif
  3401. break;
  3402. #if WASM_ENABLE_EXCE_HANDLING != 0
  3403. case WASM_OP_TRY:
  3404. case WASM_OP_CATCH:
  3405. case WASM_OP_THROW:
  3406. case WASM_OP_RETHROW:
  3407. case WASM_OP_DELEGATE:
  3408. case WASM_OP_CATCH_ALL:
  3409. /* TODO */
  3410. return false;
  3411. #endif
  3412. case WASM_OP_DROP:
  3413. case WASM_OP_SELECT:
  3414. case WASM_OP_DROP_64:
  3415. case WASM_OP_SELECT_64:
  3416. break;
  3417. #if WASM_ENABLE_REF_TYPES != 0
  3418. case WASM_OP_SELECT_T:
  3419. skip_leb_uint32(p, p_end); /* vec length */
  3420. CHECK_BUF(p, p_end, 1);
  3421. u8 = read_uint8(p); /* typeidx */
  3422. break;
  3423. case WASM_OP_TABLE_GET:
  3424. case WASM_OP_TABLE_SET:
  3425. skip_leb_uint32(p, p_end); /* table index */
  3426. break;
  3427. case WASM_OP_REF_NULL:
  3428. CHECK_BUF(p, p_end, 1);
  3429. u8 = read_uint8(p); /* type */
  3430. break;
  3431. case WASM_OP_REF_IS_NULL:
  3432. break;
  3433. case WASM_OP_REF_FUNC:
  3434. skip_leb_uint32(p, p_end); /* func index */
  3435. break;
  3436. #endif /* WASM_ENABLE_REF_TYPES */
  3437. case WASM_OP_GET_LOCAL:
  3438. case WASM_OP_SET_LOCAL:
  3439. case WASM_OP_TEE_LOCAL:
  3440. case WASM_OP_GET_GLOBAL:
  3441. case WASM_OP_SET_GLOBAL:
  3442. case WASM_OP_GET_GLOBAL_64:
  3443. case WASM_OP_SET_GLOBAL_64:
  3444. case WASM_OP_SET_GLOBAL_AUX_STACK:
  3445. skip_leb_uint32(p, p_end); /* localidx */
  3446. break;
  3447. case EXT_OP_GET_LOCAL_FAST:
  3448. case EXT_OP_SET_LOCAL_FAST:
  3449. case EXT_OP_TEE_LOCAL_FAST:
  3450. CHECK_BUF(p, p_end, 1);
  3451. p++;
  3452. break;
  3453. case WASM_OP_I32_LOAD:
  3454. case WASM_OP_I64_LOAD:
  3455. case WASM_OP_F32_LOAD:
  3456. case WASM_OP_F64_LOAD:
  3457. case WASM_OP_I32_LOAD8_S:
  3458. case WASM_OP_I32_LOAD8_U:
  3459. case WASM_OP_I32_LOAD16_S:
  3460. case WASM_OP_I32_LOAD16_U:
  3461. case WASM_OP_I64_LOAD8_S:
  3462. case WASM_OP_I64_LOAD8_U:
  3463. case WASM_OP_I64_LOAD16_S:
  3464. case WASM_OP_I64_LOAD16_U:
  3465. case WASM_OP_I64_LOAD32_S:
  3466. case WASM_OP_I64_LOAD32_U:
  3467. case WASM_OP_I32_STORE:
  3468. case WASM_OP_I64_STORE:
  3469. case WASM_OP_F32_STORE:
  3470. case WASM_OP_F64_STORE:
  3471. case WASM_OP_I32_STORE8:
  3472. case WASM_OP_I32_STORE16:
  3473. case WASM_OP_I64_STORE8:
  3474. case WASM_OP_I64_STORE16:
  3475. case WASM_OP_I64_STORE32:
  3476. skip_leb_align(p, p_end); /* align */
  3477. skip_leb_mem_offset(p, p_end); /* offset */
  3478. break;
  3479. case WASM_OP_MEMORY_SIZE:
  3480. case WASM_OP_MEMORY_GROW:
  3481. skip_leb_memidx(p, p_end); /* memidx */
  3482. break;
  3483. case WASM_OP_I32_CONST:
  3484. skip_leb_int32(p, p_end);
  3485. break;
  3486. case WASM_OP_I64_CONST:
  3487. skip_leb_int64(p, p_end);
  3488. break;
  3489. case WASM_OP_F32_CONST:
  3490. p += sizeof(float32);
  3491. break;
  3492. case WASM_OP_F64_CONST:
  3493. p += sizeof(float64);
  3494. break;
  3495. case WASM_OP_I32_EQZ:
  3496. case WASM_OP_I32_EQ:
  3497. case WASM_OP_I32_NE:
  3498. case WASM_OP_I32_LT_S:
  3499. case WASM_OP_I32_LT_U:
  3500. case WASM_OP_I32_GT_S:
  3501. case WASM_OP_I32_GT_U:
  3502. case WASM_OP_I32_LE_S:
  3503. case WASM_OP_I32_LE_U:
  3504. case WASM_OP_I32_GE_S:
  3505. case WASM_OP_I32_GE_U:
  3506. case WASM_OP_I64_EQZ:
  3507. case WASM_OP_I64_EQ:
  3508. case WASM_OP_I64_NE:
  3509. case WASM_OP_I64_LT_S:
  3510. case WASM_OP_I64_LT_U:
  3511. case WASM_OP_I64_GT_S:
  3512. case WASM_OP_I64_GT_U:
  3513. case WASM_OP_I64_LE_S:
  3514. case WASM_OP_I64_LE_U:
  3515. case WASM_OP_I64_GE_S:
  3516. case WASM_OP_I64_GE_U:
  3517. case WASM_OP_F32_EQ:
  3518. case WASM_OP_F32_NE:
  3519. case WASM_OP_F32_LT:
  3520. case WASM_OP_F32_GT:
  3521. case WASM_OP_F32_LE:
  3522. case WASM_OP_F32_GE:
  3523. case WASM_OP_F64_EQ:
  3524. case WASM_OP_F64_NE:
  3525. case WASM_OP_F64_LT:
  3526. case WASM_OP_F64_GT:
  3527. case WASM_OP_F64_LE:
  3528. case WASM_OP_F64_GE:
  3529. case WASM_OP_I32_CLZ:
  3530. case WASM_OP_I32_CTZ:
  3531. case WASM_OP_I32_POPCNT:
  3532. case WASM_OP_I32_ADD:
  3533. case WASM_OP_I32_SUB:
  3534. case WASM_OP_I32_MUL:
  3535. case WASM_OP_I32_DIV_S:
  3536. case WASM_OP_I32_DIV_U:
  3537. case WASM_OP_I32_REM_S:
  3538. case WASM_OP_I32_REM_U:
  3539. case WASM_OP_I32_AND:
  3540. case WASM_OP_I32_OR:
  3541. case WASM_OP_I32_XOR:
  3542. case WASM_OP_I32_SHL:
  3543. case WASM_OP_I32_SHR_S:
  3544. case WASM_OP_I32_SHR_U:
  3545. case WASM_OP_I32_ROTL:
  3546. case WASM_OP_I32_ROTR:
  3547. case WASM_OP_I64_CLZ:
  3548. case WASM_OP_I64_CTZ:
  3549. case WASM_OP_I64_POPCNT:
  3550. case WASM_OP_I64_ADD:
  3551. case WASM_OP_I64_SUB:
  3552. case WASM_OP_I64_MUL:
  3553. case WASM_OP_I64_DIV_S:
  3554. case WASM_OP_I64_DIV_U:
  3555. case WASM_OP_I64_REM_S:
  3556. case WASM_OP_I64_REM_U:
  3557. case WASM_OP_I64_AND:
  3558. case WASM_OP_I64_OR:
  3559. case WASM_OP_I64_XOR:
  3560. case WASM_OP_I64_SHL:
  3561. case WASM_OP_I64_SHR_S:
  3562. case WASM_OP_I64_SHR_U:
  3563. case WASM_OP_I64_ROTL:
  3564. case WASM_OP_I64_ROTR:
  3565. case WASM_OP_F32_ABS:
  3566. case WASM_OP_F32_NEG:
  3567. case WASM_OP_F32_CEIL:
  3568. case WASM_OP_F32_FLOOR:
  3569. case WASM_OP_F32_TRUNC:
  3570. case WASM_OP_F32_NEAREST:
  3571. case WASM_OP_F32_SQRT:
  3572. case WASM_OP_F32_ADD:
  3573. case WASM_OP_F32_SUB:
  3574. case WASM_OP_F32_MUL:
  3575. case WASM_OP_F32_DIV:
  3576. case WASM_OP_F32_MIN:
  3577. case WASM_OP_F32_MAX:
  3578. case WASM_OP_F32_COPYSIGN:
  3579. case WASM_OP_F64_ABS:
  3580. case WASM_OP_F64_NEG:
  3581. case WASM_OP_F64_CEIL:
  3582. case WASM_OP_F64_FLOOR:
  3583. case WASM_OP_F64_TRUNC:
  3584. case WASM_OP_F64_NEAREST:
  3585. case WASM_OP_F64_SQRT:
  3586. case WASM_OP_F64_ADD:
  3587. case WASM_OP_F64_SUB:
  3588. case WASM_OP_F64_MUL:
  3589. case WASM_OP_F64_DIV:
  3590. case WASM_OP_F64_MIN:
  3591. case WASM_OP_F64_MAX:
  3592. case WASM_OP_F64_COPYSIGN:
  3593. case WASM_OP_I32_WRAP_I64:
  3594. case WASM_OP_I32_TRUNC_S_F32:
  3595. case WASM_OP_I32_TRUNC_U_F32:
  3596. case WASM_OP_I32_TRUNC_S_F64:
  3597. case WASM_OP_I32_TRUNC_U_F64:
  3598. case WASM_OP_I64_EXTEND_S_I32:
  3599. case WASM_OP_I64_EXTEND_U_I32:
  3600. case WASM_OP_I64_TRUNC_S_F32:
  3601. case WASM_OP_I64_TRUNC_U_F32:
  3602. case WASM_OP_I64_TRUNC_S_F64:
  3603. case WASM_OP_I64_TRUNC_U_F64:
  3604. case WASM_OP_F32_CONVERT_S_I32:
  3605. case WASM_OP_F32_CONVERT_U_I32:
  3606. case WASM_OP_F32_CONVERT_S_I64:
  3607. case WASM_OP_F32_CONVERT_U_I64:
  3608. case WASM_OP_F32_DEMOTE_F64:
  3609. case WASM_OP_F64_CONVERT_S_I32:
  3610. case WASM_OP_F64_CONVERT_U_I32:
  3611. case WASM_OP_F64_CONVERT_S_I64:
  3612. case WASM_OP_F64_CONVERT_U_I64:
  3613. case WASM_OP_F64_PROMOTE_F32:
  3614. case WASM_OP_I32_REINTERPRET_F32:
  3615. case WASM_OP_I64_REINTERPRET_F64:
  3616. case WASM_OP_F32_REINTERPRET_I32:
  3617. case WASM_OP_F64_REINTERPRET_I64:
  3618. case WASM_OP_I32_EXTEND8_S:
  3619. case WASM_OP_I32_EXTEND16_S:
  3620. case WASM_OP_I64_EXTEND8_S:
  3621. case WASM_OP_I64_EXTEND16_S:
  3622. case WASM_OP_I64_EXTEND32_S:
  3623. break;
  3624. case WASM_OP_MISC_PREFIX:
  3625. {
  3626. uint32 opcode1;
  3627. read_leb_uint32(p, p_end, opcode1);
  3628. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  3629. is no larger than UINT8_MAX */
  3630. opcode = (uint8)opcode1;
  3631. switch (opcode) {
  3632. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3633. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3634. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3635. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3636. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3637. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3638. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3639. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3640. break;
  3641. #if WASM_ENABLE_BULK_MEMORY != 0
  3642. case WASM_OP_MEMORY_INIT:
  3643. skip_leb_uint32(p, p_end);
  3644. skip_leb_memidx(p, p_end);
  3645. break;
  3646. case WASM_OP_DATA_DROP:
  3647. skip_leb_uint32(p, p_end);
  3648. break;
  3649. #endif
  3650. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  3651. case WASM_OP_MEMORY_COPY:
  3652. skip_leb_memidx(p, p_end);
  3653. skip_leb_memidx(p, p_end);
  3654. break;
  3655. case WASM_OP_MEMORY_FILL:
  3656. skip_leb_memidx(p, p_end);
  3657. break;
  3658. #endif /* WASM_ENABLE_BULK_MEMORY_OPT */
  3659. #if WASM_ENABLE_REF_TYPES != 0
  3660. case WASM_OP_TABLE_INIT:
  3661. case WASM_OP_TABLE_COPY:
  3662. /* tableidx */
  3663. skip_leb_uint32(p, p_end);
  3664. /* elemidx */
  3665. skip_leb_uint32(p, p_end);
  3666. break;
  3667. case WASM_OP_ELEM_DROP:
  3668. /* elemidx */
  3669. skip_leb_uint32(p, p_end);
  3670. break;
  3671. case WASM_OP_TABLE_SIZE:
  3672. case WASM_OP_TABLE_GROW:
  3673. case WASM_OP_TABLE_FILL:
  3674. skip_leb_uint32(p, p_end); /* table idx */
  3675. break;
  3676. #endif /* WASM_ENABLE_REF_TYPES */
  3677. default:
  3678. bh_assert(0);
  3679. break;
  3680. }
  3681. break;
  3682. }
  3683. #if WASM_ENABLE_SHARED_MEMORY != 0
  3684. case WASM_OP_ATOMIC_PREFIX:
  3685. {
  3686. /* TODO: memory64 offset type changes */
  3687. uint32 opcode1;
  3688. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  3689. read_leb_uint32(p, p_end, opcode1);
  3690. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  3691. is no larger than UINT8_MAX */
  3692. opcode = (uint8)opcode1;
  3693. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3694. skip_leb_uint32(p, p_end); /* align */
  3695. skip_leb_mem_offset(p, p_end); /* offset */
  3696. }
  3697. else {
  3698. /* atomic.fence doesn't have memarg */
  3699. p++;
  3700. }
  3701. break;
  3702. }
  3703. #endif
  3704. default:
  3705. bh_assert(0);
  3706. break;
  3707. }
  3708. }
  3709. (void)u8;
  3710. return false;
  3711. }
  3712. #define REF_I32 VALUE_TYPE_I32
  3713. #define REF_F32 VALUE_TYPE_F32
  3714. #define REF_I64_1 VALUE_TYPE_I64
  3715. #define REF_I64_2 VALUE_TYPE_I64
  3716. #define REF_F64_1 VALUE_TYPE_F64
  3717. #define REF_F64_2 VALUE_TYPE_F64
  3718. #define REF_ANY VALUE_TYPE_ANY
  3719. #if WASM_ENABLE_FAST_INTERP != 0
  3720. #if WASM_DEBUG_PREPROCESSOR != 0
  3721. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3722. #else
  3723. #define LOG_OP(...) (void)0
  3724. #endif
  3725. #define PATCH_ELSE 0
  3726. #define PATCH_END 1
  3727. typedef struct BranchBlockPatch {
  3728. struct BranchBlockPatch *next;
  3729. uint8 patch_type;
  3730. uint8 *code_compiled;
  3731. } BranchBlockPatch;
  3732. #endif
  3733. typedef struct BranchBlock {
  3734. uint8 label_type;
  3735. BlockType block_type;
  3736. uint8 *start_addr;
  3737. uint8 *else_addr;
  3738. uint8 *end_addr;
  3739. uint32 stack_cell_num;
  3740. #if WASM_ENABLE_FAST_INTERP != 0
  3741. uint16 dynamic_offset;
  3742. uint8 *code_compiled;
  3743. BranchBlockPatch *patch_list;
  3744. /* This is used to save params frame_offset of of if block */
  3745. int16 *param_frame_offsets;
  3746. /* This is used to store available param num for if/else branch, so the else
  3747. * opcode can know how many parameters should be copied to the stack */
  3748. uint32 available_param_num;
  3749. /* This is used to recover the dynamic offset for else branch,
  3750. * and also to remember the start offset of dynamic space which
  3751. * stores the block arguments for loop block, so we can use it
  3752. * to copy the stack operands to the loop block's arguments in
  3753. * wasm_loader_emit_br_info for opcode br. */
  3754. uint16 start_dynamic_offset;
  3755. #endif
  3756. /* Indicate the operand stack is in polymorphic state.
  3757. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3758. * to polymorphic state until the block's 'end' opcode is processed.
  3759. * If stack is in polymorphic state and stack is empty, instruction can
  3760. * pop any type of value directly without decreasing stack top pointer
  3761. * and stack cell num. */
  3762. bool is_stack_polymorphic;
  3763. } BranchBlock;
  3764. typedef struct WASMLoaderContext {
  3765. /* frame ref stack */
  3766. uint8 *frame_ref;
  3767. uint8 *frame_ref_bottom;
  3768. uint8 *frame_ref_boundary;
  3769. uint32 frame_ref_size;
  3770. uint32 stack_cell_num;
  3771. uint32 max_stack_cell_num;
  3772. /* frame csp stack */
  3773. BranchBlock *frame_csp;
  3774. BranchBlock *frame_csp_bottom;
  3775. BranchBlock *frame_csp_boundary;
  3776. uint32 frame_csp_size;
  3777. uint32 csp_num;
  3778. uint32 max_csp_num;
  3779. #if WASM_ENABLE_FAST_INTERP != 0
  3780. /* frame offset stack */
  3781. int16 *frame_offset;
  3782. int16 *frame_offset_bottom;
  3783. int16 *frame_offset_boundary;
  3784. uint32 frame_offset_size;
  3785. int16 dynamic_offset;
  3786. int16 start_dynamic_offset;
  3787. int16 max_dynamic_offset;
  3788. /* preserved local offset */
  3789. int16 preserved_local_offset;
  3790. /* const buffer for i64 and f64 consts, note that the raw bytes
  3791. * of i64 and f64 are the same, so we read an i64 value from an
  3792. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  3793. int64 *i64_consts;
  3794. uint32 i64_const_max_num;
  3795. uint32 i64_const_num;
  3796. /* const buffer for i32 and f32 consts */
  3797. int32 *i32_consts;
  3798. uint32 i32_const_max_num;
  3799. uint32 i32_const_num;
  3800. /* processed code */
  3801. uint8 *p_code_compiled;
  3802. uint8 *p_code_compiled_end;
  3803. uint32 code_compiled_size;
  3804. /* If the last opcode will be dropped, the peak memory usage will be larger
  3805. * than the final code_compiled_size, we record the peak size to ensure
  3806. * there will not be invalid memory access during second traverse */
  3807. uint32 code_compiled_peak_size;
  3808. #endif
  3809. } WASMLoaderContext;
  3810. #define CHECK_CSP_PUSH() \
  3811. do { \
  3812. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3813. MEM_REALLOC( \
  3814. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3815. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  3816. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3817. ctx->frame_csp_boundary = \
  3818. ctx->frame_csp_bottom \
  3819. + ctx->frame_csp_size / sizeof(BranchBlock); \
  3820. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3821. } \
  3822. } while (0)
  3823. #define CHECK_CSP_POP() \
  3824. do { \
  3825. bh_assert(ctx->csp_num >= 1); \
  3826. } while (0)
  3827. #if WASM_ENABLE_FAST_INTERP != 0
  3828. static bool
  3829. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  3830. uint32 error_buf_size)
  3831. {
  3832. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  3833. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3834. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3835. ctx->frame_offset_size + 16);
  3836. ctx->frame_offset_size += 16;
  3837. ctx->frame_offset_boundary =
  3838. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  3839. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3840. }
  3841. return true;
  3842. fail:
  3843. return false;
  3844. }
  3845. static bool
  3846. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3847. {
  3848. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3849. return false;
  3850. return true;
  3851. }
  3852. static bool
  3853. check_dynamic_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3854. {
  3855. if (ctx->dynamic_offset < 0
  3856. || (ctx->dynamic_offset > 0 && (uint32)ctx->dynamic_offset < cells))
  3857. return false;
  3858. return true;
  3859. }
  3860. static void
  3861. free_label_patch_list(BranchBlock *frame_csp)
  3862. {
  3863. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3864. BranchBlockPatch *next;
  3865. while (label_patch != NULL) {
  3866. next = label_patch->next;
  3867. wasm_runtime_free(label_patch);
  3868. label_patch = next;
  3869. }
  3870. frame_csp->patch_list = NULL;
  3871. }
  3872. static void
  3873. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3874. {
  3875. BranchBlock *tmp_csp = frame_csp;
  3876. uint32 i;
  3877. for (i = 0; i < csp_num; i++) {
  3878. free_label_patch_list(tmp_csp);
  3879. tmp_csp++;
  3880. }
  3881. }
  3882. static void
  3883. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  3884. {
  3885. BranchBlock *tmp_csp = frame_csp;
  3886. uint32 i;
  3887. for (i = 0; i < csp_num; i++) {
  3888. if (tmp_csp->param_frame_offsets)
  3889. wasm_runtime_free(tmp_csp->param_frame_offsets);
  3890. tmp_csp++;
  3891. }
  3892. }
  3893. #endif
  3894. static bool
  3895. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  3896. {
  3897. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3898. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3899. ctx->frame_ref_size + 16);
  3900. ctx->frame_ref_size += 16;
  3901. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3902. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3903. }
  3904. return true;
  3905. fail:
  3906. return false;
  3907. }
  3908. static bool
  3909. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3910. char *error_buf, uint32 error_buf_size)
  3911. {
  3912. bh_assert(!((is_32bit_type(type) && stack_cell_num < 1)
  3913. || (is_64bit_type(type) && stack_cell_num < 2)));
  3914. bh_assert(!(
  3915. (type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  3916. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  3917. || (type == VALUE_TYPE_I64
  3918. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  3919. || (type == VALUE_TYPE_F64
  3920. && (*(frame_ref - 2) != REF_F64_1
  3921. || *(frame_ref - 1) != REF_F64_2))));
  3922. return true;
  3923. }
  3924. static bool
  3925. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3926. uint32 error_buf_size)
  3927. {
  3928. int32 block_stack_cell_num =
  3929. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3930. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3931. /* the stack top is a value of any type, return success */
  3932. return true;
  3933. }
  3934. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  3935. error_buf, error_buf_size))
  3936. return false;
  3937. return true;
  3938. }
  3939. static void
  3940. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3941. {
  3942. if (ctx) {
  3943. if (ctx->frame_ref_bottom)
  3944. wasm_runtime_free(ctx->frame_ref_bottom);
  3945. if (ctx->frame_csp_bottom) {
  3946. #if WASM_ENABLE_FAST_INTERP != 0
  3947. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3948. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  3949. ctx->csp_num);
  3950. #endif
  3951. wasm_runtime_free(ctx->frame_csp_bottom);
  3952. }
  3953. #if WASM_ENABLE_FAST_INTERP != 0
  3954. if (ctx->frame_offset_bottom)
  3955. wasm_runtime_free(ctx->frame_offset_bottom);
  3956. if (ctx->i64_consts)
  3957. wasm_runtime_free(ctx->i64_consts);
  3958. if (ctx->i32_consts)
  3959. wasm_runtime_free(ctx->i32_consts);
  3960. #endif
  3961. wasm_runtime_free(ctx);
  3962. }
  3963. }
  3964. static WASMLoaderContext *
  3965. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  3966. {
  3967. WASMLoaderContext *loader_ctx =
  3968. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  3969. if (!loader_ctx)
  3970. return NULL;
  3971. loader_ctx->frame_ref_size = 32;
  3972. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  3973. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  3974. goto fail;
  3975. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  3976. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3977. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  3978. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  3979. goto fail;
  3980. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3981. #if WASM_ENABLE_FAST_INTERP != 0
  3982. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3983. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3984. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  3985. error_buf_size)))
  3986. goto fail;
  3987. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  3988. loader_ctx->i64_const_max_num = 8;
  3989. if (!(loader_ctx->i64_consts =
  3990. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  3991. error_buf, error_buf_size)))
  3992. goto fail;
  3993. loader_ctx->i32_const_max_num = 8;
  3994. if (!(loader_ctx->i32_consts =
  3995. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  3996. error_buf, error_buf_size)))
  3997. goto fail;
  3998. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  3999. set_error_buf(error_buf, error_buf_size,
  4000. "fast interpreter offset overflow");
  4001. goto fail;
  4002. }
  4003. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  4004. loader_ctx->max_dynamic_offset =
  4005. func->param_cell_num + func->local_cell_num;
  4006. #endif
  4007. return loader_ctx;
  4008. fail:
  4009. wasm_loader_ctx_destroy(loader_ctx);
  4010. return NULL;
  4011. }
  4012. static bool
  4013. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4014. uint32 error_buf_size)
  4015. {
  4016. if (type == VALUE_TYPE_VOID)
  4017. return true;
  4018. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4019. return false;
  4020. *ctx->frame_ref++ = type;
  4021. ctx->stack_cell_num++;
  4022. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  4023. ctx->max_stack_cell_num = ctx->stack_cell_num;
  4024. if (is_32bit_type(type))
  4025. return true;
  4026. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4027. return false;
  4028. *ctx->frame_ref++ = type;
  4029. ctx->stack_cell_num++;
  4030. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  4031. ctx->max_stack_cell_num = ctx->stack_cell_num;
  4032. bh_assert(ctx->max_stack_cell_num <= UINT16_MAX);
  4033. }
  4034. return true;
  4035. }
  4036. static bool
  4037. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4038. uint32 error_buf_size)
  4039. {
  4040. BranchBlock *cur_block = ctx->frame_csp - 1;
  4041. int32 available_stack_cell =
  4042. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4043. /* Directly return success if current block is in stack
  4044. * polymorphic state while stack is empty. */
  4045. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4046. return true;
  4047. if (type == VALUE_TYPE_VOID)
  4048. return true;
  4049. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  4050. return false;
  4051. ctx->frame_ref--;
  4052. ctx->stack_cell_num--;
  4053. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  4054. return true;
  4055. ctx->frame_ref--;
  4056. ctx->stack_cell_num--;
  4057. return true;
  4058. }
  4059. #if WASM_ENABLE_FAST_INTERP == 0
  4060. static bool
  4061. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  4062. uint8 type_push, uint8 type_pop, char *error_buf,
  4063. uint32 error_buf_size)
  4064. {
  4065. for (int i = 0; i < pop_cnt; i++) {
  4066. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4067. error_buf_size))
  4068. return false;
  4069. }
  4070. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4071. return false;
  4072. return true;
  4073. }
  4074. #endif
  4075. static bool
  4076. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  4077. BlockType block_type, uint8 *start_addr,
  4078. char *error_buf, uint32 error_buf_size)
  4079. {
  4080. CHECK_CSP_PUSH();
  4081. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  4082. ctx->frame_csp->label_type = label_type;
  4083. ctx->frame_csp->block_type = block_type;
  4084. ctx->frame_csp->start_addr = start_addr;
  4085. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  4086. #if WASM_ENABLE_FAST_INTERP != 0
  4087. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  4088. ctx->frame_csp->patch_list = NULL;
  4089. #endif
  4090. ctx->frame_csp++;
  4091. ctx->csp_num++;
  4092. if (ctx->csp_num > ctx->max_csp_num) {
  4093. ctx->max_csp_num = ctx->csp_num;
  4094. bh_assert(ctx->max_csp_num <= UINT16_MAX);
  4095. }
  4096. return true;
  4097. fail:
  4098. return false;
  4099. }
  4100. static bool
  4101. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  4102. uint32 error_buf_size)
  4103. {
  4104. CHECK_CSP_POP();
  4105. #if WASM_ENABLE_FAST_INTERP != 0
  4106. if ((ctx->frame_csp - 1)->param_frame_offsets)
  4107. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  4108. #endif
  4109. ctx->frame_csp--;
  4110. ctx->csp_num--;
  4111. return true;
  4112. }
  4113. #if WASM_ENABLE_FAST_INTERP != 0
  4114. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4115. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4116. #define emit_label(opcode) \
  4117. do { \
  4118. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  4119. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4120. } while (0)
  4121. #define skip_label() \
  4122. do { \
  4123. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  4124. LOG_OP("\ndelete last op\n"); \
  4125. } while (0)
  4126. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4127. #if UINTPTR_MAX == UINT64_MAX
  4128. #define emit_label(opcode) \
  4129. do { \
  4130. int32 offset = \
  4131. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  4132. /* emit int32 relative offset in 64-bit target */ \
  4133. wasm_loader_emit_uint32(loader_ctx, offset); \
  4134. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4135. } while (0)
  4136. #else
  4137. #define emit_label(opcode) \
  4138. do { \
  4139. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  4140. /* emit uint32 label address in 32-bit target */ \
  4141. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  4142. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4143. } while (0)
  4144. #endif
  4145. #define skip_label() \
  4146. do { \
  4147. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  4148. LOG_OP("\ndelete last op\n"); \
  4149. } while (0)
  4150. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4151. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  4152. #define emit_label(opcode) \
  4153. do { \
  4154. wasm_loader_emit_uint8(loader_ctx, opcode); \
  4155. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4156. } while (0)
  4157. #define skip_label() \
  4158. do { \
  4159. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  4160. LOG_OP("\ndelete last op\n"); \
  4161. } while (0)
  4162. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  4163. #define emit_empty_label_addr_and_frame_ip(type) \
  4164. do { \
  4165. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  4166. loader_ctx->p_code_compiled, error_buf, \
  4167. error_buf_size)) \
  4168. goto fail; \
  4169. /* label address, to be patched */ \
  4170. wasm_loader_emit_ptr(loader_ctx, NULL); \
  4171. } while (0)
  4172. #define emit_br_info(frame_csp, is_br) \
  4173. do { \
  4174. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  4175. error_buf_size)) \
  4176. goto fail; \
  4177. } while (0)
  4178. #define LAST_OP_OUTPUT_I32() \
  4179. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  4180. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  4181. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  4182. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  4183. || (last_op >= WASM_OP_I32_WRAP_I64 \
  4184. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  4185. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  4186. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  4187. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  4188. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  4189. || (last_op == EXT_OP_COPY_STACK_TOP)
  4190. #define LAST_OP_OUTPUT_I64() \
  4191. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  4192. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  4193. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  4194. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  4195. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  4196. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  4197. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  4198. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  4199. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  4200. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  4201. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  4202. #define GET_CONST_OFFSET(type, val) \
  4203. do { \
  4204. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  4205. &operand_offset, error_buf, \
  4206. error_buf_size))) \
  4207. goto fail; \
  4208. } while (0)
  4209. #define GET_CONST_F32_OFFSET(type, fval) \
  4210. do { \
  4211. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  4212. &operand_offset, error_buf, \
  4213. error_buf_size))) \
  4214. goto fail; \
  4215. } while (0)
  4216. #define GET_CONST_F64_OFFSET(type, fval) \
  4217. do { \
  4218. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  4219. &operand_offset, error_buf, \
  4220. error_buf_size))) \
  4221. goto fail; \
  4222. } while (0)
  4223. #define emit_operand(ctx, offset) \
  4224. do { \
  4225. wasm_loader_emit_int16(ctx, offset); \
  4226. LOG_OP("%d\t", offset); \
  4227. } while (0)
  4228. #define emit_byte(ctx, byte) \
  4229. do { \
  4230. wasm_loader_emit_uint8(ctx, byte); \
  4231. LOG_OP("%d\t", byte); \
  4232. } while (0)
  4233. #define emit_uint32(ctx, value) \
  4234. do { \
  4235. wasm_loader_emit_uint32(ctx, value); \
  4236. LOG_OP("%d\t", value); \
  4237. } while (0)
  4238. #define emit_uint64(ctx, value) \
  4239. do { \
  4240. wasm_loader_emit_const(ctx, &value, false); \
  4241. LOG_OP("%lld\t", value); \
  4242. } while (0)
  4243. #define emit_float32(ctx, value) \
  4244. do { \
  4245. wasm_loader_emit_const(ctx, &value, true); \
  4246. LOG_OP("%f\t", value); \
  4247. } while (0)
  4248. #define emit_float64(ctx, value) \
  4249. do { \
  4250. wasm_loader_emit_const(ctx, &value, false); \
  4251. LOG_OP("%f\t", value); \
  4252. } while (0)
  4253. static bool
  4254. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  4255. {
  4256. if (!(ctx->p_code_compiled =
  4257. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  4258. return false;
  4259. ctx->p_code_compiled_end =
  4260. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  4261. /* clean up frame ref */
  4262. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  4263. ctx->frame_ref = ctx->frame_ref_bottom;
  4264. ctx->stack_cell_num = 0;
  4265. /* clean up frame csp */
  4266. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  4267. ctx->frame_csp = ctx->frame_csp_bottom;
  4268. ctx->csp_num = 0;
  4269. ctx->max_csp_num = 0;
  4270. /* clean up frame offset */
  4271. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  4272. ctx->frame_offset = ctx->frame_offset_bottom;
  4273. ctx->dynamic_offset = ctx->start_dynamic_offset;
  4274. /* init preserved local offsets */
  4275. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  4276. /* const buf is reserved */
  4277. return true;
  4278. }
  4279. static void
  4280. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  4281. {
  4282. ctx->code_compiled_size += size;
  4283. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  4284. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  4285. }
  4286. }
  4287. static void
  4288. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  4289. {
  4290. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  4291. if (ctx->p_code_compiled) {
  4292. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4293. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4294. #endif
  4295. bh_memcpy_s(ctx->p_code_compiled,
  4296. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  4297. value, size);
  4298. ctx->p_code_compiled += size;
  4299. }
  4300. else {
  4301. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4302. bh_assert((ctx->code_compiled_size & 1) == 0);
  4303. #endif
  4304. increase_compiled_code_space(ctx, size);
  4305. }
  4306. }
  4307. static void
  4308. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  4309. {
  4310. if (ctx->p_code_compiled) {
  4311. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4312. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4313. #endif
  4314. STORE_U32(ctx->p_code_compiled, value);
  4315. ctx->p_code_compiled += sizeof(uint32);
  4316. }
  4317. else {
  4318. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4319. bh_assert((ctx->code_compiled_size & 1) == 0);
  4320. #endif
  4321. increase_compiled_code_space(ctx, sizeof(uint32));
  4322. }
  4323. }
  4324. static void
  4325. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  4326. {
  4327. if (ctx->p_code_compiled) {
  4328. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4329. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4330. #endif
  4331. STORE_U16(ctx->p_code_compiled, (uint16)value);
  4332. ctx->p_code_compiled += sizeof(int16);
  4333. }
  4334. else {
  4335. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4336. bh_assert((ctx->code_compiled_size & 1) == 0);
  4337. #endif
  4338. increase_compiled_code_space(ctx, sizeof(uint16));
  4339. }
  4340. }
  4341. static void
  4342. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  4343. {
  4344. if (ctx->p_code_compiled) {
  4345. *(ctx->p_code_compiled) = value;
  4346. ctx->p_code_compiled += sizeof(uint8);
  4347. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4348. ctx->p_code_compiled++;
  4349. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4350. #endif
  4351. }
  4352. else {
  4353. increase_compiled_code_space(ctx, sizeof(uint8));
  4354. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4355. increase_compiled_code_space(ctx, sizeof(uint8));
  4356. bh_assert((ctx->code_compiled_size & 1) == 0);
  4357. #endif
  4358. }
  4359. }
  4360. static void
  4361. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  4362. {
  4363. if (ctx->p_code_compiled) {
  4364. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4365. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4366. #endif
  4367. STORE_PTR(ctx->p_code_compiled, value);
  4368. ctx->p_code_compiled += sizeof(void *);
  4369. }
  4370. else {
  4371. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4372. bh_assert((ctx->code_compiled_size & 1) == 0);
  4373. #endif
  4374. increase_compiled_code_space(ctx, sizeof(void *));
  4375. }
  4376. }
  4377. static void
  4378. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  4379. {
  4380. if (ctx->p_code_compiled) {
  4381. ctx->p_code_compiled -= size;
  4382. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4383. if (size == sizeof(uint8)) {
  4384. ctx->p_code_compiled--;
  4385. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4386. }
  4387. #endif
  4388. }
  4389. else {
  4390. ctx->code_compiled_size -= size;
  4391. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4392. if (size == sizeof(uint8)) {
  4393. ctx->code_compiled_size--;
  4394. bh_assert((ctx->code_compiled_size & 1) == 0);
  4395. }
  4396. #endif
  4397. }
  4398. }
  4399. static bool
  4400. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  4401. uint32 local_index, uint32 local_type,
  4402. bool *preserved, char *error_buf,
  4403. uint32 error_buf_size)
  4404. {
  4405. uint32 i = 0;
  4406. int16 preserved_offset = (int16)local_index;
  4407. *preserved = false;
  4408. while (i < loader_ctx->stack_cell_num) {
  4409. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4410. /* move previous local into dynamic space before a set/tee_local opcode
  4411. */
  4412. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  4413. if (!(*preserved)) {
  4414. *preserved = true;
  4415. skip_label();
  4416. preserved_offset = loader_ctx->preserved_local_offset;
  4417. if (loader_ctx->p_code_compiled) {
  4418. bh_assert(preserved_offset != (int16)local_index);
  4419. }
  4420. if (is_32bit_type(local_type)) {
  4421. /* Only increase preserve offset in the second traversal */
  4422. if (loader_ctx->p_code_compiled)
  4423. loader_ctx->preserved_local_offset++;
  4424. emit_label(EXT_OP_COPY_STACK_TOP);
  4425. }
  4426. else {
  4427. if (loader_ctx->p_code_compiled)
  4428. loader_ctx->preserved_local_offset += 2;
  4429. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  4430. }
  4431. /* overflow */
  4432. bh_assert(preserved_offset
  4433. <= loader_ctx->preserved_local_offset);
  4434. emit_operand(loader_ctx, local_index);
  4435. emit_operand(loader_ctx, preserved_offset);
  4436. emit_label(opcode);
  4437. }
  4438. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  4439. }
  4440. if (is_32bit_type(cur_type))
  4441. i++;
  4442. else
  4443. i += 2;
  4444. }
  4445. return true;
  4446. }
  4447. static bool
  4448. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  4449. char *error_buf, uint32 error_buf_size)
  4450. {
  4451. uint32 i = 0;
  4452. bool preserve_local;
  4453. /* preserve locals before blocks to ensure that "tee/set_local" inside
  4454. blocks will not influence the value of these locals */
  4455. while (i < loader_ctx->stack_cell_num) {
  4456. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  4457. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4458. if ((cur_offset < loader_ctx->start_dynamic_offset)
  4459. && (cur_offset >= 0)) {
  4460. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  4461. cur_type, &preserve_local,
  4462. error_buf, error_buf_size)))
  4463. return false;
  4464. }
  4465. if (is_32bit_type(cur_type == VALUE_TYPE_I32)) {
  4466. i++;
  4467. }
  4468. else {
  4469. i += 2;
  4470. }
  4471. }
  4472. return true;
  4473. }
  4474. static bool
  4475. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  4476. uint8 *p_code_compiled, char *error_buf,
  4477. uint32 error_buf_size)
  4478. {
  4479. BranchBlockPatch *patch =
  4480. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  4481. if (!patch) {
  4482. return false;
  4483. }
  4484. patch->patch_type = patch_type;
  4485. patch->code_compiled = p_code_compiled;
  4486. if (!frame_csp->patch_list) {
  4487. frame_csp->patch_list = patch;
  4488. patch->next = NULL;
  4489. }
  4490. else {
  4491. patch->next = frame_csp->patch_list;
  4492. frame_csp->patch_list = patch;
  4493. }
  4494. return true;
  4495. }
  4496. static void
  4497. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  4498. {
  4499. BranchBlock *frame_csp = ctx->frame_csp - depth;
  4500. BranchBlockPatch *node = frame_csp->patch_list;
  4501. BranchBlockPatch *node_prev = NULL, *node_next;
  4502. if (!ctx->p_code_compiled)
  4503. return;
  4504. while (node) {
  4505. node_next = node->next;
  4506. if (node->patch_type == patch_type) {
  4507. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  4508. if (node_prev == NULL) {
  4509. frame_csp->patch_list = node_next;
  4510. }
  4511. else {
  4512. node_prev->next = node_next;
  4513. }
  4514. wasm_runtime_free(node);
  4515. }
  4516. else {
  4517. node_prev = node;
  4518. }
  4519. node = node_next;
  4520. }
  4521. }
  4522. static bool
  4523. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  4524. bool is_br, char *error_buf, uint32 error_buf_size)
  4525. {
  4526. /* br info layout:
  4527. * a) arity of target block
  4528. * b) total cell num of arity values
  4529. * c) each arity value's cell num
  4530. * d) each arity value's src frame offset
  4531. * e) each arity values's dst dynamic offset
  4532. * f) branch target address
  4533. *
  4534. * Note: b-e are omitted when arity is 0 so that
  4535. * interpreter can recover the br info quickly.
  4536. */
  4537. BlockType *block_type = &frame_csp->block_type;
  4538. uint8 *types = NULL, cell;
  4539. uint32 arity = 0;
  4540. int32 i;
  4541. int16 *frame_offset = ctx->frame_offset;
  4542. uint16 dynamic_offset;
  4543. /* Note: loop's arity is different from if and block. loop's arity is
  4544. * its parameter count while if and block arity is result count.
  4545. */
  4546. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  4547. arity = block_type_get_param_types(block_type, &types);
  4548. else
  4549. arity = block_type_get_result_types(block_type, &types);
  4550. /* Part a */
  4551. emit_uint32(ctx, arity);
  4552. if (arity) {
  4553. /* Part b */
  4554. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  4555. /* Part c */
  4556. for (i = (int32)arity - 1; i >= 0; i--) {
  4557. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4558. emit_byte(ctx, cell);
  4559. }
  4560. /* Part d */
  4561. for (i = (int32)arity - 1; i >= 0; i--) {
  4562. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4563. frame_offset -= cell;
  4564. emit_operand(ctx, *(int16 *)(frame_offset));
  4565. }
  4566. /* Part e */
  4567. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  4568. /* Use start_dynamic_offset which was set in
  4569. copy_params_to_dynamic_space */
  4570. dynamic_offset = frame_csp->start_dynamic_offset
  4571. + wasm_get_cell_num(types, arity);
  4572. else
  4573. dynamic_offset =
  4574. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  4575. if (is_br)
  4576. ctx->dynamic_offset = dynamic_offset;
  4577. for (i = (int32)arity - 1; i >= 0; i--) {
  4578. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4579. dynamic_offset -= cell;
  4580. emit_operand(ctx, dynamic_offset);
  4581. }
  4582. }
  4583. /* Part f */
  4584. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  4585. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  4586. }
  4587. else {
  4588. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  4589. error_buf, error_buf_size))
  4590. return false;
  4591. /* label address, to be patched */
  4592. wasm_loader_emit_ptr(ctx, NULL);
  4593. }
  4594. return true;
  4595. }
  4596. static bool
  4597. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4598. bool disable_emit, int16 operand_offset,
  4599. char *error_buf, uint32 error_buf_size)
  4600. {
  4601. uint32 cell_num_to_push, i;
  4602. if (type == VALUE_TYPE_VOID)
  4603. return true;
  4604. /* only check memory overflow in first traverse */
  4605. if (ctx->p_code_compiled == NULL) {
  4606. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4607. return false;
  4608. }
  4609. if (disable_emit)
  4610. *(ctx->frame_offset)++ = operand_offset;
  4611. else {
  4612. emit_operand(ctx, ctx->dynamic_offset);
  4613. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4614. ctx->dynamic_offset++;
  4615. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4616. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4617. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4618. }
  4619. }
  4620. if (is_32bit_type(type))
  4621. return true;
  4622. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  4623. for (i = 0; i < cell_num_to_push; i++) {
  4624. if (ctx->p_code_compiled == NULL) {
  4625. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4626. return false;
  4627. }
  4628. ctx->frame_offset++;
  4629. if (!disable_emit) {
  4630. ctx->dynamic_offset++;
  4631. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4632. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4633. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4634. }
  4635. }
  4636. }
  4637. return true;
  4638. }
  4639. /* This function should be in front of wasm_loader_pop_frame_ref
  4640. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4641. will be modified by wasm_loader_pop_frame_ref */
  4642. static bool
  4643. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4644. char *error_buf, uint32 error_buf_size)
  4645. {
  4646. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4647. then current block is the function block */
  4648. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4649. BranchBlock *cur_block = ctx->frame_csp - depth;
  4650. int32 available_stack_cell =
  4651. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4652. uint32 cell_num_to_pop;
  4653. /* Directly return success if current block is in stack
  4654. polymorphic state while stack is empty. */
  4655. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4656. return true;
  4657. if (type == VALUE_TYPE_VOID)
  4658. return true;
  4659. /* Change type to ANY when the stack top is ANY, so as to avoid
  4660. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  4661. two offsets */
  4662. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  4663. type = VALUE_TYPE_ANY;
  4664. cell_num_to_pop = wasm_value_type_cell_num(type);
  4665. /* Check the offset stack bottom to ensure the frame offset
  4666. stack will not go underflow. But we don't thrown error
  4667. and return true here, because the error msg should be
  4668. given in wasm_loader_pop_frame_ref */
  4669. if (!check_offset_pop(ctx, cell_num_to_pop))
  4670. return true;
  4671. ctx->frame_offset -= cell_num_to_pop;
  4672. if (check_dynamic_offset_pop(ctx, cell_num_to_pop)
  4673. && (*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4674. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4675. ctx->dynamic_offset -= cell_num_to_pop;
  4676. emit_operand(ctx, *(ctx->frame_offset));
  4677. (void)error_buf;
  4678. (void)error_buf_size;
  4679. return true;
  4680. }
  4681. static bool
  4682. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4683. bool disable_emit, int16 operand_offset,
  4684. char *error_buf, uint32 error_buf_size)
  4685. {
  4686. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  4687. error_buf, error_buf_size)))
  4688. return false;
  4689. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  4690. return false;
  4691. return true;
  4692. }
  4693. static bool
  4694. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4695. char *error_buf, uint32 error_buf_size)
  4696. {
  4697. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  4698. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  4699. return false;
  4700. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  4701. return false;
  4702. return true;
  4703. }
  4704. static bool
  4705. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4706. uint8 type_push, uint8 type_pop,
  4707. bool disable_emit, int16 operand_offset,
  4708. char *error_buf, uint32 error_buf_size)
  4709. {
  4710. uint8 i;
  4711. for (i = 0; i < pop_cnt; i++) {
  4712. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  4713. error_buf_size))
  4714. return false;
  4715. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4716. error_buf_size))
  4717. return false;
  4718. }
  4719. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  4720. operand_offset, error_buf,
  4721. error_buf_size))
  4722. return false;
  4723. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4724. return false;
  4725. return true;
  4726. }
  4727. static int
  4728. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  4729. {
  4730. int64 i64_const1 = *(int64 *)p_i64_const1;
  4731. int64 i64_const2 = *(int64 *)p_i64_const2;
  4732. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  4733. }
  4734. static int
  4735. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  4736. {
  4737. int32 i32_const1 = *(int32 *)p_i32_const1;
  4738. int32 i32_const2 = *(int32 *)p_i32_const2;
  4739. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  4740. }
  4741. static bool
  4742. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  4743. int16 *offset, char *error_buf,
  4744. uint32 error_buf_size)
  4745. {
  4746. if (!ctx->p_code_compiled) {
  4747. /* Treat i64 and f64 as the same by reading i64 value from
  4748. the raw bytes */
  4749. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  4750. /* No slot left, emit const instead */
  4751. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  4752. *offset = 0;
  4753. return true;
  4754. }
  4755. /* Traverse the list if the const num is small */
  4756. if (ctx->i64_const_num < 10) {
  4757. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  4758. if (ctx->i64_consts[i] == *(int64 *)value) {
  4759. *offset = -1;
  4760. return true;
  4761. }
  4762. }
  4763. }
  4764. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  4765. MEM_REALLOC(ctx->i64_consts,
  4766. sizeof(int64) * ctx->i64_const_max_num,
  4767. sizeof(int64) * (ctx->i64_const_max_num * 2));
  4768. ctx->i64_const_max_num *= 2;
  4769. }
  4770. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  4771. }
  4772. else {
  4773. /* Treat i32 and f32 as the same by reading i32 value from
  4774. the raw bytes */
  4775. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  4776. /* No slot left, emit const instead */
  4777. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  4778. *offset = 0;
  4779. return true;
  4780. }
  4781. /* Traverse the list if the const num is small */
  4782. if (ctx->i32_const_num < 10) {
  4783. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  4784. if (ctx->i32_consts[i] == *(int32 *)value) {
  4785. *offset = -1;
  4786. return true;
  4787. }
  4788. }
  4789. }
  4790. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  4791. MEM_REALLOC(ctx->i32_consts,
  4792. sizeof(int32) * ctx->i32_const_max_num,
  4793. sizeof(int32) * (ctx->i32_const_max_num * 2));
  4794. ctx->i32_const_max_num *= 2;
  4795. }
  4796. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  4797. }
  4798. *offset = -1;
  4799. return true;
  4800. }
  4801. else {
  4802. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  4803. int64 key = *(int64 *)value, *i64_const;
  4804. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  4805. sizeof(int64), cmp_i64_const);
  4806. if (!i64_const) { /* not found, emit const instead */
  4807. *offset = 0;
  4808. return true;
  4809. }
  4810. *offset = -(uint32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  4811. + (uint32)(i64_const - ctx->i64_consts) * 2;
  4812. }
  4813. else {
  4814. int32 key = *(int32 *)value, *i32_const;
  4815. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  4816. sizeof(int32), cmp_i32_const);
  4817. if (!i32_const) { /* not found, emit const instead */
  4818. *offset = 0;
  4819. return true;
  4820. }
  4821. *offset = -(uint32)(ctx->i32_const_num)
  4822. + (uint32)(i32_const - ctx->i32_consts);
  4823. }
  4824. return true;
  4825. }
  4826. fail:
  4827. return false;
  4828. }
  4829. /*
  4830. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4831. -- Mostly used for the binary / compare operation
  4832. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4833. -- Mostly used in block / control instructions
  4834. The POP will always emit the offset on the top of the frame_offset stack
  4835. PUSH can be used in two ways:
  4836. 1. directly PUSH:
  4837. PUSH_XXX();
  4838. will allocate a dynamic space and emit
  4839. 2. silent PUSH:
  4840. operand_offset = xxx; disable_emit = true;
  4841. PUSH_XXX();
  4842. only push the frame_offset stack, no emit
  4843. */
  4844. #define PUSH_I32() \
  4845. do { \
  4846. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4847. disable_emit, operand_offset, \
  4848. error_buf, error_buf_size)) \
  4849. goto fail; \
  4850. } while (0)
  4851. #define PUSH_F32() \
  4852. do { \
  4853. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4854. disable_emit, operand_offset, \
  4855. error_buf, error_buf_size)) \
  4856. goto fail; \
  4857. } while (0)
  4858. #define PUSH_I64() \
  4859. do { \
  4860. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4861. disable_emit, operand_offset, \
  4862. error_buf, error_buf_size)) \
  4863. goto fail; \
  4864. } while (0)
  4865. #define PUSH_F64() \
  4866. do { \
  4867. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4868. disable_emit, operand_offset, \
  4869. error_buf, error_buf_size)) \
  4870. goto fail; \
  4871. } while (0)
  4872. #define PUSH_FUNCREF() \
  4873. do { \
  4874. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF, \
  4875. disable_emit, operand_offset, \
  4876. error_buf, error_buf_size)) \
  4877. goto fail; \
  4878. } while (0)
  4879. #define POP_I32() \
  4880. do { \
  4881. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4882. error_buf, error_buf_size)) \
  4883. goto fail; \
  4884. } while (0)
  4885. #define POP_F32() \
  4886. do { \
  4887. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4888. error_buf, error_buf_size)) \
  4889. goto fail; \
  4890. } while (0)
  4891. #define POP_I64() \
  4892. do { \
  4893. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4894. error_buf, error_buf_size)) \
  4895. goto fail; \
  4896. } while (0)
  4897. #define POP_F64() \
  4898. do { \
  4899. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4900. error_buf, error_buf_size)) \
  4901. goto fail; \
  4902. } while (0)
  4903. #define PUSH_OFFSET_TYPE(type) \
  4904. do { \
  4905. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  4906. operand_offset, error_buf, \
  4907. error_buf_size))) \
  4908. goto fail; \
  4909. } while (0)
  4910. #define POP_OFFSET_TYPE(type) \
  4911. do { \
  4912. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  4913. error_buf_size))) \
  4914. goto fail; \
  4915. } while (0)
  4916. #define PUSH_MEM_OFFSET() \
  4917. do { \
  4918. if (!wasm_loader_push_frame_ref_offset(loader_ctx, mem_offset_type, \
  4919. disable_emit, operand_offset, \
  4920. error_buf, error_buf_size)) \
  4921. goto fail; \
  4922. } while (0)
  4923. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  4924. #define PUSH_TBL_ELEM_IDX() \
  4925. do { \
  4926. if (!(wasm_loader_push_frame_ref(loader_ctx, table_elem_idx_type, \
  4927. error_buf, error_buf_size))) \
  4928. goto fail; \
  4929. } while (0)
  4930. #define POP_MEM_OFFSET() \
  4931. do { \
  4932. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, mem_offset_type, \
  4933. error_buf, error_buf_size)) \
  4934. goto fail; \
  4935. } while (0)
  4936. #define POP_TBL_ELEM_IDX() \
  4937. do { \
  4938. if (!(wasm_loader_pop_frame_ref(loader_ctx, table_elem_idx_type, \
  4939. error_buf, error_buf_size))) \
  4940. goto fail; \
  4941. } while (0)
  4942. #define POP_AND_PUSH(type_pop, type_push) \
  4943. do { \
  4944. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4945. loader_ctx, 1, type_push, type_pop, disable_emit, \
  4946. operand_offset, error_buf, error_buf_size))) \
  4947. goto fail; \
  4948. } while (0)
  4949. /* type of POPs should be the same */
  4950. #define POP2_AND_PUSH(type_pop, type_push) \
  4951. do { \
  4952. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4953. loader_ctx, 2, type_push, type_pop, disable_emit, \
  4954. operand_offset, error_buf, error_buf_size))) \
  4955. goto fail; \
  4956. } while (0)
  4957. #else /* WASM_ENABLE_FAST_INTERP */
  4958. #define PUSH_I32() \
  4959. do { \
  4960. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4961. error_buf, error_buf_size))) \
  4962. goto fail; \
  4963. } while (0)
  4964. #define PUSH_F32() \
  4965. do { \
  4966. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4967. error_buf, error_buf_size))) \
  4968. goto fail; \
  4969. } while (0)
  4970. #define PUSH_I64() \
  4971. do { \
  4972. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4973. error_buf, error_buf_size))) \
  4974. goto fail; \
  4975. } while (0)
  4976. #define PUSH_F64() \
  4977. do { \
  4978. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4979. error_buf, error_buf_size))) \
  4980. goto fail; \
  4981. } while (0)
  4982. #define PUSH_FUNCREF() \
  4983. do { \
  4984. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4985. error_buf, error_buf_size))) \
  4986. goto fail; \
  4987. } while (0)
  4988. #define PUSH_MEM_OFFSET() \
  4989. do { \
  4990. if (!(wasm_loader_push_frame_ref(loader_ctx, mem_offset_type, \
  4991. error_buf, error_buf_size))) \
  4992. goto fail; \
  4993. } while (0)
  4994. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  4995. #define PUSH_TBL_ELEM_IDX() \
  4996. do { \
  4997. if (!(wasm_loader_push_frame_ref(loader_ctx, table_elem_idx_type, \
  4998. error_buf, error_buf_size))) \
  4999. goto fail; \
  5000. } while (0)
  5001. #define POP_I32() \
  5002. do { \
  5003. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, error_buf, \
  5004. error_buf_size))) \
  5005. goto fail; \
  5006. } while (0)
  5007. #define POP_F32() \
  5008. do { \
  5009. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, error_buf, \
  5010. error_buf_size))) \
  5011. goto fail; \
  5012. } while (0)
  5013. #define POP_I64() \
  5014. do { \
  5015. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, error_buf, \
  5016. error_buf_size))) \
  5017. goto fail; \
  5018. } while (0)
  5019. #define POP_F64() \
  5020. do { \
  5021. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, error_buf, \
  5022. error_buf_size))) \
  5023. goto fail; \
  5024. } while (0)
  5025. #define POP_FUNCREF() \
  5026. do { \
  5027. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  5028. error_buf, error_buf_size))) \
  5029. goto fail; \
  5030. } while (0)
  5031. #define POP_MEM_OFFSET() \
  5032. do { \
  5033. if (!(wasm_loader_pop_frame_ref(loader_ctx, mem_offset_type, \
  5034. error_buf, error_buf_size))) \
  5035. goto fail; \
  5036. } while (0)
  5037. #define POP_TBL_ELEM_IDX() \
  5038. do { \
  5039. if (!(wasm_loader_pop_frame_ref(loader_ctx, table_elem_idx_type, \
  5040. error_buf, error_buf_size))) \
  5041. goto fail; \
  5042. } while (0)
  5043. #define POP_AND_PUSH(type_pop, type_push) \
  5044. do { \
  5045. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  5046. type_pop, error_buf, \
  5047. error_buf_size))) \
  5048. goto fail; \
  5049. } while (0)
  5050. /* type of POPs should be the same */
  5051. #define POP2_AND_PUSH(type_pop, type_push) \
  5052. do { \
  5053. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  5054. type_pop, error_buf, \
  5055. error_buf_size))) \
  5056. goto fail; \
  5057. } while (0)
  5058. #endif /* WASM_ENABLE_FAST_INTERP */
  5059. #if WASM_ENABLE_FAST_INTERP != 0
  5060. static bool
  5061. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  5062. bool disable_emit, char *error_buf, uint32 error_buf_size)
  5063. {
  5064. int16 operand_offset = 0;
  5065. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  5066. : loader_ctx->frame_csp;
  5067. BlockType *block_type = &block->block_type;
  5068. uint8 *return_types = NULL;
  5069. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  5070. int32 i = 0;
  5071. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  5072. *frame_offset_org = NULL;
  5073. return_count = block_type_get_result_types(block_type, &return_types);
  5074. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  5075. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  5076. if (return_count == 1) {
  5077. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  5078. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  5079. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  5080. /* insert op_copy before else opcode */
  5081. if (opcode == WASM_OP_ELSE)
  5082. skip_label();
  5083. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  5084. : EXT_OP_COPY_STACK_TOP_I64);
  5085. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  5086. emit_operand(loader_ctx, block->dynamic_offset);
  5087. if (opcode == WASM_OP_ELSE) {
  5088. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  5089. }
  5090. else {
  5091. loader_ctx->frame_offset -= cell;
  5092. loader_ctx->dynamic_offset = block->dynamic_offset;
  5093. PUSH_OFFSET_TYPE(return_types[0]);
  5094. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5095. }
  5096. if (opcode == WASM_OP_ELSE)
  5097. emit_label(opcode);
  5098. }
  5099. return true;
  5100. }
  5101. /* Copy stack top values to block's results which are in dynamic space.
  5102. * The instruction format:
  5103. * Part a: values count
  5104. * Part b: all values total cell num
  5105. * Part c: each value's cell_num, src offset and dst offset
  5106. * Part d: each value's src offset and dst offset
  5107. * Part e: each value's dst offset
  5108. */
  5109. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  5110. dynamic_offset = dynamic_offset_org =
  5111. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  5112. /* First traversal to get the count of values needed to be copied. */
  5113. for (i = (int32)return_count - 1; i >= 0; i--) {
  5114. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  5115. frame_offset -= cells;
  5116. dynamic_offset -= cells;
  5117. if (dynamic_offset != *frame_offset) {
  5118. value_count++;
  5119. total_cel_num += cells;
  5120. }
  5121. }
  5122. if (value_count) {
  5123. uint32 j = 0;
  5124. uint8 *emit_data = NULL, *cells = NULL;
  5125. int16 *src_offsets = NULL;
  5126. uint16 *dst_offsets = NULL;
  5127. uint64 size =
  5128. (uint64)value_count
  5129. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  5130. /* Allocate memory for the emit data */
  5131. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5132. return false;
  5133. cells = emit_data;
  5134. src_offsets = (int16 *)(cells + value_count);
  5135. dst_offsets = (uint16 *)(src_offsets + value_count);
  5136. /* insert op_copy before else opcode */
  5137. if (opcode == WASM_OP_ELSE)
  5138. skip_label();
  5139. emit_label(EXT_OP_COPY_STACK_VALUES);
  5140. /* Part a) */
  5141. emit_uint32(loader_ctx, value_count);
  5142. /* Part b) */
  5143. emit_uint32(loader_ctx, total_cel_num);
  5144. /* Second traversal to get each value's cell num, src offset and dst
  5145. * offset. */
  5146. frame_offset = frame_offset_org;
  5147. dynamic_offset = dynamic_offset_org;
  5148. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  5149. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  5150. frame_offset -= cell;
  5151. dynamic_offset -= cell;
  5152. if (dynamic_offset != *frame_offset) {
  5153. /* cell num */
  5154. cells[j] = cell;
  5155. /* src offset */
  5156. src_offsets[j] = *frame_offset;
  5157. /* dst offset */
  5158. dst_offsets[j] = dynamic_offset;
  5159. j++;
  5160. }
  5161. if (opcode == WASM_OP_ELSE) {
  5162. *frame_offset = dynamic_offset;
  5163. }
  5164. else {
  5165. loader_ctx->frame_offset = frame_offset;
  5166. loader_ctx->dynamic_offset = dynamic_offset;
  5167. if (!(wasm_loader_push_frame_offset(
  5168. loader_ctx, return_types[i], disable_emit,
  5169. operand_offset, error_buf, error_buf_size))) {
  5170. wasm_runtime_free(emit_data);
  5171. goto fail;
  5172. }
  5173. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5174. loader_ctx->frame_offset = frame_offset_org;
  5175. loader_ctx->dynamic_offset = dynamic_offset_org;
  5176. }
  5177. }
  5178. bh_assert(j == value_count);
  5179. /* Emit the cells, src_offsets and dst_offsets */
  5180. for (j = 0; j < value_count; j++)
  5181. emit_byte(loader_ctx, cells[j]);
  5182. for (j = 0; j < value_count; j++)
  5183. emit_operand(loader_ctx, src_offsets[j]);
  5184. for (j = 0; j < value_count; j++)
  5185. emit_operand(loader_ctx, dst_offsets[j]);
  5186. if (opcode == WASM_OP_ELSE)
  5187. emit_label(opcode);
  5188. wasm_runtime_free(emit_data);
  5189. }
  5190. return true;
  5191. fail:
  5192. return false;
  5193. }
  5194. #endif /* WASM_ENABLE_FAST_INTERP */
  5195. #define PUSH_TYPE(type) \
  5196. do { \
  5197. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  5198. error_buf_size))) \
  5199. goto fail; \
  5200. } while (0)
  5201. #define POP_TYPE(type) \
  5202. do { \
  5203. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  5204. error_buf_size))) \
  5205. goto fail; \
  5206. } while (0)
  5207. #define PUSH_CSP(label_type, block_type, _start_addr) \
  5208. do { \
  5209. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  5210. _start_addr, error_buf, \
  5211. error_buf_size)) \
  5212. goto fail; \
  5213. } while (0)
  5214. #define POP_CSP() \
  5215. do { \
  5216. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  5217. goto fail; \
  5218. } while (0)
  5219. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  5220. do { \
  5221. read_leb_uint32(p, p_end, local_idx); \
  5222. bh_assert(local_idx < param_count + local_count); \
  5223. local_type = local_idx < param_count \
  5224. ? param_types[local_idx] \
  5225. : local_types[local_idx - param_count]; \
  5226. local_offset = local_offsets[local_idx]; \
  5227. } while (0)
  5228. #define CHECK_MEMORY() \
  5229. do { \
  5230. bh_assert(module->import_memory_count + module->memory_count > 0); \
  5231. } while (0)
  5232. static bool
  5233. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  5234. char *error_buf, uint32 error_buf_size)
  5235. {
  5236. BranchBlock *target_block, *cur_block;
  5237. BlockType *target_block_type;
  5238. uint8 *types = NULL, *frame_ref;
  5239. uint32 arity = 0;
  5240. int32 i, available_stack_cell;
  5241. uint16 cell_num;
  5242. uint8 *frame_ref_old = loader_ctx->frame_ref;
  5243. uint8 *frame_ref_after_popped = NULL;
  5244. uint8 frame_ref_tmp[4] = { 0 };
  5245. uint8 *frame_ref_buf = frame_ref_tmp;
  5246. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  5247. #if WASM_ENABLE_FAST_INTERP != 0
  5248. int16 *frame_offset_old = loader_ctx->frame_offset;
  5249. int16 *frame_offset_after_popped = NULL;
  5250. int16 frame_offset_tmp[4] = { 0 };
  5251. int16 *frame_offset_buf = frame_offset_tmp;
  5252. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  5253. #endif
  5254. bool ret = false;
  5255. bh_assert(loader_ctx->csp_num > 0);
  5256. if (loader_ctx->csp_num - 1 < depth) {
  5257. set_error_buf(error_buf, error_buf_size,
  5258. "unknown label, "
  5259. "unexpected end of section or function");
  5260. return false;
  5261. }
  5262. cur_block = loader_ctx->frame_csp - 1;
  5263. target_block = loader_ctx->frame_csp - (depth + 1);
  5264. target_block_type = &target_block->block_type;
  5265. frame_ref = loader_ctx->frame_ref;
  5266. /* Note: loop's arity is different from if and block. loop's arity is
  5267. * its parameter count while if and block arity is result count.
  5268. */
  5269. if (target_block->label_type == LABEL_TYPE_LOOP)
  5270. arity = block_type_get_param_types(target_block_type, &types);
  5271. else
  5272. arity = block_type_get_result_types(target_block_type, &types);
  5273. /* If the stack is in polymorphic state, just clear the stack
  5274. * and then re-push the values to make the stack top values
  5275. * match block type. */
  5276. if (cur_block->is_stack_polymorphic) {
  5277. for (i = (int32)arity - 1; i >= 0; i--) {
  5278. #if WASM_ENABLE_FAST_INTERP != 0
  5279. POP_OFFSET_TYPE(types[i]);
  5280. #endif
  5281. POP_TYPE(types[i]);
  5282. }
  5283. /* Backup stack data since it may be changed in the below
  5284. push operations, and the stack data may be used when
  5285. checking other target blocks of opcode br_table */
  5286. if (opcode == WASM_OP_BR_TABLE) {
  5287. uint64 total_size;
  5288. frame_ref_after_popped = loader_ctx->frame_ref;
  5289. total_size = (uint64)sizeof(uint8)
  5290. * (frame_ref_old - frame_ref_after_popped);
  5291. if (total_size > sizeof(frame_ref_tmp)
  5292. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  5293. error_buf_size))) {
  5294. goto fail;
  5295. }
  5296. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  5297. frame_ref_after_popped, (uint32)total_size);
  5298. #if WASM_ENABLE_FAST_INTERP != 0
  5299. frame_offset_after_popped = loader_ctx->frame_offset;
  5300. total_size = (uint64)sizeof(int16)
  5301. * (frame_offset_old - frame_offset_after_popped);
  5302. if (total_size > sizeof(frame_offset_tmp)
  5303. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  5304. error_buf_size))) {
  5305. goto fail;
  5306. }
  5307. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  5308. frame_offset_after_popped, (uint32)total_size);
  5309. #endif
  5310. }
  5311. for (i = 0; i < (int32)arity; i++) {
  5312. #if WASM_ENABLE_FAST_INTERP != 0
  5313. bool disable_emit = true;
  5314. int16 operand_offset = 0;
  5315. PUSH_OFFSET_TYPE(types[i]);
  5316. #endif
  5317. PUSH_TYPE(types[i]);
  5318. }
  5319. #if WASM_ENABLE_FAST_INTERP != 0
  5320. emit_br_info(target_block, opcode == WASM_OP_BR);
  5321. #endif
  5322. /* Restore the stack data, note that frame_ref_bottom,
  5323. frame_reftype_map_bottom, frame_offset_bottom may be
  5324. re-allocated in the above push operations */
  5325. if (opcode == WASM_OP_BR_TABLE) {
  5326. uint32 total_size;
  5327. /* The stack operand num should not be smaller than before
  5328. after pop and push operations */
  5329. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  5330. loader_ctx->stack_cell_num = stack_cell_num_old;
  5331. loader_ctx->frame_ref =
  5332. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  5333. total_size = (uint32)sizeof(uint8)
  5334. * (frame_ref_old - frame_ref_after_popped);
  5335. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  5336. frame_ref_buf, total_size);
  5337. #if WASM_ENABLE_FAST_INTERP != 0
  5338. loader_ctx->frame_offset =
  5339. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  5340. total_size = (uint32)sizeof(int16)
  5341. * (frame_offset_old - frame_offset_after_popped);
  5342. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  5343. total_size, frame_offset_buf, total_size);
  5344. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  5345. #endif
  5346. }
  5347. ret = true;
  5348. goto cleanup_and_return;
  5349. }
  5350. available_stack_cell =
  5351. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5352. /* Check stack top values match target block type */
  5353. for (i = (int32)arity - 1; i >= 0; i--) {
  5354. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  5355. error_buf, error_buf_size)) {
  5356. goto fail;
  5357. }
  5358. cell_num = wasm_value_type_cell_num(types[i]);
  5359. frame_ref -= cell_num;
  5360. available_stack_cell -= cell_num;
  5361. }
  5362. #if WASM_ENABLE_FAST_INTERP != 0
  5363. emit_br_info(target_block, opcode == WASM_OP_BR);
  5364. #endif
  5365. ret = true;
  5366. cleanup_and_return:
  5367. fail:
  5368. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  5369. wasm_runtime_free(frame_ref_buf);
  5370. #if WASM_ENABLE_FAST_INTERP != 0
  5371. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  5372. wasm_runtime_free(frame_offset_buf);
  5373. #endif
  5374. return ret;
  5375. }
  5376. static BranchBlock *
  5377. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  5378. uint8 opcode, char *error_buf, uint32 error_buf_size)
  5379. {
  5380. uint8 *p = *p_buf, *p_end = buf_end;
  5381. BranchBlock *frame_csp_tmp;
  5382. uint32 depth;
  5383. read_leb_uint32(p, p_end, depth);
  5384. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  5385. error_buf_size)) {
  5386. goto fail;
  5387. }
  5388. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  5389. *p_buf = p;
  5390. return frame_csp_tmp;
  5391. fail:
  5392. return NULL;
  5393. }
  5394. static bool
  5395. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  5396. char *error_buf, uint32 error_buf_size)
  5397. {
  5398. BlockType *block_type = &block->block_type;
  5399. uint8 *return_types = NULL;
  5400. uint32 return_count = 0;
  5401. int32 available_stack_cell, return_cell_num, i;
  5402. uint8 *frame_ref = NULL;
  5403. available_stack_cell =
  5404. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  5405. return_count = block_type_get_result_types(block_type, &return_types);
  5406. return_cell_num =
  5407. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  5408. /* If the stack is in polymorphic state, just clear the stack
  5409. * and then re-push the values to make the stack top values
  5410. * match block type. */
  5411. if (block->is_stack_polymorphic) {
  5412. for (i = (int32)return_count - 1; i >= 0; i--) {
  5413. #if WASM_ENABLE_FAST_INTERP != 0
  5414. POP_OFFSET_TYPE(return_types[i]);
  5415. #endif
  5416. POP_TYPE(return_types[i]);
  5417. }
  5418. /* Check stack is empty */
  5419. bh_assert(loader_ctx->stack_cell_num == block->stack_cell_num);
  5420. for (i = 0; i < (int32)return_count; i++) {
  5421. #if WASM_ENABLE_FAST_INTERP != 0
  5422. bool disable_emit = true;
  5423. int16 operand_offset = 0;
  5424. PUSH_OFFSET_TYPE(return_types[i]);
  5425. #endif
  5426. PUSH_TYPE(return_types[i]);
  5427. }
  5428. return true;
  5429. }
  5430. /* Check stack cell num equals return cell num */
  5431. bh_assert(available_stack_cell == return_cell_num);
  5432. /* Check stack values match return types */
  5433. frame_ref = loader_ctx->frame_ref;
  5434. for (i = (int32)return_count - 1; i >= 0; i--) {
  5435. if (!check_stack_top_values(frame_ref, available_stack_cell,
  5436. return_types[i], error_buf, error_buf_size))
  5437. return false;
  5438. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  5439. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  5440. }
  5441. (void)return_cell_num;
  5442. return true;
  5443. fail:
  5444. return false;
  5445. }
  5446. #if WASM_ENABLE_FAST_INTERP != 0
  5447. /* Copy parameters to dynamic space.
  5448. * 1) POP original parameter out;
  5449. * 2) Push and copy original values to dynamic space.
  5450. * The copy instruction format:
  5451. * Part a: param count
  5452. * Part b: all param total cell num
  5453. * Part c: each param's cell_num, src offset and dst offset
  5454. * Part d: each param's src offset
  5455. * Part e: each param's dst offset
  5456. */
  5457. static bool
  5458. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  5459. uint32 error_buf_size)
  5460. {
  5461. bool ret = false;
  5462. int16 *frame_offset = NULL;
  5463. uint8 *cells = NULL, cell;
  5464. int16 *src_offsets = NULL;
  5465. uint8 *emit_data = NULL;
  5466. uint32 i;
  5467. BranchBlock *block = loader_ctx->frame_csp - 1;
  5468. BlockType *block_type = &block->block_type;
  5469. WASMFuncType *wasm_type = block_type->u.type;
  5470. uint32 param_count = block_type->u.type->param_count;
  5471. int16 condition_offset = 0;
  5472. bool disable_emit = false;
  5473. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  5474. int16 operand_offset = 0;
  5475. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  5476. bh_assert(size > 0);
  5477. /* For if block, we also need copy the condition operand offset. */
  5478. if (is_if_block)
  5479. size += sizeof(*cells) + sizeof(*src_offsets);
  5480. /* Allocate memory for the emit data */
  5481. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5482. return false;
  5483. cells = emit_data;
  5484. src_offsets = (int16 *)(cells + param_count);
  5485. if (is_if_block)
  5486. condition_offset = *loader_ctx->frame_offset;
  5487. /* POP original parameter out */
  5488. for (i = 0; i < param_count; i++) {
  5489. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  5490. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5491. }
  5492. frame_offset = loader_ctx->frame_offset;
  5493. /* Get each param's cell num and src offset */
  5494. for (i = 0; i < param_count; i++) {
  5495. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  5496. cells[i] = cell;
  5497. src_offsets[i] = *frame_offset;
  5498. frame_offset += cell;
  5499. }
  5500. /* emit copy instruction */
  5501. emit_label(EXT_OP_COPY_STACK_VALUES);
  5502. /* Part a) */
  5503. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  5504. /* Part b) */
  5505. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  5506. : wasm_type->param_cell_num);
  5507. /* Part c) */
  5508. for (i = 0; i < param_count; i++)
  5509. emit_byte(loader_ctx, cells[i]);
  5510. if (is_if_block)
  5511. emit_byte(loader_ctx, 1);
  5512. /* Part d) */
  5513. for (i = 0; i < param_count; i++)
  5514. emit_operand(loader_ctx, src_offsets[i]);
  5515. if (is_if_block)
  5516. emit_operand(loader_ctx, condition_offset);
  5517. /* Since the start offset to save the block's params and
  5518. * the start offset to save the block's results may be
  5519. * different, we remember the dynamic offset for loop block
  5520. * so that we can use it to copy the stack operands to the
  5521. * loop block's params in wasm_loader_emit_br_info. */
  5522. if (block->label_type == LABEL_TYPE_LOOP)
  5523. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  5524. /* Part e) */
  5525. /* Push to dynamic space. The push will emit the dst offset. */
  5526. for (i = 0; i < param_count; i++)
  5527. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  5528. if (is_if_block)
  5529. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  5530. ret = true;
  5531. fail:
  5532. /* Free the emit data */
  5533. wasm_runtime_free(emit_data);
  5534. return ret;
  5535. }
  5536. #endif
  5537. /* reset the stack to the state of before entering the last block */
  5538. #if WASM_ENABLE_FAST_INTERP != 0
  5539. #define RESET_STACK() \
  5540. do { \
  5541. loader_ctx->stack_cell_num = \
  5542. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5543. loader_ctx->frame_ref = \
  5544. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5545. loader_ctx->frame_offset = \
  5546. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  5547. } while (0)
  5548. #else
  5549. #define RESET_STACK() \
  5550. do { \
  5551. loader_ctx->stack_cell_num = \
  5552. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5553. loader_ctx->frame_ref = \
  5554. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5555. } while (0)
  5556. #endif
  5557. /* set current block's stack polymorphic state */
  5558. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  5559. do { \
  5560. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  5561. _cur_block->is_stack_polymorphic = flag; \
  5562. } while (0)
  5563. #define BLOCK_HAS_PARAM(block_type) \
  5564. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  5565. #define PRESERVE_LOCAL_FOR_BLOCK() \
  5566. do { \
  5567. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  5568. error_buf_size))) { \
  5569. goto fail; \
  5570. } \
  5571. } while (0)
  5572. #if WASM_ENABLE_FAST_INTERP == 0
  5573. #define pb_read_leb_uint32 read_leb_uint32
  5574. #define pb_read_leb_int32 read_leb_int32
  5575. #define pb_read_leb_int64 read_leb_int64
  5576. #define pb_read_leb_memarg read_leb_memarg
  5577. #define pb_read_leb_mem_offset read_leb_mem_offset
  5578. #define pb_read_leb_memidx read_leb_memidx
  5579. #else
  5580. /* Read leb without malformed format check */
  5581. static uint64
  5582. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  5583. {
  5584. uint8 *buf = *p_buf;
  5585. uint64 result = 0, byte = 0;
  5586. uint32 shift = 0;
  5587. do {
  5588. byte = *buf++;
  5589. result |= ((byte & 0x7f) << shift);
  5590. shift += 7;
  5591. } while (byte & 0x80);
  5592. if (sign && (shift < maxbits) && (byte & 0x40)) {
  5593. /* Sign extend */
  5594. result |= (~((uint64)0)) << shift;
  5595. }
  5596. *p_buf = buf;
  5597. return result;
  5598. }
  5599. #define pb_read_leb_uint32(p, p_end, res) \
  5600. do { \
  5601. if (!loader_ctx->p_code_compiled) \
  5602. /* Enable format check in the first scan */ \
  5603. read_leb_uint32(p, p_end, res); \
  5604. else \
  5605. /* Disable format check in the second scan */ \
  5606. res = (uint32)read_leb_quick(&p, 32, false); \
  5607. } while (0)
  5608. #define pb_read_leb_int32(p, p_end, res) \
  5609. do { \
  5610. if (!loader_ctx->p_code_compiled) \
  5611. /* Enable format check in the first scan */ \
  5612. read_leb_int32(p, p_end, res); \
  5613. else \
  5614. /* Disable format check in the second scan */ \
  5615. res = (int32)read_leb_quick(&p, 32, true); \
  5616. } while (0)
  5617. #define pb_read_leb_int64(p, p_end, res) \
  5618. do { \
  5619. if (!loader_ctx->p_code_compiled) \
  5620. /* Enable format check in the first scan */ \
  5621. read_leb_int64(p, p_end, res); \
  5622. else \
  5623. /* Disable format check in the second scan */ \
  5624. res = (int64)read_leb_quick(&p, 64, true); \
  5625. } while (0)
  5626. #if WASM_ENABLE_MULTI_MEMORY != 0
  5627. #define pb_read_leb_memarg read_leb_memarg
  5628. #else
  5629. #define pb_read_leb_memarg pb_read_leb_uint32
  5630. #endif
  5631. #if WASM_ENABLE_MEMORY64 != 0
  5632. #define pb_read_leb_mem_offset read_leb_mem_offset
  5633. #else
  5634. #define pb_read_leb_mem_offset pb_read_leb_uint32
  5635. #endif
  5636. #define pb_read_leb_memidx pb_read_leb_uint32
  5637. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  5638. static bool
  5639. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5640. uint32 cur_func_idx, char *error_buf,
  5641. uint32 error_buf_size)
  5642. {
  5643. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  5644. uint32 param_count, local_count, global_count;
  5645. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  5646. table_elem_idx_type;
  5647. BlockType func_block_type;
  5648. uint16 *local_offsets, local_offset;
  5649. uint32 count, local_idx, global_idx, u32, align, i, memidx;
  5650. mem_offset_t mem_offset;
  5651. int32 i32, i32_const = 0;
  5652. int64 i64_const;
  5653. uint8 opcode, u8;
  5654. bool return_value = false;
  5655. WASMLoaderContext *loader_ctx;
  5656. BranchBlock *frame_csp_tmp;
  5657. #if WASM_ENABLE_BULK_MEMORY != 0
  5658. uint32 segment_index;
  5659. #endif
  5660. #if WASM_ENABLE_FAST_INTERP != 0
  5661. int16 operand_offset = 0;
  5662. uint8 last_op = 0;
  5663. bool disable_emit, preserve_local = false, if_condition_available = true;
  5664. float32 f32_const;
  5665. float64 f64_const;
  5666. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  5667. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  5668. #endif
  5669. #if WASM_ENABLE_MEMORY64 != 0
  5670. bool is_memory64 = has_module_memory64(module);
  5671. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  5672. #else
  5673. mem_offset_type = VALUE_TYPE_I32;
  5674. table_elem_idx_type = VALUE_TYPE_I32;
  5675. #endif
  5676. global_count = module->import_global_count + module->global_count;
  5677. param_count = func->func_type->param_count;
  5678. param_types = func->func_type->types;
  5679. func_block_type.is_value_type = false;
  5680. func_block_type.u.type = func->func_type;
  5681. local_count = func->local_count;
  5682. local_types = func->local_types;
  5683. local_offsets = func->local_offsets;
  5684. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  5685. goto fail;
  5686. }
  5687. #if WASM_ENABLE_FAST_INTERP != 0
  5688. /* For the first traverse, the initial value of preserved_local_offset has
  5689. * not been determined, we use the INT16_MAX to represent that a slot has
  5690. * been copied to preserve space. For second traverse, this field will be
  5691. * set to the appropriate value in wasm_loader_ctx_reinit.
  5692. * This is for Issue #1230,
  5693. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  5694. * drop opcodes need to know which slots are preserved, so those slots will
  5695. * not be treated as dynamically allocated slots */
  5696. loader_ctx->preserved_local_offset = INT16_MAX;
  5697. re_scan:
  5698. if (loader_ctx->code_compiled_size > 0) {
  5699. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  5700. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  5701. goto fail;
  5702. }
  5703. p = func->code;
  5704. func->code_compiled = loader_ctx->p_code_compiled;
  5705. func->code_compiled_size = loader_ctx->code_compiled_size;
  5706. if (loader_ctx->i64_const_num > 0) {
  5707. int64 *i64_consts_old = loader_ctx->i64_consts;
  5708. /* Sort the i64 consts */
  5709. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  5710. cmp_i64_const);
  5711. /* Remove the duplicated i64 consts */
  5712. uint32 k = 1;
  5713. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  5714. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  5715. i64_consts_old[k++] = i64_consts_old[i];
  5716. }
  5717. }
  5718. if (k < loader_ctx->i64_const_num) {
  5719. int64 *i64_consts_new;
  5720. /* Try to reallocate memory with a smaller size */
  5721. if ((i64_consts_new =
  5722. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  5723. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  5724. i64_consts_old, (uint32)sizeof(int64) * k);
  5725. /* Free the old memory */
  5726. wasm_runtime_free(i64_consts_old);
  5727. loader_ctx->i64_consts = i64_consts_new;
  5728. loader_ctx->i64_const_max_num = k;
  5729. }
  5730. loader_ctx->i64_const_num = k;
  5731. }
  5732. }
  5733. if (loader_ctx->i32_const_num > 0) {
  5734. int32 *i32_consts_old = loader_ctx->i32_consts;
  5735. /* Sort the i32 consts */
  5736. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  5737. cmp_i32_const);
  5738. /* Remove the duplicated i32 consts */
  5739. uint32 k = 1;
  5740. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  5741. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  5742. i32_consts_old[k++] = i32_consts_old[i];
  5743. }
  5744. }
  5745. if (k < loader_ctx->i32_const_num) {
  5746. int32 *i32_consts_new;
  5747. /* Try to reallocate memory with a smaller size */
  5748. if ((i32_consts_new =
  5749. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  5750. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  5751. i32_consts_old, (uint32)sizeof(int32) * k);
  5752. /* Free the old memory */
  5753. wasm_runtime_free(i32_consts_old);
  5754. loader_ctx->i32_consts = i32_consts_new;
  5755. loader_ctx->i32_const_max_num = k;
  5756. }
  5757. loader_ctx->i32_const_num = k;
  5758. }
  5759. }
  5760. }
  5761. #endif
  5762. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  5763. while (p < p_end) {
  5764. opcode = *p++;
  5765. #if WASM_ENABLE_FAST_INTERP != 0
  5766. p_org = p;
  5767. disable_emit = false;
  5768. emit_label(opcode);
  5769. #endif
  5770. switch (opcode) {
  5771. case WASM_OP_UNREACHABLE:
  5772. RESET_STACK();
  5773. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5774. break;
  5775. case WASM_OP_NOP:
  5776. #if WASM_ENABLE_FAST_INTERP != 0
  5777. skip_label();
  5778. #endif
  5779. break;
  5780. case WASM_OP_IF:
  5781. {
  5782. #if WASM_ENABLE_FAST_INTERP != 0
  5783. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  5784. int32 available_stack_cell =
  5785. (int32)(loader_ctx->stack_cell_num
  5786. - parent_block->stack_cell_num);
  5787. if (available_stack_cell <= 0
  5788. && parent_block->is_stack_polymorphic)
  5789. if_condition_available = false;
  5790. else
  5791. if_condition_available = true;
  5792. PRESERVE_LOCAL_FOR_BLOCK();
  5793. #endif
  5794. POP_I32();
  5795. goto handle_op_block_and_loop;
  5796. }
  5797. case WASM_OP_BLOCK:
  5798. case WASM_OP_LOOP:
  5799. #if WASM_ENABLE_FAST_INTERP != 0
  5800. PRESERVE_LOCAL_FOR_BLOCK();
  5801. #endif
  5802. handle_op_block_and_loop:
  5803. {
  5804. uint8 value_type;
  5805. BlockType block_type;
  5806. #if WASM_ENABLE_FAST_INTERP != 0
  5807. uint32 available_params = 0;
  5808. #endif
  5809. p_org = p - 1;
  5810. value_type = read_uint8(p);
  5811. if (is_byte_a_type(value_type)) {
  5812. /* If the first byte is one of these special values:
  5813. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  5814. * the single return value. */
  5815. block_type.is_value_type = true;
  5816. block_type.u.value_type.type = value_type;
  5817. }
  5818. else {
  5819. int32 type_index;
  5820. /* Resolve the leb128 encoded type index as block type */
  5821. p--;
  5822. pb_read_leb_int32(p, p_end, type_index);
  5823. bh_assert((uint32)type_index < module->type_count);
  5824. block_type.is_value_type = false;
  5825. block_type.u.type = module->types[type_index];
  5826. #if WASM_ENABLE_FAST_INTERP == 0
  5827. /* If block use type index as block type, change the opcode
  5828. * to new extended opcode so that interpreter can resolve
  5829. * the block quickly.
  5830. */
  5831. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  5832. #endif
  5833. }
  5834. /* Pop block parameters from stack */
  5835. if (BLOCK_HAS_PARAM(block_type)) {
  5836. WASMFuncType *wasm_type = block_type.u.type;
  5837. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5838. #if WASM_ENABLE_FAST_INTERP != 0
  5839. uint32 cell_num;
  5840. available_params = block_type.u.type->param_count;
  5841. #endif
  5842. for (i = 0; i < block_type.u.type->param_count; i++) {
  5843. int32 available_stack_cell =
  5844. (int32)(loader_ctx->stack_cell_num
  5845. - cur_block->stack_cell_num);
  5846. if (available_stack_cell <= 0
  5847. && cur_block->is_stack_polymorphic) {
  5848. #if WASM_ENABLE_FAST_INTERP != 0
  5849. available_params = i;
  5850. #endif
  5851. break;
  5852. }
  5853. POP_TYPE(
  5854. wasm_type->types[wasm_type->param_count - i - 1]);
  5855. #if WASM_ENABLE_FAST_INTERP != 0
  5856. /* decrease the frame_offset pointer accordingly to keep
  5857. * consistent with frame_ref stack */
  5858. cell_num = wasm_value_type_cell_num(
  5859. wasm_type->types[wasm_type->param_count - i - 1]);
  5860. loader_ctx->frame_offset -= cell_num;
  5861. #endif
  5862. }
  5863. }
  5864. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  5865. block_type, p);
  5866. /* Pass parameters to block */
  5867. if (BLOCK_HAS_PARAM(block_type)) {
  5868. for (i = 0; i < block_type.u.type->param_count; i++) {
  5869. #if WASM_ENABLE_FAST_INTERP != 0
  5870. uint32 cell_num = wasm_value_type_cell_num(
  5871. block_type.u.type->types[i]);
  5872. if (i >= available_params) {
  5873. /* If there isn't enough data on stack, push a dummy
  5874. * offset to keep the stack consistent with
  5875. * frame_ref.
  5876. * Since the stack is already in polymorphic state,
  5877. * the opcode will not be executed, so the dummy
  5878. * offset won't cause any error */
  5879. uint32 n;
  5880. for (n = 0; n < cell_num; n++) {
  5881. if (loader_ctx->p_code_compiled == NULL) {
  5882. if (!check_offset_push(loader_ctx,
  5883. error_buf,
  5884. error_buf_size))
  5885. goto fail;
  5886. }
  5887. *loader_ctx->frame_offset++ = 0;
  5888. }
  5889. }
  5890. else {
  5891. loader_ctx->frame_offset += cell_num;
  5892. }
  5893. #endif
  5894. PUSH_TYPE(block_type.u.type->types[i]);
  5895. }
  5896. }
  5897. #if WASM_ENABLE_FAST_INTERP != 0
  5898. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  5899. skip_label();
  5900. if (BLOCK_HAS_PARAM(block_type)) {
  5901. /* Make sure params are in dynamic space */
  5902. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  5903. error_buf_size))
  5904. goto fail;
  5905. }
  5906. if (opcode == WASM_OP_LOOP) {
  5907. (loader_ctx->frame_csp - 1)->code_compiled =
  5908. loader_ctx->p_code_compiled;
  5909. }
  5910. }
  5911. else if (opcode == WASM_OP_IF) {
  5912. BranchBlock *block = loader_ctx->frame_csp - 1;
  5913. /* If block has parameters, we should make sure they are in
  5914. * dynamic space. Otherwise, when else branch is missing,
  5915. * the later opcode may consume incorrect operand offset.
  5916. * Spec case:
  5917. * (func (export "params-id") (param i32) (result i32)
  5918. * (i32.const 1)
  5919. * (i32.const 2)
  5920. * (if (param i32 i32) (result i32 i32) (local.get 0)
  5921. * (then)) (i32.add)
  5922. * )
  5923. *
  5924. * So we should emit a copy instruction before the if.
  5925. *
  5926. * And we also need to save the parameter offsets and
  5927. * recover them before entering else branch.
  5928. *
  5929. */
  5930. if (BLOCK_HAS_PARAM(block_type)) {
  5931. uint64 size;
  5932. /* In polymorphic state, there may be no if condition on
  5933. * the stack, so the offset may not emitted */
  5934. if (if_condition_available) {
  5935. /* skip the if condition operand offset */
  5936. wasm_loader_emit_backspace(loader_ctx,
  5937. sizeof(int16));
  5938. }
  5939. /* skip the if label */
  5940. skip_label();
  5941. /* Emit a copy instruction */
  5942. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  5943. error_buf_size))
  5944. goto fail;
  5945. /* Emit the if instruction */
  5946. emit_label(opcode);
  5947. /* Emit the new condition operand offset */
  5948. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  5949. /* Save top param_count values of frame_offset stack, so
  5950. * that we can recover it before executing else branch
  5951. */
  5952. size = sizeof(int16)
  5953. * (uint64)block_type.u.type->param_cell_num;
  5954. if (!(block->param_frame_offsets = loader_malloc(
  5955. size, error_buf, error_buf_size)))
  5956. goto fail;
  5957. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  5958. loader_ctx->frame_offset
  5959. - size / sizeof(int16),
  5960. (uint32)size);
  5961. }
  5962. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  5963. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  5964. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5965. }
  5966. #endif
  5967. break;
  5968. }
  5969. case WASM_OP_ELSE:
  5970. handle_op_else:
  5971. {
  5972. BranchBlock *block = NULL;
  5973. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  5974. bh_assert(loader_ctx->csp_num >= 2
  5975. /* the matched if is found */
  5976. && (loader_ctx->frame_csp - 1)->label_type
  5977. == LABEL_TYPE_IF
  5978. /* duplicated else isn't found */
  5979. && !(loader_ctx->frame_csp - 1)->else_addr);
  5980. block = loader_ctx->frame_csp - 1;
  5981. /* check whether if branch's stack matches its result type */
  5982. if (!check_block_stack(loader_ctx, block, error_buf,
  5983. error_buf_size))
  5984. goto fail;
  5985. block->else_addr = p - 1;
  5986. #if WASM_ENABLE_FAST_INTERP != 0
  5987. /* if the result of if branch is in local or const area, add a
  5988. * copy op */
  5989. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  5990. error_buf, error_buf_size)) {
  5991. goto fail;
  5992. }
  5993. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5994. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  5995. #endif
  5996. RESET_STACK();
  5997. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5998. /* Pass parameters to if-false branch */
  5999. if (BLOCK_HAS_PARAM(block_type)) {
  6000. for (i = 0; i < block_type.u.type->param_count; i++)
  6001. PUSH_TYPE(block_type.u.type->types[i]);
  6002. }
  6003. #if WASM_ENABLE_FAST_INTERP != 0
  6004. /* Recover top param_count values of frame_offset stack */
  6005. if (BLOCK_HAS_PARAM((block_type))) {
  6006. uint32 size;
  6007. size = sizeof(int16) * block_type.u.type->param_cell_num;
  6008. bh_memcpy_s(loader_ctx->frame_offset, size,
  6009. block->param_frame_offsets, size);
  6010. loader_ctx->frame_offset += (size / sizeof(int16));
  6011. }
  6012. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  6013. #endif
  6014. break;
  6015. }
  6016. case WASM_OP_END:
  6017. {
  6018. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6019. /* check whether block stack matches its result type */
  6020. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  6021. error_buf_size))
  6022. goto fail;
  6023. /* if there is no else branch, make a virtual else opcode for
  6024. easier integrity check and to copy the correct results to
  6025. the block return address for fast-interp mode:
  6026. change if block from `if ... end` to `if ... else end` */
  6027. if (cur_block->label_type == LABEL_TYPE_IF
  6028. && !cur_block->else_addr) {
  6029. opcode = WASM_OP_ELSE;
  6030. p--;
  6031. #if WASM_ENABLE_FAST_INTERP != 0
  6032. p_org = p;
  6033. skip_label();
  6034. disable_emit = false;
  6035. emit_label(opcode);
  6036. #endif
  6037. goto handle_op_else;
  6038. }
  6039. POP_CSP();
  6040. #if WASM_ENABLE_FAST_INTERP != 0
  6041. skip_label();
  6042. /* copy the result to the block return address */
  6043. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  6044. error_buf, error_buf_size)) {
  6045. free_label_patch_list(loader_ctx->frame_csp);
  6046. goto fail;
  6047. }
  6048. apply_label_patch(loader_ctx, 0, PATCH_END);
  6049. free_label_patch_list(loader_ctx->frame_csp);
  6050. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  6051. int32 idx;
  6052. uint8 ret_type;
  6053. emit_label(WASM_OP_RETURN);
  6054. for (idx = (int32)func->func_type->result_count - 1;
  6055. idx >= 0; idx--) {
  6056. ret_type = *(func->func_type->types
  6057. + func->func_type->param_count + idx);
  6058. POP_OFFSET_TYPE(ret_type);
  6059. }
  6060. }
  6061. #endif
  6062. if (loader_ctx->csp_num > 0) {
  6063. loader_ctx->frame_csp->end_addr = p - 1;
  6064. }
  6065. else {
  6066. /* end of function block, function will return */
  6067. bh_assert(p == p_end);
  6068. }
  6069. break;
  6070. }
  6071. case WASM_OP_BR:
  6072. {
  6073. if (!(frame_csp_tmp =
  6074. check_branch_block(loader_ctx, &p, p_end, opcode,
  6075. error_buf, error_buf_size)))
  6076. goto fail;
  6077. RESET_STACK();
  6078. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6079. break;
  6080. }
  6081. case WASM_OP_BR_IF:
  6082. {
  6083. POP_I32();
  6084. if (!(frame_csp_tmp =
  6085. check_branch_block(loader_ctx, &p, p_end, opcode,
  6086. error_buf, error_buf_size)))
  6087. goto fail;
  6088. break;
  6089. }
  6090. case WASM_OP_BR_TABLE:
  6091. {
  6092. uint8 *ret_types = NULL;
  6093. uint32 ret_count = 0, depth = 0;
  6094. #if WASM_ENABLE_FAST_INTERP == 0
  6095. BrTableCache *br_table_cache = NULL;
  6096. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  6097. uint32 j;
  6098. #endif
  6099. pb_read_leb_uint32(p, p_end, count);
  6100. #if WASM_ENABLE_FAST_INTERP != 0
  6101. emit_uint32(loader_ctx, count);
  6102. #endif
  6103. POP_I32();
  6104. /* Get each depth and check it */
  6105. p_org = p;
  6106. for (i = 0; i <= count; i++) {
  6107. pb_read_leb_uint32(p, p_end, depth);
  6108. bh_assert(loader_ctx->csp_num > 0);
  6109. bh_assert(loader_ctx->csp_num - 1 >= depth);
  6110. (void)depth;
  6111. }
  6112. p = p_org;
  6113. #if WASM_ENABLE_FAST_INTERP == 0
  6114. p_depth_begin = p_depth = p;
  6115. #endif
  6116. for (i = 0; i <= count; i++) {
  6117. if (!(frame_csp_tmp =
  6118. check_branch_block(loader_ctx, &p, p_end, opcode,
  6119. error_buf, error_buf_size)))
  6120. goto fail;
  6121. #if WASM_ENABLE_FAST_INTERP == 0
  6122. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  6123. if (br_table_cache) {
  6124. br_table_cache->br_depths[i] = depth;
  6125. }
  6126. else {
  6127. if (depth > 255) {
  6128. /* The depth cannot be stored in one byte,
  6129. create br_table cache to store each depth */
  6130. if (!(br_table_cache = loader_malloc(
  6131. offsetof(BrTableCache, br_depths)
  6132. + sizeof(uint32)
  6133. * (uint64)(count + 1),
  6134. error_buf, error_buf_size))) {
  6135. goto fail;
  6136. }
  6137. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  6138. br_table_cache->br_table_op_addr = p_opcode;
  6139. br_table_cache->br_count = count;
  6140. /* Copy previous depths which are one byte */
  6141. for (j = 0; j < i; j++) {
  6142. br_table_cache->br_depths[j] = p_depth_begin[j];
  6143. }
  6144. br_table_cache->br_depths[i] = depth;
  6145. bh_list_insert(module->br_table_cache_list,
  6146. br_table_cache);
  6147. }
  6148. else {
  6149. /* The depth can be stored in one byte, use the
  6150. byte of the leb to store it */
  6151. *p_depth++ = (uint8)depth;
  6152. }
  6153. }
  6154. #endif
  6155. }
  6156. #if WASM_ENABLE_FAST_INTERP == 0
  6157. /* Set the tailing bytes to nop */
  6158. if (br_table_cache)
  6159. p_depth = p_depth_begin;
  6160. while (p_depth < p)
  6161. *p_depth++ = WASM_OP_NOP;
  6162. #endif
  6163. RESET_STACK();
  6164. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6165. (void)ret_count;
  6166. (void)ret_types;
  6167. break;
  6168. }
  6169. case WASM_OP_RETURN:
  6170. {
  6171. int32 idx;
  6172. uint8 ret_type;
  6173. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  6174. idx--) {
  6175. ret_type = *(func->func_type->types
  6176. + func->func_type->param_count + idx);
  6177. #if WASM_ENABLE_FAST_INTERP != 0
  6178. /* emit the offset after return opcode */
  6179. POP_OFFSET_TYPE(ret_type);
  6180. #endif
  6181. POP_TYPE(ret_type);
  6182. }
  6183. RESET_STACK();
  6184. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6185. break;
  6186. }
  6187. case WASM_OP_CALL:
  6188. #if WASM_ENABLE_TAIL_CALL != 0
  6189. case WASM_OP_RETURN_CALL:
  6190. #endif
  6191. {
  6192. WASMFuncType *func_type;
  6193. uint32 func_idx;
  6194. int32 idx;
  6195. pb_read_leb_uint32(p, p_end, func_idx);
  6196. #if WASM_ENABLE_FAST_INTERP != 0
  6197. /* we need to emit func_idx before arguments */
  6198. emit_uint32(loader_ctx, func_idx);
  6199. #endif
  6200. bh_assert(func_idx < module->import_function_count
  6201. + module->function_count);
  6202. if (func_idx < module->import_function_count)
  6203. func_type =
  6204. module->import_functions[func_idx].u.function.func_type;
  6205. else
  6206. func_type = module
  6207. ->functions[func_idx
  6208. - module->import_function_count]
  6209. ->func_type;
  6210. if (func_type->param_count > 0) {
  6211. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6212. idx--) {
  6213. #if WASM_ENABLE_FAST_INTERP != 0
  6214. POP_OFFSET_TYPE(func_type->types[idx]);
  6215. #endif
  6216. POP_TYPE(func_type->types[idx]);
  6217. }
  6218. }
  6219. #if WASM_ENABLE_TAIL_CALL != 0
  6220. if (opcode == WASM_OP_CALL) {
  6221. #endif
  6222. for (i = 0; i < func_type->result_count; i++) {
  6223. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6224. #if WASM_ENABLE_FAST_INTERP != 0
  6225. /* Here we emit each return value's dynamic_offset. But
  6226. * in fact these offsets are continuous, so interpreter
  6227. * only need to get the first return value's offset.
  6228. */
  6229. PUSH_OFFSET_TYPE(
  6230. func_type->types[func_type->param_count + i]);
  6231. #endif
  6232. }
  6233. #if WASM_ENABLE_TAIL_CALL != 0
  6234. }
  6235. else {
  6236. bh_assert(func_type->result_count
  6237. == func->func_type->result_count);
  6238. for (i = 0; i < func_type->result_count; i++) {
  6239. bh_assert(
  6240. func_type->types[func_type->param_count + i]
  6241. == func->func_type
  6242. ->types[func->func_type->param_count + i]);
  6243. }
  6244. }
  6245. #endif
  6246. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6247. || WASM_ENABLE_WAMR_COMPILER != 0
  6248. func->has_op_func_call = true;
  6249. #endif
  6250. break;
  6251. }
  6252. case WASM_OP_CALL_INDIRECT:
  6253. #if WASM_ENABLE_TAIL_CALL != 0
  6254. case WASM_OP_RETURN_CALL_INDIRECT:
  6255. #endif
  6256. {
  6257. int32 idx;
  6258. WASMFuncType *func_type;
  6259. uint32 type_idx, table_idx;
  6260. bh_assert(module->import_table_count + module->table_count > 0);
  6261. pb_read_leb_uint32(p, p_end, type_idx);
  6262. #if WASM_ENABLE_CALL_INDIRECT_OVERLONG != 0
  6263. pb_read_leb_uint32(p, p_end, table_idx);
  6264. #else
  6265. CHECK_BUF(p, p_end, 1);
  6266. table_idx = read_uint8(p);
  6267. #endif
  6268. if (!check_table_index(module, table_idx, error_buf,
  6269. error_buf_size)) {
  6270. goto fail;
  6271. }
  6272. bh_assert(
  6273. (table_idx < module->import_table_count
  6274. ? module->import_tables[table_idx]
  6275. .u.table.table_type.elem_type
  6276. : module
  6277. ->tables[table_idx - module->import_table_count]
  6278. .table_type.elem_type)
  6279. == VALUE_TYPE_FUNCREF);
  6280. #if WASM_ENABLE_FAST_INTERP != 0
  6281. /* we need to emit before arguments */
  6282. emit_uint32(loader_ctx, type_idx);
  6283. emit_uint32(loader_ctx, table_idx);
  6284. #endif
  6285. #if WASM_ENABLE_MEMORY64 != 0
  6286. table_elem_idx_type = is_table_64bit(module, table_idx)
  6287. ? VALUE_TYPE_I64
  6288. : VALUE_TYPE_I32;
  6289. #endif
  6290. /* skip elem idx */
  6291. POP_TBL_ELEM_IDX();
  6292. bh_assert(type_idx < module->type_count);
  6293. func_type = module->types[type_idx];
  6294. if (func_type->param_count > 0) {
  6295. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6296. idx--) {
  6297. #if WASM_ENABLE_FAST_INTERP != 0
  6298. POP_OFFSET_TYPE(func_type->types[idx]);
  6299. #endif
  6300. POP_TYPE(func_type->types[idx]);
  6301. }
  6302. }
  6303. #if WASM_ENABLE_TAIL_CALL != 0
  6304. if (opcode == WASM_OP_CALL) {
  6305. #endif
  6306. for (i = 0; i < func_type->result_count; i++) {
  6307. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6308. #if WASM_ENABLE_FAST_INTERP != 0
  6309. PUSH_OFFSET_TYPE(
  6310. func_type->types[func_type->param_count + i]);
  6311. #endif
  6312. }
  6313. #if WASM_ENABLE_TAIL_CALL != 0
  6314. }
  6315. else {
  6316. bh_assert(func_type->result_count
  6317. == func->func_type->result_count);
  6318. for (i = 0; i < func_type->result_count; i++) {
  6319. bh_assert(
  6320. func_type->types[func_type->param_count + i]
  6321. == func->func_type
  6322. ->types[func->func_type->param_count + i]);
  6323. }
  6324. }
  6325. #endif
  6326. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6327. || WASM_ENABLE_WAMR_COMPILER != 0
  6328. func->has_op_func_call = true;
  6329. #endif
  6330. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6331. func->has_op_call_indirect = true;
  6332. #endif
  6333. break;
  6334. }
  6335. #if WASM_ENABLE_EXCE_HANDLING != 0
  6336. case WASM_OP_TRY:
  6337. case WASM_OP_CATCH:
  6338. case WASM_OP_THROW:
  6339. case WASM_OP_RETHROW:
  6340. case WASM_OP_DELEGATE:
  6341. case WASM_OP_CATCH_ALL:
  6342. /* TODO */
  6343. set_error_buf(error_buf, error_buf_size, "unsupported opcode");
  6344. goto fail;
  6345. #endif
  6346. case WASM_OP_DROP:
  6347. {
  6348. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6349. int32 available_stack_cell =
  6350. (int32)(loader_ctx->stack_cell_num
  6351. - cur_block->stack_cell_num);
  6352. bh_assert(!(available_stack_cell <= 0
  6353. && !cur_block->is_stack_polymorphic));
  6354. if (available_stack_cell > 0) {
  6355. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  6356. loader_ctx->frame_ref--;
  6357. loader_ctx->stack_cell_num--;
  6358. #if WASM_ENABLE_FAST_INTERP != 0
  6359. skip_label();
  6360. loader_ctx->frame_offset--;
  6361. if ((*(loader_ctx->frame_offset)
  6362. > loader_ctx->start_dynamic_offset)
  6363. && (*(loader_ctx->frame_offset)
  6364. < loader_ctx->max_dynamic_offset))
  6365. loader_ctx->dynamic_offset--;
  6366. #endif
  6367. }
  6368. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  6369. loader_ctx->frame_ref -= 2;
  6370. loader_ctx->stack_cell_num -= 2;
  6371. #if WASM_ENABLE_FAST_INTERP == 0
  6372. *(p - 1) = WASM_OP_DROP_64;
  6373. #endif
  6374. #if WASM_ENABLE_FAST_INTERP != 0
  6375. skip_label();
  6376. loader_ctx->frame_offset -= 2;
  6377. if ((*(loader_ctx->frame_offset)
  6378. > loader_ctx->start_dynamic_offset)
  6379. && (*(loader_ctx->frame_offset)
  6380. < loader_ctx->max_dynamic_offset))
  6381. loader_ctx->dynamic_offset -= 2;
  6382. #endif
  6383. }
  6384. else {
  6385. bh_assert(0);
  6386. }
  6387. }
  6388. else {
  6389. #if WASM_ENABLE_FAST_INTERP != 0
  6390. skip_label();
  6391. #endif
  6392. }
  6393. break;
  6394. }
  6395. case WASM_OP_SELECT:
  6396. {
  6397. uint8 ref_type;
  6398. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6399. int32 available_stack_cell;
  6400. #if WASM_ENABLE_FAST_INTERP != 0
  6401. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  6402. #endif
  6403. POP_I32();
  6404. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  6405. - cur_block->stack_cell_num);
  6406. bh_assert(!(available_stack_cell <= 0
  6407. && !cur_block->is_stack_polymorphic));
  6408. if (available_stack_cell > 0) {
  6409. switch (*(loader_ctx->frame_ref - 1)) {
  6410. case REF_I32:
  6411. case REF_F32:
  6412. case REF_ANY:
  6413. break;
  6414. case REF_I64_2:
  6415. case REF_F64_2:
  6416. #if WASM_ENABLE_FAST_INTERP == 0
  6417. *(p - 1) = WASM_OP_SELECT_64;
  6418. #endif
  6419. #if WASM_ENABLE_FAST_INTERP != 0
  6420. if (loader_ctx->p_code_compiled) {
  6421. uint8 opcode_tmp = WASM_OP_SELECT_64;
  6422. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6423. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6424. *(void **)(p_code_compiled_tmp
  6425. - sizeof(void *)) =
  6426. handle_table[opcode_tmp];
  6427. #else
  6428. #if UINTPTR_MAX == UINT64_MAX
  6429. /* emit int32 relative offset in 64-bit target
  6430. */
  6431. int32 offset =
  6432. (int32)((uint8 *)handle_table[opcode_tmp]
  6433. - (uint8 *)handle_table[0]);
  6434. *(int32 *)(p_code_compiled_tmp
  6435. - sizeof(int32)) = offset;
  6436. #else
  6437. /* emit uint32 label address in 32-bit target */
  6438. *(uint32 *)(p_code_compiled_tmp
  6439. - sizeof(uint32)) =
  6440. (uint32)(uintptr_t)handle_table[opcode_tmp];
  6441. #endif
  6442. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6443. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6444. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6445. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6446. #else
  6447. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6448. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6449. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6450. }
  6451. #endif
  6452. break;
  6453. }
  6454. ref_type = *(loader_ctx->frame_ref - 1);
  6455. #if WASM_ENABLE_FAST_INTERP != 0
  6456. POP_OFFSET_TYPE(ref_type);
  6457. #endif
  6458. POP_TYPE(ref_type);
  6459. #if WASM_ENABLE_FAST_INTERP != 0
  6460. POP_OFFSET_TYPE(ref_type);
  6461. #endif
  6462. POP_TYPE(ref_type);
  6463. #if WASM_ENABLE_FAST_INTERP != 0
  6464. PUSH_OFFSET_TYPE(ref_type);
  6465. #endif
  6466. PUSH_TYPE(ref_type);
  6467. }
  6468. else {
  6469. #if WASM_ENABLE_FAST_INTERP != 0
  6470. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  6471. #endif
  6472. PUSH_TYPE(VALUE_TYPE_ANY);
  6473. }
  6474. break;
  6475. }
  6476. #if WASM_ENABLE_REF_TYPES != 0
  6477. case WASM_OP_SELECT_T:
  6478. {
  6479. uint8 vec_len, ref_type;
  6480. #if WASM_ENABLE_FAST_INTERP != 0
  6481. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  6482. #endif
  6483. pb_read_leb_uint32(p, p_end, vec_len);
  6484. if (vec_len != 1) {
  6485. /* typed select must have exactly one result */
  6486. set_error_buf(error_buf, error_buf_size,
  6487. "invalid result arity");
  6488. goto fail;
  6489. }
  6490. CHECK_BUF(p, p_end, 1);
  6491. ref_type = read_uint8(p);
  6492. if (!is_valid_value_type_for_interpreter(ref_type)) {
  6493. set_error_buf(error_buf, error_buf_size,
  6494. "unknown value type");
  6495. goto fail;
  6496. }
  6497. POP_I32();
  6498. #if WASM_ENABLE_FAST_INTERP != 0
  6499. if (loader_ctx->p_code_compiled) {
  6500. uint8 opcode_tmp = WASM_OP_SELECT;
  6501. if (ref_type == VALUE_TYPE_F64
  6502. || ref_type == VALUE_TYPE_I64)
  6503. opcode_tmp = WASM_OP_SELECT_64;
  6504. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6505. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6506. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  6507. handle_table[opcode_tmp];
  6508. #else
  6509. #if UINTPTR_MAX == UINT64_MAX
  6510. /* emit int32 relative offset in 64-bit target */
  6511. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  6512. - (uint8 *)handle_table[0]);
  6513. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) = offset;
  6514. #else
  6515. /* emit uint32 label address in 32-bit target */
  6516. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  6517. (uint32)(uintptr_t)handle_table[opcode_tmp];
  6518. #endif
  6519. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6520. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6521. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6522. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6523. #else
  6524. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6525. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6526. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6527. }
  6528. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6529. #if WASM_ENABLE_FAST_INTERP != 0
  6530. POP_OFFSET_TYPE(ref_type);
  6531. POP_TYPE(ref_type);
  6532. POP_OFFSET_TYPE(ref_type);
  6533. POP_TYPE(ref_type);
  6534. PUSH_OFFSET_TYPE(ref_type);
  6535. PUSH_TYPE(ref_type);
  6536. #else
  6537. POP2_AND_PUSH(ref_type, ref_type);
  6538. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6539. (void)vec_len;
  6540. break;
  6541. }
  6542. /* table.get x. tables[x]. [it] -> [t] */
  6543. /* table.set x. tables[x]. [it t] -> [] */
  6544. case WASM_OP_TABLE_GET:
  6545. case WASM_OP_TABLE_SET:
  6546. {
  6547. uint8 decl_ref_type;
  6548. uint32 table_idx;
  6549. pb_read_leb_uint32(p, p_end, table_idx);
  6550. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  6551. error_buf, error_buf_size))
  6552. goto fail;
  6553. #if WASM_ENABLE_FAST_INTERP != 0
  6554. emit_uint32(loader_ctx, table_idx);
  6555. #endif
  6556. #if WASM_ENABLE_MEMORY64 != 0
  6557. table_elem_idx_type = is_table_64bit(module, table_idx)
  6558. ? VALUE_TYPE_I64
  6559. : VALUE_TYPE_I32;
  6560. #endif
  6561. if (opcode == WASM_OP_TABLE_GET) {
  6562. POP_TBL_ELEM_IDX();
  6563. #if WASM_ENABLE_FAST_INTERP != 0
  6564. PUSH_OFFSET_TYPE(decl_ref_type);
  6565. #endif
  6566. PUSH_TYPE(decl_ref_type);
  6567. }
  6568. else {
  6569. #if WASM_ENABLE_FAST_INTERP != 0
  6570. POP_OFFSET_TYPE(decl_ref_type);
  6571. #endif
  6572. POP_TYPE(decl_ref_type);
  6573. POP_TBL_ELEM_IDX();
  6574. }
  6575. break;
  6576. }
  6577. case WASM_OP_REF_NULL:
  6578. {
  6579. uint8 ref_type;
  6580. CHECK_BUF(p, p_end, 1);
  6581. ref_type = read_uint8(p);
  6582. if (ref_type != VALUE_TYPE_FUNCREF
  6583. && ref_type != VALUE_TYPE_EXTERNREF) {
  6584. set_error_buf(error_buf, error_buf_size,
  6585. "unknown value type");
  6586. goto fail;
  6587. }
  6588. #if WASM_ENABLE_FAST_INTERP != 0
  6589. PUSH_OFFSET_TYPE(ref_type);
  6590. #endif
  6591. PUSH_TYPE(ref_type);
  6592. break;
  6593. }
  6594. case WASM_OP_REF_IS_NULL:
  6595. {
  6596. #if WASM_ENABLE_FAST_INTERP != 0
  6597. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6598. int32 block_stack_cell_num =
  6599. (int32)(loader_ctx->stack_cell_num
  6600. - cur_block->stack_cell_num);
  6601. if (block_stack_cell_num <= 0) {
  6602. if (!cur_block->is_stack_polymorphic) {
  6603. set_error_buf(
  6604. error_buf, error_buf_size,
  6605. "type mismatch: expect data but stack was empty");
  6606. goto fail;
  6607. }
  6608. }
  6609. else {
  6610. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  6611. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  6612. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  6613. if (!wasm_loader_pop_frame_ref_offset(
  6614. loader_ctx, *(loader_ctx->frame_ref - 1),
  6615. error_buf, error_buf_size)) {
  6616. goto fail;
  6617. }
  6618. }
  6619. else {
  6620. set_error_buf(error_buf, error_buf_size,
  6621. "type mismatch");
  6622. goto fail;
  6623. }
  6624. }
  6625. #else
  6626. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  6627. error_buf, error_buf_size)
  6628. && !wasm_loader_pop_frame_ref(loader_ctx,
  6629. VALUE_TYPE_EXTERNREF,
  6630. error_buf, error_buf_size)) {
  6631. goto fail;
  6632. }
  6633. #endif
  6634. PUSH_I32();
  6635. break;
  6636. }
  6637. case WASM_OP_REF_FUNC:
  6638. {
  6639. uint32 func_idx = 0;
  6640. pb_read_leb_uint32(p, p_end, func_idx);
  6641. if (!check_function_index(module, func_idx, error_buf,
  6642. error_buf_size)) {
  6643. goto fail;
  6644. }
  6645. /* Refer to a forward-declared function:
  6646. the function must be an import, exported, or present in
  6647. a table elem segment or global initializer to be used as
  6648. the operand to ref.func */
  6649. if (func_idx >= module->import_function_count) {
  6650. WASMTableSeg *table_seg = module->table_segments;
  6651. bool func_declared = false;
  6652. uint32 j;
  6653. for (i = 0; i < module->global_count; i++) {
  6654. if (module->globals[i].type.val_type
  6655. == VALUE_TYPE_FUNCREF
  6656. && module->globals[i].init_expr.init_expr_type
  6657. == INIT_EXPR_TYPE_FUNCREF_CONST
  6658. && module->globals[i].init_expr.u.unary.v.ref_index
  6659. == func_idx) {
  6660. func_declared = true;
  6661. break;
  6662. }
  6663. }
  6664. if (!func_declared) {
  6665. /* Check whether the function is declared in table segs,
  6666. note that it doesn't matter whether the table seg's
  6667. mode is passive, active or declarative. */
  6668. for (i = 0; i < module->table_seg_count;
  6669. i++, table_seg++) {
  6670. if (table_seg->elem_type == VALUE_TYPE_FUNCREF) {
  6671. for (j = 0; j < table_seg->value_count; j++) {
  6672. if (table_seg->init_values[j]
  6673. .u.unary.v.ref_index
  6674. == func_idx) {
  6675. func_declared = true;
  6676. break;
  6677. }
  6678. }
  6679. }
  6680. }
  6681. }
  6682. if (!func_declared) {
  6683. /* Check whether the function is exported */
  6684. for (i = 0; i < module->export_count; i++) {
  6685. if (module->exports[i].kind == EXPORT_KIND_FUNC
  6686. && module->exports[i].index == func_idx) {
  6687. func_declared = true;
  6688. break;
  6689. }
  6690. }
  6691. }
  6692. bh_assert(func_declared);
  6693. (void)func_declared;
  6694. }
  6695. #if WASM_ENABLE_FAST_INTERP != 0
  6696. emit_uint32(loader_ctx, func_idx);
  6697. #endif
  6698. PUSH_FUNCREF();
  6699. break;
  6700. }
  6701. #endif /* WASM_ENABLE_REF_TYPES */
  6702. case WASM_OP_GET_LOCAL:
  6703. {
  6704. p_org = p - 1;
  6705. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6706. PUSH_TYPE(local_type);
  6707. #if WASM_ENABLE_FAST_INTERP != 0
  6708. /* Get Local is optimized out */
  6709. skip_label();
  6710. disable_emit = true;
  6711. operand_offset = local_offset;
  6712. PUSH_OFFSET_TYPE(local_type);
  6713. #else
  6714. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6715. && (WASM_ENABLE_FAST_JIT == 0)
  6716. if (local_offset < 0x80) {
  6717. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  6718. if (is_32bit_type(local_type))
  6719. *p_org++ = (uint8)local_offset;
  6720. else
  6721. *p_org++ = (uint8)(local_offset | 0x80);
  6722. while (p_org < p)
  6723. *p_org++ = WASM_OP_NOP;
  6724. }
  6725. #endif
  6726. #endif
  6727. break;
  6728. }
  6729. case WASM_OP_SET_LOCAL:
  6730. {
  6731. p_org = p - 1;
  6732. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6733. #if WASM_ENABLE_FAST_INTERP != 0
  6734. if (!(preserve_referenced_local(
  6735. loader_ctx, opcode, local_offset, local_type,
  6736. &preserve_local, error_buf, error_buf_size)))
  6737. goto fail;
  6738. if (local_offset < 256) {
  6739. skip_label();
  6740. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  6741. if (loader_ctx->p_code_compiled)
  6742. STORE_U16(loader_ctx->p_code_compiled - 2,
  6743. local_offset);
  6744. loader_ctx->frame_offset--;
  6745. loader_ctx->dynamic_offset--;
  6746. }
  6747. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  6748. if (loader_ctx->p_code_compiled)
  6749. STORE_U16(loader_ctx->p_code_compiled - 2,
  6750. local_offset);
  6751. loader_ctx->frame_offset -= 2;
  6752. loader_ctx->dynamic_offset -= 2;
  6753. }
  6754. else {
  6755. if (is_32bit_type(local_type)) {
  6756. emit_label(EXT_OP_SET_LOCAL_FAST);
  6757. emit_byte(loader_ctx, (uint8)local_offset);
  6758. }
  6759. else {
  6760. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  6761. emit_byte(loader_ctx, (uint8)local_offset);
  6762. }
  6763. POP_OFFSET_TYPE(local_type);
  6764. }
  6765. }
  6766. else { /* local index larger than 255, reserve leb */
  6767. emit_uint32(loader_ctx, local_idx);
  6768. POP_OFFSET_TYPE(local_type);
  6769. }
  6770. #else
  6771. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6772. && (WASM_ENABLE_FAST_JIT == 0)
  6773. if (local_offset < 0x80) {
  6774. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  6775. if (is_32bit_type(local_type))
  6776. *p_org++ = (uint8)local_offset;
  6777. else
  6778. *p_org++ = (uint8)(local_offset | 0x80);
  6779. while (p_org < p)
  6780. *p_org++ = WASM_OP_NOP;
  6781. }
  6782. #endif
  6783. #endif
  6784. POP_TYPE(local_type);
  6785. break;
  6786. }
  6787. case WASM_OP_TEE_LOCAL:
  6788. {
  6789. p_org = p - 1;
  6790. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6791. #if WASM_ENABLE_FAST_INTERP != 0
  6792. /* If the stack is in polymorphic state, do fake pop and push on
  6793. offset stack to keep the depth of offset stack to be the
  6794. same with ref stack */
  6795. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6796. if (cur_block->is_stack_polymorphic) {
  6797. POP_OFFSET_TYPE(local_type);
  6798. PUSH_OFFSET_TYPE(local_type);
  6799. }
  6800. #endif
  6801. POP_TYPE(local_type);
  6802. PUSH_TYPE(local_type);
  6803. #if WASM_ENABLE_FAST_INTERP != 0
  6804. if (!(preserve_referenced_local(
  6805. loader_ctx, opcode, local_offset, local_type,
  6806. &preserve_local, error_buf, error_buf_size)))
  6807. goto fail;
  6808. if (local_offset < 256) {
  6809. skip_label();
  6810. if (is_32bit_type(local_type)) {
  6811. emit_label(EXT_OP_TEE_LOCAL_FAST);
  6812. emit_byte(loader_ctx, (uint8)local_offset);
  6813. }
  6814. else {
  6815. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  6816. emit_byte(loader_ctx, (uint8)local_offset);
  6817. }
  6818. }
  6819. else { /* local index larger than 255, reserve leb */
  6820. emit_uint32(loader_ctx, local_idx);
  6821. }
  6822. emit_operand(loader_ctx,
  6823. *(loader_ctx->frame_offset
  6824. - wasm_value_type_cell_num(local_type)));
  6825. #else
  6826. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6827. && (WASM_ENABLE_FAST_JIT == 0)
  6828. if (local_offset < 0x80) {
  6829. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  6830. if (is_32bit_type(local_type))
  6831. *p_org++ = (uint8)local_offset;
  6832. else
  6833. *p_org++ = (uint8)(local_offset | 0x80);
  6834. while (p_org < p)
  6835. *p_org++ = WASM_OP_NOP;
  6836. }
  6837. #endif
  6838. #endif
  6839. break;
  6840. }
  6841. case WASM_OP_GET_GLOBAL:
  6842. {
  6843. p_org = p - 1;
  6844. pb_read_leb_uint32(p, p_end, global_idx);
  6845. bh_assert(global_idx < global_count);
  6846. global_type = global_idx < module->import_global_count
  6847. ? module->import_globals[global_idx]
  6848. .u.global.type.val_type
  6849. : module
  6850. ->globals[global_idx
  6851. - module->import_global_count]
  6852. .type.val_type;
  6853. PUSH_TYPE(global_type);
  6854. #if WASM_ENABLE_FAST_INTERP == 0
  6855. if (global_type == VALUE_TYPE_I64
  6856. || global_type == VALUE_TYPE_F64) {
  6857. *p_org = WASM_OP_GET_GLOBAL_64;
  6858. }
  6859. #else /* else of WASM_ENABLE_FAST_INTERP */
  6860. if (is_64bit_type(global_type)) {
  6861. skip_label();
  6862. emit_label(WASM_OP_GET_GLOBAL_64);
  6863. }
  6864. emit_uint32(loader_ctx, global_idx);
  6865. PUSH_OFFSET_TYPE(global_type);
  6866. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6867. break;
  6868. }
  6869. case WASM_OP_SET_GLOBAL:
  6870. {
  6871. bool is_mutable = false;
  6872. p_org = p - 1;
  6873. pb_read_leb_uint32(p, p_end, global_idx);
  6874. bh_assert(global_idx < global_count);
  6875. is_mutable = global_idx < module->import_global_count
  6876. ? module->import_globals[global_idx]
  6877. .u.global.type.is_mutable
  6878. : module
  6879. ->globals[global_idx
  6880. - module->import_global_count]
  6881. .type.is_mutable;
  6882. bh_assert(is_mutable);
  6883. global_type = global_idx < module->import_global_count
  6884. ? module->import_globals[global_idx]
  6885. .u.global.type.val_type
  6886. : module
  6887. ->globals[global_idx
  6888. - module->import_global_count]
  6889. .type.val_type;
  6890. #if WASM_ENABLE_FAST_INTERP == 0
  6891. if (is_64bit_type(global_type)) {
  6892. *p_org = WASM_OP_SET_GLOBAL_64;
  6893. }
  6894. else if (module->aux_stack_size > 0
  6895. && global_idx == module->aux_stack_top_global_index) {
  6896. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  6897. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6898. func->has_op_set_global_aux_stack = true;
  6899. #endif
  6900. }
  6901. #else /* else of WASM_ENABLE_FAST_INTERP */
  6902. if (is_64bit_type(global_type)) {
  6903. skip_label();
  6904. emit_label(WASM_OP_SET_GLOBAL_64);
  6905. }
  6906. else if (module->aux_stack_size > 0
  6907. && global_idx == module->aux_stack_top_global_index) {
  6908. skip_label();
  6909. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  6910. }
  6911. emit_uint32(loader_ctx, global_idx);
  6912. POP_OFFSET_TYPE(global_type);
  6913. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6914. POP_TYPE(global_type);
  6915. (void)is_mutable;
  6916. break;
  6917. }
  6918. /* load */
  6919. case WASM_OP_I32_LOAD:
  6920. case WASM_OP_I32_LOAD8_S:
  6921. case WASM_OP_I32_LOAD8_U:
  6922. case WASM_OP_I32_LOAD16_S:
  6923. case WASM_OP_I32_LOAD16_U:
  6924. case WASM_OP_I64_LOAD:
  6925. case WASM_OP_I64_LOAD8_S:
  6926. case WASM_OP_I64_LOAD8_U:
  6927. case WASM_OP_I64_LOAD16_S:
  6928. case WASM_OP_I64_LOAD16_U:
  6929. case WASM_OP_I64_LOAD32_S:
  6930. case WASM_OP_I64_LOAD32_U:
  6931. case WASM_OP_F32_LOAD:
  6932. case WASM_OP_F64_LOAD:
  6933. /* store */
  6934. case WASM_OP_I32_STORE:
  6935. case WASM_OP_I32_STORE8:
  6936. case WASM_OP_I32_STORE16:
  6937. case WASM_OP_I64_STORE:
  6938. case WASM_OP_I64_STORE8:
  6939. case WASM_OP_I64_STORE16:
  6940. case WASM_OP_I64_STORE32:
  6941. case WASM_OP_F32_STORE:
  6942. case WASM_OP_F64_STORE:
  6943. {
  6944. #if WASM_ENABLE_FAST_INTERP != 0
  6945. /* change F32/F64 into I32/I64 */
  6946. if (opcode == WASM_OP_F32_LOAD) {
  6947. skip_label();
  6948. emit_label(WASM_OP_I32_LOAD);
  6949. }
  6950. else if (opcode == WASM_OP_F64_LOAD) {
  6951. skip_label();
  6952. emit_label(WASM_OP_I64_LOAD);
  6953. }
  6954. else if (opcode == WASM_OP_F32_STORE) {
  6955. skip_label();
  6956. emit_label(WASM_OP_I32_STORE);
  6957. }
  6958. else if (opcode == WASM_OP_F64_STORE) {
  6959. skip_label();
  6960. emit_label(WASM_OP_I64_STORE);
  6961. }
  6962. #endif
  6963. CHECK_MEMORY();
  6964. pb_read_leb_memarg(p, p_end, align); /* align */
  6965. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  6966. #if WASM_ENABLE_FAST_INTERP != 0
  6967. emit_uint32(loader_ctx, mem_offset);
  6968. #endif
  6969. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6970. func->has_memory_operations = true;
  6971. #endif
  6972. switch (opcode) {
  6973. /* load */
  6974. case WASM_OP_I32_LOAD:
  6975. case WASM_OP_I32_LOAD8_S:
  6976. case WASM_OP_I32_LOAD8_U:
  6977. case WASM_OP_I32_LOAD16_S:
  6978. case WASM_OP_I32_LOAD16_U:
  6979. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  6980. break;
  6981. case WASM_OP_I64_LOAD:
  6982. case WASM_OP_I64_LOAD8_S:
  6983. case WASM_OP_I64_LOAD8_U:
  6984. case WASM_OP_I64_LOAD16_S:
  6985. case WASM_OP_I64_LOAD16_U:
  6986. case WASM_OP_I64_LOAD32_S:
  6987. case WASM_OP_I64_LOAD32_U:
  6988. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  6989. break;
  6990. case WASM_OP_F32_LOAD:
  6991. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  6992. break;
  6993. case WASM_OP_F64_LOAD:
  6994. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  6995. break;
  6996. /* store */
  6997. case WASM_OP_I32_STORE:
  6998. case WASM_OP_I32_STORE8:
  6999. case WASM_OP_I32_STORE16:
  7000. POP_I32();
  7001. POP_MEM_OFFSET();
  7002. break;
  7003. case WASM_OP_I64_STORE:
  7004. case WASM_OP_I64_STORE8:
  7005. case WASM_OP_I64_STORE16:
  7006. case WASM_OP_I64_STORE32:
  7007. POP_I64();
  7008. POP_MEM_OFFSET();
  7009. break;
  7010. case WASM_OP_F32_STORE:
  7011. POP_F32();
  7012. POP_MEM_OFFSET();
  7013. break;
  7014. case WASM_OP_F64_STORE:
  7015. POP_F64();
  7016. POP_MEM_OFFSET();
  7017. break;
  7018. default:
  7019. break;
  7020. }
  7021. break;
  7022. }
  7023. case WASM_OP_MEMORY_SIZE:
  7024. CHECK_MEMORY();
  7025. pb_read_leb_memidx(p, p_end, memidx);
  7026. check_memidx(module, memidx);
  7027. PUSH_PAGE_COUNT();
  7028. module->possible_memory_grow = true;
  7029. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7030. func->has_memory_operations = true;
  7031. #endif
  7032. break;
  7033. case WASM_OP_MEMORY_GROW:
  7034. CHECK_MEMORY();
  7035. pb_read_leb_memidx(p, p_end, memidx);
  7036. check_memidx(module, memidx);
  7037. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  7038. module->possible_memory_grow = true;
  7039. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  7040. || WASM_ENABLE_WAMR_COMPILER != 0
  7041. func->has_op_memory_grow = true;
  7042. #endif
  7043. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7044. func->has_memory_operations = true;
  7045. #endif
  7046. break;
  7047. case WASM_OP_I32_CONST:
  7048. pb_read_leb_int32(p, p_end, i32_const);
  7049. #if WASM_ENABLE_FAST_INTERP != 0
  7050. skip_label();
  7051. disable_emit = true;
  7052. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  7053. if (operand_offset == 0) {
  7054. disable_emit = false;
  7055. emit_label(WASM_OP_I32_CONST);
  7056. emit_uint32(loader_ctx, i32_const);
  7057. }
  7058. #else
  7059. (void)i32_const;
  7060. #endif
  7061. PUSH_I32();
  7062. break;
  7063. case WASM_OP_I64_CONST:
  7064. pb_read_leb_int64(p, p_end, i64_const);
  7065. #if WASM_ENABLE_FAST_INTERP != 0
  7066. skip_label();
  7067. disable_emit = true;
  7068. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  7069. if (operand_offset == 0) {
  7070. disable_emit = false;
  7071. emit_label(WASM_OP_I64_CONST);
  7072. emit_uint64(loader_ctx, i64_const);
  7073. }
  7074. #endif
  7075. PUSH_I64();
  7076. break;
  7077. case WASM_OP_F32_CONST:
  7078. CHECK_BUF(p, p_end, sizeof(float32));
  7079. p += sizeof(float32);
  7080. #if WASM_ENABLE_FAST_INTERP != 0
  7081. skip_label();
  7082. disable_emit = true;
  7083. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  7084. sizeof(float32));
  7085. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  7086. if (operand_offset == 0) {
  7087. disable_emit = false;
  7088. emit_label(WASM_OP_F32_CONST);
  7089. emit_float32(loader_ctx, f32_const);
  7090. }
  7091. #endif
  7092. PUSH_F32();
  7093. break;
  7094. case WASM_OP_F64_CONST:
  7095. CHECK_BUF(p, p_end, sizeof(float64));
  7096. p += sizeof(float64);
  7097. #if WASM_ENABLE_FAST_INTERP != 0
  7098. skip_label();
  7099. disable_emit = true;
  7100. /* Some MCU may require 8-byte align */
  7101. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  7102. sizeof(float64));
  7103. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  7104. if (operand_offset == 0) {
  7105. disable_emit = false;
  7106. emit_label(WASM_OP_F64_CONST);
  7107. emit_float64(loader_ctx, f64_const);
  7108. }
  7109. #endif
  7110. PUSH_F64();
  7111. break;
  7112. case WASM_OP_I32_EQZ:
  7113. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7114. break;
  7115. case WASM_OP_I32_EQ:
  7116. case WASM_OP_I32_NE:
  7117. case WASM_OP_I32_LT_S:
  7118. case WASM_OP_I32_LT_U:
  7119. case WASM_OP_I32_GT_S:
  7120. case WASM_OP_I32_GT_U:
  7121. case WASM_OP_I32_LE_S:
  7122. case WASM_OP_I32_LE_U:
  7123. case WASM_OP_I32_GE_S:
  7124. case WASM_OP_I32_GE_U:
  7125. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7126. break;
  7127. case WASM_OP_I64_EQZ:
  7128. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7129. break;
  7130. case WASM_OP_I64_EQ:
  7131. case WASM_OP_I64_NE:
  7132. case WASM_OP_I64_LT_S:
  7133. case WASM_OP_I64_LT_U:
  7134. case WASM_OP_I64_GT_S:
  7135. case WASM_OP_I64_GT_U:
  7136. case WASM_OP_I64_LE_S:
  7137. case WASM_OP_I64_LE_U:
  7138. case WASM_OP_I64_GE_S:
  7139. case WASM_OP_I64_GE_U:
  7140. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7141. break;
  7142. case WASM_OP_F32_EQ:
  7143. case WASM_OP_F32_NE:
  7144. case WASM_OP_F32_LT:
  7145. case WASM_OP_F32_GT:
  7146. case WASM_OP_F32_LE:
  7147. case WASM_OP_F32_GE:
  7148. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7149. break;
  7150. case WASM_OP_F64_EQ:
  7151. case WASM_OP_F64_NE:
  7152. case WASM_OP_F64_LT:
  7153. case WASM_OP_F64_GT:
  7154. case WASM_OP_F64_LE:
  7155. case WASM_OP_F64_GE:
  7156. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7157. break;
  7158. case WASM_OP_I32_CLZ:
  7159. case WASM_OP_I32_CTZ:
  7160. case WASM_OP_I32_POPCNT:
  7161. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7162. break;
  7163. case WASM_OP_I32_ADD:
  7164. case WASM_OP_I32_SUB:
  7165. case WASM_OP_I32_MUL:
  7166. case WASM_OP_I32_DIV_S:
  7167. case WASM_OP_I32_DIV_U:
  7168. case WASM_OP_I32_REM_S:
  7169. case WASM_OP_I32_REM_U:
  7170. case WASM_OP_I32_AND:
  7171. case WASM_OP_I32_OR:
  7172. case WASM_OP_I32_XOR:
  7173. case WASM_OP_I32_SHL:
  7174. case WASM_OP_I32_SHR_S:
  7175. case WASM_OP_I32_SHR_U:
  7176. case WASM_OP_I32_ROTL:
  7177. case WASM_OP_I32_ROTR:
  7178. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7179. break;
  7180. case WASM_OP_I64_CLZ:
  7181. case WASM_OP_I64_CTZ:
  7182. case WASM_OP_I64_POPCNT:
  7183. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7184. break;
  7185. case WASM_OP_I64_ADD:
  7186. case WASM_OP_I64_SUB:
  7187. case WASM_OP_I64_MUL:
  7188. case WASM_OP_I64_DIV_S:
  7189. case WASM_OP_I64_DIV_U:
  7190. case WASM_OP_I64_REM_S:
  7191. case WASM_OP_I64_REM_U:
  7192. case WASM_OP_I64_AND:
  7193. case WASM_OP_I64_OR:
  7194. case WASM_OP_I64_XOR:
  7195. case WASM_OP_I64_SHL:
  7196. case WASM_OP_I64_SHR_S:
  7197. case WASM_OP_I64_SHR_U:
  7198. case WASM_OP_I64_ROTL:
  7199. case WASM_OP_I64_ROTR:
  7200. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7201. break;
  7202. case WASM_OP_F32_ABS:
  7203. case WASM_OP_F32_NEG:
  7204. case WASM_OP_F32_CEIL:
  7205. case WASM_OP_F32_FLOOR:
  7206. case WASM_OP_F32_TRUNC:
  7207. case WASM_OP_F32_NEAREST:
  7208. case WASM_OP_F32_SQRT:
  7209. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7210. break;
  7211. case WASM_OP_F32_ADD:
  7212. case WASM_OP_F32_SUB:
  7213. case WASM_OP_F32_MUL:
  7214. case WASM_OP_F32_DIV:
  7215. case WASM_OP_F32_MIN:
  7216. case WASM_OP_F32_MAX:
  7217. case WASM_OP_F32_COPYSIGN:
  7218. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7219. break;
  7220. case WASM_OP_F64_ABS:
  7221. case WASM_OP_F64_NEG:
  7222. case WASM_OP_F64_CEIL:
  7223. case WASM_OP_F64_FLOOR:
  7224. case WASM_OP_F64_TRUNC:
  7225. case WASM_OP_F64_NEAREST:
  7226. case WASM_OP_F64_SQRT:
  7227. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7228. break;
  7229. case WASM_OP_F64_ADD:
  7230. case WASM_OP_F64_SUB:
  7231. case WASM_OP_F64_MUL:
  7232. case WASM_OP_F64_DIV:
  7233. case WASM_OP_F64_MIN:
  7234. case WASM_OP_F64_MAX:
  7235. case WASM_OP_F64_COPYSIGN:
  7236. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7237. break;
  7238. case WASM_OP_I32_WRAP_I64:
  7239. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7240. break;
  7241. case WASM_OP_I32_TRUNC_S_F32:
  7242. case WASM_OP_I32_TRUNC_U_F32:
  7243. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7244. break;
  7245. case WASM_OP_I32_TRUNC_S_F64:
  7246. case WASM_OP_I32_TRUNC_U_F64:
  7247. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7248. break;
  7249. case WASM_OP_I64_EXTEND_S_I32:
  7250. case WASM_OP_I64_EXTEND_U_I32:
  7251. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7252. break;
  7253. case WASM_OP_I64_TRUNC_S_F32:
  7254. case WASM_OP_I64_TRUNC_U_F32:
  7255. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7256. break;
  7257. case WASM_OP_I64_TRUNC_S_F64:
  7258. case WASM_OP_I64_TRUNC_U_F64:
  7259. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7260. break;
  7261. case WASM_OP_F32_CONVERT_S_I32:
  7262. case WASM_OP_F32_CONVERT_U_I32:
  7263. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7264. break;
  7265. case WASM_OP_F32_CONVERT_S_I64:
  7266. case WASM_OP_F32_CONVERT_U_I64:
  7267. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  7268. break;
  7269. case WASM_OP_F32_DEMOTE_F64:
  7270. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  7271. break;
  7272. case WASM_OP_F64_CONVERT_S_I32:
  7273. case WASM_OP_F64_CONVERT_U_I32:
  7274. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7275. break;
  7276. case WASM_OP_F64_CONVERT_S_I64:
  7277. case WASM_OP_F64_CONVERT_U_I64:
  7278. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  7279. break;
  7280. case WASM_OP_F64_PROMOTE_F32:
  7281. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  7282. break;
  7283. case WASM_OP_I32_REINTERPRET_F32:
  7284. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7285. break;
  7286. case WASM_OP_I64_REINTERPRET_F64:
  7287. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7288. break;
  7289. case WASM_OP_F32_REINTERPRET_I32:
  7290. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7291. break;
  7292. case WASM_OP_F64_REINTERPRET_I64:
  7293. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  7294. break;
  7295. case WASM_OP_I32_EXTEND8_S:
  7296. case WASM_OP_I32_EXTEND16_S:
  7297. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7298. break;
  7299. case WASM_OP_I64_EXTEND8_S:
  7300. case WASM_OP_I64_EXTEND16_S:
  7301. case WASM_OP_I64_EXTEND32_S:
  7302. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7303. break;
  7304. case WASM_OP_MISC_PREFIX:
  7305. {
  7306. uint32 opcode1;
  7307. pb_read_leb_uint32(p, p_end, opcode1);
  7308. #if WASM_ENABLE_FAST_INTERP != 0
  7309. emit_byte(loader_ctx, ((uint8)opcode1));
  7310. #endif
  7311. switch (opcode1) {
  7312. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7313. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7314. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7315. break;
  7316. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7317. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7318. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7319. break;
  7320. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7321. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7322. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7323. break;
  7324. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7325. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7326. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7327. break;
  7328. #if WASM_ENABLE_BULK_MEMORY != 0
  7329. case WASM_OP_MEMORY_INIT:
  7330. {
  7331. CHECK_MEMORY();
  7332. pb_read_leb_uint32(p, p_end, segment_index);
  7333. #if WASM_ENABLE_FAST_INTERP != 0
  7334. emit_uint32(loader_ctx, segment_index);
  7335. #endif
  7336. pb_read_leb_memidx(p, p_end, memidx);
  7337. check_memidx(module, memidx);
  7338. bh_assert(segment_index < module->data_seg_count);
  7339. bh_assert(module->data_seg_count1 > 0);
  7340. POP_I32();
  7341. POP_I32();
  7342. POP_MEM_OFFSET();
  7343. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7344. func->has_memory_operations = true;
  7345. #endif
  7346. break;
  7347. }
  7348. case WASM_OP_DATA_DROP:
  7349. {
  7350. pb_read_leb_uint32(p, p_end, segment_index);
  7351. #if WASM_ENABLE_FAST_INTERP != 0
  7352. emit_uint32(loader_ctx, segment_index);
  7353. #endif
  7354. bh_assert(segment_index < module->data_seg_count);
  7355. bh_assert(module->data_seg_count1 > 0);
  7356. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7357. func->has_memory_operations = true;
  7358. #endif
  7359. break;
  7360. }
  7361. #endif /* WASM_ENABLE_BULK_MEMORY */
  7362. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  7363. case WASM_OP_MEMORY_COPY:
  7364. {
  7365. CHECK_MEMORY();
  7366. CHECK_BUF(p, p_end, sizeof(int16));
  7367. /* check both src and dst memory index */
  7368. pb_read_leb_memidx(p, p_end, memidx);
  7369. check_memidx(module, memidx);
  7370. pb_read_leb_memidx(p, p_end, memidx);
  7371. check_memidx(module, memidx);
  7372. POP_MEM_OFFSET();
  7373. POP_MEM_OFFSET();
  7374. POP_MEM_OFFSET();
  7375. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7376. func->has_memory_operations = true;
  7377. #endif
  7378. break;
  7379. }
  7380. case WASM_OP_MEMORY_FILL:
  7381. {
  7382. CHECK_MEMORY();
  7383. pb_read_leb_memidx(p, p_end, memidx);
  7384. check_memidx(module, memidx);
  7385. POP_MEM_OFFSET();
  7386. POP_I32();
  7387. POP_MEM_OFFSET();
  7388. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7389. func->has_memory_operations = true;
  7390. #endif
  7391. break;
  7392. }
  7393. #endif /* WASM_ENABLE_BULK_MEMORY_OPT */
  7394. #if WASM_ENABLE_REF_TYPES != 0
  7395. case WASM_OP_TABLE_INIT:
  7396. {
  7397. uint8 seg_ref_type, tbl_ref_type;
  7398. uint32 table_seg_idx, table_idx;
  7399. pb_read_leb_uint32(p, p_end, table_seg_idx);
  7400. pb_read_leb_uint32(p, p_end, table_idx);
  7401. if (!get_table_elem_type(module, table_idx,
  7402. &tbl_ref_type, error_buf,
  7403. error_buf_size))
  7404. goto fail;
  7405. if (!get_table_seg_elem_type(module, table_seg_idx,
  7406. &seg_ref_type, error_buf,
  7407. error_buf_size))
  7408. goto fail;
  7409. if (seg_ref_type != tbl_ref_type) {
  7410. set_error_buf(error_buf, error_buf_size,
  7411. "type mismatch");
  7412. goto fail;
  7413. }
  7414. #if WASM_ENABLE_FAST_INTERP != 0
  7415. emit_uint32(loader_ctx, table_seg_idx);
  7416. emit_uint32(loader_ctx, table_idx);
  7417. #endif
  7418. POP_I32();
  7419. POP_I32();
  7420. #if WASM_ENABLE_MEMORY64 != 0
  7421. table_elem_idx_type = is_table_64bit(module, table_idx)
  7422. ? VALUE_TYPE_I64
  7423. : VALUE_TYPE_I32;
  7424. #endif
  7425. POP_TBL_ELEM_IDX();
  7426. break;
  7427. }
  7428. case WASM_OP_ELEM_DROP:
  7429. {
  7430. uint32 table_seg_idx;
  7431. pb_read_leb_uint32(p, p_end, table_seg_idx);
  7432. if (!get_table_seg_elem_type(module, table_seg_idx,
  7433. NULL, error_buf,
  7434. error_buf_size))
  7435. goto fail;
  7436. #if WASM_ENABLE_FAST_INTERP != 0
  7437. emit_uint32(loader_ctx, table_seg_idx);
  7438. #endif
  7439. break;
  7440. }
  7441. case WASM_OP_TABLE_COPY:
  7442. {
  7443. uint8 src_ref_type, dst_ref_type;
  7444. uint32 src_tbl_idx, dst_tbl_idx, src_tbl_idx_type,
  7445. dst_tbl_idx_type, min_tbl_idx_type;
  7446. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  7447. if (!get_table_elem_type(module, src_tbl_idx,
  7448. &src_ref_type, error_buf,
  7449. error_buf_size))
  7450. goto fail;
  7451. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  7452. if (!get_table_elem_type(module, dst_tbl_idx,
  7453. &dst_ref_type, error_buf,
  7454. error_buf_size))
  7455. goto fail;
  7456. if (src_ref_type != dst_ref_type) {
  7457. set_error_buf(error_buf, error_buf_size,
  7458. "type mismatch");
  7459. goto fail;
  7460. }
  7461. #if WASM_ENABLE_FAST_INTERP != 0
  7462. emit_uint32(loader_ctx, src_tbl_idx);
  7463. emit_uint32(loader_ctx, dst_tbl_idx);
  7464. #endif
  7465. #if WASM_ENABLE_MEMORY64 != 0
  7466. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  7467. ? VALUE_TYPE_I64
  7468. : VALUE_TYPE_I32;
  7469. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  7470. ? VALUE_TYPE_I64
  7471. : VALUE_TYPE_I32;
  7472. min_tbl_idx_type =
  7473. (src_tbl_idx_type == VALUE_TYPE_I32
  7474. || dst_tbl_idx_type == VALUE_TYPE_I32)
  7475. ? VALUE_TYPE_I32
  7476. : VALUE_TYPE_I64;
  7477. #else
  7478. src_tbl_idx_type = VALUE_TYPE_I32;
  7479. dst_tbl_idx_type = VALUE_TYPE_I32;
  7480. min_tbl_idx_type = VALUE_TYPE_I32;
  7481. #endif
  7482. table_elem_idx_type = min_tbl_idx_type;
  7483. POP_TBL_ELEM_IDX();
  7484. table_elem_idx_type = src_tbl_idx_type;
  7485. POP_TBL_ELEM_IDX();
  7486. table_elem_idx_type = dst_tbl_idx_type;
  7487. POP_TBL_ELEM_IDX();
  7488. break;
  7489. }
  7490. case WASM_OP_TABLE_SIZE:
  7491. {
  7492. uint32 table_idx;
  7493. pb_read_leb_uint32(p, p_end, table_idx);
  7494. /* TODO: shall we create a new function to check
  7495. table idx instead of using below function? */
  7496. if (!get_table_elem_type(module, table_idx, NULL,
  7497. error_buf, error_buf_size))
  7498. goto fail;
  7499. #if WASM_ENABLE_FAST_INTERP != 0
  7500. emit_uint32(loader_ctx, table_idx);
  7501. #endif
  7502. #if WASM_ENABLE_MEMORY64 != 0
  7503. table_elem_idx_type = is_table_64bit(module, table_idx)
  7504. ? VALUE_TYPE_I64
  7505. : VALUE_TYPE_I32;
  7506. #endif
  7507. PUSH_TBL_ELEM_IDX();
  7508. break;
  7509. }
  7510. case WASM_OP_TABLE_GROW:
  7511. case WASM_OP_TABLE_FILL:
  7512. {
  7513. uint8 decl_ref_type;
  7514. uint32 table_idx;
  7515. pb_read_leb_uint32(p, p_end, table_idx);
  7516. if (!get_table_elem_type(module, table_idx,
  7517. &decl_ref_type, error_buf,
  7518. error_buf_size))
  7519. goto fail;
  7520. if (opcode1 == WASM_OP_TABLE_GROW) {
  7521. if (table_idx < module->import_table_count) {
  7522. module->import_tables[table_idx]
  7523. .u.table.table_type.possible_grow = true;
  7524. }
  7525. else {
  7526. module
  7527. ->tables[table_idx
  7528. - module->import_table_count]
  7529. .table_type.possible_grow = true;
  7530. }
  7531. }
  7532. #if WASM_ENABLE_FAST_INTERP != 0
  7533. emit_uint32(loader_ctx, table_idx);
  7534. #endif
  7535. #if WASM_ENABLE_MEMORY64 != 0
  7536. table_elem_idx_type = is_table_64bit(module, table_idx)
  7537. ? VALUE_TYPE_I64
  7538. : VALUE_TYPE_I32;
  7539. #endif
  7540. POP_TBL_ELEM_IDX();
  7541. #if WASM_ENABLE_FAST_INTERP != 0
  7542. POP_OFFSET_TYPE(decl_ref_type);
  7543. #endif
  7544. POP_TYPE(decl_ref_type);
  7545. if (opcode1 == WASM_OP_TABLE_GROW)
  7546. PUSH_TBL_ELEM_IDX();
  7547. else
  7548. PUSH_TBL_ELEM_IDX();
  7549. break;
  7550. }
  7551. #endif /* WASM_ENABLE_REF_TYPES */
  7552. default:
  7553. bh_assert(0);
  7554. break;
  7555. }
  7556. break;
  7557. }
  7558. #if WASM_ENABLE_SHARED_MEMORY != 0
  7559. case WASM_OP_ATOMIC_PREFIX:
  7560. {
  7561. uint32 opcode1;
  7562. pb_read_leb_uint32(p, p_end, opcode1);
  7563. #if WASM_ENABLE_FAST_INTERP != 0
  7564. emit_byte(loader_ctx, opcode1);
  7565. #endif
  7566. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  7567. CHECK_MEMORY();
  7568. pb_read_leb_uint32(p, p_end, align); /* align */
  7569. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  7570. #if WASM_ENABLE_FAST_INTERP != 0
  7571. emit_uint32(loader_ctx, mem_offset);
  7572. #endif
  7573. }
  7574. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7575. func->has_memory_operations = true;
  7576. #endif
  7577. switch (opcode1) {
  7578. case WASM_OP_ATOMIC_NOTIFY:
  7579. POP_I32();
  7580. POP_MEM_OFFSET();
  7581. PUSH_I32();
  7582. break;
  7583. case WASM_OP_ATOMIC_WAIT32:
  7584. POP_I64();
  7585. POP_I32();
  7586. POP_MEM_OFFSET();
  7587. PUSH_I32();
  7588. break;
  7589. case WASM_OP_ATOMIC_WAIT64:
  7590. POP_I64();
  7591. POP_I64();
  7592. POP_MEM_OFFSET();
  7593. PUSH_I32();
  7594. break;
  7595. case WASM_OP_ATOMIC_FENCE:
  7596. /* reserved byte 0x00 */
  7597. bh_assert(*p == 0x00);
  7598. p++;
  7599. break;
  7600. case WASM_OP_ATOMIC_I32_LOAD:
  7601. case WASM_OP_ATOMIC_I32_LOAD8_U:
  7602. case WASM_OP_ATOMIC_I32_LOAD16_U:
  7603. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  7604. break;
  7605. case WASM_OP_ATOMIC_I32_STORE:
  7606. case WASM_OP_ATOMIC_I32_STORE8:
  7607. case WASM_OP_ATOMIC_I32_STORE16:
  7608. POP_I32();
  7609. POP_MEM_OFFSET();
  7610. break;
  7611. case WASM_OP_ATOMIC_I64_LOAD:
  7612. case WASM_OP_ATOMIC_I64_LOAD8_U:
  7613. case WASM_OP_ATOMIC_I64_LOAD16_U:
  7614. case WASM_OP_ATOMIC_I64_LOAD32_U:
  7615. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  7616. break;
  7617. case WASM_OP_ATOMIC_I64_STORE:
  7618. case WASM_OP_ATOMIC_I64_STORE8:
  7619. case WASM_OP_ATOMIC_I64_STORE16:
  7620. case WASM_OP_ATOMIC_I64_STORE32:
  7621. POP_I64();
  7622. POP_MEM_OFFSET();
  7623. break;
  7624. case WASM_OP_ATOMIC_RMW_I32_ADD:
  7625. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  7626. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  7627. case WASM_OP_ATOMIC_RMW_I32_SUB:
  7628. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  7629. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  7630. case WASM_OP_ATOMIC_RMW_I32_AND:
  7631. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  7632. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  7633. case WASM_OP_ATOMIC_RMW_I32_OR:
  7634. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  7635. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  7636. case WASM_OP_ATOMIC_RMW_I32_XOR:
  7637. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  7638. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  7639. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  7640. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  7641. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  7642. POP_I32();
  7643. POP_MEM_OFFSET();
  7644. PUSH_I32();
  7645. break;
  7646. case WASM_OP_ATOMIC_RMW_I64_ADD:
  7647. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  7648. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  7649. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  7650. case WASM_OP_ATOMIC_RMW_I64_SUB:
  7651. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  7652. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  7653. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  7654. case WASM_OP_ATOMIC_RMW_I64_AND:
  7655. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  7656. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  7657. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  7658. case WASM_OP_ATOMIC_RMW_I64_OR:
  7659. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  7660. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  7661. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  7662. case WASM_OP_ATOMIC_RMW_I64_XOR:
  7663. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  7664. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  7665. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  7666. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  7667. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  7668. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  7669. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  7670. POP_I64();
  7671. POP_MEM_OFFSET();
  7672. PUSH_I64();
  7673. break;
  7674. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  7675. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  7676. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  7677. POP_I32();
  7678. POP_I32();
  7679. POP_MEM_OFFSET();
  7680. PUSH_I32();
  7681. break;
  7682. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  7683. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  7684. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  7685. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  7686. POP_I64();
  7687. POP_I64();
  7688. POP_MEM_OFFSET();
  7689. PUSH_I64();
  7690. break;
  7691. default:
  7692. bh_assert(0);
  7693. break;
  7694. }
  7695. break;
  7696. }
  7697. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  7698. default:
  7699. bh_assert(0);
  7700. break;
  7701. }
  7702. #if WASM_ENABLE_FAST_INTERP != 0
  7703. last_op = opcode;
  7704. #endif
  7705. }
  7706. if (loader_ctx->csp_num > 0) {
  7707. set_error_buf(error_buf, error_buf_size,
  7708. "function body must end with END opcode");
  7709. goto fail;
  7710. }
  7711. #if WASM_ENABLE_FAST_INTERP != 0
  7712. if (loader_ctx->p_code_compiled == NULL)
  7713. goto re_scan;
  7714. func->const_cell_num =
  7715. loader_ctx->i64_const_num * 2 + loader_ctx->i32_const_num;
  7716. if (func->const_cell_num > 0) {
  7717. if (!(func->consts =
  7718. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  7719. error_buf, error_buf_size)))
  7720. goto fail;
  7721. if (loader_ctx->i64_const_num > 0) {
  7722. bh_memcpy_s(func->consts,
  7723. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  7724. loader_ctx->i64_consts,
  7725. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  7726. }
  7727. if (loader_ctx->i32_const_num > 0) {
  7728. bh_memcpy_s(func->consts
  7729. + sizeof(int64) * loader_ctx->i64_const_num,
  7730. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  7731. loader_ctx->i32_consts,
  7732. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  7733. }
  7734. }
  7735. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  7736. - loader_ctx->start_dynamic_offset + 1;
  7737. #else
  7738. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  7739. #endif
  7740. func->max_block_num = loader_ctx->max_csp_num;
  7741. return_value = true;
  7742. fail:
  7743. wasm_loader_ctx_destroy(loader_ctx);
  7744. (void)u8;
  7745. (void)u32;
  7746. (void)i32;
  7747. (void)i64_const;
  7748. (void)global_count;
  7749. (void)local_count;
  7750. (void)local_offset;
  7751. (void)p_org;
  7752. (void)mem_offset;
  7753. (void)align;
  7754. #if WASM_ENABLE_BULK_MEMORY != 0
  7755. (void)segment_index;
  7756. #endif
  7757. return return_value;
  7758. }