wasm_mini_loader.c 239 KB

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