wasm_loader.c 360 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_DEBUG_INTERP != 0
  14. #include "../libraries/debug-engine/debug_engine.h"
  15. #endif
  16. #if WASM_ENABLE_FAST_JIT != 0
  17. #include "../fast-jit/jit_compiler.h"
  18. #include "../fast-jit/jit_codecache.h"
  19. #endif
  20. #if WASM_ENABLE_JIT != 0
  21. #include "../compilation/aot_llvm.h"
  22. #endif
  23. /* Read a value of given type from the address pointed to by the given
  24. pointer and increase the pointer to the position just after the
  25. value being read. */
  26. #define TEMPLATE_READ_VALUE(Type, p) \
  27. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  28. static void
  29. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  30. {
  31. if (error_buf != NULL) {
  32. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  33. string);
  34. }
  35. }
  36. static void
  37. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  38. {
  39. va_list args;
  40. char buf[128];
  41. if (error_buf != NULL) {
  42. va_start(args, format);
  43. vsnprintf(buf, sizeof(buf), format, args);
  44. va_end(args);
  45. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  46. }
  47. }
  48. static bool
  49. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  50. char *error_buf, uint32 error_buf_size)
  51. {
  52. if ((uintptr_t)buf + length < (uintptr_t)buf
  53. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  54. set_error_buf(error_buf, error_buf_size,
  55. "unexpected end of section or function");
  56. return false;
  57. }
  58. return true;
  59. }
  60. static bool
  61. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  62. char *error_buf, uint32 error_buf_size)
  63. {
  64. if ((uintptr_t)buf + length < (uintptr_t)buf
  65. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  66. set_error_buf(error_buf, error_buf_size, "unexpected end");
  67. return false;
  68. }
  69. return true;
  70. }
  71. #define CHECK_BUF(buf, buf_end, length) \
  72. do { \
  73. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  74. goto fail; \
  75. } \
  76. } while (0)
  77. #define CHECK_BUF1(buf, buf_end, length) \
  78. do { \
  79. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  80. goto fail; \
  81. } \
  82. } while (0)
  83. #define skip_leb(p) while (*p++ & 0x80)
  84. #define skip_leb_int64(p, p_end) skip_leb(p)
  85. #define skip_leb_uint32(p, p_end) skip_leb(p)
  86. #define skip_leb_int32(p, p_end) skip_leb(p)
  87. static bool
  88. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  89. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  90. {
  91. const uint8 *buf = *p_buf;
  92. uint64 result = 0;
  93. uint32 shift = 0;
  94. uint32 offset = 0, bcnt = 0;
  95. uint64 byte;
  96. while (true) {
  97. /* uN or SN must not exceed ceil(N/7) bytes */
  98. if (bcnt + 1 > (maxbits + 6) / 7) {
  99. set_error_buf(error_buf, error_buf_size,
  100. "integer representation too long");
  101. return false;
  102. }
  103. CHECK_BUF(buf, buf_end, offset + 1);
  104. byte = buf[offset];
  105. offset += 1;
  106. result |= ((byte & 0x7f) << shift);
  107. shift += 7;
  108. bcnt += 1;
  109. if ((byte & 0x80) == 0) {
  110. break;
  111. }
  112. }
  113. if (!sign && maxbits == 32 && shift >= maxbits) {
  114. /* The top bits set represent values > 32 bits */
  115. if (((uint8)byte) & 0xf0)
  116. goto fail_integer_too_large;
  117. }
  118. else if (sign && maxbits == 32) {
  119. if (shift < maxbits) {
  120. /* Sign extend, second highest bit is the sign bit */
  121. if ((uint8)byte & 0x40)
  122. result |= (~((uint64)0)) << shift;
  123. }
  124. else {
  125. /* The top bits should be a sign-extension of the sign bit */
  126. bool sign_bit_set = ((uint8)byte) & 0x8;
  127. int top_bits = ((uint8)byte) & 0xf0;
  128. if ((sign_bit_set && top_bits != 0x70)
  129. || (!sign_bit_set && top_bits != 0))
  130. goto fail_integer_too_large;
  131. }
  132. }
  133. else if (sign && maxbits == 64) {
  134. if (shift < maxbits) {
  135. /* Sign extend, second highest bit is the sign bit */
  136. if ((uint8)byte & 0x40)
  137. result |= (~((uint64)0)) << shift;
  138. }
  139. else {
  140. /* The top bits should be a sign-extension of the sign bit */
  141. bool sign_bit_set = ((uint8)byte) & 0x1;
  142. int top_bits = ((uint8)byte) & 0xfe;
  143. if ((sign_bit_set && top_bits != 0x7e)
  144. || (!sign_bit_set && top_bits != 0))
  145. goto fail_integer_too_large;
  146. }
  147. }
  148. *p_buf += offset;
  149. *p_result = result;
  150. return true;
  151. fail_integer_too_large:
  152. set_error_buf(error_buf, error_buf_size, "integer too large");
  153. fail:
  154. return false;
  155. }
  156. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  157. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  158. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  159. #define read_leb_int64(p, p_end, res) \
  160. do { \
  161. uint64 res64; \
  162. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  163. error_buf_size)) \
  164. goto fail; \
  165. res = (int64)res64; \
  166. } while (0)
  167. #define read_leb_uint32(p, p_end, res) \
  168. do { \
  169. uint64 res64; \
  170. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  171. error_buf_size)) \
  172. goto fail; \
  173. res = (uint32)res64; \
  174. } while (0)
  175. #define read_leb_int32(p, p_end, res) \
  176. do { \
  177. uint64 res64; \
  178. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  179. error_buf_size)) \
  180. goto fail; \
  181. res = (int32)res64; \
  182. } while (0)
  183. static char *
  184. type2str(uint8 type)
  185. {
  186. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  187. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  188. return type_str[type - VALUE_TYPE_V128];
  189. else if (type == VALUE_TYPE_FUNCREF)
  190. return "funcref";
  191. else if (type == VALUE_TYPE_EXTERNREF)
  192. return "externref";
  193. else
  194. return "unknown type";
  195. }
  196. static bool
  197. is_32bit_type(uint8 type)
  198. {
  199. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  200. #if WASM_ENABLE_REF_TYPES != 0
  201. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  202. #endif
  203. )
  204. return true;
  205. return false;
  206. }
  207. static bool
  208. is_64bit_type(uint8 type)
  209. {
  210. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  211. return true;
  212. return false;
  213. }
  214. static bool
  215. is_value_type(uint8 type)
  216. {
  217. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
  218. || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
  219. #if WASM_ENABLE_REF_TYPES != 0
  220. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  221. #endif
  222. #if WASM_ENABLE_SIMD != 0
  223. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  224. || type == VALUE_TYPE_V128
  225. #endif
  226. #endif
  227. )
  228. return true;
  229. return false;
  230. }
  231. static bool
  232. is_byte_a_type(uint8 type)
  233. {
  234. return is_value_type(type) || (type == VALUE_TYPE_VOID);
  235. }
  236. #if WASM_ENABLE_SIMD != 0
  237. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  238. static V128
  239. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  240. {
  241. V128 result;
  242. uint8 i;
  243. for (i = 0; i != 16; ++i) {
  244. result.i8x16[i] = read_uint8(p_buf);
  245. }
  246. return result;
  247. }
  248. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  249. #endif /* end of WASM_ENABLE_SIMD */
  250. static void *
  251. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  252. {
  253. void *mem;
  254. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  255. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  256. return NULL;
  257. }
  258. memset(mem, 0, (uint32)size);
  259. return mem;
  260. }
  261. static bool
  262. check_utf8_str(const uint8 *str, uint32 len)
  263. {
  264. /* The valid ranges are taken from page 125, below link
  265. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  266. const uint8 *p = str, *p_end = str + len;
  267. uint8 chr;
  268. while (p < p_end) {
  269. chr = *p;
  270. if (chr < 0x80) {
  271. p++;
  272. }
  273. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  274. if (p[1] < 0x80 || p[1] > 0xBF) {
  275. return false;
  276. }
  277. p += 2;
  278. }
  279. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  280. if (chr == 0xE0) {
  281. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  282. return false;
  283. }
  284. }
  285. else if (chr == 0xED) {
  286. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  287. return false;
  288. }
  289. }
  290. else if (chr >= 0xE1 && chr <= 0xEF) {
  291. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  292. return false;
  293. }
  294. }
  295. p += 3;
  296. }
  297. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  298. if (chr == 0xF0) {
  299. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  300. || p[3] < 0x80 || p[3] > 0xBF) {
  301. return false;
  302. }
  303. }
  304. else if (chr >= 0xF1 && chr <= 0xF3) {
  305. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  306. || p[3] < 0x80 || p[3] > 0xBF) {
  307. return false;
  308. }
  309. }
  310. else if (chr == 0xF4) {
  311. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  312. || p[3] < 0x80 || p[3] > 0xBF) {
  313. return false;
  314. }
  315. }
  316. p += 4;
  317. }
  318. else {
  319. return false;
  320. }
  321. }
  322. return (p == p_end);
  323. }
  324. static char *
  325. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  326. bool is_load_from_file_buf, char *error_buf,
  327. uint32 error_buf_size)
  328. {
  329. StringNode *node, *node_next;
  330. if (!check_utf8_str(str, len)) {
  331. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  332. return NULL;
  333. }
  334. if (len == 0) {
  335. return "";
  336. }
  337. else if (is_load_from_file_buf) {
  338. /* As the file buffer can be referred to after loading, we use
  339. the previous byte of leb encoded size to adjust the string:
  340. move string 1 byte backward and then append '\0' */
  341. char *c_str = (char *)str - 1;
  342. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  343. c_str[len] = '\0';
  344. return c_str;
  345. }
  346. /* Search const str list */
  347. node = module->const_str_list;
  348. while (node) {
  349. node_next = node->next;
  350. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  351. break;
  352. node = node_next;
  353. }
  354. if (node) {
  355. return node->str;
  356. }
  357. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  358. error_buf_size))) {
  359. return NULL;
  360. }
  361. node->str = ((char *)node) + sizeof(StringNode);
  362. bh_memcpy_s(node->str, len + 1, str, len);
  363. node->str[len] = '\0';
  364. if (!module->const_str_list) {
  365. /* set as head */
  366. module->const_str_list = node;
  367. node->next = NULL;
  368. }
  369. else {
  370. /* insert it */
  371. node->next = module->const_str_list;
  372. module->const_str_list = node;
  373. }
  374. return node->str;
  375. }
  376. static void
  377. destroy_wasm_type(WASMType *type)
  378. {
  379. if (type->ref_count > 1) {
  380. /* The type is referenced by other types
  381. of current wasm module */
  382. type->ref_count--;
  383. return;
  384. }
  385. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  386. && WASM_ENABLE_LAZY_JIT != 0
  387. if (type->call_to_llvm_jit_from_fast_jit)
  388. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  389. #endif
  390. wasm_runtime_free(type);
  391. }
  392. static bool
  393. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  394. InitializerExpression *init_expr, uint8 type, char *error_buf,
  395. uint32 error_buf_size)
  396. {
  397. const uint8 *p = *p_buf, *p_end = buf_end;
  398. uint8 flag, end_byte, *p_float;
  399. uint32 i;
  400. CHECK_BUF(p, p_end, 1);
  401. init_expr->init_expr_type = read_uint8(p);
  402. flag = init_expr->init_expr_type;
  403. switch (flag) {
  404. /* i32.const */
  405. case INIT_EXPR_TYPE_I32_CONST:
  406. if (type != VALUE_TYPE_I32)
  407. goto fail_type_mismatch;
  408. read_leb_int32(p, p_end, init_expr->u.i32);
  409. break;
  410. /* i64.const */
  411. case INIT_EXPR_TYPE_I64_CONST:
  412. if (type != VALUE_TYPE_I64)
  413. goto fail_type_mismatch;
  414. read_leb_int64(p, p_end, init_expr->u.i64);
  415. break;
  416. /* f32.const */
  417. case INIT_EXPR_TYPE_F32_CONST:
  418. if (type != VALUE_TYPE_F32)
  419. goto fail_type_mismatch;
  420. CHECK_BUF(p, p_end, 4);
  421. p_float = (uint8 *)&init_expr->u.f32;
  422. for (i = 0; i < sizeof(float32); i++)
  423. *p_float++ = *p++;
  424. break;
  425. /* f64.const */
  426. case INIT_EXPR_TYPE_F64_CONST:
  427. if (type != VALUE_TYPE_F64)
  428. goto fail_type_mismatch;
  429. CHECK_BUF(p, p_end, 8);
  430. p_float = (uint8 *)&init_expr->u.f64;
  431. for (i = 0; i < sizeof(float64); i++)
  432. *p_float++ = *p++;
  433. break;
  434. #if WASM_ENABLE_SIMD != 0
  435. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  436. case INIT_EXPR_TYPE_V128_CONST:
  437. {
  438. uint64 high, low;
  439. if (type != VALUE_TYPE_V128)
  440. goto fail_type_mismatch;
  441. flag = read_uint8(p);
  442. (void)flag;
  443. CHECK_BUF(p, p_end, 16);
  444. wasm_runtime_read_v128(p, &high, &low);
  445. p += 16;
  446. init_expr->u.v128.i64x2[0] = high;
  447. init_expr->u.v128.i64x2[1] = low;
  448. break;
  449. }
  450. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  451. #endif /* end of WASM_ENABLE_SIMD */
  452. #if WASM_ENABLE_REF_TYPES != 0
  453. case INIT_EXPR_TYPE_FUNCREF_CONST:
  454. {
  455. if (type != VALUE_TYPE_FUNCREF)
  456. goto fail_type_mismatch;
  457. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  458. break;
  459. }
  460. case INIT_EXPR_TYPE_REFNULL_CONST:
  461. {
  462. uint8 reftype;
  463. CHECK_BUF(p, p_end, 1);
  464. reftype = read_uint8(p);
  465. if (reftype != type)
  466. goto fail_type_mismatch;
  467. init_expr->u.ref_index = NULL_REF;
  468. break;
  469. }
  470. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  471. /* get_global */
  472. case INIT_EXPR_TYPE_GET_GLOBAL:
  473. read_leb_uint32(p, p_end, init_expr->u.global_index);
  474. break;
  475. default:
  476. {
  477. set_error_buf(error_buf, error_buf_size,
  478. "illegal opcode "
  479. "or constant expression required "
  480. "or type mismatch");
  481. goto fail;
  482. }
  483. }
  484. CHECK_BUF(p, p_end, 1);
  485. end_byte = read_uint8(p);
  486. if (end_byte != 0x0b)
  487. goto fail_type_mismatch;
  488. *p_buf = p;
  489. return true;
  490. fail_type_mismatch:
  491. set_error_buf(error_buf, error_buf_size,
  492. "type mismatch or constant expression required");
  493. fail:
  494. return false;
  495. }
  496. static bool
  497. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  498. char *error_buf, uint32 error_buf_size)
  499. {
  500. const uint8 *p = buf, *p_end = buf_end, *p_org;
  501. uint32 type_count, param_count, result_count, i, j;
  502. uint32 param_cell_num, ret_cell_num;
  503. uint64 total_size;
  504. uint8 flag;
  505. WASMType *type;
  506. read_leb_uint32(p, p_end, type_count);
  507. if (type_count) {
  508. module->type_count = type_count;
  509. total_size = sizeof(WASMType *) * (uint64)type_count;
  510. if (!(module->types =
  511. loader_malloc(total_size, error_buf, error_buf_size))) {
  512. return false;
  513. }
  514. for (i = 0; i < type_count; i++) {
  515. CHECK_BUF(p, p_end, 1);
  516. flag = read_uint8(p);
  517. if (flag != 0x60) {
  518. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  519. return false;
  520. }
  521. read_leb_uint32(p, p_end, param_count);
  522. /* Resolve param count and result count firstly */
  523. p_org = p;
  524. CHECK_BUF(p, p_end, param_count);
  525. p += param_count;
  526. read_leb_uint32(p, p_end, result_count);
  527. CHECK_BUF(p, p_end, result_count);
  528. p = p_org;
  529. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  530. set_error_buf(error_buf, error_buf_size,
  531. "param count or result count too large");
  532. return false;
  533. }
  534. total_size = offsetof(WASMType, types)
  535. + sizeof(uint8) * (uint64)(param_count + result_count);
  536. if (!(type = module->types[i] =
  537. loader_malloc(total_size, error_buf, error_buf_size))) {
  538. return false;
  539. }
  540. /* Resolve param types and result types */
  541. type->ref_count = 1;
  542. type->param_count = (uint16)param_count;
  543. type->result_count = (uint16)result_count;
  544. for (j = 0; j < param_count; j++) {
  545. CHECK_BUF(p, p_end, 1);
  546. type->types[j] = read_uint8(p);
  547. }
  548. read_leb_uint32(p, p_end, result_count);
  549. for (j = 0; j < result_count; j++) {
  550. CHECK_BUF(p, p_end, 1);
  551. type->types[param_count + j] = read_uint8(p);
  552. }
  553. for (j = 0; j < param_count + result_count; j++) {
  554. if (!is_value_type(type->types[j])) {
  555. set_error_buf(error_buf, error_buf_size,
  556. "unknown value type");
  557. return false;
  558. }
  559. }
  560. param_cell_num = wasm_get_cell_num(type->types, param_count);
  561. ret_cell_num =
  562. wasm_get_cell_num(type->types + param_count, result_count);
  563. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  564. set_error_buf(error_buf, error_buf_size,
  565. "param count or result count too large");
  566. return false;
  567. }
  568. type->param_cell_num = (uint16)param_cell_num;
  569. type->ret_cell_num = (uint16)ret_cell_num;
  570. /* If there is already a same type created, use it instead */
  571. for (j = 0; j < i; j++) {
  572. if (wasm_type_equal(type, module->types[j])) {
  573. if (module->types[j]->ref_count == UINT16_MAX) {
  574. set_error_buf(error_buf, error_buf_size,
  575. "wasm type's ref count too large");
  576. return false;
  577. }
  578. destroy_wasm_type(type);
  579. module->types[i] = module->types[j];
  580. module->types[j]->ref_count++;
  581. break;
  582. }
  583. }
  584. }
  585. }
  586. if (p != p_end) {
  587. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  588. return false;
  589. }
  590. LOG_VERBOSE("Load type section success.\n");
  591. return true;
  592. fail:
  593. return false;
  594. }
  595. static void
  596. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  597. {
  598. uint32 default_max_size =
  599. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  600. if (max_size_flag) {
  601. /* module defines the table limitation */
  602. bh_assert(init_size <= *max_size);
  603. if (init_size < *max_size) {
  604. *max_size =
  605. *max_size < default_max_size ? *max_size : default_max_size;
  606. }
  607. }
  608. else {
  609. /* partial defined table limitation, gives a default value */
  610. *max_size = default_max_size;
  611. }
  612. }
  613. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  614. /**
  615. * Find export item of a module with export info:
  616. * module name, field name and export kind
  617. */
  618. static WASMExport *
  619. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  620. const char *field_name, uint8 export_kind,
  621. char *error_buf, uint32 error_buf_size)
  622. {
  623. WASMExport *export;
  624. uint32 i;
  625. for (i = 0, export = module->exports; i < module->export_count;
  626. ++i, ++export) {
  627. /**
  628. * need to consider a scenario that different kinds of exports
  629. * may have the same name, like
  630. * (table (export "m1" "exported") 10 funcref)
  631. * (memory (export "m1" "exported") 10)
  632. **/
  633. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  634. break;
  635. }
  636. }
  637. if (i == module->export_count) {
  638. LOG_DEBUG("can not find an export %d named %s in the module %s",
  639. export_kind, field_name, module_name);
  640. set_error_buf(error_buf, error_buf_size,
  641. "unknown import or incompatible import type");
  642. return NULL;
  643. }
  644. (void)module_name;
  645. /* since there is a validation in load_export_section(), it is for sure
  646. * export->index is valid*/
  647. return export;
  648. }
  649. #endif
  650. #if WASM_ENABLE_MULTI_MODULE != 0
  651. static WASMFunction *
  652. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  653. const WASMType *expected_function_type,
  654. char *error_buf, uint32 error_buf_size)
  655. {
  656. WASMModuleCommon *module_reg;
  657. WASMFunction *function = NULL;
  658. WASMExport *export = NULL;
  659. WASMModule *module = NULL;
  660. WASMType *target_function_type = NULL;
  661. module_reg = wasm_runtime_find_module_registered(module_name);
  662. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  663. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  664. function_name);
  665. set_error_buf(error_buf, error_buf_size, "unknown import");
  666. return NULL;
  667. }
  668. module = (WASMModule *)module_reg;
  669. export =
  670. wasm_loader_find_export(module, module_name, function_name,
  671. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  672. if (!export) {
  673. return NULL;
  674. }
  675. /* resolve function type and function */
  676. if (export->index < module->import_function_count) {
  677. target_function_type =
  678. module->import_functions[export->index].u.function.func_type;
  679. function = module->import_functions[export->index]
  680. .u.function.import_func_linked;
  681. }
  682. else {
  683. target_function_type =
  684. module->functions[export->index - module->import_function_count]
  685. ->func_type;
  686. function =
  687. module->functions[export->index - module->import_function_count];
  688. }
  689. /* check function type */
  690. if (!wasm_type_equal(expected_function_type, target_function_type)) {
  691. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  692. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  693. return NULL;
  694. }
  695. return function;
  696. }
  697. static WASMTable *
  698. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  699. uint32 init_size, uint32 max_size, char *error_buf,
  700. uint32 error_buf_size)
  701. {
  702. WASMModuleCommon *module_reg;
  703. WASMTable *table = NULL;
  704. WASMExport *export = NULL;
  705. WASMModule *module = NULL;
  706. module_reg = wasm_runtime_find_module_registered(module_name);
  707. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  708. LOG_DEBUG("can not find a module named %s for table", module_name);
  709. set_error_buf(error_buf, error_buf_size, "unknown import");
  710. return NULL;
  711. }
  712. module = (WASMModule *)module_reg;
  713. export =
  714. wasm_loader_find_export(module, module_name, table_name,
  715. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  716. if (!export) {
  717. return NULL;
  718. }
  719. /* resolve table and check the init/max size */
  720. if (export->index < module->import_table_count) {
  721. table =
  722. module->import_tables[export->index].u.table.import_table_linked;
  723. }
  724. else {
  725. table = &(module->tables[export->index - module->import_table_count]);
  726. }
  727. if (table->init_size < init_size || table->max_size > max_size) {
  728. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  729. module_name, table_name, table->init_size, table->max_size,
  730. init_size, max_size);
  731. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  732. return NULL;
  733. }
  734. return table;
  735. }
  736. static WASMMemory *
  737. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  738. uint32 init_page_count, uint32 max_page_count,
  739. char *error_buf, uint32 error_buf_size)
  740. {
  741. WASMModuleCommon *module_reg;
  742. WASMMemory *memory = NULL;
  743. WASMExport *export = NULL;
  744. WASMModule *module = NULL;
  745. module_reg = wasm_runtime_find_module_registered(module_name);
  746. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  747. LOG_DEBUG("can not find a module named %s for memory", module_name);
  748. set_error_buf(error_buf, error_buf_size, "unknown import");
  749. return NULL;
  750. }
  751. module = (WASMModule *)module_reg;
  752. export =
  753. wasm_loader_find_export(module, module_name, memory_name,
  754. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  755. if (!export) {
  756. return NULL;
  757. }
  758. /* resolve memory and check the init/max page count */
  759. if (export->index < module->import_memory_count) {
  760. memory = module->import_memories[export->index]
  761. .u.memory.import_memory_linked;
  762. }
  763. else {
  764. memory =
  765. &(module->memories[export->index - module->import_memory_count]);
  766. }
  767. if (memory->init_page_count < init_page_count
  768. || memory->max_page_count > max_page_count) {
  769. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  770. module_name, memory_name, memory->init_page_count,
  771. memory->max_page_count, init_page_count, max_page_count);
  772. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  773. return NULL;
  774. }
  775. return memory;
  776. }
  777. static WASMGlobal *
  778. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  779. uint8 type, bool is_mutable, char *error_buf,
  780. uint32 error_buf_size)
  781. {
  782. WASMModuleCommon *module_reg;
  783. WASMGlobal *global = NULL;
  784. WASMExport *export = NULL;
  785. WASMModule *module = NULL;
  786. module_reg = wasm_runtime_find_module_registered(module_name);
  787. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  788. LOG_DEBUG("can not find a module named %s for global", module_name);
  789. set_error_buf(error_buf, error_buf_size, "unknown import");
  790. return NULL;
  791. }
  792. module = (WASMModule *)module_reg;
  793. export =
  794. wasm_loader_find_export(module, module_name, global_name,
  795. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  796. if (!export) {
  797. return NULL;
  798. }
  799. /* resolve and check the global */
  800. if (export->index < module->import_global_count) {
  801. global =
  802. module->import_globals[export->index].u.global.import_global_linked;
  803. }
  804. else {
  805. global =
  806. &(module->globals[export->index - module->import_global_count]);
  807. }
  808. if (global->type != type || global->is_mutable != is_mutable) {
  809. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  810. module_name, global_name, global->type, global->is_mutable,
  811. type, is_mutable);
  812. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  813. return NULL;
  814. }
  815. return global;
  816. }
  817. static WASMModule *
  818. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  819. {
  820. WASMRegisteredModule *node =
  821. bh_list_first_elem(parent_module->import_module_list);
  822. while (node && strcmp(sub_module_name, node->module_name)) {
  823. node = bh_list_elem_next(node);
  824. }
  825. return node ? (WASMModule *)node->module : NULL;
  826. }
  827. static bool
  828. register_sub_module(const WASMModule *parent_module,
  829. const char *sub_module_name, WASMModule *sub_module)
  830. {
  831. /* register sub_module into its parent sub module list */
  832. WASMRegisteredModule *node = NULL;
  833. bh_list_status ret;
  834. if (search_sub_module(parent_module, sub_module_name)) {
  835. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  836. return true;
  837. }
  838. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  839. if (!node) {
  840. return false;
  841. }
  842. node->module_name = sub_module_name;
  843. node->module = (WASMModuleCommon *)sub_module;
  844. ret = bh_list_insert(parent_module->import_module_list, node);
  845. bh_assert(BH_LIST_SUCCESS == ret);
  846. (void)ret;
  847. return true;
  848. }
  849. static WASMModule *
  850. load_depended_module(const WASMModule *parent_module,
  851. const char *sub_module_name, char *error_buf,
  852. uint32 error_buf_size)
  853. {
  854. WASMModule *sub_module = NULL;
  855. bool ret = false;
  856. uint8 *buffer = NULL;
  857. uint32 buffer_size = 0;
  858. const module_reader reader = wasm_runtime_get_module_reader();
  859. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  860. /* check the registered module list of the parent */
  861. sub_module = search_sub_module(parent_module, sub_module_name);
  862. if (sub_module) {
  863. LOG_DEBUG("%s has been loaded before", sub_module_name);
  864. return sub_module;
  865. }
  866. /* check the global registered module list */
  867. sub_module =
  868. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  869. if (sub_module) {
  870. LOG_DEBUG("%s has been loaded", sub_module_name);
  871. goto register_sub_module;
  872. }
  873. LOG_VERBOSE("loading %s", sub_module_name);
  874. if (!reader) {
  875. set_error_buf_v(error_buf, error_buf_size,
  876. "no sub module reader to load %s", sub_module_name);
  877. return NULL;
  878. }
  879. /* start to maintain a loading module list */
  880. ret = wasm_runtime_is_loading_module(sub_module_name);
  881. if (ret) {
  882. set_error_buf_v(error_buf, error_buf_size,
  883. "found circular dependency on %s", sub_module_name);
  884. return NULL;
  885. }
  886. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  887. error_buf_size);
  888. if (!ret) {
  889. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  890. return NULL;
  891. }
  892. ret = reader(sub_module_name, &buffer, &buffer_size);
  893. if (!ret) {
  894. LOG_DEBUG("read the file of %s failed", sub_module_name);
  895. set_error_buf_v(error_buf, error_buf_size, "unknown import",
  896. sub_module_name);
  897. goto delete_loading_module;
  898. }
  899. sub_module =
  900. wasm_loader_load(buffer, buffer_size, false, error_buf, error_buf_size);
  901. if (!sub_module) {
  902. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  903. /* others will be destroyed in runtime_destroy() */
  904. goto destroy_file_buffer;
  905. }
  906. wasm_runtime_delete_loading_module(sub_module_name);
  907. /* register on a global list */
  908. ret = wasm_runtime_register_module_internal(
  909. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  910. error_buf, error_buf_size);
  911. if (!ret) {
  912. LOG_DEBUG("error: can not register module %s globally\n",
  913. sub_module_name);
  914. /* others will be unloaded in runtime_destroy() */
  915. goto unload_module;
  916. }
  917. /* register into its parent list */
  918. register_sub_module:
  919. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  920. if (!ret) {
  921. set_error_buf_v(error_buf, error_buf_size,
  922. "failed to register sub module %s", sub_module_name);
  923. /* since it is in the global module list, no need to
  924. * unload the module. the runtime_destroy() will do it
  925. */
  926. return NULL;
  927. }
  928. return sub_module;
  929. unload_module:
  930. wasm_loader_unload(sub_module);
  931. destroy_file_buffer:
  932. if (destroyer) {
  933. destroyer(buffer, buffer_size);
  934. }
  935. else {
  936. LOG_WARNING("need to release the reading buffer of %s manually",
  937. sub_module_name);
  938. }
  939. delete_loading_module:
  940. wasm_runtime_delete_loading_module(sub_module_name);
  941. return NULL;
  942. }
  943. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  944. static bool
  945. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  946. const WASMModule *parent_module,
  947. const char *sub_module_name, const char *function_name,
  948. WASMFunctionImport *function, char *error_buf,
  949. uint32 error_buf_size)
  950. {
  951. const uint8 *p = *p_buf, *p_end = buf_end;
  952. uint32 declare_type_index = 0;
  953. WASMType *declare_func_type = NULL;
  954. WASMFunction *linked_func = NULL;
  955. #if WASM_ENABLE_MULTI_MODULE != 0
  956. WASMModule *sub_module = NULL;
  957. #endif
  958. const char *linked_signature = NULL;
  959. void *linked_attachment = NULL;
  960. bool linked_call_conv_raw = false;
  961. bool is_native_symbol = false;
  962. read_leb_uint32(p, p_end, declare_type_index);
  963. *p_buf = p;
  964. if (declare_type_index >= parent_module->type_count) {
  965. set_error_buf(error_buf, error_buf_size, "unknown type");
  966. return false;
  967. }
  968. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  969. declare_type_index = wasm_get_smallest_type_idx(
  970. parent_module->types, parent_module->type_count, declare_type_index);
  971. #endif
  972. declare_func_type = parent_module->types[declare_type_index];
  973. /* lookup registered native symbols first */
  974. linked_func = wasm_native_resolve_symbol(
  975. sub_module_name, function_name, declare_func_type, &linked_signature,
  976. &linked_attachment, &linked_call_conv_raw);
  977. if (linked_func) {
  978. is_native_symbol = true;
  979. }
  980. #if WASM_ENABLE_MULTI_MODULE != 0
  981. else {
  982. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  983. sub_module = load_depended_module(parent_module, sub_module_name,
  984. error_buf, error_buf_size);
  985. if (!sub_module) {
  986. return false;
  987. }
  988. }
  989. linked_func = wasm_loader_resolve_function(
  990. sub_module_name, function_name, declare_func_type, error_buf,
  991. error_buf_size);
  992. }
  993. #endif
  994. function->module_name = (char *)sub_module_name;
  995. function->field_name = (char *)function_name;
  996. function->func_type = declare_func_type;
  997. /* func_ptr_linked is for native registered symbol */
  998. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  999. function->signature = linked_signature;
  1000. function->attachment = linked_attachment;
  1001. function->call_conv_raw = linked_call_conv_raw;
  1002. #if WASM_ENABLE_MULTI_MODULE != 0
  1003. function->import_module = is_native_symbol ? NULL : sub_module;
  1004. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  1005. #endif
  1006. return true;
  1007. fail:
  1008. return false;
  1009. }
  1010. static bool
  1011. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1012. uint32 error_buf_size)
  1013. {
  1014. if (max_size < init_size) {
  1015. set_error_buf(error_buf, error_buf_size,
  1016. "size minimum must not be greater than maximum");
  1017. return false;
  1018. }
  1019. return true;
  1020. }
  1021. static bool
  1022. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  1023. WASMModule *parent_module, const char *sub_module_name,
  1024. const char *table_name, WASMTableImport *table,
  1025. char *error_buf, uint32 error_buf_size)
  1026. {
  1027. const uint8 *p = *p_buf, *p_end = buf_end;
  1028. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  1029. declare_init_size = 0, declare_max_size = 0;
  1030. #if WASM_ENABLE_MULTI_MODULE != 0
  1031. WASMModule *sub_module = NULL;
  1032. WASMTable *linked_table = NULL;
  1033. #endif
  1034. CHECK_BUF(p, p_end, 1);
  1035. /* 0x70 or 0x6F */
  1036. declare_elem_type = read_uint8(p);
  1037. if (VALUE_TYPE_FUNCREF != declare_elem_type
  1038. #if WASM_ENABLE_REF_TYPES != 0
  1039. && VALUE_TYPE_EXTERNREF != declare_elem_type
  1040. #endif
  1041. ) {
  1042. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1043. return false;
  1044. }
  1045. read_leb_uint32(p, p_end, declare_max_size_flag);
  1046. if (declare_max_size_flag > 1) {
  1047. set_error_buf(error_buf, error_buf_size, "integer too large");
  1048. return false;
  1049. }
  1050. read_leb_uint32(p, p_end, declare_init_size);
  1051. if (declare_max_size_flag) {
  1052. read_leb_uint32(p, p_end, declare_max_size);
  1053. if (!check_table_max_size(declare_init_size, declare_max_size,
  1054. error_buf, error_buf_size))
  1055. return false;
  1056. }
  1057. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1058. &declare_max_size);
  1059. *p_buf = p;
  1060. #if WASM_ENABLE_MULTI_MODULE != 0
  1061. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1062. sub_module = load_depended_module(parent_module, sub_module_name,
  1063. error_buf, error_buf_size);
  1064. if (!sub_module) {
  1065. return false;
  1066. }
  1067. linked_table = wasm_loader_resolve_table(
  1068. sub_module_name, table_name, declare_init_size, declare_max_size,
  1069. error_buf, error_buf_size);
  1070. if (!linked_table) {
  1071. return false;
  1072. }
  1073. /* reset with linked table limit */
  1074. declare_elem_type = linked_table->elem_type;
  1075. declare_init_size = linked_table->init_size;
  1076. declare_max_size = linked_table->max_size;
  1077. declare_max_size_flag = linked_table->flags;
  1078. table->import_table_linked = linked_table;
  1079. table->import_module = sub_module;
  1080. }
  1081. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1082. /* (table (export "table") 10 20 funcref) */
  1083. /* we need this section working in wamrc */
  1084. if (!strcmp("spectest", sub_module_name)) {
  1085. const uint32 spectest_table_init_size = 10;
  1086. const uint32 spectest_table_max_size = 20;
  1087. if (strcmp("table", table_name)) {
  1088. set_error_buf(error_buf, error_buf_size,
  1089. "incompatible import type or unknown import");
  1090. return false;
  1091. }
  1092. if (declare_init_size > spectest_table_init_size
  1093. || declare_max_size < spectest_table_max_size) {
  1094. set_error_buf(error_buf, error_buf_size,
  1095. "incompatible import type");
  1096. return false;
  1097. }
  1098. declare_init_size = spectest_table_init_size;
  1099. declare_max_size = spectest_table_max_size;
  1100. }
  1101. /* now we believe all declaration are ok */
  1102. table->elem_type = declare_elem_type;
  1103. table->init_size = declare_init_size;
  1104. table->flags = declare_max_size_flag;
  1105. table->max_size = declare_max_size;
  1106. (void)parent_module;
  1107. return true;
  1108. fail:
  1109. return false;
  1110. }
  1111. static bool
  1112. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1113. {
  1114. if (init_size > DEFAULT_MAX_PAGES) {
  1115. set_error_buf(error_buf, error_buf_size,
  1116. "memory size must be at most 65536 pages (4GiB)");
  1117. return false;
  1118. }
  1119. return true;
  1120. }
  1121. static bool
  1122. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1123. uint32 error_buf_size)
  1124. {
  1125. if (max_size < init_size) {
  1126. set_error_buf(error_buf, error_buf_size,
  1127. "size minimum must not be greater than maximum");
  1128. return false;
  1129. }
  1130. if (max_size > DEFAULT_MAX_PAGES) {
  1131. set_error_buf(error_buf, error_buf_size,
  1132. "memory size must be at most 65536 pages (4GiB)");
  1133. return false;
  1134. }
  1135. return true;
  1136. }
  1137. static bool
  1138. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1139. WASMModule *parent_module, const char *sub_module_name,
  1140. const char *memory_name, WASMMemoryImport *memory,
  1141. char *error_buf, uint32 error_buf_size)
  1142. {
  1143. const uint8 *p = *p_buf, *p_end = buf_end;
  1144. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1145. uint32 pool_size = wasm_runtime_memory_pool_size();
  1146. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1147. / DEFAULT_NUM_BYTES_PER_PAGE;
  1148. #else
  1149. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1150. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1151. uint32 declare_max_page_count_flag = 0;
  1152. uint32 declare_init_page_count = 0;
  1153. uint32 declare_max_page_count = 0;
  1154. #if WASM_ENABLE_MULTI_MODULE != 0
  1155. WASMModule *sub_module = NULL;
  1156. WASMMemory *linked_memory = NULL;
  1157. #endif
  1158. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1159. read_leb_uint32(p, p_end, declare_init_page_count);
  1160. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1161. error_buf_size)) {
  1162. return false;
  1163. }
  1164. if (declare_max_page_count_flag & 1) {
  1165. read_leb_uint32(p, p_end, declare_max_page_count);
  1166. if (!check_memory_max_size(declare_init_page_count,
  1167. declare_max_page_count, error_buf,
  1168. error_buf_size)) {
  1169. return false;
  1170. }
  1171. if (declare_max_page_count > max_page_count) {
  1172. declare_max_page_count = max_page_count;
  1173. }
  1174. }
  1175. else {
  1176. /* Limit the maximum memory size to max_page_count */
  1177. declare_max_page_count = max_page_count;
  1178. }
  1179. #if WASM_ENABLE_MULTI_MODULE != 0
  1180. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1181. sub_module = load_depended_module(parent_module, sub_module_name,
  1182. error_buf, error_buf_size);
  1183. if (!sub_module) {
  1184. return false;
  1185. }
  1186. linked_memory = wasm_loader_resolve_memory(
  1187. sub_module_name, memory_name, declare_init_page_count,
  1188. declare_max_page_count, error_buf, error_buf_size);
  1189. if (!linked_memory) {
  1190. return false;
  1191. }
  1192. /**
  1193. * reset with linked memory limit
  1194. */
  1195. memory->import_module = sub_module;
  1196. memory->import_memory_linked = linked_memory;
  1197. declare_init_page_count = linked_memory->init_page_count;
  1198. declare_max_page_count = linked_memory->max_page_count;
  1199. }
  1200. #endif
  1201. /* (memory (export "memory") 1 2) */
  1202. if (!strcmp("spectest", sub_module_name)) {
  1203. uint32 spectest_memory_init_page = 1;
  1204. uint32 spectest_memory_max_page = 2;
  1205. if (strcmp("memory", memory_name)) {
  1206. set_error_buf(error_buf, error_buf_size,
  1207. "incompatible import type or unknown import");
  1208. return false;
  1209. }
  1210. if (declare_init_page_count > spectest_memory_init_page
  1211. || declare_max_page_count < spectest_memory_max_page) {
  1212. set_error_buf(error_buf, error_buf_size,
  1213. "incompatible import type");
  1214. return false;
  1215. }
  1216. declare_init_page_count = spectest_memory_init_page;
  1217. declare_max_page_count = spectest_memory_max_page;
  1218. }
  1219. /* now we believe all declaration are ok */
  1220. memory->flags = declare_max_page_count_flag;
  1221. memory->init_page_count = declare_init_page_count;
  1222. memory->max_page_count = declare_max_page_count;
  1223. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1224. *p_buf = p;
  1225. (void)parent_module;
  1226. return true;
  1227. fail:
  1228. return false;
  1229. }
  1230. static bool
  1231. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  1232. const WASMModule *parent_module, char *sub_module_name,
  1233. char *global_name, WASMGlobalImport *global, char *error_buf,
  1234. uint32 error_buf_size)
  1235. {
  1236. const uint8 *p = *p_buf, *p_end = buf_end;
  1237. uint8 declare_type = 0;
  1238. uint8 declare_mutable = 0;
  1239. #if WASM_ENABLE_MULTI_MODULE != 0
  1240. WASMModule *sub_module = NULL;
  1241. WASMGlobal *linked_global = NULL;
  1242. #endif
  1243. bool ret = false;
  1244. CHECK_BUF(p, p_end, 2);
  1245. declare_type = read_uint8(p);
  1246. declare_mutable = read_uint8(p);
  1247. *p_buf = p;
  1248. if (declare_mutable >= 2) {
  1249. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1250. return false;
  1251. }
  1252. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1253. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  1254. global);
  1255. if (ret) {
  1256. if (global->type != declare_type
  1257. || global->is_mutable != declare_mutable) {
  1258. set_error_buf(error_buf, error_buf_size,
  1259. "incompatible import type");
  1260. return false;
  1261. }
  1262. global->is_linked = true;
  1263. }
  1264. #endif
  1265. #if WASM_ENABLE_MULTI_MODULE != 0
  1266. if (!global->is_linked
  1267. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  1268. sub_module = load_depended_module(parent_module, sub_module_name,
  1269. error_buf, error_buf_size);
  1270. if (!sub_module) {
  1271. return false;
  1272. }
  1273. /* check sub modules */
  1274. linked_global = wasm_loader_resolve_global(
  1275. sub_module_name, global_name, declare_type, declare_mutable,
  1276. error_buf, error_buf_size);
  1277. if (linked_global) {
  1278. global->import_module = sub_module;
  1279. global->import_global_linked = linked_global;
  1280. global->is_linked = true;
  1281. }
  1282. }
  1283. #endif
  1284. global->module_name = sub_module_name;
  1285. global->field_name = global_name;
  1286. global->type = declare_type;
  1287. global->is_mutable = (declare_mutable == 1);
  1288. (void)parent_module;
  1289. (void)ret;
  1290. return true;
  1291. fail:
  1292. return false;
  1293. }
  1294. static bool
  1295. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  1296. char *error_buf, uint32 error_buf_size)
  1297. {
  1298. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1299. CHECK_BUF(p, p_end, 1);
  1300. /* 0x70 or 0x6F */
  1301. table->elem_type = read_uint8(p);
  1302. if (VALUE_TYPE_FUNCREF != table->elem_type
  1303. #if WASM_ENABLE_REF_TYPES != 0
  1304. && VALUE_TYPE_EXTERNREF != table->elem_type
  1305. #endif
  1306. ) {
  1307. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1308. return false;
  1309. }
  1310. p_org = p;
  1311. read_leb_uint32(p, p_end, table->flags);
  1312. #if WASM_ENABLE_SHARED_MEMORY == 0
  1313. if (p - p_org > 1) {
  1314. set_error_buf(error_buf, error_buf_size,
  1315. "integer representation too long");
  1316. return false;
  1317. }
  1318. if (table->flags > 1) {
  1319. set_error_buf(error_buf, error_buf_size, "integer too large");
  1320. return false;
  1321. }
  1322. #else
  1323. if (p - p_org > 1) {
  1324. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1325. return false;
  1326. }
  1327. if (table->flags == 2) {
  1328. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  1329. return false;
  1330. }
  1331. if (table->flags > 1) {
  1332. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1333. return false;
  1334. }
  1335. #endif
  1336. read_leb_uint32(p, p_end, table->init_size);
  1337. if (table->flags) {
  1338. read_leb_uint32(p, p_end, table->max_size);
  1339. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  1340. error_buf_size))
  1341. return false;
  1342. }
  1343. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  1344. *p_buf = p;
  1345. return true;
  1346. fail:
  1347. return false;
  1348. }
  1349. static bool
  1350. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  1351. char *error_buf, uint32 error_buf_size)
  1352. {
  1353. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1354. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1355. uint32 pool_size = wasm_runtime_memory_pool_size();
  1356. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1357. / DEFAULT_NUM_BYTES_PER_PAGE;
  1358. #else
  1359. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1360. #endif
  1361. p_org = p;
  1362. read_leb_uint32(p, p_end, memory->flags);
  1363. #if WASM_ENABLE_SHARED_MEMORY == 0
  1364. if (p - p_org > 1) {
  1365. set_error_buf(error_buf, error_buf_size,
  1366. "integer representation too long");
  1367. return false;
  1368. }
  1369. if (memory->flags > 1) {
  1370. set_error_buf(error_buf, error_buf_size, "integer too large");
  1371. return false;
  1372. }
  1373. #else
  1374. if (p - p_org > 1) {
  1375. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1376. return false;
  1377. }
  1378. if (memory->flags > 3) {
  1379. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1380. return false;
  1381. }
  1382. else if (memory->flags == 2) {
  1383. set_error_buf(error_buf, error_buf_size,
  1384. "shared memory must have maximum");
  1385. return false;
  1386. }
  1387. #endif
  1388. read_leb_uint32(p, p_end, memory->init_page_count);
  1389. if (!check_memory_init_size(memory->init_page_count, error_buf,
  1390. error_buf_size))
  1391. return false;
  1392. if (memory->flags & 1) {
  1393. read_leb_uint32(p, p_end, memory->max_page_count);
  1394. if (!check_memory_max_size(memory->init_page_count,
  1395. memory->max_page_count, error_buf,
  1396. error_buf_size))
  1397. return false;
  1398. if (memory->max_page_count > max_page_count)
  1399. memory->max_page_count = max_page_count;
  1400. }
  1401. else {
  1402. /* Limit the maximum memory size to max_page_count */
  1403. memory->max_page_count = max_page_count;
  1404. }
  1405. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1406. *p_buf = p;
  1407. return true;
  1408. fail:
  1409. return false;
  1410. }
  1411. static bool
  1412. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1413. bool is_load_from_file_buf, char *error_buf,
  1414. uint32 error_buf_size)
  1415. {
  1416. const uint8 *p = buf, *p_end = buf_end, *p_old;
  1417. uint32 import_count, name_len, type_index, i, u32, flags;
  1418. uint64 total_size;
  1419. WASMImport *import;
  1420. WASMImport *import_functions = NULL, *import_tables = NULL;
  1421. WASMImport *import_memories = NULL, *import_globals = NULL;
  1422. char *sub_module_name, *field_name;
  1423. uint8 u8, kind;
  1424. read_leb_uint32(p, p_end, import_count);
  1425. if (import_count) {
  1426. module->import_count = import_count;
  1427. total_size = sizeof(WASMImport) * (uint64)import_count;
  1428. if (!(module->imports =
  1429. loader_malloc(total_size, error_buf, error_buf_size))) {
  1430. return false;
  1431. }
  1432. p_old = p;
  1433. /* Scan firstly to get import count of each type */
  1434. for (i = 0; i < import_count; i++) {
  1435. /* module name */
  1436. read_leb_uint32(p, p_end, name_len);
  1437. CHECK_BUF(p, p_end, name_len);
  1438. p += name_len;
  1439. /* field name */
  1440. read_leb_uint32(p, p_end, name_len);
  1441. CHECK_BUF(p, p_end, name_len);
  1442. p += name_len;
  1443. CHECK_BUF(p, p_end, 1);
  1444. /* 0x00/0x01/0x02/0x03 */
  1445. kind = read_uint8(p);
  1446. switch (kind) {
  1447. case IMPORT_KIND_FUNC: /* import function */
  1448. read_leb_uint32(p, p_end, type_index);
  1449. module->import_function_count++;
  1450. break;
  1451. case IMPORT_KIND_TABLE: /* import table */
  1452. CHECK_BUF(p, p_end, 1);
  1453. /* 0x70 */
  1454. u8 = read_uint8(p);
  1455. read_leb_uint32(p, p_end, flags);
  1456. read_leb_uint32(p, p_end, u32);
  1457. if (flags & 1)
  1458. read_leb_uint32(p, p_end, u32);
  1459. module->import_table_count++;
  1460. #if WASM_ENABLE_REF_TYPES == 0
  1461. if (module->import_table_count > 1) {
  1462. set_error_buf(error_buf, error_buf_size,
  1463. "multiple tables");
  1464. return false;
  1465. }
  1466. #endif
  1467. break;
  1468. case IMPORT_KIND_MEMORY: /* import memory */
  1469. read_leb_uint32(p, p_end, flags);
  1470. read_leb_uint32(p, p_end, u32);
  1471. if (flags & 1)
  1472. read_leb_uint32(p, p_end, u32);
  1473. module->import_memory_count++;
  1474. if (module->import_memory_count > 1) {
  1475. set_error_buf(error_buf, error_buf_size,
  1476. "multiple memories");
  1477. return false;
  1478. }
  1479. break;
  1480. case IMPORT_KIND_GLOBAL: /* import global */
  1481. CHECK_BUF(p, p_end, 2);
  1482. p += 2;
  1483. module->import_global_count++;
  1484. break;
  1485. default:
  1486. set_error_buf(error_buf, error_buf_size,
  1487. "invalid import kind");
  1488. return false;
  1489. }
  1490. }
  1491. if (module->import_function_count)
  1492. import_functions = module->import_functions = module->imports;
  1493. if (module->import_table_count)
  1494. import_tables = module->import_tables =
  1495. module->imports + module->import_function_count;
  1496. if (module->import_memory_count)
  1497. import_memories = module->import_memories =
  1498. module->imports + module->import_function_count
  1499. + module->import_table_count;
  1500. if (module->import_global_count)
  1501. import_globals = module->import_globals =
  1502. module->imports + module->import_function_count
  1503. + module->import_table_count + module->import_memory_count;
  1504. p = p_old;
  1505. /* Scan again to resolve the data */
  1506. for (i = 0; i < import_count; i++) {
  1507. /* load module name */
  1508. read_leb_uint32(p, p_end, name_len);
  1509. CHECK_BUF(p, p_end, name_len);
  1510. if (!(sub_module_name = const_str_list_insert(
  1511. p, name_len, module, is_load_from_file_buf, error_buf,
  1512. error_buf_size))) {
  1513. return false;
  1514. }
  1515. p += name_len;
  1516. /* load field name */
  1517. read_leb_uint32(p, p_end, name_len);
  1518. CHECK_BUF(p, p_end, name_len);
  1519. if (!(field_name = const_str_list_insert(
  1520. p, name_len, module, is_load_from_file_buf, error_buf,
  1521. error_buf_size))) {
  1522. return false;
  1523. }
  1524. p += name_len;
  1525. CHECK_BUF(p, p_end, 1);
  1526. /* 0x00/0x01/0x02/0x03 */
  1527. kind = read_uint8(p);
  1528. switch (kind) {
  1529. case IMPORT_KIND_FUNC: /* import function */
  1530. bh_assert(import_functions);
  1531. import = import_functions++;
  1532. if (!load_function_import(
  1533. &p, p_end, module, sub_module_name, field_name,
  1534. &import->u.function, error_buf, error_buf_size)) {
  1535. return false;
  1536. }
  1537. break;
  1538. case IMPORT_KIND_TABLE: /* import table */
  1539. bh_assert(import_tables);
  1540. import = import_tables++;
  1541. if (!load_table_import(&p, p_end, module, sub_module_name,
  1542. field_name, &import->u.table,
  1543. error_buf, error_buf_size)) {
  1544. LOG_DEBUG("can not import such a table (%s,%s)",
  1545. sub_module_name, field_name);
  1546. return false;
  1547. }
  1548. break;
  1549. case IMPORT_KIND_MEMORY: /* import memory */
  1550. bh_assert(import_memories);
  1551. import = import_memories++;
  1552. if (!load_memory_import(&p, p_end, module, sub_module_name,
  1553. field_name, &import->u.memory,
  1554. error_buf, error_buf_size)) {
  1555. return false;
  1556. }
  1557. break;
  1558. case IMPORT_KIND_GLOBAL: /* import global */
  1559. bh_assert(import_globals);
  1560. import = import_globals++;
  1561. if (!load_global_import(&p, p_end, module, sub_module_name,
  1562. field_name, &import->u.global,
  1563. error_buf, error_buf_size)) {
  1564. return false;
  1565. }
  1566. break;
  1567. default:
  1568. set_error_buf(error_buf, error_buf_size,
  1569. "invalid import kind");
  1570. return false;
  1571. }
  1572. import->kind = kind;
  1573. import->u.names.module_name = sub_module_name;
  1574. import->u.names.field_name = field_name;
  1575. }
  1576. #if WASM_ENABLE_LIBC_WASI != 0
  1577. import = module->import_functions;
  1578. for (i = 0; i < module->import_function_count; i++, import++) {
  1579. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  1580. || !strcmp(import->u.names.module_name,
  1581. "wasi_snapshot_preview1")) {
  1582. module->import_wasi_api = true;
  1583. break;
  1584. }
  1585. }
  1586. #endif
  1587. }
  1588. if (p != p_end) {
  1589. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1590. return false;
  1591. }
  1592. LOG_VERBOSE("Load import section success.\n");
  1593. (void)u8;
  1594. (void)u32;
  1595. (void)type_index;
  1596. return true;
  1597. fail:
  1598. return false;
  1599. }
  1600. static bool
  1601. init_function_local_offsets(WASMFunction *func, char *error_buf,
  1602. uint32 error_buf_size)
  1603. {
  1604. WASMType *param_type = func->func_type;
  1605. uint32 param_count = param_type->param_count;
  1606. uint8 *param_types = param_type->types;
  1607. uint32 local_count = func->local_count;
  1608. uint8 *local_types = func->local_types;
  1609. uint32 i, local_offset = 0;
  1610. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  1611. /*
  1612. * Only allocate memory when total_size is not 0,
  1613. * or the return value of malloc(0) might be NULL on some platforms,
  1614. * which causes wasm loader return false.
  1615. */
  1616. if (total_size > 0
  1617. && !(func->local_offsets =
  1618. loader_malloc(total_size, error_buf, error_buf_size))) {
  1619. return false;
  1620. }
  1621. for (i = 0; i < param_count; i++) {
  1622. func->local_offsets[i] = (uint16)local_offset;
  1623. local_offset += wasm_value_type_cell_num(param_types[i]);
  1624. }
  1625. for (i = 0; i < local_count; i++) {
  1626. func->local_offsets[param_count + i] = (uint16)local_offset;
  1627. local_offset += wasm_value_type_cell_num(local_types[i]);
  1628. }
  1629. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  1630. return true;
  1631. }
  1632. static bool
  1633. load_function_section(const uint8 *buf, const uint8 *buf_end,
  1634. const uint8 *buf_code, const uint8 *buf_code_end,
  1635. WASMModule *module, char *error_buf,
  1636. uint32 error_buf_size)
  1637. {
  1638. const uint8 *p = buf, *p_end = buf_end;
  1639. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  1640. uint32 func_count;
  1641. uint64 total_size;
  1642. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  1643. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  1644. uint8 type;
  1645. WASMFunction *func;
  1646. read_leb_uint32(p, p_end, func_count);
  1647. if (buf_code)
  1648. read_leb_uint32(p_code, buf_code_end, code_count);
  1649. if (func_count != code_count) {
  1650. set_error_buf(error_buf, error_buf_size,
  1651. "function and code section have inconsistent lengths or "
  1652. "unexpected end");
  1653. return false;
  1654. }
  1655. if (func_count) {
  1656. module->function_count = func_count;
  1657. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  1658. if (!(module->functions =
  1659. loader_malloc(total_size, error_buf, error_buf_size))) {
  1660. return false;
  1661. }
  1662. for (i = 0; i < func_count; i++) {
  1663. /* Resolve function type */
  1664. read_leb_uint32(p, p_end, type_index);
  1665. if (type_index >= module->type_count) {
  1666. set_error_buf(error_buf, error_buf_size, "unknown type");
  1667. return false;
  1668. }
  1669. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1670. type_index = wasm_get_smallest_type_idx(
  1671. module->types, module->type_count, type_index);
  1672. #endif
  1673. read_leb_uint32(p_code, buf_code_end, code_size);
  1674. if (code_size == 0 || p_code + code_size > buf_code_end) {
  1675. set_error_buf(error_buf, error_buf_size,
  1676. "invalid function code size");
  1677. return false;
  1678. }
  1679. /* Resolve local set count */
  1680. p_code_end = p_code + code_size;
  1681. local_count = 0;
  1682. read_leb_uint32(p_code, buf_code_end, local_set_count);
  1683. p_code_save = p_code;
  1684. /* Calculate total local count */
  1685. for (j = 0; j < local_set_count; j++) {
  1686. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1687. if (sub_local_count > UINT32_MAX - local_count) {
  1688. set_error_buf(error_buf, error_buf_size, "too many locals");
  1689. return false;
  1690. }
  1691. CHECK_BUF(p_code, buf_code_end, 1);
  1692. /* 0x7F/0x7E/0x7D/0x7C */
  1693. type = read_uint8(p_code);
  1694. local_count += sub_local_count;
  1695. }
  1696. /* Alloc memory, layout: function structure + local types */
  1697. code_size = (uint32)(p_code_end - p_code);
  1698. total_size = sizeof(WASMFunction) + (uint64)local_count;
  1699. if (!(func = module->functions[i] =
  1700. loader_malloc(total_size, error_buf, error_buf_size))) {
  1701. return false;
  1702. }
  1703. /* Set function type, local count, code size and code body */
  1704. func->func_type = module->types[type_index];
  1705. func->local_count = local_count;
  1706. if (local_count > 0)
  1707. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  1708. func->code_size = code_size;
  1709. /*
  1710. * we shall make a copy of code body [p_code, p_code + code_size]
  1711. * when we are worrying about inappropriate releasing behaviour.
  1712. * all code bodies are actually in a buffer which user allocates in
  1713. * his embedding environment and we don't have power on them.
  1714. * it will be like:
  1715. * code_body_cp = malloc(code_size);
  1716. * memcpy(code_body_cp, p_code, code_size);
  1717. * func->code = code_body_cp;
  1718. */
  1719. func->code = (uint8 *)p_code;
  1720. /* Load each local type */
  1721. p_code = p_code_save;
  1722. local_type_index = 0;
  1723. for (j = 0; j < local_set_count; j++) {
  1724. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1725. /* Note: sub_local_count is allowed to be 0 */
  1726. if (local_type_index > UINT32_MAX - sub_local_count
  1727. || local_type_index + sub_local_count > local_count) {
  1728. set_error_buf(error_buf, error_buf_size,
  1729. "invalid local count");
  1730. return false;
  1731. }
  1732. CHECK_BUF(p_code, buf_code_end, 1);
  1733. /* 0x7F/0x7E/0x7D/0x7C */
  1734. type = read_uint8(p_code);
  1735. if (!is_value_type(type)) {
  1736. if (type == VALUE_TYPE_V128)
  1737. set_error_buf(error_buf, error_buf_size,
  1738. "v128 value type requires simd feature");
  1739. else if (type == VALUE_TYPE_FUNCREF
  1740. || type == VALUE_TYPE_EXTERNREF)
  1741. set_error_buf(error_buf, error_buf_size,
  1742. "ref value type requires "
  1743. "reference types feature");
  1744. else
  1745. set_error_buf_v(error_buf, error_buf_size,
  1746. "invalid local type 0x%02X", type);
  1747. return false;
  1748. }
  1749. for (k = 0; k < sub_local_count; k++) {
  1750. func->local_types[local_type_index++] = type;
  1751. }
  1752. }
  1753. func->param_cell_num = func->func_type->param_cell_num;
  1754. func->ret_cell_num = func->func_type->ret_cell_num;
  1755. local_cell_num =
  1756. wasm_get_cell_num(func->local_types, func->local_count);
  1757. if (local_cell_num > UINT16_MAX) {
  1758. set_error_buf(error_buf, error_buf_size,
  1759. "local count too large");
  1760. return false;
  1761. }
  1762. func->local_cell_num = (uint16)local_cell_num;
  1763. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  1764. return false;
  1765. p_code = p_code_end;
  1766. }
  1767. }
  1768. if (p != p_end) {
  1769. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1770. return false;
  1771. }
  1772. LOG_VERBOSE("Load function section success.\n");
  1773. return true;
  1774. fail:
  1775. return false;
  1776. }
  1777. static bool
  1778. check_function_index(const WASMModule *module, uint32 function_index,
  1779. char *error_buf, uint32 error_buf_size)
  1780. {
  1781. if (function_index
  1782. >= module->import_function_count + module->function_count) {
  1783. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  1784. function_index);
  1785. return false;
  1786. }
  1787. return true;
  1788. }
  1789. static bool
  1790. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1791. char *error_buf, uint32 error_buf_size)
  1792. {
  1793. const uint8 *p = buf, *p_end = buf_end;
  1794. uint32 table_count, i;
  1795. uint64 total_size;
  1796. WASMTable *table;
  1797. read_leb_uint32(p, p_end, table_count);
  1798. #if WASM_ENABLE_REF_TYPES == 0
  1799. if (module->import_table_count + table_count > 1) {
  1800. /* a total of one table is allowed */
  1801. set_error_buf(error_buf, error_buf_size, "multiple tables");
  1802. return false;
  1803. }
  1804. #endif
  1805. if (table_count) {
  1806. module->table_count = table_count;
  1807. total_size = sizeof(WASMTable) * (uint64)table_count;
  1808. if (!(module->tables =
  1809. loader_malloc(total_size, error_buf, error_buf_size))) {
  1810. return false;
  1811. }
  1812. /* load each table */
  1813. table = module->tables;
  1814. for (i = 0; i < table_count; i++, table++)
  1815. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  1816. return false;
  1817. }
  1818. if (p != p_end) {
  1819. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1820. return false;
  1821. }
  1822. LOG_VERBOSE("Load table section success.\n");
  1823. return true;
  1824. fail:
  1825. return false;
  1826. }
  1827. static bool
  1828. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1829. char *error_buf, uint32 error_buf_size)
  1830. {
  1831. const uint8 *p = buf, *p_end = buf_end;
  1832. uint32 memory_count, i;
  1833. uint64 total_size;
  1834. WASMMemory *memory;
  1835. read_leb_uint32(p, p_end, memory_count);
  1836. /* a total of one memory is allowed */
  1837. if (module->import_memory_count + memory_count > 1) {
  1838. set_error_buf(error_buf, error_buf_size, "multiple memories");
  1839. return false;
  1840. }
  1841. if (memory_count) {
  1842. module->memory_count = memory_count;
  1843. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  1844. if (!(module->memories =
  1845. loader_malloc(total_size, error_buf, error_buf_size))) {
  1846. return false;
  1847. }
  1848. /* load each memory */
  1849. memory = module->memories;
  1850. for (i = 0; i < memory_count; i++, memory++)
  1851. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  1852. return false;
  1853. }
  1854. if (p != p_end) {
  1855. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1856. return false;
  1857. }
  1858. LOG_VERBOSE("Load memory section success.\n");
  1859. return true;
  1860. fail:
  1861. return false;
  1862. }
  1863. static bool
  1864. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1865. char *error_buf, uint32 error_buf_size)
  1866. {
  1867. const uint8 *p = buf, *p_end = buf_end;
  1868. uint32 global_count, i;
  1869. uint64 total_size;
  1870. WASMGlobal *global;
  1871. uint8 mutable;
  1872. read_leb_uint32(p, p_end, global_count);
  1873. if (global_count) {
  1874. module->global_count = global_count;
  1875. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  1876. if (!(module->globals =
  1877. loader_malloc(total_size, error_buf, error_buf_size))) {
  1878. return false;
  1879. }
  1880. global = module->globals;
  1881. for (i = 0; i < global_count; i++, global++) {
  1882. CHECK_BUF(p, p_end, 2);
  1883. global->type = read_uint8(p);
  1884. mutable = read_uint8(p);
  1885. if (mutable >= 2) {
  1886. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1887. return false;
  1888. }
  1889. global->is_mutable = mutable ? true : false;
  1890. /* initialize expression */
  1891. if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
  1892. error_buf, error_buf_size))
  1893. return false;
  1894. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1895. /**
  1896. * Currently, constant expressions occurring as initializers
  1897. * of globals are further constrained in that contained
  1898. * global.get instructions are
  1899. * only allowed to refer to imported globals.
  1900. */
  1901. uint32 target_global_index = global->init_expr.u.global_index;
  1902. if (target_global_index >= module->import_global_count) {
  1903. set_error_buf(error_buf, error_buf_size, "unknown global");
  1904. return false;
  1905. }
  1906. }
  1907. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  1908. == global->init_expr.init_expr_type) {
  1909. if (!check_function_index(module, global->init_expr.u.ref_index,
  1910. error_buf, error_buf_size)) {
  1911. return false;
  1912. }
  1913. }
  1914. }
  1915. }
  1916. if (p != p_end) {
  1917. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1918. return false;
  1919. }
  1920. LOG_VERBOSE("Load global section success.\n");
  1921. return true;
  1922. fail:
  1923. return false;
  1924. }
  1925. static bool
  1926. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1927. bool is_load_from_file_buf, char *error_buf,
  1928. uint32 error_buf_size)
  1929. {
  1930. const uint8 *p = buf, *p_end = buf_end;
  1931. uint32 export_count, i, j, index;
  1932. uint64 total_size;
  1933. uint32 str_len;
  1934. WASMExport *export;
  1935. const char *name;
  1936. read_leb_uint32(p, p_end, export_count);
  1937. if (export_count) {
  1938. module->export_count = export_count;
  1939. total_size = sizeof(WASMExport) * (uint64)export_count;
  1940. if (!(module->exports =
  1941. loader_malloc(total_size, error_buf, error_buf_size))) {
  1942. return false;
  1943. }
  1944. export = module->exports;
  1945. for (i = 0; i < export_count; i++, export ++) {
  1946. #if WASM_ENABLE_THREAD_MGR == 0
  1947. if (p == p_end) {
  1948. /* export section with inconsistent count:
  1949. n export declared, but less than n given */
  1950. set_error_buf(error_buf, error_buf_size,
  1951. "length out of bounds");
  1952. return false;
  1953. }
  1954. #endif
  1955. read_leb_uint32(p, p_end, str_len);
  1956. CHECK_BUF(p, p_end, str_len);
  1957. for (j = 0; j < i; j++) {
  1958. name = module->exports[j].name;
  1959. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  1960. set_error_buf(error_buf, error_buf_size,
  1961. "duplicate export name");
  1962. return false;
  1963. }
  1964. }
  1965. if (!(export->name = const_str_list_insert(
  1966. p, str_len, module, is_load_from_file_buf, error_buf,
  1967. error_buf_size))) {
  1968. return false;
  1969. }
  1970. p += str_len;
  1971. CHECK_BUF(p, p_end, 1);
  1972. export->kind = read_uint8(p);
  1973. read_leb_uint32(p, p_end, index);
  1974. export->index = index;
  1975. switch (export->kind) {
  1976. /* function index */
  1977. case EXPORT_KIND_FUNC:
  1978. if (index >= module->function_count
  1979. + module->import_function_count) {
  1980. set_error_buf(error_buf, error_buf_size,
  1981. "unknown function");
  1982. return false;
  1983. }
  1984. #if WASM_ENABLE_SIMD != 0
  1985. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1986. /* TODO: check func type, if it has v128 param or result,
  1987. report error */
  1988. #endif
  1989. #endif
  1990. break;
  1991. /* table index */
  1992. case EXPORT_KIND_TABLE:
  1993. if (index
  1994. >= module->table_count + module->import_table_count) {
  1995. set_error_buf(error_buf, error_buf_size,
  1996. "unknown table");
  1997. return false;
  1998. }
  1999. break;
  2000. /* memory index */
  2001. case EXPORT_KIND_MEMORY:
  2002. if (index
  2003. >= module->memory_count + module->import_memory_count) {
  2004. set_error_buf(error_buf, error_buf_size,
  2005. "unknown memory");
  2006. return false;
  2007. }
  2008. break;
  2009. /* global index */
  2010. case EXPORT_KIND_GLOBAL:
  2011. if (index
  2012. >= module->global_count + module->import_global_count) {
  2013. set_error_buf(error_buf, error_buf_size,
  2014. "unknown global");
  2015. return false;
  2016. }
  2017. break;
  2018. default:
  2019. set_error_buf(error_buf, error_buf_size,
  2020. "invalid export kind");
  2021. return false;
  2022. }
  2023. }
  2024. }
  2025. if (p != p_end) {
  2026. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2027. return false;
  2028. }
  2029. LOG_VERBOSE("Load export section success.\n");
  2030. return true;
  2031. fail:
  2032. return false;
  2033. }
  2034. static bool
  2035. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  2036. uint32 error_buf_size)
  2037. {
  2038. #if WASM_ENABLE_REF_TYPES == 0
  2039. if (table_index != 0) {
  2040. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  2041. return false;
  2042. }
  2043. #endif
  2044. if (table_index >= module->import_table_count + module->table_count) {
  2045. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  2046. table_index);
  2047. return false;
  2048. }
  2049. return true;
  2050. }
  2051. static bool
  2052. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2053. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  2054. {
  2055. const uint8 *p = *p_buf, *p_end = buf_end;
  2056. uint32 table_index;
  2057. read_leb_uint32(p, p_end, table_index);
  2058. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  2059. return false;
  2060. }
  2061. *p_table_index = table_index;
  2062. *p_buf = p;
  2063. return true;
  2064. fail:
  2065. return false;
  2066. }
  2067. #if WASM_ENABLE_REF_TYPES != 0
  2068. static bool
  2069. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  2070. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  2071. {
  2072. const uint8 *p = *p_buf, *p_end = buf_end;
  2073. uint8 elem_type;
  2074. CHECK_BUF(p, p_end, 1);
  2075. elem_type = read_uint8(p);
  2076. if ((elemkind_zero && elem_type != 0)
  2077. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  2078. && elem_type != VALUE_TYPE_EXTERNREF)) {
  2079. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  2080. return false;
  2081. }
  2082. if (elemkind_zero)
  2083. *p_elem_type = VALUE_TYPE_FUNCREF;
  2084. else
  2085. *p_elem_type = elem_type;
  2086. *p_buf = p;
  2087. return true;
  2088. fail:
  2089. return false;
  2090. }
  2091. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  2092. static bool
  2093. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  2094. WASMModule *module, WASMTableSeg *table_segment,
  2095. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  2096. {
  2097. const uint8 *p = *p_buf, *p_end = buf_end;
  2098. uint32 function_count, function_index = 0, i;
  2099. uint64 total_size;
  2100. read_leb_uint32(p, p_end, function_count);
  2101. table_segment->function_count = function_count;
  2102. total_size = sizeof(uint32) * (uint64)function_count;
  2103. if (total_size > 0
  2104. && !(table_segment->func_indexes = (uint32 *)loader_malloc(
  2105. total_size, error_buf, error_buf_size))) {
  2106. return false;
  2107. }
  2108. for (i = 0; i < function_count; i++) {
  2109. InitializerExpression init_expr = { 0 };
  2110. #if WASM_ENABLE_REF_TYPES != 0
  2111. if (!use_init_expr) {
  2112. read_leb_uint32(p, p_end, function_index);
  2113. }
  2114. else {
  2115. if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
  2116. error_buf, error_buf_size))
  2117. return false;
  2118. function_index = init_expr.u.ref_index;
  2119. }
  2120. #else
  2121. read_leb_uint32(p, p_end, function_index);
  2122. (void)use_init_expr;
  2123. #endif
  2124. /* since we are using -1 to indicate ref.null */
  2125. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  2126. && !check_function_index(module, function_index, error_buf,
  2127. error_buf_size)) {
  2128. return false;
  2129. }
  2130. table_segment->func_indexes[i] = function_index;
  2131. }
  2132. *p_buf = p;
  2133. return true;
  2134. fail:
  2135. return false;
  2136. }
  2137. static bool
  2138. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  2139. WASMModule *module, char *error_buf,
  2140. uint32 error_buf_size)
  2141. {
  2142. const uint8 *p = buf, *p_end = buf_end;
  2143. uint32 table_segment_count, i;
  2144. uint64 total_size;
  2145. WASMTableSeg *table_segment;
  2146. read_leb_uint32(p, p_end, table_segment_count);
  2147. if (table_segment_count) {
  2148. module->table_seg_count = table_segment_count;
  2149. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  2150. if (!(module->table_segments =
  2151. loader_malloc(total_size, error_buf, error_buf_size))) {
  2152. return false;
  2153. }
  2154. table_segment = module->table_segments;
  2155. for (i = 0; i < table_segment_count; i++, table_segment++) {
  2156. if (p >= p_end) {
  2157. set_error_buf(error_buf, error_buf_size,
  2158. "invalid value type or "
  2159. "invalid elements segment kind");
  2160. return false;
  2161. }
  2162. #if WASM_ENABLE_REF_TYPES != 0
  2163. read_leb_uint32(p, p_end, table_segment->mode);
  2164. /* last three bits */
  2165. table_segment->mode = table_segment->mode & 0x07;
  2166. switch (table_segment->mode) {
  2167. /* elemkind/elemtype + active */
  2168. case 0:
  2169. case 4:
  2170. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  2171. table_segment->table_index = 0;
  2172. if (!check_table_index(module, table_segment->table_index,
  2173. error_buf, error_buf_size))
  2174. return false;
  2175. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2176. VALUE_TYPE_I32, error_buf,
  2177. error_buf_size))
  2178. return false;
  2179. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2180. table_segment->mode == 0 ? false
  2181. : true,
  2182. error_buf, error_buf_size))
  2183. return false;
  2184. break;
  2185. /* elemkind + passive/declarative */
  2186. case 1:
  2187. case 3:
  2188. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2189. true, error_buf, error_buf_size))
  2190. return false;
  2191. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2192. false, error_buf, error_buf_size))
  2193. return false;
  2194. break;
  2195. /* elemkind/elemtype + table_idx + active */
  2196. case 2:
  2197. case 6:
  2198. if (!load_table_index(&p, p_end, module,
  2199. &table_segment->table_index,
  2200. error_buf, error_buf_size))
  2201. return false;
  2202. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2203. VALUE_TYPE_I32, error_buf,
  2204. error_buf_size))
  2205. return false;
  2206. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2207. table_segment->mode == 2 ? true : false,
  2208. error_buf, error_buf_size))
  2209. return false;
  2210. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2211. table_segment->mode == 2 ? false
  2212. : true,
  2213. error_buf, error_buf_size))
  2214. return false;
  2215. break;
  2216. case 5:
  2217. case 7:
  2218. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2219. false, error_buf, error_buf_size))
  2220. return false;
  2221. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2222. true, error_buf, error_buf_size))
  2223. return false;
  2224. break;
  2225. default:
  2226. set_error_buf(error_buf, error_buf_size,
  2227. "unknown element segment kind");
  2228. return false;
  2229. }
  2230. #else
  2231. /*
  2232. * like: 00 41 05 0b 04 00 01 00 01
  2233. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  2234. */
  2235. if (!load_table_index(&p, p_end, module,
  2236. &table_segment->table_index, error_buf,
  2237. error_buf_size))
  2238. return false;
  2239. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2240. VALUE_TYPE_I32, error_buf, error_buf_size))
  2241. return false;
  2242. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  2243. error_buf, error_buf_size))
  2244. return false;
  2245. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2246. }
  2247. }
  2248. if (p != p_end) {
  2249. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2250. return false;
  2251. }
  2252. LOG_VERBOSE("Load table segment section success.\n");
  2253. return true;
  2254. fail:
  2255. return false;
  2256. }
  2257. static bool
  2258. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  2259. WASMModule *module, char *error_buf,
  2260. uint32 error_buf_size)
  2261. {
  2262. const uint8 *p = buf, *p_end = buf_end;
  2263. uint32 data_seg_count, i, mem_index, data_seg_len;
  2264. uint64 total_size;
  2265. WASMDataSeg *dataseg;
  2266. InitializerExpression init_expr;
  2267. #if WASM_ENABLE_BULK_MEMORY != 0
  2268. bool is_passive = false;
  2269. uint32 mem_flag;
  2270. #endif
  2271. read_leb_uint32(p, p_end, data_seg_count);
  2272. #if WASM_ENABLE_BULK_MEMORY != 0
  2273. if ((module->data_seg_count1 != 0)
  2274. && (data_seg_count != module->data_seg_count1)) {
  2275. set_error_buf(error_buf, error_buf_size,
  2276. "data count and data section have inconsistent lengths");
  2277. return false;
  2278. }
  2279. #endif
  2280. if (data_seg_count) {
  2281. module->data_seg_count = data_seg_count;
  2282. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  2283. if (!(module->data_segments =
  2284. loader_malloc(total_size, error_buf, error_buf_size))) {
  2285. return false;
  2286. }
  2287. for (i = 0; i < data_seg_count; i++) {
  2288. read_leb_uint32(p, p_end, mem_index);
  2289. #if WASM_ENABLE_BULK_MEMORY != 0
  2290. is_passive = false;
  2291. mem_flag = mem_index & 0x03;
  2292. switch (mem_flag) {
  2293. case 0x01:
  2294. is_passive = true;
  2295. break;
  2296. case 0x00:
  2297. /* no memory index, treat index as 0 */
  2298. mem_index = 0;
  2299. goto check_mem_index;
  2300. case 0x02:
  2301. /* read following memory index */
  2302. read_leb_uint32(p, p_end, mem_index);
  2303. check_mem_index:
  2304. if (mem_index
  2305. >= module->import_memory_count + module->memory_count) {
  2306. set_error_buf_v(error_buf, error_buf_size,
  2307. "unknown memory %d", mem_index);
  2308. return false;
  2309. }
  2310. break;
  2311. case 0x03:
  2312. default:
  2313. set_error_buf(error_buf, error_buf_size, "unknown memory");
  2314. return false;
  2315. break;
  2316. }
  2317. #else
  2318. if (mem_index
  2319. >= module->import_memory_count + module->memory_count) {
  2320. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  2321. mem_index);
  2322. return false;
  2323. }
  2324. #endif /* WASM_ENABLE_BULK_MEMORY */
  2325. #if WASM_ENABLE_BULK_MEMORY != 0
  2326. if (!is_passive)
  2327. #endif
  2328. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  2329. error_buf, error_buf_size))
  2330. return false;
  2331. read_leb_uint32(p, p_end, data_seg_len);
  2332. if (!(dataseg = module->data_segments[i] = loader_malloc(
  2333. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  2334. return false;
  2335. }
  2336. #if WASM_ENABLE_BULK_MEMORY != 0
  2337. dataseg->is_passive = is_passive;
  2338. if (!is_passive)
  2339. #endif
  2340. {
  2341. bh_memcpy_s(&dataseg->base_offset,
  2342. sizeof(InitializerExpression), &init_expr,
  2343. sizeof(InitializerExpression));
  2344. dataseg->memory_index = mem_index;
  2345. }
  2346. dataseg->data_length = data_seg_len;
  2347. CHECK_BUF(p, p_end, data_seg_len);
  2348. dataseg->data = (uint8 *)p;
  2349. p += data_seg_len;
  2350. }
  2351. }
  2352. if (p != p_end) {
  2353. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2354. return false;
  2355. }
  2356. LOG_VERBOSE("Load data segment section success.\n");
  2357. return true;
  2358. fail:
  2359. return false;
  2360. }
  2361. #if WASM_ENABLE_BULK_MEMORY != 0
  2362. static bool
  2363. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  2364. WASMModule *module, char *error_buf,
  2365. uint32 error_buf_size)
  2366. {
  2367. const uint8 *p = buf, *p_end = buf_end;
  2368. uint32 data_seg_count1 = 0;
  2369. read_leb_uint32(p, p_end, data_seg_count1);
  2370. module->data_seg_count1 = data_seg_count1;
  2371. if (p != p_end) {
  2372. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2373. return false;
  2374. }
  2375. LOG_VERBOSE("Load datacount section success.\n");
  2376. return true;
  2377. fail:
  2378. return false;
  2379. }
  2380. #endif
  2381. static bool
  2382. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  2383. const uint8 *buf_func_end, WASMModule *module,
  2384. char *error_buf, uint32 error_buf_size)
  2385. {
  2386. const uint8 *p = buf, *p_end = buf_end;
  2387. const uint8 *p_func = buf_func;
  2388. uint32 func_count = 0, code_count;
  2389. /* code has been loaded in function section, so pass it here, just check
  2390. * whether function and code section have inconsistent lengths */
  2391. read_leb_uint32(p, p_end, code_count);
  2392. if (buf_func)
  2393. read_leb_uint32(p_func, buf_func_end, func_count);
  2394. if (func_count != code_count) {
  2395. set_error_buf(error_buf, error_buf_size,
  2396. "function and code section have inconsistent lengths");
  2397. return false;
  2398. }
  2399. LOG_VERBOSE("Load code segment section success.\n");
  2400. (void)module;
  2401. return true;
  2402. fail:
  2403. return false;
  2404. }
  2405. static bool
  2406. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2407. char *error_buf, uint32 error_buf_size)
  2408. {
  2409. const uint8 *p = buf, *p_end = buf_end;
  2410. WASMType *type;
  2411. uint32 start_function;
  2412. read_leb_uint32(p, p_end, start_function);
  2413. if (start_function
  2414. >= module->function_count + module->import_function_count) {
  2415. set_error_buf(error_buf, error_buf_size, "unknown function");
  2416. return false;
  2417. }
  2418. if (start_function < module->import_function_count)
  2419. type = module->import_functions[start_function].u.function.func_type;
  2420. else
  2421. type = module->functions[start_function - module->import_function_count]
  2422. ->func_type;
  2423. if (type->param_count != 0 || type->result_count != 0) {
  2424. set_error_buf(error_buf, error_buf_size, "invalid start function");
  2425. return false;
  2426. }
  2427. module->start_function = start_function;
  2428. if (p != p_end) {
  2429. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2430. return false;
  2431. }
  2432. LOG_VERBOSE("Load start section success.\n");
  2433. return true;
  2434. fail:
  2435. return false;
  2436. }
  2437. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2438. static bool
  2439. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2440. bool is_load_from_file_buf, char *error_buf,
  2441. uint32 error_buf_size)
  2442. {
  2443. const uint8 *p = buf, *p_end = buf_end;
  2444. uint32 name_type, subsection_size;
  2445. uint32 previous_name_type = 0;
  2446. uint32 num_func_name;
  2447. uint32 func_index;
  2448. uint32 previous_func_index = ~0U;
  2449. uint32 func_name_len;
  2450. uint32 name_index;
  2451. int i = 0;
  2452. if (p >= p_end) {
  2453. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2454. return false;
  2455. }
  2456. while (p < p_end) {
  2457. read_leb_uint32(p, p_end, name_type);
  2458. if (i != 0) {
  2459. if (name_type == previous_name_type) {
  2460. set_error_buf(error_buf, error_buf_size,
  2461. "duplicate sub-section");
  2462. return false;
  2463. }
  2464. if (name_type < previous_name_type) {
  2465. set_error_buf(error_buf, error_buf_size,
  2466. "out-of-order sub-section");
  2467. return false;
  2468. }
  2469. }
  2470. previous_name_type = name_type;
  2471. read_leb_uint32(p, p_end, subsection_size);
  2472. CHECK_BUF(p, p_end, subsection_size);
  2473. switch (name_type) {
  2474. case SUB_SECTION_TYPE_FUNC:
  2475. if (subsection_size) {
  2476. read_leb_uint32(p, p_end, num_func_name);
  2477. for (name_index = 0; name_index < num_func_name;
  2478. name_index++) {
  2479. read_leb_uint32(p, p_end, func_index);
  2480. if (func_index == previous_func_index) {
  2481. set_error_buf(error_buf, error_buf_size,
  2482. "duplicate function name");
  2483. return false;
  2484. }
  2485. if (func_index < previous_func_index
  2486. && previous_func_index != ~0U) {
  2487. set_error_buf(error_buf, error_buf_size,
  2488. "out-of-order function index ");
  2489. return false;
  2490. }
  2491. previous_func_index = func_index;
  2492. read_leb_uint32(p, p_end, func_name_len);
  2493. CHECK_BUF(p, p_end, func_name_len);
  2494. /* Skip the import functions */
  2495. if (func_index >= module->import_function_count) {
  2496. func_index -= module->import_function_count;
  2497. if (func_index >= module->function_count) {
  2498. set_error_buf(error_buf, error_buf_size,
  2499. "out-of-range function index");
  2500. return false;
  2501. }
  2502. if (!(module->functions[func_index]->field_name =
  2503. const_str_list_insert(
  2504. p, func_name_len, module,
  2505. is_load_from_file_buf, error_buf,
  2506. error_buf_size))) {
  2507. return false;
  2508. }
  2509. }
  2510. p += func_name_len;
  2511. }
  2512. }
  2513. break;
  2514. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  2515. */
  2516. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  2517. default:
  2518. p = p + subsection_size;
  2519. break;
  2520. }
  2521. i++;
  2522. }
  2523. return true;
  2524. fail:
  2525. return false;
  2526. }
  2527. #endif
  2528. static bool
  2529. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2530. bool is_load_from_file_buf, char *error_buf,
  2531. uint32 error_buf_size)
  2532. {
  2533. const uint8 *p = buf, *p_end = buf_end;
  2534. char section_name[32];
  2535. uint32 name_len, buffer_len;
  2536. if (p >= p_end) {
  2537. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2538. return false;
  2539. }
  2540. read_leb_uint32(p, p_end, name_len);
  2541. if (name_len == 0 || p + name_len > p_end) {
  2542. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2543. return false;
  2544. }
  2545. if (!check_utf8_str(p, name_len)) {
  2546. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  2547. return false;
  2548. }
  2549. buffer_len = sizeof(section_name);
  2550. memset(section_name, 0, buffer_len);
  2551. if (name_len < buffer_len) {
  2552. bh_memcpy_s(section_name, buffer_len, p, name_len);
  2553. }
  2554. else {
  2555. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  2556. memset(section_name + buffer_len - 4, '.', 3);
  2557. }
  2558. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2559. if (memcmp(p, "name", 4) == 0) {
  2560. module->name_section_buf = buf;
  2561. module->name_section_buf_end = buf_end;
  2562. p += name_len;
  2563. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  2564. error_buf_size);
  2565. LOG_VERBOSE("Load custom name section success.");
  2566. return true;
  2567. }
  2568. #endif
  2569. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  2570. {
  2571. WASMCustomSection *section =
  2572. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  2573. if (!section) {
  2574. return false;
  2575. }
  2576. section->name_addr = (char *)p;
  2577. section->name_len = name_len;
  2578. section->content_addr = (uint8 *)(p + name_len);
  2579. section->content_len = (uint32)(p_end - p - name_len);
  2580. section->next = module->custom_section_list;
  2581. module->custom_section_list = section;
  2582. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  2583. return true;
  2584. }
  2585. #endif
  2586. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  2587. (void)is_load_from_file_buf;
  2588. (void)module;
  2589. return true;
  2590. fail:
  2591. return false;
  2592. }
  2593. static void
  2594. calculate_global_data_offset(WASMModule *module)
  2595. {
  2596. uint32 i, data_offset;
  2597. data_offset = 0;
  2598. for (i = 0; i < module->import_global_count; i++) {
  2599. WASMGlobalImport *import_global =
  2600. &((module->import_globals + i)->u.global);
  2601. #if WASM_ENABLE_FAST_JIT != 0
  2602. import_global->data_offset = data_offset;
  2603. #endif
  2604. data_offset += wasm_value_type_size(import_global->type);
  2605. }
  2606. for (i = 0; i < module->global_count; i++) {
  2607. WASMGlobal *global = module->globals + i;
  2608. #if WASM_ENABLE_FAST_JIT != 0
  2609. global->data_offset = data_offset;
  2610. #endif
  2611. data_offset += wasm_value_type_size(global->type);
  2612. }
  2613. module->global_data_size = data_offset;
  2614. }
  2615. #if WASM_ENABLE_FAST_JIT != 0
  2616. static bool
  2617. init_fast_jit_functions(WASMModule *module, char *error_buf,
  2618. uint32 error_buf_size)
  2619. {
  2620. #if WASM_ENABLE_LAZY_JIT != 0
  2621. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  2622. #endif
  2623. uint32 i;
  2624. if (!module->function_count)
  2625. return true;
  2626. if (!(module->fast_jit_func_ptrs =
  2627. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  2628. error_buf_size))) {
  2629. return false;
  2630. }
  2631. #if WASM_ENABLE_LAZY_JIT != 0
  2632. for (i = 0; i < module->function_count; i++) {
  2633. module->fast_jit_func_ptrs[i] =
  2634. jit_globals->compile_fast_jit_and_then_call;
  2635. }
  2636. #endif
  2637. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  2638. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  2639. set_error_buf(error_buf, error_buf_size,
  2640. "init fast jit thread lock failed");
  2641. return false;
  2642. }
  2643. module->fast_jit_thread_locks_inited[i] = true;
  2644. }
  2645. return true;
  2646. }
  2647. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2648. #if WASM_ENABLE_JIT != 0
  2649. static bool
  2650. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  2651. uint32 error_buf_size)
  2652. {
  2653. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  2654. AOTCompOption option = { 0 };
  2655. char *aot_last_error;
  2656. uint64 size;
  2657. if (module->function_count == 0)
  2658. return true;
  2659. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LLVM_JIT != 0
  2660. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  2661. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  2662. return false;
  2663. }
  2664. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  2665. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  2666. os_mutex_destroy(&module->tierup_wait_lock);
  2667. return false;
  2668. }
  2669. module->tierup_wait_lock_inited = true;
  2670. #endif
  2671. size = sizeof(void *) * (uint64)module->function_count
  2672. + sizeof(bool) * (uint64)module->function_count;
  2673. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  2674. return false;
  2675. }
  2676. module->func_ptrs_compiled =
  2677. (bool *)((uint8 *)module->func_ptrs
  2678. + sizeof(void *) * module->function_count);
  2679. module->comp_data = aot_create_comp_data(module);
  2680. if (!module->comp_data) {
  2681. aot_last_error = aot_get_last_error();
  2682. bh_assert(aot_last_error != NULL);
  2683. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2684. return false;
  2685. }
  2686. option.is_jit_mode = true;
  2687. llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  2688. option.opt_level = llvm_jit_options.opt_level;
  2689. option.size_level = llvm_jit_options.size_level;
  2690. #if WASM_ENABLE_BULK_MEMORY != 0
  2691. option.enable_bulk_memory = true;
  2692. #endif
  2693. #if WASM_ENABLE_THREAD_MGR != 0
  2694. option.enable_thread_mgr = true;
  2695. #endif
  2696. #if WASM_ENABLE_TAIL_CALL != 0
  2697. option.enable_tail_call = true;
  2698. #endif
  2699. #if WASM_ENABLE_SIMD != 0
  2700. option.enable_simd = true;
  2701. #endif
  2702. #if WASM_ENABLE_REF_TYPES != 0
  2703. option.enable_ref_types = true;
  2704. #endif
  2705. option.enable_aux_stack_check = true;
  2706. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  2707. option.enable_aux_stack_frame = true;
  2708. #endif
  2709. #if WASM_ENABLE_MEMORY_PROFILING != 0
  2710. option.enable_stack_estimation = true;
  2711. #endif
  2712. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  2713. if (!module->comp_ctx) {
  2714. aot_last_error = aot_get_last_error();
  2715. bh_assert(aot_last_error != NULL);
  2716. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2717. return false;
  2718. }
  2719. return true;
  2720. }
  2721. static bool
  2722. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  2723. uint32 error_buf_size)
  2724. {
  2725. char *aot_last_error;
  2726. uint32 i;
  2727. if (module->function_count == 0)
  2728. return true;
  2729. if (!aot_compile_wasm(module->comp_ctx)) {
  2730. aot_last_error = aot_get_last_error();
  2731. bh_assert(aot_last_error != NULL);
  2732. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2733. return false;
  2734. }
  2735. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2736. if (module->orcjit_stop_compiling)
  2737. return false;
  2738. #endif
  2739. bh_print_time("Begin to lookup llvm jit functions");
  2740. for (i = 0; i < module->function_count; i++) {
  2741. LLVMOrcJITTargetAddress func_addr = 0;
  2742. LLVMErrorRef error;
  2743. char func_name[48];
  2744. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2745. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  2746. func_name);
  2747. if (error != LLVMErrorSuccess) {
  2748. char *err_msg = LLVMGetErrorMessage(error);
  2749. set_error_buf_v(error_buf, error_buf_size,
  2750. "failed to compile llvm jit function: %s", err_msg);
  2751. LLVMDisposeErrorMessage(err_msg);
  2752. return false;
  2753. }
  2754. /**
  2755. * No need to lock the func_ptr[func_idx] here as it is basic
  2756. * data type, the load/store for it can be finished by one cpu
  2757. * instruction, and there can be only one cpu instruction
  2758. * loading/storing at the same time.
  2759. */
  2760. module->func_ptrs[i] = (void *)func_addr;
  2761. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2762. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  2763. if (module->orcjit_stop_compiling)
  2764. return false;
  2765. #endif
  2766. }
  2767. bh_print_time("End lookup llvm jit functions");
  2768. return true;
  2769. }
  2770. #endif /* end of WASM_ENABLE_JIT != 0 */
  2771. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2772. && WASM_ENABLE_LAZY_JIT != 0
  2773. static void *
  2774. init_llvm_jit_functions_stage2_callback(void *arg)
  2775. {
  2776. WASMModule *module = (WASMModule *)arg;
  2777. char error_buf[128];
  2778. uint32 error_buf_size = (uint32)sizeof(error_buf);
  2779. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  2780. module->orcjit_stop_compiling = true;
  2781. return NULL;
  2782. }
  2783. os_mutex_lock(&module->tierup_wait_lock);
  2784. module->llvm_jit_inited = true;
  2785. os_cond_broadcast(&module->tierup_wait_cond);
  2786. os_mutex_unlock(&module->tierup_wait_lock);
  2787. return NULL;
  2788. }
  2789. #endif
  2790. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2791. /* The callback function to compile jit functions */
  2792. static void *
  2793. orcjit_thread_callback(void *arg)
  2794. {
  2795. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  2796. #if WASM_ENABLE_JIT != 0
  2797. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  2798. #endif
  2799. WASMModule *module = thread_arg->module;
  2800. uint32 group_idx = thread_arg->group_idx;
  2801. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  2802. uint32 func_count = module->function_count;
  2803. uint32 i;
  2804. #if WASM_ENABLE_FAST_JIT != 0
  2805. /* Compile fast jit funcitons of this group */
  2806. for (i = group_idx; i < func_count; i += group_stride) {
  2807. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  2808. os_printf("failed to compile fast jit function %u\n", i);
  2809. break;
  2810. }
  2811. if (module->orcjit_stop_compiling) {
  2812. return NULL;
  2813. }
  2814. }
  2815. #endif
  2816. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2817. && WASM_ENABLE_LAZY_JIT != 0
  2818. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  2819. for (i = group_idx; i < func_count;
  2820. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  2821. uint32 j;
  2822. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  2823. if (i + j * group_stride < func_count) {
  2824. if (!jit_compiler_set_call_to_fast_jit(
  2825. module,
  2826. i + j * group_stride + module->import_function_count)) {
  2827. os_printf(
  2828. "failed to compile call_to_fast_jit for func %u\n",
  2829. i + j * group_stride + module->import_function_count);
  2830. module->orcjit_stop_compiling = true;
  2831. return NULL;
  2832. }
  2833. }
  2834. if (module->orcjit_stop_compiling) {
  2835. return NULL;
  2836. }
  2837. }
  2838. }
  2839. /* Wait until init_llvm_jit_functions_stage2 finishes */
  2840. os_mutex_lock(&module->tierup_wait_lock);
  2841. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation)) {
  2842. os_cond_reltimedwait(&module->tierup_wait_cond,
  2843. &module->tierup_wait_lock, 10000);
  2844. if (module->orcjit_stop_compiling) {
  2845. /* init_llvm_jit_functions_stage2 failed */
  2846. os_mutex_unlock(&module->tierup_wait_lock);
  2847. return NULL;
  2848. }
  2849. }
  2850. os_mutex_unlock(&module->tierup_wait_lock);
  2851. #endif
  2852. #if WASM_ENABLE_JIT != 0
  2853. /* Compile llvm jit functions of this group */
  2854. for (i = group_idx; i < func_count;
  2855. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  2856. LLVMOrcJITTargetAddress func_addr = 0;
  2857. LLVMErrorRef error;
  2858. char func_name[48];
  2859. typedef void (*F)(void);
  2860. union {
  2861. F f;
  2862. void *v;
  2863. } u;
  2864. uint32 j;
  2865. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  2866. "_wrapper");
  2867. LOG_DEBUG("compile llvm jit func %s", func_name);
  2868. error =
  2869. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  2870. if (error != LLVMErrorSuccess) {
  2871. char *err_msg = LLVMGetErrorMessage(error);
  2872. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  2873. LLVMDisposeErrorMessage(err_msg);
  2874. break;
  2875. }
  2876. /* Call the jit wrapper function to trigger its compilation, so as
  2877. to compile the actual jit functions, since we add the latter to
  2878. function list in the PartitionFunction callback */
  2879. u.v = (void *)func_addr;
  2880. u.f();
  2881. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  2882. if (i + j * group_stride < func_count) {
  2883. module->func_ptrs_compiled[i + j * group_stride] = true;
  2884. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2885. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  2886. i + j * group_stride);
  2887. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  2888. func_name);
  2889. if (error != LLVMErrorSuccess) {
  2890. char *err_msg = LLVMGetErrorMessage(error);
  2891. os_printf("failed to compile llvm jit function %u: %s", i,
  2892. err_msg);
  2893. LLVMDisposeErrorMessage(err_msg);
  2894. /* Ignore current llvm jit func, as its func ptr is
  2895. previous set to call_to_fast_jit, which also works */
  2896. continue;
  2897. }
  2898. jit_compiler_set_llvm_jit_func_ptr(
  2899. module,
  2900. i + j * group_stride + module->import_function_count,
  2901. (void *)func_addr);
  2902. /* Try to switch to call this llvm jit funtion instead of
  2903. fast jit function from fast jit jitted code */
  2904. jit_compiler_set_call_to_llvm_jit(
  2905. module,
  2906. i + j * group_stride + module->import_function_count);
  2907. #endif
  2908. }
  2909. }
  2910. if (module->orcjit_stop_compiling) {
  2911. break;
  2912. }
  2913. }
  2914. #endif
  2915. return NULL;
  2916. }
  2917. static void
  2918. orcjit_stop_compile_threads(WASMModule *module)
  2919. {
  2920. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  2921. / sizeof(OrcJitThreadArg));
  2922. module->orcjit_stop_compiling = true;
  2923. for (i = 0; i < thread_num; i++) {
  2924. if (module->orcjit_threads[i])
  2925. os_thread_join(module->orcjit_threads[i], NULL);
  2926. }
  2927. }
  2928. static bool
  2929. compile_jit_functions(WASMModule *module, char *error_buf,
  2930. uint32 error_buf_size)
  2931. {
  2932. uint32 thread_num =
  2933. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  2934. uint32 i, j;
  2935. bh_print_time("Begin to compile jit functions");
  2936. /* Create threads to compile the jit functions */
  2937. for (i = 0; i < thread_num && i < module->function_count; i++) {
  2938. #if WASM_ENABLE_JIT != 0
  2939. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  2940. #endif
  2941. module->orcjit_thread_args[i].module = module;
  2942. module->orcjit_thread_args[i].group_idx = i;
  2943. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  2944. (void *)&module->orcjit_thread_args[i],
  2945. APP_THREAD_STACK_SIZE_DEFAULT)
  2946. != 0) {
  2947. set_error_buf(error_buf, error_buf_size,
  2948. "create orcjit compile thread failed");
  2949. /* Terminate the threads created */
  2950. module->orcjit_stop_compiling = true;
  2951. for (j = 0; j < i; j++) {
  2952. os_thread_join(module->orcjit_threads[j], NULL);
  2953. }
  2954. return false;
  2955. }
  2956. }
  2957. #if WASM_ENABLE_LAZY_JIT == 0
  2958. /* Wait until all jit functions are compiled for eager mode */
  2959. for (i = 0; i < thread_num; i++) {
  2960. if (module->orcjit_threads[i])
  2961. os_thread_join(module->orcjit_threads[i], NULL);
  2962. }
  2963. #if WASM_ENABLE_FAST_JIT != 0
  2964. /* Ensure all the fast-jit functions are compiled */
  2965. for (i = 0; i < module->function_count; i++) {
  2966. if (!jit_compiler_is_compiled(module,
  2967. i + module->import_function_count)) {
  2968. set_error_buf(error_buf, error_buf_size,
  2969. "failed to compile fast jit function");
  2970. return false;
  2971. }
  2972. }
  2973. #endif
  2974. #if WASM_ENABLE_JIT != 0
  2975. /* Ensure all the llvm-jit functions are compiled */
  2976. for (i = 0; i < module->function_count; i++) {
  2977. if (!module->func_ptrs_compiled[i]) {
  2978. set_error_buf(error_buf, error_buf_size,
  2979. "failed to compile llvm jit function");
  2980. return false;
  2981. }
  2982. }
  2983. #endif
  2984. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  2985. bh_print_time("End compile jit functions");
  2986. return true;
  2987. }
  2988. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  2989. static bool
  2990. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2991. uint32 cur_func_idx, char *error_buf,
  2992. uint32 error_buf_size);
  2993. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2994. void **
  2995. wasm_interp_get_handle_table();
  2996. static void **handle_table;
  2997. #endif
  2998. static bool
  2999. load_from_sections(WASMModule *module, WASMSection *sections,
  3000. bool is_load_from_file_buf, char *error_buf,
  3001. uint32 error_buf_size)
  3002. {
  3003. WASMExport *export;
  3004. WASMSection *section = sections;
  3005. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  3006. *buf_func = NULL, *buf_func_end = NULL;
  3007. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  3008. WASMGlobal *aux_stack_top_global = NULL, *global;
  3009. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  3010. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  3011. uint32 aux_data_end_global_index = (uint32)-1;
  3012. uint32 aux_heap_base_global_index = (uint32)-1;
  3013. WASMType *func_type;
  3014. /* Find code and function sections if have */
  3015. while (section) {
  3016. if (section->section_type == SECTION_TYPE_CODE) {
  3017. buf_code = section->section_body;
  3018. buf_code_end = buf_code + section->section_body_size;
  3019. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  3020. module->buf_code = (uint8 *)buf_code;
  3021. module->buf_code_size = section->section_body_size;
  3022. #endif
  3023. }
  3024. else if (section->section_type == SECTION_TYPE_FUNC) {
  3025. buf_func = section->section_body;
  3026. buf_func_end = buf_func + section->section_body_size;
  3027. }
  3028. section = section->next;
  3029. }
  3030. section = sections;
  3031. while (section) {
  3032. buf = section->section_body;
  3033. buf_end = buf + section->section_body_size;
  3034. switch (section->section_type) {
  3035. case SECTION_TYPE_USER:
  3036. /* unsupported user section, ignore it. */
  3037. if (!load_user_section(buf, buf_end, module,
  3038. is_load_from_file_buf, error_buf,
  3039. error_buf_size))
  3040. return false;
  3041. break;
  3042. case SECTION_TYPE_TYPE:
  3043. if (!load_type_section(buf, buf_end, module, error_buf,
  3044. error_buf_size))
  3045. return false;
  3046. break;
  3047. case SECTION_TYPE_IMPORT:
  3048. if (!load_import_section(buf, buf_end, module,
  3049. is_load_from_file_buf, error_buf,
  3050. error_buf_size))
  3051. return false;
  3052. break;
  3053. case SECTION_TYPE_FUNC:
  3054. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  3055. module, error_buf, error_buf_size))
  3056. return false;
  3057. break;
  3058. case SECTION_TYPE_TABLE:
  3059. if (!load_table_section(buf, buf_end, module, error_buf,
  3060. error_buf_size))
  3061. return false;
  3062. break;
  3063. case SECTION_TYPE_MEMORY:
  3064. if (!load_memory_section(buf, buf_end, module, error_buf,
  3065. error_buf_size))
  3066. return false;
  3067. break;
  3068. case SECTION_TYPE_GLOBAL:
  3069. if (!load_global_section(buf, buf_end, module, error_buf,
  3070. error_buf_size))
  3071. return false;
  3072. break;
  3073. case SECTION_TYPE_EXPORT:
  3074. if (!load_export_section(buf, buf_end, module,
  3075. is_load_from_file_buf, error_buf,
  3076. error_buf_size))
  3077. return false;
  3078. break;
  3079. case SECTION_TYPE_START:
  3080. if (!load_start_section(buf, buf_end, module, error_buf,
  3081. error_buf_size))
  3082. return false;
  3083. break;
  3084. case SECTION_TYPE_ELEM:
  3085. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  3086. error_buf_size))
  3087. return false;
  3088. break;
  3089. case SECTION_TYPE_CODE:
  3090. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  3091. module, error_buf, error_buf_size))
  3092. return false;
  3093. break;
  3094. case SECTION_TYPE_DATA:
  3095. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  3096. error_buf_size))
  3097. return false;
  3098. break;
  3099. #if WASM_ENABLE_BULK_MEMORY != 0
  3100. case SECTION_TYPE_DATACOUNT:
  3101. if (!load_datacount_section(buf, buf_end, module, error_buf,
  3102. error_buf_size))
  3103. return false;
  3104. break;
  3105. #endif
  3106. default:
  3107. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3108. return false;
  3109. }
  3110. section = section->next;
  3111. }
  3112. module->aux_data_end_global_index = (uint32)-1;
  3113. module->aux_heap_base_global_index = (uint32)-1;
  3114. module->aux_stack_top_global_index = (uint32)-1;
  3115. /* Resolve auxiliary data/stack/heap info and reset memory info */
  3116. export = module->exports;
  3117. for (i = 0; i < module->export_count; i++, export ++) {
  3118. if (export->kind == EXPORT_KIND_GLOBAL) {
  3119. if (!strcmp(export->name, "__heap_base")) {
  3120. global_index = export->index - module->import_global_count;
  3121. global = module->globals + global_index;
  3122. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  3123. && global->init_expr.init_expr_type
  3124. == INIT_EXPR_TYPE_I32_CONST) {
  3125. aux_heap_base_global = global;
  3126. aux_heap_base = global->init_expr.u.i32;
  3127. aux_heap_base_global_index = export->index;
  3128. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  3129. aux_heap_base);
  3130. }
  3131. }
  3132. else if (!strcmp(export->name, "__data_end")) {
  3133. global_index = export->index - module->import_global_count;
  3134. global = module->globals + global_index;
  3135. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  3136. && global->init_expr.init_expr_type
  3137. == INIT_EXPR_TYPE_I32_CONST) {
  3138. aux_data_end_global = global;
  3139. aux_data_end = global->init_expr.u.i32;
  3140. aux_data_end_global_index = export->index;
  3141. LOG_VERBOSE("Found aux __data_end global, value: %d",
  3142. aux_data_end);
  3143. aux_data_end = align_uint(aux_data_end, 16);
  3144. }
  3145. }
  3146. /* For module compiled with -pthread option, the global is:
  3147. [0] stack_top <-- 0
  3148. [1] tls_pointer
  3149. [2] tls_size
  3150. [3] data_end <-- 3
  3151. [4] global_base
  3152. [5] heap_base <-- 5
  3153. [6] dso_handle
  3154. For module compiled without -pthread option:
  3155. [0] stack_top <-- 0
  3156. [1] data_end <-- 1
  3157. [2] global_base
  3158. [3] heap_base <-- 3
  3159. [4] dso_handle
  3160. */
  3161. if (aux_data_end_global && aux_heap_base_global
  3162. && aux_data_end <= aux_heap_base) {
  3163. module->aux_data_end_global_index = aux_data_end_global_index;
  3164. module->aux_data_end = aux_data_end;
  3165. module->aux_heap_base_global_index = aux_heap_base_global_index;
  3166. module->aux_heap_base = aux_heap_base;
  3167. /* Resolve aux stack top global */
  3168. for (global_index = 0; global_index < module->global_count;
  3169. global_index++) {
  3170. global = module->globals + global_index;
  3171. if (global->is_mutable /* heap_base and data_end is
  3172. not mutable */
  3173. && global->type == VALUE_TYPE_I32
  3174. && global->init_expr.init_expr_type
  3175. == INIT_EXPR_TYPE_I32_CONST
  3176. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  3177. aux_stack_top_global = global;
  3178. aux_stack_top = (uint32)global->init_expr.u.i32;
  3179. module->aux_stack_top_global_index =
  3180. module->import_global_count + global_index;
  3181. module->aux_stack_bottom = aux_stack_top;
  3182. module->aux_stack_size =
  3183. aux_stack_top > aux_data_end
  3184. ? aux_stack_top - aux_data_end
  3185. : aux_stack_top;
  3186. LOG_VERBOSE("Found aux stack top global, value: %d, "
  3187. "global index: %d, stack size: %d",
  3188. aux_stack_top, global_index,
  3189. module->aux_stack_size);
  3190. break;
  3191. }
  3192. }
  3193. if (!aux_stack_top_global) {
  3194. /* Auxiliary stack global isn't found, it must be unused
  3195. in the wasm app, as if it is used, the global must be
  3196. defined. Here we set it to __heap_base global and set
  3197. its size to 0. */
  3198. aux_stack_top_global = aux_heap_base_global;
  3199. aux_stack_top = aux_heap_base;
  3200. module->aux_stack_top_global_index =
  3201. module->aux_heap_base_global_index;
  3202. module->aux_stack_bottom = aux_stack_top;
  3203. module->aux_stack_size = 0;
  3204. }
  3205. break;
  3206. }
  3207. }
  3208. }
  3209. module->malloc_function = (uint32)-1;
  3210. module->free_function = (uint32)-1;
  3211. module->retain_function = (uint32)-1;
  3212. /* Resolve malloc/free function exported by wasm module */
  3213. export = module->exports;
  3214. for (i = 0; i < module->export_count; i++, export ++) {
  3215. if (export->kind == EXPORT_KIND_FUNC) {
  3216. if (!strcmp(export->name, "malloc")
  3217. && export->index >= module->import_function_count) {
  3218. func_index = export->index - module->import_function_count;
  3219. func_type = module->functions[func_index]->func_type;
  3220. if (func_type->param_count == 1 && func_type->result_count == 1
  3221. && func_type->types[0] == VALUE_TYPE_I32
  3222. && func_type->types[1] == VALUE_TYPE_I32) {
  3223. bh_assert(module->malloc_function == (uint32)-1);
  3224. module->malloc_function = export->index;
  3225. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  3226. export->name, export->index);
  3227. }
  3228. }
  3229. else if (!strcmp(export->name, "__new")
  3230. && export->index >= module->import_function_count) {
  3231. /* __new && __pin for AssemblyScript */
  3232. func_index = export->index - module->import_function_count;
  3233. func_type = module->functions[func_index]->func_type;
  3234. if (func_type->param_count == 2 && func_type->result_count == 1
  3235. && func_type->types[0] == VALUE_TYPE_I32
  3236. && func_type->types[1] == VALUE_TYPE_I32
  3237. && func_type->types[2] == VALUE_TYPE_I32) {
  3238. uint32 j;
  3239. WASMExport *export_tmp;
  3240. bh_assert(module->malloc_function == (uint32)-1);
  3241. module->malloc_function = export->index;
  3242. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  3243. export->name, export->index);
  3244. /* resolve retain function.
  3245. If not found, reset malloc function index */
  3246. export_tmp = module->exports;
  3247. for (j = 0; j < module->export_count; j++, export_tmp++) {
  3248. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  3249. && (!strcmp(export_tmp->name, "__retain")
  3250. || (!strcmp(export_tmp->name, "__pin")))
  3251. && (export_tmp->index
  3252. >= module->import_function_count)) {
  3253. func_index = export_tmp->index
  3254. - module->import_function_count;
  3255. func_type =
  3256. module->functions[func_index]->func_type;
  3257. if (func_type->param_count == 1
  3258. && func_type->result_count == 1
  3259. && func_type->types[0] == VALUE_TYPE_I32
  3260. && func_type->types[1] == VALUE_TYPE_I32) {
  3261. bh_assert(module->retain_function
  3262. == (uint32)-1);
  3263. module->retain_function = export_tmp->index;
  3264. LOG_VERBOSE("Found retain function, name: %s, "
  3265. "index: %u",
  3266. export_tmp->name,
  3267. export_tmp->index);
  3268. break;
  3269. }
  3270. }
  3271. }
  3272. if (j == module->export_count) {
  3273. module->malloc_function = (uint32)-1;
  3274. LOG_VERBOSE("Can't find retain function,"
  3275. "reset malloc function index to -1");
  3276. }
  3277. }
  3278. }
  3279. else if (((!strcmp(export->name, "free"))
  3280. || (!strcmp(export->name, "__release"))
  3281. || (!strcmp(export->name, "__unpin")))
  3282. && export->index >= module->import_function_count) {
  3283. func_index = export->index - module->import_function_count;
  3284. func_type = module->functions[func_index]->func_type;
  3285. if (func_type->param_count == 1 && func_type->result_count == 0
  3286. && func_type->types[0] == VALUE_TYPE_I32) {
  3287. bh_assert(module->free_function == (uint32)-1);
  3288. module->free_function = export->index;
  3289. LOG_VERBOSE("Found free function, name: %s, index: %u",
  3290. export->name, export->index);
  3291. }
  3292. }
  3293. }
  3294. }
  3295. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  3296. handle_table = wasm_interp_get_handle_table();
  3297. #endif
  3298. for (i = 0; i < module->function_count; i++) {
  3299. WASMFunction *func = module->functions[i];
  3300. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  3301. error_buf_size)) {
  3302. return false;
  3303. }
  3304. if (i == module->function_count - 1
  3305. && func->code + func->code_size != buf_code_end) {
  3306. set_error_buf(error_buf, error_buf_size,
  3307. "code section size mismatch");
  3308. return false;
  3309. }
  3310. }
  3311. if (!module->possible_memory_grow) {
  3312. WASMMemoryImport *memory_import;
  3313. WASMMemory *memory;
  3314. if (aux_data_end_global && aux_heap_base_global
  3315. && aux_stack_top_global) {
  3316. uint64 init_memory_size;
  3317. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  3318. if (module->import_memory_count) {
  3319. memory_import = &module->import_memories[0].u.memory;
  3320. init_memory_size = (uint64)memory_import->num_bytes_per_page
  3321. * memory_import->init_page_count;
  3322. if (shrunk_memory_size <= init_memory_size) {
  3323. /* Reset memory info to decrease memory usage */
  3324. memory_import->num_bytes_per_page = shrunk_memory_size;
  3325. memory_import->init_page_count = 1;
  3326. LOG_VERBOSE("Shrink import memory size to %d",
  3327. shrunk_memory_size);
  3328. }
  3329. }
  3330. if (module->memory_count) {
  3331. memory = &module->memories[0];
  3332. init_memory_size = (uint64)memory->num_bytes_per_page
  3333. * memory->init_page_count;
  3334. if (shrunk_memory_size <= init_memory_size) {
  3335. /* Reset memory info to decrease memory usage */
  3336. memory->num_bytes_per_page = shrunk_memory_size;
  3337. memory->init_page_count = 1;
  3338. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  3339. }
  3340. }
  3341. }
  3342. #if WASM_ENABLE_MULTI_MODULE == 0
  3343. if (module->import_memory_count) {
  3344. memory_import = &module->import_memories[0].u.memory;
  3345. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  3346. memory_import->num_bytes_per_page *=
  3347. memory_import->init_page_count;
  3348. else
  3349. memory_import->num_bytes_per_page = UINT32_MAX;
  3350. if (memory_import->init_page_count > 0)
  3351. memory_import->init_page_count = memory_import->max_page_count =
  3352. 1;
  3353. else
  3354. memory_import->init_page_count = memory_import->max_page_count =
  3355. 0;
  3356. }
  3357. if (module->memory_count) {
  3358. memory = &module->memories[0];
  3359. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  3360. memory->num_bytes_per_page *= memory->init_page_count;
  3361. else
  3362. memory->num_bytes_per_page = UINT32_MAX;
  3363. if (memory->init_page_count > 0)
  3364. memory->init_page_count = memory->max_page_count = 1;
  3365. else
  3366. memory->init_page_count = memory->max_page_count = 0;
  3367. }
  3368. #endif
  3369. }
  3370. calculate_global_data_offset(module);
  3371. #if WASM_ENABLE_FAST_JIT != 0
  3372. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  3373. return false;
  3374. }
  3375. #endif
  3376. #if WASM_ENABLE_JIT != 0
  3377. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  3378. return false;
  3379. }
  3380. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  3381. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  3382. return false;
  3383. }
  3384. #else
  3385. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  3386. thread fast jit execution, since applying llvm optimizations in
  3387. aot_compile_wasm may cost a lot of time.
  3388. Create thread with enough native stack to apply llvm optimizations */
  3389. if (os_thread_create(&module->llvm_jit_init_thread,
  3390. init_llvm_jit_functions_stage2_callback,
  3391. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  3392. != 0) {
  3393. set_error_buf(error_buf, error_buf_size,
  3394. "create orcjit compile thread failed");
  3395. return false;
  3396. }
  3397. #endif
  3398. #endif
  3399. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3400. /* Create threads to compile the jit functions */
  3401. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  3402. return false;
  3403. }
  3404. #endif
  3405. #if WASM_ENABLE_MEMORY_TRACING != 0
  3406. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  3407. #endif
  3408. return true;
  3409. }
  3410. static WASMModule *
  3411. create_module(char *error_buf, uint32 error_buf_size)
  3412. {
  3413. WASMModule *module =
  3414. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  3415. bh_list_status ret;
  3416. if (!module) {
  3417. return NULL;
  3418. }
  3419. module->module_type = Wasm_Module_Bytecode;
  3420. /* Set start_function to -1, means no start function */
  3421. module->start_function = (uint32)-1;
  3422. #if WASM_ENABLE_FAST_INTERP == 0
  3423. module->br_table_cache_list = &module->br_table_cache_list_head;
  3424. ret = bh_list_init(module->br_table_cache_list);
  3425. bh_assert(ret == BH_LIST_SUCCESS);
  3426. #endif
  3427. #if WASM_ENABLE_MULTI_MODULE != 0
  3428. module->import_module_list = &module->import_module_list_head;
  3429. ret = bh_list_init(module->import_module_list);
  3430. bh_assert(ret == BH_LIST_SUCCESS);
  3431. #endif
  3432. #if WASM_ENABLE_DEBUG_INTERP != 0
  3433. ret = bh_list_init(&module->fast_opcode_list);
  3434. bh_assert(ret == BH_LIST_SUCCESS);
  3435. #endif
  3436. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  3437. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3438. && WASM_ENABLE_LAZY_JIT != 0)
  3439. if (os_mutex_init(&module->instance_list_lock) != 0) {
  3440. set_error_buf(error_buf, error_buf_size,
  3441. "init instance list lock failed");
  3442. wasm_runtime_free(module);
  3443. return NULL;
  3444. }
  3445. #endif
  3446. (void)ret;
  3447. return module;
  3448. }
  3449. #if WASM_ENABLE_DEBUG_INTERP != 0
  3450. static bool
  3451. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  3452. uint32 error_buf_size)
  3453. {
  3454. WASMFastOPCodeNode *fast_op =
  3455. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  3456. if (fast_op) {
  3457. fast_op->offset = pos - module->load_addr;
  3458. fast_op->orig_op = orig_op;
  3459. bh_list_insert(&module->fast_opcode_list, fast_op);
  3460. }
  3461. return fast_op ? true : false;
  3462. }
  3463. #endif
  3464. WASMModule *
  3465. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  3466. uint32 error_buf_size)
  3467. {
  3468. WASMModule *module = create_module(error_buf, error_buf_size);
  3469. if (!module)
  3470. return NULL;
  3471. if (!load_from_sections(module, section_list, false, error_buf,
  3472. error_buf_size)) {
  3473. wasm_loader_unload(module);
  3474. return NULL;
  3475. }
  3476. LOG_VERBOSE("Load module from sections success.\n");
  3477. return module;
  3478. }
  3479. static void
  3480. destroy_sections(WASMSection *section_list)
  3481. {
  3482. WASMSection *section = section_list, *next;
  3483. while (section) {
  3484. next = section->next;
  3485. wasm_runtime_free(section);
  3486. section = next;
  3487. }
  3488. }
  3489. /* clang-format off */
  3490. static uint8 section_ids[] = {
  3491. SECTION_TYPE_USER,
  3492. SECTION_TYPE_TYPE,
  3493. SECTION_TYPE_IMPORT,
  3494. SECTION_TYPE_FUNC,
  3495. SECTION_TYPE_TABLE,
  3496. SECTION_TYPE_MEMORY,
  3497. SECTION_TYPE_GLOBAL,
  3498. SECTION_TYPE_EXPORT,
  3499. SECTION_TYPE_START,
  3500. SECTION_TYPE_ELEM,
  3501. #if WASM_ENABLE_BULK_MEMORY != 0
  3502. SECTION_TYPE_DATACOUNT,
  3503. #endif
  3504. SECTION_TYPE_CODE,
  3505. SECTION_TYPE_DATA
  3506. };
  3507. /* clang-format on */
  3508. static uint8
  3509. get_section_index(uint8 section_type)
  3510. {
  3511. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  3512. for (uint8 i = 0; i < max_id; i++) {
  3513. if (section_type == section_ids[i])
  3514. return i;
  3515. }
  3516. return (uint8)-1;
  3517. }
  3518. static bool
  3519. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  3520. char *error_buf, uint32 error_buf_size)
  3521. {
  3522. WASMSection *section_list_end = NULL, *section;
  3523. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  3524. uint8 section_type, section_index, last_section_index = (uint8)-1;
  3525. uint32 section_size;
  3526. bh_assert(!*p_section_list);
  3527. p += 8;
  3528. while (p < p_end) {
  3529. CHECK_BUF(p, p_end, 1);
  3530. section_type = read_uint8(p);
  3531. section_index = get_section_index(section_type);
  3532. if (section_index != (uint8)-1) {
  3533. if (section_type != SECTION_TYPE_USER) {
  3534. /* Custom sections may be inserted at any place,
  3535. while other sections must occur at most once
  3536. and in prescribed order. */
  3537. if (last_section_index != (uint8)-1
  3538. && (section_index <= last_section_index)) {
  3539. set_error_buf(error_buf, error_buf_size,
  3540. "unexpected content after last section or "
  3541. "junk after last section");
  3542. return false;
  3543. }
  3544. last_section_index = section_index;
  3545. }
  3546. read_leb_uint32(p, p_end, section_size);
  3547. CHECK_BUF1(p, p_end, section_size);
  3548. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  3549. error_buf_size))) {
  3550. return false;
  3551. }
  3552. section->section_type = section_type;
  3553. section->section_body = (uint8 *)p;
  3554. section->section_body_size = section_size;
  3555. if (!section_list_end)
  3556. *p_section_list = section_list_end = section;
  3557. else {
  3558. section_list_end->next = section;
  3559. section_list_end = section;
  3560. }
  3561. p += section_size;
  3562. }
  3563. else {
  3564. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3565. return false;
  3566. }
  3567. }
  3568. return true;
  3569. fail:
  3570. return false;
  3571. }
  3572. static void
  3573. exchange32(uint8 *p_data)
  3574. {
  3575. uint8 value = *p_data;
  3576. *p_data = *(p_data + 3);
  3577. *(p_data + 3) = value;
  3578. value = *(p_data + 1);
  3579. *(p_data + 1) = *(p_data + 2);
  3580. *(p_data + 2) = value;
  3581. }
  3582. static union {
  3583. int a;
  3584. char b;
  3585. } __ue = { .a = 1 };
  3586. #define is_little_endian() (__ue.b == 1)
  3587. static bool
  3588. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  3589. uint32 error_buf_size)
  3590. {
  3591. const uint8 *buf_end = buf + size;
  3592. const uint8 *p = buf, *p_end = buf_end;
  3593. uint32 magic_number, version;
  3594. WASMSection *section_list = NULL;
  3595. CHECK_BUF1(p, p_end, sizeof(uint32));
  3596. magic_number = read_uint32(p);
  3597. if (!is_little_endian())
  3598. exchange32((uint8 *)&magic_number);
  3599. if (magic_number != WASM_MAGIC_NUMBER) {
  3600. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  3601. return false;
  3602. }
  3603. CHECK_BUF1(p, p_end, sizeof(uint32));
  3604. version = read_uint32(p);
  3605. if (!is_little_endian())
  3606. exchange32((uint8 *)&version);
  3607. if (version != WASM_CURRENT_VERSION) {
  3608. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  3609. return false;
  3610. }
  3611. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  3612. || !load_from_sections(module, section_list, true, error_buf,
  3613. error_buf_size)) {
  3614. destroy_sections(section_list);
  3615. return false;
  3616. }
  3617. destroy_sections(section_list);
  3618. return true;
  3619. fail:
  3620. return false;
  3621. }
  3622. #if WASM_ENABLE_LIBC_WASI != 0
  3623. /**
  3624. * refer to
  3625. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  3626. */
  3627. static bool
  3628. check_wasi_abi_compatibility(const WASMModule *module,
  3629. #if WASM_ENABLE_MULTI_MODULE != 0
  3630. bool main_module,
  3631. #endif
  3632. char *error_buf, uint32 error_buf_size)
  3633. {
  3634. /**
  3635. * be careful with:
  3636. * wasi compatiable modules(command/reactor) which don't import any wasi
  3637. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  3638. * reactor can depend on nothing. At the same time, each has its own entry
  3639. * point.
  3640. *
  3641. * observations:
  3642. * - clang always injects `_start` into a command
  3643. * - clang always injects `_initialize` into a reactor
  3644. * - `iwasm -f` allows to run a function in the reactor
  3645. *
  3646. * strong assumptions:
  3647. * - no one will define either `_start` or `_initialize` on purpose
  3648. * - `_start` should always be `void _start(void)`
  3649. * - `_initialize` should always be `void _initialize(void)`
  3650. *
  3651. */
  3652. /* clang-format off */
  3653. /**
  3654. *
  3655. * | | import_wasi_api True | | import_wasi_api False | |
  3656. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  3657. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  3658. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  3659. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  3660. */
  3661. /* clang-format on */
  3662. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  3663. /* (func (export "_start") (...) */
  3664. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  3665. error_buf, error_buf_size);
  3666. if (start) {
  3667. WASMType *func_type =
  3668. module->functions[start->index - module->import_function_count]
  3669. ->func_type;
  3670. if (func_type->param_count || func_type->result_count) {
  3671. set_error_buf(error_buf, error_buf_size,
  3672. "the signature of builtin _start function is wrong");
  3673. return false;
  3674. }
  3675. }
  3676. /* (func (export "_initialize") (...) */
  3677. initialize = wasm_loader_find_export(
  3678. module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
  3679. if (initialize) {
  3680. WASMType *func_type =
  3681. module->functions[initialize->index - module->import_function_count]
  3682. ->func_type;
  3683. if (func_type->param_count || func_type->result_count) {
  3684. set_error_buf(
  3685. error_buf, error_buf_size,
  3686. "the signature of builtin _initialize function is wrong");
  3687. return false;
  3688. }
  3689. }
  3690. /* filter out non-wasi compatiable modules */
  3691. if (!module->import_wasi_api && !start && !initialize) {
  3692. return true;
  3693. }
  3694. /* should have one at least */
  3695. if (module->import_wasi_api && !start && !initialize) {
  3696. LOG_WARNING("warning: a module with WASI apis should be either "
  3697. "a command or a reactor");
  3698. }
  3699. /*
  3700. * there is at least one of `_start` and `_initialize` in below cases.
  3701. * according to the assumption, they should be all wasi compatiable
  3702. */
  3703. /* always can not have both at the same time */
  3704. if (start && initialize) {
  3705. set_error_buf(
  3706. error_buf, error_buf_size,
  3707. "neither a command nor a reactor can both have _start function "
  3708. "and _initialize function at the same time");
  3709. return false;
  3710. }
  3711. #if WASM_ENABLE_MULTI_MODULE != 0
  3712. /* filter out commands (with `_start`) cases */
  3713. if (start && !main_module) {
  3714. set_error_buf(
  3715. error_buf, error_buf_size,
  3716. "a command (with _start function) can not be a sub-module");
  3717. return false;
  3718. }
  3719. #endif
  3720. /*
  3721. * it is ok a reactor acts as a main module,
  3722. * so skip the check about (with `_initialize`)
  3723. */
  3724. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  3725. error_buf, error_buf_size);
  3726. if (!memory
  3727. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  3728. /*
  3729. * with wasi-threads, it's still an open question if a memory
  3730. * should be exported.
  3731. *
  3732. * https://github.com/WebAssembly/wasi-threads/issues/22
  3733. * https://github.com/WebAssembly/WASI/issues/502
  3734. *
  3735. * Note: this code assumes the number of memories is at most 1.
  3736. */
  3737. && module->import_memory_count == 0
  3738. #endif
  3739. ) {
  3740. set_error_buf(error_buf, error_buf_size,
  3741. "a module with WASI apis must export memory by default");
  3742. return false;
  3743. }
  3744. return true;
  3745. }
  3746. #endif
  3747. WASMModule *
  3748. wasm_loader_load(uint8 *buf, uint32 size,
  3749. #if WASM_ENABLE_MULTI_MODULE != 0
  3750. bool main_module,
  3751. #endif
  3752. char *error_buf, uint32 error_buf_size)
  3753. {
  3754. WASMModule *module = create_module(error_buf, error_buf_size);
  3755. if (!module) {
  3756. return NULL;
  3757. }
  3758. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
  3759. module->load_addr = (uint8 *)buf;
  3760. module->load_size = size;
  3761. #endif
  3762. if (!load(buf, size, module, error_buf, error_buf_size)) {
  3763. goto fail;
  3764. }
  3765. #if WASM_ENABLE_LIBC_WASI != 0
  3766. /* Check the WASI application ABI */
  3767. if (!check_wasi_abi_compatibility(module,
  3768. #if WASM_ENABLE_MULTI_MODULE != 0
  3769. main_module,
  3770. #endif
  3771. error_buf, error_buf_size)) {
  3772. goto fail;
  3773. }
  3774. #endif
  3775. LOG_VERBOSE("Load module success.\n");
  3776. return module;
  3777. fail:
  3778. wasm_loader_unload(module);
  3779. return NULL;
  3780. }
  3781. void
  3782. wasm_loader_unload(WASMModule *module)
  3783. {
  3784. uint32 i;
  3785. if (!module)
  3786. return;
  3787. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3788. && WASM_ENABLE_LAZY_JIT != 0
  3789. module->orcjit_stop_compiling = true;
  3790. if (module->llvm_jit_init_thread)
  3791. os_thread_join(module->llvm_jit_init_thread, NULL);
  3792. #endif
  3793. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3794. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  3795. module internal data after they were freed */
  3796. orcjit_stop_compile_threads(module);
  3797. #endif
  3798. #if WASM_ENABLE_JIT != 0
  3799. if (module->func_ptrs)
  3800. wasm_runtime_free(module->func_ptrs);
  3801. if (module->comp_ctx)
  3802. aot_destroy_comp_context(module->comp_ctx);
  3803. if (module->comp_data)
  3804. aot_destroy_comp_data(module->comp_data);
  3805. #endif
  3806. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3807. && WASM_ENABLE_LAZY_JIT != 0
  3808. if (module->tierup_wait_lock_inited) {
  3809. os_mutex_destroy(&module->tierup_wait_lock);
  3810. os_cond_destroy(&module->tierup_wait_cond);
  3811. }
  3812. #endif
  3813. if (module->types) {
  3814. for (i = 0; i < module->type_count; i++) {
  3815. if (module->types[i])
  3816. destroy_wasm_type(module->types[i]);
  3817. }
  3818. wasm_runtime_free(module->types);
  3819. }
  3820. if (module->imports)
  3821. wasm_runtime_free(module->imports);
  3822. if (module->functions) {
  3823. for (i = 0; i < module->function_count; i++) {
  3824. if (module->functions[i]) {
  3825. if (module->functions[i]->local_offsets)
  3826. wasm_runtime_free(module->functions[i]->local_offsets);
  3827. #if WASM_ENABLE_FAST_INTERP != 0
  3828. if (module->functions[i]->code_compiled)
  3829. wasm_runtime_free(module->functions[i]->code_compiled);
  3830. if (module->functions[i]->consts)
  3831. wasm_runtime_free(module->functions[i]->consts);
  3832. #endif
  3833. #if WASM_ENABLE_FAST_JIT != 0
  3834. if (module->functions[i]->fast_jit_jitted_code) {
  3835. jit_code_cache_free(
  3836. module->functions[i]->fast_jit_jitted_code);
  3837. }
  3838. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3839. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  3840. jit_code_cache_free(
  3841. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  3842. }
  3843. #endif
  3844. #endif
  3845. wasm_runtime_free(module->functions[i]);
  3846. }
  3847. }
  3848. wasm_runtime_free(module->functions);
  3849. }
  3850. if (module->tables)
  3851. wasm_runtime_free(module->tables);
  3852. if (module->memories)
  3853. wasm_runtime_free(module->memories);
  3854. if (module->globals)
  3855. wasm_runtime_free(module->globals);
  3856. if (module->exports)
  3857. wasm_runtime_free(module->exports);
  3858. if (module->table_segments) {
  3859. for (i = 0; i < module->table_seg_count; i++) {
  3860. if (module->table_segments[i].func_indexes)
  3861. wasm_runtime_free(module->table_segments[i].func_indexes);
  3862. }
  3863. wasm_runtime_free(module->table_segments);
  3864. }
  3865. if (module->data_segments) {
  3866. for (i = 0; i < module->data_seg_count; i++) {
  3867. if (module->data_segments[i])
  3868. wasm_runtime_free(module->data_segments[i]);
  3869. }
  3870. wasm_runtime_free(module->data_segments);
  3871. }
  3872. if (module->const_str_list) {
  3873. StringNode *node = module->const_str_list, *node_next;
  3874. while (node) {
  3875. node_next = node->next;
  3876. wasm_runtime_free(node);
  3877. node = node_next;
  3878. }
  3879. }
  3880. #if WASM_ENABLE_FAST_INTERP == 0
  3881. if (module->br_table_cache_list) {
  3882. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  3883. BrTableCache *node_next;
  3884. while (node) {
  3885. node_next = bh_list_elem_next(node);
  3886. wasm_runtime_free(node);
  3887. node = node_next;
  3888. }
  3889. }
  3890. #endif
  3891. #if WASM_ENABLE_MULTI_MODULE != 0
  3892. /* just release the sub module list */
  3893. if (module->import_module_list) {
  3894. WASMRegisteredModule *node =
  3895. bh_list_first_elem(module->import_module_list);
  3896. while (node) {
  3897. WASMRegisteredModule *next = bh_list_elem_next(node);
  3898. bh_list_remove(module->import_module_list, node);
  3899. /*
  3900. * unload(sub_module) will be trigged during runtime_destroy().
  3901. * every module in the global module list will be unloaded one by
  3902. * one. so don't worry.
  3903. */
  3904. wasm_runtime_free(node);
  3905. /*
  3906. * the module file reading buffer will be released
  3907. * in runtime_destroy()
  3908. */
  3909. node = next;
  3910. }
  3911. }
  3912. #endif
  3913. #if WASM_ENABLE_DEBUG_INTERP != 0
  3914. WASMFastOPCodeNode *fast_opcode =
  3915. bh_list_first_elem(&module->fast_opcode_list);
  3916. while (fast_opcode) {
  3917. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  3918. wasm_runtime_free(fast_opcode);
  3919. fast_opcode = next;
  3920. }
  3921. #endif
  3922. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  3923. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3924. && WASM_ENABLE_LAZY_JIT != 0)
  3925. os_mutex_destroy(&module->instance_list_lock);
  3926. #endif
  3927. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3928. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  3929. #endif
  3930. #if WASM_ENABLE_FAST_JIT != 0
  3931. if (module->fast_jit_func_ptrs) {
  3932. wasm_runtime_free(module->fast_jit_func_ptrs);
  3933. }
  3934. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  3935. if (module->fast_jit_thread_locks_inited[i]) {
  3936. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  3937. }
  3938. }
  3939. #endif
  3940. wasm_runtime_free(module);
  3941. }
  3942. bool
  3943. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  3944. const uint8 *start_addr, const uint8 *code_end_addr,
  3945. uint8 label_type, uint8 **p_else_addr,
  3946. uint8 **p_end_addr)
  3947. {
  3948. const uint8 *p = start_addr, *p_end = code_end_addr;
  3949. uint8 *else_addr = NULL;
  3950. char error_buf[128];
  3951. uint32 block_nested_depth = 1, count, i, j, t;
  3952. uint32 error_buf_size = sizeof(error_buf);
  3953. uint8 opcode, u8;
  3954. BlockAddr block_stack[16] = { { 0 } }, *block;
  3955. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3956. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3957. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  3958. if (block[j].start_addr == start_addr) {
  3959. /* Cache hit */
  3960. *p_else_addr = block[j].else_addr;
  3961. *p_end_addr = block[j].end_addr;
  3962. return true;
  3963. }
  3964. }
  3965. /* Cache unhit */
  3966. block_stack[0].start_addr = start_addr;
  3967. while (p < code_end_addr) {
  3968. opcode = *p++;
  3969. #if WASM_ENABLE_DEBUG_INTERP != 0
  3970. op_break_retry:
  3971. #endif
  3972. switch (opcode) {
  3973. case WASM_OP_UNREACHABLE:
  3974. case WASM_OP_NOP:
  3975. break;
  3976. case WASM_OP_BLOCK:
  3977. case WASM_OP_LOOP:
  3978. case WASM_OP_IF:
  3979. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  3980. u8 = read_uint8(p);
  3981. if (block_nested_depth
  3982. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3983. block_stack[block_nested_depth].start_addr = p;
  3984. block_stack[block_nested_depth].else_addr = NULL;
  3985. }
  3986. block_nested_depth++;
  3987. break;
  3988. case EXT_OP_BLOCK:
  3989. case EXT_OP_LOOP:
  3990. case EXT_OP_IF:
  3991. /* block type */
  3992. skip_leb_uint32(p, p_end);
  3993. if (block_nested_depth
  3994. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3995. block_stack[block_nested_depth].start_addr = p;
  3996. block_stack[block_nested_depth].else_addr = NULL;
  3997. }
  3998. block_nested_depth++;
  3999. break;
  4000. case WASM_OP_ELSE:
  4001. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  4002. else_addr = (uint8 *)(p - 1);
  4003. if (block_nested_depth - 1
  4004. < sizeof(block_stack) / sizeof(BlockAddr))
  4005. block_stack[block_nested_depth - 1].else_addr =
  4006. (uint8 *)(p - 1);
  4007. break;
  4008. case WASM_OP_END:
  4009. if (block_nested_depth == 1) {
  4010. if (label_type == LABEL_TYPE_IF)
  4011. *p_else_addr = else_addr;
  4012. *p_end_addr = (uint8 *)(p - 1);
  4013. block_stack[0].end_addr = (uint8 *)(p - 1);
  4014. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  4015. t++) {
  4016. start_addr = block_stack[t].start_addr;
  4017. if (start_addr) {
  4018. i = ((uintptr_t)start_addr)
  4019. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  4020. block =
  4021. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  4022. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  4023. if (!block[j].start_addr)
  4024. break;
  4025. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  4026. memmove(block + 1, block,
  4027. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  4028. * sizeof(BlockAddr));
  4029. j = 0;
  4030. }
  4031. block[j].start_addr = block_stack[t].start_addr;
  4032. block[j].else_addr = block_stack[t].else_addr;
  4033. block[j].end_addr = block_stack[t].end_addr;
  4034. }
  4035. else
  4036. break;
  4037. }
  4038. return true;
  4039. }
  4040. else {
  4041. block_nested_depth--;
  4042. if (block_nested_depth
  4043. < sizeof(block_stack) / sizeof(BlockAddr))
  4044. block_stack[block_nested_depth].end_addr =
  4045. (uint8 *)(p - 1);
  4046. }
  4047. break;
  4048. case WASM_OP_BR:
  4049. case WASM_OP_BR_IF:
  4050. skip_leb_uint32(p, p_end); /* labelidx */
  4051. break;
  4052. case WASM_OP_BR_TABLE:
  4053. read_leb_uint32(p, p_end, count); /* lable num */
  4054. #if WASM_ENABLE_FAST_INTERP != 0
  4055. for (i = 0; i <= count; i++) /* lableidxs */
  4056. skip_leb_uint32(p, p_end);
  4057. #else
  4058. p += count + 1;
  4059. while (*p == WASM_OP_NOP)
  4060. p++;
  4061. #endif
  4062. break;
  4063. #if WASM_ENABLE_FAST_INTERP == 0
  4064. case EXT_OP_BR_TABLE_CACHE:
  4065. read_leb_uint32(p, p_end, count); /* lable num */
  4066. while (*p == WASM_OP_NOP)
  4067. p++;
  4068. break;
  4069. #endif
  4070. case WASM_OP_RETURN:
  4071. break;
  4072. case WASM_OP_CALL:
  4073. #if WASM_ENABLE_TAIL_CALL != 0
  4074. case WASM_OP_RETURN_CALL:
  4075. #endif
  4076. skip_leb_uint32(p, p_end); /* funcidx */
  4077. break;
  4078. case WASM_OP_CALL_INDIRECT:
  4079. #if WASM_ENABLE_TAIL_CALL != 0
  4080. case WASM_OP_RETURN_CALL_INDIRECT:
  4081. #endif
  4082. skip_leb_uint32(p, p_end); /* typeidx */
  4083. CHECK_BUF(p, p_end, 1);
  4084. u8 = read_uint8(p); /* 0x00 */
  4085. break;
  4086. case WASM_OP_DROP:
  4087. case WASM_OP_SELECT:
  4088. case WASM_OP_DROP_64:
  4089. case WASM_OP_SELECT_64:
  4090. break;
  4091. #if WASM_ENABLE_REF_TYPES != 0
  4092. case WASM_OP_SELECT_T:
  4093. skip_leb_uint32(p, p_end); /* vec length */
  4094. CHECK_BUF(p, p_end, 1);
  4095. u8 = read_uint8(p); /* typeidx */
  4096. break;
  4097. case WASM_OP_TABLE_GET:
  4098. case WASM_OP_TABLE_SET:
  4099. skip_leb_uint32(p, p_end); /* table index */
  4100. break;
  4101. case WASM_OP_REF_NULL:
  4102. CHECK_BUF(p, p_end, 1);
  4103. u8 = read_uint8(p); /* type */
  4104. break;
  4105. case WASM_OP_REF_IS_NULL:
  4106. break;
  4107. case WASM_OP_REF_FUNC:
  4108. skip_leb_uint32(p, p_end); /* func index */
  4109. break;
  4110. #endif /* WASM_ENABLE_REF_TYPES */
  4111. case WASM_OP_GET_LOCAL:
  4112. case WASM_OP_SET_LOCAL:
  4113. case WASM_OP_TEE_LOCAL:
  4114. case WASM_OP_GET_GLOBAL:
  4115. case WASM_OP_SET_GLOBAL:
  4116. case WASM_OP_GET_GLOBAL_64:
  4117. case WASM_OP_SET_GLOBAL_64:
  4118. case WASM_OP_SET_GLOBAL_AUX_STACK:
  4119. skip_leb_uint32(p, p_end); /* local index */
  4120. break;
  4121. case EXT_OP_GET_LOCAL_FAST:
  4122. case EXT_OP_SET_LOCAL_FAST:
  4123. case EXT_OP_TEE_LOCAL_FAST:
  4124. CHECK_BUF(p, p_end, 1);
  4125. p++;
  4126. break;
  4127. case WASM_OP_I32_LOAD:
  4128. case WASM_OP_I64_LOAD:
  4129. case WASM_OP_F32_LOAD:
  4130. case WASM_OP_F64_LOAD:
  4131. case WASM_OP_I32_LOAD8_S:
  4132. case WASM_OP_I32_LOAD8_U:
  4133. case WASM_OP_I32_LOAD16_S:
  4134. case WASM_OP_I32_LOAD16_U:
  4135. case WASM_OP_I64_LOAD8_S:
  4136. case WASM_OP_I64_LOAD8_U:
  4137. case WASM_OP_I64_LOAD16_S:
  4138. case WASM_OP_I64_LOAD16_U:
  4139. case WASM_OP_I64_LOAD32_S:
  4140. case WASM_OP_I64_LOAD32_U:
  4141. case WASM_OP_I32_STORE:
  4142. case WASM_OP_I64_STORE:
  4143. case WASM_OP_F32_STORE:
  4144. case WASM_OP_F64_STORE:
  4145. case WASM_OP_I32_STORE8:
  4146. case WASM_OP_I32_STORE16:
  4147. case WASM_OP_I64_STORE8:
  4148. case WASM_OP_I64_STORE16:
  4149. case WASM_OP_I64_STORE32:
  4150. skip_leb_uint32(p, p_end); /* align */
  4151. skip_leb_uint32(p, p_end); /* offset */
  4152. break;
  4153. case WASM_OP_MEMORY_SIZE:
  4154. case WASM_OP_MEMORY_GROW:
  4155. skip_leb_uint32(p, p_end); /* 0x00 */
  4156. break;
  4157. case WASM_OP_I32_CONST:
  4158. skip_leb_int32(p, p_end);
  4159. break;
  4160. case WASM_OP_I64_CONST:
  4161. skip_leb_int64(p, p_end);
  4162. break;
  4163. case WASM_OP_F32_CONST:
  4164. p += sizeof(float32);
  4165. break;
  4166. case WASM_OP_F64_CONST:
  4167. p += sizeof(float64);
  4168. break;
  4169. case WASM_OP_I32_EQZ:
  4170. case WASM_OP_I32_EQ:
  4171. case WASM_OP_I32_NE:
  4172. case WASM_OP_I32_LT_S:
  4173. case WASM_OP_I32_LT_U:
  4174. case WASM_OP_I32_GT_S:
  4175. case WASM_OP_I32_GT_U:
  4176. case WASM_OP_I32_LE_S:
  4177. case WASM_OP_I32_LE_U:
  4178. case WASM_OP_I32_GE_S:
  4179. case WASM_OP_I32_GE_U:
  4180. case WASM_OP_I64_EQZ:
  4181. case WASM_OP_I64_EQ:
  4182. case WASM_OP_I64_NE:
  4183. case WASM_OP_I64_LT_S:
  4184. case WASM_OP_I64_LT_U:
  4185. case WASM_OP_I64_GT_S:
  4186. case WASM_OP_I64_GT_U:
  4187. case WASM_OP_I64_LE_S:
  4188. case WASM_OP_I64_LE_U:
  4189. case WASM_OP_I64_GE_S:
  4190. case WASM_OP_I64_GE_U:
  4191. case WASM_OP_F32_EQ:
  4192. case WASM_OP_F32_NE:
  4193. case WASM_OP_F32_LT:
  4194. case WASM_OP_F32_GT:
  4195. case WASM_OP_F32_LE:
  4196. case WASM_OP_F32_GE:
  4197. case WASM_OP_F64_EQ:
  4198. case WASM_OP_F64_NE:
  4199. case WASM_OP_F64_LT:
  4200. case WASM_OP_F64_GT:
  4201. case WASM_OP_F64_LE:
  4202. case WASM_OP_F64_GE:
  4203. case WASM_OP_I32_CLZ:
  4204. case WASM_OP_I32_CTZ:
  4205. case WASM_OP_I32_POPCNT:
  4206. case WASM_OP_I32_ADD:
  4207. case WASM_OP_I32_SUB:
  4208. case WASM_OP_I32_MUL:
  4209. case WASM_OP_I32_DIV_S:
  4210. case WASM_OP_I32_DIV_U:
  4211. case WASM_OP_I32_REM_S:
  4212. case WASM_OP_I32_REM_U:
  4213. case WASM_OP_I32_AND:
  4214. case WASM_OP_I32_OR:
  4215. case WASM_OP_I32_XOR:
  4216. case WASM_OP_I32_SHL:
  4217. case WASM_OP_I32_SHR_S:
  4218. case WASM_OP_I32_SHR_U:
  4219. case WASM_OP_I32_ROTL:
  4220. case WASM_OP_I32_ROTR:
  4221. case WASM_OP_I64_CLZ:
  4222. case WASM_OP_I64_CTZ:
  4223. case WASM_OP_I64_POPCNT:
  4224. case WASM_OP_I64_ADD:
  4225. case WASM_OP_I64_SUB:
  4226. case WASM_OP_I64_MUL:
  4227. case WASM_OP_I64_DIV_S:
  4228. case WASM_OP_I64_DIV_U:
  4229. case WASM_OP_I64_REM_S:
  4230. case WASM_OP_I64_REM_U:
  4231. case WASM_OP_I64_AND:
  4232. case WASM_OP_I64_OR:
  4233. case WASM_OP_I64_XOR:
  4234. case WASM_OP_I64_SHL:
  4235. case WASM_OP_I64_SHR_S:
  4236. case WASM_OP_I64_SHR_U:
  4237. case WASM_OP_I64_ROTL:
  4238. case WASM_OP_I64_ROTR:
  4239. case WASM_OP_F32_ABS:
  4240. case WASM_OP_F32_NEG:
  4241. case WASM_OP_F32_CEIL:
  4242. case WASM_OP_F32_FLOOR:
  4243. case WASM_OP_F32_TRUNC:
  4244. case WASM_OP_F32_NEAREST:
  4245. case WASM_OP_F32_SQRT:
  4246. case WASM_OP_F32_ADD:
  4247. case WASM_OP_F32_SUB:
  4248. case WASM_OP_F32_MUL:
  4249. case WASM_OP_F32_DIV:
  4250. case WASM_OP_F32_MIN:
  4251. case WASM_OP_F32_MAX:
  4252. case WASM_OP_F32_COPYSIGN:
  4253. case WASM_OP_F64_ABS:
  4254. case WASM_OP_F64_NEG:
  4255. case WASM_OP_F64_CEIL:
  4256. case WASM_OP_F64_FLOOR:
  4257. case WASM_OP_F64_TRUNC:
  4258. case WASM_OP_F64_NEAREST:
  4259. case WASM_OP_F64_SQRT:
  4260. case WASM_OP_F64_ADD:
  4261. case WASM_OP_F64_SUB:
  4262. case WASM_OP_F64_MUL:
  4263. case WASM_OP_F64_DIV:
  4264. case WASM_OP_F64_MIN:
  4265. case WASM_OP_F64_MAX:
  4266. case WASM_OP_F64_COPYSIGN:
  4267. case WASM_OP_I32_WRAP_I64:
  4268. case WASM_OP_I32_TRUNC_S_F32:
  4269. case WASM_OP_I32_TRUNC_U_F32:
  4270. case WASM_OP_I32_TRUNC_S_F64:
  4271. case WASM_OP_I32_TRUNC_U_F64:
  4272. case WASM_OP_I64_EXTEND_S_I32:
  4273. case WASM_OP_I64_EXTEND_U_I32:
  4274. case WASM_OP_I64_TRUNC_S_F32:
  4275. case WASM_OP_I64_TRUNC_U_F32:
  4276. case WASM_OP_I64_TRUNC_S_F64:
  4277. case WASM_OP_I64_TRUNC_U_F64:
  4278. case WASM_OP_F32_CONVERT_S_I32:
  4279. case WASM_OP_F32_CONVERT_U_I32:
  4280. case WASM_OP_F32_CONVERT_S_I64:
  4281. case WASM_OP_F32_CONVERT_U_I64:
  4282. case WASM_OP_F32_DEMOTE_F64:
  4283. case WASM_OP_F64_CONVERT_S_I32:
  4284. case WASM_OP_F64_CONVERT_U_I32:
  4285. case WASM_OP_F64_CONVERT_S_I64:
  4286. case WASM_OP_F64_CONVERT_U_I64:
  4287. case WASM_OP_F64_PROMOTE_F32:
  4288. case WASM_OP_I32_REINTERPRET_F32:
  4289. case WASM_OP_I64_REINTERPRET_F64:
  4290. case WASM_OP_F32_REINTERPRET_I32:
  4291. case WASM_OP_F64_REINTERPRET_I64:
  4292. case WASM_OP_I32_EXTEND8_S:
  4293. case WASM_OP_I32_EXTEND16_S:
  4294. case WASM_OP_I64_EXTEND8_S:
  4295. case WASM_OP_I64_EXTEND16_S:
  4296. case WASM_OP_I64_EXTEND32_S:
  4297. break;
  4298. case WASM_OP_MISC_PREFIX:
  4299. {
  4300. uint32 opcode1;
  4301. read_leb_uint32(p, p_end, opcode1);
  4302. switch (opcode1) {
  4303. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4304. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4305. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4306. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4307. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4308. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4309. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4310. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4311. break;
  4312. #if WASM_ENABLE_BULK_MEMORY != 0
  4313. case WASM_OP_MEMORY_INIT:
  4314. skip_leb_uint32(p, p_end);
  4315. /* skip memory idx */
  4316. p++;
  4317. break;
  4318. case WASM_OP_DATA_DROP:
  4319. skip_leb_uint32(p, p_end);
  4320. break;
  4321. case WASM_OP_MEMORY_COPY:
  4322. /* skip two memory idx */
  4323. p += 2;
  4324. break;
  4325. case WASM_OP_MEMORY_FILL:
  4326. /* skip memory idx */
  4327. p++;
  4328. break;
  4329. #endif /* WASM_ENABLE_BULK_MEMORY */
  4330. #if WASM_ENABLE_REF_TYPES != 0
  4331. case WASM_OP_TABLE_INIT:
  4332. case WASM_OP_TABLE_COPY:
  4333. /* tableidx */
  4334. skip_leb_uint32(p, p_end);
  4335. /* elemidx */
  4336. skip_leb_uint32(p, p_end);
  4337. break;
  4338. case WASM_OP_ELEM_DROP:
  4339. /* elemidx */
  4340. skip_leb_uint32(p, p_end);
  4341. break;
  4342. case WASM_OP_TABLE_SIZE:
  4343. case WASM_OP_TABLE_GROW:
  4344. case WASM_OP_TABLE_FILL:
  4345. skip_leb_uint32(p, p_end); /* table idx */
  4346. break;
  4347. #endif /* WASM_ENABLE_REF_TYPES */
  4348. default:
  4349. return false;
  4350. }
  4351. break;
  4352. }
  4353. #if WASM_ENABLE_SIMD != 0
  4354. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4355. case WASM_OP_SIMD_PREFIX:
  4356. {
  4357. /* TODO: shall we ceate a table to be friendly to branch
  4358. * prediction */
  4359. opcode = read_uint8(p);
  4360. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  4361. */
  4362. switch (opcode) {
  4363. case SIMD_v128_load:
  4364. case SIMD_v128_load8x8_s:
  4365. case SIMD_v128_load8x8_u:
  4366. case SIMD_v128_load16x4_s:
  4367. case SIMD_v128_load16x4_u:
  4368. case SIMD_v128_load32x2_s:
  4369. case SIMD_v128_load32x2_u:
  4370. case SIMD_v128_load8_splat:
  4371. case SIMD_v128_load16_splat:
  4372. case SIMD_v128_load32_splat:
  4373. case SIMD_v128_load64_splat:
  4374. case SIMD_v128_store:
  4375. /* memarg align */
  4376. skip_leb_uint32(p, p_end);
  4377. /* memarg offset*/
  4378. skip_leb_uint32(p, p_end);
  4379. break;
  4380. case SIMD_v128_const:
  4381. case SIMD_v8x16_shuffle:
  4382. /* immByte[16] immLaneId[16] */
  4383. CHECK_BUF1(p, p_end, 16);
  4384. p += 16;
  4385. break;
  4386. case SIMD_i8x16_extract_lane_s:
  4387. case SIMD_i8x16_extract_lane_u:
  4388. case SIMD_i8x16_replace_lane:
  4389. case SIMD_i16x8_extract_lane_s:
  4390. case SIMD_i16x8_extract_lane_u:
  4391. case SIMD_i16x8_replace_lane:
  4392. case SIMD_i32x4_extract_lane:
  4393. case SIMD_i32x4_replace_lane:
  4394. case SIMD_i64x2_extract_lane:
  4395. case SIMD_i64x2_replace_lane:
  4396. case SIMD_f32x4_extract_lane:
  4397. case SIMD_f32x4_replace_lane:
  4398. case SIMD_f64x2_extract_lane:
  4399. case SIMD_f64x2_replace_lane:
  4400. /* ImmLaneId */
  4401. CHECK_BUF(p, p_end, 1);
  4402. p++;
  4403. break;
  4404. case SIMD_v128_load8_lane:
  4405. case SIMD_v128_load16_lane:
  4406. case SIMD_v128_load32_lane:
  4407. case SIMD_v128_load64_lane:
  4408. case SIMD_v128_store8_lane:
  4409. case SIMD_v128_store16_lane:
  4410. case SIMD_v128_store32_lane:
  4411. case SIMD_v128_store64_lane:
  4412. /* memarg align */
  4413. skip_leb_uint32(p, p_end);
  4414. /* memarg offset*/
  4415. skip_leb_uint32(p, p_end);
  4416. /* ImmLaneId */
  4417. CHECK_BUF(p, p_end, 1);
  4418. p++;
  4419. break;
  4420. case SIMD_v128_load32_zero:
  4421. case SIMD_v128_load64_zero:
  4422. /* memarg align */
  4423. skip_leb_uint32(p, p_end);
  4424. /* memarg offset*/
  4425. skip_leb_uint32(p, p_end);
  4426. break;
  4427. default:
  4428. /*
  4429. * since latest SIMD specific used almost every value
  4430. * from 0x00 to 0xff, the default branch will present
  4431. * all opcodes without imm
  4432. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  4433. */
  4434. break;
  4435. }
  4436. break;
  4437. }
  4438. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  4439. #endif /* end of WASM_ENABLE_SIMD */
  4440. #if WASM_ENABLE_SHARED_MEMORY != 0
  4441. case WASM_OP_ATOMIC_PREFIX:
  4442. {
  4443. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  4444. opcode = read_uint8(p);
  4445. if (opcode != WASM_OP_ATOMIC_FENCE) {
  4446. skip_leb_uint32(p, p_end); /* align */
  4447. skip_leb_uint32(p, p_end); /* offset */
  4448. }
  4449. else {
  4450. /* atomic.fence doesn't have memarg */
  4451. p++;
  4452. }
  4453. break;
  4454. }
  4455. #endif
  4456. #if WASM_ENABLE_DEBUG_INTERP != 0
  4457. case DEBUG_OP_BREAK:
  4458. {
  4459. WASMDebugInstance *debug_instance =
  4460. wasm_exec_env_get_instance(exec_env);
  4461. char orignal_opcode[1];
  4462. uint64 size = 1;
  4463. WASMModuleInstance *module_inst =
  4464. (WASMModuleInstance *)exec_env->module_inst;
  4465. uint64 offset = (p - 1) >= module_inst->module->load_addr
  4466. ? (p - 1) - module_inst->module->load_addr
  4467. : ~0;
  4468. if (debug_instance) {
  4469. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  4470. orignal_opcode, &size)
  4471. && size == 1) {
  4472. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  4473. "with %02x: ",
  4474. orignal_opcode[0]);
  4475. opcode = orignal_opcode[0];
  4476. goto op_break_retry;
  4477. }
  4478. }
  4479. break;
  4480. }
  4481. #endif
  4482. default:
  4483. return false;
  4484. }
  4485. }
  4486. (void)u8;
  4487. (void)exec_env;
  4488. return false;
  4489. fail:
  4490. return false;
  4491. }
  4492. #define REF_ANY VALUE_TYPE_ANY
  4493. #define REF_I32 VALUE_TYPE_I32
  4494. #define REF_F32 VALUE_TYPE_F32
  4495. #define REF_I64_1 VALUE_TYPE_I64
  4496. #define REF_I64_2 VALUE_TYPE_I64
  4497. #define REF_F64_1 VALUE_TYPE_F64
  4498. #define REF_F64_2 VALUE_TYPE_F64
  4499. #define REF_V128_1 VALUE_TYPE_V128
  4500. #define REF_V128_2 VALUE_TYPE_V128
  4501. #define REF_V128_3 VALUE_TYPE_V128
  4502. #define REF_V128_4 VALUE_TYPE_V128
  4503. #define REF_FUNCREF VALUE_TYPE_FUNCREF
  4504. #define REF_EXTERNREF VALUE_TYPE_EXTERNREF
  4505. #if WASM_ENABLE_FAST_INTERP != 0
  4506. #if WASM_DEBUG_PREPROCESSOR != 0
  4507. #define LOG_OP(...) os_printf(__VA_ARGS__)
  4508. #else
  4509. #define LOG_OP(...) (void)0
  4510. #endif
  4511. #define PATCH_ELSE 0
  4512. #define PATCH_END 1
  4513. typedef struct BranchBlockPatch {
  4514. struct BranchBlockPatch *next;
  4515. uint8 patch_type;
  4516. uint8 *code_compiled;
  4517. } BranchBlockPatch;
  4518. #endif
  4519. typedef struct BranchBlock {
  4520. uint8 label_type;
  4521. BlockType block_type;
  4522. uint8 *start_addr;
  4523. uint8 *else_addr;
  4524. uint8 *end_addr;
  4525. uint32 stack_cell_num;
  4526. #if WASM_ENABLE_FAST_INTERP != 0
  4527. uint16 dynamic_offset;
  4528. uint8 *code_compiled;
  4529. BranchBlockPatch *patch_list;
  4530. /* This is used to save params frame_offset of of if block */
  4531. int16 *param_frame_offsets;
  4532. #endif
  4533. /* Indicate the operand stack is in polymorphic state.
  4534. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  4535. * to polymorphic state until the block's 'end' opcode is processed.
  4536. * If stack is in polymorphic state and stack is empty, instruction can
  4537. * pop any type of value directly without decreasing stack top pointer
  4538. * and stack cell num. */
  4539. bool is_stack_polymorphic;
  4540. } BranchBlock;
  4541. typedef struct WASMLoaderContext {
  4542. /* frame ref stack */
  4543. uint8 *frame_ref;
  4544. uint8 *frame_ref_bottom;
  4545. uint8 *frame_ref_boundary;
  4546. uint32 frame_ref_size;
  4547. uint32 stack_cell_num;
  4548. uint32 max_stack_cell_num;
  4549. /* frame csp stack */
  4550. BranchBlock *frame_csp;
  4551. BranchBlock *frame_csp_bottom;
  4552. BranchBlock *frame_csp_boundary;
  4553. uint32 frame_csp_size;
  4554. uint32 csp_num;
  4555. uint32 max_csp_num;
  4556. #if WASM_ENABLE_FAST_INTERP != 0
  4557. /* frame offset stack */
  4558. int16 *frame_offset;
  4559. int16 *frame_offset_bottom;
  4560. int16 *frame_offset_boundary;
  4561. uint32 frame_offset_size;
  4562. int16 dynamic_offset;
  4563. int16 start_dynamic_offset;
  4564. int16 max_dynamic_offset;
  4565. /* preserved local offset */
  4566. int16 preserved_local_offset;
  4567. /* const buffer */
  4568. uint8 *const_buf;
  4569. uint16 num_const;
  4570. uint16 const_cell_num;
  4571. uint32 const_buf_size;
  4572. /* processed code */
  4573. uint8 *p_code_compiled;
  4574. uint8 *p_code_compiled_end;
  4575. uint32 code_compiled_size;
  4576. /* If the last opcode will be dropped, the peak memory usage will be larger
  4577. * than the final code_compiled_size, we record the peak size to ensure
  4578. * there will not be invalid memory access during second traverse */
  4579. uint32 code_compiled_peak_size;
  4580. #endif
  4581. } WASMLoaderContext;
  4582. typedef struct Const {
  4583. WASMValue value;
  4584. uint16 slot_index;
  4585. uint8 value_type;
  4586. } Const;
  4587. static void *
  4588. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  4589. uint32 error_buf_size)
  4590. {
  4591. uint8 *mem_new;
  4592. bh_assert(size_new > size_old);
  4593. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  4594. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  4595. memset(mem_new + size_old, 0, size_new - size_old);
  4596. wasm_runtime_free(mem_old);
  4597. }
  4598. return mem_new;
  4599. }
  4600. #define MEM_REALLOC(mem, size_old, size_new) \
  4601. do { \
  4602. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  4603. error_buf_size); \
  4604. if (!mem_new) \
  4605. goto fail; \
  4606. mem = mem_new; \
  4607. } while (0)
  4608. #define CHECK_CSP_PUSH() \
  4609. do { \
  4610. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  4611. MEM_REALLOC( \
  4612. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  4613. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  4614. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  4615. ctx->frame_csp_boundary = \
  4616. ctx->frame_csp_bottom \
  4617. + ctx->frame_csp_size / sizeof(BranchBlock); \
  4618. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  4619. } \
  4620. } while (0)
  4621. #define CHECK_CSP_POP() \
  4622. do { \
  4623. if (ctx->csp_num < 1) { \
  4624. set_error_buf(error_buf, error_buf_size, \
  4625. "type mismatch: " \
  4626. "expect data but block stack was empty"); \
  4627. goto fail; \
  4628. } \
  4629. } while (0)
  4630. #if WASM_ENABLE_FAST_INTERP != 0
  4631. static bool
  4632. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  4633. uint32 error_buf_size)
  4634. {
  4635. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  4636. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  4637. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  4638. ctx->frame_offset_size + 16);
  4639. ctx->frame_offset_size += 16;
  4640. ctx->frame_offset_boundary =
  4641. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  4642. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  4643. }
  4644. return true;
  4645. fail:
  4646. return false;
  4647. }
  4648. static bool
  4649. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  4650. {
  4651. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  4652. return false;
  4653. return true;
  4654. }
  4655. static void
  4656. free_label_patch_list(BranchBlock *frame_csp)
  4657. {
  4658. BranchBlockPatch *label_patch = frame_csp->patch_list;
  4659. BranchBlockPatch *next;
  4660. while (label_patch != NULL) {
  4661. next = label_patch->next;
  4662. wasm_runtime_free(label_patch);
  4663. label_patch = next;
  4664. }
  4665. frame_csp->patch_list = NULL;
  4666. }
  4667. static void
  4668. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  4669. {
  4670. BranchBlock *tmp_csp = frame_csp;
  4671. for (uint32 i = 0; i < csp_num; i++) {
  4672. free_label_patch_list(tmp_csp);
  4673. tmp_csp++;
  4674. }
  4675. }
  4676. #endif /* end of WASM_ENABLE_FAST_INTERP */
  4677. static bool
  4678. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  4679. {
  4680. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  4681. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  4682. ctx->frame_ref_size + 16);
  4683. ctx->frame_ref_size += 16;
  4684. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  4685. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  4686. }
  4687. return true;
  4688. fail:
  4689. return false;
  4690. }
  4691. static bool
  4692. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  4693. char *error_buf, uint32 error_buf_size)
  4694. {
  4695. if ((is_32bit_type(type) && stack_cell_num < 1)
  4696. || (is_64bit_type(type) && stack_cell_num < 2)
  4697. #if WASM_ENABLE_SIMD != 0
  4698. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4699. || (type == VALUE_TYPE_V128 && stack_cell_num < 4)
  4700. #endif
  4701. #endif
  4702. ) {
  4703. set_error_buf(error_buf, error_buf_size,
  4704. "type mismatch: expect data but stack was empty");
  4705. return false;
  4706. }
  4707. if ((is_32bit_type(type) && *(frame_ref - 1) != type)
  4708. || (is_64bit_type(type)
  4709. && (*(frame_ref - 2) != type || *(frame_ref - 1) != type))
  4710. #if WASM_ENABLE_SIMD != 0
  4711. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4712. || (type == VALUE_TYPE_V128
  4713. && (*(frame_ref - 4) != REF_V128_1 || *(frame_ref - 3) != REF_V128_2
  4714. || *(frame_ref - 2) != REF_V128_3
  4715. || *(frame_ref - 1) != REF_V128_4))
  4716. #endif
  4717. #endif
  4718. ) {
  4719. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  4720. "type mismatch: expect ", type2str(type),
  4721. " but got other");
  4722. return false;
  4723. }
  4724. return true;
  4725. }
  4726. static bool
  4727. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4728. uint32 error_buf_size)
  4729. {
  4730. int32 block_stack_cell_num =
  4731. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  4732. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  4733. /* the stack top is a value of any type, return success */
  4734. return true;
  4735. }
  4736. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  4737. error_buf, error_buf_size))
  4738. return false;
  4739. return true;
  4740. }
  4741. static void
  4742. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  4743. {
  4744. if (ctx) {
  4745. if (ctx->frame_ref_bottom)
  4746. wasm_runtime_free(ctx->frame_ref_bottom);
  4747. if (ctx->frame_csp_bottom) {
  4748. #if WASM_ENABLE_FAST_INTERP != 0
  4749. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  4750. #endif
  4751. wasm_runtime_free(ctx->frame_csp_bottom);
  4752. }
  4753. #if WASM_ENABLE_FAST_INTERP != 0
  4754. if (ctx->frame_offset_bottom)
  4755. wasm_runtime_free(ctx->frame_offset_bottom);
  4756. if (ctx->const_buf)
  4757. wasm_runtime_free(ctx->const_buf);
  4758. #endif
  4759. wasm_runtime_free(ctx);
  4760. }
  4761. }
  4762. static WASMLoaderContext *
  4763. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  4764. {
  4765. WASMLoaderContext *loader_ctx =
  4766. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  4767. if (!loader_ctx)
  4768. return NULL;
  4769. loader_ctx->frame_ref_size = 32;
  4770. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  4771. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  4772. goto fail;
  4773. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  4774. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  4775. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  4776. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  4777. goto fail;
  4778. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  4779. #if WASM_ENABLE_FAST_INTERP != 0
  4780. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  4781. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  4782. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  4783. error_buf_size)))
  4784. goto fail;
  4785. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  4786. loader_ctx->num_const = 0;
  4787. loader_ctx->const_buf_size = sizeof(Const) * 8;
  4788. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  4789. error_buf, error_buf_size)))
  4790. goto fail;
  4791. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  4792. set_error_buf(error_buf, error_buf_size,
  4793. "fast interpreter offset overflow");
  4794. goto fail;
  4795. }
  4796. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  4797. loader_ctx->max_dynamic_offset =
  4798. func->param_cell_num + func->local_cell_num;
  4799. #endif
  4800. return loader_ctx;
  4801. fail:
  4802. wasm_loader_ctx_destroy(loader_ctx);
  4803. return NULL;
  4804. }
  4805. static bool
  4806. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4807. uint32 error_buf_size)
  4808. {
  4809. if (type == VALUE_TYPE_VOID)
  4810. return true;
  4811. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4812. return false;
  4813. *ctx->frame_ref++ = type;
  4814. ctx->stack_cell_num++;
  4815. if (is_32bit_type(type) || type == VALUE_TYPE_ANY)
  4816. goto check_stack_and_return;
  4817. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4818. return false;
  4819. *ctx->frame_ref++ = type;
  4820. ctx->stack_cell_num++;
  4821. #if WASM_ENABLE_SIMD != 0
  4822. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4823. if (type == VALUE_TYPE_V128) {
  4824. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4825. return false;
  4826. *ctx->frame_ref++ = type;
  4827. ctx->stack_cell_num++;
  4828. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4829. return false;
  4830. *ctx->frame_ref++ = type;
  4831. ctx->stack_cell_num++;
  4832. }
  4833. #endif
  4834. #endif
  4835. check_stack_and_return:
  4836. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  4837. ctx->max_stack_cell_num = ctx->stack_cell_num;
  4838. if (ctx->max_stack_cell_num > UINT16_MAX) {
  4839. set_error_buf(error_buf, error_buf_size,
  4840. "operand stack depth limit exceeded");
  4841. return false;
  4842. }
  4843. }
  4844. return true;
  4845. }
  4846. static bool
  4847. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4848. uint32 error_buf_size)
  4849. {
  4850. BranchBlock *cur_block = ctx->frame_csp - 1;
  4851. int32 available_stack_cell =
  4852. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4853. /* Directly return success if current block is in stack
  4854. * polymorphic state while stack is empty. */
  4855. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4856. return true;
  4857. if (type == VALUE_TYPE_VOID)
  4858. return true;
  4859. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  4860. return false;
  4861. ctx->frame_ref--;
  4862. ctx->stack_cell_num--;
  4863. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  4864. return true;
  4865. ctx->frame_ref--;
  4866. ctx->stack_cell_num--;
  4867. #if WASM_ENABLE_SIMD != 0
  4868. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4869. if (type == VALUE_TYPE_V128) {
  4870. ctx->frame_ref -= 2;
  4871. ctx->stack_cell_num -= 2;
  4872. }
  4873. #endif
  4874. #endif
  4875. return true;
  4876. }
  4877. static bool
  4878. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  4879. uint8 type_push, uint8 type_pop, char *error_buf,
  4880. uint32 error_buf_size)
  4881. {
  4882. for (int i = 0; i < pop_cnt; i++) {
  4883. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4884. error_buf_size))
  4885. return false;
  4886. }
  4887. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4888. return false;
  4889. return true;
  4890. }
  4891. static bool
  4892. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  4893. BlockType block_type, uint8 *start_addr,
  4894. char *error_buf, uint32 error_buf_size)
  4895. {
  4896. CHECK_CSP_PUSH();
  4897. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  4898. ctx->frame_csp->label_type = label_type;
  4899. ctx->frame_csp->block_type = block_type;
  4900. ctx->frame_csp->start_addr = start_addr;
  4901. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  4902. #if WASM_ENABLE_FAST_INTERP != 0
  4903. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  4904. ctx->frame_csp->patch_list = NULL;
  4905. #endif
  4906. ctx->frame_csp++;
  4907. ctx->csp_num++;
  4908. if (ctx->csp_num > ctx->max_csp_num) {
  4909. ctx->max_csp_num = ctx->csp_num;
  4910. if (ctx->max_csp_num > UINT16_MAX) {
  4911. set_error_buf(error_buf, error_buf_size,
  4912. "label stack depth limit exceeded");
  4913. return false;
  4914. }
  4915. }
  4916. return true;
  4917. fail:
  4918. return false;
  4919. }
  4920. static bool
  4921. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  4922. uint32 error_buf_size)
  4923. {
  4924. CHECK_CSP_POP();
  4925. #if WASM_ENABLE_FAST_INTERP != 0
  4926. if ((ctx->frame_csp - 1)->param_frame_offsets)
  4927. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  4928. #endif
  4929. ctx->frame_csp--;
  4930. ctx->csp_num--;
  4931. return true;
  4932. fail:
  4933. return false;
  4934. }
  4935. #if WASM_ENABLE_FAST_INTERP != 0
  4936. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4937. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4938. #define emit_label(opcode) \
  4939. do { \
  4940. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  4941. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4942. } while (0)
  4943. #define skip_label() \
  4944. do { \
  4945. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  4946. LOG_OP("\ndelete last op\n"); \
  4947. } while (0)
  4948. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4949. #define emit_label(opcode) \
  4950. do { \
  4951. int32 offset = \
  4952. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  4953. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  4954. set_error_buf(error_buf, error_buf_size, \
  4955. "pre-compiled label offset out of range"); \
  4956. goto fail; \
  4957. } \
  4958. wasm_loader_emit_int16(loader_ctx, offset); \
  4959. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4960. } while (0)
  4961. #define skip_label() \
  4962. do { \
  4963. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  4964. LOG_OP("\ndelete last op\n"); \
  4965. } while (0)
  4966. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4967. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  4968. #define emit_label(opcode) \
  4969. do { \
  4970. wasm_loader_emit_uint8(loader_ctx, opcode); \
  4971. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4972. } while (0)
  4973. #define skip_label() \
  4974. do { \
  4975. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  4976. LOG_OP("\ndelete last op\n"); \
  4977. } while (0)
  4978. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  4979. #define emit_empty_label_addr_and_frame_ip(type) \
  4980. do { \
  4981. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  4982. loader_ctx->p_code_compiled, error_buf, \
  4983. error_buf_size)) \
  4984. goto fail; \
  4985. /* label address, to be patched */ \
  4986. wasm_loader_emit_ptr(loader_ctx, NULL); \
  4987. } while (0)
  4988. #define emit_br_info(frame_csp) \
  4989. do { \
  4990. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  4991. error_buf_size)) \
  4992. goto fail; \
  4993. } while (0)
  4994. #define LAST_OP_OUTPUT_I32() \
  4995. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  4996. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  4997. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  4998. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  4999. || (last_op >= WASM_OP_I32_WRAP_I64 \
  5000. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  5001. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  5002. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  5003. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  5004. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  5005. || (last_op == EXT_OP_COPY_STACK_TOP)
  5006. #define LAST_OP_OUTPUT_I64() \
  5007. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  5008. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  5009. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  5010. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  5011. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  5012. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  5013. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  5014. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  5015. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  5016. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  5017. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  5018. #define GET_CONST_OFFSET(type, val) \
  5019. do { \
  5020. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  5021. &operand_offset, error_buf, \
  5022. error_buf_size))) \
  5023. goto fail; \
  5024. } while (0)
  5025. #define GET_CONST_F32_OFFSET(type, fval) \
  5026. do { \
  5027. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  5028. &operand_offset, error_buf, \
  5029. error_buf_size))) \
  5030. goto fail; \
  5031. } while (0)
  5032. #define GET_CONST_F64_OFFSET(type, fval) \
  5033. do { \
  5034. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  5035. &operand_offset, error_buf, \
  5036. error_buf_size))) \
  5037. goto fail; \
  5038. } while (0)
  5039. #define emit_operand(ctx, offset) \
  5040. do { \
  5041. wasm_loader_emit_int16(ctx, offset); \
  5042. LOG_OP("%d\t", offset); \
  5043. } while (0)
  5044. #define emit_byte(ctx, byte) \
  5045. do { \
  5046. wasm_loader_emit_uint8(ctx, byte); \
  5047. LOG_OP("%d\t", byte); \
  5048. } while (0)
  5049. #define emit_uint32(ctx, value) \
  5050. do { \
  5051. wasm_loader_emit_uint32(ctx, value); \
  5052. LOG_OP("%d\t", value); \
  5053. } while (0)
  5054. #define emit_uint64(ctx, value) \
  5055. do { \
  5056. wasm_loader_emit_const(ctx, &value, false); \
  5057. LOG_OP("%lld\t", value); \
  5058. } while (0)
  5059. #define emit_float32(ctx, value) \
  5060. do { \
  5061. wasm_loader_emit_const(ctx, &value, true); \
  5062. LOG_OP("%f\t", value); \
  5063. } while (0)
  5064. #define emit_float64(ctx, value) \
  5065. do { \
  5066. wasm_loader_emit_const(ctx, &value, false); \
  5067. LOG_OP("%f\t", value); \
  5068. } while (0)
  5069. static bool
  5070. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  5071. {
  5072. if (!(ctx->p_code_compiled =
  5073. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  5074. return false;
  5075. ctx->p_code_compiled_end =
  5076. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  5077. /* clean up frame ref */
  5078. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  5079. ctx->frame_ref = ctx->frame_ref_bottom;
  5080. ctx->stack_cell_num = 0;
  5081. /* clean up frame csp */
  5082. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  5083. ctx->frame_csp = ctx->frame_csp_bottom;
  5084. ctx->csp_num = 0;
  5085. ctx->max_csp_num = 0;
  5086. /* clean up frame offset */
  5087. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  5088. ctx->frame_offset = ctx->frame_offset_bottom;
  5089. ctx->dynamic_offset = ctx->start_dynamic_offset;
  5090. /* init preserved local offsets */
  5091. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  5092. /* const buf is reserved */
  5093. return true;
  5094. }
  5095. static void
  5096. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  5097. {
  5098. ctx->code_compiled_size += size;
  5099. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  5100. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  5101. }
  5102. }
  5103. static void
  5104. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  5105. {
  5106. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  5107. if (ctx->p_code_compiled) {
  5108. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5109. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5110. #endif
  5111. bh_memcpy_s(ctx->p_code_compiled,
  5112. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  5113. value, size);
  5114. ctx->p_code_compiled += size;
  5115. }
  5116. else {
  5117. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5118. bh_assert((ctx->code_compiled_size & 1) == 0);
  5119. #endif
  5120. increase_compiled_code_space(ctx, size);
  5121. }
  5122. }
  5123. static void
  5124. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  5125. {
  5126. if (ctx->p_code_compiled) {
  5127. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5128. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5129. #endif
  5130. STORE_U32(ctx->p_code_compiled, value);
  5131. ctx->p_code_compiled += sizeof(uint32);
  5132. }
  5133. else {
  5134. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5135. bh_assert((ctx->code_compiled_size & 1) == 0);
  5136. #endif
  5137. increase_compiled_code_space(ctx, sizeof(uint32));
  5138. }
  5139. }
  5140. static void
  5141. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  5142. {
  5143. if (ctx->p_code_compiled) {
  5144. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5145. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5146. #endif
  5147. STORE_U16(ctx->p_code_compiled, (uint16)value);
  5148. ctx->p_code_compiled += sizeof(int16);
  5149. }
  5150. else {
  5151. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5152. bh_assert((ctx->code_compiled_size & 1) == 0);
  5153. #endif
  5154. increase_compiled_code_space(ctx, sizeof(uint16));
  5155. }
  5156. }
  5157. static void
  5158. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  5159. {
  5160. if (ctx->p_code_compiled) {
  5161. *(ctx->p_code_compiled) = value;
  5162. ctx->p_code_compiled += sizeof(uint8);
  5163. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5164. ctx->p_code_compiled++;
  5165. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5166. #endif
  5167. }
  5168. else {
  5169. increase_compiled_code_space(ctx, sizeof(uint8));
  5170. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5171. increase_compiled_code_space(ctx, sizeof(uint8));
  5172. bh_assert((ctx->code_compiled_size & 1) == 0);
  5173. #endif
  5174. }
  5175. }
  5176. static void
  5177. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  5178. {
  5179. if (ctx->p_code_compiled) {
  5180. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5181. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5182. #endif
  5183. STORE_PTR(ctx->p_code_compiled, value);
  5184. ctx->p_code_compiled += sizeof(void *);
  5185. }
  5186. else {
  5187. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5188. bh_assert((ctx->code_compiled_size & 1) == 0);
  5189. #endif
  5190. increase_compiled_code_space(ctx, sizeof(void *));
  5191. }
  5192. }
  5193. static void
  5194. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  5195. {
  5196. if (ctx->p_code_compiled) {
  5197. ctx->p_code_compiled -= size;
  5198. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5199. if (size == sizeof(uint8)) {
  5200. ctx->p_code_compiled--;
  5201. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5202. }
  5203. #endif
  5204. }
  5205. else {
  5206. ctx->code_compiled_size -= size;
  5207. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5208. if (size == sizeof(uint8)) {
  5209. ctx->code_compiled_size--;
  5210. bh_assert((ctx->code_compiled_size & 1) == 0);
  5211. }
  5212. #endif
  5213. }
  5214. }
  5215. static bool
  5216. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  5217. uint32 local_index, uint32 local_type,
  5218. bool *preserved, char *error_buf,
  5219. uint32 error_buf_size)
  5220. {
  5221. uint32 i = 0;
  5222. int16 preserved_offset = (int16)local_index;
  5223. *preserved = false;
  5224. while (i < loader_ctx->stack_cell_num) {
  5225. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  5226. /* move previous local into dynamic space before a set/tee_local opcode
  5227. */
  5228. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  5229. if (!(*preserved)) {
  5230. *preserved = true;
  5231. skip_label();
  5232. preserved_offset = loader_ctx->preserved_local_offset;
  5233. if (loader_ctx->p_code_compiled) {
  5234. bh_assert(preserved_offset != (int16)local_index);
  5235. }
  5236. if (is_32bit_type(local_type)) {
  5237. /* Only increase preserve offset in the second traversal */
  5238. if (loader_ctx->p_code_compiled)
  5239. loader_ctx->preserved_local_offset++;
  5240. emit_label(EXT_OP_COPY_STACK_TOP);
  5241. }
  5242. else {
  5243. if (loader_ctx->p_code_compiled)
  5244. loader_ctx->preserved_local_offset += 2;
  5245. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  5246. }
  5247. emit_operand(loader_ctx, local_index);
  5248. emit_operand(loader_ctx, preserved_offset);
  5249. emit_label(opcode);
  5250. }
  5251. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  5252. }
  5253. if (is_32bit_type(cur_type))
  5254. i++;
  5255. else
  5256. i += 2;
  5257. }
  5258. (void)error_buf;
  5259. (void)error_buf_size;
  5260. return true;
  5261. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5262. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5263. fail:
  5264. return false;
  5265. #endif
  5266. #endif
  5267. }
  5268. static bool
  5269. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  5270. char *error_buf, uint32 error_buf_size)
  5271. {
  5272. uint32 i = 0;
  5273. bool preserve_local;
  5274. /* preserve locals before blocks to ensure that "tee/set_local" inside
  5275. blocks will not influence the value of these locals */
  5276. while (i < loader_ctx->stack_cell_num) {
  5277. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  5278. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  5279. if ((cur_offset < loader_ctx->start_dynamic_offset)
  5280. && (cur_offset >= 0)) {
  5281. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  5282. cur_type, &preserve_local,
  5283. error_buf, error_buf_size)))
  5284. return false;
  5285. }
  5286. if (is_32bit_type(cur_type)) {
  5287. i++;
  5288. }
  5289. else {
  5290. i += 2;
  5291. }
  5292. }
  5293. return true;
  5294. }
  5295. static bool
  5296. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  5297. uint8 *p_code_compiled, char *error_buf,
  5298. uint32 error_buf_size)
  5299. {
  5300. BranchBlockPatch *patch =
  5301. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  5302. if (!patch) {
  5303. return false;
  5304. }
  5305. patch->patch_type = patch_type;
  5306. patch->code_compiled = p_code_compiled;
  5307. if (!frame_csp->patch_list) {
  5308. frame_csp->patch_list = patch;
  5309. patch->next = NULL;
  5310. }
  5311. else {
  5312. patch->next = frame_csp->patch_list;
  5313. frame_csp->patch_list = patch;
  5314. }
  5315. return true;
  5316. }
  5317. static void
  5318. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  5319. {
  5320. BranchBlock *frame_csp = ctx->frame_csp - depth;
  5321. BranchBlockPatch *node = frame_csp->patch_list;
  5322. BranchBlockPatch *node_prev = NULL, *node_next;
  5323. if (!ctx->p_code_compiled)
  5324. return;
  5325. while (node) {
  5326. node_next = node->next;
  5327. if (node->patch_type == patch_type) {
  5328. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  5329. if (node_prev == NULL) {
  5330. frame_csp->patch_list = node_next;
  5331. }
  5332. else {
  5333. node_prev->next = node_next;
  5334. }
  5335. wasm_runtime_free(node);
  5336. }
  5337. else {
  5338. node_prev = node;
  5339. }
  5340. node = node_next;
  5341. }
  5342. }
  5343. static bool
  5344. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  5345. char *error_buf, uint32 error_buf_size)
  5346. {
  5347. /* br info layout:
  5348. * a) arity of target block
  5349. * b) total cell num of arity values
  5350. * c) each arity value's cell num
  5351. * d) each arity value's src frame offset
  5352. * e) each arity values's dst dynamic offset
  5353. * f) branch target address
  5354. *
  5355. * Note: b-e are omitted when arity is 0 so that
  5356. * interpreter can recover the br info quickly.
  5357. */
  5358. BlockType *block_type = &frame_csp->block_type;
  5359. uint8 *types = NULL, cell;
  5360. uint32 arity = 0;
  5361. int32 i;
  5362. int16 *frame_offset = ctx->frame_offset;
  5363. uint16 dynamic_offset;
  5364. /* Note: loop's arity is different from if and block. loop's arity is
  5365. * its parameter count while if and block arity is result count.
  5366. */
  5367. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  5368. arity = block_type_get_param_types(block_type, &types);
  5369. else
  5370. arity = block_type_get_result_types(block_type, &types);
  5371. /* Part a */
  5372. emit_uint32(ctx, arity);
  5373. if (arity) {
  5374. /* Part b */
  5375. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  5376. /* Part c */
  5377. for (i = (int32)arity - 1; i >= 0; i--) {
  5378. cell = (uint8)wasm_value_type_cell_num(types[i]);
  5379. emit_byte(ctx, cell);
  5380. }
  5381. /* Part d */
  5382. for (i = (int32)arity - 1; i >= 0; i--) {
  5383. cell = (uint8)wasm_value_type_cell_num(types[i]);
  5384. frame_offset -= cell;
  5385. emit_operand(ctx, *(int16 *)(frame_offset));
  5386. }
  5387. /* Part e */
  5388. dynamic_offset =
  5389. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  5390. for (i = (int32)arity - 1; i >= 0; i--) {
  5391. cell = (uint8)wasm_value_type_cell_num(types[i]);
  5392. dynamic_offset -= cell;
  5393. emit_operand(ctx, dynamic_offset);
  5394. }
  5395. }
  5396. /* Part f */
  5397. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  5398. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  5399. }
  5400. else {
  5401. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  5402. error_buf, error_buf_size))
  5403. return false;
  5404. /* label address, to be patched */
  5405. wasm_loader_emit_ptr(ctx, NULL);
  5406. }
  5407. return true;
  5408. }
  5409. static bool
  5410. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  5411. bool disable_emit, int16 operand_offset,
  5412. char *error_buf, uint32 error_buf_size)
  5413. {
  5414. if (type == VALUE_TYPE_VOID)
  5415. return true;
  5416. /* only check memory overflow in first traverse */
  5417. if (ctx->p_code_compiled == NULL) {
  5418. if (!check_offset_push(ctx, error_buf, error_buf_size))
  5419. return false;
  5420. }
  5421. if (disable_emit)
  5422. *(ctx->frame_offset)++ = operand_offset;
  5423. else {
  5424. emit_operand(ctx, ctx->dynamic_offset);
  5425. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  5426. ctx->dynamic_offset++;
  5427. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  5428. ctx->max_dynamic_offset = ctx->dynamic_offset;
  5429. if (ctx->max_dynamic_offset >= INT16_MAX) {
  5430. goto fail;
  5431. }
  5432. }
  5433. }
  5434. if (is_32bit_type(type))
  5435. return true;
  5436. if (ctx->p_code_compiled == NULL) {
  5437. if (!check_offset_push(ctx, error_buf, error_buf_size))
  5438. return false;
  5439. }
  5440. ctx->frame_offset++;
  5441. if (!disable_emit) {
  5442. ctx->dynamic_offset++;
  5443. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  5444. ctx->max_dynamic_offset = ctx->dynamic_offset;
  5445. if (ctx->max_dynamic_offset >= INT16_MAX) {
  5446. goto fail;
  5447. }
  5448. }
  5449. }
  5450. return true;
  5451. fail:
  5452. set_error_buf(error_buf, error_buf_size,
  5453. "fast interpreter offset overflow");
  5454. return false;
  5455. }
  5456. /* This function should be in front of wasm_loader_pop_frame_ref
  5457. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  5458. will be modified by wasm_loader_pop_frame_ref */
  5459. static bool
  5460. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  5461. char *error_buf, uint32 error_buf_size)
  5462. {
  5463. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  5464. then current block is the function block */
  5465. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  5466. BranchBlock *cur_block = ctx->frame_csp - depth;
  5467. int32 available_stack_cell =
  5468. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  5469. /* Directly return success if current block is in stack
  5470. * polymorphic state while stack is empty. */
  5471. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  5472. return true;
  5473. if (type == VALUE_TYPE_VOID)
  5474. return true;
  5475. if (is_32bit_type(type)) {
  5476. /* Check the offset stack bottom to ensure the frame offset
  5477. stack will not go underflow. But we don't thrown error
  5478. and return true here, because the error msg should be
  5479. given in wasm_loader_pop_frame_ref */
  5480. if (!check_offset_pop(ctx, 1))
  5481. return true;
  5482. ctx->frame_offset -= 1;
  5483. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  5484. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  5485. ctx->dynamic_offset -= 1;
  5486. }
  5487. else {
  5488. if (!check_offset_pop(ctx, 2))
  5489. return true;
  5490. ctx->frame_offset -= 2;
  5491. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  5492. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  5493. ctx->dynamic_offset -= 2;
  5494. }
  5495. emit_operand(ctx, *(ctx->frame_offset));
  5496. (void)error_buf;
  5497. (void)error_buf_size;
  5498. return true;
  5499. }
  5500. static bool
  5501. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  5502. uint8 type_push, uint8 type_pop,
  5503. bool disable_emit, int16 operand_offset,
  5504. char *error_buf, uint32 error_buf_size)
  5505. {
  5506. uint8 i;
  5507. for (i = 0; i < pop_cnt; i++) {
  5508. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  5509. error_buf_size))
  5510. return false;
  5511. }
  5512. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  5513. operand_offset, error_buf,
  5514. error_buf_size))
  5515. return false;
  5516. return true;
  5517. }
  5518. static bool
  5519. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  5520. bool disable_emit, int16 operand_offset,
  5521. char *error_buf, uint32 error_buf_size)
  5522. {
  5523. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  5524. error_buf, error_buf_size)))
  5525. return false;
  5526. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  5527. return false;
  5528. return true;
  5529. }
  5530. static bool
  5531. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  5532. char *error_buf, uint32 error_buf_size)
  5533. {
  5534. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  5535. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  5536. return false;
  5537. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  5538. return false;
  5539. return true;
  5540. }
  5541. static bool
  5542. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  5543. uint8 type_push, uint8 type_pop,
  5544. bool disable_emit, int16 operand_offset,
  5545. char *error_buf, uint32 error_buf_size)
  5546. {
  5547. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  5548. disable_emit, operand_offset,
  5549. error_buf, error_buf_size))
  5550. return false;
  5551. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  5552. error_buf, error_buf_size))
  5553. return false;
  5554. return true;
  5555. }
  5556. static bool
  5557. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  5558. int16 *offset, char *error_buf,
  5559. uint32 error_buf_size)
  5560. {
  5561. int8 bytes_to_increase;
  5562. int16 operand_offset = 0;
  5563. Const *c;
  5564. /* Search existing constant */
  5565. for (c = (Const *)ctx->const_buf;
  5566. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  5567. /* TODO: handle v128 type? */
  5568. if ((type == c->value_type)
  5569. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  5570. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  5571. #if WASM_ENABLE_REF_TYPES != 0
  5572. || (type == VALUE_TYPE_FUNCREF
  5573. && *(int32 *)value == c->value.i32)
  5574. || (type == VALUE_TYPE_EXTERNREF
  5575. && *(int32 *)value == c->value.i32)
  5576. #endif
  5577. || (type == VALUE_TYPE_F64
  5578. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  5579. || (type == VALUE_TYPE_F32
  5580. && (0
  5581. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  5582. operand_offset = c->slot_index;
  5583. break;
  5584. }
  5585. if (is_32bit_type(c->value_type))
  5586. operand_offset += 1;
  5587. else
  5588. operand_offset += 2;
  5589. }
  5590. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  5591. /* New constant, append to the const buffer */
  5592. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  5593. bytes_to_increase = 2;
  5594. }
  5595. else {
  5596. bytes_to_increase = 1;
  5597. }
  5598. /* The max cell num of const buffer is 32768 since the valid index range
  5599. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  5600. * full */
  5601. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  5602. *offset = 0;
  5603. return true;
  5604. }
  5605. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  5606. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  5607. ctx->const_buf_size + 4 * sizeof(Const));
  5608. ctx->const_buf_size += 4 * sizeof(Const);
  5609. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  5610. }
  5611. c->value_type = type;
  5612. switch (type) {
  5613. case VALUE_TYPE_F64:
  5614. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  5615. sizeof(float64));
  5616. ctx->const_cell_num += 2;
  5617. /* The const buf will be reversed, we use the second cell */
  5618. /* of the i64/f64 const so the finnal offset is corrent */
  5619. operand_offset++;
  5620. break;
  5621. case VALUE_TYPE_I64:
  5622. c->value.i64 = *(int64 *)value;
  5623. ctx->const_cell_num += 2;
  5624. operand_offset++;
  5625. break;
  5626. case VALUE_TYPE_F32:
  5627. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  5628. sizeof(float32));
  5629. ctx->const_cell_num++;
  5630. break;
  5631. case VALUE_TYPE_I32:
  5632. c->value.i32 = *(int32 *)value;
  5633. ctx->const_cell_num++;
  5634. break;
  5635. #if WASM_ENABLE_REF_TYPES != 0
  5636. case VALUE_TYPE_EXTERNREF:
  5637. case VALUE_TYPE_FUNCREF:
  5638. c->value.i32 = *(int32 *)value;
  5639. ctx->const_cell_num++;
  5640. break;
  5641. #endif
  5642. default:
  5643. break;
  5644. }
  5645. c->slot_index = operand_offset;
  5646. ctx->num_const++;
  5647. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  5648. (int64)c->value.i64);
  5649. }
  5650. /* use negetive index for const */
  5651. operand_offset = -(operand_offset + 1);
  5652. *offset = operand_offset;
  5653. return true;
  5654. fail:
  5655. return false;
  5656. }
  5657. /*
  5658. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  5659. -- Mostly used for the binary / compare operation
  5660. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  5661. -- Mostly used in block / control instructions
  5662. The POP will always emit the offset on the top of the frame_offset stack
  5663. PUSH can be used in two ways:
  5664. 1. directly PUSH:
  5665. PUSH_XXX();
  5666. will allocate a dynamic space and emit
  5667. 2. silent PUSH:
  5668. operand_offset = xxx; disable_emit = true;
  5669. PUSH_XXX();
  5670. only push the frame_offset stack, no emit
  5671. */
  5672. #define TEMPLATE_PUSH(Type) \
  5673. do { \
  5674. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  5675. disable_emit, operand_offset, \
  5676. error_buf, error_buf_size)) \
  5677. goto fail; \
  5678. } while (0)
  5679. #define TEMPLATE_POP(Type) \
  5680. do { \
  5681. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  5682. error_buf, error_buf_size)) \
  5683. goto fail; \
  5684. } while (0)
  5685. #define PUSH_OFFSET_TYPE(type) \
  5686. do { \
  5687. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  5688. operand_offset, error_buf, \
  5689. error_buf_size))) \
  5690. goto fail; \
  5691. } while (0)
  5692. #define POP_OFFSET_TYPE(type) \
  5693. do { \
  5694. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  5695. error_buf_size))) \
  5696. goto fail; \
  5697. } while (0)
  5698. #define POP_AND_PUSH(type_pop, type_push) \
  5699. do { \
  5700. if (!(wasm_loader_push_pop_frame_ref_offset( \
  5701. loader_ctx, 1, type_push, type_pop, disable_emit, \
  5702. operand_offset, error_buf, error_buf_size))) \
  5703. goto fail; \
  5704. } while (0)
  5705. /* type of POPs should be the same */
  5706. #define POP2_AND_PUSH(type_pop, type_push) \
  5707. do { \
  5708. if (!(wasm_loader_push_pop_frame_ref_offset( \
  5709. loader_ctx, 2, type_push, type_pop, disable_emit, \
  5710. operand_offset, error_buf, error_buf_size))) \
  5711. goto fail; \
  5712. } while (0)
  5713. #else /* WASM_ENABLE_FAST_INTERP */
  5714. #define TEMPLATE_PUSH(Type) \
  5715. do { \
  5716. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  5717. error_buf, error_buf_size))) \
  5718. goto fail; \
  5719. } while (0)
  5720. #define TEMPLATE_POP(Type) \
  5721. do { \
  5722. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  5723. error_buf, error_buf_size))) \
  5724. goto fail; \
  5725. } while (0)
  5726. #define POP_AND_PUSH(type_pop, type_push) \
  5727. do { \
  5728. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  5729. type_pop, error_buf, \
  5730. error_buf_size))) \
  5731. goto fail; \
  5732. } while (0)
  5733. /* type of POPs should be the same */
  5734. #define POP2_AND_PUSH(type_pop, type_push) \
  5735. do { \
  5736. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  5737. type_pop, error_buf, \
  5738. error_buf_size))) \
  5739. goto fail; \
  5740. } while (0)
  5741. #endif /* WASM_ENABLE_FAST_INTERP */
  5742. #define PUSH_I32() TEMPLATE_PUSH(I32)
  5743. #define PUSH_F32() TEMPLATE_PUSH(F32)
  5744. #define PUSH_I64() TEMPLATE_PUSH(I64)
  5745. #define PUSH_F64() TEMPLATE_PUSH(F64)
  5746. #define PUSH_V128() TEMPLATE_PUSH(V128)
  5747. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  5748. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  5749. #define POP_I32() TEMPLATE_POP(I32)
  5750. #define POP_F32() TEMPLATE_POP(F32)
  5751. #define POP_I64() TEMPLATE_POP(I64)
  5752. #define POP_F64() TEMPLATE_POP(F64)
  5753. #define POP_V128() TEMPLATE_POP(V128)
  5754. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  5755. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  5756. #if WASM_ENABLE_FAST_INTERP != 0
  5757. static bool
  5758. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  5759. bool disable_emit, char *error_buf, uint32 error_buf_size)
  5760. {
  5761. int16 operand_offset = 0;
  5762. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  5763. : loader_ctx->frame_csp;
  5764. BlockType *block_type = &block->block_type;
  5765. uint8 *return_types = NULL;
  5766. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  5767. int32 i = 0;
  5768. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  5769. *frame_offset_org = NULL;
  5770. return_count = block_type_get_result_types(block_type, &return_types);
  5771. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  5772. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  5773. if (return_count == 1) {
  5774. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  5775. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  5776. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  5777. /* insert op_copy before else opcode */
  5778. if (opcode == WASM_OP_ELSE)
  5779. skip_label();
  5780. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  5781. : EXT_OP_COPY_STACK_TOP_I64);
  5782. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  5783. emit_operand(loader_ctx, block->dynamic_offset);
  5784. if (opcode == WASM_OP_ELSE) {
  5785. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  5786. }
  5787. else {
  5788. loader_ctx->frame_offset -= cell;
  5789. loader_ctx->dynamic_offset = block->dynamic_offset;
  5790. PUSH_OFFSET_TYPE(return_types[0]);
  5791. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5792. }
  5793. if (opcode == WASM_OP_ELSE)
  5794. emit_label(opcode);
  5795. }
  5796. return true;
  5797. }
  5798. /* Copy stack top values to block's results which are in dynamic space.
  5799. * The instruction format:
  5800. * Part a: values count
  5801. * Part b: all values total cell num
  5802. * Part c: each value's cell_num, src offset and dst offset
  5803. * Part d: each value's src offset and dst offset
  5804. * Part e: each value's dst offset
  5805. */
  5806. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  5807. dynamic_offset = dynamic_offset_org =
  5808. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  5809. /* First traversal to get the count of values needed to be copied. */
  5810. for (i = (int32)return_count - 1; i >= 0; i--) {
  5811. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  5812. frame_offset -= cells;
  5813. dynamic_offset -= cells;
  5814. if (dynamic_offset != *frame_offset) {
  5815. value_count++;
  5816. total_cel_num += cells;
  5817. }
  5818. }
  5819. if (value_count) {
  5820. uint32 j = 0;
  5821. uint8 *emit_data = NULL, *cells = NULL;
  5822. int16 *src_offsets = NULL;
  5823. uint16 *dst_offsets = NULL;
  5824. uint64 size =
  5825. (uint64)value_count
  5826. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  5827. /* Allocate memory for the emit data */
  5828. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5829. return false;
  5830. cells = emit_data;
  5831. src_offsets = (int16 *)(cells + value_count);
  5832. dst_offsets = (uint16 *)(src_offsets + value_count);
  5833. /* insert op_copy before else opcode */
  5834. if (opcode == WASM_OP_ELSE)
  5835. skip_label();
  5836. emit_label(EXT_OP_COPY_STACK_VALUES);
  5837. /* Part a) */
  5838. emit_uint32(loader_ctx, value_count);
  5839. /* Part b) */
  5840. emit_uint32(loader_ctx, total_cel_num);
  5841. /* Second traversal to get each value's cell num, src offset and dst
  5842. * offset. */
  5843. frame_offset = frame_offset_org;
  5844. dynamic_offset = dynamic_offset_org;
  5845. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  5846. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  5847. frame_offset -= cell;
  5848. dynamic_offset -= cell;
  5849. if (dynamic_offset != *frame_offset) {
  5850. /* cell num */
  5851. cells[j] = cell;
  5852. /* src offset */
  5853. src_offsets[j] = *frame_offset;
  5854. /* dst offset */
  5855. dst_offsets[j] = dynamic_offset;
  5856. j++;
  5857. }
  5858. if (opcode == WASM_OP_ELSE) {
  5859. *frame_offset = dynamic_offset;
  5860. }
  5861. else {
  5862. loader_ctx->frame_offset = frame_offset;
  5863. loader_ctx->dynamic_offset = dynamic_offset;
  5864. PUSH_OFFSET_TYPE(return_types[i]);
  5865. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5866. loader_ctx->frame_offset = frame_offset_org;
  5867. loader_ctx->dynamic_offset = dynamic_offset_org;
  5868. }
  5869. }
  5870. bh_assert(j == value_count);
  5871. /* Emit the cells, src_offsets and dst_offsets */
  5872. for (j = 0; j < value_count; j++)
  5873. emit_byte(loader_ctx, cells[j]);
  5874. for (j = 0; j < value_count; j++)
  5875. emit_operand(loader_ctx, src_offsets[j]);
  5876. for (j = 0; j < value_count; j++)
  5877. emit_operand(loader_ctx, dst_offsets[j]);
  5878. if (opcode == WASM_OP_ELSE)
  5879. emit_label(opcode);
  5880. wasm_runtime_free(emit_data);
  5881. }
  5882. return true;
  5883. fail:
  5884. return false;
  5885. }
  5886. #endif /* WASM_ENABLE_FAST_INTERP */
  5887. #define RESERVE_BLOCK_RET() \
  5888. do { \
  5889. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  5890. error_buf_size)) \
  5891. goto fail; \
  5892. } while (0)
  5893. #define PUSH_TYPE(type) \
  5894. do { \
  5895. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  5896. error_buf_size))) \
  5897. goto fail; \
  5898. } while (0)
  5899. #define POP_TYPE(type) \
  5900. do { \
  5901. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  5902. error_buf_size))) \
  5903. goto fail; \
  5904. } while (0)
  5905. #define PUSH_CSP(label_type, block_type, _start_addr) \
  5906. do { \
  5907. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  5908. _start_addr, error_buf, \
  5909. error_buf_size)) \
  5910. goto fail; \
  5911. } while (0)
  5912. #define POP_CSP() \
  5913. do { \
  5914. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  5915. goto fail; \
  5916. } while (0)
  5917. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  5918. do { \
  5919. read_leb_uint32(p, p_end, local_idx); \
  5920. if (local_idx >= param_count + local_count) { \
  5921. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  5922. goto fail; \
  5923. } \
  5924. local_type = local_idx < param_count \
  5925. ? param_types[local_idx] \
  5926. : local_types[local_idx - param_count]; \
  5927. local_offset = local_offsets[local_idx]; \
  5928. } while (0)
  5929. #define CHECK_BR(depth) \
  5930. do { \
  5931. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  5932. error_buf_size)) \
  5933. goto fail; \
  5934. } while (0)
  5935. static bool
  5936. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  5937. {
  5938. if (module->memory_count == 0 && module->import_memory_count == 0) {
  5939. set_error_buf(error_buf, error_buf_size, "unknown memory");
  5940. return false;
  5941. }
  5942. return true;
  5943. }
  5944. #define CHECK_MEMORY() \
  5945. do { \
  5946. if (!check_memory(module, error_buf, error_buf_size)) \
  5947. goto fail; \
  5948. } while (0)
  5949. static bool
  5950. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5951. uint32 error_buf_size)
  5952. {
  5953. uint8 mem_access_aligns[] = {
  5954. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  5955. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  5956. };
  5957. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  5958. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  5959. set_error_buf(error_buf, error_buf_size,
  5960. "alignment must not be larger than natural");
  5961. return false;
  5962. }
  5963. return true;
  5964. }
  5965. #if WASM_ENABLE_SIMD != 0
  5966. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5967. static bool
  5968. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5969. uint32 error_buf_size)
  5970. {
  5971. uint8 mem_access_aligns[] = {
  5972. 4, /* load */
  5973. 3, 3, 3, 3, 3, 3, /* load and extend */
  5974. 0, 1, 2, 3, /* load and splat */
  5975. 4, /* store */
  5976. };
  5977. uint8 mem_access_aligns_load_lane[] = {
  5978. 0, 1, 2, 3, /* load lane */
  5979. 0, 1, 2, 3, /* store lane */
  5980. 2, 3 /* store zero */
  5981. };
  5982. if (!((opcode <= SIMD_v128_store)
  5983. || (SIMD_v128_load8_lane <= opcode
  5984. && opcode <= SIMD_v128_load64_zero))) {
  5985. set_error_buf(error_buf, error_buf_size,
  5986. "the opcode doesn't include memarg");
  5987. return false;
  5988. }
  5989. if ((opcode <= SIMD_v128_store
  5990. && align > mem_access_aligns[opcode - SIMD_v128_load])
  5991. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  5992. && align > mem_access_aligns_load_lane[opcode
  5993. - SIMD_v128_load8_lane])) {
  5994. set_error_buf(error_buf, error_buf_size,
  5995. "alignment must not be larger than natural");
  5996. return false;
  5997. }
  5998. return true;
  5999. }
  6000. static bool
  6001. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  6002. uint32 error_buf_size)
  6003. {
  6004. switch (opcode) {
  6005. case SIMD_i8x16_extract_lane_s:
  6006. case SIMD_i8x16_extract_lane_u:
  6007. case SIMD_i8x16_replace_lane:
  6008. if (lane >= 16) {
  6009. goto fail;
  6010. }
  6011. break;
  6012. case SIMD_i16x8_extract_lane_s:
  6013. case SIMD_i16x8_extract_lane_u:
  6014. case SIMD_i16x8_replace_lane:
  6015. if (lane >= 8) {
  6016. goto fail;
  6017. }
  6018. break;
  6019. case SIMD_i32x4_extract_lane:
  6020. case SIMD_i32x4_replace_lane:
  6021. case SIMD_f32x4_extract_lane:
  6022. case SIMD_f32x4_replace_lane:
  6023. if (lane >= 4) {
  6024. goto fail;
  6025. }
  6026. break;
  6027. case SIMD_i64x2_extract_lane:
  6028. case SIMD_i64x2_replace_lane:
  6029. case SIMD_f64x2_extract_lane:
  6030. case SIMD_f64x2_replace_lane:
  6031. if (lane >= 2) {
  6032. goto fail;
  6033. }
  6034. break;
  6035. case SIMD_v128_load8_lane:
  6036. case SIMD_v128_load16_lane:
  6037. case SIMD_v128_load32_lane:
  6038. case SIMD_v128_load64_lane:
  6039. case SIMD_v128_store8_lane:
  6040. case SIMD_v128_store16_lane:
  6041. case SIMD_v128_store32_lane:
  6042. case SIMD_v128_store64_lane:
  6043. case SIMD_v128_load32_zero:
  6044. case SIMD_v128_load64_zero:
  6045. {
  6046. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  6047. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  6048. goto fail;
  6049. }
  6050. break;
  6051. }
  6052. default:
  6053. goto fail;
  6054. }
  6055. return true;
  6056. fail:
  6057. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  6058. return false;
  6059. }
  6060. static bool
  6061. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  6062. {
  6063. uint8 i;
  6064. for (i = 0; i != 16; ++i) {
  6065. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  6066. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  6067. return false;
  6068. }
  6069. }
  6070. return true;
  6071. }
  6072. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6073. #endif /* end of WASM_ENABLE_SIMD */
  6074. #if WASM_ENABLE_SHARED_MEMORY != 0
  6075. static bool
  6076. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  6077. uint32 error_buf_size)
  6078. {
  6079. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  6080. uint8 mem_access_aligns[] = {
  6081. 2, 3, 0, 1, 0, 1, 2,
  6082. };
  6083. uint8 expect;
  6084. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  6085. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  6086. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  6087. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  6088. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  6089. }
  6090. else {
  6091. /* 7 opcodes in every group */
  6092. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  6093. }
  6094. if (align != expect) {
  6095. set_error_buf(error_buf, error_buf_size,
  6096. "alignment isn't equal to natural");
  6097. return false;
  6098. }
  6099. return true;
  6100. }
  6101. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6102. static bool
  6103. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  6104. char *error_buf, uint32 error_buf_size)
  6105. {
  6106. BranchBlock *target_block, *cur_block;
  6107. BlockType *target_block_type;
  6108. uint8 *types = NULL, *frame_ref;
  6109. uint32 arity = 0;
  6110. int32 i, available_stack_cell;
  6111. uint16 cell_num;
  6112. if (loader_ctx->csp_num < depth + 1) {
  6113. set_error_buf(error_buf, error_buf_size,
  6114. "unknown label, "
  6115. "unexpected end of section or function");
  6116. return false;
  6117. }
  6118. cur_block = loader_ctx->frame_csp - 1;
  6119. target_block = loader_ctx->frame_csp - (depth + 1);
  6120. target_block_type = &target_block->block_type;
  6121. frame_ref = loader_ctx->frame_ref;
  6122. /* Note: loop's arity is different from if and block. loop's arity is
  6123. * its parameter count while if and block arity is result count.
  6124. */
  6125. if (target_block->label_type == LABEL_TYPE_LOOP)
  6126. arity = block_type_get_param_types(target_block_type, &types);
  6127. else
  6128. arity = block_type_get_result_types(target_block_type, &types);
  6129. /* If the stack is in polymorphic state, just clear the stack
  6130. * and then re-push the values to make the stack top values
  6131. * match block type. */
  6132. if (cur_block->is_stack_polymorphic) {
  6133. for (i = (int32)arity - 1; i >= 0; i--) {
  6134. #if WASM_ENABLE_FAST_INTERP != 0
  6135. POP_OFFSET_TYPE(types[i]);
  6136. #endif
  6137. POP_TYPE(types[i]);
  6138. }
  6139. for (i = 0; i < (int32)arity; i++) {
  6140. #if WASM_ENABLE_FAST_INTERP != 0
  6141. bool disable_emit = true;
  6142. int16 operand_offset = 0;
  6143. PUSH_OFFSET_TYPE(types[i]);
  6144. #endif
  6145. PUSH_TYPE(types[i]);
  6146. }
  6147. return true;
  6148. }
  6149. available_stack_cell =
  6150. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  6151. /* Check stack top values match target block type */
  6152. for (i = (int32)arity - 1; i >= 0; i--) {
  6153. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  6154. error_buf, error_buf_size))
  6155. return false;
  6156. cell_num = wasm_value_type_cell_num(types[i]);
  6157. frame_ref -= cell_num;
  6158. available_stack_cell -= cell_num;
  6159. }
  6160. return true;
  6161. fail:
  6162. return false;
  6163. }
  6164. static BranchBlock *
  6165. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  6166. char *error_buf, uint32 error_buf_size)
  6167. {
  6168. uint8 *p = *p_buf, *p_end = buf_end;
  6169. BranchBlock *frame_csp_tmp;
  6170. uint32 depth;
  6171. read_leb_uint32(p, p_end, depth);
  6172. CHECK_BR(depth);
  6173. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  6174. #if WASM_ENABLE_FAST_INTERP != 0
  6175. emit_br_info(frame_csp_tmp);
  6176. #endif
  6177. *p_buf = p;
  6178. return frame_csp_tmp;
  6179. fail:
  6180. return NULL;
  6181. }
  6182. static bool
  6183. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  6184. char *error_buf, uint32 error_buf_size)
  6185. {
  6186. BlockType *block_type = &block->block_type;
  6187. uint8 *return_types = NULL;
  6188. uint32 return_count = 0;
  6189. int32 available_stack_cell, return_cell_num, i;
  6190. uint8 *frame_ref = NULL;
  6191. available_stack_cell =
  6192. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  6193. return_count = block_type_get_result_types(block_type, &return_types);
  6194. return_cell_num =
  6195. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  6196. /* If the stack is in polymorphic state, just clear the stack
  6197. * and then re-push the values to make the stack top values
  6198. * match block type. */
  6199. if (block->is_stack_polymorphic) {
  6200. for (i = (int32)return_count - 1; i >= 0; i--) {
  6201. #if WASM_ENABLE_FAST_INTERP != 0
  6202. POP_OFFSET_TYPE(return_types[i]);
  6203. #endif
  6204. POP_TYPE(return_types[i]);
  6205. }
  6206. /* Check stack is empty */
  6207. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  6208. set_error_buf(
  6209. error_buf, error_buf_size,
  6210. "type mismatch: stack size does not match block type");
  6211. goto fail;
  6212. }
  6213. for (i = 0; i < (int32)return_count; i++) {
  6214. #if WASM_ENABLE_FAST_INTERP != 0
  6215. bool disable_emit = true;
  6216. int16 operand_offset = 0;
  6217. PUSH_OFFSET_TYPE(return_types[i]);
  6218. #endif
  6219. PUSH_TYPE(return_types[i]);
  6220. }
  6221. return true;
  6222. }
  6223. /* Check stack cell num equals return cell num */
  6224. if (available_stack_cell != return_cell_num) {
  6225. set_error_buf(error_buf, error_buf_size,
  6226. "type mismatch: stack size does not match block type");
  6227. goto fail;
  6228. }
  6229. /* Check stack values match return types */
  6230. frame_ref = loader_ctx->frame_ref;
  6231. for (i = (int32)return_count - 1; i >= 0; i--) {
  6232. if (!check_stack_top_values(frame_ref, available_stack_cell,
  6233. return_types[i], error_buf, error_buf_size))
  6234. return false;
  6235. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  6236. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  6237. }
  6238. return true;
  6239. fail:
  6240. return false;
  6241. }
  6242. #if WASM_ENABLE_FAST_INTERP != 0
  6243. /* Copy parameters to dynamic space.
  6244. * 1) POP original parameter out;
  6245. * 2) Push and copy original values to dynamic space.
  6246. * The copy instruction format:
  6247. * Part a: param count
  6248. * Part b: all param total cell num
  6249. * Part c: each param's cell_num, src offset and dst offset
  6250. * Part d: each param's src offset
  6251. * Part e: each param's dst offset
  6252. */
  6253. static bool
  6254. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  6255. char *error_buf, uint32 error_buf_size)
  6256. {
  6257. int16 *frame_offset = NULL;
  6258. uint8 *cells = NULL, cell;
  6259. int16 *src_offsets = NULL;
  6260. uint8 *emit_data = NULL;
  6261. uint32 i;
  6262. BranchBlock *block = loader_ctx->frame_csp - 1;
  6263. BlockType *block_type = &block->block_type;
  6264. WASMType *wasm_type = block_type->u.type;
  6265. uint32 param_count = block_type->u.type->param_count;
  6266. int16 condition_offset = 0;
  6267. bool disable_emit = false;
  6268. int16 operand_offset = 0;
  6269. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  6270. /* For if block, we also need copy the condition operand offset. */
  6271. if (is_if_block)
  6272. size += sizeof(*cells) + sizeof(*src_offsets);
  6273. /* Allocate memory for the emit data */
  6274. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  6275. return false;
  6276. cells = emit_data;
  6277. src_offsets = (int16 *)(cells + param_count);
  6278. if (is_if_block)
  6279. condition_offset = *loader_ctx->frame_offset;
  6280. /* POP original parameter out */
  6281. for (i = 0; i < param_count; i++) {
  6282. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  6283. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  6284. }
  6285. frame_offset = loader_ctx->frame_offset;
  6286. /* Get each param's cell num and src offset */
  6287. for (i = 0; i < param_count; i++) {
  6288. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  6289. cells[i] = cell;
  6290. src_offsets[i] = *frame_offset;
  6291. frame_offset += cell;
  6292. }
  6293. /* emit copy instruction */
  6294. emit_label(EXT_OP_COPY_STACK_VALUES);
  6295. /* Part a) */
  6296. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  6297. /* Part b) */
  6298. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  6299. : wasm_type->param_cell_num);
  6300. /* Part c) */
  6301. for (i = 0; i < param_count; i++)
  6302. emit_byte(loader_ctx, cells[i]);
  6303. if (is_if_block)
  6304. emit_byte(loader_ctx, 1);
  6305. /* Part d) */
  6306. for (i = 0; i < param_count; i++)
  6307. emit_operand(loader_ctx, src_offsets[i]);
  6308. if (is_if_block)
  6309. emit_operand(loader_ctx, condition_offset);
  6310. /* Part e) */
  6311. /* Push to dynamic space. The push will emit the dst offset. */
  6312. for (i = 0; i < param_count; i++)
  6313. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  6314. if (is_if_block)
  6315. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  6316. /* Free the emit data */
  6317. wasm_runtime_free(emit_data);
  6318. return true;
  6319. fail:
  6320. return false;
  6321. }
  6322. #endif
  6323. /* reset the stack to the state of before entering the last block */
  6324. #if WASM_ENABLE_FAST_INTERP != 0
  6325. #define RESET_STACK() \
  6326. do { \
  6327. loader_ctx->stack_cell_num = \
  6328. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  6329. loader_ctx->frame_ref = \
  6330. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  6331. loader_ctx->frame_offset = \
  6332. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  6333. } while (0)
  6334. #else
  6335. #define RESET_STACK() \
  6336. do { \
  6337. loader_ctx->stack_cell_num = \
  6338. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  6339. loader_ctx->frame_ref = \
  6340. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  6341. } while (0)
  6342. #endif
  6343. /* set current block's stack polymorphic state */
  6344. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  6345. do { \
  6346. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  6347. _cur_block->is_stack_polymorphic = flag; \
  6348. } while (0)
  6349. #define BLOCK_HAS_PARAM(block_type) \
  6350. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  6351. #define PRESERVE_LOCAL_FOR_BLOCK() \
  6352. do { \
  6353. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  6354. error_buf_size))) { \
  6355. goto fail; \
  6356. } \
  6357. } while (0)
  6358. #if WASM_ENABLE_REF_TYPES != 0
  6359. static bool
  6360. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  6361. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  6362. {
  6363. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  6364. return false;
  6365. }
  6366. if (p_elem_type) {
  6367. if (table_idx < module->import_table_count)
  6368. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  6369. else
  6370. *p_elem_type =
  6371. module->tables[module->import_table_count + table_idx]
  6372. .elem_type;
  6373. }
  6374. return true;
  6375. }
  6376. static bool
  6377. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  6378. uint8 *p_elem_type, char *error_buf,
  6379. uint32 error_buf_size)
  6380. {
  6381. if (table_seg_idx >= module->table_seg_count) {
  6382. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  6383. table_seg_idx);
  6384. return false;
  6385. }
  6386. if (p_elem_type) {
  6387. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  6388. }
  6389. return true;
  6390. }
  6391. #endif
  6392. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6393. const uint8 *
  6394. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  6395. uint32 *len)
  6396. {
  6397. WASMCustomSection *section = module->custom_section_list;
  6398. while (section) {
  6399. if ((section->name_len == strlen(name))
  6400. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  6401. if (len) {
  6402. *len = section->content_len;
  6403. }
  6404. return section->content_addr;
  6405. }
  6406. section = section->next;
  6407. }
  6408. return false;
  6409. }
  6410. #endif
  6411. static bool
  6412. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  6413. uint32 cur_func_idx, char *error_buf,
  6414. uint32 error_buf_size)
  6415. {
  6416. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  6417. uint32 param_count, local_count, global_count;
  6418. uint8 *param_types, *local_types, local_type, global_type;
  6419. BlockType func_block_type;
  6420. uint16 *local_offsets, local_offset;
  6421. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  6422. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  6423. int32 i32_const = 0;
  6424. int64 i64_const;
  6425. uint8 opcode;
  6426. bool return_value = false;
  6427. WASMLoaderContext *loader_ctx;
  6428. BranchBlock *frame_csp_tmp;
  6429. #if WASM_ENABLE_FAST_INTERP != 0
  6430. uint8 *func_const_end, *func_const = NULL;
  6431. int16 operand_offset = 0;
  6432. uint8 last_op = 0;
  6433. bool disable_emit, preserve_local = false;
  6434. float32 f32_const;
  6435. float64 f64_const;
  6436. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  6437. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  6438. #endif
  6439. global_count = module->import_global_count + module->global_count;
  6440. param_count = func->func_type->param_count;
  6441. param_types = func->func_type->types;
  6442. func_block_type.is_value_type = false;
  6443. func_block_type.u.type = func->func_type;
  6444. local_count = func->local_count;
  6445. local_types = func->local_types;
  6446. local_offsets = func->local_offsets;
  6447. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  6448. goto fail;
  6449. }
  6450. #if WASM_ENABLE_FAST_INTERP != 0
  6451. /* For the first traverse, the initial value of preserved_local_offset has
  6452. * not been determined, we use the INT16_MAX to represent that a slot has
  6453. * been copied to preserve space. For second traverse, this field will be
  6454. * set to the appropriate value in wasm_loader_ctx_reinit.
  6455. * This is for Issue #1230,
  6456. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  6457. * drop opcodes need to know which slots are preserved, so those slots will
  6458. * not be treated as dynamically allocated slots */
  6459. loader_ctx->preserved_local_offset = INT16_MAX;
  6460. re_scan:
  6461. if (loader_ctx->code_compiled_size > 0) {
  6462. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  6463. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  6464. goto fail;
  6465. }
  6466. p = func->code;
  6467. func->code_compiled = loader_ctx->p_code_compiled;
  6468. func->code_compiled_size = loader_ctx->code_compiled_size;
  6469. }
  6470. #endif
  6471. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  6472. while (p < p_end) {
  6473. opcode = *p++;
  6474. #if WASM_ENABLE_FAST_INTERP != 0
  6475. p_org = p;
  6476. disable_emit = false;
  6477. emit_label(opcode);
  6478. #endif
  6479. switch (opcode) {
  6480. case WASM_OP_UNREACHABLE:
  6481. RESET_STACK();
  6482. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6483. break;
  6484. case WASM_OP_NOP:
  6485. #if WASM_ENABLE_FAST_INTERP != 0
  6486. skip_label();
  6487. #endif
  6488. break;
  6489. case WASM_OP_IF:
  6490. #if WASM_ENABLE_FAST_INTERP != 0
  6491. PRESERVE_LOCAL_FOR_BLOCK();
  6492. #endif
  6493. POP_I32();
  6494. goto handle_op_block_and_loop;
  6495. case WASM_OP_BLOCK:
  6496. case WASM_OP_LOOP:
  6497. #if WASM_ENABLE_FAST_INTERP != 0
  6498. PRESERVE_LOCAL_FOR_BLOCK();
  6499. #endif
  6500. handle_op_block_and_loop:
  6501. {
  6502. uint8 value_type;
  6503. BlockType block_type;
  6504. p_org = p - 1;
  6505. value_type = read_uint8(p);
  6506. if (is_byte_a_type(value_type)) {
  6507. /* If the first byte is one of these special values:
  6508. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  6509. * the single return value. */
  6510. block_type.is_value_type = true;
  6511. block_type.u.value_type = value_type;
  6512. }
  6513. else {
  6514. uint32 type_index;
  6515. /* Resolve the leb128 encoded type index as block type */
  6516. p--;
  6517. read_leb_uint32(p, p_end, type_index);
  6518. if (type_index >= module->type_count) {
  6519. set_error_buf(error_buf, error_buf_size,
  6520. "unknown type");
  6521. goto fail;
  6522. }
  6523. block_type.is_value_type = false;
  6524. block_type.u.type = module->types[type_index];
  6525. #if WASM_ENABLE_FAST_INTERP == 0
  6526. /* If block use type index as block type, change the opcode
  6527. * to new extended opcode so that interpreter can resolve
  6528. * the block quickly.
  6529. */
  6530. #if WASM_ENABLE_DEBUG_INTERP != 0
  6531. if (!record_fast_op(module, p_org, *p_org, error_buf,
  6532. error_buf_size)) {
  6533. goto fail;
  6534. }
  6535. #endif
  6536. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  6537. #endif
  6538. }
  6539. /* Pop block parameters from stack */
  6540. if (BLOCK_HAS_PARAM(block_type)) {
  6541. WASMType *wasm_type = block_type.u.type;
  6542. for (i = 0; i < block_type.u.type->param_count; i++)
  6543. POP_TYPE(
  6544. wasm_type->types[wasm_type->param_count - i - 1]);
  6545. }
  6546. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  6547. block_type, p);
  6548. /* Pass parameters to block */
  6549. if (BLOCK_HAS_PARAM(block_type)) {
  6550. for (i = 0; i < block_type.u.type->param_count; i++)
  6551. PUSH_TYPE(block_type.u.type->types[i]);
  6552. }
  6553. #if WASM_ENABLE_FAST_INTERP != 0
  6554. if (opcode == WASM_OP_BLOCK) {
  6555. skip_label();
  6556. }
  6557. else if (opcode == WASM_OP_LOOP) {
  6558. skip_label();
  6559. if (BLOCK_HAS_PARAM(block_type)) {
  6560. /* Make sure params are in dynamic space */
  6561. if (!copy_params_to_dynamic_space(
  6562. loader_ctx, false, error_buf, error_buf_size))
  6563. goto fail;
  6564. }
  6565. (loader_ctx->frame_csp - 1)->code_compiled =
  6566. loader_ctx->p_code_compiled;
  6567. }
  6568. else if (opcode == WASM_OP_IF) {
  6569. /* If block has parameters, we should make sure they are in
  6570. * dynamic space. Otherwise, when else branch is missing,
  6571. * the later opcode may consume incorrect operand offset.
  6572. * Spec case:
  6573. * (func (export "params-id") (param i32) (result i32)
  6574. * (i32.const 1)
  6575. * (i32.const 2)
  6576. * (if (param i32 i32) (result i32 i32) (local.get 0)
  6577. * (then)) (i32.add)
  6578. * )
  6579. *
  6580. * So we should emit a copy instruction before the if.
  6581. *
  6582. * And we also need to save the parameter offsets and
  6583. * recover them before entering else branch.
  6584. *
  6585. */
  6586. if (BLOCK_HAS_PARAM(block_type)) {
  6587. BranchBlock *block = loader_ctx->frame_csp - 1;
  6588. uint64 size;
  6589. /* skip the if condition operand offset */
  6590. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  6591. /* skip the if label */
  6592. skip_label();
  6593. /* Emit a copy instruction */
  6594. if (!copy_params_to_dynamic_space(
  6595. loader_ctx, true, error_buf, error_buf_size))
  6596. goto fail;
  6597. /* Emit the if instruction */
  6598. emit_label(opcode);
  6599. /* Emit the new condition operand offset */
  6600. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  6601. /* Save top param_count values of frame_offset stack, so
  6602. * that we can recover it before executing else branch
  6603. */
  6604. size = sizeof(int16)
  6605. * (uint64)block_type.u.type->param_cell_num;
  6606. if (!(block->param_frame_offsets = loader_malloc(
  6607. size, error_buf, error_buf_size)))
  6608. goto fail;
  6609. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  6610. loader_ctx->frame_offset
  6611. - size / sizeof(int16),
  6612. (uint32)size);
  6613. }
  6614. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  6615. emit_empty_label_addr_and_frame_ip(PATCH_END);
  6616. }
  6617. #endif
  6618. break;
  6619. }
  6620. case WASM_OP_ELSE:
  6621. {
  6622. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  6623. if (loader_ctx->csp_num < 2
  6624. || (loader_ctx->frame_csp - 1)->label_type
  6625. != LABEL_TYPE_IF) {
  6626. set_error_buf(
  6627. error_buf, error_buf_size,
  6628. "opcode else found without matched opcode if");
  6629. goto fail;
  6630. }
  6631. /* check whether if branch's stack matches its result type */
  6632. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  6633. error_buf, error_buf_size))
  6634. goto fail;
  6635. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  6636. #if WASM_ENABLE_FAST_INTERP != 0
  6637. /* if the result of if branch is in local or const area, add a
  6638. * copy op */
  6639. RESERVE_BLOCK_RET();
  6640. emit_empty_label_addr_and_frame_ip(PATCH_END);
  6641. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  6642. #endif
  6643. RESET_STACK();
  6644. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  6645. /* Pass parameters to if-false branch */
  6646. if (BLOCK_HAS_PARAM(block_type)) {
  6647. for (i = 0; i < block_type.u.type->param_count; i++)
  6648. PUSH_TYPE(block_type.u.type->types[i]);
  6649. }
  6650. #if WASM_ENABLE_FAST_INTERP != 0
  6651. /* Recover top param_count values of frame_offset stack */
  6652. if (BLOCK_HAS_PARAM((block_type))) {
  6653. uint32 size;
  6654. BranchBlock *block = loader_ctx->frame_csp - 1;
  6655. size = sizeof(int16) * block_type.u.type->param_cell_num;
  6656. bh_memcpy_s(loader_ctx->frame_offset, size,
  6657. block->param_frame_offsets, size);
  6658. loader_ctx->frame_offset += (size / sizeof(int16));
  6659. }
  6660. #endif
  6661. break;
  6662. }
  6663. case WASM_OP_END:
  6664. {
  6665. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6666. /* check whether block stack matches its result type */
  6667. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  6668. error_buf_size))
  6669. goto fail;
  6670. /* if no else branch, and return types do not match param types,
  6671. * fail */
  6672. if (cur_block->label_type == LABEL_TYPE_IF
  6673. && !cur_block->else_addr) {
  6674. uint32 block_param_count = 0, block_ret_count = 0;
  6675. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  6676. BlockType *cur_block_type = &cur_block->block_type;
  6677. if (cur_block_type->is_value_type) {
  6678. if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
  6679. block_ret_count = 1;
  6680. block_ret_types = &cur_block_type->u.value_type;
  6681. }
  6682. }
  6683. else {
  6684. block_param_count = cur_block_type->u.type->param_count;
  6685. block_ret_count = cur_block_type->u.type->result_count;
  6686. block_param_types = cur_block_type->u.type->types;
  6687. block_ret_types =
  6688. cur_block_type->u.type->types + block_param_count;
  6689. }
  6690. if (block_param_count != block_ret_count
  6691. || (block_param_count
  6692. && memcmp(block_param_types, block_ret_types,
  6693. block_param_count))) {
  6694. set_error_buf(error_buf, error_buf_size,
  6695. "type mismatch: else branch missing");
  6696. goto fail;
  6697. }
  6698. }
  6699. POP_CSP();
  6700. #if WASM_ENABLE_FAST_INTERP != 0
  6701. skip_label();
  6702. /* copy the result to the block return address */
  6703. RESERVE_BLOCK_RET();
  6704. apply_label_patch(loader_ctx, 0, PATCH_END);
  6705. free_label_patch_list(loader_ctx->frame_csp);
  6706. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  6707. int32 idx;
  6708. uint8 ret_type;
  6709. emit_label(WASM_OP_RETURN);
  6710. for (idx = (int32)func->func_type->result_count - 1;
  6711. idx >= 0; idx--) {
  6712. ret_type = *(func->func_type->types
  6713. + func->func_type->param_count + idx);
  6714. POP_OFFSET_TYPE(ret_type);
  6715. }
  6716. }
  6717. #endif
  6718. if (loader_ctx->csp_num > 0) {
  6719. loader_ctx->frame_csp->end_addr = p - 1;
  6720. }
  6721. else {
  6722. /* end of function block, function will return */
  6723. if (p < p_end) {
  6724. set_error_buf(error_buf, error_buf_size,
  6725. "section size mismatch");
  6726. goto fail;
  6727. }
  6728. }
  6729. break;
  6730. }
  6731. case WASM_OP_BR:
  6732. {
  6733. if (!(frame_csp_tmp = check_branch_block(
  6734. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  6735. goto fail;
  6736. RESET_STACK();
  6737. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6738. break;
  6739. }
  6740. case WASM_OP_BR_IF:
  6741. {
  6742. POP_I32();
  6743. if (!(frame_csp_tmp = check_branch_block(
  6744. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  6745. goto fail;
  6746. break;
  6747. }
  6748. case WASM_OP_BR_TABLE:
  6749. {
  6750. uint8 *ret_types = NULL;
  6751. uint32 ret_count = 0;
  6752. #if WASM_ENABLE_FAST_INTERP == 0
  6753. uint8 *p_depth_begin, *p_depth;
  6754. uint32 depth, j;
  6755. BrTableCache *br_table_cache = NULL;
  6756. p_org = p - 1;
  6757. #endif
  6758. read_leb_uint32(p, p_end, count);
  6759. #if WASM_ENABLE_FAST_INTERP != 0
  6760. emit_uint32(loader_ctx, count);
  6761. #endif
  6762. POP_I32();
  6763. #if WASM_ENABLE_FAST_INTERP == 0
  6764. p_depth_begin = p_depth = p;
  6765. #endif
  6766. for (i = 0; i <= count; i++) {
  6767. if (!(frame_csp_tmp =
  6768. check_branch_block(loader_ctx, &p, p_end,
  6769. error_buf, error_buf_size)))
  6770. goto fail;
  6771. if (i == 0) {
  6772. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  6773. ret_count = block_type_get_result_types(
  6774. &frame_csp_tmp->block_type, &ret_types);
  6775. }
  6776. else {
  6777. uint8 *tmp_ret_types = NULL;
  6778. uint32 tmp_ret_count = 0;
  6779. /* Check whether all table items have the same return
  6780. * type */
  6781. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  6782. tmp_ret_count = block_type_get_result_types(
  6783. &frame_csp_tmp->block_type, &tmp_ret_types);
  6784. if (ret_count != tmp_ret_count
  6785. || (ret_count
  6786. && 0
  6787. != memcmp(ret_types, tmp_ret_types,
  6788. ret_count))) {
  6789. set_error_buf(
  6790. error_buf, error_buf_size,
  6791. "type mismatch: br_table targets must "
  6792. "all use same result type");
  6793. goto fail;
  6794. }
  6795. }
  6796. #if WASM_ENABLE_FAST_INTERP == 0
  6797. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  6798. if (br_table_cache) {
  6799. br_table_cache->br_depths[i] = depth;
  6800. }
  6801. else {
  6802. if (depth > 255) {
  6803. /* The depth cannot be stored in one byte,
  6804. create br_table cache to store each depth */
  6805. #if WASM_ENABLE_DEBUG_INTERP != 0
  6806. if (!record_fast_op(module, p_org, *p_org,
  6807. error_buf, error_buf_size)) {
  6808. goto fail;
  6809. }
  6810. #endif
  6811. if (!(br_table_cache = loader_malloc(
  6812. offsetof(BrTableCache, br_depths)
  6813. + sizeof(uint32)
  6814. * (uint64)(count + 1),
  6815. error_buf, error_buf_size))) {
  6816. goto fail;
  6817. }
  6818. *p_org = EXT_OP_BR_TABLE_CACHE;
  6819. br_table_cache->br_table_op_addr = p_org;
  6820. br_table_cache->br_count = count;
  6821. /* Copy previous depths which are one byte */
  6822. for (j = 0; j < i; j++) {
  6823. br_table_cache->br_depths[j] = p_depth_begin[j];
  6824. }
  6825. br_table_cache->br_depths[i] = depth;
  6826. bh_list_insert(module->br_table_cache_list,
  6827. br_table_cache);
  6828. }
  6829. else {
  6830. /* The depth can be stored in one byte, use the
  6831. byte of the leb to store it */
  6832. *p_depth++ = (uint8)depth;
  6833. }
  6834. }
  6835. #endif
  6836. }
  6837. #if WASM_ENABLE_FAST_INTERP == 0
  6838. /* Set the tailing bytes to nop */
  6839. if (br_table_cache)
  6840. p_depth = p_depth_begin;
  6841. while (p_depth < p)
  6842. *p_depth++ = WASM_OP_NOP;
  6843. #endif
  6844. RESET_STACK();
  6845. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6846. break;
  6847. }
  6848. case WASM_OP_RETURN:
  6849. {
  6850. int32 idx;
  6851. uint8 ret_type;
  6852. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  6853. idx--) {
  6854. ret_type = *(func->func_type->types
  6855. + func->func_type->param_count + idx);
  6856. POP_TYPE(ret_type);
  6857. #if WASM_ENABLE_FAST_INTERP != 0
  6858. /* emit the offset after return opcode */
  6859. POP_OFFSET_TYPE(ret_type);
  6860. #endif
  6861. }
  6862. RESET_STACK();
  6863. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6864. break;
  6865. }
  6866. case WASM_OP_CALL:
  6867. #if WASM_ENABLE_TAIL_CALL != 0
  6868. case WASM_OP_RETURN_CALL:
  6869. #endif
  6870. {
  6871. WASMType *func_type;
  6872. int32 idx;
  6873. read_leb_uint32(p, p_end, func_idx);
  6874. #if WASM_ENABLE_FAST_INTERP != 0
  6875. /* we need to emit func_idx before arguments */
  6876. emit_uint32(loader_ctx, func_idx);
  6877. #endif
  6878. if (!check_function_index(module, func_idx, error_buf,
  6879. error_buf_size)) {
  6880. goto fail;
  6881. }
  6882. if (func_idx < module->import_function_count)
  6883. func_type =
  6884. module->import_functions[func_idx].u.function.func_type;
  6885. else
  6886. func_type = module
  6887. ->functions[func_idx
  6888. - module->import_function_count]
  6889. ->func_type;
  6890. if (func_type->param_count > 0) {
  6891. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6892. idx--) {
  6893. POP_TYPE(func_type->types[idx]);
  6894. #if WASM_ENABLE_FAST_INTERP != 0
  6895. POP_OFFSET_TYPE(func_type->types[idx]);
  6896. #endif
  6897. }
  6898. }
  6899. #if WASM_ENABLE_TAIL_CALL != 0
  6900. if (opcode == WASM_OP_CALL) {
  6901. #endif
  6902. for (i = 0; i < func_type->result_count; i++) {
  6903. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6904. #if WASM_ENABLE_FAST_INTERP != 0
  6905. /* Here we emit each return value's dynamic_offset. But
  6906. * in fact these offsets are continuous, so interpreter
  6907. * only need to get the first return value's offset.
  6908. */
  6909. PUSH_OFFSET_TYPE(
  6910. func_type->types[func_type->param_count + i]);
  6911. #endif
  6912. }
  6913. #if WASM_ENABLE_TAIL_CALL != 0
  6914. }
  6915. else {
  6916. uint8 type;
  6917. if (func_type->result_count
  6918. != func->func_type->result_count) {
  6919. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  6920. "type mismatch: expect ",
  6921. func->func_type->result_count,
  6922. " return values but got other");
  6923. goto fail;
  6924. }
  6925. for (i = 0; i < func_type->result_count; i++) {
  6926. type = func->func_type
  6927. ->types[func->func_type->param_count + i];
  6928. if (func_type->types[func_type->param_count + i]
  6929. != type) {
  6930. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6931. "type mismatch: expect ",
  6932. type2str(type), " but got other");
  6933. goto fail;
  6934. }
  6935. }
  6936. RESET_STACK();
  6937. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6938. }
  6939. #endif
  6940. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6941. || WASM_ENABLE_WAMR_COMPILER != 0
  6942. func->has_op_func_call = true;
  6943. #endif
  6944. break;
  6945. }
  6946. /*
  6947. * if disable reference type: call_indirect typeidx, 0x00
  6948. * if enable reference type: call_indirect typeidx, tableidx
  6949. */
  6950. case WASM_OP_CALL_INDIRECT:
  6951. #if WASM_ENABLE_TAIL_CALL != 0
  6952. case WASM_OP_RETURN_CALL_INDIRECT:
  6953. #endif
  6954. {
  6955. int32 idx;
  6956. WASMType *func_type;
  6957. read_leb_uint32(p, p_end, type_idx);
  6958. #if WASM_ENABLE_REF_TYPES != 0
  6959. read_leb_uint32(p, p_end, table_idx);
  6960. #else
  6961. CHECK_BUF(p, p_end, 1);
  6962. table_idx = read_uint8(p);
  6963. #endif
  6964. if (!check_table_index(module, table_idx, error_buf,
  6965. error_buf_size)) {
  6966. goto fail;
  6967. }
  6968. #if WASM_ENABLE_FAST_INTERP != 0
  6969. /* we need to emit before arguments */
  6970. #if WASM_ENABLE_TAIL_CALL != 0
  6971. emit_byte(loader_ctx, opcode);
  6972. #endif
  6973. emit_uint32(loader_ctx, type_idx);
  6974. emit_uint32(loader_ctx, table_idx);
  6975. #endif
  6976. /* skip elem idx */
  6977. POP_I32();
  6978. if (type_idx >= module->type_count) {
  6979. set_error_buf(error_buf, error_buf_size, "unknown type");
  6980. goto fail;
  6981. }
  6982. func_type = module->types[type_idx];
  6983. if (func_type->param_count > 0) {
  6984. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6985. idx--) {
  6986. POP_TYPE(func_type->types[idx]);
  6987. #if WASM_ENABLE_FAST_INTERP != 0
  6988. POP_OFFSET_TYPE(func_type->types[idx]);
  6989. #endif
  6990. }
  6991. }
  6992. #if WASM_ENABLE_TAIL_CALL != 0
  6993. if (opcode == WASM_OP_CALL_INDIRECT) {
  6994. #endif
  6995. for (i = 0; i < func_type->result_count; i++) {
  6996. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6997. #if WASM_ENABLE_FAST_INTERP != 0
  6998. PUSH_OFFSET_TYPE(
  6999. func_type->types[func_type->param_count + i]);
  7000. #endif
  7001. }
  7002. #if WASM_ENABLE_TAIL_CALL != 0
  7003. }
  7004. else {
  7005. uint8 type;
  7006. if (func_type->result_count
  7007. != func->func_type->result_count) {
  7008. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  7009. "type mismatch: expect ",
  7010. func->func_type->result_count,
  7011. " return values but got other");
  7012. goto fail;
  7013. }
  7014. for (i = 0; i < func_type->result_count; i++) {
  7015. type = func->func_type
  7016. ->types[func->func_type->param_count + i];
  7017. if (func_type->types[func_type->param_count + i]
  7018. != type) {
  7019. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7020. "type mismatch: expect ",
  7021. type2str(type), " but got other");
  7022. goto fail;
  7023. }
  7024. }
  7025. RESET_STACK();
  7026. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  7027. }
  7028. #endif
  7029. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  7030. || WASM_ENABLE_WAMR_COMPILER != 0
  7031. func->has_op_func_call = true;
  7032. #endif
  7033. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7034. func->has_op_call_indirect = true;
  7035. #endif
  7036. break;
  7037. }
  7038. case WASM_OP_DROP:
  7039. {
  7040. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  7041. int32 available_stack_cell =
  7042. (int32)(loader_ctx->stack_cell_num
  7043. - cur_block->stack_cell_num);
  7044. if (available_stack_cell <= 0
  7045. && !cur_block->is_stack_polymorphic) {
  7046. set_error_buf(error_buf, error_buf_size,
  7047. "type mismatch, opcode drop was found "
  7048. "but stack was empty");
  7049. goto fail;
  7050. }
  7051. if (available_stack_cell > 0) {
  7052. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  7053. loader_ctx->frame_ref--;
  7054. loader_ctx->stack_cell_num--;
  7055. #if WASM_ENABLE_FAST_INTERP != 0
  7056. skip_label();
  7057. loader_ctx->frame_offset--;
  7058. if ((*(loader_ctx->frame_offset)
  7059. > loader_ctx->start_dynamic_offset)
  7060. && (*(loader_ctx->frame_offset)
  7061. < loader_ctx->max_dynamic_offset))
  7062. loader_ctx->dynamic_offset--;
  7063. #endif
  7064. }
  7065. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  7066. loader_ctx->frame_ref -= 2;
  7067. loader_ctx->stack_cell_num -= 2;
  7068. #if WASM_ENABLE_FAST_INTERP == 0
  7069. *(p - 1) = WASM_OP_DROP_64;
  7070. #endif
  7071. #if WASM_ENABLE_FAST_INTERP != 0
  7072. skip_label();
  7073. loader_ctx->frame_offset -= 2;
  7074. if ((*(loader_ctx->frame_offset)
  7075. > loader_ctx->start_dynamic_offset)
  7076. && (*(loader_ctx->frame_offset)
  7077. < loader_ctx->max_dynamic_offset))
  7078. loader_ctx->dynamic_offset -= 2;
  7079. #endif
  7080. }
  7081. #if WASM_ENABLE_SIMD != 0
  7082. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7083. else if (*(loader_ctx->frame_ref - 1) == REF_V128_1) {
  7084. loader_ctx->frame_ref -= 4;
  7085. loader_ctx->stack_cell_num -= 4;
  7086. }
  7087. #endif
  7088. #endif
  7089. else {
  7090. set_error_buf(error_buf, error_buf_size,
  7091. "type mismatch");
  7092. goto fail;
  7093. }
  7094. }
  7095. else {
  7096. #if WASM_ENABLE_FAST_INTERP != 0
  7097. skip_label();
  7098. #endif
  7099. }
  7100. break;
  7101. }
  7102. case WASM_OP_SELECT:
  7103. {
  7104. uint8 ref_type;
  7105. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  7106. int32 available_stack_cell;
  7107. POP_I32();
  7108. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  7109. - cur_block->stack_cell_num);
  7110. if (available_stack_cell <= 0
  7111. && !cur_block->is_stack_polymorphic) {
  7112. set_error_buf(error_buf, error_buf_size,
  7113. "type mismatch or invalid result arity, "
  7114. "opcode select was found "
  7115. "but stack was empty");
  7116. goto fail;
  7117. }
  7118. if (available_stack_cell > 0) {
  7119. switch (*(loader_ctx->frame_ref - 1)) {
  7120. case REF_I32:
  7121. case REF_F32:
  7122. break;
  7123. case REF_I64_2:
  7124. case REF_F64_2:
  7125. #if WASM_ENABLE_FAST_INTERP == 0
  7126. *(p - 1) = WASM_OP_SELECT_64;
  7127. #endif
  7128. #if WASM_ENABLE_FAST_INTERP != 0
  7129. if (loader_ctx->p_code_compiled) {
  7130. uint8 opcode_tmp = WASM_OP_SELECT_64;
  7131. uint8 *p_code_compiled_tmp =
  7132. loader_ctx->p_code_compiled - 2;
  7133. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7134. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7135. *(void **)(p_code_compiled_tmp
  7136. - sizeof(void *)) =
  7137. handle_table[opcode_tmp];
  7138. #else
  7139. int32 offset =
  7140. (int32)((uint8 *)handle_table[opcode_tmp]
  7141. - (uint8 *)handle_table[0]);
  7142. if (!(offset >= INT16_MIN
  7143. && offset < INT16_MAX)) {
  7144. set_error_buf(error_buf, error_buf_size,
  7145. "pre-compiled label offset "
  7146. "out of range");
  7147. goto fail;
  7148. }
  7149. *(int16 *)(p_code_compiled_tmp
  7150. - sizeof(int16)) = (int16)offset;
  7151. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7152. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7153. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7154. *(p_code_compiled_tmp - 1) = opcode_tmp;
  7155. #else
  7156. *(p_code_compiled_tmp - 2) = opcode_tmp;
  7157. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7158. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7159. }
  7160. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7161. break;
  7162. #if WASM_ENABLE_SIMD != 0
  7163. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7164. case REF_V128_4:
  7165. break;
  7166. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7167. #endif /* WASM_ENABLE_SIMD != 0 */
  7168. default:
  7169. {
  7170. set_error_buf(error_buf, error_buf_size,
  7171. "type mismatch");
  7172. goto fail;
  7173. }
  7174. }
  7175. ref_type = *(loader_ctx->frame_ref - 1);
  7176. #if WASM_ENABLE_FAST_INTERP != 0
  7177. POP_OFFSET_TYPE(ref_type);
  7178. POP_TYPE(ref_type);
  7179. POP_OFFSET_TYPE(ref_type);
  7180. POP_TYPE(ref_type);
  7181. PUSH_OFFSET_TYPE(ref_type);
  7182. PUSH_TYPE(ref_type);
  7183. #else
  7184. POP2_AND_PUSH(ref_type, ref_type);
  7185. #endif
  7186. }
  7187. else {
  7188. #if WASM_ENABLE_FAST_INTERP != 0
  7189. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  7190. #endif
  7191. PUSH_TYPE(VALUE_TYPE_ANY);
  7192. }
  7193. break;
  7194. }
  7195. #if WASM_ENABLE_REF_TYPES != 0
  7196. case WASM_OP_SELECT_T:
  7197. {
  7198. uint8 vec_len, ref_type;
  7199. read_leb_uint32(p, p_end, vec_len);
  7200. if (!vec_len) {
  7201. set_error_buf(error_buf, error_buf_size,
  7202. "invalid result arity");
  7203. goto fail;
  7204. }
  7205. CHECK_BUF(p, p_end, 1);
  7206. ref_type = read_uint8(p);
  7207. if (!is_value_type(ref_type)) {
  7208. set_error_buf(error_buf, error_buf_size,
  7209. "unknown value type");
  7210. goto fail;
  7211. }
  7212. POP_I32();
  7213. #if WASM_ENABLE_FAST_INTERP != 0
  7214. if (loader_ctx->p_code_compiled) {
  7215. uint8 opcode_tmp = WASM_OP_SELECT;
  7216. uint8 *p_code_compiled_tmp =
  7217. loader_ctx->p_code_compiled - 2;
  7218. if (ref_type == VALUE_TYPE_V128) {
  7219. #if (WASM_ENABLE_SIMD == 0) \
  7220. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  7221. set_error_buf(error_buf, error_buf_size,
  7222. "SIMD v128 type isn't supported");
  7223. goto fail;
  7224. #endif
  7225. }
  7226. else {
  7227. if (ref_type == VALUE_TYPE_F64
  7228. || ref_type == VALUE_TYPE_I64)
  7229. opcode_tmp = WASM_OP_SELECT_64;
  7230. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7231. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7232. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  7233. handle_table[opcode_tmp];
  7234. #else
  7235. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  7236. - (uint8 *)handle_table[0]);
  7237. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  7238. set_error_buf(
  7239. error_buf, error_buf_size,
  7240. "pre-compiled label offset out of range");
  7241. goto fail;
  7242. }
  7243. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  7244. (int16)offset;
  7245. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7246. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7247. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7248. *(p_code_compiled_tmp - 1) = opcode_tmp;
  7249. #else
  7250. *(p_code_compiled_tmp - 2) = opcode_tmp;
  7251. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7252. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7253. }
  7254. }
  7255. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  7256. #if WASM_ENABLE_FAST_INTERP != 0
  7257. POP_OFFSET_TYPE(ref_type);
  7258. POP_TYPE(ref_type);
  7259. POP_OFFSET_TYPE(ref_type);
  7260. POP_TYPE(ref_type);
  7261. PUSH_OFFSET_TYPE(ref_type);
  7262. PUSH_TYPE(ref_type);
  7263. #else
  7264. POP2_AND_PUSH(ref_type, ref_type);
  7265. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  7266. (void)vec_len;
  7267. break;
  7268. }
  7269. /* table.get x. tables[x]. [i32] -> [t] */
  7270. /* table.set x. tables[x]. [i32 t] -> [] */
  7271. case WASM_OP_TABLE_GET:
  7272. case WASM_OP_TABLE_SET:
  7273. {
  7274. uint8 decl_ref_type;
  7275. read_leb_uint32(p, p_end, table_idx);
  7276. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  7277. error_buf, error_buf_size))
  7278. goto fail;
  7279. #if WASM_ENABLE_FAST_INTERP != 0
  7280. emit_uint32(loader_ctx, table_idx);
  7281. #endif
  7282. if (opcode == WASM_OP_TABLE_GET) {
  7283. POP_I32();
  7284. #if WASM_ENABLE_FAST_INTERP != 0
  7285. PUSH_OFFSET_TYPE(decl_ref_type);
  7286. #endif
  7287. PUSH_TYPE(decl_ref_type);
  7288. }
  7289. else {
  7290. #if WASM_ENABLE_FAST_INTERP != 0
  7291. POP_OFFSET_TYPE(decl_ref_type);
  7292. #endif
  7293. POP_TYPE(decl_ref_type);
  7294. POP_I32();
  7295. }
  7296. break;
  7297. }
  7298. case WASM_OP_REF_NULL:
  7299. {
  7300. uint8 ref_type;
  7301. CHECK_BUF(p, p_end, 1);
  7302. ref_type = read_uint8(p);
  7303. if (ref_type != VALUE_TYPE_FUNCREF
  7304. && ref_type != VALUE_TYPE_EXTERNREF) {
  7305. set_error_buf(error_buf, error_buf_size,
  7306. "unknown value type");
  7307. goto fail;
  7308. }
  7309. #if WASM_ENABLE_FAST_INTERP != 0
  7310. PUSH_OFFSET_TYPE(ref_type);
  7311. #endif
  7312. PUSH_TYPE(ref_type);
  7313. break;
  7314. }
  7315. case WASM_OP_REF_IS_NULL:
  7316. {
  7317. #if WASM_ENABLE_FAST_INTERP != 0
  7318. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  7319. VALUE_TYPE_FUNCREF,
  7320. error_buf, error_buf_size)
  7321. && !wasm_loader_pop_frame_ref_offset(
  7322. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  7323. error_buf_size)) {
  7324. goto fail;
  7325. }
  7326. #else
  7327. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  7328. error_buf, error_buf_size)
  7329. && !wasm_loader_pop_frame_ref(loader_ctx,
  7330. VALUE_TYPE_EXTERNREF,
  7331. error_buf, error_buf_size)) {
  7332. goto fail;
  7333. }
  7334. #endif
  7335. PUSH_I32();
  7336. break;
  7337. }
  7338. case WASM_OP_REF_FUNC:
  7339. {
  7340. read_leb_uint32(p, p_end, func_idx);
  7341. if (!check_function_index(module, func_idx, error_buf,
  7342. error_buf_size)) {
  7343. goto fail;
  7344. }
  7345. if (func_idx == cur_func_idx + module->import_function_count) {
  7346. WASMTableSeg *table_seg = module->table_segments;
  7347. bool func_declared = false;
  7348. uint32 j;
  7349. /* Check whether current function is declared */
  7350. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  7351. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  7352. && wasm_elem_is_declarative(table_seg->mode)) {
  7353. for (j = 0; j < table_seg->function_count; j++) {
  7354. if (table_seg->func_indexes[j] == func_idx) {
  7355. func_declared = true;
  7356. break;
  7357. }
  7358. }
  7359. }
  7360. }
  7361. if (!func_declared) {
  7362. set_error_buf(error_buf, error_buf_size,
  7363. "undeclared function reference");
  7364. goto fail;
  7365. }
  7366. }
  7367. #if WASM_ENABLE_FAST_INTERP != 0
  7368. emit_uint32(loader_ctx, func_idx);
  7369. #endif
  7370. PUSH_FUNCREF();
  7371. break;
  7372. }
  7373. #endif /* WASM_ENABLE_REF_TYPES */
  7374. case WASM_OP_GET_LOCAL:
  7375. {
  7376. p_org = p - 1;
  7377. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  7378. PUSH_TYPE(local_type);
  7379. #if WASM_ENABLE_FAST_INTERP != 0
  7380. /* Get Local is optimized out */
  7381. skip_label();
  7382. disable_emit = true;
  7383. operand_offset = local_offset;
  7384. PUSH_OFFSET_TYPE(local_type);
  7385. #else
  7386. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  7387. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  7388. if (local_offset < 0x80) {
  7389. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  7390. if (is_32bit_type(local_type)) {
  7391. *p_org++ = (uint8)local_offset;
  7392. }
  7393. else {
  7394. *p_org++ = (uint8)(local_offset | 0x80);
  7395. }
  7396. while (p_org < p) {
  7397. *p_org++ = WASM_OP_NOP;
  7398. }
  7399. }
  7400. #endif
  7401. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7402. break;
  7403. }
  7404. case WASM_OP_SET_LOCAL:
  7405. {
  7406. p_org = p - 1;
  7407. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  7408. POP_TYPE(local_type);
  7409. #if WASM_ENABLE_FAST_INTERP != 0
  7410. if (!(preserve_referenced_local(
  7411. loader_ctx, opcode, local_offset, local_type,
  7412. &preserve_local, error_buf, error_buf_size)))
  7413. goto fail;
  7414. if (local_offset < 256) {
  7415. skip_label();
  7416. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  7417. if (loader_ctx->p_code_compiled)
  7418. STORE_U16(loader_ctx->p_code_compiled - 2,
  7419. local_offset);
  7420. loader_ctx->frame_offset--;
  7421. loader_ctx->dynamic_offset--;
  7422. }
  7423. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  7424. if (loader_ctx->p_code_compiled)
  7425. STORE_U16(loader_ctx->p_code_compiled - 2,
  7426. local_offset);
  7427. loader_ctx->frame_offset -= 2;
  7428. loader_ctx->dynamic_offset -= 2;
  7429. }
  7430. else {
  7431. if (is_32bit_type(local_type)) {
  7432. emit_label(EXT_OP_SET_LOCAL_FAST);
  7433. emit_byte(loader_ctx, (uint8)local_offset);
  7434. }
  7435. else {
  7436. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  7437. emit_byte(loader_ctx, (uint8)local_offset);
  7438. }
  7439. POP_OFFSET_TYPE(local_type);
  7440. }
  7441. }
  7442. else { /* local index larger than 255, reserve leb */
  7443. emit_uint32(loader_ctx, local_idx);
  7444. POP_OFFSET_TYPE(local_type);
  7445. }
  7446. #else
  7447. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  7448. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  7449. if (local_offset < 0x80) {
  7450. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  7451. if (is_32bit_type(local_type)) {
  7452. *p_org++ = (uint8)local_offset;
  7453. }
  7454. else {
  7455. *p_org++ = (uint8)(local_offset | 0x80);
  7456. }
  7457. while (p_org < p) {
  7458. *p_org++ = WASM_OP_NOP;
  7459. }
  7460. }
  7461. #endif
  7462. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7463. break;
  7464. }
  7465. case WASM_OP_TEE_LOCAL:
  7466. {
  7467. p_org = p - 1;
  7468. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  7469. #if WASM_ENABLE_FAST_INTERP != 0
  7470. /* If the stack is in polymorphic state, do fake pop and push on
  7471. offset stack to keep the depth of offset stack to be the
  7472. same with ref stack */
  7473. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  7474. if (cur_block->is_stack_polymorphic) {
  7475. POP_OFFSET_TYPE(local_type);
  7476. PUSH_OFFSET_TYPE(local_type);
  7477. }
  7478. #endif
  7479. POP_TYPE(local_type);
  7480. PUSH_TYPE(local_type);
  7481. #if WASM_ENABLE_FAST_INTERP != 0
  7482. if (!(preserve_referenced_local(
  7483. loader_ctx, opcode, local_offset, local_type,
  7484. &preserve_local, error_buf, error_buf_size)))
  7485. goto fail;
  7486. if (local_offset < 256) {
  7487. skip_label();
  7488. if (is_32bit_type(local_type)) {
  7489. emit_label(EXT_OP_TEE_LOCAL_FAST);
  7490. emit_byte(loader_ctx, (uint8)local_offset);
  7491. }
  7492. else {
  7493. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  7494. emit_byte(loader_ctx, (uint8)local_offset);
  7495. }
  7496. }
  7497. else { /* local index larger than 255, reserve leb */
  7498. emit_uint32(loader_ctx, local_idx);
  7499. }
  7500. emit_operand(loader_ctx,
  7501. *(loader_ctx->frame_offset
  7502. - wasm_value_type_cell_num(local_type)));
  7503. #else
  7504. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  7505. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  7506. if (local_offset < 0x80) {
  7507. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  7508. if (is_32bit_type(local_type)) {
  7509. *p_org++ = (uint8)local_offset;
  7510. }
  7511. else {
  7512. *p_org++ = (uint8)(local_offset | 0x80);
  7513. }
  7514. while (p_org < p) {
  7515. *p_org++ = WASM_OP_NOP;
  7516. }
  7517. }
  7518. #endif
  7519. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7520. break;
  7521. }
  7522. case WASM_OP_GET_GLOBAL:
  7523. {
  7524. p_org = p - 1;
  7525. read_leb_uint32(p, p_end, global_idx);
  7526. if (global_idx >= global_count) {
  7527. set_error_buf(error_buf, error_buf_size, "unknown global");
  7528. goto fail;
  7529. }
  7530. global_type =
  7531. global_idx < module->import_global_count
  7532. ? module->import_globals[global_idx].u.global.type
  7533. : module
  7534. ->globals[global_idx
  7535. - module->import_global_count]
  7536. .type;
  7537. PUSH_TYPE(global_type);
  7538. #if WASM_ENABLE_FAST_INTERP == 0
  7539. if (global_type == VALUE_TYPE_I64
  7540. || global_type == VALUE_TYPE_F64) {
  7541. #if WASM_ENABLE_DEBUG_INTERP != 0
  7542. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7543. error_buf_size)) {
  7544. goto fail;
  7545. }
  7546. #endif
  7547. *p_org = WASM_OP_GET_GLOBAL_64;
  7548. }
  7549. #else /* else of WASM_ENABLE_FAST_INTERP */
  7550. if (global_type == VALUE_TYPE_I64
  7551. || global_type == VALUE_TYPE_F64) {
  7552. skip_label();
  7553. emit_label(WASM_OP_GET_GLOBAL_64);
  7554. }
  7555. emit_uint32(loader_ctx, global_idx);
  7556. PUSH_OFFSET_TYPE(global_type);
  7557. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7558. break;
  7559. }
  7560. case WASM_OP_SET_GLOBAL:
  7561. {
  7562. bool is_mutable = false;
  7563. p_org = p - 1;
  7564. read_leb_uint32(p, p_end, global_idx);
  7565. if (global_idx >= global_count) {
  7566. set_error_buf(error_buf, error_buf_size, "unknown global");
  7567. goto fail;
  7568. }
  7569. is_mutable =
  7570. global_idx < module->import_global_count
  7571. ? module->import_globals[global_idx].u.global.is_mutable
  7572. : module
  7573. ->globals[global_idx
  7574. - module->import_global_count]
  7575. .is_mutable;
  7576. if (!is_mutable) {
  7577. set_error_buf(error_buf, error_buf_size,
  7578. "global is immutable");
  7579. goto fail;
  7580. }
  7581. global_type =
  7582. global_idx < module->import_global_count
  7583. ? module->import_globals[global_idx].u.global.type
  7584. : module
  7585. ->globals[global_idx
  7586. - module->import_global_count]
  7587. .type;
  7588. POP_TYPE(global_type);
  7589. #if WASM_ENABLE_FAST_INTERP == 0
  7590. if (global_type == VALUE_TYPE_I64
  7591. || global_type == VALUE_TYPE_F64) {
  7592. #if WASM_ENABLE_DEBUG_INTERP != 0
  7593. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7594. error_buf_size)) {
  7595. goto fail;
  7596. }
  7597. #endif
  7598. *p_org = WASM_OP_SET_GLOBAL_64;
  7599. }
  7600. else if (module->aux_stack_size > 0
  7601. && global_idx == module->aux_stack_top_global_index) {
  7602. #if WASM_ENABLE_DEBUG_INTERP != 0
  7603. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7604. error_buf_size)) {
  7605. goto fail;
  7606. }
  7607. #endif
  7608. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  7609. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7610. func->has_op_set_global_aux_stack = true;
  7611. #endif
  7612. }
  7613. #else /* else of WASM_ENABLE_FAST_INTERP */
  7614. if (global_type == VALUE_TYPE_I64
  7615. || global_type == VALUE_TYPE_F64) {
  7616. skip_label();
  7617. emit_label(WASM_OP_SET_GLOBAL_64);
  7618. }
  7619. else if (module->aux_stack_size > 0
  7620. && global_idx == module->aux_stack_top_global_index) {
  7621. skip_label();
  7622. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  7623. }
  7624. emit_uint32(loader_ctx, global_idx);
  7625. POP_OFFSET_TYPE(global_type);
  7626. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7627. break;
  7628. }
  7629. /* load */
  7630. case WASM_OP_I32_LOAD:
  7631. case WASM_OP_I32_LOAD8_S:
  7632. case WASM_OP_I32_LOAD8_U:
  7633. case WASM_OP_I32_LOAD16_S:
  7634. case WASM_OP_I32_LOAD16_U:
  7635. case WASM_OP_I64_LOAD:
  7636. case WASM_OP_I64_LOAD8_S:
  7637. case WASM_OP_I64_LOAD8_U:
  7638. case WASM_OP_I64_LOAD16_S:
  7639. case WASM_OP_I64_LOAD16_U:
  7640. case WASM_OP_I64_LOAD32_S:
  7641. case WASM_OP_I64_LOAD32_U:
  7642. case WASM_OP_F32_LOAD:
  7643. case WASM_OP_F64_LOAD:
  7644. /* store */
  7645. case WASM_OP_I32_STORE:
  7646. case WASM_OP_I32_STORE8:
  7647. case WASM_OP_I32_STORE16:
  7648. case WASM_OP_I64_STORE:
  7649. case WASM_OP_I64_STORE8:
  7650. case WASM_OP_I64_STORE16:
  7651. case WASM_OP_I64_STORE32:
  7652. case WASM_OP_F32_STORE:
  7653. case WASM_OP_F64_STORE:
  7654. {
  7655. #if WASM_ENABLE_FAST_INTERP != 0
  7656. /* change F32/F64 into I32/I64 */
  7657. if (opcode == WASM_OP_F32_LOAD) {
  7658. skip_label();
  7659. emit_label(WASM_OP_I32_LOAD);
  7660. }
  7661. else if (opcode == WASM_OP_F64_LOAD) {
  7662. skip_label();
  7663. emit_label(WASM_OP_I64_LOAD);
  7664. }
  7665. else if (opcode == WASM_OP_F32_STORE) {
  7666. skip_label();
  7667. emit_label(WASM_OP_I32_STORE);
  7668. }
  7669. else if (opcode == WASM_OP_F64_STORE) {
  7670. skip_label();
  7671. emit_label(WASM_OP_I64_STORE);
  7672. }
  7673. #endif
  7674. CHECK_MEMORY();
  7675. read_leb_uint32(p, p_end, align); /* align */
  7676. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7677. if (!check_memory_access_align(opcode, align, error_buf,
  7678. error_buf_size)) {
  7679. goto fail;
  7680. }
  7681. #if WASM_ENABLE_FAST_INTERP != 0
  7682. emit_uint32(loader_ctx, mem_offset);
  7683. #endif
  7684. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7685. func->has_memory_operations = true;
  7686. #endif
  7687. switch (opcode) {
  7688. /* load */
  7689. case WASM_OP_I32_LOAD:
  7690. case WASM_OP_I32_LOAD8_S:
  7691. case WASM_OP_I32_LOAD8_U:
  7692. case WASM_OP_I32_LOAD16_S:
  7693. case WASM_OP_I32_LOAD16_U:
  7694. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7695. break;
  7696. case WASM_OP_I64_LOAD:
  7697. case WASM_OP_I64_LOAD8_S:
  7698. case WASM_OP_I64_LOAD8_U:
  7699. case WASM_OP_I64_LOAD16_S:
  7700. case WASM_OP_I64_LOAD16_U:
  7701. case WASM_OP_I64_LOAD32_S:
  7702. case WASM_OP_I64_LOAD32_U:
  7703. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7704. break;
  7705. case WASM_OP_F32_LOAD:
  7706. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7707. break;
  7708. case WASM_OP_F64_LOAD:
  7709. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7710. break;
  7711. /* store */
  7712. case WASM_OP_I32_STORE:
  7713. case WASM_OP_I32_STORE8:
  7714. case WASM_OP_I32_STORE16:
  7715. POP_I32();
  7716. POP_I32();
  7717. break;
  7718. case WASM_OP_I64_STORE:
  7719. case WASM_OP_I64_STORE8:
  7720. case WASM_OP_I64_STORE16:
  7721. case WASM_OP_I64_STORE32:
  7722. POP_I64();
  7723. POP_I32();
  7724. break;
  7725. case WASM_OP_F32_STORE:
  7726. POP_F32();
  7727. POP_I32();
  7728. break;
  7729. case WASM_OP_F64_STORE:
  7730. POP_F64();
  7731. POP_I32();
  7732. break;
  7733. default:
  7734. break;
  7735. }
  7736. break;
  7737. }
  7738. case WASM_OP_MEMORY_SIZE:
  7739. CHECK_MEMORY();
  7740. /* reserved byte 0x00 */
  7741. if (*p++ != 0x00) {
  7742. set_error_buf(error_buf, error_buf_size,
  7743. "zero byte expected");
  7744. goto fail;
  7745. }
  7746. PUSH_I32();
  7747. module->possible_memory_grow = true;
  7748. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7749. func->has_memory_operations = true;
  7750. #endif
  7751. break;
  7752. case WASM_OP_MEMORY_GROW:
  7753. CHECK_MEMORY();
  7754. /* reserved byte 0x00 */
  7755. if (*p++ != 0x00) {
  7756. set_error_buf(error_buf, error_buf_size,
  7757. "zero byte expected");
  7758. goto fail;
  7759. }
  7760. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7761. module->possible_memory_grow = true;
  7762. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  7763. || WASM_ENABLE_WAMR_COMPILER != 0
  7764. func->has_op_memory_grow = true;
  7765. #endif
  7766. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7767. func->has_memory_operations = true;
  7768. #endif
  7769. break;
  7770. case WASM_OP_I32_CONST:
  7771. read_leb_int32(p, p_end, i32_const);
  7772. #if WASM_ENABLE_FAST_INTERP != 0
  7773. skip_label();
  7774. disable_emit = true;
  7775. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  7776. if (operand_offset == 0) {
  7777. disable_emit = false;
  7778. emit_label(WASM_OP_I32_CONST);
  7779. emit_uint32(loader_ctx, i32_const);
  7780. }
  7781. #else
  7782. (void)i32_const;
  7783. #endif
  7784. PUSH_I32();
  7785. break;
  7786. case WASM_OP_I64_CONST:
  7787. read_leb_int64(p, p_end, i64_const);
  7788. #if WASM_ENABLE_FAST_INTERP != 0
  7789. skip_label();
  7790. disable_emit = true;
  7791. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  7792. if (operand_offset == 0) {
  7793. disable_emit = false;
  7794. emit_label(WASM_OP_I64_CONST);
  7795. emit_uint64(loader_ctx, i64_const);
  7796. }
  7797. #endif
  7798. PUSH_I64();
  7799. break;
  7800. case WASM_OP_F32_CONST:
  7801. p += sizeof(float32);
  7802. #if WASM_ENABLE_FAST_INTERP != 0
  7803. skip_label();
  7804. disable_emit = true;
  7805. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  7806. sizeof(float32));
  7807. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  7808. if (operand_offset == 0) {
  7809. disable_emit = false;
  7810. emit_label(WASM_OP_F32_CONST);
  7811. emit_float32(loader_ctx, f32_const);
  7812. }
  7813. #endif
  7814. PUSH_F32();
  7815. break;
  7816. case WASM_OP_F64_CONST:
  7817. p += sizeof(float64);
  7818. #if WASM_ENABLE_FAST_INTERP != 0
  7819. skip_label();
  7820. disable_emit = true;
  7821. /* Some MCU may require 8-byte align */
  7822. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  7823. sizeof(float64));
  7824. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  7825. if (operand_offset == 0) {
  7826. disable_emit = false;
  7827. emit_label(WASM_OP_F64_CONST);
  7828. emit_float64(loader_ctx, f64_const);
  7829. }
  7830. #endif
  7831. PUSH_F64();
  7832. break;
  7833. case WASM_OP_I32_EQZ:
  7834. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7835. break;
  7836. case WASM_OP_I32_EQ:
  7837. case WASM_OP_I32_NE:
  7838. case WASM_OP_I32_LT_S:
  7839. case WASM_OP_I32_LT_U:
  7840. case WASM_OP_I32_GT_S:
  7841. case WASM_OP_I32_GT_U:
  7842. case WASM_OP_I32_LE_S:
  7843. case WASM_OP_I32_LE_U:
  7844. case WASM_OP_I32_GE_S:
  7845. case WASM_OP_I32_GE_U:
  7846. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7847. break;
  7848. case WASM_OP_I64_EQZ:
  7849. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7850. break;
  7851. case WASM_OP_I64_EQ:
  7852. case WASM_OP_I64_NE:
  7853. case WASM_OP_I64_LT_S:
  7854. case WASM_OP_I64_LT_U:
  7855. case WASM_OP_I64_GT_S:
  7856. case WASM_OP_I64_GT_U:
  7857. case WASM_OP_I64_LE_S:
  7858. case WASM_OP_I64_LE_U:
  7859. case WASM_OP_I64_GE_S:
  7860. case WASM_OP_I64_GE_U:
  7861. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7862. break;
  7863. case WASM_OP_F32_EQ:
  7864. case WASM_OP_F32_NE:
  7865. case WASM_OP_F32_LT:
  7866. case WASM_OP_F32_GT:
  7867. case WASM_OP_F32_LE:
  7868. case WASM_OP_F32_GE:
  7869. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7870. break;
  7871. case WASM_OP_F64_EQ:
  7872. case WASM_OP_F64_NE:
  7873. case WASM_OP_F64_LT:
  7874. case WASM_OP_F64_GT:
  7875. case WASM_OP_F64_LE:
  7876. case WASM_OP_F64_GE:
  7877. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7878. break;
  7879. case WASM_OP_I32_CLZ:
  7880. case WASM_OP_I32_CTZ:
  7881. case WASM_OP_I32_POPCNT:
  7882. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7883. break;
  7884. case WASM_OP_I32_ADD:
  7885. case WASM_OP_I32_SUB:
  7886. case WASM_OP_I32_MUL:
  7887. case WASM_OP_I32_DIV_S:
  7888. case WASM_OP_I32_DIV_U:
  7889. case WASM_OP_I32_REM_S:
  7890. case WASM_OP_I32_REM_U:
  7891. case WASM_OP_I32_AND:
  7892. case WASM_OP_I32_OR:
  7893. case WASM_OP_I32_XOR:
  7894. case WASM_OP_I32_SHL:
  7895. case WASM_OP_I32_SHR_S:
  7896. case WASM_OP_I32_SHR_U:
  7897. case WASM_OP_I32_ROTL:
  7898. case WASM_OP_I32_ROTR:
  7899. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7900. break;
  7901. case WASM_OP_I64_CLZ:
  7902. case WASM_OP_I64_CTZ:
  7903. case WASM_OP_I64_POPCNT:
  7904. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7905. break;
  7906. case WASM_OP_I64_ADD:
  7907. case WASM_OP_I64_SUB:
  7908. case WASM_OP_I64_MUL:
  7909. case WASM_OP_I64_DIV_S:
  7910. case WASM_OP_I64_DIV_U:
  7911. case WASM_OP_I64_REM_S:
  7912. case WASM_OP_I64_REM_U:
  7913. case WASM_OP_I64_AND:
  7914. case WASM_OP_I64_OR:
  7915. case WASM_OP_I64_XOR:
  7916. case WASM_OP_I64_SHL:
  7917. case WASM_OP_I64_SHR_S:
  7918. case WASM_OP_I64_SHR_U:
  7919. case WASM_OP_I64_ROTL:
  7920. case WASM_OP_I64_ROTR:
  7921. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7922. break;
  7923. case WASM_OP_F32_ABS:
  7924. case WASM_OP_F32_NEG:
  7925. case WASM_OP_F32_CEIL:
  7926. case WASM_OP_F32_FLOOR:
  7927. case WASM_OP_F32_TRUNC:
  7928. case WASM_OP_F32_NEAREST:
  7929. case WASM_OP_F32_SQRT:
  7930. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7931. break;
  7932. case WASM_OP_F32_ADD:
  7933. case WASM_OP_F32_SUB:
  7934. case WASM_OP_F32_MUL:
  7935. case WASM_OP_F32_DIV:
  7936. case WASM_OP_F32_MIN:
  7937. case WASM_OP_F32_MAX:
  7938. case WASM_OP_F32_COPYSIGN:
  7939. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7940. break;
  7941. case WASM_OP_F64_ABS:
  7942. case WASM_OP_F64_NEG:
  7943. case WASM_OP_F64_CEIL:
  7944. case WASM_OP_F64_FLOOR:
  7945. case WASM_OP_F64_TRUNC:
  7946. case WASM_OP_F64_NEAREST:
  7947. case WASM_OP_F64_SQRT:
  7948. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7949. break;
  7950. case WASM_OP_F64_ADD:
  7951. case WASM_OP_F64_SUB:
  7952. case WASM_OP_F64_MUL:
  7953. case WASM_OP_F64_DIV:
  7954. case WASM_OP_F64_MIN:
  7955. case WASM_OP_F64_MAX:
  7956. case WASM_OP_F64_COPYSIGN:
  7957. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7958. break;
  7959. case WASM_OP_I32_WRAP_I64:
  7960. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7961. break;
  7962. case WASM_OP_I32_TRUNC_S_F32:
  7963. case WASM_OP_I32_TRUNC_U_F32:
  7964. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7965. break;
  7966. case WASM_OP_I32_TRUNC_S_F64:
  7967. case WASM_OP_I32_TRUNC_U_F64:
  7968. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7969. break;
  7970. case WASM_OP_I64_EXTEND_S_I32:
  7971. case WASM_OP_I64_EXTEND_U_I32:
  7972. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7973. break;
  7974. case WASM_OP_I64_TRUNC_S_F32:
  7975. case WASM_OP_I64_TRUNC_U_F32:
  7976. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7977. break;
  7978. case WASM_OP_I64_TRUNC_S_F64:
  7979. case WASM_OP_I64_TRUNC_U_F64:
  7980. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7981. break;
  7982. case WASM_OP_F32_CONVERT_S_I32:
  7983. case WASM_OP_F32_CONVERT_U_I32:
  7984. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7985. break;
  7986. case WASM_OP_F32_CONVERT_S_I64:
  7987. case WASM_OP_F32_CONVERT_U_I64:
  7988. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  7989. break;
  7990. case WASM_OP_F32_DEMOTE_F64:
  7991. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  7992. break;
  7993. case WASM_OP_F64_CONVERT_S_I32:
  7994. case WASM_OP_F64_CONVERT_U_I32:
  7995. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7996. break;
  7997. case WASM_OP_F64_CONVERT_S_I64:
  7998. case WASM_OP_F64_CONVERT_U_I64:
  7999. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  8000. break;
  8001. case WASM_OP_F64_PROMOTE_F32:
  8002. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  8003. break;
  8004. case WASM_OP_I32_REINTERPRET_F32:
  8005. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  8006. break;
  8007. case WASM_OP_I64_REINTERPRET_F64:
  8008. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  8009. break;
  8010. case WASM_OP_F32_REINTERPRET_I32:
  8011. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  8012. break;
  8013. case WASM_OP_F64_REINTERPRET_I64:
  8014. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  8015. break;
  8016. case WASM_OP_I32_EXTEND8_S:
  8017. case WASM_OP_I32_EXTEND16_S:
  8018. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8019. break;
  8020. case WASM_OP_I64_EXTEND8_S:
  8021. case WASM_OP_I64_EXTEND16_S:
  8022. case WASM_OP_I64_EXTEND32_S:
  8023. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  8024. break;
  8025. case WASM_OP_MISC_PREFIX:
  8026. {
  8027. uint32 opcode1;
  8028. read_leb_uint32(p, p_end, opcode1);
  8029. #if WASM_ENABLE_FAST_INTERP != 0
  8030. emit_byte(loader_ctx, ((uint8)opcode1));
  8031. #endif
  8032. switch (opcode1) {
  8033. case WASM_OP_I32_TRUNC_SAT_S_F32:
  8034. case WASM_OP_I32_TRUNC_SAT_U_F32:
  8035. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  8036. break;
  8037. case WASM_OP_I32_TRUNC_SAT_S_F64:
  8038. case WASM_OP_I32_TRUNC_SAT_U_F64:
  8039. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  8040. break;
  8041. case WASM_OP_I64_TRUNC_SAT_S_F32:
  8042. case WASM_OP_I64_TRUNC_SAT_U_F32:
  8043. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  8044. break;
  8045. case WASM_OP_I64_TRUNC_SAT_S_F64:
  8046. case WASM_OP_I64_TRUNC_SAT_U_F64:
  8047. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  8048. break;
  8049. #if WASM_ENABLE_BULK_MEMORY != 0
  8050. case WASM_OP_MEMORY_INIT:
  8051. {
  8052. read_leb_uint32(p, p_end, data_seg_idx);
  8053. #if WASM_ENABLE_FAST_INTERP != 0
  8054. emit_uint32(loader_ctx, data_seg_idx);
  8055. #endif
  8056. if (module->import_memory_count == 0
  8057. && module->memory_count == 0)
  8058. goto fail_unknown_memory;
  8059. if (*p++ != 0x00)
  8060. goto fail_zero_byte_expected;
  8061. if (data_seg_idx >= module->data_seg_count) {
  8062. set_error_buf_v(error_buf, error_buf_size,
  8063. "unknown data segment %d",
  8064. data_seg_idx);
  8065. goto fail;
  8066. }
  8067. if (module->data_seg_count1 == 0)
  8068. goto fail_data_cnt_sec_require;
  8069. POP_I32();
  8070. POP_I32();
  8071. POP_I32();
  8072. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8073. func->has_memory_operations = true;
  8074. #endif
  8075. break;
  8076. }
  8077. case WASM_OP_DATA_DROP:
  8078. {
  8079. read_leb_uint32(p, p_end, data_seg_idx);
  8080. #if WASM_ENABLE_FAST_INTERP != 0
  8081. emit_uint32(loader_ctx, data_seg_idx);
  8082. #endif
  8083. if (data_seg_idx >= module->data_seg_count) {
  8084. set_error_buf(error_buf, error_buf_size,
  8085. "unknown data segment");
  8086. goto fail;
  8087. }
  8088. if (module->data_seg_count1 == 0)
  8089. goto fail_data_cnt_sec_require;
  8090. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8091. func->has_memory_operations = true;
  8092. #endif
  8093. break;
  8094. }
  8095. case WASM_OP_MEMORY_COPY:
  8096. {
  8097. /* both src and dst memory index should be 0 */
  8098. if (*(int16 *)p != 0x0000)
  8099. goto fail_zero_byte_expected;
  8100. p += 2;
  8101. if (module->import_memory_count == 0
  8102. && module->memory_count == 0)
  8103. goto fail_unknown_memory;
  8104. POP_I32();
  8105. POP_I32();
  8106. POP_I32();
  8107. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8108. func->has_memory_operations = true;
  8109. #endif
  8110. break;
  8111. }
  8112. case WASM_OP_MEMORY_FILL:
  8113. {
  8114. if (*p++ != 0x00) {
  8115. goto fail_zero_byte_expected;
  8116. }
  8117. if (module->import_memory_count == 0
  8118. && module->memory_count == 0) {
  8119. goto fail_unknown_memory;
  8120. }
  8121. POP_I32();
  8122. POP_I32();
  8123. POP_I32();
  8124. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8125. func->has_memory_operations = true;
  8126. #endif
  8127. break;
  8128. }
  8129. fail_zero_byte_expected:
  8130. set_error_buf(error_buf, error_buf_size,
  8131. "zero byte expected");
  8132. goto fail;
  8133. fail_unknown_memory:
  8134. set_error_buf(error_buf, error_buf_size,
  8135. "unknown memory 0");
  8136. goto fail;
  8137. fail_data_cnt_sec_require:
  8138. set_error_buf(error_buf, error_buf_size,
  8139. "data count section required");
  8140. goto fail;
  8141. #endif /* WASM_ENABLE_BULK_MEMORY */
  8142. #if WASM_ENABLE_REF_TYPES != 0
  8143. case WASM_OP_TABLE_INIT:
  8144. {
  8145. uint8 seg_ref_type = 0, tbl_ref_type = 0;
  8146. read_leb_uint32(p, p_end, table_seg_idx);
  8147. read_leb_uint32(p, p_end, table_idx);
  8148. if (!get_table_elem_type(module, table_idx,
  8149. &tbl_ref_type, error_buf,
  8150. error_buf_size))
  8151. goto fail;
  8152. if (!get_table_seg_elem_type(module, table_seg_idx,
  8153. &seg_ref_type, error_buf,
  8154. error_buf_size))
  8155. goto fail;
  8156. if (seg_ref_type != tbl_ref_type) {
  8157. set_error_buf(error_buf, error_buf_size,
  8158. "type mismatch");
  8159. goto fail;
  8160. }
  8161. #if WASM_ENABLE_FAST_INTERP != 0
  8162. emit_uint32(loader_ctx, table_seg_idx);
  8163. emit_uint32(loader_ctx, table_idx);
  8164. #endif
  8165. POP_I32();
  8166. POP_I32();
  8167. POP_I32();
  8168. break;
  8169. }
  8170. case WASM_OP_ELEM_DROP:
  8171. {
  8172. read_leb_uint32(p, p_end, table_seg_idx);
  8173. if (!get_table_seg_elem_type(module, table_seg_idx,
  8174. NULL, error_buf,
  8175. error_buf_size))
  8176. goto fail;
  8177. #if WASM_ENABLE_FAST_INTERP != 0
  8178. emit_uint32(loader_ctx, table_seg_idx);
  8179. #endif
  8180. break;
  8181. }
  8182. case WASM_OP_TABLE_COPY:
  8183. {
  8184. uint8 src_ref_type, dst_ref_type;
  8185. uint32 src_tbl_idx, dst_tbl_idx;
  8186. read_leb_uint32(p, p_end, src_tbl_idx);
  8187. if (!get_table_elem_type(module, src_tbl_idx,
  8188. &src_ref_type, error_buf,
  8189. error_buf_size))
  8190. goto fail;
  8191. read_leb_uint32(p, p_end, dst_tbl_idx);
  8192. if (!get_table_elem_type(module, dst_tbl_idx,
  8193. &dst_ref_type, error_buf,
  8194. error_buf_size))
  8195. goto fail;
  8196. if (src_ref_type != dst_ref_type) {
  8197. set_error_buf(error_buf, error_buf_size,
  8198. "type mismatch");
  8199. goto fail;
  8200. }
  8201. #if WASM_ENABLE_FAST_INTERP != 0
  8202. emit_uint32(loader_ctx, src_tbl_idx);
  8203. emit_uint32(loader_ctx, dst_tbl_idx);
  8204. #endif
  8205. POP_I32();
  8206. POP_I32();
  8207. POP_I32();
  8208. break;
  8209. }
  8210. case WASM_OP_TABLE_SIZE:
  8211. {
  8212. read_leb_uint32(p, p_end, table_idx);
  8213. /* TODO: shall we create a new function to check
  8214. table idx instead of using below function? */
  8215. if (!get_table_elem_type(module, table_idx, NULL,
  8216. error_buf, error_buf_size))
  8217. goto fail;
  8218. #if WASM_ENABLE_FAST_INTERP != 0
  8219. emit_uint32(loader_ctx, table_idx);
  8220. #endif
  8221. PUSH_I32();
  8222. break;
  8223. }
  8224. case WASM_OP_TABLE_GROW:
  8225. case WASM_OP_TABLE_FILL:
  8226. {
  8227. uint8 decl_ref_type;
  8228. read_leb_uint32(p, p_end, table_idx);
  8229. if (!get_table_elem_type(module, table_idx,
  8230. &decl_ref_type, error_buf,
  8231. error_buf_size))
  8232. goto fail;
  8233. if (opcode1 == WASM_OP_TABLE_GROW) {
  8234. if (table_idx < module->import_table_count) {
  8235. module->import_tables[table_idx]
  8236. .u.table.possible_grow = true;
  8237. }
  8238. else {
  8239. module
  8240. ->tables[table_idx
  8241. - module->import_table_count]
  8242. .possible_grow = true;
  8243. }
  8244. }
  8245. #if WASM_ENABLE_FAST_INTERP != 0
  8246. emit_uint32(loader_ctx, table_idx);
  8247. #endif
  8248. POP_I32();
  8249. #if WASM_ENABLE_FAST_INTERP != 0
  8250. POP_OFFSET_TYPE(decl_ref_type);
  8251. #endif
  8252. POP_TYPE(decl_ref_type);
  8253. if (opcode1 == WASM_OP_TABLE_GROW)
  8254. PUSH_I32();
  8255. else
  8256. POP_I32();
  8257. break;
  8258. }
  8259. #endif /* WASM_ENABLE_REF_TYPES */
  8260. default:
  8261. set_error_buf_v(error_buf, error_buf_size,
  8262. "%s %02x %02x", "unsupported opcode",
  8263. 0xfc, opcode1);
  8264. goto fail;
  8265. }
  8266. break;
  8267. }
  8268. #if WASM_ENABLE_SIMD != 0
  8269. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8270. case WASM_OP_SIMD_PREFIX:
  8271. {
  8272. opcode = read_uint8(p);
  8273. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  8274. */
  8275. switch (opcode) {
  8276. /* memory instruction */
  8277. case SIMD_v128_load:
  8278. case SIMD_v128_load8x8_s:
  8279. case SIMD_v128_load8x8_u:
  8280. case SIMD_v128_load16x4_s:
  8281. case SIMD_v128_load16x4_u:
  8282. case SIMD_v128_load32x2_s:
  8283. case SIMD_v128_load32x2_u:
  8284. case SIMD_v128_load8_splat:
  8285. case SIMD_v128_load16_splat:
  8286. case SIMD_v128_load32_splat:
  8287. case SIMD_v128_load64_splat:
  8288. {
  8289. CHECK_MEMORY();
  8290. read_leb_uint32(p, p_end, align); /* align */
  8291. if (!check_simd_memory_access_align(
  8292. opcode, align, error_buf, error_buf_size)) {
  8293. goto fail;
  8294. }
  8295. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8296. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  8297. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8298. func->has_memory_operations = true;
  8299. #endif
  8300. break;
  8301. }
  8302. case SIMD_v128_store:
  8303. {
  8304. CHECK_MEMORY();
  8305. read_leb_uint32(p, p_end, align); /* align */
  8306. if (!check_simd_memory_access_align(
  8307. opcode, align, error_buf, error_buf_size)) {
  8308. goto fail;
  8309. }
  8310. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8311. POP_V128();
  8312. POP_I32();
  8313. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8314. func->has_memory_operations = true;
  8315. #endif
  8316. break;
  8317. }
  8318. /* basic operation */
  8319. case SIMD_v128_const:
  8320. {
  8321. CHECK_BUF1(p, p_end, 16);
  8322. p += 16;
  8323. PUSH_V128();
  8324. break;
  8325. }
  8326. case SIMD_v8x16_shuffle:
  8327. {
  8328. V128 mask;
  8329. CHECK_BUF1(p, p_end, 16);
  8330. mask = read_i8x16(p, error_buf, error_buf_size);
  8331. p += 16;
  8332. if (!check_simd_shuffle_mask(mask, error_buf,
  8333. error_buf_size)) {
  8334. goto fail;
  8335. }
  8336. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8337. break;
  8338. }
  8339. case SIMD_v8x16_swizzle:
  8340. {
  8341. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8342. break;
  8343. }
  8344. /* splat operation */
  8345. case SIMD_i8x16_splat:
  8346. case SIMD_i16x8_splat:
  8347. case SIMD_i32x4_splat:
  8348. case SIMD_i64x2_splat:
  8349. case SIMD_f32x4_splat:
  8350. case SIMD_f64x2_splat:
  8351. {
  8352. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  8353. VALUE_TYPE_I32, VALUE_TYPE_I64,
  8354. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  8355. POP_AND_PUSH(pop_type[opcode - SIMD_i8x16_splat],
  8356. VALUE_TYPE_V128);
  8357. break;
  8358. }
  8359. /* lane operation */
  8360. case SIMD_i8x16_extract_lane_s:
  8361. case SIMD_i8x16_extract_lane_u:
  8362. case SIMD_i8x16_replace_lane:
  8363. case SIMD_i16x8_extract_lane_s:
  8364. case SIMD_i16x8_extract_lane_u:
  8365. case SIMD_i16x8_replace_lane:
  8366. case SIMD_i32x4_extract_lane:
  8367. case SIMD_i32x4_replace_lane:
  8368. case SIMD_i64x2_extract_lane:
  8369. case SIMD_i64x2_replace_lane:
  8370. case SIMD_f32x4_extract_lane:
  8371. case SIMD_f32x4_replace_lane:
  8372. case SIMD_f64x2_extract_lane:
  8373. case SIMD_f64x2_replace_lane:
  8374. {
  8375. uint8 lane;
  8376. /* clang-format off */
  8377. uint8 replace[] = {
  8378. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  8379. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  8380. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  8381. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  8382. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  8383. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  8384. };
  8385. uint8 push_type[] = {
  8386. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  8387. VALUE_TYPE_V128,
  8388. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  8389. VALUE_TYPE_V128,
  8390. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  8391. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  8392. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  8393. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  8394. };
  8395. /* clang-format on */
  8396. CHECK_BUF(p, p_end, 1);
  8397. lane = read_uint8(p);
  8398. if (!check_simd_access_lane(opcode, lane, error_buf,
  8399. error_buf_size)) {
  8400. goto fail;
  8401. }
  8402. if (replace[opcode - SIMD_i8x16_extract_lane_s]) {
  8403. if (!(wasm_loader_pop_frame_ref(
  8404. loader_ctx,
  8405. replace[opcode - SIMD_i8x16_extract_lane_s],
  8406. error_buf, error_buf_size)))
  8407. goto fail;
  8408. }
  8409. POP_AND_PUSH(
  8410. VALUE_TYPE_V128,
  8411. push_type[opcode - SIMD_i8x16_extract_lane_s]);
  8412. break;
  8413. }
  8414. /* i8x16 compare operation */
  8415. case SIMD_i8x16_eq:
  8416. case SIMD_i8x16_ne:
  8417. case SIMD_i8x16_lt_s:
  8418. case SIMD_i8x16_lt_u:
  8419. case SIMD_i8x16_gt_s:
  8420. case SIMD_i8x16_gt_u:
  8421. case SIMD_i8x16_le_s:
  8422. case SIMD_i8x16_le_u:
  8423. case SIMD_i8x16_ge_s:
  8424. case SIMD_i8x16_ge_u:
  8425. /* i16x8 compare operation */
  8426. case SIMD_i16x8_eq:
  8427. case SIMD_i16x8_ne:
  8428. case SIMD_i16x8_lt_s:
  8429. case SIMD_i16x8_lt_u:
  8430. case SIMD_i16x8_gt_s:
  8431. case SIMD_i16x8_gt_u:
  8432. case SIMD_i16x8_le_s:
  8433. case SIMD_i16x8_le_u:
  8434. case SIMD_i16x8_ge_s:
  8435. case SIMD_i16x8_ge_u:
  8436. /* i32x4 compare operation */
  8437. case SIMD_i32x4_eq:
  8438. case SIMD_i32x4_ne:
  8439. case SIMD_i32x4_lt_s:
  8440. case SIMD_i32x4_lt_u:
  8441. case SIMD_i32x4_gt_s:
  8442. case SIMD_i32x4_gt_u:
  8443. case SIMD_i32x4_le_s:
  8444. case SIMD_i32x4_le_u:
  8445. case SIMD_i32x4_ge_s:
  8446. case SIMD_i32x4_ge_u:
  8447. /* f32x4 compare operation */
  8448. case SIMD_f32x4_eq:
  8449. case SIMD_f32x4_ne:
  8450. case SIMD_f32x4_lt:
  8451. case SIMD_f32x4_gt:
  8452. case SIMD_f32x4_le:
  8453. case SIMD_f32x4_ge:
  8454. /* f64x2 compare operation */
  8455. case SIMD_f64x2_eq:
  8456. case SIMD_f64x2_ne:
  8457. case SIMD_f64x2_lt:
  8458. case SIMD_f64x2_gt:
  8459. case SIMD_f64x2_le:
  8460. case SIMD_f64x2_ge:
  8461. {
  8462. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8463. break;
  8464. }
  8465. /* v128 operation */
  8466. case SIMD_v128_not:
  8467. {
  8468. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8469. break;
  8470. }
  8471. case SIMD_v128_and:
  8472. case SIMD_v128_andnot:
  8473. case SIMD_v128_or:
  8474. case SIMD_v128_xor:
  8475. {
  8476. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8477. break;
  8478. }
  8479. case SIMD_v128_bitselect:
  8480. {
  8481. POP_V128();
  8482. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8483. break;
  8484. }
  8485. case SIMD_v128_any_true:
  8486. {
  8487. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8488. break;
  8489. }
  8490. /* Load Lane Operation */
  8491. case SIMD_v128_load8_lane:
  8492. case SIMD_v128_load16_lane:
  8493. case SIMD_v128_load32_lane:
  8494. case SIMD_v128_load64_lane:
  8495. case SIMD_v128_store8_lane:
  8496. case SIMD_v128_store16_lane:
  8497. case SIMD_v128_store32_lane:
  8498. case SIMD_v128_store64_lane:
  8499. {
  8500. uint8 lane;
  8501. CHECK_MEMORY();
  8502. read_leb_uint32(p, p_end, align); /* align */
  8503. if (!check_simd_memory_access_align(
  8504. opcode, align, error_buf, error_buf_size)) {
  8505. goto fail;
  8506. }
  8507. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8508. CHECK_BUF(p, p_end, 1);
  8509. lane = read_uint8(p);
  8510. if (!check_simd_access_lane(opcode, lane, error_buf,
  8511. error_buf_size)) {
  8512. goto fail;
  8513. }
  8514. POP_V128();
  8515. POP_I32();
  8516. if (opcode < SIMD_v128_store8_lane) {
  8517. PUSH_V128();
  8518. }
  8519. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8520. func->has_memory_operations = true;
  8521. #endif
  8522. break;
  8523. }
  8524. case SIMD_v128_load32_zero:
  8525. case SIMD_v128_load64_zero:
  8526. {
  8527. CHECK_MEMORY();
  8528. read_leb_uint32(p, p_end, align); /* align */
  8529. if (!check_simd_memory_access_align(
  8530. opcode, align, error_buf, error_buf_size)) {
  8531. goto fail;
  8532. }
  8533. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8534. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  8535. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8536. func->has_memory_operations = true;
  8537. #endif
  8538. break;
  8539. }
  8540. /* Float conversion */
  8541. case SIMD_f32x4_demote_f64x2_zero:
  8542. case SIMD_f64x2_promote_low_f32x4_zero:
  8543. {
  8544. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8545. break;
  8546. }
  8547. /* i8x16 Operation */
  8548. case SIMD_i8x16_abs:
  8549. case SIMD_i8x16_neg:
  8550. case SIMD_i8x16_popcnt:
  8551. {
  8552. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8553. break;
  8554. }
  8555. case SIMD_i8x16_all_true:
  8556. case SIMD_i8x16_bitmask:
  8557. {
  8558. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8559. break;
  8560. }
  8561. case SIMD_i8x16_narrow_i16x8_s:
  8562. case SIMD_i8x16_narrow_i16x8_u:
  8563. {
  8564. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8565. break;
  8566. }
  8567. case SIMD_f32x4_ceil:
  8568. case SIMD_f32x4_floor:
  8569. case SIMD_f32x4_trunc:
  8570. case SIMD_f32x4_nearest:
  8571. {
  8572. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8573. break;
  8574. }
  8575. case SIMD_i8x16_shl:
  8576. case SIMD_i8x16_shr_s:
  8577. case SIMD_i8x16_shr_u:
  8578. {
  8579. POP_I32();
  8580. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8581. break;
  8582. }
  8583. case SIMD_i8x16_add:
  8584. case SIMD_i8x16_add_sat_s:
  8585. case SIMD_i8x16_add_sat_u:
  8586. case SIMD_i8x16_sub:
  8587. case SIMD_i8x16_sub_sat_s:
  8588. case SIMD_i8x16_sub_sat_u:
  8589. {
  8590. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8591. break;
  8592. }
  8593. case SIMD_f64x2_ceil:
  8594. case SIMD_f64x2_floor:
  8595. {
  8596. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8597. break;
  8598. }
  8599. case SIMD_i8x16_min_s:
  8600. case SIMD_i8x16_min_u:
  8601. case SIMD_i8x16_max_s:
  8602. case SIMD_i8x16_max_u:
  8603. {
  8604. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8605. break;
  8606. }
  8607. case SIMD_f64x2_trunc:
  8608. {
  8609. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8610. break;
  8611. }
  8612. case SIMD_i8x16_avgr_u:
  8613. {
  8614. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8615. break;
  8616. }
  8617. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  8618. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  8619. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  8620. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  8621. /* i16x8 operation */
  8622. case SIMD_i16x8_abs:
  8623. case SIMD_i16x8_neg:
  8624. {
  8625. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8626. break;
  8627. }
  8628. case SIMD_i16x8_q15mulr_sat_s:
  8629. {
  8630. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8631. break;
  8632. }
  8633. case SIMD_i16x8_all_true:
  8634. case SIMD_i16x8_bitmask:
  8635. {
  8636. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8637. break;
  8638. }
  8639. case SIMD_i16x8_narrow_i32x4_s:
  8640. case SIMD_i16x8_narrow_i32x4_u:
  8641. {
  8642. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8643. break;
  8644. }
  8645. case SIMD_i16x8_extend_low_i8x16_s:
  8646. case SIMD_i16x8_extend_high_i8x16_s:
  8647. case SIMD_i16x8_extend_low_i8x16_u:
  8648. case SIMD_i16x8_extend_high_i8x16_u:
  8649. {
  8650. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8651. break;
  8652. }
  8653. case SIMD_i16x8_shl:
  8654. case SIMD_i16x8_shr_s:
  8655. case SIMD_i16x8_shr_u:
  8656. {
  8657. POP_I32();
  8658. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8659. break;
  8660. }
  8661. case SIMD_i16x8_add:
  8662. case SIMD_i16x8_add_sat_s:
  8663. case SIMD_i16x8_add_sat_u:
  8664. case SIMD_i16x8_sub:
  8665. case SIMD_i16x8_sub_sat_s:
  8666. case SIMD_i16x8_sub_sat_u:
  8667. {
  8668. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8669. break;
  8670. }
  8671. case SIMD_f64x2_nearest:
  8672. {
  8673. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8674. break;
  8675. }
  8676. case SIMD_i16x8_mul:
  8677. case SIMD_i16x8_min_s:
  8678. case SIMD_i16x8_min_u:
  8679. case SIMD_i16x8_max_s:
  8680. case SIMD_i16x8_max_u:
  8681. case SIMD_i16x8_avgr_u:
  8682. case SIMD_i16x8_extmul_low_i8x16_s:
  8683. case SIMD_i16x8_extmul_high_i8x16_s:
  8684. case SIMD_i16x8_extmul_low_i8x16_u:
  8685. case SIMD_i16x8_extmul_high_i8x16_u:
  8686. {
  8687. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8688. break;
  8689. }
  8690. /* i32x4 operation */
  8691. case SIMD_i32x4_abs:
  8692. case SIMD_i32x4_neg:
  8693. {
  8694. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8695. break;
  8696. }
  8697. case SIMD_i32x4_all_true:
  8698. case SIMD_i32x4_bitmask:
  8699. {
  8700. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8701. break;
  8702. }
  8703. case SIMD_i32x4_narrow_i64x2_s:
  8704. case SIMD_i32x4_narrow_i64x2_u:
  8705. {
  8706. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8707. break;
  8708. }
  8709. case SIMD_i32x4_extend_low_i16x8_s:
  8710. case SIMD_i32x4_extend_high_i16x8_s:
  8711. case SIMD_i32x4_extend_low_i16x8_u:
  8712. case SIMD_i32x4_extend_high_i16x8_u:
  8713. {
  8714. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8715. break;
  8716. }
  8717. case SIMD_i32x4_shl:
  8718. case SIMD_i32x4_shr_s:
  8719. case SIMD_i32x4_shr_u:
  8720. {
  8721. POP_I32();
  8722. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8723. break;
  8724. }
  8725. case SIMD_i32x4_add:
  8726. case SIMD_i32x4_sub:
  8727. case SIMD_i32x4_mul:
  8728. case SIMD_i32x4_min_s:
  8729. case SIMD_i32x4_min_u:
  8730. case SIMD_i32x4_max_s:
  8731. case SIMD_i32x4_max_u:
  8732. case SIMD_i32x4_dot_i16x8_s:
  8733. case SIMD_i32x4_avgr_u:
  8734. case SIMD_i32x4_extmul_low_i16x8_s:
  8735. case SIMD_i32x4_extmul_high_i16x8_s:
  8736. case SIMD_i32x4_extmul_low_i16x8_u:
  8737. case SIMD_i32x4_extmul_high_i16x8_u:
  8738. {
  8739. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8740. break;
  8741. }
  8742. /* i64x2 operation */
  8743. case SIMD_i64x2_abs:
  8744. case SIMD_i64x2_neg:
  8745. {
  8746. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8747. break;
  8748. }
  8749. case SIMD_i64x2_all_true:
  8750. case SIMD_i64x2_bitmask:
  8751. {
  8752. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8753. break;
  8754. }
  8755. case SIMD_i64x2_extend_low_i32x4_s:
  8756. case SIMD_i64x2_extend_high_i32x4_s:
  8757. case SIMD_i64x2_extend_low_i32x4_u:
  8758. case SIMD_i64x2_extend_high_i32x4_u:
  8759. {
  8760. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8761. break;
  8762. }
  8763. case SIMD_i64x2_shl:
  8764. case SIMD_i64x2_shr_s:
  8765. case SIMD_i64x2_shr_u:
  8766. {
  8767. POP_I32();
  8768. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8769. break;
  8770. }
  8771. case SIMD_i64x2_add:
  8772. case SIMD_i64x2_sub:
  8773. case SIMD_i64x2_mul:
  8774. case SIMD_i64x2_eq:
  8775. case SIMD_i64x2_ne:
  8776. case SIMD_i64x2_lt_s:
  8777. case SIMD_i64x2_gt_s:
  8778. case SIMD_i64x2_le_s:
  8779. case SIMD_i64x2_ge_s:
  8780. case SIMD_i64x2_extmul_low_i32x4_s:
  8781. case SIMD_i64x2_extmul_high_i32x4_s:
  8782. case SIMD_i64x2_extmul_low_i32x4_u:
  8783. case SIMD_i64x2_extmul_high_i32x4_u:
  8784. {
  8785. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8786. break;
  8787. }
  8788. /* f32x4 operation */
  8789. case SIMD_f32x4_abs:
  8790. case SIMD_f32x4_neg:
  8791. case SIMD_f32x4_round:
  8792. case SIMD_f32x4_sqrt:
  8793. {
  8794. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8795. break;
  8796. }
  8797. case SIMD_f32x4_add:
  8798. case SIMD_f32x4_sub:
  8799. case SIMD_f32x4_mul:
  8800. case SIMD_f32x4_div:
  8801. case SIMD_f32x4_min:
  8802. case SIMD_f32x4_max:
  8803. case SIMD_f32x4_pmin:
  8804. case SIMD_f32x4_pmax:
  8805. {
  8806. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8807. break;
  8808. }
  8809. /* f64x2 operation */
  8810. case SIMD_f64x2_abs:
  8811. case SIMD_f64x2_neg:
  8812. case SIMD_f64x2_round:
  8813. case SIMD_f64x2_sqrt:
  8814. {
  8815. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8816. break;
  8817. }
  8818. case SIMD_f64x2_add:
  8819. case SIMD_f64x2_sub:
  8820. case SIMD_f64x2_mul:
  8821. case SIMD_f64x2_div:
  8822. case SIMD_f64x2_min:
  8823. case SIMD_f64x2_max:
  8824. case SIMD_f64x2_pmin:
  8825. case SIMD_f64x2_pmax:
  8826. {
  8827. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8828. break;
  8829. }
  8830. case SIMD_i32x4_trunc_sat_f32x4_s:
  8831. case SIMD_i32x4_trunc_sat_f32x4_u:
  8832. case SIMD_f32x4_convert_i32x4_s:
  8833. case SIMD_f32x4_convert_i32x4_u:
  8834. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  8835. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  8836. case SIMD_f64x2_convert_low_i32x4_s:
  8837. case SIMD_f64x2_convert_low_i32x4_u:
  8838. {
  8839. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8840. break;
  8841. }
  8842. default:
  8843. {
  8844. if (error_buf != NULL) {
  8845. snprintf(error_buf, error_buf_size,
  8846. "WASM module load failed: "
  8847. "invalid opcode 0xfd %02x.",
  8848. opcode);
  8849. }
  8850. goto fail;
  8851. }
  8852. }
  8853. break;
  8854. }
  8855. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8856. #endif /* end of WASM_ENABLE_SIMD */
  8857. #if WASM_ENABLE_SHARED_MEMORY != 0
  8858. case WASM_OP_ATOMIC_PREFIX:
  8859. {
  8860. opcode = read_uint8(p);
  8861. #if WASM_ENABLE_FAST_INTERP != 0
  8862. emit_byte(loader_ctx, opcode);
  8863. #endif
  8864. if (opcode != WASM_OP_ATOMIC_FENCE) {
  8865. CHECK_MEMORY();
  8866. read_leb_uint32(p, p_end, align); /* align */
  8867. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8868. if (!check_memory_align_equal(opcode, align, error_buf,
  8869. error_buf_size)) {
  8870. goto fail;
  8871. }
  8872. #if WASM_ENABLE_FAST_INTERP != 0
  8873. emit_uint32(loader_ctx, mem_offset);
  8874. #endif
  8875. }
  8876. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8877. func->has_memory_operations = true;
  8878. #endif
  8879. switch (opcode) {
  8880. case WASM_OP_ATOMIC_NOTIFY:
  8881. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8882. break;
  8883. case WASM_OP_ATOMIC_WAIT32:
  8884. POP_I64();
  8885. POP_I32();
  8886. POP_I32();
  8887. PUSH_I32();
  8888. break;
  8889. case WASM_OP_ATOMIC_WAIT64:
  8890. POP_I64();
  8891. POP_I64();
  8892. POP_I32();
  8893. PUSH_I32();
  8894. break;
  8895. case WASM_OP_ATOMIC_FENCE:
  8896. /* reserved byte 0x00 */
  8897. if (*p++ != 0x00) {
  8898. set_error_buf(error_buf, error_buf_size,
  8899. "zero byte expected");
  8900. goto fail;
  8901. }
  8902. break;
  8903. case WASM_OP_ATOMIC_I32_LOAD:
  8904. case WASM_OP_ATOMIC_I32_LOAD8_U:
  8905. case WASM_OP_ATOMIC_I32_LOAD16_U:
  8906. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8907. break;
  8908. case WASM_OP_ATOMIC_I32_STORE:
  8909. case WASM_OP_ATOMIC_I32_STORE8:
  8910. case WASM_OP_ATOMIC_I32_STORE16:
  8911. POP_I32();
  8912. POP_I32();
  8913. break;
  8914. case WASM_OP_ATOMIC_I64_LOAD:
  8915. case WASM_OP_ATOMIC_I64_LOAD8_U:
  8916. case WASM_OP_ATOMIC_I64_LOAD16_U:
  8917. case WASM_OP_ATOMIC_I64_LOAD32_U:
  8918. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  8919. break;
  8920. case WASM_OP_ATOMIC_I64_STORE:
  8921. case WASM_OP_ATOMIC_I64_STORE8:
  8922. case WASM_OP_ATOMIC_I64_STORE16:
  8923. case WASM_OP_ATOMIC_I64_STORE32:
  8924. POP_I64();
  8925. POP_I32();
  8926. break;
  8927. case WASM_OP_ATOMIC_RMW_I32_ADD:
  8928. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  8929. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  8930. case WASM_OP_ATOMIC_RMW_I32_SUB:
  8931. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  8932. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  8933. case WASM_OP_ATOMIC_RMW_I32_AND:
  8934. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  8935. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  8936. case WASM_OP_ATOMIC_RMW_I32_OR:
  8937. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  8938. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  8939. case WASM_OP_ATOMIC_RMW_I32_XOR:
  8940. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  8941. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  8942. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  8943. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  8944. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  8945. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8946. break;
  8947. case WASM_OP_ATOMIC_RMW_I64_ADD:
  8948. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  8949. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  8950. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  8951. case WASM_OP_ATOMIC_RMW_I64_SUB:
  8952. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  8953. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  8954. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  8955. case WASM_OP_ATOMIC_RMW_I64_AND:
  8956. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  8957. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  8958. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  8959. case WASM_OP_ATOMIC_RMW_I64_OR:
  8960. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  8961. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  8962. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  8963. case WASM_OP_ATOMIC_RMW_I64_XOR:
  8964. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  8965. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  8966. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  8967. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  8968. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  8969. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  8970. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  8971. POP_I64();
  8972. POP_I32();
  8973. PUSH_I64();
  8974. break;
  8975. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  8976. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  8977. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  8978. POP_I32();
  8979. POP_I32();
  8980. POP_I32();
  8981. PUSH_I32();
  8982. break;
  8983. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  8984. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  8985. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  8986. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  8987. POP_I64();
  8988. POP_I64();
  8989. POP_I32();
  8990. PUSH_I64();
  8991. break;
  8992. default:
  8993. set_error_buf_v(error_buf, error_buf_size,
  8994. "%s %02x %02x", "unsupported opcode",
  8995. 0xfe, opcode);
  8996. goto fail;
  8997. }
  8998. break;
  8999. }
  9000. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9001. default:
  9002. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  9003. "unsupported opcode", opcode);
  9004. goto fail;
  9005. }
  9006. #if WASM_ENABLE_FAST_INTERP != 0
  9007. last_op = opcode;
  9008. #endif
  9009. }
  9010. if (loader_ctx->csp_num > 0) {
  9011. if (cur_func_idx < module->function_count - 1)
  9012. /* Function with missing end marker (between two functions) */
  9013. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  9014. else
  9015. /* Function with missing end marker
  9016. (at EOF or end of code sections) */
  9017. set_error_buf(error_buf, error_buf_size,
  9018. "unexpected end of section or function, "
  9019. "or section size mismatch");
  9020. goto fail;
  9021. }
  9022. #if WASM_ENABLE_FAST_INTERP != 0
  9023. if (loader_ctx->p_code_compiled == NULL)
  9024. goto re_scan;
  9025. func->const_cell_num = loader_ctx->const_cell_num;
  9026. if (func->const_cell_num > 0) {
  9027. int32 j;
  9028. if (!(func->consts = func_const = loader_malloc(
  9029. func->const_cell_num * 4, error_buf, error_buf_size)))
  9030. goto fail;
  9031. func_const_end = func->consts + func->const_cell_num * 4;
  9032. /* reverse the const buf */
  9033. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  9034. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  9035. if (c->value_type == VALUE_TYPE_F64
  9036. || c->value_type == VALUE_TYPE_I64) {
  9037. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  9038. &(c->value.f64), (uint32)sizeof(int64));
  9039. func_const += sizeof(int64);
  9040. }
  9041. else {
  9042. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  9043. &(c->value.f32), (uint32)sizeof(int32));
  9044. func_const += sizeof(int32);
  9045. }
  9046. }
  9047. }
  9048. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  9049. - loader_ctx->start_dynamic_offset + 1;
  9050. #else
  9051. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  9052. #endif
  9053. func->max_block_num = loader_ctx->max_csp_num;
  9054. return_value = true;
  9055. fail:
  9056. wasm_loader_ctx_destroy(loader_ctx);
  9057. (void)table_idx;
  9058. (void)table_seg_idx;
  9059. (void)data_seg_idx;
  9060. (void)i64_const;
  9061. (void)local_offset;
  9062. (void)p_org;
  9063. (void)mem_offset;
  9064. (void)align;
  9065. return return_value;
  9066. }