wasm_mini_loader.c 283 KB

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