wasm_runtime_common.c 243 KB

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