wasm_mini_loader.c 311 KB

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