wasm_runtime_common.c 242 KB

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