wasm_mini_loader.c 294 KB

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