wasm_mini_loader.c 247 KB

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