wasm_mini_loader.c 296 KB

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