wasm_runtime_common.c 206 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "bh_common.h"
  7. #include "bh_assert.h"
  8. #include "bh_log.h"
  9. #include "wasm_native.h"
  10. #include "wasm_runtime_common.h"
  11. #include "wasm_memory.h"
  12. #if WASM_ENABLE_INTERP != 0
  13. #include "../interpreter/wasm_runtime.h"
  14. #endif
  15. #if WASM_ENABLE_AOT != 0
  16. #include "../aot/aot_runtime.h"
  17. #if WASM_ENABLE_DEBUG_AOT != 0
  18. #include "../aot/debug/jit_debug.h"
  19. #endif
  20. #endif
  21. #if WASM_ENABLE_GC != 0
  22. #include "gc/gc_object.h"
  23. #endif
  24. #if WASM_ENABLE_THREAD_MGR != 0
  25. #include "../libraries/thread-mgr/thread_manager.h"
  26. #if WASM_ENABLE_DEBUG_INTERP != 0
  27. #include "../libraries/debug-engine/debug_engine.h"
  28. #endif
  29. #endif
  30. #if WASM_ENABLE_SHARED_MEMORY != 0
  31. #include "wasm_shared_memory.h"
  32. #endif
  33. #if WASM_ENABLE_FAST_JIT != 0
  34. #include "../fast-jit/jit_compiler.h"
  35. #endif
  36. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  37. #include "../compilation/aot_llvm.h"
  38. #endif
  39. #include "../common/wasm_c_api_internal.h"
  40. #include "../../version.h"
  41. /**
  42. * For runtime build, BH_MALLOC/BH_FREE should be defined as
  43. * wasm_runtime_malloc/wasm_runtime_free.
  44. */
  45. #define CHECK(a) CHECK1(a)
  46. #define CHECK1(a) SHOULD_BE_##a
  47. #define SHOULD_BE_wasm_runtime_malloc 1
  48. #if !CHECK(BH_MALLOC)
  49. #error unexpected BH_MALLOC
  50. #endif
  51. #undef SHOULD_BE_wasm_runtime_malloc
  52. #define SHOULD_BE_wasm_runtime_free 1
  53. #if !CHECK(BH_FREE)
  54. #error unexpected BH_FREE
  55. #endif
  56. #undef SHOULD_BE_wasm_runtime_free
  57. #undef CHECK
  58. #undef CHECK1
  59. #if WASM_ENABLE_MULTI_MODULE != 0
  60. /**
  61. * A safety insurance to prevent
  62. * circular depencies which leads stack overflow
  63. * try to break early
  64. */
  65. typedef struct LoadingModule {
  66. bh_list_link l;
  67. /* point to a string pool */
  68. const char *module_name;
  69. } LoadingModule;
  70. static bh_list loading_module_list_head;
  71. static bh_list *const loading_module_list = &loading_module_list_head;
  72. static korp_mutex loading_module_list_lock;
  73. /**
  74. * A list to store all exported functions/globals/memories/tables
  75. * of every fully loaded module
  76. */
  77. static bh_list registered_module_list_head;
  78. static bh_list *const registered_module_list = &registered_module_list_head;
  79. static korp_mutex registered_module_list_lock;
  80. static void
  81. wasm_runtime_destroy_registered_module_list();
  82. #endif /* WASM_ENABLE_MULTI_MODULE */
  83. #define E_TYPE_XIP 4
  84. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  85. /* Initialize externref hashmap */
  86. static bool
  87. wasm_externref_map_init();
  88. /* Destroy externref hashmap */
  89. static void
  90. wasm_externref_map_destroy();
  91. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  92. static void
  93. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  94. {
  95. if (error_buf != NULL)
  96. snprintf(error_buf, error_buf_size, "%s", string);
  97. }
  98. static void *
  99. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  100. char *error_buf, uint32 error_buf_size)
  101. {
  102. void *mem;
  103. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  104. if (module_inst != NULL) {
  105. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  106. }
  107. else if (error_buf != NULL) {
  108. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  109. }
  110. return NULL;
  111. }
  112. memset(mem, 0, (uint32)size);
  113. return mem;
  114. }
  115. #if WASM_ENABLE_MULTI_MODULE != 0
  116. /* TODO: Let loader_malloc be a general API both for AOT and WASM. */
  117. #define loader_malloc(size, error_buf, error_buf_size) \
  118. runtime_malloc(size, NULL, error_buf, error_buf_size)
  119. static void
  120. set_error_buf_v(const WASMModuleCommon *module, char *error_buf,
  121. uint32 error_buf_size, const char *format, ...)
  122. {
  123. va_list args;
  124. char buf[128];
  125. if (error_buf != NULL) {
  126. va_start(args, format);
  127. vsnprintf(buf, sizeof(buf), format, args);
  128. va_end(args);
  129. if (module->module_type == Wasm_Module_AoT) {
  130. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  131. buf);
  132. }
  133. else if (module->module_type == Wasm_Module_Bytecode) {
  134. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  135. buf);
  136. }
  137. }
  138. }
  139. #endif
  140. #if WASM_ENABLE_FAST_JIT != 0
  141. static JitCompOptions jit_options = { 0 };
  142. #endif
  143. #if WASM_ENABLE_JIT != 0
  144. /* opt_level: 3, size_level: 3, segue-flags: 0,
  145. quick_invoke_c_api_import: false */
  146. static LLVMJITOptions llvm_jit_options = { 3, 3, 0, false };
  147. #endif
  148. #if WASM_ENABLE_GC != 0
  149. static uint32 gc_heap_size_default = GC_HEAP_SIZE_DEFAULT;
  150. #endif
  151. static RunningMode runtime_running_mode = Mode_Default;
  152. #ifdef OS_ENABLE_HW_BOUND_CHECK
  153. /* The exec_env of thread local storage, set before calling function
  154. and used in signal handler, as we cannot get it from the argument
  155. of signal handler */
  156. static os_thread_local_attribute WASMExecEnv *exec_env_tls = NULL;
  157. #ifndef BH_PLATFORM_WINDOWS
  158. static void
  159. runtime_signal_handler(void *sig_addr)
  160. {
  161. WASMModuleInstance *module_inst;
  162. WASMMemoryInstance *memory_inst;
  163. WASMJmpBuf *jmpbuf_node;
  164. uint8 *mapped_mem_start_addr = NULL;
  165. uint8 *mapped_mem_end_addr = NULL;
  166. uint32 page_size = os_getpagesize();
  167. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  168. uint8 *stack_min_addr;
  169. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  170. #endif
  171. /* Check whether current thread is running wasm function */
  172. if (exec_env_tls && exec_env_tls->handle == os_self_thread()
  173. && (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
  174. /* Get mapped mem info of current instance */
  175. module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
  176. /* Get the default memory instance */
  177. memory_inst = wasm_get_default_memory(module_inst);
  178. if (memory_inst) {
  179. mapped_mem_start_addr = memory_inst->memory_data;
  180. mapped_mem_end_addr = memory_inst->memory_data + 8 * (uint64)BH_GB;
  181. }
  182. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  183. /* Get stack info of current thread */
  184. stack_min_addr = os_thread_get_stack_boundary();
  185. #endif
  186. if (memory_inst
  187. && (mapped_mem_start_addr <= (uint8 *)sig_addr
  188. && (uint8 *)sig_addr < mapped_mem_end_addr)) {
  189. /* The address which causes segmentation fault is inside
  190. the memory instance's guard regions */
  191. wasm_set_exception(module_inst, "out of bounds memory access");
  192. os_longjmp(jmpbuf_node->jmpbuf, 1);
  193. }
  194. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  195. else if (stack_min_addr - page_size <= (uint8 *)sig_addr
  196. && (uint8 *)sig_addr
  197. < stack_min_addr + page_size * guard_page_count) {
  198. /* The address which causes segmentation fault is inside
  199. native thread's guard page */
  200. wasm_set_exception(module_inst, "native stack overflow");
  201. os_longjmp(jmpbuf_node->jmpbuf, 1);
  202. }
  203. #endif
  204. else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
  205. && (uint8 *)sig_addr
  206. < exec_env_tls->exce_check_guard_page + page_size) {
  207. bh_assert(wasm_copy_exception(module_inst, NULL));
  208. os_longjmp(jmpbuf_node->jmpbuf, 1);
  209. }
  210. }
  211. }
  212. #else /* else of BH_PLATFORM_WINDOWS */
  213. #if WASM_ENABLE_AOT != 0
  214. #include <Zydis/Zydis.h>
  215. static uint32
  216. decode_insn(uint8 *insn)
  217. {
  218. uint8 *data = (uint8 *)insn;
  219. uint32 length = 32; /* reserve enough size */
  220. /* Initialize decoder context */
  221. ZydisDecoder decoder;
  222. ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64,
  223. ZYDIS_STACK_WIDTH_64);
  224. /* Initialize formatter */
  225. ZydisFormatter formatter;
  226. ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
  227. /* Loop over the instructions in our buffer */
  228. ZyanU64 runtime_address = (ZyanU64)(uintptr_t)data;
  229. ZyanUSize offset = 0;
  230. ZydisDecodedInstruction instruction;
  231. ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
  232. char buffer[256];
  233. if (ZYAN_SUCCESS(ZydisDecoderDecodeFull(
  234. &decoder, data + offset, length - offset, &instruction, operands,
  235. ZYDIS_MAX_OPERAND_COUNT_VISIBLE,
  236. ZYDIS_DFLAG_VISIBLE_OPERANDS_ONLY))) {
  237. /* Format & print the binary instruction structure to
  238. human readable format */
  239. ZydisFormatterFormatInstruction(&formatter, &instruction, operands,
  240. instruction.operand_count_visible,
  241. buffer, sizeof(buffer),
  242. runtime_address);
  243. /* Print current instruction */
  244. /*
  245. os_printf("%012" PRIX64 " ", runtime_address);
  246. puts(buffer);
  247. */
  248. return instruction.length;
  249. }
  250. /* Decode failed */
  251. return 0;
  252. }
  253. #endif /* end of WASM_ENABLE_AOT != 0 */
  254. static LONG
  255. next_action(WASMModuleInstance *module_inst, EXCEPTION_POINTERS *exce_info)
  256. {
  257. #if WASM_ENABLE_AOT != 0
  258. uint32 insn_size;
  259. #endif
  260. if (module_inst->module_type == Wasm_Module_Bytecode
  261. && module_inst->e->running_mode == Mode_Interp) {
  262. /* Continue to search next exception handler for
  263. interpreter mode as it can be caught by
  264. `__try { .. } __except { .. }` sentences in
  265. wasm_runtime.c */
  266. return EXCEPTION_CONTINUE_SEARCH;
  267. }
  268. #if WASM_ENABLE_AOT != 0
  269. /* Skip current instruction and continue to run for AOT/JIT mode.
  270. TODO: implement unwind support for AOT/JIT code in Windows platform */
  271. insn_size = decode_insn((uint8 *)exce_info->ContextRecord->Rip);
  272. if (insn_size > 0) {
  273. exce_info->ContextRecord->Rip += insn_size;
  274. return EXCEPTION_CONTINUE_EXECUTION;
  275. }
  276. #endif
  277. /* return different value from EXCEPTION_CONTINUE_SEARCH (= 0)
  278. and EXCEPTION_CONTINUE_EXECUTION (= -1) */
  279. return -2;
  280. }
  281. static LONG
  282. runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
  283. {
  284. PEXCEPTION_RECORD ExceptionRecord = exce_info->ExceptionRecord;
  285. uint8 *sig_addr = (uint8 *)ExceptionRecord->ExceptionInformation[1];
  286. WASMModuleInstance *module_inst;
  287. WASMMemoryInstance *memory_inst;
  288. WASMJmpBuf *jmpbuf_node;
  289. uint8 *mapped_mem_start_addr = NULL;
  290. uint8 *mapped_mem_end_addr = NULL;
  291. uint32 page_size = os_getpagesize();
  292. LONG ret;
  293. if (exec_env_tls && exec_env_tls->handle == os_self_thread()
  294. && (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
  295. module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
  296. if (ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
  297. /* Get the default memory instance */
  298. memory_inst = wasm_get_default_memory(module_inst);
  299. if (memory_inst) {
  300. mapped_mem_start_addr = memory_inst->memory_data;
  301. mapped_mem_end_addr =
  302. memory_inst->memory_data + 8 * (uint64)BH_GB;
  303. }
  304. if (memory_inst && mapped_mem_start_addr <= (uint8 *)sig_addr
  305. && (uint8 *)sig_addr < mapped_mem_end_addr) {
  306. /* The address which causes segmentation fault is inside
  307. the memory instance's guard regions.
  308. Set exception and let the wasm func continue to run, when
  309. the wasm func returns, the caller will check whether the
  310. exception is thrown and return to runtime. */
  311. wasm_set_exception(module_inst, "out of bounds memory access");
  312. ret = next_action(module_inst, exce_info);
  313. if (ret == EXCEPTION_CONTINUE_SEARCH
  314. || ret == EXCEPTION_CONTINUE_EXECUTION)
  315. return ret;
  316. }
  317. else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
  318. && (uint8 *)sig_addr
  319. < exec_env_tls->exce_check_guard_page + page_size) {
  320. bh_assert(wasm_copy_exception(module_inst, NULL));
  321. ret = next_action(module_inst, exce_info);
  322. if (ret == EXCEPTION_CONTINUE_SEARCH
  323. || ret == EXCEPTION_CONTINUE_EXECUTION)
  324. return ret;
  325. }
  326. }
  327. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  328. else if (ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
  329. /* Set stack overflow exception and let the wasm func continue
  330. to run, when the wasm func returns, the caller will check
  331. whether the exception is thrown and return to runtime, and
  332. the damaged stack will be recovered by _resetstkoflw(). */
  333. wasm_set_exception(module_inst, "native stack overflow");
  334. ret = next_action(module_inst, exce_info);
  335. if (ret == EXCEPTION_CONTINUE_SEARCH
  336. || ret == EXCEPTION_CONTINUE_EXECUTION)
  337. return ret;
  338. }
  339. #endif
  340. }
  341. LOG_ERROR("Unhandled exception thrown: exception code: 0x%lx, "
  342. "exception address: %p, exception information: %p\n",
  343. ExceptionRecord->ExceptionCode, ExceptionRecord->ExceptionAddress,
  344. sig_addr);
  345. return EXCEPTION_CONTINUE_SEARCH;
  346. }
  347. #endif /* end of BH_PLATFORM_WINDOWS */
  348. static bool
  349. runtime_signal_init()
  350. {
  351. #ifndef BH_PLATFORM_WINDOWS
  352. return os_thread_signal_init(runtime_signal_handler) == 0 ? true : false;
  353. #else
  354. if (os_thread_signal_init() != 0)
  355. return false;
  356. if (!AddVectoredExceptionHandler(1, runtime_exception_handler)) {
  357. os_thread_signal_destroy();
  358. return false;
  359. }
  360. #endif
  361. return true;
  362. }
  363. static void
  364. runtime_signal_destroy()
  365. {
  366. #ifdef BH_PLATFORM_WINDOWS
  367. RemoveVectoredExceptionHandler(runtime_exception_handler);
  368. #endif
  369. os_thread_signal_destroy();
  370. }
  371. void
  372. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env)
  373. {
  374. exec_env_tls = exec_env;
  375. }
  376. WASMExecEnv *
  377. wasm_runtime_get_exec_env_tls()
  378. {
  379. return exec_env_tls;
  380. }
  381. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  382. static bool
  383. wasm_runtime_env_init()
  384. {
  385. if (bh_platform_init() != 0)
  386. return false;
  387. if (wasm_native_init() == false) {
  388. goto fail1;
  389. }
  390. #if WASM_ENABLE_MULTI_MODULE
  391. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  392. goto fail2;
  393. }
  394. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  395. goto fail3;
  396. }
  397. #endif
  398. #if WASM_ENABLE_SHARED_MEMORY
  399. if (!wasm_shared_memory_init()) {
  400. goto fail4;
  401. }
  402. #endif
  403. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  404. if (!thread_manager_init()) {
  405. goto fail5;
  406. }
  407. #endif
  408. #ifdef OS_ENABLE_HW_BOUND_CHECK
  409. if (!runtime_signal_init()) {
  410. goto fail6;
  411. }
  412. #endif
  413. #if WASM_ENABLE_AOT != 0
  414. #if WASM_ENABLE_DEBUG_AOT != 0
  415. if (!jit_debug_engine_init()) {
  416. goto fail7;
  417. }
  418. #endif
  419. #endif
  420. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  421. if (!wasm_externref_map_init()) {
  422. goto fail8;
  423. }
  424. #endif
  425. #if WASM_ENABLE_FAST_JIT != 0
  426. if (!jit_compiler_init(&jit_options)) {
  427. goto fail9;
  428. }
  429. #endif
  430. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  431. if (!aot_compiler_init()) {
  432. goto fail10;
  433. }
  434. #endif
  435. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  436. if (os_blocking_op_init() != BHT_OK) {
  437. goto fail11;
  438. }
  439. os_end_blocking_op();
  440. #endif
  441. return true;
  442. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  443. fail11:
  444. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  445. aot_compiler_destroy();
  446. #endif
  447. #endif
  448. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  449. fail10:
  450. #if WASM_ENABLE_FAST_JIT != 0
  451. jit_compiler_destroy();
  452. #endif
  453. #endif
  454. #if WASM_ENABLE_FAST_JIT != 0
  455. fail9:
  456. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  457. wasm_externref_map_destroy();
  458. #endif
  459. #endif
  460. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  461. fail8:
  462. #endif
  463. #if WASM_ENABLE_AOT != 0
  464. #if WASM_ENABLE_DEBUG_AOT != 0
  465. jit_debug_engine_destroy();
  466. fail7:
  467. #endif
  468. #endif
  469. #ifdef OS_ENABLE_HW_BOUND_CHECK
  470. runtime_signal_destroy();
  471. fail6:
  472. #endif
  473. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  474. thread_manager_destroy();
  475. fail5:
  476. #endif
  477. #if WASM_ENABLE_SHARED_MEMORY
  478. wasm_shared_memory_destroy();
  479. fail4:
  480. #endif
  481. #if WASM_ENABLE_MULTI_MODULE
  482. os_mutex_destroy(&loading_module_list_lock);
  483. fail3:
  484. os_mutex_destroy(&registered_module_list_lock);
  485. fail2:
  486. #endif
  487. wasm_native_destroy();
  488. fail1:
  489. bh_platform_destroy();
  490. return false;
  491. }
  492. static bool
  493. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  494. {
  495. return exec_env && exec_env->module_inst && exec_env->wasm_stack_size > 0
  496. && exec_env->wasm_stack.top_boundary
  497. == exec_env->wasm_stack.bottom + exec_env->wasm_stack_size
  498. && exec_env->wasm_stack.top <= exec_env->wasm_stack.top_boundary;
  499. }
  500. bool
  501. wasm_runtime_init()
  502. {
  503. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  504. return false;
  505. if (!wasm_runtime_env_init()) {
  506. wasm_runtime_memory_destroy();
  507. return false;
  508. }
  509. return true;
  510. }
  511. void
  512. wasm_runtime_destroy()
  513. {
  514. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  515. wasm_externref_map_destroy();
  516. #endif
  517. #if WASM_ENABLE_AOT != 0
  518. #if WASM_ENABLE_DEBUG_AOT != 0
  519. jit_debug_engine_destroy();
  520. #endif
  521. #endif
  522. #ifdef OS_ENABLE_HW_BOUND_CHECK
  523. runtime_signal_destroy();
  524. #endif
  525. /* runtime env destroy */
  526. #if WASM_ENABLE_MULTI_MODULE
  527. wasm_runtime_destroy_loading_module_list();
  528. os_mutex_destroy(&loading_module_list_lock);
  529. wasm_runtime_destroy_registered_module_list();
  530. os_mutex_destroy(&registered_module_list_lock);
  531. #endif
  532. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  533. /* Destroy LLVM-JIT compiler after destroying the modules
  534. * loaded by multi-module feature, since these modules may
  535. * create backend threads to compile the wasm functions,
  536. * which may access the LLVM resources. We wait until they
  537. * finish the compilation to avoid accessing the destroyed
  538. * resources in the compilation threads.
  539. */
  540. aot_compiler_destroy();
  541. #endif
  542. #if WASM_ENABLE_FAST_JIT != 0
  543. /* Destroy Fast-JIT compiler after destroying the modules
  544. * loaded by multi-module feature, since the Fast JIT's
  545. * code cache allocator may be used by these modules.
  546. */
  547. jit_compiler_destroy();
  548. #endif
  549. #if WASM_ENABLE_SHARED_MEMORY
  550. wasm_shared_memory_destroy();
  551. #endif
  552. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  553. #if WASM_ENABLE_DEBUG_INTERP != 0
  554. wasm_debug_engine_destroy();
  555. #endif
  556. thread_manager_destroy();
  557. #endif
  558. wasm_native_destroy();
  559. bh_platform_destroy();
  560. wasm_runtime_memory_destroy();
  561. }
  562. RunningMode
  563. wasm_runtime_get_default_running_mode(void)
  564. {
  565. return runtime_running_mode;
  566. }
  567. #if WASM_ENABLE_JIT != 0
  568. LLVMJITOptions *
  569. wasm_runtime_get_llvm_jit_options(void)
  570. {
  571. return &llvm_jit_options;
  572. }
  573. #endif
  574. #if WASM_ENABLE_GC != 0
  575. uint32
  576. wasm_runtime_get_gc_heap_size_default(void)
  577. {
  578. return gc_heap_size_default;
  579. }
  580. #endif
  581. bool
  582. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  583. {
  584. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  585. &init_args->mem_alloc_option))
  586. return false;
  587. if (!wasm_runtime_set_default_running_mode(init_args->running_mode)) {
  588. wasm_runtime_memory_destroy();
  589. return false;
  590. }
  591. #if WASM_ENABLE_FAST_JIT != 0
  592. jit_options.code_cache_size = init_args->fast_jit_code_cache_size;
  593. #endif
  594. #if WASM_ENABLE_GC != 0
  595. gc_heap_size_default = init_args->gc_heap_size;
  596. #endif
  597. #if WASM_ENABLE_JIT != 0
  598. llvm_jit_options.size_level = init_args->llvm_jit_size_level;
  599. llvm_jit_options.opt_level = init_args->llvm_jit_opt_level;
  600. llvm_jit_options.segue_flags = init_args->segue_flags;
  601. #endif
  602. #if WASM_ENABLE_LINUX_PERF != 0
  603. wasm_runtime_set_linux_perf(init_args->enable_linux_perf);
  604. #else
  605. if (init_args->enable_linux_perf)
  606. LOG_WARNING("warning: to enable linux perf support, please recompile "
  607. "with -DWAMR_BUILD_LINUX_PERF=1");
  608. #endif
  609. if (!wasm_runtime_env_init()) {
  610. wasm_runtime_memory_destroy();
  611. return false;
  612. }
  613. #if WASM_ENABLE_DEBUG_INTERP != 0
  614. if (strlen(init_args->ip_addr))
  615. if (!wasm_debug_engine_init(init_args->ip_addr,
  616. init_args->instance_port)) {
  617. wasm_runtime_destroy();
  618. return false;
  619. }
  620. #endif
  621. if (init_args->n_native_symbols > 0
  622. && !wasm_runtime_register_natives(init_args->native_module_name,
  623. init_args->native_symbols,
  624. init_args->n_native_symbols)) {
  625. wasm_runtime_destroy();
  626. return false;
  627. }
  628. #if WASM_ENABLE_THREAD_MGR != 0
  629. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  630. #endif
  631. return true;
  632. }
  633. void
  634. wasm_runtime_set_log_level(log_level_t level)
  635. {
  636. bh_log_set_verbose_level(level);
  637. }
  638. bool
  639. wasm_runtime_is_running_mode_supported(RunningMode running_mode)
  640. {
  641. if (running_mode == Mode_Default) {
  642. return true;
  643. }
  644. else if (running_mode == Mode_Interp) {
  645. #if WASM_ENABLE_INTERP != 0
  646. return true;
  647. #endif
  648. }
  649. else if (running_mode == Mode_Fast_JIT) {
  650. #if WASM_ENABLE_FAST_JIT != 0
  651. return true;
  652. #endif
  653. }
  654. else if (running_mode == Mode_LLVM_JIT) {
  655. #if WASM_ENABLE_JIT != 0
  656. return true;
  657. #endif
  658. }
  659. else if (running_mode == Mode_Multi_Tier_JIT) {
  660. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  661. && WASM_ENABLE_LAZY_JIT != 0
  662. return true;
  663. #endif
  664. }
  665. return false;
  666. }
  667. bool
  668. wasm_runtime_set_default_running_mode(RunningMode running_mode)
  669. {
  670. if (wasm_runtime_is_running_mode_supported(running_mode)) {
  671. runtime_running_mode = running_mode;
  672. return true;
  673. }
  674. return false;
  675. }
  676. PackageType
  677. get_package_type(const uint8 *buf, uint32 size)
  678. {
  679. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  680. uint32 buf32 = *(uint32 *)buf;
  681. buf = (const uint8 *)&buf32;
  682. #endif
  683. if (buf && size >= 4) {
  684. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  685. return Wasm_Module_Bytecode;
  686. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  687. return Wasm_Module_AoT;
  688. }
  689. return Package_Type_Unknown;
  690. }
  691. #if WASM_ENABLE_AOT != 0
  692. static uint8 *
  693. align_ptr(const uint8 *p, uint32 b)
  694. {
  695. uintptr_t v = (uintptr_t)p;
  696. uintptr_t m = b - 1;
  697. return (uint8 *)((v + m) & ~m);
  698. }
  699. #define CHECK_BUF(buf, buf_end, length) \
  700. do { \
  701. if ((uintptr_t)buf + length < (uintptr_t)buf \
  702. || (uintptr_t)buf + length > (uintptr_t)buf_end) \
  703. return false; \
  704. } while (0)
  705. /* NOLINTNEXTLINE */
  706. #define read_uint16(p, p_end, res) \
  707. do { \
  708. p = (uint8 *)align_ptr(p, sizeof(uint16)); \
  709. CHECK_BUF(p, p_end, sizeof(uint16)); \
  710. res = *(uint16 *)p; \
  711. p += sizeof(uint16); \
  712. } while (0)
  713. /* NOLINTNEXTLINE */
  714. #define read_uint32(p, p_end, res) \
  715. do { \
  716. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  717. CHECK_BUF(p, p_end, sizeof(uint32)); \
  718. res = *(uint32 *)p; \
  719. p += sizeof(uint32); \
  720. } while (0)
  721. bool
  722. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
  723. {
  724. const uint8 *p = buf, *p_end = buf + size;
  725. uint32 section_type, section_size;
  726. uint16 e_type;
  727. if (get_package_type(buf, size) != Wasm_Module_AoT)
  728. return false;
  729. CHECK_BUF(p, p_end, 8);
  730. p += 8;
  731. while (p < p_end) {
  732. read_uint32(p, p_end, section_type);
  733. read_uint32(p, p_end, section_size);
  734. CHECK_BUF(p, p_end, section_size);
  735. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  736. p += 4;
  737. read_uint16(p, p_end, e_type);
  738. return (e_type == E_TYPE_XIP) ? true : false;
  739. }
  740. else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
  741. return false;
  742. }
  743. p += section_size;
  744. }
  745. return false;
  746. }
  747. #endif /* end of WASM_ENABLE_AOT */
  748. #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
  749. uint32
  750. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
  751. {
  752. WASMModuleInstanceCommon *module_inst =
  753. wasm_runtime_get_module_inst(exec_env);
  754. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  755. bh_assert(module_inst);
  756. bh_assert(cluster);
  757. if (module_inst->module_type != Wasm_Module_Bytecode) {
  758. LOG_WARNING("Attempt to create a debug instance for an AOT module");
  759. return 0;
  760. }
  761. if (cluster->debug_inst) {
  762. LOG_WARNING("Cluster already bind to a debug instance");
  763. return cluster->debug_inst->control_thread->port;
  764. }
  765. if (wasm_debug_instance_create(cluster, port)) {
  766. return cluster->debug_inst->control_thread->port;
  767. }
  768. return 0;
  769. }
  770. uint32
  771. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
  772. {
  773. return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
  774. }
  775. #endif
  776. #if WASM_ENABLE_MULTI_MODULE != 0
  777. static module_reader reader;
  778. static module_destroyer destroyer;
  779. void
  780. wasm_runtime_set_module_reader(const module_reader reader_cb,
  781. const module_destroyer destroyer_cb)
  782. {
  783. reader = reader_cb;
  784. destroyer = destroyer_cb;
  785. }
  786. module_reader
  787. wasm_runtime_get_module_reader()
  788. {
  789. return reader;
  790. }
  791. module_destroyer
  792. wasm_runtime_get_module_destroyer()
  793. {
  794. return destroyer;
  795. }
  796. static WASMRegisteredModule *
  797. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  798. {
  799. WASMRegisteredModule *reg_module = NULL;
  800. os_mutex_lock(&registered_module_list_lock);
  801. reg_module = bh_list_first_elem(registered_module_list);
  802. while (reg_module && module != reg_module->module) {
  803. reg_module = bh_list_elem_next(reg_module);
  804. }
  805. os_mutex_unlock(&registered_module_list_lock);
  806. return reg_module;
  807. }
  808. bool
  809. wasm_runtime_register_module_internal(const char *module_name,
  810. WASMModuleCommon *module,
  811. uint8 *orig_file_buf,
  812. uint32 orig_file_buf_size,
  813. char *error_buf, uint32 error_buf_size)
  814. {
  815. WASMRegisteredModule *node = NULL;
  816. node = wasm_runtime_find_module_registered_by_reference(module);
  817. if (node) { /* module has been registered */
  818. if (node->module_name) { /* module has name */
  819. if (!module_name || strcmp(node->module_name, module_name)) {
  820. /* module has different name */
  821. LOG_DEBUG("module(%p) has been registered with name %s", module,
  822. node->module_name);
  823. set_error_buf(error_buf, error_buf_size,
  824. "Register module failed: "
  825. "failed to rename the module");
  826. return false;
  827. }
  828. else {
  829. /* module has the same name */
  830. LOG_DEBUG(
  831. "module(%p) has been registered with the same name %s",
  832. module, node->module_name);
  833. return true;
  834. }
  835. }
  836. else {
  837. /* module has empyt name, reset it */
  838. node->module_name = module_name;
  839. return true;
  840. }
  841. }
  842. /* module hasn't been registered */
  843. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  844. if (!node) {
  845. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
  846. sizeof(WASMRegisteredModule));
  847. return false;
  848. }
  849. /* share the string and the module */
  850. node->module_name = module_name;
  851. node->module = module;
  852. node->orig_file_buf = orig_file_buf;
  853. node->orig_file_buf_size = orig_file_buf_size;
  854. os_mutex_lock(&registered_module_list_lock);
  855. bh_list_status ret = bh_list_insert(registered_module_list, node);
  856. bh_assert(BH_LIST_SUCCESS == ret);
  857. (void)ret;
  858. os_mutex_unlock(&registered_module_list_lock);
  859. return true;
  860. }
  861. bool
  862. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  863. char *error_buf, uint32 error_buf_size)
  864. {
  865. if (!error_buf || !error_buf_size) {
  866. LOG_ERROR("error buffer is required");
  867. return false;
  868. }
  869. if (!module_name || !module) {
  870. LOG_DEBUG("module_name and module are required");
  871. set_error_buf(error_buf, error_buf_size,
  872. "Register module failed: "
  873. "module_name and module are required");
  874. return false;
  875. }
  876. if (wasm_runtime_is_built_in_module(module_name)) {
  877. LOG_DEBUG("%s is a built-in module name", module_name);
  878. set_error_buf(error_buf, error_buf_size,
  879. "Register module failed: "
  880. "can not register as a built-in module");
  881. return false;
  882. }
  883. return wasm_runtime_register_module_internal(module_name, module, NULL, 0,
  884. error_buf, error_buf_size);
  885. }
  886. void
  887. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  888. {
  889. WASMRegisteredModule *registered_module = NULL;
  890. os_mutex_lock(&registered_module_list_lock);
  891. registered_module = bh_list_first_elem(registered_module_list);
  892. while (registered_module && module != registered_module->module) {
  893. registered_module = bh_list_elem_next(registered_module);
  894. }
  895. /* it does not matter if it is not exist. after all, it is gone */
  896. if (registered_module) {
  897. bh_list_remove(registered_module_list, registered_module);
  898. wasm_runtime_free(registered_module);
  899. }
  900. os_mutex_unlock(&registered_module_list_lock);
  901. }
  902. WASMModuleCommon *
  903. wasm_runtime_find_module_registered(const char *module_name)
  904. {
  905. WASMRegisteredModule *module = NULL, *module_next;
  906. os_mutex_lock(&registered_module_list_lock);
  907. module = bh_list_first_elem(registered_module_list);
  908. while (module) {
  909. module_next = bh_list_elem_next(module);
  910. if (module->module_name && !strcmp(module_name, module->module_name)) {
  911. break;
  912. }
  913. module = module_next;
  914. }
  915. os_mutex_unlock(&registered_module_list_lock);
  916. return module ? module->module : NULL;
  917. }
  918. /*
  919. * simply destroy all
  920. */
  921. static void
  922. wasm_runtime_destroy_registered_module_list()
  923. {
  924. WASMRegisteredModule *reg_module = NULL;
  925. os_mutex_lock(&registered_module_list_lock);
  926. reg_module = bh_list_first_elem(registered_module_list);
  927. while (reg_module) {
  928. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  929. bh_list_remove(registered_module_list, reg_module);
  930. /* now, it is time to release every module in the runtime */
  931. if (reg_module->module->module_type == Wasm_Module_Bytecode) {
  932. #if WASM_ENABLE_INTERP != 0
  933. wasm_unload((WASMModule *)reg_module->module);
  934. #endif
  935. }
  936. else {
  937. #if WASM_ENABLE_AOT != 0
  938. aot_unload((AOTModule *)reg_module->module);
  939. #endif
  940. }
  941. /* destroy the file buffer */
  942. if (destroyer && reg_module->orig_file_buf) {
  943. destroyer(reg_module->orig_file_buf,
  944. reg_module->orig_file_buf_size);
  945. reg_module->orig_file_buf = NULL;
  946. reg_module->orig_file_buf_size = 0;
  947. }
  948. wasm_runtime_free(reg_module);
  949. reg_module = next_reg_module;
  950. }
  951. os_mutex_unlock(&registered_module_list_lock);
  952. }
  953. bool
  954. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  955. uint32 error_buf_size)
  956. {
  957. LOG_DEBUG("add %s into a loading list", module_name);
  958. LoadingModule *loadingModule =
  959. runtime_malloc(sizeof(LoadingModule), NULL, error_buf, error_buf_size);
  960. if (!loadingModule) {
  961. return false;
  962. }
  963. /* share the incoming string */
  964. loadingModule->module_name = module_name;
  965. os_mutex_lock(&loading_module_list_lock);
  966. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  967. bh_assert(BH_LIST_SUCCESS == ret);
  968. (void)ret;
  969. os_mutex_unlock(&loading_module_list_lock);
  970. return true;
  971. }
  972. void
  973. wasm_runtime_delete_loading_module(const char *module_name)
  974. {
  975. LOG_DEBUG("delete %s from a loading list", module_name);
  976. LoadingModule *module = NULL;
  977. os_mutex_lock(&loading_module_list_lock);
  978. module = bh_list_first_elem(loading_module_list);
  979. while (module && strcmp(module->module_name, module_name)) {
  980. module = bh_list_elem_next(module);
  981. }
  982. /* it does not matter if it is not exist. after all, it is gone */
  983. if (module) {
  984. bh_list_remove(loading_module_list, module);
  985. wasm_runtime_free(module);
  986. }
  987. os_mutex_unlock(&loading_module_list_lock);
  988. }
  989. bool
  990. wasm_runtime_is_loading_module(const char *module_name)
  991. {
  992. LOG_DEBUG("find %s in a loading list", module_name);
  993. LoadingModule *module = NULL;
  994. os_mutex_lock(&loading_module_list_lock);
  995. module = bh_list_first_elem(loading_module_list);
  996. while (module && strcmp(module_name, module->module_name)) {
  997. module = bh_list_elem_next(module);
  998. }
  999. os_mutex_unlock(&loading_module_list_lock);
  1000. return module != NULL;
  1001. }
  1002. void
  1003. wasm_runtime_destroy_loading_module_list()
  1004. {
  1005. LoadingModule *module = NULL;
  1006. os_mutex_lock(&loading_module_list_lock);
  1007. module = bh_list_first_elem(loading_module_list);
  1008. while (module) {
  1009. LoadingModule *next_module = bh_list_elem_next(module);
  1010. bh_list_remove(loading_module_list, module);
  1011. /*
  1012. * will not free the module_name since it is
  1013. * shared one of the const string pool
  1014. */
  1015. wasm_runtime_free(module);
  1016. module = next_module;
  1017. }
  1018. os_mutex_unlock(&loading_module_list_lock);
  1019. }
  1020. #endif /* WASM_ENABLE_MULTI_MODULE */
  1021. bool
  1022. wasm_runtime_is_built_in_module(const char *module_name)
  1023. {
  1024. return (!strcmp("env", module_name) || !strcmp("wasi_unstable", module_name)
  1025. || !strcmp("wasi_snapshot_preview1", module_name)
  1026. #if WASM_ENABLE_SPEC_TEST != 0
  1027. || !strcmp("spectest", module_name)
  1028. #endif
  1029. || !strcmp("", module_name));
  1030. }
  1031. #if WASM_ENABLE_THREAD_MGR != 0
  1032. bool
  1033. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  1034. uint32 size)
  1035. {
  1036. WASMModuleInstanceCommon *module_inst =
  1037. wasm_exec_env_get_module_inst(exec_env);
  1038. #if WASM_ENABLE_INTERP != 0
  1039. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1040. return wasm_set_aux_stack(exec_env, start_offset, size);
  1041. }
  1042. #endif
  1043. #if WASM_ENABLE_AOT != 0
  1044. if (module_inst->module_type == Wasm_Module_AoT) {
  1045. return aot_set_aux_stack(exec_env, start_offset, size);
  1046. }
  1047. #endif
  1048. return false;
  1049. }
  1050. bool
  1051. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  1052. uint32 *size)
  1053. {
  1054. WASMModuleInstanceCommon *module_inst =
  1055. wasm_exec_env_get_module_inst(exec_env);
  1056. #if WASM_ENABLE_INTERP != 0
  1057. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1058. return wasm_get_aux_stack(exec_env, start_offset, size);
  1059. }
  1060. #endif
  1061. #if WASM_ENABLE_AOT != 0
  1062. if (module_inst->module_type == Wasm_Module_AoT) {
  1063. return aot_get_aux_stack(exec_env, start_offset, size);
  1064. }
  1065. #endif
  1066. return false;
  1067. }
  1068. void
  1069. wasm_runtime_set_max_thread_num(uint32 num)
  1070. {
  1071. wasm_cluster_set_max_thread_num(num);
  1072. }
  1073. #endif /* end of WASM_ENABLE_THREAD_MGR */
  1074. static WASMModuleCommon *
  1075. register_module_with_null_name(WASMModuleCommon *module_common, char *error_buf,
  1076. uint32 error_buf_size)
  1077. {
  1078. #if WASM_ENABLE_MULTI_MODULE != 0
  1079. if (module_common) {
  1080. if (!wasm_runtime_register_module_internal(NULL, module_common, NULL, 0,
  1081. error_buf, error_buf_size)) {
  1082. wasm_runtime_unload(module_common);
  1083. return NULL;
  1084. }
  1085. return module_common;
  1086. }
  1087. else
  1088. return NULL;
  1089. #else
  1090. return module_common;
  1091. #endif
  1092. }
  1093. WASMModuleCommon *
  1094. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  1095. uint32 error_buf_size)
  1096. {
  1097. WASMModuleCommon *module_common = NULL;
  1098. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  1099. #if WASM_ENABLE_INTERP != 0
  1100. module_common =
  1101. (WASMModuleCommon *)wasm_load(buf, size,
  1102. #if WASM_ENABLE_MULTI_MODULE != 0
  1103. true,
  1104. #endif
  1105. error_buf, error_buf_size);
  1106. #endif
  1107. }
  1108. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  1109. #if WASM_ENABLE_AOT != 0
  1110. module_common = (WASMModuleCommon *)aot_load_from_aot_file(
  1111. buf, size, error_buf, error_buf_size);
  1112. #endif
  1113. }
  1114. else {
  1115. if (size < 4)
  1116. set_error_buf(error_buf, error_buf_size,
  1117. "WASM module load failed: unexpected end");
  1118. else
  1119. set_error_buf(error_buf, error_buf_size,
  1120. "WASM module load failed: magic header not detected");
  1121. return NULL;
  1122. }
  1123. if (!module_common) {
  1124. LOG_DEBUG("WASM module load failed");
  1125. return NULL;
  1126. }
  1127. return register_module_with_null_name(module_common, error_buf,
  1128. error_buf_size);
  1129. }
  1130. WASMModuleCommon *
  1131. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  1132. char *error_buf, uint32 error_buf_size)
  1133. {
  1134. WASMModuleCommon *module_common;
  1135. if (!is_aot) {
  1136. #if WASM_ENABLE_INTERP != 0
  1137. module_common = (WASMModuleCommon *)wasm_load_from_sections(
  1138. section_list, error_buf, error_buf_size);
  1139. if (!module_common) {
  1140. LOG_DEBUG("WASM module load failed from sections");
  1141. return NULL;
  1142. }
  1143. return register_module_with_null_name(module_common, error_buf,
  1144. error_buf_size);
  1145. #endif
  1146. }
  1147. else {
  1148. #if WASM_ENABLE_AOT != 0
  1149. module_common = (WASMModuleCommon *)aot_load_from_sections(
  1150. section_list, error_buf, error_buf_size);
  1151. if (!module_common) {
  1152. LOG_DEBUG("WASM module load failed from sections");
  1153. return NULL;
  1154. }
  1155. return register_module_with_null_name(module_common, error_buf,
  1156. error_buf_size);
  1157. #endif
  1158. }
  1159. #if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
  1160. set_error_buf(error_buf, error_buf_size,
  1161. "WASM module load failed: invalid section list type");
  1162. return NULL;
  1163. #endif
  1164. }
  1165. void
  1166. wasm_runtime_unload(WASMModuleCommon *module)
  1167. {
  1168. #if WASM_ENABLE_MULTI_MODULE != 0
  1169. /**
  1170. * since we will unload and free all module when runtime_destroy()
  1171. * we don't want users to unwillingly disrupt it
  1172. */
  1173. return;
  1174. #endif
  1175. #if WASM_ENABLE_INTERP != 0
  1176. if (module->module_type == Wasm_Module_Bytecode) {
  1177. wasm_unload((WASMModule *)module);
  1178. return;
  1179. }
  1180. #endif
  1181. #if WASM_ENABLE_AOT != 0
  1182. if (module->module_type == Wasm_Module_AoT) {
  1183. aot_unload((AOTModule *)module);
  1184. return;
  1185. }
  1186. #endif
  1187. }
  1188. WASMModuleInstanceCommon *
  1189. wasm_runtime_instantiate_internal(WASMModuleCommon *module,
  1190. WASMModuleInstanceCommon *parent,
  1191. WASMExecEnv *exec_env_main, uint32 stack_size,
  1192. uint32 heap_size, char *error_buf,
  1193. uint32 error_buf_size)
  1194. {
  1195. #if WASM_ENABLE_INTERP != 0
  1196. if (module->module_type == Wasm_Module_Bytecode)
  1197. return (WASMModuleInstanceCommon *)wasm_instantiate(
  1198. (WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main,
  1199. stack_size, heap_size, error_buf, error_buf_size);
  1200. #endif
  1201. #if WASM_ENABLE_AOT != 0
  1202. if (module->module_type == Wasm_Module_AoT)
  1203. return (WASMModuleInstanceCommon *)aot_instantiate(
  1204. (AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main,
  1205. stack_size, heap_size, error_buf, error_buf_size);
  1206. #endif
  1207. set_error_buf(error_buf, error_buf_size,
  1208. "Instantiate module failed, invalid module type");
  1209. return NULL;
  1210. }
  1211. WASMModuleInstanceCommon *
  1212. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  1213. uint32 heap_size, char *error_buf,
  1214. uint32 error_buf_size)
  1215. {
  1216. return wasm_runtime_instantiate_internal(
  1217. module, NULL, NULL, stack_size, heap_size, error_buf, error_buf_size);
  1218. }
  1219. void
  1220. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  1221. bool is_sub_inst)
  1222. {
  1223. #if WASM_ENABLE_INTERP != 0
  1224. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1225. wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst);
  1226. return;
  1227. }
  1228. #endif
  1229. #if WASM_ENABLE_AOT != 0
  1230. if (module_inst->module_type == Wasm_Module_AoT) {
  1231. aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst);
  1232. return;
  1233. }
  1234. #endif
  1235. }
  1236. bool
  1237. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  1238. RunningMode running_mode)
  1239. {
  1240. #if WASM_ENABLE_AOT != 0
  1241. if (module_inst->module_type == Wasm_Module_AoT)
  1242. return true;
  1243. #endif
  1244. #if WASM_ENABLE_INTERP != 0
  1245. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1246. WASMModuleInstance *module_inst_interp =
  1247. (WASMModuleInstance *)module_inst;
  1248. return wasm_set_running_mode(module_inst_interp, running_mode);
  1249. }
  1250. #endif
  1251. return false;
  1252. }
  1253. RunningMode
  1254. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst)
  1255. {
  1256. #if WASM_ENABLE_INTERP != 0
  1257. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1258. WASMModuleInstance *module_inst_interp =
  1259. (WASMModuleInstance *)module_inst;
  1260. return module_inst_interp->e->running_mode;
  1261. }
  1262. #endif
  1263. return Mode_Default;
  1264. }
  1265. void
  1266. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  1267. {
  1268. wasm_runtime_deinstantiate_internal(module_inst, false);
  1269. }
  1270. WASMModuleCommon *
  1271. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst)
  1272. {
  1273. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  1274. }
  1275. WASMExecEnv *
  1276. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  1277. uint32 stack_size)
  1278. {
  1279. return wasm_exec_env_create(module_inst, stack_size);
  1280. }
  1281. void
  1282. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  1283. {
  1284. wasm_exec_env_destroy(exec_env);
  1285. }
  1286. bool
  1287. wasm_runtime_init_thread_env(void)
  1288. {
  1289. #ifdef BH_PLATFORM_WINDOWS
  1290. if (os_thread_env_init() != 0)
  1291. return false;
  1292. #endif
  1293. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1294. if (!runtime_signal_init()) {
  1295. #ifdef BH_PLATFORM_WINDOWS
  1296. os_thread_env_destroy();
  1297. #endif
  1298. return false;
  1299. }
  1300. #endif
  1301. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  1302. os_end_blocking_op();
  1303. #endif
  1304. return true;
  1305. }
  1306. void
  1307. wasm_runtime_destroy_thread_env(void)
  1308. {
  1309. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1310. runtime_signal_destroy();
  1311. #endif
  1312. #ifdef BH_PLATFORM_WINDOWS
  1313. os_thread_env_destroy();
  1314. #endif
  1315. }
  1316. bool
  1317. wasm_runtime_thread_env_inited(void)
  1318. {
  1319. #ifdef BH_PLATFORM_WINDOWS
  1320. if (!os_thread_env_inited())
  1321. return false;
  1322. #endif
  1323. #if WASM_ENABLE_AOT != 0
  1324. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1325. if (!os_thread_signal_inited())
  1326. return false;
  1327. #endif
  1328. #endif
  1329. return true;
  1330. }
  1331. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1332. void
  1333. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  1334. {
  1335. WASMModuleMemConsumption mem_conspn = { 0 };
  1336. #if WASM_ENABLE_INTERP != 0
  1337. if (module->module_type == Wasm_Module_Bytecode) {
  1338. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  1339. }
  1340. #endif
  1341. #if WASM_ENABLE_AOT != 0
  1342. if (module->module_type == Wasm_Module_AoT) {
  1343. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  1344. }
  1345. #endif
  1346. os_printf("WASM module memory consumption, total size: %u\n",
  1347. mem_conspn.total_size);
  1348. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  1349. os_printf(" types size: %u\n", mem_conspn.types_size);
  1350. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  1351. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  1352. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1353. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1354. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1355. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1356. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  1357. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  1358. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  1359. #if WASM_ENABLE_AOT != 0
  1360. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  1361. #endif
  1362. }
  1363. void
  1364. wasm_runtime_dump_module_inst_mem_consumption(
  1365. const WASMModuleInstanceCommon *module_inst)
  1366. {
  1367. WASMModuleInstMemConsumption mem_conspn = { 0 };
  1368. #if WASM_ENABLE_INTERP != 0
  1369. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1370. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  1371. &mem_conspn);
  1372. }
  1373. #endif
  1374. #if WASM_ENABLE_AOT != 0
  1375. if (module_inst->module_type == Wasm_Module_AoT) {
  1376. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  1377. &mem_conspn);
  1378. }
  1379. #endif
  1380. os_printf("WASM module inst memory consumption, total size: %u\n",
  1381. mem_conspn.total_size);
  1382. os_printf(" module inst struct size: %u\n",
  1383. mem_conspn.module_inst_struct_size);
  1384. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1385. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  1386. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1387. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  1388. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1389. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1390. }
  1391. void
  1392. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  1393. {
  1394. uint32 total_size =
  1395. offsetof(WASMExecEnv, wasm_stack_u.bottom) + exec_env->wasm_stack_size;
  1396. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  1397. os_printf(" exec env struct size: %u\n",
  1398. offsetof(WASMExecEnv, wasm_stack_u.bottom));
  1399. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  1400. os_printf(" block addr cache size: %u\n",
  1401. sizeof(exec_env->block_addr_cache));
  1402. #endif
  1403. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  1404. }
  1405. uint32
  1406. gc_get_heap_highmark_size(void *heap);
  1407. void
  1408. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  1409. {
  1410. WASMModuleInstMemConsumption module_inst_mem_consps;
  1411. WASMModuleMemConsumption module_mem_consps;
  1412. WASMModuleInstanceCommon *module_inst_common;
  1413. WASMModuleCommon *module_common = NULL;
  1414. void *heap_handle = NULL;
  1415. uint32 total_size = 0, app_heap_peak_size = 0;
  1416. uint32 max_aux_stack_used = -1;
  1417. module_inst_common = exec_env->module_inst;
  1418. #if WASM_ENABLE_INTERP != 0
  1419. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  1420. WASMModuleInstance *wasm_module_inst =
  1421. (WASMModuleInstance *)module_inst_common;
  1422. WASMModule *wasm_module = wasm_module_inst->module;
  1423. module_common = (WASMModuleCommon *)wasm_module;
  1424. if (wasm_module_inst->memories) {
  1425. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  1426. }
  1427. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  1428. &module_inst_mem_consps);
  1429. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  1430. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  1431. max_aux_stack_used = wasm_module_inst->e->max_aux_stack_used;
  1432. }
  1433. #endif
  1434. #if WASM_ENABLE_AOT != 0
  1435. if (module_inst_common->module_type == Wasm_Module_AoT) {
  1436. AOTModuleInstance *aot_module_inst =
  1437. (AOTModuleInstance *)module_inst_common;
  1438. AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1439. module_common = (WASMModuleCommon *)aot_module;
  1440. if (aot_module_inst->memories) {
  1441. AOTMemoryInstance **memories = aot_module_inst->memories;
  1442. heap_handle = memories[0]->heap_handle;
  1443. }
  1444. aot_get_module_inst_mem_consumption(aot_module_inst,
  1445. &module_inst_mem_consps);
  1446. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  1447. }
  1448. #endif
  1449. bh_assert(module_common != NULL);
  1450. if (heap_handle) {
  1451. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  1452. }
  1453. total_size = offsetof(WASMExecEnv, wasm_stack_u.bottom)
  1454. + exec_env->wasm_stack_size + module_mem_consps.total_size
  1455. + module_inst_mem_consps.total_size;
  1456. os_printf("\nMemory consumption summary (bytes):\n");
  1457. wasm_runtime_dump_module_mem_consumption(module_common);
  1458. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  1459. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  1460. os_printf("\nTotal memory consumption of module, module inst and "
  1461. "exec env: %u\n",
  1462. total_size);
  1463. os_printf("Total interpreter stack used: %u\n",
  1464. exec_env->max_wasm_stack_used);
  1465. if (max_aux_stack_used != (uint32)-1)
  1466. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  1467. else
  1468. os_printf("Total aux stack used: no enough info to profile\n");
  1469. /*
  1470. * Report the native stack usage estimation.
  1471. *
  1472. * Unlike the aux stack above, we report the amount unused
  1473. * because we don't know the stack "bottom".
  1474. *
  1475. * Note that this is just about what the runtime itself observed.
  1476. * It doesn't cover host func implementations, signal handlers, etc.
  1477. */
  1478. if (exec_env->native_stack_top_min != (void *)UINTPTR_MAX)
  1479. os_printf("Native stack left: %zd\n",
  1480. exec_env->native_stack_top_min
  1481. - exec_env->native_stack_boundary);
  1482. else
  1483. os_printf("Native stack left: no enough info to profile\n");
  1484. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  1485. }
  1486. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  1487. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1488. #if WASM_ENABLE_PERF_PROFILING != 0
  1489. void
  1490. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  1491. {
  1492. #if WASM_ENABLE_INTERP != 0
  1493. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1494. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1495. }
  1496. #endif
  1497. #if WASM_ENABLE_AOT != 0
  1498. if (module_inst->module_type == Wasm_Module_AoT) {
  1499. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1500. }
  1501. #endif
  1502. }
  1503. double
  1504. wasm_runtime_sum_wasm_exec_time(WASMModuleInstanceCommon *inst)
  1505. {
  1506. #if WASM_ENABLE_INTERP != 0
  1507. if (inst->module_type == Wasm_Module_Bytecode)
  1508. return wasm_summarize_wasm_execute_time((WASMModuleInstance *)inst);
  1509. #endif
  1510. #if WASM_ENABLE_AOT != 0
  1511. if (inst->module_type == Wasm_Module_AoT)
  1512. return aot_summarize_wasm_execute_time((AOTModuleInstance *)inst);
  1513. #endif
  1514. return 0.0;
  1515. }
  1516. double
  1517. wasm_runtime_get_wasm_func_exec_time(WASMModuleInstanceCommon *inst,
  1518. const char *func_name)
  1519. {
  1520. #if WASM_ENABLE_INTERP != 0
  1521. if (inst->module_type == Wasm_Module_Bytecode)
  1522. return wasm_get_wasm_func_exec_time((WASMModuleInstance *)inst,
  1523. func_name);
  1524. #endif
  1525. #if WASM_ENABLE_AOT != 0
  1526. if (inst->module_type == Wasm_Module_AoT)
  1527. return aot_get_wasm_func_exec_time((AOTModuleInstance *)inst,
  1528. func_name);
  1529. #endif
  1530. return 0.0;
  1531. }
  1532. #endif /* WASM_ENABLE_PERF_PROFILING != 0 */
  1533. WASMModuleInstanceCommon *
  1534. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1535. {
  1536. return wasm_exec_env_get_module_inst(exec_env);
  1537. }
  1538. void
  1539. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  1540. WASMModuleInstanceCommon *const module_inst)
  1541. {
  1542. wasm_exec_env_set_module_inst(exec_env, module_inst);
  1543. }
  1544. void *
  1545. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  1546. {
  1547. return exec_env->attachment;
  1548. }
  1549. void
  1550. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  1551. {
  1552. exec_env->user_data = user_data;
  1553. }
  1554. void *
  1555. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  1556. {
  1557. return exec_env->user_data;
  1558. }
  1559. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1560. void
  1561. wasm_runtime_access_exce_check_guard_page()
  1562. {
  1563. if (exec_env_tls && exec_env_tls->handle == os_self_thread()) {
  1564. uint32 page_size = os_getpagesize();
  1565. memset(exec_env_tls->exce_check_guard_page, 0, page_size);
  1566. }
  1567. }
  1568. #endif
  1569. WASMFuncType *
  1570. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  1571. uint32 module_type)
  1572. {
  1573. WASMFuncType *type = NULL;
  1574. #if WASM_ENABLE_INTERP != 0
  1575. if (module_type == Wasm_Module_Bytecode) {
  1576. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  1577. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  1578. : wasm_func->u.func->func_type;
  1579. }
  1580. #endif
  1581. #if WASM_ENABLE_AOT != 0
  1582. if (module_type == Wasm_Module_AoT) {
  1583. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  1584. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  1585. : aot_func->u.func.func_type;
  1586. }
  1587. #endif
  1588. return type;
  1589. }
  1590. WASMFunctionInstanceCommon *
  1591. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  1592. const char *name, const char *signature)
  1593. {
  1594. #if WASM_ENABLE_INTERP != 0
  1595. if (module_inst->module_type == Wasm_Module_Bytecode)
  1596. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  1597. (const WASMModuleInstance *)module_inst, name, signature);
  1598. #endif
  1599. #if WASM_ENABLE_AOT != 0
  1600. if (module_inst->module_type == Wasm_Module_AoT)
  1601. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  1602. (const AOTModuleInstance *)module_inst, name, signature);
  1603. #endif
  1604. return NULL;
  1605. }
  1606. uint32
  1607. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  1608. WASMModuleInstanceCommon *const module_inst)
  1609. {
  1610. WASMFuncType *type =
  1611. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1612. bh_assert(type);
  1613. return type->param_count;
  1614. }
  1615. uint32
  1616. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  1617. WASMModuleInstanceCommon *const module_inst)
  1618. {
  1619. WASMFuncType *type =
  1620. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1621. bh_assert(type);
  1622. return type->result_count;
  1623. }
  1624. static uint8
  1625. val_type_to_val_kind(uint8 value_type)
  1626. {
  1627. switch (value_type) {
  1628. case VALUE_TYPE_I32:
  1629. return WASM_I32;
  1630. case VALUE_TYPE_I64:
  1631. return WASM_I64;
  1632. case VALUE_TYPE_F32:
  1633. return WASM_F32;
  1634. case VALUE_TYPE_F64:
  1635. return WASM_F64;
  1636. case VALUE_TYPE_FUNCREF:
  1637. return WASM_FUNCREF;
  1638. case VALUE_TYPE_EXTERNREF:
  1639. return WASM_ANYREF;
  1640. default:
  1641. bh_assert(0);
  1642. return 0;
  1643. }
  1644. }
  1645. void
  1646. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  1647. WASMModuleInstanceCommon *const module_inst,
  1648. wasm_valkind_t *param_types)
  1649. {
  1650. WASMFuncType *type =
  1651. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1652. uint32 i;
  1653. bh_assert(type);
  1654. for (i = 0; i < type->param_count; i++) {
  1655. param_types[i] = val_type_to_val_kind(type->types[i]);
  1656. }
  1657. }
  1658. void
  1659. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  1660. WASMModuleInstanceCommon *const module_inst,
  1661. wasm_valkind_t *result_types)
  1662. {
  1663. WASMFuncType *type =
  1664. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1665. uint32 i;
  1666. bh_assert(type);
  1667. for (i = 0; i < type->result_count; i++) {
  1668. result_types[i] =
  1669. val_type_to_val_kind(type->types[type->param_count + i]);
  1670. }
  1671. }
  1672. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1673. /* (uintptr_t)externref -> (uint32)index */
  1674. /* argv -> *ret_argv */
  1675. static bool
  1676. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  1677. WASMFunctionInstanceCommon *function,
  1678. uint32 *argv, uint32 argc, uint32 **ret_argv,
  1679. uint32 *ret_argc_param,
  1680. uint32 *ret_argc_result)
  1681. {
  1682. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  1683. result_i = 0;
  1684. bool need_param_transform = false, need_result_transform = false;
  1685. uint64 size = 0;
  1686. WASMFuncType *func_type = wasm_runtime_get_function_type(
  1687. function, exec_env->module_inst->module_type);
  1688. bh_assert(func_type);
  1689. *ret_argc_param = func_type->param_cell_num;
  1690. *ret_argc_result = func_type->ret_cell_num;
  1691. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  1692. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  1693. need_param_transform = true;
  1694. }
  1695. }
  1696. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  1697. if (VALUE_TYPE_EXTERNREF
  1698. == func_type->types[func_type->param_count + result_i]) {
  1699. need_result_transform = true;
  1700. }
  1701. }
  1702. if (!need_param_transform && !need_result_transform) {
  1703. *ret_argv = argv;
  1704. return true;
  1705. }
  1706. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  1707. size = sizeof(uint32) * func_type->param_cell_num;
  1708. }
  1709. else {
  1710. size = sizeof(uint32) * func_type->ret_cell_num;
  1711. }
  1712. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  1713. return false;
  1714. }
  1715. if (!need_param_transform) {
  1716. bh_memcpy_s(new_argv, (uint32)size, argv, (uint32)size);
  1717. }
  1718. else {
  1719. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  1720. && new_argv_i < func_type->param_cell_num;
  1721. param_i++) {
  1722. uint8 param_type = func_type->types[param_i];
  1723. if (VALUE_TYPE_EXTERNREF == param_type) {
  1724. void *externref_obj;
  1725. uint32 externref_index;
  1726. #if UINTPTR_MAX == UINT32_MAX
  1727. externref_obj = (void *)argv[argv_i];
  1728. #else
  1729. union {
  1730. uintptr_t val;
  1731. uint32 parts[2];
  1732. } u;
  1733. u.parts[0] = argv[argv_i];
  1734. u.parts[1] = argv[argv_i + 1];
  1735. externref_obj = (void *)u.val;
  1736. #endif
  1737. if (!wasm_externref_obj2ref(exec_env->module_inst,
  1738. externref_obj, &externref_index)) {
  1739. wasm_runtime_free(new_argv);
  1740. return false;
  1741. }
  1742. new_argv[new_argv_i] = externref_index;
  1743. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1744. new_argv_i++;
  1745. }
  1746. else {
  1747. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  1748. uint32 param_size = sizeof(uint32) * param_cell_num;
  1749. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  1750. param_size);
  1751. argv_i += param_cell_num;
  1752. new_argv_i += param_cell_num;
  1753. }
  1754. }
  1755. }
  1756. *ret_argv = new_argv;
  1757. return true;
  1758. }
  1759. /* (uintptr_t)externref <- (uint32)index */
  1760. /* argv <- new_argv */
  1761. static bool
  1762. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  1763. WASMFunctionInstanceCommon *function,
  1764. uint32 *argv, uint32 argc, uint32 *ret_argv)
  1765. {
  1766. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  1767. WASMFuncType *func_type;
  1768. bh_assert((argv && ret_argv) || (argc == 0));
  1769. if (argv == ret_argv) {
  1770. /* no need to transfrom externref results */
  1771. return true;
  1772. }
  1773. func_type = wasm_runtime_get_function_type(
  1774. function, exec_env->module_inst->module_type);
  1775. bh_assert(func_type);
  1776. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  1777. result_i++) {
  1778. uint8 result_type = func_type->types[func_type->param_count + result_i];
  1779. if (result_type == VALUE_TYPE_EXTERNREF) {
  1780. void *externref_obj;
  1781. #if UINTPTR_MAX != UINT32_MAX
  1782. union {
  1783. uintptr_t val;
  1784. uint32 parts[2];
  1785. } u;
  1786. #endif
  1787. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  1788. wasm_runtime_free(argv);
  1789. return false;
  1790. }
  1791. #if UINTPTR_MAX == UINT32_MAX
  1792. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  1793. #else
  1794. u.val = (uintptr_t)externref_obj;
  1795. ret_argv[ret_argv_i] = u.parts[0];
  1796. ret_argv[ret_argv_i + 1] = u.parts[1];
  1797. #endif
  1798. argv_i += 1;
  1799. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1800. }
  1801. else {
  1802. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  1803. uint32 result_size = sizeof(uint32) * result_cell_num;
  1804. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  1805. result_size);
  1806. argv_i += result_cell_num;
  1807. ret_argv_i += result_cell_num;
  1808. }
  1809. }
  1810. wasm_runtime_free(argv);
  1811. return true;
  1812. }
  1813. #endif
  1814. bool
  1815. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  1816. WASMFunctionInstanceCommon *function, uint32 argc,
  1817. uint32 argv[])
  1818. {
  1819. bool ret = false;
  1820. uint32 *new_argv = NULL, param_argc;
  1821. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1822. uint32 result_argc = 0;
  1823. #endif
  1824. if (!wasm_runtime_exec_env_check(exec_env)) {
  1825. LOG_ERROR("Invalid exec env stack info.");
  1826. return false;
  1827. }
  1828. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1829. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  1830. &new_argv, &param_argc,
  1831. &result_argc)) {
  1832. wasm_runtime_set_exception(exec_env->module_inst,
  1833. "the arguments conversion is failed");
  1834. return false;
  1835. }
  1836. #else
  1837. new_argv = argv;
  1838. param_argc = argc;
  1839. #endif
  1840. #if WASM_ENABLE_INTERP != 0
  1841. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  1842. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  1843. param_argc, new_argv);
  1844. #endif
  1845. #if WASM_ENABLE_AOT != 0
  1846. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  1847. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  1848. param_argc, new_argv);
  1849. #endif
  1850. if (!ret) {
  1851. if (new_argv != argv) {
  1852. wasm_runtime_free(new_argv);
  1853. }
  1854. return false;
  1855. }
  1856. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1857. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  1858. result_argc, argv)) {
  1859. wasm_runtime_set_exception(exec_env->module_inst,
  1860. "the result conversion is failed");
  1861. return false;
  1862. }
  1863. #endif
  1864. return ret;
  1865. }
  1866. static void
  1867. parse_args_to_uint32_array(WASMFuncType *type, wasm_val_t *args,
  1868. uint32 *out_argv)
  1869. {
  1870. uint32 i, p;
  1871. for (i = 0, p = 0; i < type->param_count; i++) {
  1872. switch (args[i].kind) {
  1873. case WASM_I32:
  1874. out_argv[p++] = args[i].of.i32;
  1875. break;
  1876. case WASM_I64:
  1877. {
  1878. union {
  1879. uint64 val;
  1880. uint32 parts[2];
  1881. } u;
  1882. u.val = args[i].of.i64;
  1883. out_argv[p++] = u.parts[0];
  1884. out_argv[p++] = u.parts[1];
  1885. break;
  1886. }
  1887. case WASM_F32:
  1888. {
  1889. union {
  1890. float32 val;
  1891. uint32 part;
  1892. } u;
  1893. u.val = args[i].of.f32;
  1894. out_argv[p++] = u.part;
  1895. break;
  1896. }
  1897. case WASM_F64:
  1898. {
  1899. union {
  1900. float64 val;
  1901. uint32 parts[2];
  1902. } u;
  1903. u.val = args[i].of.f64;
  1904. out_argv[p++] = u.parts[0];
  1905. out_argv[p++] = u.parts[1];
  1906. break;
  1907. }
  1908. #if WASM_ENABLE_REF_TYPES != 0
  1909. #if WASM_ENABLE_GC == 0
  1910. case WASM_FUNCREF:
  1911. {
  1912. out_argv[p++] = args[i].of.i32;
  1913. break;
  1914. }
  1915. #else
  1916. case WASM_FUNCREF:
  1917. #endif
  1918. case WASM_ANYREF:
  1919. {
  1920. #if UINTPTR_MAX == UINT32_MAX
  1921. out_argv[p++] = args[i].of.foreign;
  1922. #else
  1923. union {
  1924. uintptr_t val;
  1925. uint32 parts[2];
  1926. } u;
  1927. u.val = (uintptr_t)args[i].of.foreign;
  1928. out_argv[p++] = u.parts[0];
  1929. out_argv[p++] = u.parts[1];
  1930. #endif
  1931. break;
  1932. }
  1933. #endif
  1934. default:
  1935. bh_assert(0);
  1936. break;
  1937. }
  1938. }
  1939. }
  1940. static void
  1941. parse_uint32_array_to_results(WASMFuncType *type, uint32 *argv,
  1942. wasm_val_t *out_results)
  1943. {
  1944. uint32 i, p;
  1945. for (i = 0, p = 0; i < type->result_count; i++) {
  1946. switch (type->types[type->param_count + i]) {
  1947. case VALUE_TYPE_I32:
  1948. out_results[i].kind = WASM_I32;
  1949. out_results[i].of.i32 = (int32)argv[p++];
  1950. break;
  1951. case VALUE_TYPE_I64:
  1952. {
  1953. union {
  1954. uint64 val;
  1955. uint32 parts[2];
  1956. } u;
  1957. u.parts[0] = argv[p++];
  1958. u.parts[1] = argv[p++];
  1959. out_results[i].kind = WASM_I64;
  1960. out_results[i].of.i64 = u.val;
  1961. break;
  1962. }
  1963. case VALUE_TYPE_F32:
  1964. {
  1965. union {
  1966. float32 val;
  1967. uint32 part;
  1968. } u;
  1969. u.part = argv[p++];
  1970. out_results[i].kind = WASM_F32;
  1971. out_results[i].of.f32 = u.val;
  1972. break;
  1973. }
  1974. case VALUE_TYPE_F64:
  1975. {
  1976. union {
  1977. float64 val;
  1978. uint32 parts[2];
  1979. } u;
  1980. u.parts[0] = argv[p++];
  1981. u.parts[1] = argv[p++];
  1982. out_results[i].kind = WASM_F64;
  1983. out_results[i].of.f64 = u.val;
  1984. break;
  1985. }
  1986. #if WASM_ENABLE_REF_TYPES != 0
  1987. #if WASM_ENABLE_GC == 0
  1988. case VALUE_TYPE_FUNCREF:
  1989. {
  1990. out_results[i].kind = WASM_I32;
  1991. out_results[i].of.i32 = (int32)argv[p++];
  1992. break;
  1993. }
  1994. case VALUE_TYPE_EXTERNREF:
  1995. #else
  1996. case REF_TYPE_FUNCREF:
  1997. case REF_TYPE_EXTERNREF:
  1998. case REF_TYPE_ANYREF:
  1999. case REF_TYPE_EQREF:
  2000. case REF_TYPE_HT_NULLABLE:
  2001. case REF_TYPE_HT_NON_NULLABLE:
  2002. case REF_TYPE_I31REF:
  2003. case REF_TYPE_NULLFUNCREF:
  2004. case REF_TYPE_NULLEXTERNREF:
  2005. case REF_TYPE_STRUCTREF:
  2006. case REF_TYPE_ARRAYREF:
  2007. case REF_TYPE_NULLREF:
  2008. #endif /* end of WASM_ENABLE_GC == 0 */
  2009. {
  2010. #if UINTPTR_MAX == UINT32_MAX
  2011. out_results[i].kind = WASM_ANYREF;
  2012. out_results[i].of.foreign = (uintptr_t)argv[p++];
  2013. #else
  2014. union {
  2015. uintptr_t val;
  2016. uint32 parts[2];
  2017. } u;
  2018. u.parts[0] = argv[p++];
  2019. u.parts[1] = argv[p++];
  2020. out_results[i].kind = WASM_ANYREF;
  2021. out_results[i].of.foreign = u.val;
  2022. #endif
  2023. break;
  2024. }
  2025. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  2026. default:
  2027. bh_assert(0);
  2028. break;
  2029. }
  2030. }
  2031. }
  2032. bool
  2033. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  2034. WASMFunctionInstanceCommon *function,
  2035. uint32 num_results, wasm_val_t results[],
  2036. uint32 num_args, wasm_val_t args[])
  2037. {
  2038. uint32 argc, argv_buf[16] = { 0 }, *argv = argv_buf, cell_num, module_type;
  2039. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2040. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  2041. #endif
  2042. uint64 total_size;
  2043. WASMFuncType *type;
  2044. bool ret = false;
  2045. module_type = exec_env->module_inst->module_type;
  2046. type = wasm_runtime_get_function_type(function, module_type);
  2047. if (!type) {
  2048. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  2049. "enabled at least one.");
  2050. goto fail1;
  2051. }
  2052. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2053. for (i = 0; i < type->param_count; i++) {
  2054. param_size_in_double_world +=
  2055. wasm_value_type_cell_num_outside(type->types[i]);
  2056. }
  2057. for (i = 0; i < type->result_count; i++) {
  2058. result_size_in_double_world += wasm_value_type_cell_num_outside(
  2059. type->types[type->param_count + i]);
  2060. }
  2061. argc = param_size_in_double_world;
  2062. cell_num = (argc >= result_size_in_double_world)
  2063. ? argc
  2064. : result_size_in_double_world;
  2065. #else
  2066. argc = type->param_cell_num;
  2067. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  2068. #endif
  2069. if (num_results != type->result_count) {
  2070. LOG_ERROR(
  2071. "The result value number does not match the function declaration.");
  2072. goto fail1;
  2073. }
  2074. if (num_args != type->param_count) {
  2075. LOG_ERROR("The argument value number does not match the function "
  2076. "declaration.");
  2077. goto fail1;
  2078. }
  2079. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  2080. if (total_size > sizeof(argv_buf)) {
  2081. if (!(argv =
  2082. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2083. goto fail1;
  2084. }
  2085. }
  2086. parse_args_to_uint32_array(type, args, argv);
  2087. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  2088. goto fail2;
  2089. parse_uint32_array_to_results(type, argv, results);
  2090. fail2:
  2091. if (argv != argv_buf)
  2092. wasm_runtime_free(argv);
  2093. fail1:
  2094. return ret;
  2095. }
  2096. bool
  2097. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  2098. WASMFunctionInstanceCommon *function,
  2099. uint32 num_results, wasm_val_t results[],
  2100. uint32 num_args, ...)
  2101. {
  2102. wasm_val_t args_buf[8] = { 0 }, *args = args_buf;
  2103. WASMFuncType *type = NULL;
  2104. bool ret = false;
  2105. uint64 total_size;
  2106. uint32 i = 0, module_type;
  2107. va_list vargs;
  2108. module_type = exec_env->module_inst->module_type;
  2109. type = wasm_runtime_get_function_type(function, module_type);
  2110. if (!type) {
  2111. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  2112. "must be enabled at least one.");
  2113. goto fail1;
  2114. }
  2115. if (num_args != type->param_count) {
  2116. LOG_ERROR("The argument value number does not match the "
  2117. "function declaration.");
  2118. goto fail1;
  2119. }
  2120. total_size = sizeof(wasm_val_t) * (uint64)num_args;
  2121. if (total_size > sizeof(args_buf)) {
  2122. if (!(args =
  2123. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2124. goto fail1;
  2125. }
  2126. }
  2127. va_start(vargs, num_args);
  2128. for (i = 0; i < num_args; i++) {
  2129. switch (type->types[i]) {
  2130. case VALUE_TYPE_I32:
  2131. args[i].kind = WASM_I32;
  2132. args[i].of.i32 = va_arg(vargs, uint32);
  2133. break;
  2134. case VALUE_TYPE_I64:
  2135. args[i].kind = WASM_I64;
  2136. args[i].of.i64 = va_arg(vargs, uint64);
  2137. break;
  2138. case VALUE_TYPE_F32:
  2139. args[i].kind = WASM_F32;
  2140. args[i].of.f32 = (float32)va_arg(vargs, float64);
  2141. break;
  2142. case VALUE_TYPE_F64:
  2143. args[i].kind = WASM_F64;
  2144. args[i].of.f64 = va_arg(vargs, float64);
  2145. break;
  2146. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2147. case VALUE_TYPE_FUNCREF:
  2148. {
  2149. args[i].kind = WASM_FUNCREF;
  2150. args[i].of.i32 = va_arg(vargs, uint32);
  2151. break;
  2152. }
  2153. case VALUE_TYPE_EXTERNREF:
  2154. {
  2155. args[i].kind = WASM_ANYREF;
  2156. args[i].of.foreign = va_arg(vargs, uintptr_t);
  2157. break;
  2158. }
  2159. #endif
  2160. default:
  2161. bh_assert(0);
  2162. break;
  2163. }
  2164. }
  2165. va_end(vargs);
  2166. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  2167. num_args, args);
  2168. if (args != args_buf)
  2169. wasm_runtime_free(args);
  2170. fail1:
  2171. return ret;
  2172. }
  2173. bool
  2174. wasm_runtime_create_exec_env_singleton(
  2175. WASMModuleInstanceCommon *module_inst_comm)
  2176. {
  2177. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2178. WASMExecEnv *exec_env = NULL;
  2179. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2180. || module_inst_comm->module_type == Wasm_Module_AoT);
  2181. if (module_inst->exec_env_singleton) {
  2182. return true;
  2183. }
  2184. exec_env = wasm_exec_env_create(module_inst_comm,
  2185. module_inst->default_wasm_stack_size);
  2186. if (exec_env)
  2187. module_inst->exec_env_singleton = exec_env;
  2188. return exec_env ? true : false;
  2189. }
  2190. WASMExecEnv *
  2191. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm)
  2192. {
  2193. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2194. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2195. || module_inst_comm->module_type == Wasm_Module_AoT);
  2196. if (!module_inst->exec_env_singleton) {
  2197. wasm_runtime_create_exec_env_singleton(module_inst_comm);
  2198. }
  2199. return module_inst->exec_env_singleton;
  2200. }
  2201. static void
  2202. wasm_set_exception_local(WASMModuleInstance *module_inst, const char *exception)
  2203. {
  2204. exception_lock(module_inst);
  2205. if (exception) {
  2206. snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
  2207. "Exception: %s", exception);
  2208. }
  2209. else {
  2210. module_inst->cur_exception[0] = '\0';
  2211. }
  2212. exception_unlock(module_inst);
  2213. }
  2214. void
  2215. wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
  2216. {
  2217. #if WASM_ENABLE_THREAD_MGR != 0
  2218. WASMExecEnv *exec_env =
  2219. wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
  2220. if (exec_env) {
  2221. wasm_cluster_set_exception(exec_env, exception);
  2222. }
  2223. else {
  2224. wasm_set_exception_local(module_inst, exception);
  2225. }
  2226. #else
  2227. wasm_set_exception_local(module_inst, exception);
  2228. #endif
  2229. }
  2230. /* clang-format off */
  2231. static const char *exception_msgs[] = {
  2232. "unreachable", /* EXCE_UNREACHABLE */
  2233. "allocate memory failed", /* EXCE_OUT_OF_MEMORY */
  2234. "out of bounds memory access", /* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
  2235. "integer overflow", /* EXCE_INTEGER_OVERFLOW */
  2236. "integer divide by zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
  2237. "invalid conversion to integer", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
  2238. "indirect call type mismatch", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
  2239. "invalid function index", /* EXCE_INVALID_FUNCTION_INDEX */
  2240. "undefined element", /* EXCE_UNDEFINED_ELEMENT */
  2241. "uninitialized element", /* EXCE_UNINITIALIZED_ELEMENT */
  2242. "failed to call unlinked import function", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
  2243. "native stack overflow", /* EXCE_NATIVE_STACK_OVERFLOW */
  2244. "unaligned atomic", /* EXCE_UNALIGNED_ATOMIC */
  2245. "wasm auxiliary stack overflow", /* EXCE_AUX_STACK_OVERFLOW */
  2246. "wasm auxiliary stack underflow", /* EXCE_AUX_STACK_UNDERFLOW */
  2247. "out of bounds table access", /* EXCE_OUT_OF_BOUNDS_TABLE_ACCESS */
  2248. "wasm operand stack overflow", /* EXCE_OPERAND_STACK_OVERFLOW */
  2249. "failed to compile fast jit function", /* EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC */
  2250. /* GC related exceptions */
  2251. "null function object", /* EXCE_NULL_FUNC_OBJ */
  2252. "null structure object", /* EXCE_NULL_STRUCT_OBJ */
  2253. "null array reference", /* EXCE_NULL_ARRAY_OBJ */
  2254. "null i31 reference", /* EXCE_NULL_I31_OBJ */
  2255. "null reference", /* EXCE_NULL_REFERENCE */
  2256. "create rtt type failed", /* EXCE_FAILED_TO_CREATE_RTT_TYPE */
  2257. "create struct object failed", /* EXCE_FAILED_TO_CREATE_STRUCT_OBJ */
  2258. "create array object failed", /* EXCE_FAILED_TO_CREATE_ARRAY_OBJ */
  2259. "create externref object failed", /* EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ */
  2260. "cast failure", /* EXCE_CAST_FAILURE */
  2261. "out of bounds array access", /* EXCE_ARRAY_IDX_OOB */
  2262. /* stringref related exceptions */
  2263. "create string object failed", /* EXCE_FAILED_TO_CREATE_STRING */
  2264. "create stringref failed", /* EXCE_FAILED_TO_CREATE_STRINGREF */
  2265. "create stringview failed", /* EXCE_FAILED_TO_CREATE_STRINGVIEW */
  2266. "encode failed", /* EXCE_FAILED_TO_ENCODE_STRING */
  2267. "", /* EXCE_ALREADY_THROWN */
  2268. };
  2269. /* clang-format on */
  2270. void
  2271. wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2272. {
  2273. if (id < EXCE_NUM)
  2274. wasm_set_exception(module_inst, exception_msgs[id]);
  2275. else
  2276. wasm_set_exception(module_inst, "unknown exception");
  2277. }
  2278. const char *
  2279. wasm_get_exception(WASMModuleInstance *module_inst)
  2280. {
  2281. if (module_inst->cur_exception[0] == '\0')
  2282. return NULL;
  2283. else
  2284. return module_inst->cur_exception;
  2285. }
  2286. bool
  2287. wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf)
  2288. {
  2289. bool has_exception = false;
  2290. exception_lock(module_inst);
  2291. if (module_inst->cur_exception[0] != '\0') {
  2292. /* NULL is passed if the caller is not interested in getting the
  2293. * exception content, but only in knowing if an exception has been
  2294. * raised
  2295. */
  2296. if (exception_buf != NULL)
  2297. bh_memcpy_s(exception_buf, sizeof(module_inst->cur_exception),
  2298. module_inst->cur_exception,
  2299. sizeof(module_inst->cur_exception));
  2300. has_exception = true;
  2301. }
  2302. exception_unlock(module_inst);
  2303. return has_exception;
  2304. }
  2305. void
  2306. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst_comm,
  2307. const char *exception)
  2308. {
  2309. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2310. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2311. || module_inst_comm->module_type == Wasm_Module_AoT);
  2312. wasm_set_exception(module_inst, exception);
  2313. }
  2314. const char *
  2315. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst_comm)
  2316. {
  2317. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2318. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2319. || module_inst_comm->module_type == Wasm_Module_AoT);
  2320. return wasm_get_exception(module_inst);
  2321. }
  2322. bool
  2323. wasm_runtime_copy_exception(WASMModuleInstanceCommon *module_inst_comm,
  2324. char *exception_buf)
  2325. {
  2326. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2327. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2328. || module_inst_comm->module_type == Wasm_Module_AoT);
  2329. return wasm_copy_exception(module_inst, exception_buf);
  2330. }
  2331. void
  2332. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst_comm)
  2333. {
  2334. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2335. || module_inst_comm->module_type == Wasm_Module_AoT);
  2336. wasm_runtime_set_exception(module_inst_comm, NULL);
  2337. }
  2338. void
  2339. wasm_runtime_terminate(WASMModuleInstanceCommon *module_inst_comm)
  2340. {
  2341. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2342. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2343. || module_inst_comm->module_type == Wasm_Module_AoT);
  2344. wasm_set_exception(module_inst, "terminated by user");
  2345. }
  2346. void
  2347. wasm_runtime_set_custom_data_internal(
  2348. WASMModuleInstanceCommon *module_inst_comm, void *custom_data)
  2349. {
  2350. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2351. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2352. || module_inst_comm->module_type == Wasm_Module_AoT);
  2353. module_inst->custom_data = custom_data;
  2354. }
  2355. void
  2356. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  2357. void *custom_data)
  2358. {
  2359. #if WASM_ENABLE_THREAD_MGR != 0
  2360. wasm_cluster_spread_custom_data(module_inst, custom_data);
  2361. #else
  2362. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  2363. #endif
  2364. }
  2365. void *
  2366. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst_comm)
  2367. {
  2368. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2369. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2370. || module_inst_comm->module_type == Wasm_Module_AoT);
  2371. return module_inst->custom_data;
  2372. }
  2373. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  2374. void
  2375. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  2376. bool enable)
  2377. {
  2378. /* Alwary disable bounds checks if hw bounds checks enabled */
  2379. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2380. enable = false;
  2381. #endif
  2382. #if WASM_ENABLE_INTERP != 0
  2383. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2384. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  2385. ->common.disable_bounds_checks = enable ? false : true;
  2386. }
  2387. #endif
  2388. #if WASM_ENABLE_AOT != 0
  2389. if (module_inst->module_type == Wasm_Module_AoT) {
  2390. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  2391. ->common.disable_bounds_checks = enable ? false : true;
  2392. }
  2393. #endif
  2394. }
  2395. bool
  2396. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  2397. {
  2398. #if WASM_ENABLE_INTERP != 0
  2399. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2400. return !((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2401. ->e)
  2402. ->common.disable_bounds_checks;
  2403. }
  2404. #endif
  2405. #if WASM_ENABLE_AOT != 0
  2406. if (module_inst->module_type == Wasm_Module_AoT) {
  2407. return !((AOTModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2408. ->e)
  2409. ->common.disable_bounds_checks;
  2410. }
  2411. #endif
  2412. return true;
  2413. }
  2414. #endif
  2415. uint32
  2416. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  2417. WASMExecEnv *exec_env, uint32 size,
  2418. void **p_native_addr)
  2419. {
  2420. #if WASM_ENABLE_INTERP != 0
  2421. if (module_inst->module_type == Wasm_Module_Bytecode)
  2422. return wasm_module_malloc_internal((WASMModuleInstance *)module_inst,
  2423. exec_env, size, p_native_addr);
  2424. #endif
  2425. #if WASM_ENABLE_AOT != 0
  2426. if (module_inst->module_type == Wasm_Module_AoT)
  2427. return aot_module_malloc_internal((AOTModuleInstance *)module_inst,
  2428. exec_env, size, p_native_addr);
  2429. #endif
  2430. return 0;
  2431. }
  2432. uint32
  2433. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  2434. WASMExecEnv *exec_env, uint32 ptr,
  2435. uint32 size, void **p_native_addr)
  2436. {
  2437. #if WASM_ENABLE_INTERP != 0
  2438. if (module_inst->module_type == Wasm_Module_Bytecode)
  2439. return wasm_module_realloc_internal((WASMModuleInstance *)module_inst,
  2440. exec_env, ptr, size, p_native_addr);
  2441. #endif
  2442. #if WASM_ENABLE_AOT != 0
  2443. if (module_inst->module_type == Wasm_Module_AoT)
  2444. return aot_module_realloc_internal((AOTModuleInstance *)module_inst,
  2445. exec_env, ptr, size, p_native_addr);
  2446. #endif
  2447. return 0;
  2448. }
  2449. void
  2450. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  2451. WASMExecEnv *exec_env, uint32 ptr)
  2452. {
  2453. #if WASM_ENABLE_INTERP != 0
  2454. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2455. wasm_module_free_internal((WASMModuleInstance *)module_inst, exec_env,
  2456. ptr);
  2457. return;
  2458. }
  2459. #endif
  2460. #if WASM_ENABLE_AOT != 0
  2461. if (module_inst->module_type == Wasm_Module_AoT) {
  2462. aot_module_free_internal((AOTModuleInstance *)module_inst, exec_env,
  2463. ptr);
  2464. return;
  2465. }
  2466. #endif
  2467. }
  2468. uint32
  2469. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  2470. void **p_native_addr)
  2471. {
  2472. #if WASM_ENABLE_INTERP != 0
  2473. if (module_inst->module_type == Wasm_Module_Bytecode)
  2474. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  2475. p_native_addr);
  2476. #endif
  2477. #if WASM_ENABLE_AOT != 0
  2478. if (module_inst->module_type == Wasm_Module_AoT)
  2479. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  2480. p_native_addr);
  2481. #endif
  2482. return 0;
  2483. }
  2484. uint32
  2485. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  2486. uint32 size, void **p_native_addr)
  2487. {
  2488. #if WASM_ENABLE_INTERP != 0
  2489. if (module_inst->module_type == Wasm_Module_Bytecode)
  2490. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  2491. p_native_addr);
  2492. #endif
  2493. #if WASM_ENABLE_AOT != 0
  2494. if (module_inst->module_type == Wasm_Module_AoT)
  2495. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  2496. p_native_addr);
  2497. #endif
  2498. return 0;
  2499. }
  2500. void
  2501. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  2502. {
  2503. #if WASM_ENABLE_INTERP != 0
  2504. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2505. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  2506. return;
  2507. }
  2508. #endif
  2509. #if WASM_ENABLE_AOT != 0
  2510. if (module_inst->module_type == Wasm_Module_AoT) {
  2511. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  2512. return;
  2513. }
  2514. #endif
  2515. }
  2516. uint32
  2517. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  2518. const char *src, uint32 size)
  2519. {
  2520. #if WASM_ENABLE_INTERP != 0
  2521. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2522. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  2523. size);
  2524. }
  2525. #endif
  2526. #if WASM_ENABLE_AOT != 0
  2527. if (module_inst->module_type == Wasm_Module_AoT) {
  2528. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  2529. }
  2530. #endif
  2531. return 0;
  2532. }
  2533. #if WASM_ENABLE_LIBC_WASI != 0
  2534. static WASIArguments *
  2535. get_wasi_args_from_module(wasm_module_t module)
  2536. {
  2537. WASIArguments *wasi_args = NULL;
  2538. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  2539. if (module->module_type == Wasm_Module_Bytecode)
  2540. wasi_args = &((WASMModule *)module)->wasi_args;
  2541. #endif
  2542. #if WASM_ENABLE_AOT != 0
  2543. if (module->module_type == Wasm_Module_AoT)
  2544. wasi_args = &((AOTModule *)module)->wasi_args;
  2545. #endif
  2546. return wasi_args;
  2547. }
  2548. void
  2549. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  2550. uint32 dir_count, const char *map_dir_list[],
  2551. uint32 map_dir_count, const char *env_list[],
  2552. uint32 env_count, char *argv[], int argc,
  2553. int64 stdinfd, int64 stdoutfd, int64 stderrfd)
  2554. {
  2555. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2556. bh_assert(wasi_args);
  2557. wasi_args->dir_list = dir_list;
  2558. wasi_args->dir_count = dir_count;
  2559. wasi_args->map_dir_list = map_dir_list;
  2560. wasi_args->map_dir_count = map_dir_count;
  2561. wasi_args->env = env_list;
  2562. wasi_args->env_count = env_count;
  2563. wasi_args->argv = argv;
  2564. wasi_args->argc = (uint32)argc;
  2565. wasi_args->stdio[0] = (os_raw_file_handle)stdinfd;
  2566. wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
  2567. wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
  2568. #if WASM_ENABLE_MULTI_MODULE != 0
  2569. #if WASM_ENABLE_INTERP != 0
  2570. if (module->module_type == Wasm_Module_Bytecode) {
  2571. wasm_propagate_wasi_args((WASMModule *)module);
  2572. }
  2573. #endif
  2574. #endif
  2575. }
  2576. void
  2577. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  2578. uint32 dir_count, const char *map_dir_list[],
  2579. uint32 map_dir_count, const char *env_list[],
  2580. uint32 env_count, char *argv[], int argc)
  2581. {
  2582. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  2583. map_dir_count, env_list, env_count, argv,
  2584. argc, -1, -1, -1);
  2585. }
  2586. void
  2587. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  2588. uint32 addr_pool_size)
  2589. {
  2590. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2591. if (wasi_args) {
  2592. wasi_args->addr_pool = addr_pool;
  2593. wasi_args->addr_count = addr_pool_size;
  2594. }
  2595. }
  2596. void
  2597. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  2598. const char *ns_lookup_pool[],
  2599. uint32 ns_lookup_pool_size)
  2600. {
  2601. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2602. if (wasi_args) {
  2603. wasi_args->ns_lookup_pool = ns_lookup_pool;
  2604. wasi_args->ns_lookup_count = ns_lookup_pool_size;
  2605. }
  2606. }
  2607. #if WASM_ENABLE_UVWASI == 0
  2608. static bool
  2609. copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
  2610. char ***list_ptr, uint64 *out_buf_size)
  2611. {
  2612. uint64 buf_size = 0, total_size;
  2613. uint32 buf_offset = 0, i;
  2614. char *buf = NULL, **list = NULL;
  2615. for (i = 0; i < array_size; i++)
  2616. buf_size += strlen(array[i]) + 1;
  2617. /* We add +1 to generate null-terminated array of strings */
  2618. total_size = sizeof(char *) * ((uint64)array_size + 1);
  2619. if (total_size >= UINT32_MAX
  2620. /* total_size must be larger than 0, don' check it again */
  2621. || !(list = wasm_runtime_malloc((uint32)total_size))
  2622. || buf_size >= UINT32_MAX
  2623. || (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
  2624. if (buf)
  2625. wasm_runtime_free(buf);
  2626. if (list)
  2627. wasm_runtime_free(list);
  2628. return false;
  2629. }
  2630. for (i = 0; i < array_size; i++) {
  2631. list[i] = buf + buf_offset;
  2632. bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
  2633. buf_offset += (uint32)(strlen(array[i]) + 1);
  2634. }
  2635. list[array_size] = NULL;
  2636. *list_ptr = list;
  2637. *buf_ptr = buf;
  2638. if (out_buf_size)
  2639. *out_buf_size = buf_size;
  2640. return true;
  2641. }
  2642. bool
  2643. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2644. const char *dir_list[], uint32 dir_count,
  2645. const char *map_dir_list[], uint32 map_dir_count,
  2646. const char *env[], uint32 env_count,
  2647. const char *addr_pool[], uint32 addr_pool_size,
  2648. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  2649. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  2650. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  2651. char *error_buf, uint32 error_buf_size)
  2652. {
  2653. WASIContext *wasi_ctx;
  2654. char *argv_buf = NULL;
  2655. char **argv_list = NULL;
  2656. char *env_buf = NULL;
  2657. char **env_list = NULL;
  2658. char *ns_lookup_buf = NULL;
  2659. char **ns_lookup_list = NULL;
  2660. uint64 argv_buf_size = 0, env_buf_size = 0;
  2661. struct fd_table *curfds = NULL;
  2662. struct fd_prestats *prestats = NULL;
  2663. struct argv_environ_values *argv_environ = NULL;
  2664. struct addr_pool *apool = NULL;
  2665. bool fd_table_inited = false, fd_prestats_inited = false;
  2666. bool argv_environ_inited = false;
  2667. bool addr_pool_inited = false;
  2668. __wasi_fd_t wasm_fd = 3;
  2669. os_file_handle file_handle;
  2670. char *path, resolved_path[PATH_MAX];
  2671. uint32 i;
  2672. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  2673. error_buf_size))) {
  2674. return false;
  2675. }
  2676. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  2677. /* process argv[0], trip the path and suffix, only keep the program name
  2678. */
  2679. if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
  2680. &argv_buf_size)) {
  2681. set_error_buf(error_buf, error_buf_size,
  2682. "Init wasi environment failed: allocate memory failed");
  2683. goto fail;
  2684. }
  2685. if (!copy_string_array(env, env_count, &env_buf, &env_list,
  2686. &env_buf_size)) {
  2687. set_error_buf(error_buf, error_buf_size,
  2688. "Init wasi environment failed: allocate memory failed");
  2689. goto fail;
  2690. }
  2691. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  2692. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  2693. || !(argv_environ =
  2694. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  2695. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  2696. set_error_buf(error_buf, error_buf_size,
  2697. "Init wasi environment failed: allocate memory failed");
  2698. goto fail;
  2699. }
  2700. if (!fd_table_init(curfds)) {
  2701. set_error_buf(error_buf, error_buf_size,
  2702. "Init wasi environment failed: "
  2703. "init fd table failed");
  2704. goto fail;
  2705. }
  2706. fd_table_inited = true;
  2707. if (!fd_prestats_init(prestats)) {
  2708. set_error_buf(error_buf, error_buf_size,
  2709. "Init wasi environment failed: "
  2710. "init fd prestats failed");
  2711. goto fail;
  2712. }
  2713. fd_prestats_inited = true;
  2714. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  2715. argc, env_buf, env_buf_size, env_list, env_count)) {
  2716. set_error_buf(error_buf, error_buf_size,
  2717. "Init wasi environment failed: "
  2718. "init argument environment failed");
  2719. goto fail;
  2720. }
  2721. argv_environ_inited = true;
  2722. if (!addr_pool_init(apool)) {
  2723. set_error_buf(error_buf, error_buf_size,
  2724. "Init wasi environment failed: "
  2725. "init the address pool failed");
  2726. goto fail;
  2727. }
  2728. addr_pool_inited = true;
  2729. os_file_handle stdin_file_handle = os_convert_stdin_handle(stdinfd);
  2730. os_file_handle stdout_file_handle = os_convert_stdout_handle(stdoutfd);
  2731. os_file_handle stderr_file_handle = os_convert_stderr_handle(stderrfd);
  2732. if (!os_is_handle_valid(&stdin_file_handle)
  2733. || !os_is_handle_valid(&stdout_file_handle)
  2734. || !os_is_handle_valid(&stderr_file_handle))
  2735. goto fail;
  2736. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  2737. if (!fd_table_insert_existing(curfds, 0, stdin_file_handle, true)
  2738. || !fd_table_insert_existing(curfds, 1, stdout_file_handle, true)
  2739. || !fd_table_insert_existing(curfds, 2, stderr_file_handle, true)) {
  2740. set_error_buf(error_buf, error_buf_size,
  2741. "Init wasi environment failed: init fd table failed");
  2742. goto fail;
  2743. }
  2744. wasm_fd = 3;
  2745. for (i = 0; i < dir_count; i++, wasm_fd++) {
  2746. path = os_realpath(dir_list[i], resolved_path);
  2747. if (!path) {
  2748. if (error_buf)
  2749. snprintf(error_buf, error_buf_size,
  2750. "error while pre-opening directory %s: %d\n",
  2751. dir_list[i], errno);
  2752. goto fail;
  2753. }
  2754. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  2755. if (error != __WASI_ESUCCESS) {
  2756. if (error_buf)
  2757. snprintf(error_buf, error_buf_size,
  2758. "error while pre-opening directory %s: %d\n",
  2759. dir_list[i], error);
  2760. goto fail;
  2761. }
  2762. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)) {
  2763. if (error_buf)
  2764. snprintf(error_buf, error_buf_size,
  2765. "error inserting preopen fd %u (directory %s) into fd "
  2766. "table",
  2767. (unsigned int)wasm_fd, dir_list[i]);
  2768. goto fail;
  2769. }
  2770. if (!fd_prestats_insert(prestats, dir_list[i], wasm_fd)) {
  2771. if (error_buf)
  2772. snprintf(error_buf, error_buf_size,
  2773. "error inserting preopen fd %u (directory %s) into "
  2774. "prestats table",
  2775. (unsigned int)wasm_fd, dir_list[i]);
  2776. goto fail;
  2777. }
  2778. }
  2779. for (i = 0; i < map_dir_count; i++, wasm_fd++) {
  2780. char mapping_copy_buf[256];
  2781. char *mapping_copy = mapping_copy_buf;
  2782. char *map_mapped = NULL, *map_host = NULL;
  2783. const unsigned long max_len = strlen(map_dir_list[i]) * 2 + 3;
  2784. /* Allocation limit for runtime environments with reduced stack size */
  2785. if (max_len > 256) {
  2786. if (!(mapping_copy = wasm_runtime_malloc(max_len))) {
  2787. snprintf(error_buf, error_buf_size,
  2788. "error while allocating for directory mapping\n");
  2789. goto fail;
  2790. }
  2791. }
  2792. bh_memcpy_s(mapping_copy, max_len, map_dir_list[i],
  2793. (uint32)(strlen(map_dir_list[i]) + 1));
  2794. map_mapped = strtok(mapping_copy, "::");
  2795. map_host = strtok(NULL, "::");
  2796. if (!map_mapped || !map_host) {
  2797. if (error_buf)
  2798. snprintf(error_buf, error_buf_size,
  2799. "error while pre-opening mapped directory: "
  2800. "invalid map\n");
  2801. if (mapping_copy != mapping_copy_buf)
  2802. wasm_runtime_free(mapping_copy);
  2803. goto fail;
  2804. }
  2805. path = os_realpath(map_host, resolved_path);
  2806. if (!path) {
  2807. if (error_buf)
  2808. snprintf(error_buf, error_buf_size,
  2809. "error while pre-opening mapped directory %s: %d\n",
  2810. map_host, errno);
  2811. if (mapping_copy != mapping_copy_buf)
  2812. wasm_runtime_free(mapping_copy);
  2813. goto fail;
  2814. }
  2815. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  2816. if (error != __WASI_ESUCCESS) {
  2817. if (error_buf)
  2818. snprintf(error_buf, error_buf_size,
  2819. "error while pre-opening mapped directory %s: %d\n",
  2820. map_host, errno);
  2821. if (mapping_copy != mapping_copy_buf)
  2822. wasm_runtime_free(mapping_copy);
  2823. goto fail;
  2824. }
  2825. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)
  2826. || !fd_prestats_insert(prestats, map_mapped, wasm_fd)) {
  2827. if (error_buf)
  2828. snprintf(error_buf, error_buf_size,
  2829. "error while pre-opening mapped directory %s: "
  2830. "insertion failed\n",
  2831. dir_list[i]);
  2832. if (mapping_copy != mapping_copy_buf)
  2833. wasm_runtime_free(mapping_copy);
  2834. goto fail;
  2835. }
  2836. if (mapping_copy != mapping_copy_buf)
  2837. wasm_runtime_free(mapping_copy);
  2838. }
  2839. /* addr_pool(textual) -> apool */
  2840. for (i = 0; i < addr_pool_size; i++) {
  2841. char *cp, *address, *mask;
  2842. bool ret = false;
  2843. cp = bh_strdup(addr_pool[i]);
  2844. if (!cp) {
  2845. set_error_buf(error_buf, error_buf_size,
  2846. "Init wasi environment failed: copy address failed");
  2847. goto fail;
  2848. }
  2849. address = strtok(cp, "/");
  2850. mask = strtok(NULL, "/");
  2851. ret = addr_pool_insert(apool, address, (uint8)(mask ? atoi(mask) : 0));
  2852. wasm_runtime_free(cp);
  2853. if (!ret) {
  2854. set_error_buf(error_buf, error_buf_size,
  2855. "Init wasi environment failed: store address failed");
  2856. goto fail;
  2857. }
  2858. }
  2859. if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
  2860. &ns_lookup_list, NULL)) {
  2861. set_error_buf(error_buf, error_buf_size,
  2862. "Init wasi environment failed: allocate memory failed");
  2863. goto fail;
  2864. }
  2865. wasi_ctx->curfds = curfds;
  2866. wasi_ctx->prestats = prestats;
  2867. wasi_ctx->argv_environ = argv_environ;
  2868. wasi_ctx->addr_pool = apool;
  2869. wasi_ctx->argv_buf = argv_buf;
  2870. wasi_ctx->argv_list = argv_list;
  2871. wasi_ctx->env_buf = env_buf;
  2872. wasi_ctx->env_list = env_list;
  2873. wasi_ctx->ns_lookup_buf = ns_lookup_buf;
  2874. wasi_ctx->ns_lookup_list = ns_lookup_list;
  2875. return true;
  2876. fail:
  2877. if (argv_environ_inited)
  2878. argv_environ_destroy(argv_environ);
  2879. if (fd_prestats_inited)
  2880. fd_prestats_destroy(prestats);
  2881. if (fd_table_inited)
  2882. fd_table_destroy(curfds);
  2883. if (addr_pool_inited)
  2884. addr_pool_destroy(apool);
  2885. if (curfds)
  2886. wasm_runtime_free(curfds);
  2887. if (prestats)
  2888. wasm_runtime_free(prestats);
  2889. if (argv_environ)
  2890. wasm_runtime_free(argv_environ);
  2891. if (apool)
  2892. wasm_runtime_free(apool);
  2893. if (argv_buf)
  2894. wasm_runtime_free(argv_buf);
  2895. if (argv_list)
  2896. wasm_runtime_free(argv_list);
  2897. if (env_buf)
  2898. wasm_runtime_free(env_buf);
  2899. if (env_list)
  2900. wasm_runtime_free(env_list);
  2901. if (ns_lookup_buf)
  2902. wasm_runtime_free(ns_lookup_buf);
  2903. if (ns_lookup_list)
  2904. wasm_runtime_free(ns_lookup_list);
  2905. return false;
  2906. }
  2907. #else /* else of WASM_ENABLE_UVWASI == 0 */
  2908. static void *
  2909. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  2910. {
  2911. return runtime_malloc(size, NULL, NULL, 0);
  2912. (void)mem_user_data;
  2913. }
  2914. static void
  2915. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  2916. {
  2917. if (ptr)
  2918. wasm_runtime_free(ptr);
  2919. (void)mem_user_data;
  2920. }
  2921. static void *
  2922. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  2923. {
  2924. uint64 total_size = (uint64)nmemb * size;
  2925. return runtime_malloc(total_size, NULL, NULL, 0);
  2926. (void)mem_user_data;
  2927. }
  2928. static void *
  2929. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  2930. {
  2931. if (size >= UINT32_MAX) {
  2932. return NULL;
  2933. }
  2934. return wasm_runtime_realloc(ptr, (uint32)size);
  2935. }
  2936. /* clang-format off */
  2937. static uvwasi_mem_t uvwasi_allocator = {
  2938. .mem_user_data = 0,
  2939. .malloc = wasm_uvwasi_malloc,
  2940. .free = wasm_uvwasi_free,
  2941. .calloc = wasm_uvwasi_calloc,
  2942. .realloc = wasm_uvwasi_realloc
  2943. };
  2944. /* clang-format on */
  2945. bool
  2946. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2947. const char *dir_list[], uint32 dir_count,
  2948. const char *map_dir_list[], uint32 map_dir_count,
  2949. const char *env[], uint32 env_count,
  2950. const char *addr_pool[], uint32 addr_pool_size,
  2951. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  2952. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  2953. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  2954. char *error_buf, uint32 error_buf_size)
  2955. {
  2956. WASIContext *ctx;
  2957. uvwasi_t *uvwasi;
  2958. uvwasi_options_t init_options;
  2959. const char **envp = NULL;
  2960. uint64 total_size;
  2961. uint32 i;
  2962. bool ret = false;
  2963. ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
  2964. if (!ctx)
  2965. return false;
  2966. uvwasi = &ctx->uvwasi;
  2967. /* Setup the initialization options */
  2968. uvwasi_options_init(&init_options);
  2969. init_options.allocator = &uvwasi_allocator;
  2970. init_options.argc = argc;
  2971. init_options.argv = (const char **)argv;
  2972. init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
  2973. init_options.out =
  2974. (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
  2975. init_options.err =
  2976. (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
  2977. if (dir_count > 0) {
  2978. init_options.preopenc = dir_count;
  2979. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  2980. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  2981. total_size, module_inst, error_buf, error_buf_size);
  2982. if (init_options.preopens == NULL)
  2983. goto fail;
  2984. for (i = 0; i < init_options.preopenc; i++) {
  2985. init_options.preopens[i].real_path = dir_list[i];
  2986. init_options.preopens[i].mapped_path =
  2987. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  2988. }
  2989. }
  2990. if (env_count > 0) {
  2991. total_size = sizeof(char *) * (uint64)(env_count + 1);
  2992. envp =
  2993. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  2994. if (envp == NULL)
  2995. goto fail;
  2996. for (i = 0; i < env_count; i++) {
  2997. envp[i] = env[i];
  2998. }
  2999. envp[env_count] = NULL;
  3000. init_options.envp = envp;
  3001. }
  3002. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  3003. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  3004. goto fail;
  3005. }
  3006. wasm_runtime_set_wasi_ctx(module_inst, ctx);
  3007. ret = true;
  3008. fail:
  3009. if (envp)
  3010. wasm_runtime_free((void *)envp);
  3011. if (init_options.preopens)
  3012. wasm_runtime_free(init_options.preopens);
  3013. if (!ret && uvwasi)
  3014. wasm_runtime_free(uvwasi);
  3015. return ret;
  3016. }
  3017. #endif /* end of WASM_ENABLE_UVWASI */
  3018. bool
  3019. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  3020. {
  3021. #if WASM_ENABLE_INTERP != 0
  3022. if (module_inst->module_type == Wasm_Module_Bytecode
  3023. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  3024. return true;
  3025. #endif
  3026. #if WASM_ENABLE_AOT != 0
  3027. if (module_inst->module_type == Wasm_Module_AoT
  3028. && ((AOTModule *)((AOTModuleInstance *)module_inst)->module)
  3029. ->import_wasi_api)
  3030. return true;
  3031. #endif
  3032. return false;
  3033. }
  3034. WASMFunctionInstanceCommon *
  3035. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  3036. {
  3037. uint32 i;
  3038. #if WASM_ENABLE_INTERP != 0
  3039. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3040. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  3041. WASMFunctionInstance *func;
  3042. for (i = 0; i < wasm_inst->export_func_count; i++) {
  3043. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  3044. func = wasm_inst->export_functions[i].function;
  3045. if (func->u.func->func_type->param_count != 0
  3046. || func->u.func->func_type->result_count != 0) {
  3047. LOG_ERROR("Lookup wasi _start function failed: "
  3048. "invalid function type.\n");
  3049. return NULL;
  3050. }
  3051. return (WASMFunctionInstanceCommon *)func;
  3052. }
  3053. }
  3054. return NULL;
  3055. }
  3056. #endif
  3057. #if WASM_ENABLE_AOT != 0
  3058. if (module_inst->module_type == Wasm_Module_AoT) {
  3059. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  3060. AOTFunctionInstance *export_funcs =
  3061. (AOTFunctionInstance *)aot_inst->export_functions;
  3062. for (i = 0; i < aot_inst->export_func_count; i++) {
  3063. if (!strcmp(export_funcs[i].func_name, "_start")) {
  3064. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  3065. if (func_type->param_count != 0
  3066. || func_type->result_count != 0) {
  3067. LOG_ERROR("Lookup wasi _start function failed: "
  3068. "invalid function type.\n");
  3069. return NULL;
  3070. }
  3071. return (WASMFunctionInstanceCommon *)&export_funcs[i];
  3072. }
  3073. }
  3074. return NULL;
  3075. }
  3076. #endif /* end of WASM_ENABLE_AOT */
  3077. return NULL;
  3078. }
  3079. #if WASM_ENABLE_UVWASI == 0
  3080. void
  3081. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3082. {
  3083. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3084. if (wasi_ctx) {
  3085. if (wasi_ctx->argv_environ) {
  3086. argv_environ_destroy(wasi_ctx->argv_environ);
  3087. wasm_runtime_free(wasi_ctx->argv_environ);
  3088. }
  3089. if (wasi_ctx->curfds) {
  3090. fd_table_destroy(wasi_ctx->curfds);
  3091. wasm_runtime_free(wasi_ctx->curfds);
  3092. }
  3093. if (wasi_ctx->prestats) {
  3094. fd_prestats_destroy(wasi_ctx->prestats);
  3095. wasm_runtime_free(wasi_ctx->prestats);
  3096. }
  3097. if (wasi_ctx->addr_pool) {
  3098. addr_pool_destroy(wasi_ctx->addr_pool);
  3099. wasm_runtime_free(wasi_ctx->addr_pool);
  3100. }
  3101. if (wasi_ctx->argv_buf)
  3102. wasm_runtime_free(wasi_ctx->argv_buf);
  3103. if (wasi_ctx->argv_list)
  3104. wasm_runtime_free(wasi_ctx->argv_list);
  3105. if (wasi_ctx->env_buf)
  3106. wasm_runtime_free(wasi_ctx->env_buf);
  3107. if (wasi_ctx->env_list)
  3108. wasm_runtime_free(wasi_ctx->env_list);
  3109. if (wasi_ctx->ns_lookup_buf)
  3110. wasm_runtime_free(wasi_ctx->ns_lookup_buf);
  3111. if (wasi_ctx->ns_lookup_list)
  3112. wasm_runtime_free(wasi_ctx->ns_lookup_list);
  3113. wasm_runtime_free(wasi_ctx);
  3114. }
  3115. }
  3116. #else
  3117. void
  3118. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3119. {
  3120. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3121. if (wasi_ctx) {
  3122. uvwasi_destroy(&wasi_ctx->uvwasi);
  3123. wasm_runtime_free(wasi_ctx);
  3124. }
  3125. }
  3126. #endif
  3127. uint32_t
  3128. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst)
  3129. {
  3130. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3131. #if WASM_ENABLE_THREAD_MGR != 0
  3132. WASMCluster *cluster;
  3133. WASMExecEnv *exec_env;
  3134. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  3135. if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
  3136. /**
  3137. * The main thread may exit earlier than other threads, and
  3138. * the exit_code of wasi_ctx may be changed by other thread
  3139. * when it runs into wasi_proc_exit, here we wait until all
  3140. * other threads exit to avoid getting invalid exit_code.
  3141. */
  3142. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  3143. }
  3144. #endif
  3145. return wasi_ctx->exit_code;
  3146. }
  3147. #endif /* end of WASM_ENABLE_LIBC_WASI */
  3148. WASMModuleCommon *
  3149. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  3150. {
  3151. WASMModuleInstanceCommon *module_inst_comm =
  3152. wasm_runtime_get_module_inst(exec_env);
  3153. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  3154. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  3155. || module_inst_comm->module_type == Wasm_Module_AoT);
  3156. return (WASMModuleCommon *)module_inst->module;
  3157. }
  3158. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3159. const uint8 *
  3160. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  3161. const char *name, uint32 *len)
  3162. {
  3163. #if WASM_ENABLE_INTERP != 0
  3164. if (module_comm->module_type == Wasm_Module_Bytecode)
  3165. return wasm_loader_get_custom_section((WASMModule *)module_comm, name,
  3166. len);
  3167. #endif
  3168. #if WASM_ENABLE_AOT != 0
  3169. if (module_comm->module_type == Wasm_Module_AoT)
  3170. return aot_get_custom_section((AOTModule *)module_comm, name, len);
  3171. #endif
  3172. return NULL;
  3173. }
  3174. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  3175. static union {
  3176. int a;
  3177. char b;
  3178. } __ue = { .a = 1 };
  3179. #define is_little_endian() (__ue.b == 1) /* NOLINT */
  3180. bool
  3181. wasm_runtime_register_natives(const char *module_name,
  3182. NativeSymbol *native_symbols,
  3183. uint32 n_native_symbols)
  3184. {
  3185. return wasm_native_register_natives(module_name, native_symbols,
  3186. n_native_symbols);
  3187. }
  3188. bool
  3189. wasm_runtime_register_natives_raw(const char *module_name,
  3190. NativeSymbol *native_symbols,
  3191. uint32 n_native_symbols)
  3192. {
  3193. return wasm_native_register_natives_raw(module_name, native_symbols,
  3194. n_native_symbols);
  3195. }
  3196. bool
  3197. wasm_runtime_unregister_natives(const char *module_name,
  3198. NativeSymbol *native_symbols)
  3199. {
  3200. return wasm_native_unregister_natives(module_name, native_symbols);
  3201. }
  3202. bool
  3203. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  3204. const WASMFuncType *func_type,
  3205. const char *signature, void *attachment,
  3206. uint32 *argv, uint32 argc, uint32 *argv_ret)
  3207. {
  3208. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3209. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  3210. NativeRawFuncPtr invoke_native_raw = (NativeRawFuncPtr)func_ptr;
  3211. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  3212. uint32 *argv_src = argv, i, argc1, ptr_len;
  3213. uint32 arg_i32;
  3214. bool ret = false;
  3215. argc1 = func_type->param_count;
  3216. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3217. size = sizeof(uint64) * (uint64)argc1;
  3218. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3219. 0))) {
  3220. return false;
  3221. }
  3222. }
  3223. argv_dst = argv1;
  3224. /* Traverse secondly to fill in each argument */
  3225. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  3226. switch (func_type->types[i]) {
  3227. case VALUE_TYPE_I32:
  3228. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3229. case VALUE_TYPE_FUNCREF:
  3230. #endif
  3231. {
  3232. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  3233. if (signature) {
  3234. if (signature[i + 1] == '*') {
  3235. /* param is a pointer */
  3236. if (signature[i + 2] == '~')
  3237. /* pointer with length followed */
  3238. ptr_len = *argv_src;
  3239. else
  3240. /* pointer without length followed */
  3241. ptr_len = 1;
  3242. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3243. ptr_len))
  3244. goto fail;
  3245. *(uintptr_t *)argv_dst =
  3246. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  3247. arg_i32);
  3248. }
  3249. else if (signature[i + 1] == '$') {
  3250. /* param is a string */
  3251. if (!wasm_runtime_validate_app_str_addr(module,
  3252. arg_i32))
  3253. goto fail;
  3254. *(uintptr_t *)argv_dst =
  3255. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  3256. arg_i32);
  3257. }
  3258. }
  3259. break;
  3260. }
  3261. case VALUE_TYPE_I64:
  3262. case VALUE_TYPE_F64:
  3263. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  3264. sizeof(uint32) * 2);
  3265. argv_src += 2;
  3266. break;
  3267. case VALUE_TYPE_F32:
  3268. *(float32 *)argv_dst = *(float32 *)argv_src++;
  3269. break;
  3270. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3271. case VALUE_TYPE_EXTERNREF:
  3272. {
  3273. uint32 externref_idx = *argv_src++;
  3274. void *externref_obj;
  3275. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3276. goto fail;
  3277. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  3278. sizeof(uintptr_t));
  3279. break;
  3280. }
  3281. #endif
  3282. #if WASM_ENABLE_GC != 0
  3283. case REF_TYPE_FUNCREF:
  3284. case REF_TYPE_EXTERNREF:
  3285. case REF_TYPE_ANYREF:
  3286. case REF_TYPE_EQREF:
  3287. case REF_TYPE_HT_NULLABLE:
  3288. case REF_TYPE_HT_NON_NULLABLE:
  3289. case REF_TYPE_I31REF:
  3290. case REF_TYPE_NULLFUNCREF:
  3291. case REF_TYPE_NULLEXTERNREF:
  3292. case REF_TYPE_STRUCTREF:
  3293. case REF_TYPE_ARRAYREF:
  3294. case REF_TYPE_NULLREF:
  3295. #if WASM_ENABLE_STRINGREF != 0
  3296. case REF_TYPE_STRINGREF:
  3297. case REF_TYPE_STRINGVIEWWTF8:
  3298. case REF_TYPE_STRINGVIEWWTF16:
  3299. case REF_TYPE_STRINGVIEWITER:
  3300. #endif
  3301. {
  3302. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  3303. sizeof(uintptr_t));
  3304. argv_src += sizeof(uintptr_t) / sizeof(uint32);
  3305. break;
  3306. }
  3307. #endif
  3308. default:
  3309. bh_assert(0);
  3310. break;
  3311. }
  3312. }
  3313. exec_env->attachment = attachment;
  3314. invoke_native_raw(exec_env, argv1);
  3315. exec_env->attachment = NULL;
  3316. if (func_type->result_count > 0) {
  3317. switch (func_type->types[func_type->param_count]) {
  3318. case VALUE_TYPE_I32:
  3319. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3320. case VALUE_TYPE_FUNCREF:
  3321. #endif
  3322. argv_ret[0] = *(uint32 *)argv1;
  3323. break;
  3324. case VALUE_TYPE_F32:
  3325. *(float32 *)argv_ret = *(float32 *)argv1;
  3326. break;
  3327. case VALUE_TYPE_I64:
  3328. case VALUE_TYPE_F64:
  3329. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  3330. sizeof(uint64));
  3331. break;
  3332. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3333. case VALUE_TYPE_EXTERNREF:
  3334. {
  3335. uint32 externref_idx;
  3336. uint64 externref_obj;
  3337. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  3338. sizeof(uint64));
  3339. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3340. (void *)(uintptr_t)externref_obj,
  3341. &externref_idx))
  3342. goto fail;
  3343. argv_ret[0] = externref_idx;
  3344. break;
  3345. }
  3346. #endif
  3347. #if WASM_ENABLE_GC != 0
  3348. case REF_TYPE_FUNCREF:
  3349. case REF_TYPE_EXTERNREF:
  3350. case REF_TYPE_ANYREF:
  3351. case REF_TYPE_EQREF:
  3352. case REF_TYPE_HT_NULLABLE:
  3353. case REF_TYPE_HT_NON_NULLABLE:
  3354. case REF_TYPE_I31REF:
  3355. case REF_TYPE_NULLFUNCREF:
  3356. case REF_TYPE_NULLEXTERNREF:
  3357. case REF_TYPE_STRUCTREF:
  3358. case REF_TYPE_ARRAYREF:
  3359. case REF_TYPE_NULLREF:
  3360. #if WASM_ENABLE_STRINGREF != 0
  3361. case REF_TYPE_STRINGREF:
  3362. case REF_TYPE_STRINGVIEWWTF8:
  3363. case REF_TYPE_STRINGVIEWWTF16:
  3364. case REF_TYPE_STRINGVIEWITER:
  3365. #endif
  3366. {
  3367. bh_memcpy_s(argv_ret, sizeof(uintptr_t), argv1,
  3368. sizeof(uintptr_t));
  3369. break;
  3370. }
  3371. #endif
  3372. default:
  3373. bh_assert(0);
  3374. break;
  3375. }
  3376. }
  3377. ret = !wasm_runtime_copy_exception(module, NULL);
  3378. fail:
  3379. if (argv1 != argv_buf)
  3380. wasm_runtime_free(argv1);
  3381. return ret;
  3382. }
  3383. /**
  3384. * Implementation of wasm_runtime_invoke_native()
  3385. */
  3386. /* The invoke native implementation on ARM platform with VFP co-processor */
  3387. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  3388. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3389. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3390. typedef void (*GenericFunctionPointer)();
  3391. void
  3392. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  3393. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3394. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3395. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3396. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3397. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  3398. static volatile Float64FuncPtr invokeNative_Float64 =
  3399. (Float64FuncPtr)(uintptr_t)invokeNative;
  3400. static volatile Float32FuncPtr invokeNative_Float32 =
  3401. (Float32FuncPtr)(uintptr_t)invokeNative;
  3402. static volatile Int64FuncPtr invokeNative_Int64 =
  3403. (Int64FuncPtr)(uintptr_t)invokeNative;
  3404. static volatile Int32FuncPtr invokeNative_Int32 =
  3405. (Int32FuncPtr)(uintptr_t)invokeNative;
  3406. static volatile VoidFuncPtr invokeNative_Void =
  3407. (VoidFuncPtr)(uintptr_t)invokeNative;
  3408. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3409. #define MAX_REG_INTS 4
  3410. #define MAX_REG_FLOATS 16
  3411. #else
  3412. #define MAX_REG_INTS 8
  3413. #define MAX_REG_FLOATS 8
  3414. #endif
  3415. bool
  3416. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3417. const WASMFuncType *func_type, const char *signature,
  3418. void *attachment, uint32 *argv, uint32 argc,
  3419. uint32 *argv_ret)
  3420. {
  3421. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3422. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args
  3423. */
  3424. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  3425. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3426. uint32 arg_i32, ptr_len;
  3427. uint32 result_count = func_type->result_count;
  3428. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3429. bool ret = false;
  3430. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3431. bool is_aot_func = (NULL == signature);
  3432. #endif
  3433. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3434. uint32 *fps;
  3435. int n_fps = 0;
  3436. #else
  3437. #define fps ints
  3438. #define n_fps n_ints
  3439. #endif
  3440. n_ints++; /* exec env */
  3441. /* Traverse firstly to calculate stack args count */
  3442. for (i = 0; i < func_type->param_count; i++) {
  3443. switch (func_type->types[i]) {
  3444. case VALUE_TYPE_I32:
  3445. #if WASM_ENABLE_GC != 0
  3446. case REF_TYPE_FUNCREF:
  3447. case REF_TYPE_EXTERNREF:
  3448. case REF_TYPE_ANYREF:
  3449. case REF_TYPE_EQREF:
  3450. case REF_TYPE_HT_NULLABLE:
  3451. case REF_TYPE_HT_NON_NULLABLE:
  3452. case REF_TYPE_I31REF:
  3453. case REF_TYPE_NULLFUNCREF:
  3454. case REF_TYPE_NULLEXTERNREF:
  3455. case REF_TYPE_STRUCTREF:
  3456. case REF_TYPE_ARRAYREF:
  3457. case REF_TYPE_NULLREF:
  3458. #if WASM_ENABLE_STRINGREF != 0
  3459. case REF_TYPE_STRINGREF:
  3460. case REF_TYPE_STRINGVIEWWTF8:
  3461. case REF_TYPE_STRINGVIEWWTF16:
  3462. case REF_TYPE_STRINGVIEWITER:
  3463. #endif
  3464. #endif
  3465. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3466. case VALUE_TYPE_FUNCREF:
  3467. case VALUE_TYPE_EXTERNREF:
  3468. #endif
  3469. if (n_ints < MAX_REG_INTS)
  3470. n_ints++;
  3471. else
  3472. n_stacks++;
  3473. break;
  3474. case VALUE_TYPE_I64:
  3475. if (n_ints < MAX_REG_INTS - 1) {
  3476. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3477. /* 64-bit data must be 8 bytes aligned in arm */
  3478. if (n_ints & 1)
  3479. n_ints++;
  3480. #endif
  3481. n_ints += 2;
  3482. }
  3483. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  3484. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  3485. /* part in register, part in stack */
  3486. else if (n_ints == MAX_REG_INTS - 1) {
  3487. n_ints++;
  3488. n_stacks++;
  3489. }
  3490. #endif
  3491. else {
  3492. /* 64-bit data in stack must be 8 bytes aligned
  3493. in arm and riscv32 */
  3494. #if !defined(BUILD_TARGET_ARC)
  3495. if (n_stacks & 1)
  3496. n_stacks++;
  3497. #endif
  3498. n_stacks += 2;
  3499. }
  3500. break;
  3501. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  3502. case VALUE_TYPE_F32:
  3503. if (n_fps < MAX_REG_FLOATS)
  3504. n_fps++;
  3505. else
  3506. n_stacks++;
  3507. break;
  3508. case VALUE_TYPE_F64:
  3509. if (n_fps < MAX_REG_FLOATS - 1) {
  3510. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3511. /* 64-bit data must be 8 bytes aligned in arm */
  3512. if (n_fps & 1)
  3513. n_fps++;
  3514. #endif
  3515. n_fps += 2;
  3516. }
  3517. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3518. else if (n_fps == MAX_REG_FLOATS - 1) {
  3519. n_fps++;
  3520. n_stacks++;
  3521. }
  3522. #endif
  3523. else {
  3524. /* 64-bit data in stack must be 8 bytes aligned
  3525. in arm and riscv32 */
  3526. #if !defined(BUILD_TARGET_ARC)
  3527. if (n_stacks & 1)
  3528. n_stacks++;
  3529. #endif
  3530. n_stacks += 2;
  3531. }
  3532. break;
  3533. #else /* BUILD_TARGET_RISCV32_ILP32D */
  3534. case VALUE_TYPE_F32:
  3535. case VALUE_TYPE_F64:
  3536. if (n_fps < MAX_REG_FLOATS) {
  3537. n_fps++;
  3538. }
  3539. else if (func_type->types[i] == VALUE_TYPE_F32
  3540. && n_ints < MAX_REG_INTS) {
  3541. /* use int reg firstly if available */
  3542. n_ints++;
  3543. }
  3544. else if (func_type->types[i] == VALUE_TYPE_F64
  3545. && n_ints < MAX_REG_INTS - 1) {
  3546. /* use int regs firstly if available */
  3547. if (n_ints & 1)
  3548. n_ints++;
  3549. ints += 2;
  3550. }
  3551. else {
  3552. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  3553. */
  3554. if (n_stacks & 1)
  3555. n_stacks++;
  3556. n_stacks += 2;
  3557. }
  3558. break;
  3559. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3560. default:
  3561. bh_assert(0);
  3562. break;
  3563. }
  3564. }
  3565. for (i = 0; i < ext_ret_count; i++) {
  3566. if (n_ints < MAX_REG_INTS)
  3567. n_ints++;
  3568. else
  3569. n_stacks++;
  3570. }
  3571. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3572. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  3573. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3574. argc1 = MAX_REG_INTS + n_stacks;
  3575. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  3576. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  3577. #endif
  3578. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3579. size = sizeof(uint32) * (uint32)argc1;
  3580. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3581. 0))) {
  3582. return false;
  3583. }
  3584. }
  3585. ints = argv1;
  3586. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3587. fps = ints + MAX_REG_INTS;
  3588. stacks = fps + MAX_REG_FLOATS;
  3589. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3590. stacks = ints + MAX_REG_INTS;
  3591. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  3592. fps = ints + MAX_REG_INTS;
  3593. stacks = fps + MAX_REG_FLOATS * 2;
  3594. #endif
  3595. n_ints = 0;
  3596. n_fps = 0;
  3597. n_stacks = 0;
  3598. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  3599. /* Traverse secondly to fill in each argument */
  3600. for (i = 0; i < func_type->param_count; i++) {
  3601. switch (func_type->types[i]) {
  3602. case VALUE_TYPE_I32:
  3603. #if WASM_ENABLE_GC != 0
  3604. case REF_TYPE_FUNCREF:
  3605. case REF_TYPE_EXTERNREF:
  3606. case REF_TYPE_ANYREF:
  3607. case REF_TYPE_EQREF:
  3608. case REF_TYPE_HT_NULLABLE:
  3609. case REF_TYPE_HT_NON_NULLABLE:
  3610. case REF_TYPE_I31REF:
  3611. case REF_TYPE_NULLFUNCREF:
  3612. case REF_TYPE_NULLEXTERNREF:
  3613. case REF_TYPE_STRUCTREF:
  3614. case REF_TYPE_ARRAYREF:
  3615. case REF_TYPE_NULLREF:
  3616. #if WASM_ENABLE_STRINGREF != 0
  3617. case REF_TYPE_STRINGREF:
  3618. case REF_TYPE_STRINGVIEWWTF8:
  3619. case REF_TYPE_STRINGVIEWWTF16:
  3620. case REF_TYPE_STRINGVIEWITER:
  3621. #endif
  3622. #endif
  3623. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3624. case VALUE_TYPE_FUNCREF:
  3625. #endif
  3626. {
  3627. arg_i32 = *argv_src++;
  3628. if (signature) {
  3629. if (signature[i + 1] == '*') {
  3630. /* param is a pointer */
  3631. if (signature[i + 2] == '~')
  3632. /* pointer with length followed */
  3633. ptr_len = *argv_src;
  3634. else
  3635. /* pointer without length followed */
  3636. ptr_len = 1;
  3637. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3638. ptr_len))
  3639. goto fail;
  3640. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3641. module, arg_i32);
  3642. }
  3643. else if (signature[i + 1] == '$') {
  3644. /* param is a string */
  3645. if (!wasm_runtime_validate_app_str_addr(module,
  3646. arg_i32))
  3647. goto fail;
  3648. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3649. module, arg_i32);
  3650. }
  3651. }
  3652. if (n_ints < MAX_REG_INTS)
  3653. ints[n_ints++] = arg_i32;
  3654. else
  3655. stacks[n_stacks++] = arg_i32;
  3656. break;
  3657. }
  3658. case VALUE_TYPE_I64:
  3659. {
  3660. if (n_ints < MAX_REG_INTS - 1) {
  3661. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3662. /* 64-bit data must be 8 bytes aligned in arm */
  3663. if (n_ints & 1)
  3664. n_ints++;
  3665. #endif
  3666. ints[n_ints++] = *argv_src++;
  3667. ints[n_ints++] = *argv_src++;
  3668. }
  3669. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  3670. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  3671. else if (n_ints == MAX_REG_INTS - 1) {
  3672. ints[n_ints++] = *argv_src++;
  3673. stacks[n_stacks++] = *argv_src++;
  3674. }
  3675. #endif
  3676. else {
  3677. /* 64-bit data in stack must be 8 bytes aligned
  3678. in arm and riscv32 */
  3679. #if !defined(BUILD_TARGET_ARC)
  3680. if (n_stacks & 1)
  3681. n_stacks++;
  3682. #endif
  3683. stacks[n_stacks++] = *argv_src++;
  3684. stacks[n_stacks++] = *argv_src++;
  3685. }
  3686. break;
  3687. }
  3688. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  3689. case VALUE_TYPE_F32:
  3690. {
  3691. if (n_fps < MAX_REG_FLOATS)
  3692. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  3693. else
  3694. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  3695. break;
  3696. }
  3697. case VALUE_TYPE_F64:
  3698. {
  3699. if (n_fps < MAX_REG_FLOATS - 1) {
  3700. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3701. /* 64-bit data must be 8 bytes aligned in arm */
  3702. if (n_fps & 1)
  3703. n_fps++;
  3704. #endif
  3705. fps[n_fps++] = *argv_src++;
  3706. fps[n_fps++] = *argv_src++;
  3707. }
  3708. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3709. else if (n_fps == MAX_REG_FLOATS - 1) {
  3710. fps[n_fps++] = *argv_src++;
  3711. stacks[n_stacks++] = *argv_src++;
  3712. }
  3713. #endif
  3714. else {
  3715. /* 64-bit data in stack must be 8 bytes aligned
  3716. in arm and riscv32 */
  3717. #if !defined(BUILD_TARGET_ARC)
  3718. if (n_stacks & 1)
  3719. n_stacks++;
  3720. #endif
  3721. stacks[n_stacks++] = *argv_src++;
  3722. stacks[n_stacks++] = *argv_src++;
  3723. }
  3724. break;
  3725. }
  3726. #else /* BUILD_TARGET_RISCV32_ILP32D */
  3727. case VALUE_TYPE_F32:
  3728. case VALUE_TYPE_F64:
  3729. {
  3730. if (n_fps < MAX_REG_FLOATS) {
  3731. if (func_type->types[i] == VALUE_TYPE_F32) {
  3732. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  3733. /* NaN boxing, the upper bits of a valid NaN-boxed
  3734. value must be all 1s. */
  3735. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  3736. }
  3737. else {
  3738. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  3739. argv_src += 2;
  3740. }
  3741. n_fps++;
  3742. }
  3743. else if (func_type->types[i] == VALUE_TYPE_F32
  3744. && n_ints < MAX_REG_INTS) {
  3745. /* use int reg firstly if available */
  3746. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  3747. }
  3748. else if (func_type->types[i] == VALUE_TYPE_F64
  3749. && n_ints < MAX_REG_INTS - 1) {
  3750. /* use int regs firstly if available */
  3751. if (n_ints & 1)
  3752. n_ints++;
  3753. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  3754. n_ints += 2;
  3755. argv_src += 2;
  3756. }
  3757. else {
  3758. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  3759. */
  3760. if (n_stacks & 1)
  3761. n_stacks++;
  3762. if (func_type->types[i] == VALUE_TYPE_F32) {
  3763. *(float32 *)&stacks[n_stacks++] =
  3764. *(float32 *)argv_src++;
  3765. }
  3766. else {
  3767. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  3768. argv_src += 2;
  3769. n_stacks += 2;
  3770. }
  3771. }
  3772. break;
  3773. }
  3774. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3775. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3776. case VALUE_TYPE_EXTERNREF:
  3777. {
  3778. uint32 externref_idx = *argv_src++;
  3779. if (is_aot_func) {
  3780. if (n_ints < MAX_REG_INTS)
  3781. ints[n_ints++] = externref_idx;
  3782. else
  3783. stacks[n_stacks++] = externref_idx;
  3784. }
  3785. else {
  3786. void *externref_obj;
  3787. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3788. goto fail;
  3789. if (n_ints < MAX_REG_INTS)
  3790. ints[n_ints++] = (uintptr_t)externref_obj;
  3791. else
  3792. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3793. }
  3794. break;
  3795. }
  3796. #endif
  3797. default:
  3798. bh_assert(0);
  3799. break;
  3800. }
  3801. }
  3802. /* Save extra result values' address to argv1 */
  3803. for (i = 0; i < ext_ret_count; i++) {
  3804. if (n_ints < MAX_REG_INTS)
  3805. ints[n_ints++] = *(uint32 *)argv_src++;
  3806. else
  3807. stacks[n_stacks++] = *(uint32 *)argv_src++;
  3808. }
  3809. exec_env->attachment = attachment;
  3810. if (func_type->result_count == 0) {
  3811. invokeNative_Void(func_ptr, argv1, n_stacks);
  3812. }
  3813. else {
  3814. switch (func_type->types[func_type->param_count]) {
  3815. case VALUE_TYPE_I32:
  3816. #if WASM_ENABLE_GC != 0
  3817. case REF_TYPE_FUNCREF:
  3818. case REF_TYPE_EXTERNREF:
  3819. case REF_TYPE_ANYREF:
  3820. case REF_TYPE_EQREF:
  3821. case REF_TYPE_HT_NULLABLE:
  3822. case REF_TYPE_HT_NON_NULLABLE:
  3823. case REF_TYPE_I31REF:
  3824. case REF_TYPE_NULLFUNCREF:
  3825. case REF_TYPE_NULLEXTERNREF:
  3826. case REF_TYPE_STRUCTREF:
  3827. case REF_TYPE_ARRAYREF:
  3828. case REF_TYPE_NULLREF:
  3829. #if WASM_ENABLE_STRINGREF != 0
  3830. case REF_TYPE_STRINGREF:
  3831. case REF_TYPE_STRINGVIEWWTF8:
  3832. case REF_TYPE_STRINGVIEWWTF16:
  3833. case REF_TYPE_STRINGVIEWITER:
  3834. #endif
  3835. #endif
  3836. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3837. case VALUE_TYPE_FUNCREF:
  3838. #endif
  3839. argv_ret[0] =
  3840. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3841. break;
  3842. case VALUE_TYPE_I64:
  3843. PUT_I64_TO_ADDR(argv_ret,
  3844. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3845. break;
  3846. case VALUE_TYPE_F32:
  3847. *(float32 *)argv_ret =
  3848. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3849. break;
  3850. case VALUE_TYPE_F64:
  3851. PUT_F64_TO_ADDR(
  3852. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3853. break;
  3854. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3855. case VALUE_TYPE_EXTERNREF:
  3856. {
  3857. if (is_aot_func) {
  3858. uint32 externref_idx =
  3859. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3860. argv_ret[0] = externref_idx;
  3861. }
  3862. else {
  3863. uint32 externref_idx;
  3864. void *externref_obj;
  3865. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3866. func_ptr, argv1, argc1);
  3867. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3868. externref_obj, &externref_idx))
  3869. goto fail;
  3870. argv_ret[0] = externref_idx;
  3871. }
  3872. break;
  3873. }
  3874. #endif
  3875. default:
  3876. bh_assert(0);
  3877. break;
  3878. }
  3879. }
  3880. exec_env->attachment = NULL;
  3881. ret = !wasm_runtime_copy_exception(module, NULL);
  3882. fail:
  3883. if (argv1 != argv_buf)
  3884. wasm_runtime_free(argv1);
  3885. return ret;
  3886. }
  3887. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  3888. || defined(BUILD_TARGET_THUMB_VFP) \
  3889. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3890. || defined(BUILD_TARGET_RISCV32_ILP32) \
  3891. || defined(BUILD_TARGET_ARC) */
  3892. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  3893. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  3894. || defined(BUILD_TARGET_XTENSA)
  3895. typedef void (*GenericFunctionPointer)();
  3896. void
  3897. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  3898. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3899. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3900. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3901. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3902. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3903. static volatile Int64FuncPtr invokeNative_Int64 =
  3904. (Int64FuncPtr)(uintptr_t)invokeNative;
  3905. static volatile Int32FuncPtr invokeNative_Int32 =
  3906. (Int32FuncPtr)(uintptr_t)invokeNative;
  3907. static volatile Float64FuncPtr invokeNative_Float64 =
  3908. (Float64FuncPtr)(uintptr_t)invokeNative;
  3909. static volatile Float32FuncPtr invokeNative_Float32 =
  3910. (Float32FuncPtr)(uintptr_t)invokeNative;
  3911. static volatile VoidFuncPtr invokeNative_Void =
  3912. (VoidFuncPtr)(uintptr_t)invokeNative;
  3913. static inline void
  3914. word_copy(uint32 *dest, uint32 *src, unsigned num)
  3915. {
  3916. for (; num > 0; num--)
  3917. *dest++ = *src++;
  3918. }
  3919. bool
  3920. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3921. const WASMFuncType *func_type, const char *signature,
  3922. void *attachment, uint32 *argv, uint32 argc,
  3923. uint32 *argv_ret)
  3924. {
  3925. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3926. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  3927. uint32 arg_i32, ptr_len;
  3928. uint32 result_count = func_type->result_count;
  3929. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3930. uint64 size;
  3931. bool ret = false;
  3932. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3933. bool is_aot_func = (NULL == signature);
  3934. #endif
  3935. #if defined(BUILD_TARGET_X86_32)
  3936. argc1 = argc + ext_ret_count + 2;
  3937. #else
  3938. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  3939. so we need to allocate more memory. */
  3940. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  3941. #endif
  3942. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3943. size = sizeof(uint32) * (uint64)argc1;
  3944. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3945. 0))) {
  3946. return false;
  3947. }
  3948. }
  3949. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  3950. argv1[j++] = ((uint32 *)&exec_env)[i];
  3951. for (i = 0; i < func_type->param_count; i++) {
  3952. switch (func_type->types[i]) {
  3953. case VALUE_TYPE_I32:
  3954. #if WASM_ENABLE_GC != 0
  3955. case REF_TYPE_FUNCREF:
  3956. case REF_TYPE_EXTERNREF:
  3957. case REF_TYPE_ANYREF:
  3958. case REF_TYPE_EQREF:
  3959. case REF_TYPE_HT_NULLABLE:
  3960. case REF_TYPE_HT_NON_NULLABLE:
  3961. case REF_TYPE_I31REF:
  3962. case REF_TYPE_NULLFUNCREF:
  3963. case REF_TYPE_NULLEXTERNREF:
  3964. case REF_TYPE_STRUCTREF:
  3965. case REF_TYPE_ARRAYREF:
  3966. case REF_TYPE_NULLREF:
  3967. #if WASM_ENABLE_STRINGREF != 0
  3968. case REF_TYPE_STRINGREF:
  3969. case REF_TYPE_STRINGVIEWWTF8:
  3970. case REF_TYPE_STRINGVIEWWTF16:
  3971. case REF_TYPE_STRINGVIEWITER:
  3972. #endif
  3973. #endif
  3974. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  3975. case VALUE_TYPE_FUNCREF:
  3976. #endif
  3977. {
  3978. arg_i32 = *argv++;
  3979. if (signature) {
  3980. if (signature[i + 1] == '*') {
  3981. /* param is a pointer */
  3982. if (signature[i + 2] == '~')
  3983. /* pointer with length followed */
  3984. ptr_len = *argv;
  3985. else
  3986. /* pointer without length followed */
  3987. ptr_len = 1;
  3988. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3989. ptr_len))
  3990. goto fail;
  3991. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3992. module, arg_i32);
  3993. }
  3994. else if (signature[i + 1] == '$') {
  3995. /* param is a string */
  3996. if (!wasm_runtime_validate_app_str_addr(module,
  3997. arg_i32))
  3998. goto fail;
  3999. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4000. module, arg_i32);
  4001. }
  4002. }
  4003. argv1[j++] = arg_i32;
  4004. break;
  4005. }
  4006. case VALUE_TYPE_I64:
  4007. case VALUE_TYPE_F64:
  4008. #if !defined(BUILD_TARGET_X86_32)
  4009. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  4010. and xtensa */
  4011. if (j & 1)
  4012. j++;
  4013. #endif
  4014. argv1[j++] = *argv++;
  4015. argv1[j++] = *argv++;
  4016. break;
  4017. case VALUE_TYPE_F32:
  4018. argv1[j++] = *argv++;
  4019. break;
  4020. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4021. case VALUE_TYPE_EXTERNREF:
  4022. {
  4023. uint32 externref_idx = *argv++;
  4024. if (is_aot_func) {
  4025. argv1[j++] = externref_idx;
  4026. }
  4027. else {
  4028. void *externref_obj;
  4029. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4030. goto fail;
  4031. argv1[j++] = (uintptr_t)externref_obj;
  4032. }
  4033. break;
  4034. }
  4035. #endif
  4036. default:
  4037. bh_assert(0);
  4038. break;
  4039. }
  4040. }
  4041. /* Save extra result values' address to argv1 */
  4042. word_copy(argv1 + j, argv, ext_ret_count);
  4043. argc1 = j + ext_ret_count;
  4044. exec_env->attachment = attachment;
  4045. if (func_type->result_count == 0) {
  4046. invokeNative_Void(func_ptr, argv1, argc1);
  4047. }
  4048. else {
  4049. switch (func_type->types[func_type->param_count]) {
  4050. case VALUE_TYPE_I32:
  4051. #if WASM_ENABLE_GC != 0
  4052. case REF_TYPE_FUNCREF:
  4053. case REF_TYPE_EXTERNREF:
  4054. case REF_TYPE_ANYREF:
  4055. case REF_TYPE_EQREF:
  4056. case REF_TYPE_HT_NULLABLE:
  4057. case REF_TYPE_HT_NON_NULLABLE:
  4058. case REF_TYPE_I31REF:
  4059. case REF_TYPE_NULLFUNCREF:
  4060. case REF_TYPE_NULLEXTERNREF:
  4061. case REF_TYPE_STRUCTREF:
  4062. case REF_TYPE_ARRAYREF:
  4063. case REF_TYPE_NULLREF:
  4064. #if WASM_ENABLE_STRINGREF != 0
  4065. case REF_TYPE_STRINGREF:
  4066. case REF_TYPE_STRINGVIEWWTF8:
  4067. case REF_TYPE_STRINGVIEWWTF16:
  4068. case REF_TYPE_STRINGVIEWITER:
  4069. #endif
  4070. #endif
  4071. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4072. case VALUE_TYPE_FUNCREF:
  4073. #endif
  4074. argv_ret[0] =
  4075. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  4076. break;
  4077. case VALUE_TYPE_I64:
  4078. PUT_I64_TO_ADDR(argv_ret,
  4079. invokeNative_Int64(func_ptr, argv1, argc1));
  4080. break;
  4081. case VALUE_TYPE_F32:
  4082. *(float32 *)argv_ret =
  4083. invokeNative_Float32(func_ptr, argv1, argc1);
  4084. break;
  4085. case VALUE_TYPE_F64:
  4086. PUT_F64_TO_ADDR(argv_ret,
  4087. invokeNative_Float64(func_ptr, argv1, argc1));
  4088. break;
  4089. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4090. case VALUE_TYPE_EXTERNREF:
  4091. {
  4092. if (is_aot_func) {
  4093. uint32 externref_idx =
  4094. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  4095. argv_ret[0] = externref_idx;
  4096. }
  4097. else {
  4098. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  4099. func_ptr, argv1, argc1);
  4100. uint32 externref_idx;
  4101. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4102. externref_obj, &externref_idx))
  4103. goto fail;
  4104. argv_ret[0] = externref_idx;
  4105. }
  4106. break;
  4107. }
  4108. #endif
  4109. default:
  4110. bh_assert(0);
  4111. break;
  4112. }
  4113. }
  4114. exec_env->attachment = NULL;
  4115. ret = !wasm_runtime_copy_exception(module, NULL);
  4116. fail:
  4117. if (argv1 != argv_buf)
  4118. wasm_runtime_free(argv1);
  4119. return ret;
  4120. }
  4121. #endif /* end of defined(BUILD_TARGET_X86_32) \
  4122. || defined(BUILD_TARGET_ARM) \
  4123. || defined(BUILD_TARGET_THUMB) \
  4124. || defined(BUILD_TARGET_MIPS) \
  4125. || defined(BUILD_TARGET_XTENSA) */
  4126. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  4127. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  4128. || defined(BUILD_TARGET_RISCV64_LP64)
  4129. #if WASM_ENABLE_SIMD != 0
  4130. #ifdef v128
  4131. #undef v128
  4132. #endif
  4133. #if defined(_WIN32) || defined(_WIN32_)
  4134. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  4135. __int8 m128i_i8[16];
  4136. __int16 m128i_i16[8];
  4137. __int32 m128i_i32[4];
  4138. __int64 m128i_i64[2];
  4139. unsigned __int8 m128i_u8[16];
  4140. unsigned __int16 m128i_u16[8];
  4141. unsigned __int32 m128i_u32[4];
  4142. unsigned __int64 m128i_u64[2];
  4143. } v128;
  4144. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  4145. || defined(BUILD_TARGET_RISCV64_LP64D) \
  4146. || defined(BUILD_TARGET_RISCV64_LP64)
  4147. typedef long long v128
  4148. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  4149. #elif defined(BUILD_TARGET_AARCH64)
  4150. #include <arm_neon.h>
  4151. typedef uint32x4_t __m128i;
  4152. #define v128 __m128i
  4153. #endif
  4154. #endif /* end of WASM_ENABLE_SIMD != 0 */
  4155. typedef void (*GenericFunctionPointer)();
  4156. void
  4157. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  4158. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4159. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4160. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4161. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4162. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4163. /* NOLINTBEGIN */
  4164. static volatile Float64FuncPtr invokeNative_Float64 =
  4165. (Float64FuncPtr)(uintptr_t)invokeNative;
  4166. static volatile Float32FuncPtr invokeNative_Float32 =
  4167. (Float32FuncPtr)(uintptr_t)invokeNative;
  4168. static volatile Int64FuncPtr invokeNative_Int64 =
  4169. (Int64FuncPtr)(uintptr_t)invokeNative;
  4170. static volatile Int32FuncPtr invokeNative_Int32 =
  4171. (Int32FuncPtr)(uintptr_t)invokeNative;
  4172. static volatile VoidFuncPtr invokeNative_Void =
  4173. (VoidFuncPtr)(uintptr_t)invokeNative;
  4174. #if WASM_ENABLE_SIMD != 0
  4175. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  4176. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  4177. #endif
  4178. /* NOLINTEND */
  4179. #if defined(_WIN32) || defined(_WIN32_)
  4180. #define MAX_REG_FLOATS 4
  4181. #define MAX_REG_INTS 4
  4182. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  4183. #define MAX_REG_FLOATS 8
  4184. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  4185. || defined(BUILD_TARGET_RISCV64_LP64)
  4186. #define MAX_REG_INTS 8
  4187. #else
  4188. #define MAX_REG_INTS 6
  4189. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  4190. || defined(BUILD_TARGET_RISCV64_LP64D) \
  4191. || defined(BUILD_TARGET_RISCV64_LP64) */
  4192. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  4193. /*
  4194. * ASAN is not designed to work with custom stack unwind or other low-level
  4195. * things. Ignore a function that does some low-level magic. (e.g. walking
  4196. * through the thread's stack bypassing the frame boundaries)
  4197. */
  4198. #if defined(__GNUC__) || defined(__clang__)
  4199. __attribute__((no_sanitize_address))
  4200. #endif
  4201. bool
  4202. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  4203. const WASMFuncType *func_type, const char *signature,
  4204. void *attachment, uint32 *argv, uint32 argc,
  4205. uint32 *argv_ret)
  4206. {
  4207. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4208. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  4209. arg_i64;
  4210. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  4211. uint32 arg_i32, ptr_len;
  4212. uint32 result_count = func_type->result_count;
  4213. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  4214. bool ret = false;
  4215. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4216. bool is_aot_func = (NULL == signature);
  4217. #endif
  4218. #ifndef BUILD_TARGET_RISCV64_LP64
  4219. #if WASM_ENABLE_SIMD == 0
  4220. uint64 *fps;
  4221. #else
  4222. v128 *fps;
  4223. #endif
  4224. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  4225. #define fps ints
  4226. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  4227. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  4228. /* important difference in calling conventions */
  4229. #define n_fps n_ints
  4230. #else
  4231. int n_fps = 0;
  4232. #endif
  4233. #if WASM_ENABLE_SIMD == 0
  4234. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  4235. #else
  4236. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  4237. + ext_ret_count;
  4238. #endif
  4239. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  4240. size = sizeof(uint64) * (uint64)argc1;
  4241. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4242. 0))) {
  4243. return false;
  4244. }
  4245. }
  4246. #ifndef BUILD_TARGET_RISCV64_LP64
  4247. #if WASM_ENABLE_SIMD == 0
  4248. fps = argv1;
  4249. ints = fps + MAX_REG_FLOATS;
  4250. #else
  4251. fps = (v128 *)argv1;
  4252. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  4253. #endif
  4254. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  4255. ints = argv1;
  4256. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  4257. stacks = ints + MAX_REG_INTS;
  4258. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  4259. for (i = 0; i < func_type->param_count; i++) {
  4260. switch (func_type->types[i]) {
  4261. case VALUE_TYPE_I32:
  4262. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4263. case VALUE_TYPE_FUNCREF:
  4264. #endif
  4265. {
  4266. arg_i32 = *argv_src++;
  4267. arg_i64 = arg_i32;
  4268. if (signature) {
  4269. if (signature[i + 1] == '*') {
  4270. /* param is a pointer */
  4271. if (signature[i + 2] == '~')
  4272. /* pointer with length followed */
  4273. ptr_len = *argv_src;
  4274. else
  4275. /* pointer without length followed */
  4276. ptr_len = 1;
  4277. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  4278. ptr_len))
  4279. goto fail;
  4280. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4281. module, arg_i32);
  4282. }
  4283. else if (signature[i + 1] == '$') {
  4284. /* param is a string */
  4285. if (!wasm_runtime_validate_app_str_addr(module,
  4286. arg_i32))
  4287. goto fail;
  4288. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4289. module, arg_i32);
  4290. }
  4291. }
  4292. if (n_ints < MAX_REG_INTS)
  4293. ints[n_ints++] = arg_i64;
  4294. else
  4295. stacks[n_stacks++] = arg_i64;
  4296. break;
  4297. }
  4298. case VALUE_TYPE_I64:
  4299. #if WASM_ENABLE_GC != 0
  4300. case REF_TYPE_FUNCREF:
  4301. case REF_TYPE_EXTERNREF:
  4302. case REF_TYPE_ANYREF:
  4303. case REF_TYPE_EQREF:
  4304. case REF_TYPE_HT_NULLABLE:
  4305. case REF_TYPE_HT_NON_NULLABLE:
  4306. case REF_TYPE_I31REF:
  4307. case REF_TYPE_NULLFUNCREF:
  4308. case REF_TYPE_NULLEXTERNREF:
  4309. case REF_TYPE_STRUCTREF:
  4310. case REF_TYPE_ARRAYREF:
  4311. case REF_TYPE_NULLREF:
  4312. #if WASM_ENABLE_STRINGREF != 0
  4313. case REF_TYPE_STRINGREF:
  4314. case REF_TYPE_STRINGVIEWWTF8:
  4315. case REF_TYPE_STRINGVIEWWTF16:
  4316. case REF_TYPE_STRINGVIEWITER:
  4317. #endif
  4318. #endif
  4319. if (n_ints < MAX_REG_INTS)
  4320. ints[n_ints++] = *(uint64 *)argv_src;
  4321. else
  4322. stacks[n_stacks++] = *(uint64 *)argv_src;
  4323. argv_src += 2;
  4324. break;
  4325. case VALUE_TYPE_F32:
  4326. if (n_fps < MAX_REG_FLOATS) {
  4327. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  4328. }
  4329. else {
  4330. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  4331. }
  4332. break;
  4333. case VALUE_TYPE_F64:
  4334. if (n_fps < MAX_REG_FLOATS) {
  4335. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  4336. }
  4337. else {
  4338. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  4339. }
  4340. argv_src += 2;
  4341. break;
  4342. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4343. case VALUE_TYPE_EXTERNREF:
  4344. {
  4345. uint32 externref_idx = *argv_src++;
  4346. if (is_aot_func) {
  4347. if (n_ints < MAX_REG_INTS)
  4348. ints[n_ints++] = externref_idx;
  4349. else
  4350. stacks[n_stacks++] = externref_idx;
  4351. }
  4352. else {
  4353. void *externref_obj;
  4354. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4355. goto fail;
  4356. if (n_ints < MAX_REG_INTS)
  4357. ints[n_ints++] = (uintptr_t)externref_obj;
  4358. else
  4359. stacks[n_stacks++] = (uintptr_t)externref_obj;
  4360. }
  4361. break;
  4362. }
  4363. #endif
  4364. #if WASM_ENABLE_SIMD != 0
  4365. case VALUE_TYPE_V128:
  4366. if (n_fps < MAX_REG_FLOATS) {
  4367. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  4368. }
  4369. else {
  4370. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  4371. n_stacks++;
  4372. }
  4373. argv_src += 4;
  4374. break;
  4375. #endif
  4376. default:
  4377. bh_assert(0);
  4378. break;
  4379. }
  4380. }
  4381. /* Save extra result values' address to argv1 */
  4382. for (i = 0; i < ext_ret_count; i++) {
  4383. if (n_ints < MAX_REG_INTS)
  4384. ints[n_ints++] = *(uint64 *)argv_src;
  4385. else
  4386. stacks[n_stacks++] = *(uint64 *)argv_src;
  4387. argv_src += 2;
  4388. }
  4389. exec_env->attachment = attachment;
  4390. if (result_count == 0) {
  4391. invokeNative_Void(func_ptr, argv1, n_stacks);
  4392. }
  4393. else {
  4394. /* Invoke the native function and get the first result value */
  4395. switch (func_type->types[func_type->param_count]) {
  4396. case VALUE_TYPE_I32:
  4397. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4398. case VALUE_TYPE_FUNCREF:
  4399. #endif
  4400. argv_ret[0] =
  4401. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  4402. break;
  4403. case VALUE_TYPE_I64:
  4404. #if WASM_ENABLE_GC != 0
  4405. case REF_TYPE_FUNCREF:
  4406. case REF_TYPE_EXTERNREF:
  4407. case REF_TYPE_ANYREF:
  4408. case REF_TYPE_EQREF:
  4409. case REF_TYPE_HT_NULLABLE:
  4410. case REF_TYPE_HT_NON_NULLABLE:
  4411. case REF_TYPE_I31REF:
  4412. case REF_TYPE_NULLFUNCREF:
  4413. case REF_TYPE_NULLEXTERNREF:
  4414. case REF_TYPE_STRUCTREF:
  4415. case REF_TYPE_ARRAYREF:
  4416. case REF_TYPE_NULLREF:
  4417. #if WASM_ENABLE_STRINGREF != 0
  4418. case REF_TYPE_STRINGREF:
  4419. case REF_TYPE_STRINGVIEWWTF8:
  4420. case REF_TYPE_STRINGVIEWWTF16:
  4421. case REF_TYPE_STRINGVIEWITER:
  4422. #endif
  4423. #endif
  4424. PUT_I64_TO_ADDR(argv_ret,
  4425. invokeNative_Int64(func_ptr, argv1, n_stacks));
  4426. break;
  4427. case VALUE_TYPE_F32:
  4428. *(float32 *)argv_ret =
  4429. invokeNative_Float32(func_ptr, argv1, n_stacks);
  4430. break;
  4431. case VALUE_TYPE_F64:
  4432. PUT_F64_TO_ADDR(
  4433. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  4434. break;
  4435. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4436. case VALUE_TYPE_EXTERNREF:
  4437. {
  4438. if (is_aot_func) {
  4439. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  4440. }
  4441. else {
  4442. uint32 externref_idx;
  4443. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  4444. func_ptr, argv1, n_stacks);
  4445. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4446. externref_obj, &externref_idx))
  4447. goto fail;
  4448. argv_ret[0] = externref_idx;
  4449. }
  4450. break;
  4451. }
  4452. #endif
  4453. #if WASM_ENABLE_SIMD != 0
  4454. case VALUE_TYPE_V128:
  4455. *(v128 *)argv_ret =
  4456. invokeNative_V128(func_ptr, argv1, n_stacks);
  4457. break;
  4458. #endif
  4459. default:
  4460. bh_assert(0);
  4461. break;
  4462. }
  4463. }
  4464. exec_env->attachment = NULL;
  4465. ret = !wasm_runtime_copy_exception(module, NULL);
  4466. fail:
  4467. if (argv1 != argv_buf)
  4468. wasm_runtime_free(argv1);
  4469. return ret;
  4470. }
  4471. #endif /* end of defined(BUILD_TARGET_X86_64) \
  4472. || defined(BUILD_TARGET_AMD_64) \
  4473. || defined(BUILD_TARGET_AARCH64) \
  4474. || defined(BUILD_TARGET_RISCV64_LP64D) \
  4475. || defined(BUILD_TARGET_RISCV64_LP64) */
  4476. bool
  4477. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  4478. uint32 argc, uint32 argv[])
  4479. {
  4480. bool ret = false;
  4481. if (!wasm_runtime_exec_env_check(exec_env)) {
  4482. LOG_ERROR("Invalid exec env stack info.");
  4483. return false;
  4484. }
  4485. /* this function is called from native code, so exec_env->handle and
  4486. exec_env->native_stack_boundary must have been set, we don't set
  4487. it again */
  4488. #if WASM_ENABLE_INTERP != 0
  4489. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  4490. ret = wasm_call_indirect(exec_env, 0, element_index, argc, argv);
  4491. #endif
  4492. #if WASM_ENABLE_AOT != 0
  4493. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  4494. ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
  4495. #endif
  4496. return ret;
  4497. }
  4498. static void
  4499. exchange_uint32(uint8 *p_data)
  4500. {
  4501. uint8 value = *p_data;
  4502. *p_data = *(p_data + 3);
  4503. *(p_data + 3) = value;
  4504. value = *(p_data + 1);
  4505. *(p_data + 1) = *(p_data + 2);
  4506. *(p_data + 2) = value;
  4507. }
  4508. static void
  4509. exchange_uint64(uint8 *p_data)
  4510. {
  4511. uint32 value;
  4512. value = *(uint32 *)p_data;
  4513. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  4514. *(uint32 *)(p_data + 4) = value;
  4515. exchange_uint32(p_data);
  4516. exchange_uint32(p_data + 4);
  4517. }
  4518. void
  4519. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  4520. {
  4521. uint64 u1, u2;
  4522. bh_memcpy_s(&u1, 8, bytes, 8);
  4523. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  4524. if (!is_little_endian()) {
  4525. exchange_uint64((uint8 *)&u1);
  4526. exchange_uint64((uint8 *)&u2);
  4527. *ret1 = u2;
  4528. *ret2 = u1;
  4529. }
  4530. else {
  4531. *ret1 = u1;
  4532. *ret2 = u2;
  4533. }
  4534. }
  4535. #if WASM_ENABLE_THREAD_MGR != 0
  4536. typedef struct WASMThreadArg {
  4537. WASMExecEnv *new_exec_env;
  4538. wasm_thread_callback_t callback;
  4539. void *arg;
  4540. } WASMThreadArg;
  4541. WASMExecEnv *
  4542. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  4543. {
  4544. return wasm_cluster_spawn_exec_env(exec_env);
  4545. }
  4546. void
  4547. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  4548. {
  4549. wasm_cluster_destroy_spawned_exec_env(exec_env);
  4550. }
  4551. static void *
  4552. wasm_runtime_thread_routine(void *arg)
  4553. {
  4554. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  4555. void *ret;
  4556. bh_assert(thread_arg->new_exec_env);
  4557. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  4558. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  4559. wasm_runtime_free(thread_arg);
  4560. os_thread_exit(ret);
  4561. return ret;
  4562. }
  4563. int32
  4564. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  4565. wasm_thread_callback_t callback, void *arg)
  4566. {
  4567. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  4568. WASMThreadArg *thread_arg;
  4569. int32 ret;
  4570. if (!new_exec_env)
  4571. return -1;
  4572. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  4573. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  4574. return -1;
  4575. }
  4576. thread_arg->new_exec_env = new_exec_env;
  4577. thread_arg->callback = callback;
  4578. thread_arg->arg = arg;
  4579. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  4580. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  4581. if (ret != 0) {
  4582. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  4583. wasm_runtime_free(thread_arg);
  4584. }
  4585. return ret;
  4586. }
  4587. int32
  4588. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  4589. {
  4590. return os_thread_join((korp_tid)tid, retval);
  4591. }
  4592. #endif /* end of WASM_ENABLE_THREAD_MGR */
  4593. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4594. static korp_mutex externref_lock;
  4595. static uint32 externref_global_id = 1;
  4596. static HashMap *externref_map;
  4597. typedef struct ExternRefMapNode {
  4598. /* The extern object from runtime embedder */
  4599. void *extern_obj;
  4600. /* The module instance it belongs to */
  4601. WASMModuleInstanceCommon *module_inst;
  4602. /* Whether it is retained */
  4603. bool retained;
  4604. /* Whether it is marked by runtime */
  4605. bool marked;
  4606. /* cleanup function called when the externref is freed */
  4607. void (*cleanup)(void *);
  4608. } ExternRefMapNode;
  4609. static uint32
  4610. wasm_externref_hash(const void *key)
  4611. {
  4612. uint32 externref_idx = (uint32)(uintptr_t)key;
  4613. return externref_idx;
  4614. }
  4615. static bool
  4616. wasm_externref_equal(void *key1, void *key2)
  4617. {
  4618. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  4619. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  4620. return externref_idx1 == externref_idx2 ? true : false;
  4621. }
  4622. static bool
  4623. wasm_externref_map_init()
  4624. {
  4625. if (os_mutex_init(&externref_lock) != 0)
  4626. return false;
  4627. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  4628. wasm_externref_equal, NULL,
  4629. wasm_runtime_free))) {
  4630. os_mutex_destroy(&externref_lock);
  4631. return false;
  4632. }
  4633. externref_global_id = 1;
  4634. return true;
  4635. }
  4636. static void
  4637. wasm_externref_map_destroy()
  4638. {
  4639. bh_hash_map_destroy(externref_map);
  4640. os_mutex_destroy(&externref_lock);
  4641. }
  4642. typedef struct LookupExtObj_UserData {
  4643. ExternRefMapNode node;
  4644. bool found;
  4645. uint32 externref_idx;
  4646. } LookupExtObj_UserData;
  4647. static void
  4648. lookup_extobj_callback(void *key, void *value, void *user_data)
  4649. {
  4650. uint32 externref_idx = (uint32)(uintptr_t)key;
  4651. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4652. LookupExtObj_UserData *user_data_lookup =
  4653. (LookupExtObj_UserData *)user_data;
  4654. if (node->extern_obj == user_data_lookup->node.extern_obj
  4655. && node->module_inst == user_data_lookup->node.module_inst) {
  4656. user_data_lookup->found = true;
  4657. user_data_lookup->externref_idx = externref_idx;
  4658. }
  4659. }
  4660. static void
  4661. delete_externref(void *key, ExternRefMapNode *node)
  4662. {
  4663. bh_hash_map_remove(externref_map, key, NULL, NULL);
  4664. if (node->cleanup) {
  4665. (*node->cleanup)(node->extern_obj);
  4666. }
  4667. wasm_runtime_free(node);
  4668. }
  4669. static void
  4670. delete_extobj_callback(void *key, void *value, void *user_data)
  4671. {
  4672. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4673. LookupExtObj_UserData *lookup_user_data =
  4674. (LookupExtObj_UserData *)user_data;
  4675. if (node->extern_obj == lookup_user_data->node.extern_obj
  4676. && node->module_inst == lookup_user_data->node.module_inst) {
  4677. lookup_user_data->found = true;
  4678. delete_externref(key, node);
  4679. }
  4680. }
  4681. bool
  4682. wasm_externref_objdel(WASMModuleInstanceCommon *module_inst, void *extern_obj)
  4683. {
  4684. LookupExtObj_UserData lookup_user_data = { 0 };
  4685. bool ok = false;
  4686. /* in a wrapper, extern_obj could be any value */
  4687. lookup_user_data.node.extern_obj = extern_obj;
  4688. lookup_user_data.node.module_inst = module_inst;
  4689. lookup_user_data.found = false;
  4690. os_mutex_lock(&externref_lock);
  4691. /* Lookup hashmap firstly */
  4692. bh_hash_map_traverse(externref_map, delete_extobj_callback,
  4693. (void *)&lookup_user_data);
  4694. if (lookup_user_data.found) {
  4695. ok = true;
  4696. }
  4697. os_mutex_unlock(&externref_lock);
  4698. return ok;
  4699. }
  4700. bool
  4701. wasm_externref_set_cleanup(WASMModuleInstanceCommon *module_inst,
  4702. void *extern_obj, void (*extern_obj_cleanup)(void *))
  4703. {
  4704. LookupExtObj_UserData lookup_user_data = { 0 };
  4705. bool ok = false;
  4706. /* in a wrapper, extern_obj could be any value */
  4707. lookup_user_data.node.extern_obj = extern_obj;
  4708. lookup_user_data.node.module_inst = module_inst;
  4709. lookup_user_data.found = false;
  4710. os_mutex_lock(&externref_lock);
  4711. /* Lookup hashmap firstly */
  4712. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  4713. (void *)&lookup_user_data);
  4714. if (lookup_user_data.found) {
  4715. void *key = (void *)(uintptr_t)lookup_user_data.externref_idx;
  4716. ExternRefMapNode *node = bh_hash_map_find(externref_map, key);
  4717. node->cleanup = extern_obj_cleanup;
  4718. ok = true;
  4719. }
  4720. os_mutex_unlock(&externref_lock);
  4721. return ok;
  4722. }
  4723. bool
  4724. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  4725. uint32 *p_externref_idx)
  4726. {
  4727. LookupExtObj_UserData lookup_user_data = { 0 };
  4728. ExternRefMapNode *node;
  4729. uint32 externref_idx;
  4730. /*
  4731. * to catch a parameter from `wasm_application_execute_func`,
  4732. * which represents a string 'null'
  4733. */
  4734. #if UINTPTR_MAX == UINT32_MAX
  4735. if ((uint32)-1 == (uintptr_t)extern_obj) {
  4736. #else
  4737. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  4738. #endif
  4739. *p_externref_idx = NULL_REF;
  4740. return true;
  4741. }
  4742. /* in a wrapper, extern_obj could be any value */
  4743. lookup_user_data.node.extern_obj = extern_obj;
  4744. lookup_user_data.node.module_inst = module_inst;
  4745. lookup_user_data.found = false;
  4746. os_mutex_lock(&externref_lock);
  4747. /* Lookup hashmap firstly */
  4748. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  4749. (void *)&lookup_user_data);
  4750. if (lookup_user_data.found) {
  4751. *p_externref_idx = lookup_user_data.externref_idx;
  4752. os_mutex_unlock(&externref_lock);
  4753. return true;
  4754. }
  4755. /* Not found in hashmap */
  4756. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  4757. goto fail1;
  4758. }
  4759. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  4760. goto fail1;
  4761. }
  4762. memset(node, 0, sizeof(ExternRefMapNode));
  4763. node->extern_obj = extern_obj;
  4764. node->module_inst = module_inst;
  4765. node->cleanup = NULL;
  4766. externref_idx = externref_global_id;
  4767. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  4768. (void *)node)) {
  4769. goto fail2;
  4770. }
  4771. externref_global_id++;
  4772. *p_externref_idx = externref_idx;
  4773. os_mutex_unlock(&externref_lock);
  4774. return true;
  4775. fail2:
  4776. wasm_runtime_free(node);
  4777. fail1:
  4778. os_mutex_unlock(&externref_lock);
  4779. return false;
  4780. }
  4781. bool
  4782. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  4783. {
  4784. ExternRefMapNode *node;
  4785. /* catch a `ref.null` vairable */
  4786. if (externref_idx == NULL_REF) {
  4787. *p_extern_obj = NULL;
  4788. return true;
  4789. }
  4790. os_mutex_lock(&externref_lock);
  4791. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4792. os_mutex_unlock(&externref_lock);
  4793. if (!node)
  4794. return false;
  4795. *p_extern_obj = node->extern_obj;
  4796. return true;
  4797. }
  4798. static void
  4799. reclaim_extobj_callback(void *key, void *value, void *user_data)
  4800. {
  4801. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4802. WASMModuleInstanceCommon *module_inst =
  4803. (WASMModuleInstanceCommon *)user_data;
  4804. if (node->module_inst == module_inst) {
  4805. if (!node->marked && !node->retained) {
  4806. delete_externref(key, node);
  4807. }
  4808. else {
  4809. node->marked = false;
  4810. }
  4811. }
  4812. }
  4813. static void
  4814. mark_externref(uint32 externref_idx)
  4815. {
  4816. ExternRefMapNode *node;
  4817. if (externref_idx != NULL_REF) {
  4818. node =
  4819. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4820. if (node) {
  4821. node->marked = true;
  4822. }
  4823. }
  4824. }
  4825. #if WASM_ENABLE_INTERP != 0
  4826. static void
  4827. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  4828. {
  4829. uint32 i, j, externref_idx;
  4830. table_elem_type_t *table_data;
  4831. uint8 *global_data = module_inst->global_data;
  4832. WASMGlobalInstance *global;
  4833. WASMTableInstance *table;
  4834. global = module_inst->e->globals;
  4835. for (i = 0; i < module_inst->e->global_count; i++, global++) {
  4836. if (global->type == VALUE_TYPE_EXTERNREF) {
  4837. externref_idx = *(uint32 *)(global_data + global->data_offset);
  4838. mark_externref(externref_idx);
  4839. }
  4840. }
  4841. for (i = 0; i < module_inst->table_count; i++) {
  4842. uint8 elem_type = 0;
  4843. uint32 init_size, max_size;
  4844. table = wasm_get_table_inst(module_inst, i);
  4845. (void)wasm_runtime_get_table_inst_elem_type(
  4846. (WASMModuleInstanceCommon *)module_inst, i, &elem_type, &init_size,
  4847. &max_size);
  4848. if (elem_type == VALUE_TYPE_EXTERNREF) {
  4849. table_data = table->elems;
  4850. for (j = 0; j < table->cur_size; j++) {
  4851. externref_idx = table_data[j];
  4852. mark_externref(externref_idx);
  4853. }
  4854. }
  4855. (void)init_size;
  4856. (void)max_size;
  4857. }
  4858. }
  4859. #endif
  4860. #if WASM_ENABLE_AOT != 0
  4861. static void
  4862. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  4863. {
  4864. uint32 i = 0, j = 0;
  4865. const AOTModule *module = (AOTModule *)module_inst->module;
  4866. const AOTTable *table = module->tables;
  4867. const AOTGlobal *global = module->globals;
  4868. const AOTTableInstance *table_inst;
  4869. for (i = 0; i < module->global_count; i++, global++) {
  4870. if (global->type == VALUE_TYPE_EXTERNREF) {
  4871. mark_externref(
  4872. *(uint32 *)(module_inst->global_data + global->data_offset));
  4873. }
  4874. }
  4875. for (i = 0; i < module->table_count; i++) {
  4876. table_inst = module_inst->tables[i];
  4877. if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
  4878. while (j < table_inst->cur_size) {
  4879. mark_externref(table_inst->elems[j++]);
  4880. }
  4881. }
  4882. }
  4883. }
  4884. #endif
  4885. void
  4886. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  4887. {
  4888. os_mutex_lock(&externref_lock);
  4889. #if WASM_ENABLE_INTERP != 0
  4890. if (module_inst->module_type == Wasm_Module_Bytecode)
  4891. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  4892. #endif
  4893. #if WASM_ENABLE_AOT != 0
  4894. if (module_inst->module_type == Wasm_Module_AoT)
  4895. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  4896. #endif
  4897. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  4898. (void *)module_inst);
  4899. os_mutex_unlock(&externref_lock);
  4900. }
  4901. static void
  4902. cleanup_extobj_callback(void *key, void *value, void *user_data)
  4903. {
  4904. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4905. WASMModuleInstanceCommon *module_inst =
  4906. (WASMModuleInstanceCommon *)user_data;
  4907. if (node->module_inst == module_inst) {
  4908. delete_externref(key, node);
  4909. }
  4910. }
  4911. void
  4912. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  4913. {
  4914. os_mutex_lock(&externref_lock);
  4915. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  4916. (void *)module_inst);
  4917. os_mutex_unlock(&externref_lock);
  4918. }
  4919. bool
  4920. wasm_externref_retain(uint32 externref_idx)
  4921. {
  4922. ExternRefMapNode *node;
  4923. os_mutex_lock(&externref_lock);
  4924. if (externref_idx != NULL_REF) {
  4925. node =
  4926. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4927. if (node) {
  4928. node->retained = true;
  4929. os_mutex_unlock(&externref_lock);
  4930. return true;
  4931. }
  4932. }
  4933. os_mutex_unlock(&externref_lock);
  4934. return false;
  4935. }
  4936. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  4937. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  4938. uint32
  4939. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  4940. char **buf, uint32 *len)
  4941. {
  4942. if (dump_or_print) {
  4943. return (uint32)os_printf("%s", line_buf);
  4944. }
  4945. else if (*buf) {
  4946. uint32 dump_len;
  4947. dump_len = snprintf(*buf, *len, "%s", line_buf);
  4948. if (dump_len >= *len) {
  4949. dump_len = *len;
  4950. }
  4951. *len = *len - dump_len;
  4952. *buf = *buf + dump_len;
  4953. return dump_len;
  4954. }
  4955. else {
  4956. return (uint32)strlen(line_buf);
  4957. }
  4958. }
  4959. void
  4960. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  4961. {
  4962. WASMModuleInstanceCommon *module_inst =
  4963. wasm_exec_env_get_module_inst(exec_env);
  4964. #if WASM_ENABLE_INTERP != 0
  4965. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4966. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  4967. }
  4968. #endif
  4969. #if WASM_ENABLE_AOT != 0
  4970. if (module_inst->module_type == Wasm_Module_AoT) {
  4971. aot_dump_call_stack(exec_env, true, NULL, 0);
  4972. }
  4973. #endif
  4974. }
  4975. uint32
  4976. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env)
  4977. {
  4978. WASMModuleInstanceCommon *module_inst =
  4979. wasm_exec_env_get_module_inst(exec_env);
  4980. #if WASM_ENABLE_INTERP != 0
  4981. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4982. return wasm_interp_dump_call_stack(exec_env, false, NULL, 0);
  4983. }
  4984. #endif
  4985. #if WASM_ENABLE_AOT != 0
  4986. if (module_inst->module_type == Wasm_Module_AoT) {
  4987. return aot_dump_call_stack(exec_env, false, NULL, 0);
  4988. }
  4989. #endif
  4990. return 0;
  4991. }
  4992. uint32
  4993. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  4994. uint32 len)
  4995. {
  4996. WASMModuleInstanceCommon *module_inst =
  4997. wasm_exec_env_get_module_inst(exec_env);
  4998. #if WASM_ENABLE_INTERP != 0
  4999. if (module_inst->module_type == Wasm_Module_Bytecode) {
  5000. return wasm_interp_dump_call_stack(exec_env, false, buf, len);
  5001. }
  5002. #endif
  5003. #if WASM_ENABLE_AOT != 0
  5004. if (module_inst->module_type == Wasm_Module_AoT) {
  5005. return aot_dump_call_stack(exec_env, false, buf, len);
  5006. }
  5007. #endif
  5008. return 0;
  5009. }
  5010. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  5011. #if WASM_ENABLE_STATIC_PGO != 0
  5012. uint32
  5013. wasm_runtime_get_pgo_prof_data_size(WASMModuleInstanceCommon *module_inst)
  5014. {
  5015. #if WASM_ENABLE_AOT != 0
  5016. if (module_inst->module_type == Wasm_Module_AoT) {
  5017. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  5018. return aot_get_pgo_prof_data_size(aot_inst);
  5019. }
  5020. #endif
  5021. return 0;
  5022. }
  5023. uint32
  5024. wasm_runtime_dump_pgo_prof_data_to_buf(WASMModuleInstanceCommon *module_inst,
  5025. char *buf, uint32 len)
  5026. {
  5027. #if WASM_ENABLE_AOT != 0
  5028. if (module_inst->module_type == Wasm_Module_AoT) {
  5029. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  5030. return aot_dump_pgo_prof_data_to_buf(aot_inst, buf, len);
  5031. }
  5032. #endif
  5033. return 0;
  5034. }
  5035. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  5036. bool
  5037. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  5038. uint32 table_idx, uint8 *out_elem_type,
  5039. #if WASM_ENABLE_GC != 0
  5040. WASMRefType **out_ref_type,
  5041. #endif
  5042. uint32 *out_min_size, uint32 *out_max_size)
  5043. {
  5044. #if WASM_ENABLE_INTERP != 0
  5045. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5046. WASMModule *module = (WASMModule *)module_comm;
  5047. if (table_idx < module->import_table_count) {
  5048. WASMTableImport *import_table =
  5049. &((module->import_tables + table_idx)->u.table);
  5050. *out_elem_type = import_table->elem_type;
  5051. #if WASM_ENABLE_GC != 0
  5052. *out_ref_type = import_table->elem_ref_type;
  5053. #endif
  5054. *out_min_size = import_table->init_size;
  5055. *out_max_size = import_table->max_size;
  5056. }
  5057. else {
  5058. WASMTable *table =
  5059. module->tables + (table_idx - module->import_table_count);
  5060. *out_elem_type = table->elem_type;
  5061. #if WASM_ENABLE_GC != 0
  5062. *out_ref_type = table->elem_ref_type;
  5063. #endif
  5064. *out_min_size = table->init_size;
  5065. *out_max_size = table->max_size;
  5066. }
  5067. return true;
  5068. }
  5069. #endif
  5070. #if WASM_ENABLE_AOT != 0
  5071. if (module_comm->module_type == Wasm_Module_AoT) {
  5072. AOTModule *module = (AOTModule *)module_comm;
  5073. if (table_idx < module->import_table_count) {
  5074. AOTImportTable *import_table = module->import_tables + table_idx;
  5075. *out_elem_type = import_table->elem_type;
  5076. #if WASM_ENABLE_GC != 0
  5077. *out_ref_type = NULL; /* TODO */
  5078. #endif
  5079. *out_min_size = import_table->table_init_size;
  5080. *out_max_size = import_table->table_max_size;
  5081. }
  5082. else {
  5083. AOTTable *table =
  5084. module->tables + (table_idx - module->import_table_count);
  5085. *out_elem_type = table->elem_type;
  5086. #if WASM_ENABLE_GC != 0
  5087. *out_ref_type = NULL; /* TODO */
  5088. #endif
  5089. *out_min_size = table->table_init_size;
  5090. *out_max_size = table->table_max_size;
  5091. }
  5092. return true;
  5093. }
  5094. #endif
  5095. return false;
  5096. }
  5097. bool
  5098. wasm_runtime_get_table_inst_elem_type(
  5099. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  5100. uint8 *out_elem_type,
  5101. #if WASM_ENABLE_GC != 0
  5102. WASMRefType **out_ref_type,
  5103. #endif
  5104. uint32 *out_min_size, uint32 *out_max_size)
  5105. {
  5106. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  5107. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  5108. || module_inst_comm->module_type == Wasm_Module_AoT);
  5109. return wasm_runtime_get_table_elem_type(
  5110. (WASMModuleCommon *)module_inst->module, table_idx, out_elem_type,
  5111. #if WASM_ENABLE_GC != 0
  5112. out_ref_type,
  5113. #endif
  5114. out_min_size, out_max_size);
  5115. }
  5116. bool
  5117. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  5118. const WASMExport *export, WASMFuncType **out)
  5119. {
  5120. #if WASM_ENABLE_INTERP != 0
  5121. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5122. WASMModule *module = (WASMModule *)module_comm;
  5123. if (export->index < module->import_function_count) {
  5124. *out = module->import_functions[export->index].u.function.func_type;
  5125. }
  5126. else {
  5127. *out =
  5128. module->functions[export->index - module->import_function_count]
  5129. ->func_type;
  5130. }
  5131. return true;
  5132. }
  5133. #endif
  5134. #if WASM_ENABLE_AOT != 0
  5135. if (module_comm->module_type == Wasm_Module_AoT) {
  5136. AOTModule *module = (AOTModule *)module_comm;
  5137. if (export->index < module->import_func_count) {
  5138. *out = (WASMFuncType *)
  5139. module->types[module->import_funcs[export->index]
  5140. .func_type_index];
  5141. }
  5142. else {
  5143. *out = (WASMFuncType *)module
  5144. ->types[module->func_type_indexes
  5145. [export->index - module->import_func_count]];
  5146. }
  5147. return true;
  5148. }
  5149. #endif
  5150. return false;
  5151. }
  5152. bool
  5153. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  5154. const WASMExport *export,
  5155. uint8 *out_val_type, bool *out_mutability)
  5156. {
  5157. #if WASM_ENABLE_INTERP != 0
  5158. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5159. WASMModule *module = (WASMModule *)module_comm;
  5160. if (export->index < module->import_global_count) {
  5161. WASMGlobalImport *import_global =
  5162. &((module->import_globals + export->index)->u.global);
  5163. *out_val_type = import_global->type;
  5164. *out_mutability = import_global->is_mutable;
  5165. }
  5166. else {
  5167. WASMGlobal *global =
  5168. module->globals + (export->index - module->import_global_count);
  5169. *out_val_type = global->type;
  5170. *out_mutability = global->is_mutable;
  5171. }
  5172. return true;
  5173. }
  5174. #endif
  5175. #if WASM_ENABLE_AOT != 0
  5176. if (module_comm->module_type == Wasm_Module_AoT) {
  5177. AOTModule *module = (AOTModule *)module_comm;
  5178. if (export->index < module->import_global_count) {
  5179. AOTImportGlobal *import_global =
  5180. module->import_globals + export->index;
  5181. *out_val_type = import_global->type;
  5182. *out_mutability = import_global->is_mutable;
  5183. }
  5184. else {
  5185. AOTGlobal *global =
  5186. module->globals + (export->index - module->import_global_count);
  5187. *out_val_type = global->type;
  5188. *out_mutability = global->is_mutable;
  5189. }
  5190. return true;
  5191. }
  5192. #endif
  5193. return false;
  5194. }
  5195. bool
  5196. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  5197. const WASMExport *export,
  5198. uint32 *out_min_page, uint32 *out_max_page)
  5199. {
  5200. #if WASM_ENABLE_INTERP != 0
  5201. if (module_comm->module_type == Wasm_Module_Bytecode) {
  5202. WASMModule *module = (WASMModule *)module_comm;
  5203. if (export->index < module->import_memory_count) {
  5204. WASMMemoryImport *import_memory =
  5205. &((module->import_memories + export->index)->u.memory);
  5206. *out_min_page = import_memory->init_page_count;
  5207. *out_max_page = import_memory->max_page_count;
  5208. }
  5209. else {
  5210. WASMMemory *memory =
  5211. module->memories
  5212. + (export->index - module->import_memory_count);
  5213. *out_min_page = memory->init_page_count;
  5214. *out_max_page = memory->max_page_count;
  5215. }
  5216. return true;
  5217. }
  5218. #endif
  5219. #if WASM_ENABLE_AOT != 0
  5220. if (module_comm->module_type == Wasm_Module_AoT) {
  5221. AOTModule *module = (AOTModule *)module_comm;
  5222. if (export->index < module->import_memory_count) {
  5223. AOTImportMemory *import_memory =
  5224. module->import_memories + export->index;
  5225. *out_min_page = import_memory->mem_init_page_count;
  5226. *out_max_page = import_memory->mem_max_page_count;
  5227. }
  5228. else {
  5229. AOTMemory *memory = module->memories
  5230. + (export->index - module->import_memory_count);
  5231. *out_min_page = memory->mem_init_page_count;
  5232. *out_max_page = memory->mem_max_page_count;
  5233. }
  5234. return true;
  5235. }
  5236. #endif
  5237. return false;
  5238. }
  5239. bool
  5240. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  5241. const WASMExport *export,
  5242. uint8 *out_elem_type,
  5243. #if WASM_ENABLE_GC != 0
  5244. WASMRefType **out_ref_type,
  5245. #endif
  5246. uint32 *out_min_size, uint32 *out_max_size)
  5247. {
  5248. return wasm_runtime_get_table_elem_type(module_comm, export->index,
  5249. out_elem_type,
  5250. #if WASM_ENABLE_GC != 0
  5251. out_ref_type,
  5252. #endif
  5253. out_min_size, out_max_size);
  5254. }
  5255. static inline bool
  5256. argv_to_params(wasm_val_t *out_params, const uint32 *argv,
  5257. WASMFuncType *func_type)
  5258. {
  5259. wasm_val_t *param = out_params;
  5260. uint32 i = 0, *u32;
  5261. for (i = 0; i < func_type->param_count; i++, param++) {
  5262. switch (func_type->types[i]) {
  5263. case VALUE_TYPE_I32:
  5264. param->kind = WASM_I32;
  5265. param->of.i32 = *argv++;
  5266. break;
  5267. case VALUE_TYPE_I64:
  5268. param->kind = WASM_I64;
  5269. u32 = (uint32 *)&param->of.i64;
  5270. u32[0] = *argv++;
  5271. u32[1] = *argv++;
  5272. break;
  5273. case VALUE_TYPE_F32:
  5274. param->kind = WASM_F32;
  5275. param->of.f32 = *(float32 *)argv++;
  5276. break;
  5277. case VALUE_TYPE_F64:
  5278. param->kind = WASM_F64;
  5279. u32 = (uint32 *)&param->of.i64;
  5280. u32[0] = *argv++;
  5281. u32[1] = *argv++;
  5282. break;
  5283. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5284. case VALUE_TYPE_EXTERNREF:
  5285. param->kind = WASM_ANYREF;
  5286. if (!wasm_externref_ref2obj(*argv,
  5287. (void **)&param->of.foreign)) {
  5288. return false;
  5289. }
  5290. argv++;
  5291. break;
  5292. #endif
  5293. default:
  5294. return false;
  5295. }
  5296. }
  5297. return true;
  5298. }
  5299. static inline bool
  5300. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  5301. const wasm_val_t *results, WASMFuncType *func_type)
  5302. {
  5303. const wasm_val_t *result = results;
  5304. uint32 *argv = out_argv, *u32, i;
  5305. uint8 *result_types = func_type->types + func_type->param_count;
  5306. for (i = 0; i < func_type->result_count; i++, result++) {
  5307. switch (result_types[i]) {
  5308. case VALUE_TYPE_I32:
  5309. case VALUE_TYPE_F32:
  5310. *(int32 *)argv++ = result->of.i32;
  5311. break;
  5312. case VALUE_TYPE_I64:
  5313. case VALUE_TYPE_F64:
  5314. u32 = (uint32 *)&result->of.i64;
  5315. *argv++ = u32[0];
  5316. *argv++ = u32[1];
  5317. break;
  5318. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5319. case VALUE_TYPE_EXTERNREF:
  5320. if (!wasm_externref_obj2ref(module_inst,
  5321. (void *)result->of.foreign,
  5322. (uint32 *)argv)) {
  5323. return false;
  5324. }
  5325. argv++;
  5326. break;
  5327. #endif
  5328. default:
  5329. return false;
  5330. }
  5331. }
  5332. return true;
  5333. }
  5334. bool
  5335. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  5336. void *func_ptr, WASMFuncType *func_type,
  5337. uint32 argc, uint32 *argv, bool with_env,
  5338. void *wasm_c_api_env)
  5339. {
  5340. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  5341. wasm_val_t *params = params_buf, *results = results_buf;
  5342. wasm_trap_t *trap = NULL;
  5343. bool ret = false;
  5344. wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
  5345. if (func_type->param_count > 16) {
  5346. if (!(params =
  5347. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  5348. module_inst, NULL, 0))) {
  5349. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  5350. return false;
  5351. }
  5352. }
  5353. if (!argv_to_params(params, argv, func_type)) {
  5354. wasm_runtime_set_exception(module_inst, "unsupported param type");
  5355. goto fail;
  5356. }
  5357. if (func_type->result_count > 4) {
  5358. if (!(results =
  5359. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  5360. module_inst, NULL, 0))) {
  5361. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  5362. goto fail;
  5363. }
  5364. }
  5365. params_vec.data = params;
  5366. params_vec.num_elems = func_type->param_count;
  5367. params_vec.size = func_type->param_count;
  5368. results_vec.data = results;
  5369. results_vec.num_elems = 0;
  5370. results_vec.size = func_type->result_count;
  5371. if (!with_env) {
  5372. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  5373. trap = callback(&params_vec, &results_vec);
  5374. }
  5375. else {
  5376. wasm_func_callback_with_env_t callback =
  5377. (wasm_func_callback_with_env_t)func_ptr;
  5378. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  5379. }
  5380. if (trap) {
  5381. if (trap->message->data) {
  5382. /* since trap->message->data does not end with '\0' */
  5383. char trap_message[108] = { 0 };
  5384. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  5385. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  5386. ? (uint32)trap->message->size
  5387. : max_size_to_copy;
  5388. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  5389. trap->message->data, size_to_copy);
  5390. wasm_runtime_set_exception(module_inst, trap_message);
  5391. }
  5392. else {
  5393. wasm_runtime_set_exception(
  5394. module_inst, "native function throw unknown exception");
  5395. }
  5396. wasm_trap_delete(trap);
  5397. goto fail;
  5398. }
  5399. if (!results_to_argv(module_inst, argv, results, func_type)) {
  5400. wasm_runtime_set_exception(module_inst, "unsupported result type");
  5401. goto fail;
  5402. }
  5403. ret = true;
  5404. fail:
  5405. if (params != params_buf)
  5406. wasm_runtime_free(params);
  5407. if (results != results_buf)
  5408. wasm_runtime_free(results);
  5409. return ret;
  5410. }
  5411. bool
  5412. wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *inst_comm,
  5413. CApiFuncImport *c_api_import,
  5414. wasm_val_t *params, uint32 param_count,
  5415. wasm_val_t *results, uint32 result_count)
  5416. {
  5417. WASMModuleInstance *module_inst = (WASMModuleInstance *)inst_comm;
  5418. void *func_ptr = c_api_import->func_ptr_linked;
  5419. bool with_env_arg = c_api_import->with_env_arg, ret = true;
  5420. wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
  5421. wasm_trap_t *trap = NULL;
  5422. params_vec.data = params;
  5423. params_vec.num_elems = param_count;
  5424. params_vec.size = param_count;
  5425. results_vec.data = results;
  5426. results_vec.num_elems = 0;
  5427. results_vec.size = result_count;
  5428. if (!func_ptr) {
  5429. wasm_set_exception_with_id(module_inst, EXCE_CALL_UNLINKED_IMPORT_FUNC);
  5430. ret = false;
  5431. goto fail;
  5432. }
  5433. if (!with_env_arg) {
  5434. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  5435. trap = callback(&params_vec, &results_vec);
  5436. }
  5437. else {
  5438. void *wasm_c_api_env = c_api_import->env_arg;
  5439. wasm_func_callback_with_env_t callback =
  5440. (wasm_func_callback_with_env_t)func_ptr;
  5441. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  5442. }
  5443. if (trap) {
  5444. if (trap->message->data) {
  5445. /* since trap->message->data does not end with '\0' */
  5446. char trap_message[108] = { 0 };
  5447. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  5448. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  5449. ? (uint32)trap->message->size
  5450. : max_size_to_copy;
  5451. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  5452. trap->message->data, size_to_copy);
  5453. wasm_set_exception(module_inst, trap_message);
  5454. }
  5455. else {
  5456. wasm_set_exception(module_inst,
  5457. "native function throw unknown exception");
  5458. }
  5459. wasm_trap_delete(trap);
  5460. ret = false;
  5461. }
  5462. fail:
  5463. #ifdef OS_ENABLE_HW_BOUND_CHECK
  5464. if (!ret)
  5465. wasm_runtime_access_exce_check_guard_page();
  5466. #endif
  5467. return ret;
  5468. }
  5469. void
  5470. wasm_runtime_show_app_heap_corrupted_prompt()
  5471. {
  5472. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  5473. "is compiled by wasi-sdk-12.0 or higher version, "
  5474. "please add -Wl,--export=malloc -Wl,--export=free "
  5475. "to export malloc and free functions. If it is "
  5476. "compiled by asc, please add --exportRuntime to "
  5477. "export the runtime helpers.");
  5478. }
  5479. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5480. void
  5481. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list)
  5482. {
  5483. WASMCustomSection *section = section_list, *next;
  5484. while (section) {
  5485. next = section->next;
  5486. wasm_runtime_free(section);
  5487. section = next;
  5488. }
  5489. }
  5490. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */
  5491. void
  5492. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch)
  5493. {
  5494. *major = WAMR_VERSION_MAJOR;
  5495. *minor = WAMR_VERSION_MINOR;
  5496. *patch = WAMR_VERSION_PATCH;
  5497. }
  5498. bool
  5499. wasm_runtime_is_import_func_linked(const char *module_name,
  5500. const char *func_name)
  5501. {
  5502. return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL,
  5503. NULL);
  5504. }
  5505. bool
  5506. wasm_runtime_is_import_global_linked(const char *module_name,
  5507. const char *global_name)
  5508. {
  5509. #if WASM_ENABLE_LIBC_BUILTIN != 0
  5510. WASMGlobalImport global = { 0 };
  5511. return wasm_native_lookup_libc_builtin_global(module_name, global_name,
  5512. &global);
  5513. #else
  5514. return false;
  5515. #endif
  5516. }
  5517. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  5518. WASMExport *
  5519. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  5520. const char *field_name, uint8 export_kind, char *error_buf,
  5521. uint32 error_buf_size)
  5522. {
  5523. WASMExport *exports = NULL, *result = NULL, *export;
  5524. uint32 export_count = 0, i;
  5525. #if WASM_ENABLE_AOT != 0
  5526. if (module->module_type == Wasm_Module_AoT) {
  5527. AOTModule *aot_module = (AOTModule *)module;
  5528. exports = (WASMExport *)aot_module->exports;
  5529. export_count = aot_module->export_count;
  5530. }
  5531. #endif
  5532. #if WASM_ENABLE_INTERP != 0
  5533. if (module->module_type == Wasm_Module_Bytecode) {
  5534. WASMModule *wasm_module = (WASMModule *)module;
  5535. exports = wasm_module->exports;
  5536. export_count = wasm_module->export_count;
  5537. }
  5538. #endif
  5539. for (i = 0, export = exports; i < export_count; ++i, ++export) {
  5540. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  5541. result = export;
  5542. goto exit;
  5543. }
  5544. }
  5545. if (i == export_count) {
  5546. LOG_DEBUG("can not find an export %d named %s in the module %s",
  5547. export_kind, field_name, module_name);
  5548. set_error_buf(error_buf, error_buf_size,
  5549. "unknown import or incompatible import type");
  5550. }
  5551. exit:
  5552. return result;
  5553. }
  5554. #endif
  5555. #if WASM_ENABLE_MULTI_MODULE != 0
  5556. WASMModuleCommon *
  5557. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  5558. const char *sub_module_name)
  5559. {
  5560. WASMRegisteredModule *node = NULL;
  5561. #if WASM_ENABLE_AOT != 0
  5562. if (parent_module->module_type == Wasm_Module_AoT) {
  5563. node = bh_list_first_elem(
  5564. ((AOTModule *)parent_module)->import_module_list);
  5565. }
  5566. #endif
  5567. #if WASM_ENABLE_INTERP != 0
  5568. if (parent_module->module_type == Wasm_Module_Bytecode) {
  5569. node = bh_list_first_elem(
  5570. ((WASMModule *)parent_module)->import_module_list);
  5571. }
  5572. #endif
  5573. while (node && strcmp(sub_module_name, node->module_name)) {
  5574. node = bh_list_elem_next(node);
  5575. }
  5576. return node ? node->module : NULL;
  5577. }
  5578. bool
  5579. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  5580. const char *sub_module_name,
  5581. WASMModuleCommon *sub_module)
  5582. {
  5583. /* register sub_module into its parent sub module list */
  5584. WASMRegisteredModule *node = NULL;
  5585. bh_list_status ret = BH_LIST_ERROR;
  5586. if (wasm_runtime_search_sub_module(parent_module, sub_module_name)) {
  5587. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  5588. return true;
  5589. }
  5590. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  5591. if (!node) {
  5592. return false;
  5593. }
  5594. node->module_name = sub_module_name;
  5595. node->module = sub_module;
  5596. #if WASM_ENABLE_AOT != 0
  5597. if (parent_module->module_type == Wasm_Module_AoT) {
  5598. ret = bh_list_insert(((AOTModule *)parent_module)->import_module_list,
  5599. node);
  5600. }
  5601. #endif
  5602. #if WASM_ENABLE_INTERP != 0
  5603. if (parent_module->module_type == Wasm_Module_Bytecode) {
  5604. ret = bh_list_insert(((WASMModule *)parent_module)->import_module_list,
  5605. node);
  5606. }
  5607. #endif
  5608. bh_assert(BH_LIST_SUCCESS == ret);
  5609. (void)ret;
  5610. return true;
  5611. }
  5612. WASMModuleCommon *
  5613. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  5614. const char *sub_module_name, char *error_buf,
  5615. uint32 error_buf_size)
  5616. {
  5617. WASMModuleCommon *sub_module = NULL;
  5618. bool ret = false;
  5619. uint8 *buffer = NULL;
  5620. uint32 buffer_size = 0;
  5621. /* check the registered module list of the parent */
  5622. sub_module = wasm_runtime_search_sub_module(parent_module, sub_module_name);
  5623. if (sub_module) {
  5624. LOG_DEBUG("%s has been loaded before", sub_module_name);
  5625. return sub_module;
  5626. }
  5627. /* check the global registered module list */
  5628. sub_module = wasm_runtime_find_module_registered(sub_module_name);
  5629. if (sub_module) {
  5630. LOG_DEBUG("%s has been loaded", sub_module_name);
  5631. goto wasm_runtime_register_sub_module;
  5632. }
  5633. LOG_VERBOSE("loading %s", sub_module_name);
  5634. if (!reader) {
  5635. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5636. "no sub module reader to load %s", sub_module_name);
  5637. return NULL;
  5638. }
  5639. /* start to maintain a loading module list */
  5640. ret = wasm_runtime_is_loading_module(sub_module_name);
  5641. if (ret) {
  5642. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5643. "found circular dependency on %s", sub_module_name);
  5644. return NULL;
  5645. }
  5646. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  5647. error_buf_size);
  5648. if (!ret) {
  5649. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  5650. return NULL;
  5651. }
  5652. ret = reader(parent_module->module_type, sub_module_name, &buffer,
  5653. &buffer_size);
  5654. if (!ret) {
  5655. LOG_DEBUG("read the file of %s failed", sub_module_name);
  5656. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5657. "unknown import", sub_module_name);
  5658. goto delete_loading_module;
  5659. }
  5660. if (get_package_type(buffer, buffer_size) != parent_module->module_type) {
  5661. LOG_DEBUG("moudle %s type error", sub_module_name);
  5662. goto delete_loading_module;
  5663. }
  5664. if (get_package_type(buffer, buffer_size) == Wasm_Module_Bytecode) {
  5665. #if WASM_ENABLE_INTERP != 0
  5666. sub_module = (WASMModuleCommon *)wasm_load(buffer, buffer_size, false,
  5667. error_buf, error_buf_size);
  5668. #endif
  5669. }
  5670. else if (get_package_type(buffer, buffer_size) == Wasm_Module_AoT) {
  5671. #if WASM_ENABLE_AOT != 0
  5672. sub_module = (WASMModuleCommon *)aot_load_from_aot_file(
  5673. buffer, buffer_size, error_buf, error_buf_size);
  5674. #endif
  5675. }
  5676. if (!sub_module) {
  5677. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  5678. /* others will be destroyed in runtime_destroy() */
  5679. goto destroy_file_buffer;
  5680. }
  5681. wasm_runtime_delete_loading_module(sub_module_name);
  5682. /* register on a global list */
  5683. ret = wasm_runtime_register_module_internal(
  5684. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  5685. error_buf, error_buf_size);
  5686. if (!ret) {
  5687. LOG_DEBUG("error: can not register module %s globally\n",
  5688. sub_module_name);
  5689. /* others will be unloaded in runtime_destroy() */
  5690. goto unload_module;
  5691. }
  5692. /* register into its parent list */
  5693. wasm_runtime_register_sub_module:
  5694. ret = wasm_runtime_register_sub_module(parent_module, sub_module_name,
  5695. sub_module);
  5696. if (!ret) {
  5697. set_error_buf_v(parent_module, error_buf, error_buf_size,
  5698. "failed to register sub module %s", sub_module_name);
  5699. /* since it is in the global module list, no need to
  5700. * unload the module. the runtime_destroy() will do it
  5701. */
  5702. return NULL;
  5703. }
  5704. return sub_module;
  5705. unload_module:
  5706. wasm_runtime_unload(sub_module);
  5707. destroy_file_buffer:
  5708. if (destroyer) {
  5709. destroyer(buffer, buffer_size);
  5710. }
  5711. else {
  5712. LOG_WARNING("need to release the reading buffer of %s manually",
  5713. sub_module_name);
  5714. }
  5715. delete_loading_module:
  5716. wasm_runtime_delete_loading_module(sub_module_name);
  5717. return NULL;
  5718. }
  5719. bool
  5720. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  5721. WASMModuleInstanceCommon *module_inst,
  5722. uint32 stack_size, uint32 heap_size,
  5723. char *error_buf, uint32 error_buf_size)
  5724. {
  5725. bh_list *sub_module_inst_list = NULL;
  5726. WASMRegisteredModule *sub_module_list_node = NULL;
  5727. #if WASM_ENABLE_AOT != 0
  5728. if (module->module_type == Wasm_Module_AoT) {
  5729. sub_module_inst_list =
  5730. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  5731. ->sub_module_inst_list;
  5732. sub_module_list_node =
  5733. bh_list_first_elem(((AOTModule *)module)->import_module_list);
  5734. }
  5735. #endif
  5736. #if WASM_ENABLE_INTERP != 0
  5737. if (module->module_type == Wasm_Module_Bytecode) {
  5738. sub_module_inst_list =
  5739. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  5740. ->sub_module_inst_list;
  5741. sub_module_list_node =
  5742. bh_list_first_elem(((WASMModule *)module)->import_module_list);
  5743. }
  5744. #endif
  5745. while (sub_module_list_node) {
  5746. WASMSubModInstNode *sub_module_inst_list_node = NULL;
  5747. WASMModuleCommon *sub_module = sub_module_list_node->module;
  5748. WASMModuleInstanceCommon *sub_module_inst = NULL;
  5749. sub_module_inst = wasm_runtime_instantiate_internal(
  5750. sub_module, NULL, NULL, stack_size, heap_size, error_buf,
  5751. error_buf_size);
  5752. if (!sub_module_inst) {
  5753. LOG_DEBUG("instantiate %s failed",
  5754. sub_module_list_node->module_name);
  5755. return false;
  5756. }
  5757. sub_module_inst_list_node = loader_malloc(sizeof(WASMSubModInstNode),
  5758. error_buf, error_buf_size);
  5759. if (!sub_module_inst_list_node) {
  5760. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  5761. sizeof(WASMSubModInstNode));
  5762. if (sub_module_inst)
  5763. wasm_runtime_deinstantiate_internal(sub_module_inst, false);
  5764. return false;
  5765. }
  5766. sub_module_inst_list_node->module_inst =
  5767. (WASMModuleInstance *)sub_module_inst;
  5768. sub_module_inst_list_node->module_name =
  5769. sub_module_list_node->module_name;
  5770. bh_list_status ret =
  5771. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  5772. bh_assert(BH_LIST_SUCCESS == ret);
  5773. (void)ret;
  5774. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  5775. }
  5776. return true;
  5777. }
  5778. void
  5779. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst)
  5780. {
  5781. bh_list *list = NULL;
  5782. #if WASM_ENABLE_AOT != 0
  5783. if (module_inst->module_type == Wasm_Module_AoT) {
  5784. list = ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  5785. ->sub_module_inst_list;
  5786. }
  5787. #endif
  5788. #if WASM_ENABLE_INTERP != 0
  5789. if (module_inst->module_type == Wasm_Module_Bytecode) {
  5790. list =
  5791. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  5792. ->sub_module_inst_list;
  5793. }
  5794. #endif
  5795. WASMSubModInstNode *node = bh_list_first_elem(list);
  5796. while (node) {
  5797. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  5798. bh_list_remove(list, node);
  5799. wasm_runtime_deinstantiate_internal(
  5800. (WASMModuleInstanceCommon *)node->module_inst, false);
  5801. wasm_runtime_free(node);
  5802. node = next_node;
  5803. }
  5804. }
  5805. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  5806. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  5807. void *
  5808. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  5809. void *ctx))
  5810. {
  5811. return wasm_native_create_context_key(dtor);
  5812. }
  5813. void
  5814. wasm_runtime_destroy_context_key(void *key)
  5815. {
  5816. wasm_native_destroy_context_key(key);
  5817. }
  5818. void
  5819. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
  5820. {
  5821. wasm_native_set_context(inst, key, ctx);
  5822. }
  5823. void
  5824. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  5825. void *ctx)
  5826. {
  5827. wasm_native_set_context_spread(inst, key, ctx);
  5828. }
  5829. void *
  5830. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key)
  5831. {
  5832. return wasm_native_get_context(inst, key);
  5833. }
  5834. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  5835. #if WASM_ENABLE_LINUX_PERF != 0
  5836. static bool enable_linux_perf = false;
  5837. bool
  5838. wasm_runtime_get_linux_perf(void)
  5839. {
  5840. return enable_linux_perf;
  5841. }
  5842. void
  5843. wasm_runtime_set_linux_perf(bool flag)
  5844. {
  5845. enable_linux_perf = flag;
  5846. }
  5847. #endif