wasm_runtime_common.c 242 KB

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