wasm_mini_loader.c 294 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113
  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. CHECK_BUF(p, p_end, 1);
  3065. u8 = read_uint8(p); /* 0x00 */
  3066. break;
  3067. #if WASM_ENABLE_EXCE_HANDLING != 0
  3068. case WASM_OP_TRY:
  3069. case WASM_OP_CATCH:
  3070. case WASM_OP_THROW:
  3071. case WASM_OP_RETHROW:
  3072. case WASM_OP_DELEGATE:
  3073. case WASM_OP_CATCH_ALL:
  3074. /* TODO */
  3075. return false;
  3076. #endif
  3077. case WASM_OP_DROP:
  3078. case WASM_OP_SELECT:
  3079. case WASM_OP_DROP_64:
  3080. case WASM_OP_SELECT_64:
  3081. break;
  3082. #if WASM_ENABLE_REF_TYPES != 0
  3083. case WASM_OP_SELECT_T:
  3084. skip_leb_uint32(p, p_end); /* vec length */
  3085. CHECK_BUF(p, p_end, 1);
  3086. u8 = read_uint8(p); /* typeidx */
  3087. break;
  3088. case WASM_OP_TABLE_GET:
  3089. case WASM_OP_TABLE_SET:
  3090. skip_leb_uint32(p, p_end); /* table index */
  3091. break;
  3092. case WASM_OP_REF_NULL:
  3093. CHECK_BUF(p, p_end, 1);
  3094. u8 = read_uint8(p); /* type */
  3095. break;
  3096. case WASM_OP_REF_IS_NULL:
  3097. break;
  3098. case WASM_OP_REF_FUNC:
  3099. skip_leb_uint32(p, p_end); /* func index */
  3100. break;
  3101. #endif /* WASM_ENABLE_REF_TYPES */
  3102. case WASM_OP_GET_LOCAL:
  3103. case WASM_OP_SET_LOCAL:
  3104. case WASM_OP_TEE_LOCAL:
  3105. case WASM_OP_GET_GLOBAL:
  3106. case WASM_OP_SET_GLOBAL:
  3107. case WASM_OP_GET_GLOBAL_64:
  3108. case WASM_OP_SET_GLOBAL_64:
  3109. case WASM_OP_SET_GLOBAL_AUX_STACK:
  3110. skip_leb_uint32(p, p_end); /* localidx */
  3111. break;
  3112. case EXT_OP_GET_LOCAL_FAST:
  3113. case EXT_OP_SET_LOCAL_FAST:
  3114. case EXT_OP_TEE_LOCAL_FAST:
  3115. CHECK_BUF(p, p_end, 1);
  3116. p++;
  3117. break;
  3118. case WASM_OP_I32_LOAD:
  3119. case WASM_OP_I64_LOAD:
  3120. case WASM_OP_F32_LOAD:
  3121. case WASM_OP_F64_LOAD:
  3122. case WASM_OP_I32_LOAD8_S:
  3123. case WASM_OP_I32_LOAD8_U:
  3124. case WASM_OP_I32_LOAD16_S:
  3125. case WASM_OP_I32_LOAD16_U:
  3126. case WASM_OP_I64_LOAD8_S:
  3127. case WASM_OP_I64_LOAD8_U:
  3128. case WASM_OP_I64_LOAD16_S:
  3129. case WASM_OP_I64_LOAD16_U:
  3130. case WASM_OP_I64_LOAD32_S:
  3131. case WASM_OP_I64_LOAD32_U:
  3132. case WASM_OP_I32_STORE:
  3133. case WASM_OP_I64_STORE:
  3134. case WASM_OP_F32_STORE:
  3135. case WASM_OP_F64_STORE:
  3136. case WASM_OP_I32_STORE8:
  3137. case WASM_OP_I32_STORE16:
  3138. case WASM_OP_I64_STORE8:
  3139. case WASM_OP_I64_STORE16:
  3140. case WASM_OP_I64_STORE32:
  3141. skip_leb_uint32(p, p_end); /* align */
  3142. skip_leb_mem_offset(p, p_end); /* offset */
  3143. break;
  3144. case WASM_OP_MEMORY_SIZE:
  3145. case WASM_OP_MEMORY_GROW:
  3146. skip_leb_uint32(p, p_end); /* 0x00 */
  3147. break;
  3148. case WASM_OP_I32_CONST:
  3149. skip_leb_int32(p, p_end);
  3150. break;
  3151. case WASM_OP_I64_CONST:
  3152. skip_leb_int64(p, p_end);
  3153. break;
  3154. case WASM_OP_F32_CONST:
  3155. p += sizeof(float32);
  3156. break;
  3157. case WASM_OP_F64_CONST:
  3158. p += sizeof(float64);
  3159. break;
  3160. case WASM_OP_I32_EQZ:
  3161. case WASM_OP_I32_EQ:
  3162. case WASM_OP_I32_NE:
  3163. case WASM_OP_I32_LT_S:
  3164. case WASM_OP_I32_LT_U:
  3165. case WASM_OP_I32_GT_S:
  3166. case WASM_OP_I32_GT_U:
  3167. case WASM_OP_I32_LE_S:
  3168. case WASM_OP_I32_LE_U:
  3169. case WASM_OP_I32_GE_S:
  3170. case WASM_OP_I32_GE_U:
  3171. case WASM_OP_I64_EQZ:
  3172. case WASM_OP_I64_EQ:
  3173. case WASM_OP_I64_NE:
  3174. case WASM_OP_I64_LT_S:
  3175. case WASM_OP_I64_LT_U:
  3176. case WASM_OP_I64_GT_S:
  3177. case WASM_OP_I64_GT_U:
  3178. case WASM_OP_I64_LE_S:
  3179. case WASM_OP_I64_LE_U:
  3180. case WASM_OP_I64_GE_S:
  3181. case WASM_OP_I64_GE_U:
  3182. case WASM_OP_F32_EQ:
  3183. case WASM_OP_F32_NE:
  3184. case WASM_OP_F32_LT:
  3185. case WASM_OP_F32_GT:
  3186. case WASM_OP_F32_LE:
  3187. case WASM_OP_F32_GE:
  3188. case WASM_OP_F64_EQ:
  3189. case WASM_OP_F64_NE:
  3190. case WASM_OP_F64_LT:
  3191. case WASM_OP_F64_GT:
  3192. case WASM_OP_F64_LE:
  3193. case WASM_OP_F64_GE:
  3194. case WASM_OP_I32_CLZ:
  3195. case WASM_OP_I32_CTZ:
  3196. case WASM_OP_I32_POPCNT:
  3197. case WASM_OP_I32_ADD:
  3198. case WASM_OP_I32_SUB:
  3199. case WASM_OP_I32_MUL:
  3200. case WASM_OP_I32_DIV_S:
  3201. case WASM_OP_I32_DIV_U:
  3202. case WASM_OP_I32_REM_S:
  3203. case WASM_OP_I32_REM_U:
  3204. case WASM_OP_I32_AND:
  3205. case WASM_OP_I32_OR:
  3206. case WASM_OP_I32_XOR:
  3207. case WASM_OP_I32_SHL:
  3208. case WASM_OP_I32_SHR_S:
  3209. case WASM_OP_I32_SHR_U:
  3210. case WASM_OP_I32_ROTL:
  3211. case WASM_OP_I32_ROTR:
  3212. case WASM_OP_I64_CLZ:
  3213. case WASM_OP_I64_CTZ:
  3214. case WASM_OP_I64_POPCNT:
  3215. case WASM_OP_I64_ADD:
  3216. case WASM_OP_I64_SUB:
  3217. case WASM_OP_I64_MUL:
  3218. case WASM_OP_I64_DIV_S:
  3219. case WASM_OP_I64_DIV_U:
  3220. case WASM_OP_I64_REM_S:
  3221. case WASM_OP_I64_REM_U:
  3222. case WASM_OP_I64_AND:
  3223. case WASM_OP_I64_OR:
  3224. case WASM_OP_I64_XOR:
  3225. case WASM_OP_I64_SHL:
  3226. case WASM_OP_I64_SHR_S:
  3227. case WASM_OP_I64_SHR_U:
  3228. case WASM_OP_I64_ROTL:
  3229. case WASM_OP_I64_ROTR:
  3230. case WASM_OP_F32_ABS:
  3231. case WASM_OP_F32_NEG:
  3232. case WASM_OP_F32_CEIL:
  3233. case WASM_OP_F32_FLOOR:
  3234. case WASM_OP_F32_TRUNC:
  3235. case WASM_OP_F32_NEAREST:
  3236. case WASM_OP_F32_SQRT:
  3237. case WASM_OP_F32_ADD:
  3238. case WASM_OP_F32_SUB:
  3239. case WASM_OP_F32_MUL:
  3240. case WASM_OP_F32_DIV:
  3241. case WASM_OP_F32_MIN:
  3242. case WASM_OP_F32_MAX:
  3243. case WASM_OP_F32_COPYSIGN:
  3244. case WASM_OP_F64_ABS:
  3245. case WASM_OP_F64_NEG:
  3246. case WASM_OP_F64_CEIL:
  3247. case WASM_OP_F64_FLOOR:
  3248. case WASM_OP_F64_TRUNC:
  3249. case WASM_OP_F64_NEAREST:
  3250. case WASM_OP_F64_SQRT:
  3251. case WASM_OP_F64_ADD:
  3252. case WASM_OP_F64_SUB:
  3253. case WASM_OP_F64_MUL:
  3254. case WASM_OP_F64_DIV:
  3255. case WASM_OP_F64_MIN:
  3256. case WASM_OP_F64_MAX:
  3257. case WASM_OP_F64_COPYSIGN:
  3258. case WASM_OP_I32_WRAP_I64:
  3259. case WASM_OP_I32_TRUNC_S_F32:
  3260. case WASM_OP_I32_TRUNC_U_F32:
  3261. case WASM_OP_I32_TRUNC_S_F64:
  3262. case WASM_OP_I32_TRUNC_U_F64:
  3263. case WASM_OP_I64_EXTEND_S_I32:
  3264. case WASM_OP_I64_EXTEND_U_I32:
  3265. case WASM_OP_I64_TRUNC_S_F32:
  3266. case WASM_OP_I64_TRUNC_U_F32:
  3267. case WASM_OP_I64_TRUNC_S_F64:
  3268. case WASM_OP_I64_TRUNC_U_F64:
  3269. case WASM_OP_F32_CONVERT_S_I32:
  3270. case WASM_OP_F32_CONVERT_U_I32:
  3271. case WASM_OP_F32_CONVERT_S_I64:
  3272. case WASM_OP_F32_CONVERT_U_I64:
  3273. case WASM_OP_F32_DEMOTE_F64:
  3274. case WASM_OP_F64_CONVERT_S_I32:
  3275. case WASM_OP_F64_CONVERT_U_I32:
  3276. case WASM_OP_F64_CONVERT_S_I64:
  3277. case WASM_OP_F64_CONVERT_U_I64:
  3278. case WASM_OP_F64_PROMOTE_F32:
  3279. case WASM_OP_I32_REINTERPRET_F32:
  3280. case WASM_OP_I64_REINTERPRET_F64:
  3281. case WASM_OP_F32_REINTERPRET_I32:
  3282. case WASM_OP_F64_REINTERPRET_I64:
  3283. case WASM_OP_I32_EXTEND8_S:
  3284. case WASM_OP_I32_EXTEND16_S:
  3285. case WASM_OP_I64_EXTEND8_S:
  3286. case WASM_OP_I64_EXTEND16_S:
  3287. case WASM_OP_I64_EXTEND32_S:
  3288. break;
  3289. case WASM_OP_MISC_PREFIX:
  3290. {
  3291. uint32 opcode1;
  3292. read_leb_uint32(p, p_end, opcode1);
  3293. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  3294. is no larger than UINT8_MAX */
  3295. opcode = (uint8)opcode1;
  3296. switch (opcode) {
  3297. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3298. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3299. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3300. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3301. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3302. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3303. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3304. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3305. break;
  3306. #if WASM_ENABLE_BULK_MEMORY != 0
  3307. case WASM_OP_MEMORY_INIT:
  3308. skip_leb_uint32(p, p_end);
  3309. /* skip memory idx */
  3310. p++;
  3311. break;
  3312. case WASM_OP_DATA_DROP:
  3313. skip_leb_uint32(p, p_end);
  3314. break;
  3315. case WASM_OP_MEMORY_COPY:
  3316. /* skip two memory idx */
  3317. p += 2;
  3318. break;
  3319. case WASM_OP_MEMORY_FILL:
  3320. /* skip memory idx */
  3321. p++;
  3322. break;
  3323. #endif
  3324. #if WASM_ENABLE_REF_TYPES != 0
  3325. case WASM_OP_TABLE_INIT:
  3326. case WASM_OP_TABLE_COPY:
  3327. /* tableidx */
  3328. skip_leb_uint32(p, p_end);
  3329. /* elemidx */
  3330. skip_leb_uint32(p, p_end);
  3331. break;
  3332. case WASM_OP_ELEM_DROP:
  3333. /* elemidx */
  3334. skip_leb_uint32(p, p_end);
  3335. break;
  3336. case WASM_OP_TABLE_SIZE:
  3337. case WASM_OP_TABLE_GROW:
  3338. case WASM_OP_TABLE_FILL:
  3339. skip_leb_uint32(p, p_end); /* table idx */
  3340. break;
  3341. #endif /* WASM_ENABLE_REF_TYPES */
  3342. default:
  3343. bh_assert(0);
  3344. break;
  3345. }
  3346. break;
  3347. }
  3348. #if WASM_ENABLE_SHARED_MEMORY != 0
  3349. case WASM_OP_ATOMIC_PREFIX:
  3350. {
  3351. /* TODO: memory64 offset type changes */
  3352. uint32 opcode1;
  3353. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  3354. read_leb_uint32(p, p_end, opcode1);
  3355. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  3356. is no larger than UINT8_MAX */
  3357. opcode = (uint8)opcode1;
  3358. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3359. skip_leb_uint32(p, p_end); /* align */
  3360. skip_leb_mem_offset(p, p_end); /* offset */
  3361. }
  3362. else {
  3363. /* atomic.fence doesn't have memarg */
  3364. p++;
  3365. }
  3366. break;
  3367. }
  3368. #endif
  3369. default:
  3370. bh_assert(0);
  3371. break;
  3372. }
  3373. }
  3374. (void)u8;
  3375. return false;
  3376. }
  3377. #define REF_I32 VALUE_TYPE_I32
  3378. #define REF_F32 VALUE_TYPE_F32
  3379. #define REF_I64_1 VALUE_TYPE_I64
  3380. #define REF_I64_2 VALUE_TYPE_I64
  3381. #define REF_F64_1 VALUE_TYPE_F64
  3382. #define REF_F64_2 VALUE_TYPE_F64
  3383. #define REF_ANY VALUE_TYPE_ANY
  3384. #if WASM_ENABLE_FAST_INTERP != 0
  3385. #if WASM_DEBUG_PREPROCESSOR != 0
  3386. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3387. #else
  3388. #define LOG_OP(...) (void)0
  3389. #endif
  3390. #define PATCH_ELSE 0
  3391. #define PATCH_END 1
  3392. typedef struct BranchBlockPatch {
  3393. struct BranchBlockPatch *next;
  3394. uint8 patch_type;
  3395. uint8 *code_compiled;
  3396. } BranchBlockPatch;
  3397. #endif
  3398. typedef struct BranchBlock {
  3399. uint8 label_type;
  3400. BlockType block_type;
  3401. uint8 *start_addr;
  3402. uint8 *else_addr;
  3403. uint8 *end_addr;
  3404. uint32 stack_cell_num;
  3405. #if WASM_ENABLE_FAST_INTERP != 0
  3406. uint16 dynamic_offset;
  3407. uint8 *code_compiled;
  3408. BranchBlockPatch *patch_list;
  3409. /* This is used to save params frame_offset of of if block */
  3410. int16 *param_frame_offsets;
  3411. /* This is used to store available param num for if/else branch, so the else
  3412. * opcode can know how many parameters should be copied to the stack */
  3413. uint32 available_param_num;
  3414. /* This is used to recover the dynamic offset for else branch,
  3415. * and also to remember the start offset of dynamic space which
  3416. * stores the block arguments for loop block, so we can use it
  3417. * to copy the stack operands to the loop block's arguments in
  3418. * wasm_loader_emit_br_info for opcode br. */
  3419. uint16 start_dynamic_offset;
  3420. #endif
  3421. /* Indicate the operand stack is in polymorphic state.
  3422. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3423. * to polymorphic state until the block's 'end' opcode is processed.
  3424. * If stack is in polymorphic state and stack is empty, instruction can
  3425. * pop any type of value directly without decreasing stack top pointer
  3426. * and stack cell num. */
  3427. bool is_stack_polymorphic;
  3428. } BranchBlock;
  3429. typedef struct WASMLoaderContext {
  3430. /* frame ref stack */
  3431. uint8 *frame_ref;
  3432. uint8 *frame_ref_bottom;
  3433. uint8 *frame_ref_boundary;
  3434. uint32 frame_ref_size;
  3435. uint32 stack_cell_num;
  3436. uint32 max_stack_cell_num;
  3437. /* frame csp stack */
  3438. BranchBlock *frame_csp;
  3439. BranchBlock *frame_csp_bottom;
  3440. BranchBlock *frame_csp_boundary;
  3441. uint32 frame_csp_size;
  3442. uint32 csp_num;
  3443. uint32 max_csp_num;
  3444. #if WASM_ENABLE_FAST_INTERP != 0
  3445. /* frame offset stack */
  3446. int16 *frame_offset;
  3447. int16 *frame_offset_bottom;
  3448. int16 *frame_offset_boundary;
  3449. uint32 frame_offset_size;
  3450. int16 dynamic_offset;
  3451. int16 start_dynamic_offset;
  3452. int16 max_dynamic_offset;
  3453. /* preserved local offset */
  3454. int16 preserved_local_offset;
  3455. /* const buffer */
  3456. uint8 *const_buf;
  3457. uint16 num_const;
  3458. uint16 const_cell_num;
  3459. uint32 const_buf_size;
  3460. /* processed code */
  3461. uint8 *p_code_compiled;
  3462. uint8 *p_code_compiled_end;
  3463. uint32 code_compiled_size;
  3464. /* If the last opcode will be dropped, the peak memory usage will be larger
  3465. * than the final code_compiled_size, we record the peak size to ensure
  3466. * there will not be invalid memory access during second traverse */
  3467. uint32 code_compiled_peak_size;
  3468. #endif
  3469. } WASMLoaderContext;
  3470. typedef struct Const {
  3471. WASMValue value;
  3472. uint16 slot_index;
  3473. uint8 value_type;
  3474. } Const;
  3475. #define CHECK_CSP_PUSH() \
  3476. do { \
  3477. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3478. MEM_REALLOC( \
  3479. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3480. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  3481. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3482. ctx->frame_csp_boundary = \
  3483. ctx->frame_csp_bottom \
  3484. + ctx->frame_csp_size / sizeof(BranchBlock); \
  3485. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3486. } \
  3487. } while (0)
  3488. #define CHECK_CSP_POP() \
  3489. do { \
  3490. bh_assert(ctx->csp_num >= 1); \
  3491. } while (0)
  3492. #if WASM_ENABLE_FAST_INTERP != 0
  3493. static bool
  3494. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  3495. uint32 error_buf_size)
  3496. {
  3497. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  3498. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3499. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3500. ctx->frame_offset_size + 16);
  3501. ctx->frame_offset_size += 16;
  3502. ctx->frame_offset_boundary =
  3503. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  3504. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3505. }
  3506. return true;
  3507. fail:
  3508. return false;
  3509. }
  3510. static bool
  3511. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3512. {
  3513. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3514. return false;
  3515. return true;
  3516. }
  3517. static void
  3518. free_label_patch_list(BranchBlock *frame_csp)
  3519. {
  3520. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3521. BranchBlockPatch *next;
  3522. while (label_patch != NULL) {
  3523. next = label_patch->next;
  3524. wasm_runtime_free(label_patch);
  3525. label_patch = next;
  3526. }
  3527. frame_csp->patch_list = NULL;
  3528. }
  3529. static void
  3530. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3531. {
  3532. BranchBlock *tmp_csp = frame_csp;
  3533. uint32 i;
  3534. for (i = 0; i < csp_num; i++) {
  3535. free_label_patch_list(tmp_csp);
  3536. tmp_csp++;
  3537. }
  3538. }
  3539. static void
  3540. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  3541. {
  3542. BranchBlock *tmp_csp = frame_csp;
  3543. uint32 i;
  3544. for (i = 0; i < csp_num; i++) {
  3545. if (tmp_csp->param_frame_offsets)
  3546. wasm_runtime_free(tmp_csp->param_frame_offsets);
  3547. tmp_csp++;
  3548. }
  3549. }
  3550. #endif
  3551. static bool
  3552. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  3553. {
  3554. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3555. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3556. ctx->frame_ref_size + 16);
  3557. ctx->frame_ref_size += 16;
  3558. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3559. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3560. }
  3561. return true;
  3562. fail:
  3563. return false;
  3564. }
  3565. static bool
  3566. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3567. char *error_buf, uint32 error_buf_size)
  3568. {
  3569. bh_assert(!((is_32bit_type(type) && stack_cell_num < 1)
  3570. || (is_64bit_type(type) && stack_cell_num < 2)));
  3571. bh_assert(!(
  3572. (type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  3573. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  3574. || (type == VALUE_TYPE_I64
  3575. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  3576. || (type == VALUE_TYPE_F64
  3577. && (*(frame_ref - 2) != REF_F64_1
  3578. || *(frame_ref - 1) != REF_F64_2))));
  3579. return true;
  3580. }
  3581. static bool
  3582. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3583. uint32 error_buf_size)
  3584. {
  3585. int32 block_stack_cell_num =
  3586. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3587. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3588. /* the stack top is a value of any type, return success */
  3589. return true;
  3590. }
  3591. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  3592. error_buf, error_buf_size))
  3593. return false;
  3594. return true;
  3595. }
  3596. static void
  3597. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3598. {
  3599. if (ctx) {
  3600. if (ctx->frame_ref_bottom)
  3601. wasm_runtime_free(ctx->frame_ref_bottom);
  3602. if (ctx->frame_csp_bottom) {
  3603. #if WASM_ENABLE_FAST_INTERP != 0
  3604. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3605. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  3606. ctx->csp_num);
  3607. #endif
  3608. wasm_runtime_free(ctx->frame_csp_bottom);
  3609. }
  3610. #if WASM_ENABLE_FAST_INTERP != 0
  3611. if (ctx->frame_offset_bottom)
  3612. wasm_runtime_free(ctx->frame_offset_bottom);
  3613. if (ctx->const_buf)
  3614. wasm_runtime_free(ctx->const_buf);
  3615. #endif
  3616. wasm_runtime_free(ctx);
  3617. }
  3618. }
  3619. static WASMLoaderContext *
  3620. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  3621. {
  3622. WASMLoaderContext *loader_ctx =
  3623. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  3624. if (!loader_ctx)
  3625. return NULL;
  3626. loader_ctx->frame_ref_size = 32;
  3627. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  3628. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  3629. goto fail;
  3630. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  3631. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3632. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  3633. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  3634. goto fail;
  3635. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3636. #if WASM_ENABLE_FAST_INTERP != 0
  3637. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3638. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3639. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  3640. error_buf_size)))
  3641. goto fail;
  3642. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  3643. loader_ctx->num_const = 0;
  3644. loader_ctx->const_buf_size = sizeof(Const) * 8;
  3645. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  3646. error_buf, error_buf_size)))
  3647. goto fail;
  3648. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  3649. set_error_buf(error_buf, error_buf_size,
  3650. "fast interpreter offset overflow");
  3651. goto fail;
  3652. }
  3653. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  3654. loader_ctx->max_dynamic_offset =
  3655. func->param_cell_num + func->local_cell_num;
  3656. #endif
  3657. return loader_ctx;
  3658. fail:
  3659. wasm_loader_ctx_destroy(loader_ctx);
  3660. return NULL;
  3661. }
  3662. static bool
  3663. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3664. uint32 error_buf_size)
  3665. {
  3666. if (type == VALUE_TYPE_VOID)
  3667. return true;
  3668. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3669. return false;
  3670. *ctx->frame_ref++ = type;
  3671. ctx->stack_cell_num++;
  3672. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3673. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3674. if (is_32bit_type(type))
  3675. return true;
  3676. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3677. return false;
  3678. *ctx->frame_ref++ = type;
  3679. ctx->stack_cell_num++;
  3680. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  3681. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3682. bh_assert(ctx->max_stack_cell_num <= UINT16_MAX);
  3683. }
  3684. return true;
  3685. }
  3686. static bool
  3687. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3688. uint32 error_buf_size)
  3689. {
  3690. BranchBlock *cur_block = ctx->frame_csp - 1;
  3691. int32 available_stack_cell =
  3692. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  3693. /* Directly return success if current block is in stack
  3694. * polymorphic state while stack is empty. */
  3695. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3696. return true;
  3697. if (type == VALUE_TYPE_VOID)
  3698. return true;
  3699. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  3700. return false;
  3701. ctx->frame_ref--;
  3702. ctx->stack_cell_num--;
  3703. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  3704. return true;
  3705. ctx->frame_ref--;
  3706. ctx->stack_cell_num--;
  3707. return true;
  3708. }
  3709. #if WASM_ENABLE_FAST_INTERP == 0
  3710. static bool
  3711. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  3712. uint8 type_push, uint8 type_pop, char *error_buf,
  3713. uint32 error_buf_size)
  3714. {
  3715. for (int i = 0; i < pop_cnt; i++) {
  3716. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  3717. error_buf_size))
  3718. return false;
  3719. }
  3720. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  3721. return false;
  3722. return true;
  3723. }
  3724. #endif
  3725. static bool
  3726. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  3727. BlockType block_type, uint8 *start_addr,
  3728. char *error_buf, uint32 error_buf_size)
  3729. {
  3730. CHECK_CSP_PUSH();
  3731. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  3732. ctx->frame_csp->label_type = label_type;
  3733. ctx->frame_csp->block_type = block_type;
  3734. ctx->frame_csp->start_addr = start_addr;
  3735. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  3736. #if WASM_ENABLE_FAST_INTERP != 0
  3737. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  3738. ctx->frame_csp->patch_list = NULL;
  3739. #endif
  3740. ctx->frame_csp++;
  3741. ctx->csp_num++;
  3742. if (ctx->csp_num > ctx->max_csp_num) {
  3743. ctx->max_csp_num = ctx->csp_num;
  3744. bh_assert(ctx->max_csp_num <= UINT16_MAX);
  3745. }
  3746. return true;
  3747. fail:
  3748. return false;
  3749. }
  3750. static bool
  3751. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  3752. uint32 error_buf_size)
  3753. {
  3754. CHECK_CSP_POP();
  3755. #if WASM_ENABLE_FAST_INTERP != 0
  3756. if ((ctx->frame_csp - 1)->param_frame_offsets)
  3757. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  3758. #endif
  3759. ctx->frame_csp--;
  3760. ctx->csp_num--;
  3761. return true;
  3762. }
  3763. #if WASM_ENABLE_FAST_INTERP != 0
  3764. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3765. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3766. #define emit_label(opcode) \
  3767. do { \
  3768. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  3769. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3770. } while (0)
  3771. #define skip_label() \
  3772. do { \
  3773. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  3774. LOG_OP("\ndelete last op\n"); \
  3775. } while (0)
  3776. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3777. #if UINTPTR_MAX == UINT64_MAX
  3778. #define emit_label(opcode) \
  3779. do { \
  3780. int32 offset = \
  3781. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  3782. /* emit int32 relative offset in 64-bit target */ \
  3783. wasm_loader_emit_uint32(loader_ctx, offset); \
  3784. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3785. } while (0)
  3786. #else
  3787. #define emit_label(opcode) \
  3788. do { \
  3789. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  3790. /* emit uint32 label address in 32-bit target */ \
  3791. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  3792. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3793. } while (0)
  3794. #endif
  3795. #define skip_label() \
  3796. do { \
  3797. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  3798. LOG_OP("\ndelete last op\n"); \
  3799. } while (0)
  3800. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3801. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  3802. #define emit_label(opcode) \
  3803. do { \
  3804. wasm_loader_emit_uint8(loader_ctx, opcode); \
  3805. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3806. } while (0)
  3807. #define skip_label() \
  3808. do { \
  3809. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  3810. LOG_OP("\ndelete last op\n"); \
  3811. } while (0)
  3812. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  3813. #define emit_empty_label_addr_and_frame_ip(type) \
  3814. do { \
  3815. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  3816. loader_ctx->p_code_compiled, error_buf, \
  3817. error_buf_size)) \
  3818. goto fail; \
  3819. /* label address, to be patched */ \
  3820. wasm_loader_emit_ptr(loader_ctx, NULL); \
  3821. } while (0)
  3822. #define emit_br_info(frame_csp, is_br) \
  3823. do { \
  3824. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  3825. error_buf_size)) \
  3826. goto fail; \
  3827. } while (0)
  3828. #define LAST_OP_OUTPUT_I32() \
  3829. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  3830. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  3831. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  3832. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  3833. || (last_op >= WASM_OP_I32_WRAP_I64 \
  3834. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  3835. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  3836. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  3837. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  3838. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  3839. || (last_op == EXT_OP_COPY_STACK_TOP)
  3840. #define LAST_OP_OUTPUT_I64() \
  3841. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  3842. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  3843. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  3844. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  3845. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  3846. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  3847. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  3848. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  3849. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  3850. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  3851. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  3852. #define GET_CONST_OFFSET(type, val) \
  3853. do { \
  3854. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  3855. &operand_offset, error_buf, \
  3856. error_buf_size))) \
  3857. goto fail; \
  3858. } while (0)
  3859. #define GET_CONST_F32_OFFSET(type, fval) \
  3860. do { \
  3861. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3862. &operand_offset, error_buf, \
  3863. error_buf_size))) \
  3864. goto fail; \
  3865. } while (0)
  3866. #define GET_CONST_F64_OFFSET(type, fval) \
  3867. do { \
  3868. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3869. &operand_offset, error_buf, \
  3870. error_buf_size))) \
  3871. goto fail; \
  3872. } while (0)
  3873. #define emit_operand(ctx, offset) \
  3874. do { \
  3875. wasm_loader_emit_int16(ctx, offset); \
  3876. LOG_OP("%d\t", offset); \
  3877. } while (0)
  3878. #define emit_byte(ctx, byte) \
  3879. do { \
  3880. wasm_loader_emit_uint8(ctx, byte); \
  3881. LOG_OP("%d\t", byte); \
  3882. } while (0)
  3883. #define emit_uint32(ctx, value) \
  3884. do { \
  3885. wasm_loader_emit_uint32(ctx, value); \
  3886. LOG_OP("%d\t", value); \
  3887. } while (0)
  3888. #define emit_uint64(ctx, value) \
  3889. do { \
  3890. wasm_loader_emit_const(ctx, &value, false); \
  3891. LOG_OP("%lld\t", value); \
  3892. } while (0)
  3893. #define emit_float32(ctx, value) \
  3894. do { \
  3895. wasm_loader_emit_const(ctx, &value, true); \
  3896. LOG_OP("%f\t", value); \
  3897. } while (0)
  3898. #define emit_float64(ctx, value) \
  3899. do { \
  3900. wasm_loader_emit_const(ctx, &value, false); \
  3901. LOG_OP("%f\t", value); \
  3902. } while (0)
  3903. static bool
  3904. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  3905. {
  3906. if (!(ctx->p_code_compiled =
  3907. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  3908. return false;
  3909. ctx->p_code_compiled_end =
  3910. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  3911. /* clean up frame ref */
  3912. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  3913. ctx->frame_ref = ctx->frame_ref_bottom;
  3914. ctx->stack_cell_num = 0;
  3915. /* clean up frame csp */
  3916. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  3917. ctx->frame_csp = ctx->frame_csp_bottom;
  3918. ctx->csp_num = 0;
  3919. ctx->max_csp_num = 0;
  3920. /* clean up frame offset */
  3921. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  3922. ctx->frame_offset = ctx->frame_offset_bottom;
  3923. ctx->dynamic_offset = ctx->start_dynamic_offset;
  3924. /* init preserved local offsets */
  3925. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  3926. /* const buf is reserved */
  3927. return true;
  3928. }
  3929. static void
  3930. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  3931. {
  3932. ctx->code_compiled_size += size;
  3933. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  3934. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  3935. }
  3936. }
  3937. static void
  3938. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  3939. {
  3940. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  3941. if (ctx->p_code_compiled) {
  3942. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3943. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3944. #endif
  3945. bh_memcpy_s(ctx->p_code_compiled,
  3946. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  3947. value, size);
  3948. ctx->p_code_compiled += size;
  3949. }
  3950. else {
  3951. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3952. bh_assert((ctx->code_compiled_size & 1) == 0);
  3953. #endif
  3954. increase_compiled_code_space(ctx, size);
  3955. }
  3956. }
  3957. static void
  3958. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  3959. {
  3960. if (ctx->p_code_compiled) {
  3961. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3962. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3963. #endif
  3964. STORE_U32(ctx->p_code_compiled, value);
  3965. ctx->p_code_compiled += sizeof(uint32);
  3966. }
  3967. else {
  3968. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3969. bh_assert((ctx->code_compiled_size & 1) == 0);
  3970. #endif
  3971. increase_compiled_code_space(ctx, sizeof(uint32));
  3972. }
  3973. }
  3974. static void
  3975. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  3976. {
  3977. if (ctx->p_code_compiled) {
  3978. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3979. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3980. #endif
  3981. STORE_U16(ctx->p_code_compiled, (uint16)value);
  3982. ctx->p_code_compiled += sizeof(int16);
  3983. }
  3984. else {
  3985. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3986. bh_assert((ctx->code_compiled_size & 1) == 0);
  3987. #endif
  3988. increase_compiled_code_space(ctx, sizeof(uint16));
  3989. }
  3990. }
  3991. static void
  3992. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  3993. {
  3994. if (ctx->p_code_compiled) {
  3995. *(ctx->p_code_compiled) = value;
  3996. ctx->p_code_compiled += sizeof(uint8);
  3997. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3998. ctx->p_code_compiled++;
  3999. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4000. #endif
  4001. }
  4002. else {
  4003. increase_compiled_code_space(ctx, sizeof(uint8));
  4004. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4005. increase_compiled_code_space(ctx, sizeof(uint8));
  4006. bh_assert((ctx->code_compiled_size & 1) == 0);
  4007. #endif
  4008. }
  4009. }
  4010. static void
  4011. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  4012. {
  4013. if (ctx->p_code_compiled) {
  4014. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4015. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4016. #endif
  4017. STORE_PTR(ctx->p_code_compiled, value);
  4018. ctx->p_code_compiled += sizeof(void *);
  4019. }
  4020. else {
  4021. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4022. bh_assert((ctx->code_compiled_size & 1) == 0);
  4023. #endif
  4024. increase_compiled_code_space(ctx, sizeof(void *));
  4025. }
  4026. }
  4027. static void
  4028. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  4029. {
  4030. if (ctx->p_code_compiled) {
  4031. ctx->p_code_compiled -= size;
  4032. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4033. if (size == sizeof(uint8)) {
  4034. ctx->p_code_compiled--;
  4035. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4036. }
  4037. #endif
  4038. }
  4039. else {
  4040. ctx->code_compiled_size -= size;
  4041. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4042. if (size == sizeof(uint8)) {
  4043. ctx->code_compiled_size--;
  4044. bh_assert((ctx->code_compiled_size & 1) == 0);
  4045. }
  4046. #endif
  4047. }
  4048. }
  4049. static bool
  4050. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  4051. uint32 local_index, uint32 local_type,
  4052. bool *preserved, char *error_buf,
  4053. uint32 error_buf_size)
  4054. {
  4055. uint32 i = 0;
  4056. int16 preserved_offset = (int16)local_index;
  4057. *preserved = false;
  4058. while (i < loader_ctx->stack_cell_num) {
  4059. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4060. /* move previous local into dynamic space before a set/tee_local opcode
  4061. */
  4062. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  4063. if (!(*preserved)) {
  4064. *preserved = true;
  4065. skip_label();
  4066. preserved_offset = loader_ctx->preserved_local_offset;
  4067. if (loader_ctx->p_code_compiled) {
  4068. bh_assert(preserved_offset != (int16)local_index);
  4069. }
  4070. if (is_32bit_type(local_type)) {
  4071. /* Only increase preserve offset in the second traversal */
  4072. if (loader_ctx->p_code_compiled)
  4073. loader_ctx->preserved_local_offset++;
  4074. emit_label(EXT_OP_COPY_STACK_TOP);
  4075. }
  4076. else {
  4077. if (loader_ctx->p_code_compiled)
  4078. loader_ctx->preserved_local_offset += 2;
  4079. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  4080. }
  4081. emit_operand(loader_ctx, local_index);
  4082. emit_operand(loader_ctx, preserved_offset);
  4083. emit_label(opcode);
  4084. }
  4085. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  4086. }
  4087. if (is_32bit_type(cur_type))
  4088. i++;
  4089. else
  4090. i += 2;
  4091. }
  4092. return true;
  4093. }
  4094. static bool
  4095. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  4096. char *error_buf, uint32 error_buf_size)
  4097. {
  4098. uint32 i = 0;
  4099. bool preserve_local;
  4100. /* preserve locals before blocks to ensure that "tee/set_local" inside
  4101. blocks will not influence the value of these locals */
  4102. while (i < loader_ctx->stack_cell_num) {
  4103. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  4104. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4105. if ((cur_offset < loader_ctx->start_dynamic_offset)
  4106. && (cur_offset >= 0)) {
  4107. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  4108. cur_type, &preserve_local,
  4109. error_buf, error_buf_size)))
  4110. return false;
  4111. }
  4112. if (is_32bit_type(cur_type == VALUE_TYPE_I32)) {
  4113. i++;
  4114. }
  4115. else {
  4116. i += 2;
  4117. }
  4118. }
  4119. return true;
  4120. }
  4121. static bool
  4122. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  4123. uint8 *p_code_compiled, char *error_buf,
  4124. uint32 error_buf_size)
  4125. {
  4126. BranchBlockPatch *patch =
  4127. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  4128. if (!patch) {
  4129. return false;
  4130. }
  4131. patch->patch_type = patch_type;
  4132. patch->code_compiled = p_code_compiled;
  4133. if (!frame_csp->patch_list) {
  4134. frame_csp->patch_list = patch;
  4135. patch->next = NULL;
  4136. }
  4137. else {
  4138. patch->next = frame_csp->patch_list;
  4139. frame_csp->patch_list = patch;
  4140. }
  4141. return true;
  4142. }
  4143. static void
  4144. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  4145. {
  4146. BranchBlock *frame_csp = ctx->frame_csp - depth;
  4147. BranchBlockPatch *node = frame_csp->patch_list;
  4148. BranchBlockPatch *node_prev = NULL, *node_next;
  4149. if (!ctx->p_code_compiled)
  4150. return;
  4151. while (node) {
  4152. node_next = node->next;
  4153. if (node->patch_type == patch_type) {
  4154. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  4155. if (node_prev == NULL) {
  4156. frame_csp->patch_list = node_next;
  4157. }
  4158. else {
  4159. node_prev->next = node_next;
  4160. }
  4161. wasm_runtime_free(node);
  4162. }
  4163. else {
  4164. node_prev = node;
  4165. }
  4166. node = node_next;
  4167. }
  4168. }
  4169. static bool
  4170. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  4171. bool is_br, char *error_buf, uint32 error_buf_size)
  4172. {
  4173. /* br info layout:
  4174. * a) arity of target block
  4175. * b) total cell num of arity values
  4176. * c) each arity value's cell num
  4177. * d) each arity value's src frame offset
  4178. * e) each arity values's dst dynamic offset
  4179. * f) branch target address
  4180. *
  4181. * Note: b-e are omitted when arity is 0 so that
  4182. * interpreter can recover the br info quickly.
  4183. */
  4184. BlockType *block_type = &frame_csp->block_type;
  4185. uint8 *types = NULL, cell;
  4186. uint32 arity = 0;
  4187. int32 i;
  4188. int16 *frame_offset = ctx->frame_offset;
  4189. uint16 dynamic_offset;
  4190. /* Note: loop's arity is different from if and block. loop's arity is
  4191. * its parameter count while if and block arity is result count.
  4192. */
  4193. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  4194. arity = block_type_get_param_types(block_type, &types);
  4195. else
  4196. arity = block_type_get_result_types(block_type, &types);
  4197. /* Part a */
  4198. emit_uint32(ctx, arity);
  4199. if (arity) {
  4200. /* Part b */
  4201. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  4202. /* Part c */
  4203. for (i = (int32)arity - 1; i >= 0; i--) {
  4204. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4205. emit_byte(ctx, cell);
  4206. }
  4207. /* Part d */
  4208. for (i = (int32)arity - 1; i >= 0; i--) {
  4209. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4210. frame_offset -= cell;
  4211. emit_operand(ctx, *(int16 *)(frame_offset));
  4212. }
  4213. /* Part e */
  4214. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  4215. /* Use start_dynamic_offset which was set in
  4216. copy_params_to_dynamic_space */
  4217. dynamic_offset = frame_csp->start_dynamic_offset
  4218. + wasm_get_cell_num(types, arity);
  4219. else
  4220. dynamic_offset =
  4221. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  4222. if (is_br)
  4223. ctx->dynamic_offset = dynamic_offset;
  4224. for (i = (int32)arity - 1; i >= 0; i--) {
  4225. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4226. dynamic_offset -= cell;
  4227. emit_operand(ctx, dynamic_offset);
  4228. }
  4229. }
  4230. /* Part f */
  4231. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  4232. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  4233. }
  4234. else {
  4235. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  4236. error_buf, error_buf_size))
  4237. return false;
  4238. /* label address, to be patched */
  4239. wasm_loader_emit_ptr(ctx, NULL);
  4240. }
  4241. return true;
  4242. }
  4243. static bool
  4244. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4245. bool disable_emit, int16 operand_offset,
  4246. char *error_buf, uint32 error_buf_size)
  4247. {
  4248. uint32 cell_num_to_push, i;
  4249. if (type == VALUE_TYPE_VOID)
  4250. return true;
  4251. /* only check memory overflow in first traverse */
  4252. if (ctx->p_code_compiled == NULL) {
  4253. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4254. return false;
  4255. }
  4256. if (disable_emit)
  4257. *(ctx->frame_offset)++ = operand_offset;
  4258. else {
  4259. emit_operand(ctx, ctx->dynamic_offset);
  4260. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4261. ctx->dynamic_offset++;
  4262. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4263. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4264. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4265. }
  4266. }
  4267. if (is_32bit_type(type))
  4268. return true;
  4269. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  4270. for (i = 0; i < cell_num_to_push; i++) {
  4271. if (ctx->p_code_compiled == NULL) {
  4272. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4273. return false;
  4274. }
  4275. ctx->frame_offset++;
  4276. if (!disable_emit) {
  4277. ctx->dynamic_offset++;
  4278. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4279. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4280. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4281. }
  4282. }
  4283. }
  4284. return true;
  4285. }
  4286. /* This function should be in front of wasm_loader_pop_frame_ref
  4287. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4288. will be modified by wasm_loader_pop_frame_ref */
  4289. static bool
  4290. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4291. char *error_buf, uint32 error_buf_size)
  4292. {
  4293. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4294. then current block is the function block */
  4295. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4296. BranchBlock *cur_block = ctx->frame_csp - depth;
  4297. int32 available_stack_cell =
  4298. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4299. uint32 cell_num_to_pop;
  4300. /* Directly return success if current block is in stack
  4301. polymorphic state while stack is empty. */
  4302. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4303. return true;
  4304. if (type == VALUE_TYPE_VOID)
  4305. return true;
  4306. /* Change type to ANY when the stack top is ANY, so as to avoid
  4307. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  4308. two offsets */
  4309. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  4310. type = VALUE_TYPE_ANY;
  4311. cell_num_to_pop = wasm_value_type_cell_num(type);
  4312. /* Check the offset stack bottom to ensure the frame offset
  4313. stack will not go underflow. But we don't thrown error
  4314. and return true here, because the error msg should be
  4315. given in wasm_loader_pop_frame_ref */
  4316. if (!check_offset_pop(ctx, cell_num_to_pop))
  4317. return true;
  4318. ctx->frame_offset -= cell_num_to_pop;
  4319. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4320. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4321. ctx->dynamic_offset -= cell_num_to_pop;
  4322. emit_operand(ctx, *(ctx->frame_offset));
  4323. (void)error_buf;
  4324. (void)error_buf_size;
  4325. return true;
  4326. }
  4327. static bool
  4328. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4329. bool disable_emit, int16 operand_offset,
  4330. char *error_buf, uint32 error_buf_size)
  4331. {
  4332. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  4333. error_buf, error_buf_size)))
  4334. return false;
  4335. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  4336. return false;
  4337. return true;
  4338. }
  4339. static bool
  4340. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4341. char *error_buf, uint32 error_buf_size)
  4342. {
  4343. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  4344. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  4345. return false;
  4346. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  4347. return false;
  4348. return true;
  4349. }
  4350. static bool
  4351. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4352. uint8 type_push, uint8 type_pop,
  4353. bool disable_emit, int16 operand_offset,
  4354. char *error_buf, uint32 error_buf_size)
  4355. {
  4356. uint8 i;
  4357. for (i = 0; i < pop_cnt; i++) {
  4358. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  4359. error_buf_size))
  4360. return false;
  4361. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4362. error_buf_size))
  4363. return false;
  4364. }
  4365. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  4366. operand_offset, error_buf,
  4367. error_buf_size))
  4368. return false;
  4369. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4370. return false;
  4371. return true;
  4372. }
  4373. static bool
  4374. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  4375. int16 *offset, char *error_buf,
  4376. uint32 error_buf_size)
  4377. {
  4378. int8 bytes_to_increase;
  4379. int16 operand_offset = 0;
  4380. Const *c;
  4381. /* Search existing constant */
  4382. for (c = (Const *)ctx->const_buf;
  4383. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  4384. if ((type == c->value_type)
  4385. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  4386. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  4387. #if WASM_ENABLE_REF_TYPES != 0
  4388. || (type == VALUE_TYPE_FUNCREF
  4389. && *(int32 *)value == c->value.i32)
  4390. || (type == VALUE_TYPE_EXTERNREF
  4391. && *(int32 *)value == c->value.i32)
  4392. #endif
  4393. || (type == VALUE_TYPE_F64
  4394. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  4395. || (type == VALUE_TYPE_F32
  4396. && (0
  4397. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  4398. operand_offset = c->slot_index;
  4399. break;
  4400. }
  4401. if (c->value_type == VALUE_TYPE_I64 || c->value_type == VALUE_TYPE_F64)
  4402. operand_offset += 2;
  4403. else
  4404. operand_offset += 1;
  4405. }
  4406. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  4407. /* New constant, append to the const buffer */
  4408. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  4409. bytes_to_increase = 2;
  4410. }
  4411. else {
  4412. bytes_to_increase = 1;
  4413. }
  4414. /* The max cell num of const buffer is 32768 since the valid index range
  4415. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  4416. * full */
  4417. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  4418. *offset = 0;
  4419. return true;
  4420. }
  4421. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  4422. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  4423. ctx->const_buf_size + 4 * sizeof(Const));
  4424. ctx->const_buf_size += 4 * sizeof(Const);
  4425. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  4426. }
  4427. c->value_type = type;
  4428. switch (type) {
  4429. case VALUE_TYPE_F64:
  4430. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  4431. sizeof(float64));
  4432. ctx->const_cell_num += 2;
  4433. /* The const buf will be reversed, we use the second cell */
  4434. /* of the i64/f64 const so the finnal offset is corrent */
  4435. operand_offset++;
  4436. break;
  4437. case VALUE_TYPE_I64:
  4438. c->value.i64 = *(int64 *)value;
  4439. ctx->const_cell_num += 2;
  4440. operand_offset++;
  4441. break;
  4442. case VALUE_TYPE_F32:
  4443. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  4444. sizeof(float32));
  4445. ctx->const_cell_num++;
  4446. break;
  4447. case VALUE_TYPE_I32:
  4448. c->value.i32 = *(int32 *)value;
  4449. ctx->const_cell_num++;
  4450. break;
  4451. #if WASM_ENABLE_REF_TYPES != 0
  4452. case VALUE_TYPE_EXTERNREF:
  4453. case VALUE_TYPE_FUNCREF:
  4454. c->value.i32 = *(int32 *)value;
  4455. ctx->const_cell_num++;
  4456. break;
  4457. #endif
  4458. default:
  4459. break;
  4460. }
  4461. c->slot_index = operand_offset;
  4462. ctx->num_const++;
  4463. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  4464. (int64)c->value.i64);
  4465. }
  4466. /* use negetive index for const */
  4467. operand_offset = -(operand_offset + 1);
  4468. *offset = operand_offset;
  4469. return true;
  4470. fail:
  4471. return false;
  4472. }
  4473. /*
  4474. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4475. -- Mostly used for the binary / compare operation
  4476. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4477. -- Mostly used in block / control instructions
  4478. The POP will always emit the offset on the top of the frame_offset stack
  4479. PUSH can be used in two ways:
  4480. 1. directly PUSH:
  4481. PUSH_XXX();
  4482. will allocate a dynamic space and emit
  4483. 2. silent PUSH:
  4484. operand_offset = xxx; disable_emit = true;
  4485. PUSH_XXX();
  4486. only push the frame_offset stack, no emit
  4487. */
  4488. #define PUSH_I32() \
  4489. do { \
  4490. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4491. disable_emit, operand_offset, \
  4492. error_buf, error_buf_size)) \
  4493. goto fail; \
  4494. } while (0)
  4495. #define PUSH_F32() \
  4496. do { \
  4497. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4498. disable_emit, operand_offset, \
  4499. error_buf, error_buf_size)) \
  4500. goto fail; \
  4501. } while (0)
  4502. #define PUSH_I64() \
  4503. do { \
  4504. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4505. disable_emit, operand_offset, \
  4506. error_buf, error_buf_size)) \
  4507. goto fail; \
  4508. } while (0)
  4509. #define PUSH_F64() \
  4510. do { \
  4511. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4512. disable_emit, operand_offset, \
  4513. error_buf, error_buf_size)) \
  4514. goto fail; \
  4515. } while (0)
  4516. #define PUSH_FUNCREF() \
  4517. do { \
  4518. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF, \
  4519. disable_emit, operand_offset, \
  4520. error_buf, error_buf_size)) \
  4521. goto fail; \
  4522. } while (0)
  4523. #define POP_I32() \
  4524. do { \
  4525. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4526. error_buf, error_buf_size)) \
  4527. goto fail; \
  4528. } while (0)
  4529. #define POP_F32() \
  4530. do { \
  4531. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4532. error_buf, error_buf_size)) \
  4533. goto fail; \
  4534. } while (0)
  4535. #define POP_I64() \
  4536. do { \
  4537. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4538. error_buf, error_buf_size)) \
  4539. goto fail; \
  4540. } while (0)
  4541. #define POP_F64() \
  4542. do { \
  4543. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4544. error_buf, error_buf_size)) \
  4545. goto fail; \
  4546. } while (0)
  4547. #define PUSH_OFFSET_TYPE(type) \
  4548. do { \
  4549. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  4550. operand_offset, error_buf, \
  4551. error_buf_size))) \
  4552. goto fail; \
  4553. } while (0)
  4554. #define POP_OFFSET_TYPE(type) \
  4555. do { \
  4556. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  4557. error_buf_size))) \
  4558. goto fail; \
  4559. } while (0)
  4560. #define PUSH_MEM_OFFSET() \
  4561. do { \
  4562. if (!wasm_loader_push_frame_ref_offset(loader_ctx, mem_offset_type, \
  4563. disable_emit, operand_offset, \
  4564. error_buf, error_buf_size)) \
  4565. goto fail; \
  4566. } while (0)
  4567. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  4568. #define POP_MEM_OFFSET() \
  4569. do { \
  4570. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, mem_offset_type, \
  4571. error_buf, error_buf_size)) \
  4572. goto fail; \
  4573. } while (0)
  4574. #define POP_AND_PUSH(type_pop, type_push) \
  4575. do { \
  4576. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4577. loader_ctx, 1, type_push, type_pop, disable_emit, \
  4578. operand_offset, error_buf, error_buf_size))) \
  4579. goto fail; \
  4580. } while (0)
  4581. /* type of POPs should be the same */
  4582. #define POP2_AND_PUSH(type_pop, type_push) \
  4583. do { \
  4584. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4585. loader_ctx, 2, type_push, type_pop, disable_emit, \
  4586. operand_offset, error_buf, error_buf_size))) \
  4587. goto fail; \
  4588. } while (0)
  4589. #else /* WASM_ENABLE_FAST_INTERP */
  4590. #define PUSH_I32() \
  4591. do { \
  4592. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4593. error_buf, error_buf_size))) \
  4594. goto fail; \
  4595. } while (0)
  4596. #define PUSH_F32() \
  4597. do { \
  4598. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4599. error_buf, error_buf_size))) \
  4600. goto fail; \
  4601. } while (0)
  4602. #define PUSH_I64() \
  4603. do { \
  4604. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4605. error_buf, error_buf_size))) \
  4606. goto fail; \
  4607. } while (0)
  4608. #define PUSH_F64() \
  4609. do { \
  4610. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4611. error_buf, error_buf_size))) \
  4612. goto fail; \
  4613. } while (0)
  4614. #define PUSH_FUNCREF() \
  4615. do { \
  4616. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4617. error_buf, error_buf_size))) \
  4618. goto fail; \
  4619. } while (0)
  4620. #define PUSH_MEM_OFFSET() \
  4621. do { \
  4622. if (!(wasm_loader_push_frame_ref(loader_ctx, mem_offset_type, \
  4623. error_buf, error_buf_size))) \
  4624. goto fail; \
  4625. } while (0)
  4626. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  4627. #define POP_I32() \
  4628. do { \
  4629. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, error_buf, \
  4630. error_buf_size))) \
  4631. goto fail; \
  4632. } while (0)
  4633. #define POP_F32() \
  4634. do { \
  4635. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, error_buf, \
  4636. error_buf_size))) \
  4637. goto fail; \
  4638. } while (0)
  4639. #define POP_I64() \
  4640. do { \
  4641. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, error_buf, \
  4642. error_buf_size))) \
  4643. goto fail; \
  4644. } while (0)
  4645. #define POP_F64() \
  4646. do { \
  4647. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, error_buf, \
  4648. error_buf_size))) \
  4649. goto fail; \
  4650. } while (0)
  4651. #define POP_FUNCREF() \
  4652. do { \
  4653. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4654. error_buf, error_buf_size))) \
  4655. goto fail; \
  4656. } while (0)
  4657. #define POP_MEM_OFFSET() \
  4658. do { \
  4659. if (!(wasm_loader_pop_frame_ref(loader_ctx, mem_offset_type, \
  4660. error_buf, error_buf_size))) \
  4661. goto fail; \
  4662. } while (0)
  4663. #define POP_AND_PUSH(type_pop, type_push) \
  4664. do { \
  4665. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  4666. type_pop, error_buf, \
  4667. error_buf_size))) \
  4668. goto fail; \
  4669. } while (0)
  4670. /* type of POPs should be the same */
  4671. #define POP2_AND_PUSH(type_pop, type_push) \
  4672. do { \
  4673. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  4674. type_pop, error_buf, \
  4675. error_buf_size))) \
  4676. goto fail; \
  4677. } while (0)
  4678. #endif /* WASM_ENABLE_FAST_INTERP */
  4679. #if WASM_ENABLE_FAST_INTERP != 0
  4680. static bool
  4681. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  4682. bool disable_emit, char *error_buf, uint32 error_buf_size)
  4683. {
  4684. int16 operand_offset = 0;
  4685. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  4686. : loader_ctx->frame_csp;
  4687. BlockType *block_type = &block->block_type;
  4688. uint8 *return_types = NULL;
  4689. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  4690. int32 i = 0;
  4691. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  4692. *frame_offset_org = NULL;
  4693. return_count = block_type_get_result_types(block_type, &return_types);
  4694. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  4695. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  4696. if (return_count == 1) {
  4697. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  4698. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  4699. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  4700. /* insert op_copy before else opcode */
  4701. if (opcode == WASM_OP_ELSE)
  4702. skip_label();
  4703. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  4704. : EXT_OP_COPY_STACK_TOP_I64);
  4705. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  4706. emit_operand(loader_ctx, block->dynamic_offset);
  4707. if (opcode == WASM_OP_ELSE) {
  4708. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  4709. }
  4710. else {
  4711. loader_ctx->frame_offset -= cell;
  4712. loader_ctx->dynamic_offset = block->dynamic_offset;
  4713. PUSH_OFFSET_TYPE(return_types[0]);
  4714. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4715. }
  4716. if (opcode == WASM_OP_ELSE)
  4717. emit_label(opcode);
  4718. }
  4719. return true;
  4720. }
  4721. /* Copy stack top values to block's results which are in dynamic space.
  4722. * The instruction format:
  4723. * Part a: values count
  4724. * Part b: all values total cell num
  4725. * Part c: each value's cell_num, src offset and dst offset
  4726. * Part d: each value's src offset and dst offset
  4727. * Part e: each value's dst offset
  4728. */
  4729. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  4730. dynamic_offset = dynamic_offset_org =
  4731. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  4732. /* First traversal to get the count of values needed to be copied. */
  4733. for (i = (int32)return_count - 1; i >= 0; i--) {
  4734. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  4735. frame_offset -= cells;
  4736. dynamic_offset -= cells;
  4737. if (dynamic_offset != *frame_offset) {
  4738. value_count++;
  4739. total_cel_num += cells;
  4740. }
  4741. }
  4742. if (value_count) {
  4743. uint32 j = 0;
  4744. uint8 *emit_data = NULL, *cells = NULL;
  4745. int16 *src_offsets = NULL;
  4746. uint16 *dst_offsets = NULL;
  4747. uint64 size =
  4748. (uint64)value_count
  4749. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  4750. /* Allocate memory for the emit data */
  4751. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4752. return false;
  4753. cells = emit_data;
  4754. src_offsets = (int16 *)(cells + value_count);
  4755. dst_offsets = (uint16 *)(src_offsets + value_count);
  4756. /* insert op_copy before else opcode */
  4757. if (opcode == WASM_OP_ELSE)
  4758. skip_label();
  4759. emit_label(EXT_OP_COPY_STACK_VALUES);
  4760. /* Part a) */
  4761. emit_uint32(loader_ctx, value_count);
  4762. /* Part b) */
  4763. emit_uint32(loader_ctx, total_cel_num);
  4764. /* Second traversal to get each value's cell num, src offset and dst
  4765. * offset. */
  4766. frame_offset = frame_offset_org;
  4767. dynamic_offset = dynamic_offset_org;
  4768. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  4769. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  4770. frame_offset -= cell;
  4771. dynamic_offset -= cell;
  4772. if (dynamic_offset != *frame_offset) {
  4773. /* cell num */
  4774. cells[j] = cell;
  4775. /* src offset */
  4776. src_offsets[j] = *frame_offset;
  4777. /* dst offset */
  4778. dst_offsets[j] = dynamic_offset;
  4779. j++;
  4780. }
  4781. if (opcode == WASM_OP_ELSE) {
  4782. *frame_offset = dynamic_offset;
  4783. }
  4784. else {
  4785. loader_ctx->frame_offset = frame_offset;
  4786. loader_ctx->dynamic_offset = dynamic_offset;
  4787. PUSH_OFFSET_TYPE(return_types[i]);
  4788. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4789. loader_ctx->frame_offset = frame_offset_org;
  4790. loader_ctx->dynamic_offset = dynamic_offset_org;
  4791. }
  4792. }
  4793. bh_assert(j == value_count);
  4794. /* Emit the cells, src_offsets and dst_offsets */
  4795. for (j = 0; j < value_count; j++)
  4796. emit_byte(loader_ctx, cells[j]);
  4797. for (j = 0; j < value_count; j++)
  4798. emit_operand(loader_ctx, src_offsets[j]);
  4799. for (j = 0; j < value_count; j++)
  4800. emit_operand(loader_ctx, dst_offsets[j]);
  4801. if (opcode == WASM_OP_ELSE)
  4802. emit_label(opcode);
  4803. wasm_runtime_free(emit_data);
  4804. }
  4805. return true;
  4806. fail:
  4807. return false;
  4808. }
  4809. #endif /* WASM_ENABLE_FAST_INTERP */
  4810. #define RESERVE_BLOCK_RET() \
  4811. do { \
  4812. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  4813. error_buf_size)) \
  4814. goto fail; \
  4815. } while (0)
  4816. #define PUSH_TYPE(type) \
  4817. do { \
  4818. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  4819. error_buf_size))) \
  4820. goto fail; \
  4821. } while (0)
  4822. #define POP_TYPE(type) \
  4823. do { \
  4824. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  4825. error_buf_size))) \
  4826. goto fail; \
  4827. } while (0)
  4828. #define PUSH_CSP(label_type, block_type, _start_addr) \
  4829. do { \
  4830. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  4831. _start_addr, error_buf, \
  4832. error_buf_size)) \
  4833. goto fail; \
  4834. } while (0)
  4835. #define POP_CSP() \
  4836. do { \
  4837. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  4838. goto fail; \
  4839. } while (0)
  4840. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  4841. do { \
  4842. read_leb_uint32(p, p_end, local_idx); \
  4843. bh_assert(local_idx < param_count + local_count); \
  4844. local_type = local_idx < param_count \
  4845. ? param_types[local_idx] \
  4846. : local_types[local_idx - param_count]; \
  4847. local_offset = local_offsets[local_idx]; \
  4848. } while (0)
  4849. #define CHECK_MEMORY() \
  4850. do { \
  4851. bh_assert(module->import_memory_count + module->memory_count > 0); \
  4852. } while (0)
  4853. static bool
  4854. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  4855. char *error_buf, uint32 error_buf_size)
  4856. {
  4857. BranchBlock *target_block, *cur_block;
  4858. BlockType *target_block_type;
  4859. uint8 *types = NULL, *frame_ref;
  4860. uint32 arity = 0;
  4861. int32 i, available_stack_cell;
  4862. uint16 cell_num;
  4863. uint8 *frame_ref_old = loader_ctx->frame_ref;
  4864. uint8 *frame_ref_after_popped = NULL;
  4865. uint8 frame_ref_tmp[4] = { 0 };
  4866. uint8 *frame_ref_buf = frame_ref_tmp;
  4867. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  4868. #if WASM_ENABLE_FAST_INTERP != 0
  4869. int16 *frame_offset_old = loader_ctx->frame_offset;
  4870. int16 *frame_offset_after_popped = NULL;
  4871. int16 frame_offset_tmp[4] = { 0 };
  4872. int16 *frame_offset_buf = frame_offset_tmp;
  4873. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  4874. #endif
  4875. bool ret = false;
  4876. bh_assert(loader_ctx->csp_num > 0);
  4877. if (loader_ctx->csp_num - 1 < depth) {
  4878. set_error_buf(error_buf, error_buf_size,
  4879. "unknown label, "
  4880. "unexpected end of section or function");
  4881. return false;
  4882. }
  4883. cur_block = loader_ctx->frame_csp - 1;
  4884. target_block = loader_ctx->frame_csp - (depth + 1);
  4885. target_block_type = &target_block->block_type;
  4886. frame_ref = loader_ctx->frame_ref;
  4887. /* Note: loop's arity is different from if and block. loop's arity is
  4888. * its parameter count while if and block arity is result count.
  4889. */
  4890. if (target_block->label_type == LABEL_TYPE_LOOP)
  4891. arity = block_type_get_param_types(target_block_type, &types);
  4892. else
  4893. arity = block_type_get_result_types(target_block_type, &types);
  4894. /* If the stack is in polymorphic state, just clear the stack
  4895. * and then re-push the values to make the stack top values
  4896. * match block type. */
  4897. if (cur_block->is_stack_polymorphic) {
  4898. for (i = (int32)arity - 1; i >= 0; i--) {
  4899. #if WASM_ENABLE_FAST_INTERP != 0
  4900. POP_OFFSET_TYPE(types[i]);
  4901. #endif
  4902. POP_TYPE(types[i]);
  4903. }
  4904. /* Backup stack data since it may be changed in the below
  4905. push operations, and the stack data may be used when
  4906. checking other target blocks of opcode br_table */
  4907. if (opcode == WASM_OP_BR_TABLE) {
  4908. uint64 total_size;
  4909. frame_ref_after_popped = loader_ctx->frame_ref;
  4910. total_size = (uint64)sizeof(uint8)
  4911. * (frame_ref_old - frame_ref_after_popped);
  4912. if (total_size > sizeof(frame_ref_tmp)
  4913. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  4914. error_buf_size))) {
  4915. goto fail;
  4916. }
  4917. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  4918. frame_ref_after_popped, (uint32)total_size);
  4919. #if WASM_ENABLE_FAST_INTERP != 0
  4920. frame_offset_after_popped = loader_ctx->frame_offset;
  4921. total_size = (uint64)sizeof(int16)
  4922. * (frame_offset_old - frame_offset_after_popped);
  4923. if (total_size > sizeof(frame_offset_tmp)
  4924. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  4925. error_buf_size))) {
  4926. goto fail;
  4927. }
  4928. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  4929. frame_offset_after_popped, (uint32)total_size);
  4930. #endif
  4931. }
  4932. for (i = 0; i < (int32)arity; i++) {
  4933. #if WASM_ENABLE_FAST_INTERP != 0
  4934. bool disable_emit = true;
  4935. int16 operand_offset = 0;
  4936. PUSH_OFFSET_TYPE(types[i]);
  4937. #endif
  4938. PUSH_TYPE(types[i]);
  4939. }
  4940. #if WASM_ENABLE_FAST_INTERP != 0
  4941. emit_br_info(target_block, opcode == WASM_OP_BR);
  4942. #endif
  4943. /* Restore the stack data, note that frame_ref_bottom,
  4944. frame_reftype_map_bottom, frame_offset_bottom may be
  4945. re-allocated in the above push operations */
  4946. if (opcode == WASM_OP_BR_TABLE) {
  4947. uint32 total_size;
  4948. /* The stack operand num should not be smaller than before
  4949. after pop and push operations */
  4950. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  4951. loader_ctx->stack_cell_num = stack_cell_num_old;
  4952. loader_ctx->frame_ref =
  4953. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  4954. total_size = (uint32)sizeof(uint8)
  4955. * (frame_ref_old - frame_ref_after_popped);
  4956. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  4957. frame_ref_buf, total_size);
  4958. #if WASM_ENABLE_FAST_INTERP != 0
  4959. loader_ctx->frame_offset =
  4960. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  4961. total_size = (uint32)sizeof(int16)
  4962. * (frame_offset_old - frame_offset_after_popped);
  4963. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  4964. total_size, frame_offset_buf, total_size);
  4965. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  4966. #endif
  4967. }
  4968. ret = true;
  4969. goto cleanup_and_return;
  4970. }
  4971. available_stack_cell =
  4972. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4973. /* Check stack top values match target block type */
  4974. for (i = (int32)arity - 1; i >= 0; i--) {
  4975. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  4976. error_buf, error_buf_size)) {
  4977. goto fail;
  4978. }
  4979. cell_num = wasm_value_type_cell_num(types[i]);
  4980. frame_ref -= cell_num;
  4981. available_stack_cell -= cell_num;
  4982. }
  4983. #if WASM_ENABLE_FAST_INTERP != 0
  4984. emit_br_info(target_block, opcode == WASM_OP_BR);
  4985. #endif
  4986. ret = true;
  4987. cleanup_and_return:
  4988. fail:
  4989. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  4990. wasm_runtime_free(frame_ref_buf);
  4991. #if WASM_ENABLE_FAST_INTERP != 0
  4992. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  4993. wasm_runtime_free(frame_offset_buf);
  4994. #endif
  4995. return ret;
  4996. }
  4997. static BranchBlock *
  4998. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  4999. uint8 opcode, char *error_buf, uint32 error_buf_size)
  5000. {
  5001. uint8 *p = *p_buf, *p_end = buf_end;
  5002. BranchBlock *frame_csp_tmp;
  5003. uint32 depth;
  5004. read_leb_uint32(p, p_end, depth);
  5005. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  5006. error_buf_size)) {
  5007. goto fail;
  5008. }
  5009. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  5010. *p_buf = p;
  5011. return frame_csp_tmp;
  5012. fail:
  5013. return NULL;
  5014. }
  5015. static bool
  5016. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  5017. char *error_buf, uint32 error_buf_size)
  5018. {
  5019. BlockType *block_type = &block->block_type;
  5020. uint8 *return_types = NULL;
  5021. uint32 return_count = 0;
  5022. int32 available_stack_cell, return_cell_num, i;
  5023. uint8 *frame_ref = NULL;
  5024. available_stack_cell =
  5025. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  5026. return_count = block_type_get_result_types(block_type, &return_types);
  5027. return_cell_num =
  5028. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  5029. /* If the stack is in polymorphic state, just clear the stack
  5030. * and then re-push the values to make the stack top values
  5031. * match block type. */
  5032. if (block->is_stack_polymorphic) {
  5033. for (i = (int32)return_count - 1; i >= 0; i--) {
  5034. #if WASM_ENABLE_FAST_INTERP != 0
  5035. POP_OFFSET_TYPE(return_types[i]);
  5036. #endif
  5037. POP_TYPE(return_types[i]);
  5038. }
  5039. /* Check stack is empty */
  5040. bh_assert(loader_ctx->stack_cell_num == block->stack_cell_num);
  5041. for (i = 0; i < (int32)return_count; i++) {
  5042. #if WASM_ENABLE_FAST_INTERP != 0
  5043. bool disable_emit = true;
  5044. int16 operand_offset = 0;
  5045. PUSH_OFFSET_TYPE(return_types[i]);
  5046. #endif
  5047. PUSH_TYPE(return_types[i]);
  5048. }
  5049. return true;
  5050. }
  5051. /* Check stack cell num equals return cell num */
  5052. bh_assert(available_stack_cell == return_cell_num);
  5053. /* Check stack values match return types */
  5054. frame_ref = loader_ctx->frame_ref;
  5055. for (i = (int32)return_count - 1; i >= 0; i--) {
  5056. if (!check_stack_top_values(frame_ref, available_stack_cell,
  5057. return_types[i], error_buf, error_buf_size))
  5058. return false;
  5059. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  5060. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  5061. }
  5062. (void)return_cell_num;
  5063. return true;
  5064. fail:
  5065. return false;
  5066. }
  5067. #if WASM_ENABLE_FAST_INTERP != 0
  5068. /* Copy parameters to dynamic space.
  5069. * 1) POP original parameter out;
  5070. * 2) Push and copy original values to dynamic space.
  5071. * The copy instruction format:
  5072. * Part a: param count
  5073. * Part b: all param total cell num
  5074. * Part c: each param's cell_num, src offset and dst offset
  5075. * Part d: each param's src offset
  5076. * Part e: each param's dst offset
  5077. */
  5078. static bool
  5079. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  5080. uint32 error_buf_size)
  5081. {
  5082. bool ret = false;
  5083. int16 *frame_offset = NULL;
  5084. uint8 *cells = NULL, cell;
  5085. int16 *src_offsets = NULL;
  5086. uint8 *emit_data = NULL;
  5087. uint32 i;
  5088. BranchBlock *block = loader_ctx->frame_csp - 1;
  5089. BlockType *block_type = &block->block_type;
  5090. WASMFuncType *wasm_type = block_type->u.type;
  5091. uint32 param_count = block_type->u.type->param_count;
  5092. int16 condition_offset = 0;
  5093. bool disable_emit = false;
  5094. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  5095. int16 operand_offset = 0;
  5096. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  5097. bh_assert(size > 0);
  5098. /* For if block, we also need copy the condition operand offset. */
  5099. if (is_if_block)
  5100. size += sizeof(*cells) + sizeof(*src_offsets);
  5101. /* Allocate memory for the emit data */
  5102. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5103. return false;
  5104. cells = emit_data;
  5105. src_offsets = (int16 *)(cells + param_count);
  5106. if (is_if_block)
  5107. condition_offset = *loader_ctx->frame_offset;
  5108. /* POP original parameter out */
  5109. for (i = 0; i < param_count; i++) {
  5110. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  5111. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5112. }
  5113. frame_offset = loader_ctx->frame_offset;
  5114. /* Get each param's cell num and src offset */
  5115. for (i = 0; i < param_count; i++) {
  5116. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  5117. cells[i] = cell;
  5118. src_offsets[i] = *frame_offset;
  5119. frame_offset += cell;
  5120. }
  5121. /* emit copy instruction */
  5122. emit_label(EXT_OP_COPY_STACK_VALUES);
  5123. /* Part a) */
  5124. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  5125. /* Part b) */
  5126. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  5127. : wasm_type->param_cell_num);
  5128. /* Part c) */
  5129. for (i = 0; i < param_count; i++)
  5130. emit_byte(loader_ctx, cells[i]);
  5131. if (is_if_block)
  5132. emit_byte(loader_ctx, 1);
  5133. /* Part d) */
  5134. for (i = 0; i < param_count; i++)
  5135. emit_operand(loader_ctx, src_offsets[i]);
  5136. if (is_if_block)
  5137. emit_operand(loader_ctx, condition_offset);
  5138. /* Since the start offset to save the block's params and
  5139. * the start offset to save the block's results may be
  5140. * different, we remember the dynamic offset for loop block
  5141. * so that we can use it to copy the stack operands to the
  5142. * loop block's params in wasm_loader_emit_br_info. */
  5143. if (block->label_type == LABEL_TYPE_LOOP)
  5144. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  5145. /* Part e) */
  5146. /* Push to dynamic space. The push will emit the dst offset. */
  5147. for (i = 0; i < param_count; i++)
  5148. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  5149. if (is_if_block)
  5150. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  5151. ret = true;
  5152. fail:
  5153. /* Free the emit data */
  5154. wasm_runtime_free(emit_data);
  5155. return ret;
  5156. }
  5157. #endif
  5158. /* reset the stack to the state of before entering the last block */
  5159. #if WASM_ENABLE_FAST_INTERP != 0
  5160. #define RESET_STACK() \
  5161. do { \
  5162. loader_ctx->stack_cell_num = \
  5163. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5164. loader_ctx->frame_ref = \
  5165. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5166. loader_ctx->frame_offset = \
  5167. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  5168. } while (0)
  5169. #else
  5170. #define RESET_STACK() \
  5171. do { \
  5172. loader_ctx->stack_cell_num = \
  5173. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5174. loader_ctx->frame_ref = \
  5175. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5176. } while (0)
  5177. #endif
  5178. /* set current block's stack polymorphic state */
  5179. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  5180. do { \
  5181. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  5182. _cur_block->is_stack_polymorphic = flag; \
  5183. } while (0)
  5184. #define BLOCK_HAS_PARAM(block_type) \
  5185. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  5186. #define PRESERVE_LOCAL_FOR_BLOCK() \
  5187. do { \
  5188. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  5189. error_buf_size))) { \
  5190. goto fail; \
  5191. } \
  5192. } while (0)
  5193. static bool
  5194. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5195. uint32 cur_func_idx, char *error_buf,
  5196. uint32 error_buf_size)
  5197. {
  5198. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  5199. uint32 param_count, local_count, global_count;
  5200. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  5201. BlockType func_block_type;
  5202. uint16 *local_offsets, local_offset;
  5203. uint32 count, local_idx, global_idx, u32, align, i;
  5204. mem_offset_t mem_offset;
  5205. int32 i32, i32_const = 0;
  5206. int64 i64_const;
  5207. uint8 opcode, u8;
  5208. bool return_value = false;
  5209. WASMLoaderContext *loader_ctx;
  5210. BranchBlock *frame_csp_tmp;
  5211. #if WASM_ENABLE_BULK_MEMORY != 0
  5212. uint32 segment_index;
  5213. #endif
  5214. #if WASM_ENABLE_FAST_INTERP != 0
  5215. uint8 *func_const_end, *func_const = NULL;
  5216. int16 operand_offset = 0;
  5217. uint8 last_op = 0;
  5218. bool disable_emit, preserve_local = false, if_condition_available = true;
  5219. ;
  5220. float32 f32_const;
  5221. float64 f64_const;
  5222. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  5223. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  5224. #endif
  5225. #if WASM_ENABLE_MEMORY64 != 0
  5226. bool is_memory64 = has_module_memory64(module);
  5227. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  5228. #else
  5229. mem_offset_type = VALUE_TYPE_I32;
  5230. #endif
  5231. global_count = module->import_global_count + module->global_count;
  5232. param_count = func->func_type->param_count;
  5233. param_types = func->func_type->types;
  5234. func_block_type.is_value_type = false;
  5235. func_block_type.u.type = func->func_type;
  5236. local_count = func->local_count;
  5237. local_types = func->local_types;
  5238. local_offsets = func->local_offsets;
  5239. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  5240. goto fail;
  5241. }
  5242. #if WASM_ENABLE_FAST_INTERP != 0
  5243. /* For the first traverse, the initial value of preserved_local_offset has
  5244. * not been determined, we use the INT16_MAX to represent that a slot has
  5245. * been copied to preserve space. For second traverse, this field will be
  5246. * set to the appropriate value in wasm_loader_ctx_reinit.
  5247. * This is for Issue #1230,
  5248. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  5249. * drop opcodes need to know which slots are preserved, so those slots will
  5250. * not be treated as dynamically allocated slots */
  5251. loader_ctx->preserved_local_offset = INT16_MAX;
  5252. re_scan:
  5253. if (loader_ctx->code_compiled_size > 0) {
  5254. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  5255. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  5256. goto fail;
  5257. }
  5258. p = func->code;
  5259. func->code_compiled = loader_ctx->p_code_compiled;
  5260. func->code_compiled_size = loader_ctx->code_compiled_size;
  5261. }
  5262. #endif
  5263. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  5264. while (p < p_end) {
  5265. opcode = *p++;
  5266. #if WASM_ENABLE_FAST_INTERP != 0
  5267. p_org = p;
  5268. disable_emit = false;
  5269. emit_label(opcode);
  5270. #endif
  5271. switch (opcode) {
  5272. case WASM_OP_UNREACHABLE:
  5273. RESET_STACK();
  5274. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5275. break;
  5276. case WASM_OP_NOP:
  5277. #if WASM_ENABLE_FAST_INTERP != 0
  5278. skip_label();
  5279. #endif
  5280. break;
  5281. case WASM_OP_IF:
  5282. {
  5283. #if WASM_ENABLE_FAST_INTERP != 0
  5284. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  5285. int32 available_stack_cell =
  5286. (int32)(loader_ctx->stack_cell_num
  5287. - parent_block->stack_cell_num);
  5288. if (available_stack_cell <= 0
  5289. && parent_block->is_stack_polymorphic)
  5290. if_condition_available = false;
  5291. else
  5292. if_condition_available = true;
  5293. PRESERVE_LOCAL_FOR_BLOCK();
  5294. #endif
  5295. POP_I32();
  5296. goto handle_op_block_and_loop;
  5297. }
  5298. case WASM_OP_BLOCK:
  5299. case WASM_OP_LOOP:
  5300. #if WASM_ENABLE_FAST_INTERP != 0
  5301. PRESERVE_LOCAL_FOR_BLOCK();
  5302. #endif
  5303. handle_op_block_and_loop:
  5304. {
  5305. uint8 value_type;
  5306. BlockType block_type;
  5307. #if WASM_ENABLE_FAST_INTERP != 0
  5308. uint32 available_params = 0;
  5309. #endif
  5310. p_org = p - 1;
  5311. value_type = read_uint8(p);
  5312. if (is_byte_a_type(value_type)) {
  5313. /* If the first byte is one of these special values:
  5314. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  5315. * the single return value. */
  5316. block_type.is_value_type = true;
  5317. block_type.u.value_type.type = value_type;
  5318. }
  5319. else {
  5320. int32 type_index;
  5321. /* Resolve the leb128 encoded type index as block type */
  5322. p--;
  5323. read_leb_int32(p, p_end, type_index);
  5324. bh_assert((uint32)type_index < module->type_count);
  5325. block_type.is_value_type = false;
  5326. block_type.u.type = module->types[type_index];
  5327. #if WASM_ENABLE_FAST_INTERP == 0
  5328. /* If block use type index as block type, change the opcode
  5329. * to new extended opcode so that interpreter can resolve
  5330. * the block quickly.
  5331. */
  5332. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  5333. #endif
  5334. }
  5335. /* Pop block parameters from stack */
  5336. if (BLOCK_HAS_PARAM(block_type)) {
  5337. WASMFuncType *wasm_type = block_type.u.type;
  5338. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5339. #if WASM_ENABLE_FAST_INTERP != 0
  5340. uint32 cell_num;
  5341. available_params = block_type.u.type->param_count;
  5342. #endif
  5343. for (i = 0; i < block_type.u.type->param_count; i++) {
  5344. int32 available_stack_cell =
  5345. (int32)(loader_ctx->stack_cell_num
  5346. - cur_block->stack_cell_num);
  5347. if (available_stack_cell <= 0
  5348. && cur_block->is_stack_polymorphic) {
  5349. #if WASM_ENABLE_FAST_INTERP != 0
  5350. available_params = i;
  5351. #endif
  5352. break;
  5353. }
  5354. POP_TYPE(
  5355. wasm_type->types[wasm_type->param_count - i - 1]);
  5356. #if WASM_ENABLE_FAST_INTERP != 0
  5357. /* decrease the frame_offset pointer accordingly to keep
  5358. * consistent with frame_ref stack */
  5359. cell_num = wasm_value_type_cell_num(
  5360. wasm_type->types[wasm_type->param_count - i - 1]);
  5361. loader_ctx->frame_offset -= cell_num;
  5362. #endif
  5363. }
  5364. }
  5365. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  5366. block_type, p);
  5367. /* Pass parameters to block */
  5368. if (BLOCK_HAS_PARAM(block_type)) {
  5369. for (i = 0; i < block_type.u.type->param_count; i++) {
  5370. #if WASM_ENABLE_FAST_INTERP != 0
  5371. uint32 cell_num = wasm_value_type_cell_num(
  5372. block_type.u.type->types[i]);
  5373. if (i >= available_params) {
  5374. /* If there isn't enough data on stack, push a dummy
  5375. * offset to keep the stack consistent with
  5376. * frame_ref.
  5377. * Since the stack is already in polymorphic state,
  5378. * the opcode will not be executed, so the dummy
  5379. * offset won't cause any error */
  5380. uint32 n;
  5381. for (n = 0; n < cell_num; n++) {
  5382. if (loader_ctx->p_code_compiled == NULL) {
  5383. if (!check_offset_push(loader_ctx,
  5384. error_buf,
  5385. error_buf_size))
  5386. goto fail;
  5387. }
  5388. *loader_ctx->frame_offset++ = 0;
  5389. }
  5390. }
  5391. else {
  5392. loader_ctx->frame_offset += cell_num;
  5393. }
  5394. #endif
  5395. PUSH_TYPE(block_type.u.type->types[i]);
  5396. }
  5397. }
  5398. #if WASM_ENABLE_FAST_INTERP != 0
  5399. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  5400. skip_label();
  5401. if (BLOCK_HAS_PARAM(block_type)) {
  5402. /* Make sure params are in dynamic space */
  5403. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  5404. error_buf_size))
  5405. goto fail;
  5406. }
  5407. if (opcode == WASM_OP_LOOP) {
  5408. (loader_ctx->frame_csp - 1)->code_compiled =
  5409. loader_ctx->p_code_compiled;
  5410. }
  5411. }
  5412. else if (opcode == WASM_OP_IF) {
  5413. BranchBlock *block = loader_ctx->frame_csp - 1;
  5414. /* If block has parameters, we should make sure they are in
  5415. * dynamic space. Otherwise, when else branch is missing,
  5416. * the later opcode may consume incorrect operand offset.
  5417. * Spec case:
  5418. * (func (export "params-id") (param i32) (result i32)
  5419. * (i32.const 1)
  5420. * (i32.const 2)
  5421. * (if (param i32 i32) (result i32 i32) (local.get 0)
  5422. * (then)) (i32.add)
  5423. * )
  5424. *
  5425. * So we should emit a copy instruction before the if.
  5426. *
  5427. * And we also need to save the parameter offsets and
  5428. * recover them before entering else branch.
  5429. *
  5430. */
  5431. if (BLOCK_HAS_PARAM(block_type)) {
  5432. uint64 size;
  5433. /* In polymorphic state, there may be no if condition on
  5434. * the stack, so the offset may not emitted */
  5435. if (if_condition_available) {
  5436. /* skip the if condition operand offset */
  5437. wasm_loader_emit_backspace(loader_ctx,
  5438. sizeof(int16));
  5439. }
  5440. /* skip the if label */
  5441. skip_label();
  5442. /* Emit a copy instruction */
  5443. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  5444. error_buf_size))
  5445. goto fail;
  5446. /* Emit the if instruction */
  5447. emit_label(opcode);
  5448. /* Emit the new condition operand offset */
  5449. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  5450. /* Save top param_count values of frame_offset stack, so
  5451. * that we can recover it before executing else branch
  5452. */
  5453. size = sizeof(int16)
  5454. * (uint64)block_type.u.type->param_cell_num;
  5455. if (!(block->param_frame_offsets = loader_malloc(
  5456. size, error_buf, error_buf_size)))
  5457. goto fail;
  5458. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  5459. loader_ctx->frame_offset
  5460. - size / sizeof(int16),
  5461. (uint32)size);
  5462. }
  5463. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  5464. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  5465. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5466. }
  5467. #endif
  5468. break;
  5469. }
  5470. case WASM_OP_ELSE:
  5471. handle_op_else:
  5472. {
  5473. BranchBlock *block = NULL;
  5474. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  5475. bh_assert(loader_ctx->csp_num >= 2
  5476. /* the matched if is found */
  5477. && (loader_ctx->frame_csp - 1)->label_type
  5478. == LABEL_TYPE_IF
  5479. /* duplicated else isn't found */
  5480. && !(loader_ctx->frame_csp - 1)->else_addr);
  5481. block = loader_ctx->frame_csp - 1;
  5482. /* check whether if branch's stack matches its result type */
  5483. if (!check_block_stack(loader_ctx, block, error_buf,
  5484. error_buf_size))
  5485. goto fail;
  5486. block->else_addr = p - 1;
  5487. #if WASM_ENABLE_FAST_INTERP != 0
  5488. /* if the result of if branch is in local or const area, add a
  5489. * copy op */
  5490. RESERVE_BLOCK_RET();
  5491. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5492. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  5493. #endif
  5494. RESET_STACK();
  5495. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5496. /* Pass parameters to if-false branch */
  5497. if (BLOCK_HAS_PARAM(block_type)) {
  5498. for (i = 0; i < block_type.u.type->param_count; i++)
  5499. PUSH_TYPE(block_type.u.type->types[i]);
  5500. }
  5501. #if WASM_ENABLE_FAST_INTERP != 0
  5502. /* Recover top param_count values of frame_offset stack */
  5503. if (BLOCK_HAS_PARAM((block_type))) {
  5504. uint32 size;
  5505. size = sizeof(int16) * block_type.u.type->param_cell_num;
  5506. bh_memcpy_s(loader_ctx->frame_offset, size,
  5507. block->param_frame_offsets, size);
  5508. loader_ctx->frame_offset += (size / sizeof(int16));
  5509. }
  5510. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  5511. #endif
  5512. break;
  5513. }
  5514. case WASM_OP_END:
  5515. {
  5516. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5517. /* check whether block stack matches its result type */
  5518. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  5519. error_buf_size))
  5520. goto fail;
  5521. /* if there is no else branch, make a virtual else opcode for
  5522. easier integrity check and to copy the correct results to
  5523. the block return address for fast-interp mode:
  5524. change if block from `if ... end` to `if ... else end` */
  5525. if (cur_block->label_type == LABEL_TYPE_IF
  5526. && !cur_block->else_addr) {
  5527. opcode = WASM_OP_ELSE;
  5528. p--;
  5529. #if WASM_ENABLE_FAST_INTERP != 0
  5530. p_org = p;
  5531. skip_label();
  5532. disable_emit = false;
  5533. emit_label(opcode);
  5534. #endif
  5535. goto handle_op_else;
  5536. }
  5537. POP_CSP();
  5538. #if WASM_ENABLE_FAST_INTERP != 0
  5539. skip_label();
  5540. /* copy the result to the block return address */
  5541. RESERVE_BLOCK_RET();
  5542. apply_label_patch(loader_ctx, 0, PATCH_END);
  5543. free_label_patch_list(loader_ctx->frame_csp);
  5544. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  5545. int32 idx;
  5546. uint8 ret_type;
  5547. emit_label(WASM_OP_RETURN);
  5548. for (idx = (int32)func->func_type->result_count - 1;
  5549. idx >= 0; idx--) {
  5550. ret_type = *(func->func_type->types
  5551. + func->func_type->param_count + idx);
  5552. POP_OFFSET_TYPE(ret_type);
  5553. }
  5554. }
  5555. #endif
  5556. if (loader_ctx->csp_num > 0) {
  5557. loader_ctx->frame_csp->end_addr = p - 1;
  5558. }
  5559. else {
  5560. /* end of function block, function will return */
  5561. bh_assert(p == p_end);
  5562. }
  5563. break;
  5564. }
  5565. case WASM_OP_BR:
  5566. {
  5567. if (!(frame_csp_tmp =
  5568. check_branch_block(loader_ctx, &p, p_end, opcode,
  5569. error_buf, error_buf_size)))
  5570. goto fail;
  5571. RESET_STACK();
  5572. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5573. break;
  5574. }
  5575. case WASM_OP_BR_IF:
  5576. {
  5577. POP_I32();
  5578. if (!(frame_csp_tmp =
  5579. check_branch_block(loader_ctx, &p, p_end, opcode,
  5580. error_buf, error_buf_size)))
  5581. goto fail;
  5582. break;
  5583. }
  5584. case WASM_OP_BR_TABLE:
  5585. {
  5586. uint8 *ret_types = NULL;
  5587. uint32 ret_count = 0, depth = 0;
  5588. #if WASM_ENABLE_FAST_INTERP == 0
  5589. BrTableCache *br_table_cache = NULL;
  5590. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  5591. uint32 j;
  5592. #endif
  5593. read_leb_uint32(p, p_end, count);
  5594. #if WASM_ENABLE_FAST_INTERP != 0
  5595. emit_uint32(loader_ctx, count);
  5596. #endif
  5597. POP_I32();
  5598. /* Get each depth and check it */
  5599. p_org = p;
  5600. for (i = 0; i <= count; i++) {
  5601. read_leb_uint32(p, p_end, depth);
  5602. bh_assert(loader_ctx->csp_num > 0);
  5603. bh_assert(loader_ctx->csp_num - 1 >= depth);
  5604. (void)depth;
  5605. }
  5606. p = p_org;
  5607. #if WASM_ENABLE_FAST_INTERP == 0
  5608. p_depth_begin = p_depth = p;
  5609. #endif
  5610. for (i = 0; i <= count; i++) {
  5611. if (!(frame_csp_tmp =
  5612. check_branch_block(loader_ctx, &p, p_end, opcode,
  5613. error_buf, error_buf_size)))
  5614. goto fail;
  5615. #if WASM_ENABLE_FAST_INTERP == 0
  5616. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  5617. if (br_table_cache) {
  5618. br_table_cache->br_depths[i] = depth;
  5619. }
  5620. else {
  5621. if (depth > 255) {
  5622. /* The depth cannot be stored in one byte,
  5623. create br_table cache to store each depth */
  5624. if (!(br_table_cache = loader_malloc(
  5625. offsetof(BrTableCache, br_depths)
  5626. + sizeof(uint32)
  5627. * (uint64)(count + 1),
  5628. error_buf, error_buf_size))) {
  5629. goto fail;
  5630. }
  5631. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  5632. br_table_cache->br_table_op_addr = p_opcode;
  5633. br_table_cache->br_count = count;
  5634. /* Copy previous depths which are one byte */
  5635. for (j = 0; j < i; j++) {
  5636. br_table_cache->br_depths[j] = p_depth_begin[j];
  5637. }
  5638. br_table_cache->br_depths[i] = depth;
  5639. bh_list_insert(module->br_table_cache_list,
  5640. br_table_cache);
  5641. }
  5642. else {
  5643. /* The depth can be stored in one byte, use the
  5644. byte of the leb to store it */
  5645. *p_depth++ = (uint8)depth;
  5646. }
  5647. }
  5648. #endif
  5649. }
  5650. #if WASM_ENABLE_FAST_INTERP == 0
  5651. /* Set the tailing bytes to nop */
  5652. if (br_table_cache)
  5653. p_depth = p_depth_begin;
  5654. while (p_depth < p)
  5655. *p_depth++ = WASM_OP_NOP;
  5656. #endif
  5657. RESET_STACK();
  5658. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5659. (void)ret_count;
  5660. (void)ret_types;
  5661. break;
  5662. }
  5663. case WASM_OP_RETURN:
  5664. {
  5665. int32 idx;
  5666. uint8 ret_type;
  5667. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  5668. idx--) {
  5669. ret_type = *(func->func_type->types
  5670. + func->func_type->param_count + idx);
  5671. #if WASM_ENABLE_FAST_INTERP != 0
  5672. /* emit the offset after return opcode */
  5673. POP_OFFSET_TYPE(ret_type);
  5674. #endif
  5675. POP_TYPE(ret_type);
  5676. }
  5677. RESET_STACK();
  5678. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5679. break;
  5680. }
  5681. case WASM_OP_CALL:
  5682. #if WASM_ENABLE_TAIL_CALL != 0
  5683. case WASM_OP_RETURN_CALL:
  5684. #endif
  5685. {
  5686. WASMFuncType *func_type;
  5687. uint32 func_idx;
  5688. int32 idx;
  5689. read_leb_uint32(p, p_end, func_idx);
  5690. #if WASM_ENABLE_FAST_INTERP != 0
  5691. /* we need to emit func_idx before arguments */
  5692. emit_uint32(loader_ctx, func_idx);
  5693. #endif
  5694. bh_assert(func_idx < module->import_function_count
  5695. + module->function_count);
  5696. if (func_idx < module->import_function_count)
  5697. func_type =
  5698. module->import_functions[func_idx].u.function.func_type;
  5699. else
  5700. func_type = module
  5701. ->functions[func_idx
  5702. - module->import_function_count]
  5703. ->func_type;
  5704. if (func_type->param_count > 0) {
  5705. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5706. idx--) {
  5707. #if WASM_ENABLE_FAST_INTERP != 0
  5708. POP_OFFSET_TYPE(func_type->types[idx]);
  5709. #endif
  5710. POP_TYPE(func_type->types[idx]);
  5711. }
  5712. }
  5713. #if WASM_ENABLE_TAIL_CALL != 0
  5714. if (opcode == WASM_OP_CALL) {
  5715. #endif
  5716. for (i = 0; i < func_type->result_count; i++) {
  5717. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5718. #if WASM_ENABLE_FAST_INTERP != 0
  5719. /* Here we emit each return value's dynamic_offset. But
  5720. * in fact these offsets are continuous, so interpreter
  5721. * only need to get the first return value's offset.
  5722. */
  5723. PUSH_OFFSET_TYPE(
  5724. func_type->types[func_type->param_count + i]);
  5725. #endif
  5726. }
  5727. #if WASM_ENABLE_TAIL_CALL != 0
  5728. }
  5729. else {
  5730. bh_assert(func_type->result_count
  5731. == func->func_type->result_count);
  5732. for (i = 0; i < func_type->result_count; i++) {
  5733. bh_assert(
  5734. func_type->types[func_type->param_count + i]
  5735. == func->func_type
  5736. ->types[func->func_type->param_count + i]);
  5737. }
  5738. }
  5739. #endif
  5740. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  5741. || WASM_ENABLE_WAMR_COMPILER != 0
  5742. func->has_op_func_call = true;
  5743. #endif
  5744. break;
  5745. }
  5746. case WASM_OP_CALL_INDIRECT:
  5747. #if WASM_ENABLE_TAIL_CALL != 0
  5748. case WASM_OP_RETURN_CALL_INDIRECT:
  5749. #endif
  5750. {
  5751. int32 idx;
  5752. WASMFuncType *func_type;
  5753. uint32 type_idx, table_idx;
  5754. bh_assert(module->import_table_count + module->table_count > 0);
  5755. read_leb_uint32(p, p_end, type_idx);
  5756. #if WASM_ENABLE_REF_TYPES != 0
  5757. read_leb_uint32(p, p_end, table_idx);
  5758. #else
  5759. CHECK_BUF(p, p_end, 1);
  5760. table_idx = read_uint8(p);
  5761. #endif
  5762. if (!check_table_index(module, table_idx, error_buf,
  5763. error_buf_size)) {
  5764. goto fail;
  5765. }
  5766. #if WASM_ENABLE_FAST_INTERP != 0
  5767. /* we need to emit before arguments */
  5768. emit_uint32(loader_ctx, type_idx);
  5769. emit_uint32(loader_ctx, table_idx);
  5770. #endif
  5771. /* skip elem idx */
  5772. POP_I32();
  5773. bh_assert(type_idx < module->type_count);
  5774. func_type = module->types[type_idx];
  5775. if (func_type->param_count > 0) {
  5776. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5777. idx--) {
  5778. #if WASM_ENABLE_FAST_INTERP != 0
  5779. POP_OFFSET_TYPE(func_type->types[idx]);
  5780. #endif
  5781. POP_TYPE(func_type->types[idx]);
  5782. }
  5783. }
  5784. #if WASM_ENABLE_TAIL_CALL != 0
  5785. if (opcode == WASM_OP_CALL) {
  5786. #endif
  5787. for (i = 0; i < func_type->result_count; i++) {
  5788. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5789. #if WASM_ENABLE_FAST_INTERP != 0
  5790. PUSH_OFFSET_TYPE(
  5791. func_type->types[func_type->param_count + i]);
  5792. #endif
  5793. }
  5794. #if WASM_ENABLE_TAIL_CALL != 0
  5795. }
  5796. else {
  5797. bh_assert(func_type->result_count
  5798. == func->func_type->result_count);
  5799. for (i = 0; i < func_type->result_count; i++) {
  5800. bh_assert(
  5801. func_type->types[func_type->param_count + i]
  5802. == func->func_type
  5803. ->types[func->func_type->param_count + i]);
  5804. }
  5805. }
  5806. #endif
  5807. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  5808. || WASM_ENABLE_WAMR_COMPILER != 0
  5809. func->has_op_func_call = true;
  5810. #endif
  5811. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5812. func->has_op_call_indirect = true;
  5813. #endif
  5814. break;
  5815. }
  5816. #if WASM_ENABLE_EXCE_HANDLING != 0
  5817. case WASM_OP_TRY:
  5818. case WASM_OP_CATCH:
  5819. case WASM_OP_THROW:
  5820. case WASM_OP_RETHROW:
  5821. case WASM_OP_DELEGATE:
  5822. case WASM_OP_CATCH_ALL:
  5823. /* TODO */
  5824. set_error_buf(error_buf, error_buf_size, "unsupported opcode");
  5825. goto fail;
  5826. #endif
  5827. case WASM_OP_DROP:
  5828. {
  5829. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5830. int32 available_stack_cell =
  5831. (int32)(loader_ctx->stack_cell_num
  5832. - cur_block->stack_cell_num);
  5833. bh_assert(!(available_stack_cell <= 0
  5834. && !cur_block->is_stack_polymorphic));
  5835. if (available_stack_cell > 0) {
  5836. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  5837. loader_ctx->frame_ref--;
  5838. loader_ctx->stack_cell_num--;
  5839. #if WASM_ENABLE_FAST_INTERP != 0
  5840. skip_label();
  5841. loader_ctx->frame_offset--;
  5842. if ((*(loader_ctx->frame_offset)
  5843. > loader_ctx->start_dynamic_offset)
  5844. && (*(loader_ctx->frame_offset)
  5845. < loader_ctx->max_dynamic_offset))
  5846. loader_ctx->dynamic_offset--;
  5847. #endif
  5848. }
  5849. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  5850. loader_ctx->frame_ref -= 2;
  5851. loader_ctx->stack_cell_num -= 2;
  5852. #if WASM_ENABLE_FAST_INTERP == 0
  5853. *(p - 1) = WASM_OP_DROP_64;
  5854. #endif
  5855. #if WASM_ENABLE_FAST_INTERP != 0
  5856. skip_label();
  5857. loader_ctx->frame_offset -= 2;
  5858. if ((*(loader_ctx->frame_offset)
  5859. > loader_ctx->start_dynamic_offset)
  5860. && (*(loader_ctx->frame_offset)
  5861. < loader_ctx->max_dynamic_offset))
  5862. loader_ctx->dynamic_offset -= 2;
  5863. #endif
  5864. }
  5865. else {
  5866. bh_assert(0);
  5867. }
  5868. }
  5869. else {
  5870. #if WASM_ENABLE_FAST_INTERP != 0
  5871. skip_label();
  5872. #endif
  5873. }
  5874. break;
  5875. }
  5876. case WASM_OP_SELECT:
  5877. {
  5878. uint8 ref_type;
  5879. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5880. int32 available_stack_cell;
  5881. #if WASM_ENABLE_FAST_INTERP != 0
  5882. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  5883. #endif
  5884. POP_I32();
  5885. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  5886. - cur_block->stack_cell_num);
  5887. bh_assert(!(available_stack_cell <= 0
  5888. && !cur_block->is_stack_polymorphic));
  5889. if (available_stack_cell > 0) {
  5890. switch (*(loader_ctx->frame_ref - 1)) {
  5891. case REF_I32:
  5892. case REF_F32:
  5893. case REF_ANY:
  5894. break;
  5895. case REF_I64_2:
  5896. case REF_F64_2:
  5897. #if WASM_ENABLE_FAST_INTERP == 0
  5898. *(p - 1) = WASM_OP_SELECT_64;
  5899. #endif
  5900. #if WASM_ENABLE_FAST_INTERP != 0
  5901. if (loader_ctx->p_code_compiled) {
  5902. uint8 opcode_tmp = WASM_OP_SELECT_64;
  5903. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5904. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5905. *(void **)(p_code_compiled_tmp
  5906. - sizeof(void *)) =
  5907. handle_table[opcode_tmp];
  5908. #else
  5909. #if UINTPTR_MAX == UINT64_MAX
  5910. /* emit int32 relative offset in 64-bit target
  5911. */
  5912. int32 offset =
  5913. (int32)((uint8 *)handle_table[opcode_tmp]
  5914. - (uint8 *)handle_table[0]);
  5915. *(int32 *)(p_code_compiled_tmp
  5916. - sizeof(int32)) = offset;
  5917. #else
  5918. /* emit uint32 label address in 32-bit target */
  5919. *(uint32 *)(p_code_compiled_tmp
  5920. - sizeof(uint32)) =
  5921. (uint32)(uintptr_t)handle_table[opcode_tmp];
  5922. #endif
  5923. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5924. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  5925. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5926. *(p_code_compiled_tmp - 1) = opcode_tmp;
  5927. #else
  5928. *(p_code_compiled_tmp - 2) = opcode_tmp;
  5929. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5930. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  5931. }
  5932. #endif
  5933. break;
  5934. }
  5935. ref_type = *(loader_ctx->frame_ref - 1);
  5936. #if WASM_ENABLE_FAST_INTERP != 0
  5937. POP_OFFSET_TYPE(ref_type);
  5938. #endif
  5939. POP_TYPE(ref_type);
  5940. #if WASM_ENABLE_FAST_INTERP != 0
  5941. POP_OFFSET_TYPE(ref_type);
  5942. #endif
  5943. POP_TYPE(ref_type);
  5944. #if WASM_ENABLE_FAST_INTERP != 0
  5945. PUSH_OFFSET_TYPE(ref_type);
  5946. #endif
  5947. PUSH_TYPE(ref_type);
  5948. }
  5949. else {
  5950. #if WASM_ENABLE_FAST_INTERP != 0
  5951. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  5952. #endif
  5953. PUSH_TYPE(VALUE_TYPE_ANY);
  5954. }
  5955. break;
  5956. }
  5957. #if WASM_ENABLE_REF_TYPES != 0
  5958. case WASM_OP_SELECT_T:
  5959. {
  5960. uint8 vec_len, ref_type;
  5961. #if WASM_ENABLE_FAST_INTERP != 0
  5962. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  5963. #endif
  5964. read_leb_uint32(p, p_end, vec_len);
  5965. if (vec_len != 1) {
  5966. /* typed select must have exactly one result */
  5967. set_error_buf(error_buf, error_buf_size,
  5968. "invalid result arity");
  5969. goto fail;
  5970. }
  5971. CHECK_BUF(p, p_end, 1);
  5972. ref_type = read_uint8(p);
  5973. if (!is_valid_value_type_for_interpreter(ref_type)) {
  5974. set_error_buf(error_buf, error_buf_size,
  5975. "unknown value type");
  5976. goto fail;
  5977. }
  5978. POP_I32();
  5979. #if WASM_ENABLE_FAST_INTERP != 0
  5980. if (loader_ctx->p_code_compiled) {
  5981. uint8 opcode_tmp = WASM_OP_SELECT;
  5982. if (ref_type == VALUE_TYPE_F64
  5983. || ref_type == VALUE_TYPE_I64)
  5984. opcode_tmp = WASM_OP_SELECT_64;
  5985. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5986. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5987. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  5988. handle_table[opcode_tmp];
  5989. #else
  5990. #if UINTPTR_MAX == UINT64_MAX
  5991. /* emit int32 relative offset in 64-bit target */
  5992. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  5993. - (uint8 *)handle_table[0]);
  5994. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) = offset;
  5995. #else
  5996. /* emit uint32 label address in 32-bit target */
  5997. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  5998. (uint32)(uintptr_t)handle_table[opcode_tmp];
  5999. #endif
  6000. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6001. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6002. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6003. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6004. #else
  6005. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6006. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6007. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6008. }
  6009. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6010. #if WASM_ENABLE_FAST_INTERP != 0
  6011. POP_OFFSET_TYPE(ref_type);
  6012. POP_TYPE(ref_type);
  6013. POP_OFFSET_TYPE(ref_type);
  6014. POP_TYPE(ref_type);
  6015. PUSH_OFFSET_TYPE(ref_type);
  6016. PUSH_TYPE(ref_type);
  6017. #else
  6018. POP2_AND_PUSH(ref_type, ref_type);
  6019. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6020. (void)vec_len;
  6021. break;
  6022. }
  6023. /* table.get x. tables[x]. [i32] -> [t] */
  6024. /* table.set x. tables[x]. [i32 t] -> [] */
  6025. case WASM_OP_TABLE_GET:
  6026. case WASM_OP_TABLE_SET:
  6027. {
  6028. uint8 decl_ref_type;
  6029. uint32 table_idx;
  6030. read_leb_uint32(p, p_end, table_idx);
  6031. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  6032. error_buf, error_buf_size))
  6033. goto fail;
  6034. #if WASM_ENABLE_FAST_INTERP != 0
  6035. emit_uint32(loader_ctx, table_idx);
  6036. #endif
  6037. if (opcode == WASM_OP_TABLE_GET) {
  6038. POP_I32();
  6039. #if WASM_ENABLE_FAST_INTERP != 0
  6040. PUSH_OFFSET_TYPE(decl_ref_type);
  6041. #endif
  6042. PUSH_TYPE(decl_ref_type);
  6043. }
  6044. else {
  6045. #if WASM_ENABLE_FAST_INTERP != 0
  6046. POP_OFFSET_TYPE(decl_ref_type);
  6047. #endif
  6048. POP_TYPE(decl_ref_type);
  6049. POP_I32();
  6050. }
  6051. break;
  6052. }
  6053. case WASM_OP_REF_NULL:
  6054. {
  6055. uint8 ref_type;
  6056. CHECK_BUF(p, p_end, 1);
  6057. ref_type = read_uint8(p);
  6058. if (ref_type != VALUE_TYPE_FUNCREF
  6059. && ref_type != VALUE_TYPE_EXTERNREF) {
  6060. set_error_buf(error_buf, error_buf_size,
  6061. "unknown value type");
  6062. goto fail;
  6063. }
  6064. #if WASM_ENABLE_FAST_INTERP != 0
  6065. PUSH_OFFSET_TYPE(ref_type);
  6066. #endif
  6067. PUSH_TYPE(ref_type);
  6068. break;
  6069. }
  6070. case WASM_OP_REF_IS_NULL:
  6071. {
  6072. #if WASM_ENABLE_FAST_INTERP != 0
  6073. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6074. int32 block_stack_cell_num =
  6075. (int32)(loader_ctx->stack_cell_num
  6076. - cur_block->stack_cell_num);
  6077. if (block_stack_cell_num <= 0) {
  6078. if (!cur_block->is_stack_polymorphic) {
  6079. set_error_buf(
  6080. error_buf, error_buf_size,
  6081. "type mismatch: expect data but stack was empty");
  6082. goto fail;
  6083. }
  6084. }
  6085. else {
  6086. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  6087. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  6088. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  6089. if (!wasm_loader_pop_frame_ref_offset(
  6090. loader_ctx, *(loader_ctx->frame_ref - 1),
  6091. error_buf, error_buf_size)) {
  6092. goto fail;
  6093. }
  6094. }
  6095. else {
  6096. set_error_buf(error_buf, error_buf_size,
  6097. "type mismatch");
  6098. goto fail;
  6099. }
  6100. }
  6101. #else
  6102. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  6103. error_buf, error_buf_size)
  6104. && !wasm_loader_pop_frame_ref(loader_ctx,
  6105. VALUE_TYPE_EXTERNREF,
  6106. error_buf, error_buf_size)) {
  6107. goto fail;
  6108. }
  6109. #endif
  6110. PUSH_I32();
  6111. break;
  6112. }
  6113. case WASM_OP_REF_FUNC:
  6114. {
  6115. uint32 func_idx = 0;
  6116. read_leb_uint32(p, p_end, func_idx);
  6117. if (!check_function_index(module, func_idx, error_buf,
  6118. error_buf_size)) {
  6119. goto fail;
  6120. }
  6121. /* Refer to a forward-declared function:
  6122. the function must be an import, exported, or present in
  6123. a table elem segment or global initializer to be used as
  6124. the operand to ref.func */
  6125. if (func_idx >= module->import_function_count) {
  6126. WASMTableSeg *table_seg = module->table_segments;
  6127. bool func_declared = false;
  6128. uint32 j;
  6129. for (i = 0; i < module->global_count; i++) {
  6130. if (module->globals[i].type.val_type
  6131. == VALUE_TYPE_FUNCREF
  6132. && module->globals[i].init_expr.init_expr_type
  6133. == INIT_EXPR_TYPE_FUNCREF_CONST
  6134. && module->globals[i].init_expr.u.u32 == func_idx) {
  6135. func_declared = true;
  6136. break;
  6137. }
  6138. }
  6139. if (!func_declared) {
  6140. /* Check whether the function is declared in table segs,
  6141. note that it doesn't matter whether the table seg's
  6142. mode is passive, active or declarative. */
  6143. for (i = 0; i < module->table_seg_count;
  6144. i++, table_seg++) {
  6145. if (table_seg->elem_type == VALUE_TYPE_FUNCREF) {
  6146. for (j = 0; j < table_seg->value_count; j++) {
  6147. if (table_seg->init_values[j].u.ref_index
  6148. == func_idx) {
  6149. func_declared = true;
  6150. break;
  6151. }
  6152. }
  6153. }
  6154. }
  6155. }
  6156. if (!func_declared) {
  6157. /* Check whether the function is exported */
  6158. for (i = 0; i < module->export_count; i++) {
  6159. if (module->exports[i].kind == EXPORT_KIND_FUNC
  6160. && module->exports[i].index == func_idx) {
  6161. func_declared = true;
  6162. break;
  6163. }
  6164. }
  6165. }
  6166. bh_assert(func_declared);
  6167. (void)func_declared;
  6168. }
  6169. #if WASM_ENABLE_FAST_INTERP != 0
  6170. emit_uint32(loader_ctx, func_idx);
  6171. #endif
  6172. PUSH_FUNCREF();
  6173. break;
  6174. }
  6175. #endif /* WASM_ENABLE_REF_TYPES */
  6176. case WASM_OP_GET_LOCAL:
  6177. {
  6178. p_org = p - 1;
  6179. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6180. PUSH_TYPE(local_type);
  6181. #if WASM_ENABLE_FAST_INTERP != 0
  6182. /* Get Local is optimized out */
  6183. skip_label();
  6184. disable_emit = true;
  6185. operand_offset = local_offset;
  6186. PUSH_OFFSET_TYPE(local_type);
  6187. #else
  6188. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6189. && (WASM_ENABLE_FAST_JIT == 0)
  6190. if (local_offset < 0x80) {
  6191. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  6192. if (is_32bit_type(local_type))
  6193. *p_org++ = (uint8)local_offset;
  6194. else
  6195. *p_org++ = (uint8)(local_offset | 0x80);
  6196. while (p_org < p)
  6197. *p_org++ = WASM_OP_NOP;
  6198. }
  6199. #endif
  6200. #endif
  6201. break;
  6202. }
  6203. case WASM_OP_SET_LOCAL:
  6204. {
  6205. p_org = p - 1;
  6206. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6207. #if WASM_ENABLE_FAST_INTERP != 0
  6208. if (!(preserve_referenced_local(
  6209. loader_ctx, opcode, local_offset, local_type,
  6210. &preserve_local, error_buf, error_buf_size)))
  6211. goto fail;
  6212. if (local_offset < 256) {
  6213. skip_label();
  6214. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  6215. if (loader_ctx->p_code_compiled)
  6216. STORE_U16(loader_ctx->p_code_compiled - 2,
  6217. local_offset);
  6218. loader_ctx->frame_offset--;
  6219. loader_ctx->dynamic_offset--;
  6220. }
  6221. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  6222. if (loader_ctx->p_code_compiled)
  6223. STORE_U16(loader_ctx->p_code_compiled - 2,
  6224. local_offset);
  6225. loader_ctx->frame_offset -= 2;
  6226. loader_ctx->dynamic_offset -= 2;
  6227. }
  6228. else {
  6229. if (is_32bit_type(local_type)) {
  6230. emit_label(EXT_OP_SET_LOCAL_FAST);
  6231. emit_byte(loader_ctx, (uint8)local_offset);
  6232. }
  6233. else {
  6234. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  6235. emit_byte(loader_ctx, (uint8)local_offset);
  6236. }
  6237. POP_OFFSET_TYPE(local_type);
  6238. }
  6239. }
  6240. else { /* local index larger than 255, reserve leb */
  6241. emit_uint32(loader_ctx, local_idx);
  6242. POP_OFFSET_TYPE(local_type);
  6243. }
  6244. #else
  6245. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6246. && (WASM_ENABLE_FAST_JIT == 0)
  6247. if (local_offset < 0x80) {
  6248. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  6249. if (is_32bit_type(local_type))
  6250. *p_org++ = (uint8)local_offset;
  6251. else
  6252. *p_org++ = (uint8)(local_offset | 0x80);
  6253. while (p_org < p)
  6254. *p_org++ = WASM_OP_NOP;
  6255. }
  6256. #endif
  6257. #endif
  6258. POP_TYPE(local_type);
  6259. break;
  6260. }
  6261. case WASM_OP_TEE_LOCAL:
  6262. {
  6263. p_org = p - 1;
  6264. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6265. #if WASM_ENABLE_FAST_INTERP != 0
  6266. /* If the stack is in polymorphic state, do fake pop and push on
  6267. offset stack to keep the depth of offset stack to be the
  6268. same with ref stack */
  6269. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6270. if (cur_block->is_stack_polymorphic) {
  6271. POP_OFFSET_TYPE(local_type);
  6272. PUSH_OFFSET_TYPE(local_type);
  6273. }
  6274. #endif
  6275. POP_TYPE(local_type);
  6276. PUSH_TYPE(local_type);
  6277. #if WASM_ENABLE_FAST_INTERP != 0
  6278. if (!(preserve_referenced_local(
  6279. loader_ctx, opcode, local_offset, local_type,
  6280. &preserve_local, error_buf, error_buf_size)))
  6281. goto fail;
  6282. if (local_offset < 256) {
  6283. skip_label();
  6284. if (is_32bit_type(local_type)) {
  6285. emit_label(EXT_OP_TEE_LOCAL_FAST);
  6286. emit_byte(loader_ctx, (uint8)local_offset);
  6287. }
  6288. else {
  6289. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  6290. emit_byte(loader_ctx, (uint8)local_offset);
  6291. }
  6292. }
  6293. else { /* local index larger than 255, reserve leb */
  6294. emit_uint32(loader_ctx, local_idx);
  6295. }
  6296. emit_operand(loader_ctx,
  6297. *(loader_ctx->frame_offset
  6298. - wasm_value_type_cell_num(local_type)));
  6299. #else
  6300. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6301. && (WASM_ENABLE_FAST_JIT == 0)
  6302. if (local_offset < 0x80) {
  6303. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  6304. if (is_32bit_type(local_type))
  6305. *p_org++ = (uint8)local_offset;
  6306. else
  6307. *p_org++ = (uint8)(local_offset | 0x80);
  6308. while (p_org < p)
  6309. *p_org++ = WASM_OP_NOP;
  6310. }
  6311. #endif
  6312. #endif
  6313. break;
  6314. }
  6315. case WASM_OP_GET_GLOBAL:
  6316. {
  6317. p_org = p - 1;
  6318. read_leb_uint32(p, p_end, global_idx);
  6319. bh_assert(global_idx < global_count);
  6320. global_type = global_idx < module->import_global_count
  6321. ? module->import_globals[global_idx]
  6322. .u.global.type.val_type
  6323. : module
  6324. ->globals[global_idx
  6325. - module->import_global_count]
  6326. .type.val_type;
  6327. PUSH_TYPE(global_type);
  6328. #if WASM_ENABLE_FAST_INTERP == 0
  6329. if (global_type == VALUE_TYPE_I64
  6330. || global_type == VALUE_TYPE_F64) {
  6331. *p_org = WASM_OP_GET_GLOBAL_64;
  6332. }
  6333. #else /* else of WASM_ENABLE_FAST_INTERP */
  6334. if (is_64bit_type(global_type)) {
  6335. skip_label();
  6336. emit_label(WASM_OP_GET_GLOBAL_64);
  6337. }
  6338. emit_uint32(loader_ctx, global_idx);
  6339. PUSH_OFFSET_TYPE(global_type);
  6340. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6341. break;
  6342. }
  6343. case WASM_OP_SET_GLOBAL:
  6344. {
  6345. bool is_mutable = false;
  6346. p_org = p - 1;
  6347. read_leb_uint32(p, p_end, global_idx);
  6348. bh_assert(global_idx < global_count);
  6349. is_mutable = global_idx < module->import_global_count
  6350. ? module->import_globals[global_idx]
  6351. .u.global.type.is_mutable
  6352. : module
  6353. ->globals[global_idx
  6354. - module->import_global_count]
  6355. .type.is_mutable;
  6356. bh_assert(is_mutable);
  6357. global_type = global_idx < module->import_global_count
  6358. ? module->import_globals[global_idx]
  6359. .u.global.type.val_type
  6360. : module
  6361. ->globals[global_idx
  6362. - module->import_global_count]
  6363. .type.val_type;
  6364. #if WASM_ENABLE_FAST_INTERP == 0
  6365. if (is_64bit_type(global_type)) {
  6366. *p_org = WASM_OP_SET_GLOBAL_64;
  6367. }
  6368. else if (module->aux_stack_size > 0
  6369. && global_idx == module->aux_stack_top_global_index) {
  6370. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  6371. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6372. func->has_op_set_global_aux_stack = true;
  6373. #endif
  6374. }
  6375. #else /* else of WASM_ENABLE_FAST_INTERP */
  6376. if (is_64bit_type(global_type)) {
  6377. skip_label();
  6378. emit_label(WASM_OP_SET_GLOBAL_64);
  6379. }
  6380. else if (module->aux_stack_size > 0
  6381. && global_idx == module->aux_stack_top_global_index) {
  6382. skip_label();
  6383. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  6384. }
  6385. emit_uint32(loader_ctx, global_idx);
  6386. POP_OFFSET_TYPE(global_type);
  6387. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6388. POP_TYPE(global_type);
  6389. (void)is_mutable;
  6390. break;
  6391. }
  6392. /* load */
  6393. case WASM_OP_I32_LOAD:
  6394. case WASM_OP_I32_LOAD8_S:
  6395. case WASM_OP_I32_LOAD8_U:
  6396. case WASM_OP_I32_LOAD16_S:
  6397. case WASM_OP_I32_LOAD16_U:
  6398. case WASM_OP_I64_LOAD:
  6399. case WASM_OP_I64_LOAD8_S:
  6400. case WASM_OP_I64_LOAD8_U:
  6401. case WASM_OP_I64_LOAD16_S:
  6402. case WASM_OP_I64_LOAD16_U:
  6403. case WASM_OP_I64_LOAD32_S:
  6404. case WASM_OP_I64_LOAD32_U:
  6405. case WASM_OP_F32_LOAD:
  6406. case WASM_OP_F64_LOAD:
  6407. /* store */
  6408. case WASM_OP_I32_STORE:
  6409. case WASM_OP_I32_STORE8:
  6410. case WASM_OP_I32_STORE16:
  6411. case WASM_OP_I64_STORE:
  6412. case WASM_OP_I64_STORE8:
  6413. case WASM_OP_I64_STORE16:
  6414. case WASM_OP_I64_STORE32:
  6415. case WASM_OP_F32_STORE:
  6416. case WASM_OP_F64_STORE:
  6417. {
  6418. #if WASM_ENABLE_FAST_INTERP != 0
  6419. /* change F32/F64 into I32/I64 */
  6420. if (opcode == WASM_OP_F32_LOAD) {
  6421. skip_label();
  6422. emit_label(WASM_OP_I32_LOAD);
  6423. }
  6424. else if (opcode == WASM_OP_F64_LOAD) {
  6425. skip_label();
  6426. emit_label(WASM_OP_I64_LOAD);
  6427. }
  6428. else if (opcode == WASM_OP_F32_STORE) {
  6429. skip_label();
  6430. emit_label(WASM_OP_I32_STORE);
  6431. }
  6432. else if (opcode == WASM_OP_F64_STORE) {
  6433. skip_label();
  6434. emit_label(WASM_OP_I64_STORE);
  6435. }
  6436. #endif
  6437. CHECK_MEMORY();
  6438. read_leb_uint32(p, p_end, align); /* align */
  6439. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  6440. #if WASM_ENABLE_FAST_INTERP != 0
  6441. emit_uint32(loader_ctx, mem_offset);
  6442. #endif
  6443. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6444. func->has_memory_operations = true;
  6445. #endif
  6446. switch (opcode) {
  6447. /* load */
  6448. case WASM_OP_I32_LOAD:
  6449. case WASM_OP_I32_LOAD8_S:
  6450. case WASM_OP_I32_LOAD8_U:
  6451. case WASM_OP_I32_LOAD16_S:
  6452. case WASM_OP_I32_LOAD16_U:
  6453. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  6454. break;
  6455. case WASM_OP_I64_LOAD:
  6456. case WASM_OP_I64_LOAD8_S:
  6457. case WASM_OP_I64_LOAD8_U:
  6458. case WASM_OP_I64_LOAD16_S:
  6459. case WASM_OP_I64_LOAD16_U:
  6460. case WASM_OP_I64_LOAD32_S:
  6461. case WASM_OP_I64_LOAD32_U:
  6462. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  6463. break;
  6464. case WASM_OP_F32_LOAD:
  6465. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  6466. break;
  6467. case WASM_OP_F64_LOAD:
  6468. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  6469. break;
  6470. /* store */
  6471. case WASM_OP_I32_STORE:
  6472. case WASM_OP_I32_STORE8:
  6473. case WASM_OP_I32_STORE16:
  6474. POP_I32();
  6475. POP_MEM_OFFSET();
  6476. break;
  6477. case WASM_OP_I64_STORE:
  6478. case WASM_OP_I64_STORE8:
  6479. case WASM_OP_I64_STORE16:
  6480. case WASM_OP_I64_STORE32:
  6481. POP_I64();
  6482. POP_MEM_OFFSET();
  6483. break;
  6484. case WASM_OP_F32_STORE:
  6485. POP_F32();
  6486. POP_MEM_OFFSET();
  6487. break;
  6488. case WASM_OP_F64_STORE:
  6489. POP_F64();
  6490. POP_MEM_OFFSET();
  6491. break;
  6492. default:
  6493. break;
  6494. }
  6495. break;
  6496. }
  6497. case WASM_OP_MEMORY_SIZE:
  6498. CHECK_MEMORY();
  6499. /* reserved byte 0x00 */
  6500. bh_assert(*p == 0x00);
  6501. p++;
  6502. PUSH_PAGE_COUNT();
  6503. module->possible_memory_grow = true;
  6504. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6505. func->has_memory_operations = true;
  6506. #endif
  6507. break;
  6508. case WASM_OP_MEMORY_GROW:
  6509. CHECK_MEMORY();
  6510. /* reserved byte 0x00 */
  6511. bh_assert(*p == 0x00);
  6512. p++;
  6513. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  6514. module->possible_memory_grow = true;
  6515. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6516. || WASM_ENABLE_WAMR_COMPILER != 0
  6517. func->has_op_memory_grow = true;
  6518. #endif
  6519. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6520. func->has_memory_operations = true;
  6521. #endif
  6522. break;
  6523. case WASM_OP_I32_CONST:
  6524. read_leb_int32(p, p_end, i32_const);
  6525. #if WASM_ENABLE_FAST_INTERP != 0
  6526. skip_label();
  6527. disable_emit = true;
  6528. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  6529. if (operand_offset == 0) {
  6530. disable_emit = false;
  6531. emit_label(WASM_OP_I32_CONST);
  6532. emit_uint32(loader_ctx, i32_const);
  6533. }
  6534. #else
  6535. (void)i32_const;
  6536. #endif
  6537. PUSH_I32();
  6538. break;
  6539. case WASM_OP_I64_CONST:
  6540. read_leb_int64(p, p_end, i64_const);
  6541. #if WASM_ENABLE_FAST_INTERP != 0
  6542. skip_label();
  6543. disable_emit = true;
  6544. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  6545. if (operand_offset == 0) {
  6546. disable_emit = false;
  6547. emit_label(WASM_OP_I64_CONST);
  6548. emit_uint64(loader_ctx, i64_const);
  6549. }
  6550. #endif
  6551. PUSH_I64();
  6552. break;
  6553. case WASM_OP_F32_CONST:
  6554. CHECK_BUF(p, p_end, sizeof(float32));
  6555. p += sizeof(float32);
  6556. #if WASM_ENABLE_FAST_INTERP != 0
  6557. skip_label();
  6558. disable_emit = true;
  6559. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  6560. sizeof(float32));
  6561. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  6562. if (operand_offset == 0) {
  6563. disable_emit = false;
  6564. emit_label(WASM_OP_F32_CONST);
  6565. emit_float32(loader_ctx, f32_const);
  6566. }
  6567. #endif
  6568. PUSH_F32();
  6569. break;
  6570. case WASM_OP_F64_CONST:
  6571. CHECK_BUF(p, p_end, sizeof(float64));
  6572. p += sizeof(float64);
  6573. #if WASM_ENABLE_FAST_INTERP != 0
  6574. skip_label();
  6575. disable_emit = true;
  6576. /* Some MCU may require 8-byte align */
  6577. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  6578. sizeof(float64));
  6579. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  6580. if (operand_offset == 0) {
  6581. disable_emit = false;
  6582. emit_label(WASM_OP_F64_CONST);
  6583. emit_float64(loader_ctx, f64_const);
  6584. }
  6585. #endif
  6586. PUSH_F64();
  6587. break;
  6588. case WASM_OP_I32_EQZ:
  6589. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6590. break;
  6591. case WASM_OP_I32_EQ:
  6592. case WASM_OP_I32_NE:
  6593. case WASM_OP_I32_LT_S:
  6594. case WASM_OP_I32_LT_U:
  6595. case WASM_OP_I32_GT_S:
  6596. case WASM_OP_I32_GT_U:
  6597. case WASM_OP_I32_LE_S:
  6598. case WASM_OP_I32_LE_U:
  6599. case WASM_OP_I32_GE_S:
  6600. case WASM_OP_I32_GE_U:
  6601. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6602. break;
  6603. case WASM_OP_I64_EQZ:
  6604. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6605. break;
  6606. case WASM_OP_I64_EQ:
  6607. case WASM_OP_I64_NE:
  6608. case WASM_OP_I64_LT_S:
  6609. case WASM_OP_I64_LT_U:
  6610. case WASM_OP_I64_GT_S:
  6611. case WASM_OP_I64_GT_U:
  6612. case WASM_OP_I64_LE_S:
  6613. case WASM_OP_I64_LE_U:
  6614. case WASM_OP_I64_GE_S:
  6615. case WASM_OP_I64_GE_U:
  6616. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6617. break;
  6618. case WASM_OP_F32_EQ:
  6619. case WASM_OP_F32_NE:
  6620. case WASM_OP_F32_LT:
  6621. case WASM_OP_F32_GT:
  6622. case WASM_OP_F32_LE:
  6623. case WASM_OP_F32_GE:
  6624. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6625. break;
  6626. case WASM_OP_F64_EQ:
  6627. case WASM_OP_F64_NE:
  6628. case WASM_OP_F64_LT:
  6629. case WASM_OP_F64_GT:
  6630. case WASM_OP_F64_LE:
  6631. case WASM_OP_F64_GE:
  6632. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6633. break;
  6634. case WASM_OP_I32_CLZ:
  6635. case WASM_OP_I32_CTZ:
  6636. case WASM_OP_I32_POPCNT:
  6637. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6638. break;
  6639. case WASM_OP_I32_ADD:
  6640. case WASM_OP_I32_SUB:
  6641. case WASM_OP_I32_MUL:
  6642. case WASM_OP_I32_DIV_S:
  6643. case WASM_OP_I32_DIV_U:
  6644. case WASM_OP_I32_REM_S:
  6645. case WASM_OP_I32_REM_U:
  6646. case WASM_OP_I32_AND:
  6647. case WASM_OP_I32_OR:
  6648. case WASM_OP_I32_XOR:
  6649. case WASM_OP_I32_SHL:
  6650. case WASM_OP_I32_SHR_S:
  6651. case WASM_OP_I32_SHR_U:
  6652. case WASM_OP_I32_ROTL:
  6653. case WASM_OP_I32_ROTR:
  6654. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6655. break;
  6656. case WASM_OP_I64_CLZ:
  6657. case WASM_OP_I64_CTZ:
  6658. case WASM_OP_I64_POPCNT:
  6659. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6660. break;
  6661. case WASM_OP_I64_ADD:
  6662. case WASM_OP_I64_SUB:
  6663. case WASM_OP_I64_MUL:
  6664. case WASM_OP_I64_DIV_S:
  6665. case WASM_OP_I64_DIV_U:
  6666. case WASM_OP_I64_REM_S:
  6667. case WASM_OP_I64_REM_U:
  6668. case WASM_OP_I64_AND:
  6669. case WASM_OP_I64_OR:
  6670. case WASM_OP_I64_XOR:
  6671. case WASM_OP_I64_SHL:
  6672. case WASM_OP_I64_SHR_S:
  6673. case WASM_OP_I64_SHR_U:
  6674. case WASM_OP_I64_ROTL:
  6675. case WASM_OP_I64_ROTR:
  6676. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6677. break;
  6678. case WASM_OP_F32_ABS:
  6679. case WASM_OP_F32_NEG:
  6680. case WASM_OP_F32_CEIL:
  6681. case WASM_OP_F32_FLOOR:
  6682. case WASM_OP_F32_TRUNC:
  6683. case WASM_OP_F32_NEAREST:
  6684. case WASM_OP_F32_SQRT:
  6685. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6686. break;
  6687. case WASM_OP_F32_ADD:
  6688. case WASM_OP_F32_SUB:
  6689. case WASM_OP_F32_MUL:
  6690. case WASM_OP_F32_DIV:
  6691. case WASM_OP_F32_MIN:
  6692. case WASM_OP_F32_MAX:
  6693. case WASM_OP_F32_COPYSIGN:
  6694. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6695. break;
  6696. case WASM_OP_F64_ABS:
  6697. case WASM_OP_F64_NEG:
  6698. case WASM_OP_F64_CEIL:
  6699. case WASM_OP_F64_FLOOR:
  6700. case WASM_OP_F64_TRUNC:
  6701. case WASM_OP_F64_NEAREST:
  6702. case WASM_OP_F64_SQRT:
  6703. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6704. break;
  6705. case WASM_OP_F64_ADD:
  6706. case WASM_OP_F64_SUB:
  6707. case WASM_OP_F64_MUL:
  6708. case WASM_OP_F64_DIV:
  6709. case WASM_OP_F64_MIN:
  6710. case WASM_OP_F64_MAX:
  6711. case WASM_OP_F64_COPYSIGN:
  6712. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6713. break;
  6714. case WASM_OP_I32_WRAP_I64:
  6715. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6716. break;
  6717. case WASM_OP_I32_TRUNC_S_F32:
  6718. case WASM_OP_I32_TRUNC_U_F32:
  6719. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6720. break;
  6721. case WASM_OP_I32_TRUNC_S_F64:
  6722. case WASM_OP_I32_TRUNC_U_F64:
  6723. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6724. break;
  6725. case WASM_OP_I64_EXTEND_S_I32:
  6726. case WASM_OP_I64_EXTEND_U_I32:
  6727. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6728. break;
  6729. case WASM_OP_I64_TRUNC_S_F32:
  6730. case WASM_OP_I64_TRUNC_U_F32:
  6731. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6732. break;
  6733. case WASM_OP_I64_TRUNC_S_F64:
  6734. case WASM_OP_I64_TRUNC_U_F64:
  6735. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6736. break;
  6737. case WASM_OP_F32_CONVERT_S_I32:
  6738. case WASM_OP_F32_CONVERT_U_I32:
  6739. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6740. break;
  6741. case WASM_OP_F32_CONVERT_S_I64:
  6742. case WASM_OP_F32_CONVERT_U_I64:
  6743. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  6744. break;
  6745. case WASM_OP_F32_DEMOTE_F64:
  6746. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  6747. break;
  6748. case WASM_OP_F64_CONVERT_S_I32:
  6749. case WASM_OP_F64_CONVERT_U_I32:
  6750. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  6751. break;
  6752. case WASM_OP_F64_CONVERT_S_I64:
  6753. case WASM_OP_F64_CONVERT_U_I64:
  6754. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6755. break;
  6756. case WASM_OP_F64_PROMOTE_F32:
  6757. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  6758. break;
  6759. case WASM_OP_I32_REINTERPRET_F32:
  6760. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6761. break;
  6762. case WASM_OP_I64_REINTERPRET_F64:
  6763. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6764. break;
  6765. case WASM_OP_F32_REINTERPRET_I32:
  6766. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6767. break;
  6768. case WASM_OP_F64_REINTERPRET_I64:
  6769. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6770. break;
  6771. case WASM_OP_I32_EXTEND8_S:
  6772. case WASM_OP_I32_EXTEND16_S:
  6773. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6774. break;
  6775. case WASM_OP_I64_EXTEND8_S:
  6776. case WASM_OP_I64_EXTEND16_S:
  6777. case WASM_OP_I64_EXTEND32_S:
  6778. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6779. break;
  6780. case WASM_OP_MISC_PREFIX:
  6781. {
  6782. uint32 opcode1;
  6783. read_leb_uint32(p, p_end, opcode1);
  6784. #if WASM_ENABLE_FAST_INTERP != 0
  6785. emit_byte(loader_ctx, ((uint8)opcode1));
  6786. #endif
  6787. switch (opcode1) {
  6788. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6789. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6790. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6791. break;
  6792. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6793. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6794. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6795. break;
  6796. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6797. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6798. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6799. break;
  6800. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6801. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6802. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6803. break;
  6804. #if WASM_ENABLE_BULK_MEMORY != 0
  6805. case WASM_OP_MEMORY_INIT:
  6806. {
  6807. read_leb_uint32(p, p_end, segment_index);
  6808. #if WASM_ENABLE_FAST_INTERP != 0
  6809. emit_uint32(loader_ctx, segment_index);
  6810. #endif
  6811. bh_assert(module->import_memory_count
  6812. + module->memory_count
  6813. > 0);
  6814. bh_assert(*p == 0x00);
  6815. p++;
  6816. bh_assert(segment_index < module->data_seg_count);
  6817. bh_assert(module->data_seg_count1 > 0);
  6818. POP_I32();
  6819. POP_I32();
  6820. POP_MEM_OFFSET();
  6821. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6822. func->has_memory_operations = true;
  6823. #endif
  6824. break;
  6825. }
  6826. case WASM_OP_DATA_DROP:
  6827. {
  6828. read_leb_uint32(p, p_end, segment_index);
  6829. #if WASM_ENABLE_FAST_INTERP != 0
  6830. emit_uint32(loader_ctx, segment_index);
  6831. #endif
  6832. bh_assert(segment_index < module->data_seg_count);
  6833. bh_assert(module->data_seg_count1 > 0);
  6834. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6835. func->has_memory_operations = true;
  6836. #endif
  6837. break;
  6838. }
  6839. case WASM_OP_MEMORY_COPY:
  6840. {
  6841. CHECK_BUF(p, p_end, sizeof(int16));
  6842. /* both src and dst memory index should be 0 */
  6843. bh_assert(*(int16 *)p == 0x0000);
  6844. p += 2;
  6845. bh_assert(module->import_memory_count
  6846. + module->memory_count
  6847. > 0);
  6848. POP_MEM_OFFSET();
  6849. POP_MEM_OFFSET();
  6850. POP_MEM_OFFSET();
  6851. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6852. func->has_memory_operations = true;
  6853. #endif
  6854. break;
  6855. }
  6856. case WASM_OP_MEMORY_FILL:
  6857. {
  6858. bh_assert(*p == 0);
  6859. p++;
  6860. bh_assert(module->import_memory_count
  6861. + module->memory_count
  6862. > 0);
  6863. POP_MEM_OFFSET();
  6864. POP_I32();
  6865. POP_MEM_OFFSET();
  6866. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6867. func->has_memory_operations = true;
  6868. #endif
  6869. break;
  6870. }
  6871. #endif /* WASM_ENABLE_BULK_MEMORY */
  6872. #if WASM_ENABLE_REF_TYPES != 0
  6873. case WASM_OP_TABLE_INIT:
  6874. {
  6875. uint8 seg_ref_type, tbl_ref_type;
  6876. uint32 table_seg_idx, table_idx;
  6877. read_leb_uint32(p, p_end, table_seg_idx);
  6878. read_leb_uint32(p, p_end, table_idx);
  6879. if (!get_table_elem_type(module, table_idx,
  6880. &tbl_ref_type, error_buf,
  6881. error_buf_size))
  6882. goto fail;
  6883. if (!get_table_seg_elem_type(module, table_seg_idx,
  6884. &seg_ref_type, error_buf,
  6885. error_buf_size))
  6886. goto fail;
  6887. if (seg_ref_type != tbl_ref_type) {
  6888. set_error_buf(error_buf, error_buf_size,
  6889. "type mismatch");
  6890. goto fail;
  6891. }
  6892. #if WASM_ENABLE_FAST_INTERP != 0
  6893. emit_uint32(loader_ctx, table_seg_idx);
  6894. emit_uint32(loader_ctx, table_idx);
  6895. #endif
  6896. POP_I32();
  6897. POP_I32();
  6898. POP_I32();
  6899. break;
  6900. }
  6901. case WASM_OP_ELEM_DROP:
  6902. {
  6903. uint32 table_seg_idx;
  6904. read_leb_uint32(p, p_end, table_seg_idx);
  6905. if (!get_table_seg_elem_type(module, table_seg_idx,
  6906. NULL, error_buf,
  6907. error_buf_size))
  6908. goto fail;
  6909. #if WASM_ENABLE_FAST_INTERP != 0
  6910. emit_uint32(loader_ctx, table_seg_idx);
  6911. #endif
  6912. break;
  6913. }
  6914. case WASM_OP_TABLE_COPY:
  6915. {
  6916. uint8 src_ref_type, dst_ref_type;
  6917. uint32 src_tbl_idx, dst_tbl_idx;
  6918. read_leb_uint32(p, p_end, src_tbl_idx);
  6919. if (!get_table_elem_type(module, src_tbl_idx,
  6920. &src_ref_type, error_buf,
  6921. error_buf_size))
  6922. goto fail;
  6923. read_leb_uint32(p, p_end, dst_tbl_idx);
  6924. if (!get_table_elem_type(module, dst_tbl_idx,
  6925. &dst_ref_type, error_buf,
  6926. error_buf_size))
  6927. goto fail;
  6928. if (src_ref_type != dst_ref_type) {
  6929. set_error_buf(error_buf, error_buf_size,
  6930. "type mismatch");
  6931. goto fail;
  6932. }
  6933. #if WASM_ENABLE_FAST_INTERP != 0
  6934. emit_uint32(loader_ctx, src_tbl_idx);
  6935. emit_uint32(loader_ctx, dst_tbl_idx);
  6936. #endif
  6937. POP_I32();
  6938. POP_I32();
  6939. POP_I32();
  6940. break;
  6941. }
  6942. case WASM_OP_TABLE_SIZE:
  6943. {
  6944. uint32 table_idx;
  6945. read_leb_uint32(p, p_end, table_idx);
  6946. /* TODO: shall we create a new function to check
  6947. table idx instead of using below function? */
  6948. if (!get_table_elem_type(module, table_idx, NULL,
  6949. error_buf, error_buf_size))
  6950. goto fail;
  6951. #if WASM_ENABLE_FAST_INTERP != 0
  6952. emit_uint32(loader_ctx, table_idx);
  6953. #endif
  6954. PUSH_I32();
  6955. break;
  6956. }
  6957. case WASM_OP_TABLE_GROW:
  6958. case WASM_OP_TABLE_FILL:
  6959. {
  6960. uint8 decl_ref_type;
  6961. uint32 table_idx;
  6962. read_leb_uint32(p, p_end, table_idx);
  6963. if (!get_table_elem_type(module, table_idx,
  6964. &decl_ref_type, error_buf,
  6965. error_buf_size))
  6966. goto fail;
  6967. if (opcode1 == WASM_OP_TABLE_GROW) {
  6968. if (table_idx < module->import_table_count) {
  6969. module->import_tables[table_idx]
  6970. .u.table.table_type.possible_grow = true;
  6971. }
  6972. else {
  6973. module
  6974. ->tables[table_idx
  6975. - module->import_table_count]
  6976. .table_type.possible_grow = true;
  6977. }
  6978. }
  6979. #if WASM_ENABLE_FAST_INTERP != 0
  6980. emit_uint32(loader_ctx, table_idx);
  6981. #endif
  6982. POP_I32();
  6983. #if WASM_ENABLE_FAST_INTERP != 0
  6984. POP_OFFSET_TYPE(decl_ref_type);
  6985. #endif
  6986. POP_TYPE(decl_ref_type);
  6987. if (opcode1 == WASM_OP_TABLE_GROW)
  6988. PUSH_I32();
  6989. else
  6990. POP_I32();
  6991. break;
  6992. }
  6993. #endif /* WASM_ENABLE_REF_TYPES */
  6994. default:
  6995. bh_assert(0);
  6996. break;
  6997. }
  6998. break;
  6999. }
  7000. #if WASM_ENABLE_SHARED_MEMORY != 0
  7001. case WASM_OP_ATOMIC_PREFIX:
  7002. {
  7003. uint32 opcode1;
  7004. read_leb_uint32(p, p_end, opcode1);
  7005. #if WASM_ENABLE_FAST_INTERP != 0
  7006. emit_byte(loader_ctx, opcode1);
  7007. #endif
  7008. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  7009. CHECK_MEMORY();
  7010. read_leb_uint32(p, p_end, align); /* align */
  7011. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  7012. #if WASM_ENABLE_FAST_INTERP != 0
  7013. emit_uint32(loader_ctx, mem_offset);
  7014. #endif
  7015. }
  7016. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7017. func->has_memory_operations = true;
  7018. #endif
  7019. switch (opcode1) {
  7020. case WASM_OP_ATOMIC_NOTIFY:
  7021. POP_I32();
  7022. POP_MEM_OFFSET();
  7023. PUSH_I32();
  7024. break;
  7025. case WASM_OP_ATOMIC_WAIT32:
  7026. POP_I64();
  7027. POP_I32();
  7028. POP_MEM_OFFSET();
  7029. PUSH_I32();
  7030. break;
  7031. case WASM_OP_ATOMIC_WAIT64:
  7032. POP_I64();
  7033. POP_I64();
  7034. POP_MEM_OFFSET();
  7035. PUSH_I32();
  7036. break;
  7037. case WASM_OP_ATOMIC_FENCE:
  7038. /* reserved byte 0x00 */
  7039. bh_assert(*p == 0x00);
  7040. p++;
  7041. break;
  7042. case WASM_OP_ATOMIC_I32_LOAD:
  7043. case WASM_OP_ATOMIC_I32_LOAD8_U:
  7044. case WASM_OP_ATOMIC_I32_LOAD16_U:
  7045. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  7046. break;
  7047. case WASM_OP_ATOMIC_I32_STORE:
  7048. case WASM_OP_ATOMIC_I32_STORE8:
  7049. case WASM_OP_ATOMIC_I32_STORE16:
  7050. POP_I32();
  7051. POP_MEM_OFFSET();
  7052. break;
  7053. case WASM_OP_ATOMIC_I64_LOAD:
  7054. case WASM_OP_ATOMIC_I64_LOAD8_U:
  7055. case WASM_OP_ATOMIC_I64_LOAD16_U:
  7056. case WASM_OP_ATOMIC_I64_LOAD32_U:
  7057. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  7058. break;
  7059. case WASM_OP_ATOMIC_I64_STORE:
  7060. case WASM_OP_ATOMIC_I64_STORE8:
  7061. case WASM_OP_ATOMIC_I64_STORE16:
  7062. case WASM_OP_ATOMIC_I64_STORE32:
  7063. POP_I64();
  7064. POP_MEM_OFFSET();
  7065. break;
  7066. case WASM_OP_ATOMIC_RMW_I32_ADD:
  7067. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  7068. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  7069. case WASM_OP_ATOMIC_RMW_I32_SUB:
  7070. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  7071. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  7072. case WASM_OP_ATOMIC_RMW_I32_AND:
  7073. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  7074. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  7075. case WASM_OP_ATOMIC_RMW_I32_OR:
  7076. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  7077. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  7078. case WASM_OP_ATOMIC_RMW_I32_XOR:
  7079. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  7080. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  7081. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  7082. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  7083. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  7084. POP_I32();
  7085. POP_MEM_OFFSET();
  7086. PUSH_I32();
  7087. break;
  7088. case WASM_OP_ATOMIC_RMW_I64_ADD:
  7089. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  7090. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  7091. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  7092. case WASM_OP_ATOMIC_RMW_I64_SUB:
  7093. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  7094. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  7095. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  7096. case WASM_OP_ATOMIC_RMW_I64_AND:
  7097. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  7098. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  7099. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  7100. case WASM_OP_ATOMIC_RMW_I64_OR:
  7101. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  7102. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  7103. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  7104. case WASM_OP_ATOMIC_RMW_I64_XOR:
  7105. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  7106. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  7107. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  7108. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  7109. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  7110. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  7111. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  7112. POP_I64();
  7113. POP_MEM_OFFSET();
  7114. PUSH_I64();
  7115. break;
  7116. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  7117. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  7118. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  7119. POP_I32();
  7120. POP_I32();
  7121. POP_MEM_OFFSET();
  7122. PUSH_I32();
  7123. break;
  7124. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  7125. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  7126. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  7127. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  7128. POP_I64();
  7129. POP_I64();
  7130. POP_MEM_OFFSET();
  7131. PUSH_I64();
  7132. break;
  7133. default:
  7134. bh_assert(0);
  7135. break;
  7136. }
  7137. break;
  7138. }
  7139. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  7140. default:
  7141. bh_assert(0);
  7142. break;
  7143. }
  7144. #if WASM_ENABLE_FAST_INTERP != 0
  7145. last_op = opcode;
  7146. #endif
  7147. }
  7148. if (loader_ctx->csp_num > 0) {
  7149. set_error_buf(error_buf, error_buf_size,
  7150. "function body must end with END opcode");
  7151. goto fail;
  7152. }
  7153. #if WASM_ENABLE_FAST_INTERP != 0
  7154. if (loader_ctx->p_code_compiled == NULL)
  7155. goto re_scan;
  7156. func->const_cell_num = loader_ctx->const_cell_num;
  7157. if (func->const_cell_num > 0) {
  7158. int32 j;
  7159. if (!(func->consts = func_const = loader_malloc(
  7160. func->const_cell_num * 4, error_buf, error_buf_size)))
  7161. goto fail;
  7162. func_const_end = func->consts + func->const_cell_num * 4;
  7163. /* reverse the const buf */
  7164. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  7165. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  7166. if (c->value_type == VALUE_TYPE_F64
  7167. || c->value_type == VALUE_TYPE_I64) {
  7168. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  7169. &(c->value.f64), (uint32)sizeof(int64));
  7170. func_const += sizeof(int64);
  7171. }
  7172. else {
  7173. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  7174. &(c->value.f32), (uint32)sizeof(int32));
  7175. func_const += sizeof(int32);
  7176. }
  7177. }
  7178. }
  7179. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  7180. - loader_ctx->start_dynamic_offset + 1;
  7181. #else
  7182. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  7183. #endif
  7184. func->max_block_num = loader_ctx->max_csp_num;
  7185. return_value = true;
  7186. fail:
  7187. wasm_loader_ctx_destroy(loader_ctx);
  7188. (void)u8;
  7189. (void)u32;
  7190. (void)i32;
  7191. (void)i64_const;
  7192. (void)global_count;
  7193. (void)local_count;
  7194. (void)local_offset;
  7195. (void)p_org;
  7196. (void)mem_offset;
  7197. (void)align;
  7198. #if WASM_ENABLE_BULK_MEMORY != 0
  7199. (void)segment_index;
  7200. #endif
  7201. return return_value;
  7202. }