wasm_runtime_common.c 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095
  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_runtime_common.h"
  10. #include "wasm_memory.h"
  11. #if WASM_ENABLE_INTERP != 0
  12. #include "../interpreter/wasm_runtime.h"
  13. #endif
  14. #if WASM_ENABLE_AOT != 0
  15. #include "../aot/aot_runtime.h"
  16. #if WASM_ENABLE_DEBUG_AOT != 0
  17. #include "../aot/debug/jit_debug.h"
  18. #endif
  19. #endif
  20. #if WASM_ENABLE_THREAD_MGR != 0
  21. #include "../libraries/thread-mgr/thread_manager.h"
  22. #if WASM_ENABLE_DEBUG_INTERP != 0
  23. #include "../libraries/debug-engine/debug_engine.h"
  24. #endif
  25. #endif
  26. #if WASM_ENABLE_SHARED_MEMORY != 0
  27. #include "wasm_shared_memory.h"
  28. #endif
  29. #if WASM_ENABLE_FAST_JIT != 0
  30. #include "../fast-jit/jit_compiler.h"
  31. #endif
  32. #include "../common/wasm_c_api_internal.h"
  33. #include "../../version.h"
  34. /**
  35. * For runtime build, BH_MALLOC/BH_FREE should be defined as
  36. * wasm_runtime_malloc/wasm_runtime_free.
  37. */
  38. #define CHECK(a) CHECK1(a)
  39. #define CHECK1(a) SHOULD_BE_##a
  40. #define SHOULD_BE_wasm_runtime_malloc 1
  41. #if !CHECK(BH_MALLOC)
  42. #error unexpected BH_MALLOC
  43. #endif
  44. #undef SHOULD_BE_wasm_runtime_malloc
  45. #define SHOULD_BE_wasm_runtime_free 1
  46. #if !CHECK(BH_FREE)
  47. #error unexpected BH_FREE
  48. #endif
  49. #undef SHOULD_BE_wasm_runtime_free
  50. #undef CHECK
  51. #undef CHECK1
  52. #if WASM_ENABLE_MULTI_MODULE != 0
  53. /**
  54. * A safety insurance to prevent
  55. * circular depencies which leads stack overflow
  56. * try to break early
  57. */
  58. typedef struct LoadingModule {
  59. bh_list_link l;
  60. /* point to a string pool */
  61. const char *module_name;
  62. } LoadingModule;
  63. static bh_list loading_module_list_head;
  64. static bh_list *const loading_module_list = &loading_module_list_head;
  65. static korp_mutex loading_module_list_lock;
  66. /**
  67. * A list to store all exported functions/globals/memories/tables
  68. * of every fully loaded module
  69. */
  70. static bh_list registered_module_list_head;
  71. static bh_list *const registered_module_list = &registered_module_list_head;
  72. static korp_mutex registered_module_list_lock;
  73. static void
  74. wasm_runtime_destroy_registered_module_list();
  75. #endif /* WASM_ENABLE_MULTI_MODULE */
  76. #define E_TYPE_XIP 4
  77. #if WASM_ENABLE_REF_TYPES != 0
  78. /* Initialize externref hashmap */
  79. static bool
  80. wasm_externref_map_init();
  81. /* Destroy externref hashmap */
  82. static void
  83. wasm_externref_map_destroy();
  84. #endif /* WASM_ENABLE_REF_TYPES */
  85. static void
  86. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  87. {
  88. if (error_buf != NULL)
  89. snprintf(error_buf, error_buf_size, "%s", string);
  90. }
  91. static void *
  92. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  93. char *error_buf, uint32 error_buf_size)
  94. {
  95. void *mem;
  96. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  97. if (module_inst != NULL) {
  98. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  99. }
  100. else if (error_buf != NULL) {
  101. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  102. }
  103. return NULL;
  104. }
  105. memset(mem, 0, (uint32)size);
  106. return mem;
  107. }
  108. #if WASM_ENABLE_FAST_JIT != 0
  109. static JitCompOptions jit_options = { 0 };
  110. #endif
  111. #ifdef OS_ENABLE_HW_BOUND_CHECK
  112. /* The exec_env of thread local storage, set before calling function
  113. and used in signal handler, as we cannot get it from the argument
  114. of signal handler */
  115. static os_thread_local_attribute WASMExecEnv *exec_env_tls = NULL;
  116. #ifndef BH_PLATFORM_WINDOWS
  117. static void
  118. runtime_signal_handler(void *sig_addr)
  119. {
  120. WASMModuleInstanceCommon *module_inst;
  121. WASMSignalInfo sig_info;
  122. sig_info.exec_env_tls = exec_env_tls;
  123. sig_info.sig_addr = sig_addr;
  124. if (exec_env_tls) {
  125. module_inst = exec_env_tls->module_inst;
  126. #if WASM_ENABLE_INTERP != 0
  127. if (module_inst->module_type == Wasm_Module_Bytecode)
  128. wasm_signal_handler(&sig_info);
  129. #endif
  130. #if WASM_ENABLE_AOT != 0
  131. if (module_inst->module_type == Wasm_Module_AoT)
  132. aot_signal_handler(&sig_info);
  133. #endif
  134. }
  135. }
  136. #else
  137. static LONG
  138. runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
  139. {
  140. WASMModuleInstanceCommon *module_inst;
  141. WASMSignalInfo sig_info;
  142. sig_info.exec_env_tls = exec_env_tls;
  143. sig_info.exce_info = exce_info;
  144. if (exec_env_tls) {
  145. module_inst = exec_env_tls->module_inst;
  146. #if WASM_ENABLE_INTERP != 0
  147. if (module_inst->module_type == Wasm_Module_Bytecode)
  148. return wasm_exception_handler(&sig_info);
  149. #endif
  150. #if WASM_ENABLE_AOT != 0
  151. if (module_inst->module_type == Wasm_Module_AoT)
  152. return aot_exception_handler(&sig_info);
  153. #endif
  154. }
  155. return EXCEPTION_CONTINUE_SEARCH;
  156. }
  157. #endif /* end of BH_PLATFORM_WINDOWS */
  158. static bool
  159. runtime_signal_init()
  160. {
  161. #ifndef BH_PLATFORM_WINDOWS
  162. return os_thread_signal_init(runtime_signal_handler) == 0 ? true : false;
  163. #else
  164. if (os_thread_signal_init() != 0)
  165. return false;
  166. if (!AddVectoredExceptionHandler(1, runtime_exception_handler)) {
  167. os_thread_signal_destroy();
  168. return false;
  169. }
  170. #endif
  171. return true;
  172. }
  173. static void
  174. runtime_signal_destroy()
  175. {
  176. #ifdef BH_PLATFORM_WINDOWS
  177. RemoveVectoredExceptionHandler(runtime_exception_handler);
  178. #endif
  179. os_thread_signal_destroy();
  180. }
  181. void
  182. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env)
  183. {
  184. exec_env_tls = exec_env;
  185. }
  186. WASMExecEnv *
  187. wasm_runtime_get_exec_env_tls()
  188. {
  189. return exec_env_tls;
  190. }
  191. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  192. static bool
  193. wasm_runtime_env_init()
  194. {
  195. if (bh_platform_init() != 0)
  196. return false;
  197. if (wasm_native_init() == false) {
  198. goto fail1;
  199. }
  200. #if WASM_ENABLE_MULTI_MODULE
  201. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  202. goto fail2;
  203. }
  204. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  205. goto fail3;
  206. }
  207. #endif
  208. #if WASM_ENABLE_SHARED_MEMORY
  209. if (!wasm_shared_memory_init()) {
  210. goto fail4;
  211. }
  212. #endif
  213. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  214. if (!thread_manager_init()) {
  215. goto fail5;
  216. }
  217. #endif
  218. #ifdef OS_ENABLE_HW_BOUND_CHECK
  219. if (!runtime_signal_init()) {
  220. goto fail6;
  221. }
  222. #endif
  223. #if WASM_ENABLE_AOT != 0
  224. #if WASM_ENABLE_DEBUG_AOT != 0
  225. if (!jit_debug_engine_init()) {
  226. goto fail7;
  227. }
  228. #endif
  229. #endif
  230. #if WASM_ENABLE_REF_TYPES != 0
  231. if (!wasm_externref_map_init()) {
  232. goto fail8;
  233. }
  234. #endif
  235. #if WASM_ENABLE_FAST_JIT != 0
  236. if (!jit_compiler_init(&jit_options)) {
  237. goto fail9;
  238. }
  239. #endif
  240. return true;
  241. #if WASM_ENABLE_FAST_JIT != 0
  242. fail9:
  243. #if WASM_ENABLE_REF_TYPES != 0
  244. wasm_externref_map_destroy();
  245. #endif
  246. #endif
  247. #if WASM_ENABLE_REF_TYPES != 0
  248. fail8:
  249. #endif
  250. #if WASM_ENABLE_AOT != 0
  251. #if WASM_ENABLE_DEBUG_AOT != 0
  252. jit_debug_engine_destroy();
  253. fail7:
  254. #endif
  255. #endif
  256. #ifdef OS_ENABLE_HW_BOUND_CHECK
  257. runtime_signal_destroy();
  258. fail6:
  259. #endif
  260. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  261. thread_manager_destroy();
  262. fail5:
  263. #endif
  264. #if WASM_ENABLE_SHARED_MEMORY
  265. wasm_shared_memory_destroy();
  266. fail4:
  267. #endif
  268. #if WASM_ENABLE_MULTI_MODULE
  269. os_mutex_destroy(&loading_module_list_lock);
  270. fail3:
  271. os_mutex_destroy(&registered_module_list_lock);
  272. fail2:
  273. #endif
  274. wasm_native_destroy();
  275. fail1:
  276. bh_platform_destroy();
  277. return false;
  278. }
  279. static bool
  280. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  281. {
  282. return exec_env && exec_env->module_inst && exec_env->wasm_stack_size > 0
  283. && exec_env->wasm_stack.s.top_boundary
  284. == exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size
  285. && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary;
  286. }
  287. bool
  288. wasm_runtime_init()
  289. {
  290. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  291. return false;
  292. if (!wasm_runtime_env_init()) {
  293. wasm_runtime_memory_destroy();
  294. return false;
  295. }
  296. return true;
  297. }
  298. void
  299. wasm_runtime_destroy()
  300. {
  301. #if WASM_ENABLE_FAST_JIT != 0
  302. jit_compiler_destroy();
  303. #endif
  304. #if WASM_ENABLE_REF_TYPES != 0
  305. wasm_externref_map_destroy();
  306. #endif
  307. #if WASM_ENABLE_AOT != 0
  308. #if WASM_ENABLE_DEBUG_AOT != 0
  309. jit_debug_engine_destroy();
  310. #endif
  311. #endif
  312. #ifdef OS_ENABLE_HW_BOUND_CHECK
  313. runtime_signal_destroy();
  314. #endif
  315. /* runtime env destroy */
  316. #if WASM_ENABLE_MULTI_MODULE
  317. wasm_runtime_destroy_loading_module_list();
  318. os_mutex_destroy(&loading_module_list_lock);
  319. wasm_runtime_destroy_registered_module_list();
  320. os_mutex_destroy(&registered_module_list_lock);
  321. #endif
  322. #if WASM_ENABLE_SHARED_MEMORY
  323. wasm_shared_memory_destroy();
  324. #endif
  325. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  326. #if WASM_ENABLE_DEBUG_INTERP != 0
  327. wasm_debug_engine_destroy();
  328. #endif
  329. thread_manager_destroy();
  330. #endif
  331. wasm_native_destroy();
  332. bh_platform_destroy();
  333. wasm_runtime_memory_destroy();
  334. }
  335. bool
  336. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  337. {
  338. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  339. &init_args->mem_alloc_option))
  340. return false;
  341. #if WASM_ENABLE_FAST_JIT != 0
  342. jit_options.code_cache_size = init_args->fast_jit_code_cache_size;
  343. #endif
  344. if (!wasm_runtime_env_init()) {
  345. wasm_runtime_memory_destroy();
  346. return false;
  347. }
  348. #if WASM_ENABLE_DEBUG_INTERP != 0
  349. if (strlen(init_args->ip_addr))
  350. if (!wasm_debug_engine_init(init_args->ip_addr,
  351. init_args->platform_port,
  352. init_args->instance_port)) {
  353. wasm_runtime_destroy();
  354. return false;
  355. }
  356. #endif
  357. if (init_args->n_native_symbols > 0
  358. && !wasm_runtime_register_natives(init_args->native_module_name,
  359. init_args->native_symbols,
  360. init_args->n_native_symbols)) {
  361. wasm_runtime_destroy();
  362. return false;
  363. }
  364. #if WASM_ENABLE_THREAD_MGR != 0
  365. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  366. #endif
  367. return true;
  368. }
  369. PackageType
  370. get_package_type(const uint8 *buf, uint32 size)
  371. {
  372. if (buf && size >= 4) {
  373. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  374. return Wasm_Module_Bytecode;
  375. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  376. return Wasm_Module_AoT;
  377. }
  378. return Package_Type_Unknown;
  379. }
  380. #if WASM_ENABLE_AOT != 0
  381. static uint8 *
  382. align_ptr(const uint8 *p, uint32 b)
  383. {
  384. uintptr_t v = (uintptr_t)p;
  385. uintptr_t m = b - 1;
  386. return (uint8 *)((v + m) & ~m);
  387. }
  388. #define CHECK_BUF(buf, buf_end, length) \
  389. do { \
  390. if ((uintptr_t)buf + length < (uintptr_t)buf \
  391. || (uintptr_t)buf + length > (uintptr_t)buf_end) \
  392. return false; \
  393. } while (0)
  394. #define read_uint16(p, p_end, res) \
  395. do { \
  396. p = (uint8 *)align_ptr(p, sizeof(uint16)); \
  397. CHECK_BUF(p, p_end, sizeof(uint16)); \
  398. res = *(uint16 *)p; \
  399. p += sizeof(uint16); \
  400. } while (0)
  401. #define read_uint32(p, p_end, res) \
  402. do { \
  403. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  404. CHECK_BUF(p, p_end, sizeof(uint32)); \
  405. res = *(uint32 *)p; \
  406. p += sizeof(uint32); \
  407. } while (0)
  408. bool
  409. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
  410. {
  411. const uint8 *p = buf, *p_end = buf + size;
  412. uint32 section_type, section_size;
  413. uint16 e_type;
  414. if (get_package_type(buf, size) != Wasm_Module_AoT)
  415. return false;
  416. CHECK_BUF(p, p_end, 8);
  417. p += 8;
  418. while (p < p_end) {
  419. read_uint32(p, p_end, section_type);
  420. read_uint32(p, p_end, section_size);
  421. CHECK_BUF(p, p_end, section_size);
  422. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  423. p += 4;
  424. read_uint16(p, p_end, e_type);
  425. return (e_type == E_TYPE_XIP) ? true : false;
  426. }
  427. else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
  428. return false;
  429. }
  430. p += section_size;
  431. }
  432. return false;
  433. }
  434. #endif /* end of WASM_ENABLE_AOT */
  435. #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
  436. uint32
  437. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
  438. {
  439. WASMModuleInstanceCommon *module_inst =
  440. wasm_runtime_get_module_inst(exec_env);
  441. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  442. bh_assert(module_inst);
  443. bh_assert(cluster);
  444. if (module_inst->module_type != Wasm_Module_Bytecode) {
  445. LOG_WARNING("Attempt to create a debug instance for an AOT module");
  446. return 0;
  447. }
  448. if (cluster->debug_inst) {
  449. LOG_WARNING("Cluster already bind to a debug instance");
  450. return cluster->debug_inst->control_thread->port;
  451. }
  452. if (wasm_debug_instance_create(cluster)) {
  453. return cluster->debug_inst->control_thread->port;
  454. }
  455. return 0;
  456. }
  457. #endif
  458. #if WASM_ENABLE_MULTI_MODULE != 0
  459. static module_reader reader;
  460. static module_destroyer destroyer;
  461. void
  462. wasm_runtime_set_module_reader(const module_reader reader_cb,
  463. const module_destroyer destroyer_cb)
  464. {
  465. reader = reader_cb;
  466. destroyer = destroyer_cb;
  467. }
  468. module_reader
  469. wasm_runtime_get_module_reader()
  470. {
  471. return reader;
  472. }
  473. module_destroyer
  474. wasm_runtime_get_module_destroyer()
  475. {
  476. return destroyer;
  477. }
  478. static WASMRegisteredModule *
  479. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  480. {
  481. WASMRegisteredModule *reg_module = NULL;
  482. os_mutex_lock(&registered_module_list_lock);
  483. reg_module = bh_list_first_elem(registered_module_list);
  484. while (reg_module && module != reg_module->module) {
  485. reg_module = bh_list_elem_next(reg_module);
  486. }
  487. os_mutex_unlock(&registered_module_list_lock);
  488. return reg_module;
  489. }
  490. bool
  491. wasm_runtime_register_module_internal(const char *module_name,
  492. WASMModuleCommon *module,
  493. uint8 *orig_file_buf,
  494. uint32 orig_file_buf_size,
  495. char *error_buf, uint32 error_buf_size)
  496. {
  497. WASMRegisteredModule *node = NULL;
  498. node = wasm_runtime_find_module_registered_by_reference(module);
  499. if (node) { /* module has been registered */
  500. if (node->module_name) { /* module has name */
  501. if (!module_name || strcmp(node->module_name, module_name)) {
  502. /* module has different name */
  503. LOG_DEBUG("module(%p) has been registered with name %s", module,
  504. node->module_name);
  505. set_error_buf(error_buf, error_buf_size,
  506. "Register module failed: "
  507. "failed to rename the module");
  508. return false;
  509. }
  510. else {
  511. /* module has the same name */
  512. LOG_DEBUG(
  513. "module(%p) has been registered with the same name %s",
  514. module, node->module_name);
  515. return true;
  516. }
  517. }
  518. else {
  519. /* module has empyt name, reset it */
  520. node->module_name = module_name;
  521. return true;
  522. }
  523. }
  524. /* module hasn't been registered */
  525. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  526. if (!node) {
  527. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
  528. sizeof(WASMRegisteredModule));
  529. return false;
  530. }
  531. /* share the string and the module */
  532. node->module_name = module_name;
  533. node->module = module;
  534. node->orig_file_buf = orig_file_buf;
  535. node->orig_file_buf_size = orig_file_buf_size;
  536. os_mutex_lock(&registered_module_list_lock);
  537. bh_list_status ret = bh_list_insert(registered_module_list, node);
  538. bh_assert(BH_LIST_SUCCESS == ret);
  539. (void)ret;
  540. os_mutex_unlock(&registered_module_list_lock);
  541. return true;
  542. }
  543. bool
  544. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  545. char *error_buf, uint32 error_buf_size)
  546. {
  547. if (!error_buf || !error_buf_size) {
  548. LOG_ERROR("error buffer is required");
  549. return false;
  550. }
  551. if (!module_name || !module) {
  552. LOG_DEBUG("module_name and module are required");
  553. set_error_buf(error_buf, error_buf_size,
  554. "Register module failed: "
  555. "module_name and module are required");
  556. return false;
  557. }
  558. if (wasm_runtime_is_built_in_module(module_name)) {
  559. LOG_DEBUG("%s is a built-in module name", module_name);
  560. set_error_buf(error_buf, error_buf_size,
  561. "Register module failed: "
  562. "can not register as a built-in module");
  563. return false;
  564. }
  565. return wasm_runtime_register_module_internal(module_name, module, NULL, 0,
  566. error_buf, error_buf_size);
  567. }
  568. void
  569. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  570. {
  571. WASMRegisteredModule *registered_module = NULL;
  572. os_mutex_lock(&registered_module_list_lock);
  573. registered_module = bh_list_first_elem(registered_module_list);
  574. while (registered_module && module != registered_module->module) {
  575. registered_module = bh_list_elem_next(registered_module);
  576. }
  577. /* it does not matter if it is not exist. after all, it is gone */
  578. if (registered_module) {
  579. bh_list_remove(registered_module_list, registered_module);
  580. wasm_runtime_free(registered_module);
  581. }
  582. os_mutex_unlock(&registered_module_list_lock);
  583. }
  584. WASMModuleCommon *
  585. wasm_runtime_find_module_registered(const char *module_name)
  586. {
  587. WASMRegisteredModule *module = NULL, *module_next;
  588. os_mutex_lock(&registered_module_list_lock);
  589. module = bh_list_first_elem(registered_module_list);
  590. while (module) {
  591. module_next = bh_list_elem_next(module);
  592. if (module->module_name && !strcmp(module_name, module->module_name)) {
  593. break;
  594. }
  595. module = module_next;
  596. }
  597. os_mutex_unlock(&registered_module_list_lock);
  598. return module ? module->module : NULL;
  599. }
  600. bool
  601. wasm_runtime_is_module_registered(const char *module_name)
  602. {
  603. return NULL != wasm_runtime_find_module_registered(module_name);
  604. }
  605. /*
  606. * simply destroy all
  607. */
  608. static void
  609. wasm_runtime_destroy_registered_module_list()
  610. {
  611. WASMRegisteredModule *reg_module = NULL;
  612. os_mutex_lock(&registered_module_list_lock);
  613. reg_module = bh_list_first_elem(registered_module_list);
  614. while (reg_module) {
  615. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  616. bh_list_remove(registered_module_list, reg_module);
  617. /* now, it is time to release every module in the runtime */
  618. if (reg_module->module->module_type == Wasm_Module_Bytecode) {
  619. #if WASM_ENABLE_INTERP != 0
  620. wasm_unload((WASMModule *)reg_module->module);
  621. #endif
  622. }
  623. else {
  624. #if WASM_ENABLE_AOT != 0
  625. aot_unload((AOTModule *)reg_module->module);
  626. #endif
  627. }
  628. /* destroy the file buffer */
  629. if (destroyer && reg_module->orig_file_buf) {
  630. destroyer(reg_module->orig_file_buf,
  631. reg_module->orig_file_buf_size);
  632. reg_module->orig_file_buf = NULL;
  633. reg_module->orig_file_buf_size = 0;
  634. }
  635. wasm_runtime_free(reg_module);
  636. reg_module = next_reg_module;
  637. }
  638. os_mutex_unlock(&registered_module_list_lock);
  639. }
  640. bool
  641. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  642. uint32 error_buf_size)
  643. {
  644. LOG_DEBUG("add %s into a loading list", module_name);
  645. LoadingModule *loadingModule =
  646. runtime_malloc(sizeof(LoadingModule), NULL, error_buf, error_buf_size);
  647. if (!loadingModule) {
  648. return false;
  649. }
  650. /* share the incoming string */
  651. loadingModule->module_name = module_name;
  652. os_mutex_lock(&loading_module_list_lock);
  653. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  654. bh_assert(BH_LIST_SUCCESS == ret);
  655. (void)ret;
  656. os_mutex_unlock(&loading_module_list_lock);
  657. return true;
  658. }
  659. void
  660. wasm_runtime_delete_loading_module(const char *module_name)
  661. {
  662. LOG_DEBUG("delete %s from a loading list", module_name);
  663. LoadingModule *module = NULL;
  664. os_mutex_lock(&loading_module_list_lock);
  665. module = bh_list_first_elem(loading_module_list);
  666. while (module && strcmp(module->module_name, module_name)) {
  667. module = bh_list_elem_next(module);
  668. }
  669. /* it does not matter if it is not exist. after all, it is gone */
  670. if (module) {
  671. bh_list_remove(loading_module_list, module);
  672. wasm_runtime_free(module);
  673. }
  674. os_mutex_unlock(&loading_module_list_lock);
  675. }
  676. bool
  677. wasm_runtime_is_loading_module(const char *module_name)
  678. {
  679. LOG_DEBUG("find %s in a loading list", module_name);
  680. LoadingModule *module = NULL;
  681. os_mutex_lock(&loading_module_list_lock);
  682. module = bh_list_first_elem(loading_module_list);
  683. while (module && strcmp(module_name, module->module_name)) {
  684. module = bh_list_elem_next(module);
  685. }
  686. os_mutex_unlock(&loading_module_list_lock);
  687. return module != NULL;
  688. }
  689. void
  690. wasm_runtime_destroy_loading_module_list()
  691. {
  692. LoadingModule *module = NULL;
  693. os_mutex_lock(&loading_module_list_lock);
  694. module = bh_list_first_elem(loading_module_list);
  695. while (module) {
  696. LoadingModule *next_module = bh_list_elem_next(module);
  697. bh_list_remove(loading_module_list, module);
  698. /*
  699. * will not free the module_name since it is
  700. * shared one of the const string pool
  701. */
  702. wasm_runtime_free(module);
  703. module = next_module;
  704. }
  705. os_mutex_unlock(&loading_module_list_lock);
  706. }
  707. #endif /* WASM_ENABLE_MULTI_MODULE */
  708. bool
  709. wasm_runtime_is_built_in_module(const char *module_name)
  710. {
  711. return (!strcmp("env", module_name) || !strcmp("wasi_unstable", module_name)
  712. || !strcmp("wasi_snapshot_preview1", module_name)
  713. #if WASM_ENABLE_SPEC_TEST != 0
  714. || !strcmp("spectest", module_name)
  715. #endif
  716. || !strcmp("", module_name));
  717. }
  718. #if WASM_ENABLE_THREAD_MGR != 0
  719. bool
  720. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  721. uint32 size)
  722. {
  723. WASMModuleInstanceCommon *module_inst =
  724. wasm_exec_env_get_module_inst(exec_env);
  725. #if WASM_ENABLE_INTERP != 0
  726. if (module_inst->module_type == Wasm_Module_Bytecode) {
  727. return wasm_set_aux_stack(exec_env, start_offset, size);
  728. }
  729. #endif
  730. #if WASM_ENABLE_AOT != 0
  731. if (module_inst->module_type == Wasm_Module_AoT) {
  732. return aot_set_aux_stack(exec_env, start_offset, size);
  733. }
  734. #endif
  735. return false;
  736. }
  737. bool
  738. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  739. uint32 *size)
  740. {
  741. WASMModuleInstanceCommon *module_inst =
  742. wasm_exec_env_get_module_inst(exec_env);
  743. #if WASM_ENABLE_INTERP != 0
  744. if (module_inst->module_type == Wasm_Module_Bytecode) {
  745. return wasm_get_aux_stack(exec_env, start_offset, size);
  746. }
  747. #endif
  748. #if WASM_ENABLE_AOT != 0
  749. if (module_inst->module_type == Wasm_Module_AoT) {
  750. return aot_get_aux_stack(exec_env, start_offset, size);
  751. }
  752. #endif
  753. return false;
  754. }
  755. void
  756. wasm_runtime_set_max_thread_num(uint32 num)
  757. {
  758. wasm_cluster_set_max_thread_num(num);
  759. }
  760. #endif /* end of WASM_ENABLE_THREAD_MGR */
  761. static WASMModuleCommon *
  762. register_module_with_null_name(WASMModuleCommon *module_common, char *error_buf,
  763. uint32 error_buf_size)
  764. {
  765. #if WASM_ENABLE_MULTI_MODULE != 0
  766. if (module_common) {
  767. if (!wasm_runtime_register_module_internal(NULL, module_common, NULL, 0,
  768. error_buf, error_buf_size)) {
  769. wasm_runtime_unload(module_common);
  770. return NULL;
  771. }
  772. return module_common;
  773. }
  774. else
  775. return NULL;
  776. #else
  777. return module_common;
  778. #endif
  779. }
  780. WASMModuleCommon *
  781. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  782. uint32 error_buf_size)
  783. {
  784. WASMModuleCommon *module_common = NULL;
  785. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  786. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  787. AOTModule *aot_module;
  788. WASMModule *module = wasm_load(buf, size, error_buf, error_buf_size);
  789. if (!module)
  790. return NULL;
  791. if (!(aot_module =
  792. aot_convert_wasm_module(module, error_buf, error_buf_size))) {
  793. wasm_unload(module);
  794. return NULL;
  795. }
  796. module_common = (WASMModuleCommon *)aot_module;
  797. return register_module_with_null_name(module_common, error_buf,
  798. error_buf_size);
  799. #elif WASM_ENABLE_INTERP != 0
  800. module_common =
  801. (WASMModuleCommon *)wasm_load(buf, size, error_buf, error_buf_size);
  802. return register_module_with_null_name(module_common, error_buf,
  803. error_buf_size);
  804. #endif
  805. }
  806. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  807. #if WASM_ENABLE_AOT != 0
  808. module_common = (WASMModuleCommon *)aot_load_from_aot_file(
  809. buf, size, error_buf, error_buf_size);
  810. return register_module_with_null_name(module_common, error_buf,
  811. error_buf_size);
  812. #endif
  813. }
  814. if (size < 4)
  815. set_error_buf(error_buf, error_buf_size,
  816. "WASM module load failed: unexpected end");
  817. else
  818. set_error_buf(error_buf, error_buf_size,
  819. "WASM module load failed: magic header not detected");
  820. return NULL;
  821. }
  822. WASMModuleCommon *
  823. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  824. char *error_buf, uint32 error_buf_size)
  825. {
  826. WASMModuleCommon *module_common;
  827. if (!is_aot) {
  828. #if WASM_ENABLE_INTERP != 0
  829. module_common = (WASMModuleCommon *)wasm_load_from_sections(
  830. section_list, error_buf, error_buf_size);
  831. return register_module_with_null_name(module_common, error_buf,
  832. error_buf_size);
  833. #endif
  834. }
  835. else {
  836. #if WASM_ENABLE_AOT != 0
  837. module_common = (WASMModuleCommon *)aot_load_from_sections(
  838. section_list, error_buf, error_buf_size);
  839. return register_module_with_null_name(module_common, error_buf,
  840. error_buf_size);
  841. #endif
  842. }
  843. #if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
  844. set_error_buf(error_buf, error_buf_size,
  845. "WASM module load failed: invalid section list type");
  846. return NULL;
  847. #endif
  848. }
  849. void
  850. wasm_runtime_unload(WASMModuleCommon *module)
  851. {
  852. #if WASM_ENABLE_MULTI_MODULE != 0
  853. /**
  854. * since we will unload and free all module when runtime_destroy()
  855. * we don't want users to unwillingly disrupt it
  856. */
  857. return;
  858. #endif
  859. #if WASM_ENABLE_INTERP != 0
  860. if (module->module_type == Wasm_Module_Bytecode) {
  861. wasm_unload((WASMModule *)module);
  862. return;
  863. }
  864. #endif
  865. #if WASM_ENABLE_AOT != 0
  866. if (module->module_type == Wasm_Module_AoT) {
  867. aot_unload((AOTModule *)module);
  868. return;
  869. }
  870. #endif
  871. }
  872. WASMModuleInstanceCommon *
  873. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  874. uint32 stack_size, uint32 heap_size,
  875. char *error_buf, uint32 error_buf_size)
  876. {
  877. #if WASM_ENABLE_INTERP != 0
  878. if (module->module_type == Wasm_Module_Bytecode)
  879. return (WASMModuleInstanceCommon *)wasm_instantiate(
  880. (WASMModule *)module, is_sub_inst, stack_size, heap_size, error_buf,
  881. error_buf_size);
  882. #endif
  883. #if WASM_ENABLE_AOT != 0
  884. if (module->module_type == Wasm_Module_AoT)
  885. return (WASMModuleInstanceCommon *)aot_instantiate(
  886. (AOTModule *)module, is_sub_inst, stack_size, heap_size, error_buf,
  887. error_buf_size);
  888. #endif
  889. set_error_buf(error_buf, error_buf_size,
  890. "Instantiate module failed, invalid module type");
  891. return NULL;
  892. }
  893. WASMModuleInstanceCommon *
  894. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  895. uint32 heap_size, char *error_buf,
  896. uint32 error_buf_size)
  897. {
  898. return wasm_runtime_instantiate_internal(
  899. module, false, stack_size, heap_size, error_buf, error_buf_size);
  900. }
  901. void
  902. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  903. bool is_sub_inst)
  904. {
  905. #if WASM_ENABLE_INTERP != 0
  906. if (module_inst->module_type == Wasm_Module_Bytecode) {
  907. wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst);
  908. return;
  909. }
  910. #endif
  911. #if WASM_ENABLE_AOT != 0
  912. if (module_inst->module_type == Wasm_Module_AoT) {
  913. aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst);
  914. return;
  915. }
  916. #endif
  917. }
  918. void
  919. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  920. {
  921. wasm_runtime_deinstantiate_internal(module_inst, false);
  922. }
  923. WASMExecEnv *
  924. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  925. uint32 stack_size)
  926. {
  927. return wasm_exec_env_create(module_inst, stack_size);
  928. }
  929. void
  930. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  931. {
  932. wasm_exec_env_destroy(exec_env);
  933. }
  934. bool
  935. wasm_runtime_init_thread_env(void)
  936. {
  937. #ifdef BH_PLATFORM_WINDOWS
  938. if (os_thread_env_init() != 0)
  939. return false;
  940. #endif
  941. #ifdef OS_ENABLE_HW_BOUND_CHECK
  942. if (!runtime_signal_init()) {
  943. #ifdef BH_PLATFORM_WINDOWS
  944. os_thread_env_destroy();
  945. #endif
  946. return false;
  947. }
  948. #endif
  949. return true;
  950. }
  951. void
  952. wasm_runtime_destroy_thread_env(void)
  953. {
  954. #ifdef OS_ENABLE_HW_BOUND_CHECK
  955. runtime_signal_destroy();
  956. #endif
  957. #ifdef BH_PLATFORM_WINDOWS
  958. os_thread_env_destroy();
  959. #endif
  960. }
  961. bool
  962. wasm_runtime_thread_env_inited(void)
  963. {
  964. #ifdef BH_PLATFORM_WINDOWS
  965. if (!os_thread_env_inited())
  966. return false;
  967. #endif
  968. #if WASM_ENABLE_AOT != 0
  969. #ifdef OS_ENABLE_HW_BOUND_CHECK
  970. if (!os_thread_signal_inited())
  971. return false;
  972. #endif
  973. #endif
  974. return true;
  975. }
  976. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  977. void
  978. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  979. {
  980. WASMModuleMemConsumption mem_conspn = { 0 };
  981. #if WASM_ENABLE_INTERP != 0
  982. if (module->module_type == Wasm_Module_Bytecode) {
  983. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  984. }
  985. #endif
  986. #if WASM_ENABLE_AOT != 0
  987. if (module->module_type == Wasm_Module_AoT) {
  988. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  989. }
  990. #endif
  991. os_printf("WASM module memory consumption, total size: %u\n",
  992. mem_conspn.total_size);
  993. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  994. os_printf(" types size: %u\n", mem_conspn.types_size);
  995. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  996. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  997. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  998. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  999. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1000. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1001. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  1002. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  1003. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  1004. #if WASM_ENABLE_AOT != 0
  1005. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  1006. #endif
  1007. }
  1008. void
  1009. wasm_runtime_dump_module_inst_mem_consumption(
  1010. const WASMModuleInstanceCommon *module_inst)
  1011. {
  1012. WASMModuleInstMemConsumption mem_conspn = { 0 };
  1013. #if WASM_ENABLE_INTERP != 0
  1014. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1015. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  1016. &mem_conspn);
  1017. }
  1018. #endif
  1019. #if WASM_ENABLE_AOT != 0
  1020. if (module_inst->module_type == Wasm_Module_AoT) {
  1021. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  1022. &mem_conspn);
  1023. }
  1024. #endif
  1025. os_printf("WASM module inst memory consumption, total size: %u\n",
  1026. mem_conspn.total_size);
  1027. os_printf(" module inst struct size: %u\n",
  1028. mem_conspn.module_inst_struct_size);
  1029. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1030. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  1031. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1032. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  1033. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1034. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1035. }
  1036. void
  1037. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  1038. {
  1039. uint32 total_size =
  1040. offsetof(WASMExecEnv, wasm_stack.s.bottom) + exec_env->wasm_stack_size;
  1041. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  1042. os_printf(" exec env struct size: %u\n",
  1043. offsetof(WASMExecEnv, wasm_stack.s.bottom));
  1044. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  1045. os_printf(" block addr cache size: %u\n",
  1046. sizeof(exec_env->block_addr_cache));
  1047. #endif
  1048. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  1049. }
  1050. uint32
  1051. gc_get_heap_highmark_size(void *heap);
  1052. void
  1053. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  1054. {
  1055. WASMModuleInstMemConsumption module_inst_mem_consps;
  1056. WASMModuleMemConsumption module_mem_consps;
  1057. WASMModuleInstanceCommon *module_inst_common;
  1058. WASMModuleCommon *module_common = NULL;
  1059. void *heap_handle = NULL;
  1060. uint32 total_size = 0, app_heap_peak_size = 0;
  1061. uint32 max_aux_stack_used = -1;
  1062. module_inst_common = exec_env->module_inst;
  1063. #if WASM_ENABLE_INTERP != 0
  1064. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  1065. WASMModuleInstance *wasm_module_inst =
  1066. (WASMModuleInstance *)module_inst_common;
  1067. WASMModule *wasm_module = wasm_module_inst->module;
  1068. module_common = (WASMModuleCommon *)wasm_module;
  1069. if (wasm_module_inst->memories) {
  1070. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  1071. }
  1072. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  1073. &module_inst_mem_consps);
  1074. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  1075. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  1076. max_aux_stack_used = wasm_module_inst->max_aux_stack_used;
  1077. }
  1078. #endif
  1079. #if WASM_ENABLE_AOT != 0
  1080. if (module_inst_common->module_type == Wasm_Module_AoT) {
  1081. AOTModuleInstance *aot_module_inst =
  1082. (AOTModuleInstance *)module_inst_common;
  1083. AOTModule *aot_module = (AOTModule *)aot_module_inst->aot_module.ptr;
  1084. module_common = (WASMModuleCommon *)aot_module;
  1085. if (aot_module_inst->memories.ptr) {
  1086. AOTMemoryInstance **memories =
  1087. (AOTMemoryInstance **)aot_module_inst->memories.ptr;
  1088. heap_handle = memories[0]->heap_handle.ptr;
  1089. }
  1090. aot_get_module_inst_mem_consumption(aot_module_inst,
  1091. &module_inst_mem_consps);
  1092. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  1093. }
  1094. #endif
  1095. bh_assert(module_common != NULL);
  1096. if (heap_handle) {
  1097. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  1098. }
  1099. total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  1100. + exec_env->wasm_stack_size + module_mem_consps.total_size
  1101. + module_inst_mem_consps.total_size;
  1102. os_printf("\nMemory consumption summary (bytes):\n");
  1103. wasm_runtime_dump_module_mem_consumption(module_common);
  1104. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  1105. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  1106. os_printf("\nTotal memory consumption of module, module inst and "
  1107. "exec env: %u\n",
  1108. total_size);
  1109. os_printf("Total interpreter stack used: %u\n",
  1110. exec_env->max_wasm_stack_used);
  1111. if (max_aux_stack_used != (uint32)-1)
  1112. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  1113. else
  1114. os_printf("Total aux stack used: no enough info to profile\n");
  1115. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  1116. }
  1117. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  1118. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1119. #if WASM_ENABLE_PERF_PROFILING != 0
  1120. void
  1121. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  1122. {
  1123. #if WASM_ENABLE_INTERP != 0
  1124. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1125. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1126. }
  1127. #endif
  1128. #if WASM_ENABLE_AOT != 0
  1129. if (module_inst->module_type == Wasm_Module_AoT) {
  1130. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1131. }
  1132. #endif
  1133. }
  1134. #endif
  1135. WASMModuleInstanceCommon *
  1136. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1137. {
  1138. return wasm_exec_env_get_module_inst(exec_env);
  1139. }
  1140. void
  1141. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  1142. WASMModuleInstanceCommon *const module_inst)
  1143. {
  1144. wasm_exec_env_set_module_inst(exec_env, module_inst);
  1145. }
  1146. void *
  1147. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  1148. {
  1149. return exec_env->attachment;
  1150. }
  1151. void
  1152. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  1153. {
  1154. exec_env->user_data = user_data;
  1155. }
  1156. void *
  1157. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  1158. {
  1159. return exec_env->user_data;
  1160. }
  1161. WASMType *
  1162. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  1163. uint32 module_type)
  1164. {
  1165. WASMType *type = NULL;
  1166. #if WASM_ENABLE_INTERP != 0
  1167. if (module_type == Wasm_Module_Bytecode) {
  1168. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  1169. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  1170. : wasm_func->u.func->func_type;
  1171. }
  1172. #endif
  1173. #if WASM_ENABLE_AOT != 0
  1174. if (module_type == Wasm_Module_AoT) {
  1175. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  1176. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  1177. : aot_func->u.func.func_type;
  1178. }
  1179. #endif
  1180. return type;
  1181. }
  1182. WASMFunctionInstanceCommon *
  1183. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  1184. const char *name, const char *signature)
  1185. {
  1186. #if WASM_ENABLE_INTERP != 0
  1187. if (module_inst->module_type == Wasm_Module_Bytecode)
  1188. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  1189. (const WASMModuleInstance *)module_inst, name, signature);
  1190. #endif
  1191. #if WASM_ENABLE_AOT != 0
  1192. if (module_inst->module_type == Wasm_Module_AoT)
  1193. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  1194. (const AOTModuleInstance *)module_inst, name, signature);
  1195. #endif
  1196. return NULL;
  1197. }
  1198. uint32
  1199. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  1200. WASMModuleInstanceCommon *const module_inst)
  1201. {
  1202. WASMType *type =
  1203. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1204. bh_assert(type);
  1205. return type->param_count;
  1206. }
  1207. uint32
  1208. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  1209. WASMModuleInstanceCommon *const module_inst)
  1210. {
  1211. WASMType *type =
  1212. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1213. bh_assert(type);
  1214. return type->result_count;
  1215. }
  1216. static uint8
  1217. val_type_to_val_kind(uint8 value_type)
  1218. {
  1219. switch (value_type) {
  1220. case VALUE_TYPE_I32:
  1221. return WASM_I32;
  1222. case VALUE_TYPE_I64:
  1223. return WASM_I64;
  1224. case VALUE_TYPE_F32:
  1225. return WASM_F32;
  1226. case VALUE_TYPE_F64:
  1227. return WASM_F64;
  1228. case VALUE_TYPE_FUNCREF:
  1229. return WASM_FUNCREF;
  1230. case VALUE_TYPE_EXTERNREF:
  1231. return WASM_ANYREF;
  1232. default:
  1233. bh_assert(0);
  1234. return 0;
  1235. }
  1236. }
  1237. void
  1238. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  1239. WASMModuleInstanceCommon *const module_inst,
  1240. wasm_valkind_t *param_types)
  1241. {
  1242. WASMType *type =
  1243. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1244. uint32 i;
  1245. bh_assert(type);
  1246. for (i = 0; i < type->param_count; i++) {
  1247. param_types[i] = val_type_to_val_kind(type->types[i]);
  1248. }
  1249. }
  1250. void
  1251. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  1252. WASMModuleInstanceCommon *const module_inst,
  1253. wasm_valkind_t *result_types)
  1254. {
  1255. WASMType *type =
  1256. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1257. uint32 i;
  1258. bh_assert(type);
  1259. for (i = 0; i < type->result_count; i++) {
  1260. result_types[i] =
  1261. val_type_to_val_kind(type->types[type->param_count + i]);
  1262. }
  1263. }
  1264. #if WASM_ENABLE_REF_TYPES != 0
  1265. /* (uintptr_t)externref -> (uint32)index */
  1266. /* argv -> *ret_argv */
  1267. static bool
  1268. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  1269. WASMFunctionInstanceCommon *function,
  1270. uint32 *argv, uint32 argc, uint32 **ret_argv,
  1271. uint32 *ret_argc_param,
  1272. uint32 *ret_argc_result)
  1273. {
  1274. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  1275. result_i = 0;
  1276. bool need_param_transform = false, need_result_transform = false;
  1277. uint64 size = 0;
  1278. WASMType *func_type = wasm_runtime_get_function_type(
  1279. function, exec_env->module_inst->module_type);
  1280. bh_assert(func_type);
  1281. *ret_argc_param = func_type->param_cell_num;
  1282. *ret_argc_result = func_type->ret_cell_num;
  1283. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  1284. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  1285. need_param_transform = true;
  1286. }
  1287. }
  1288. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  1289. if (VALUE_TYPE_EXTERNREF
  1290. == func_type->types[func_type->param_count + result_i]) {
  1291. need_result_transform = true;
  1292. }
  1293. }
  1294. if (!need_param_transform && !need_result_transform) {
  1295. *ret_argv = argv;
  1296. return true;
  1297. }
  1298. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  1299. size = sizeof(uint32) * func_type->param_cell_num;
  1300. }
  1301. else {
  1302. size = sizeof(uint32) * func_type->ret_cell_num;
  1303. }
  1304. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  1305. return false;
  1306. }
  1307. if (!need_param_transform) {
  1308. bh_memcpy_s(new_argv, (uint32)size, argv, (uint32)size);
  1309. }
  1310. else {
  1311. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  1312. && new_argv_i < func_type->param_cell_num;
  1313. param_i++) {
  1314. uint8 param_type = func_type->types[param_i];
  1315. if (VALUE_TYPE_EXTERNREF == param_type) {
  1316. void *externref_obj;
  1317. uint32 externref_index;
  1318. #if UINTPTR_MAX == UINT32_MAX
  1319. externref_obj = (void *)argv[argv_i];
  1320. #else
  1321. union {
  1322. uintptr_t val;
  1323. uint32 parts[2];
  1324. } u;
  1325. u.parts[0] = argv[argv_i];
  1326. u.parts[1] = argv[argv_i + 1];
  1327. externref_obj = (void *)u.val;
  1328. #endif
  1329. if (!wasm_externref_obj2ref(exec_env->module_inst,
  1330. externref_obj, &externref_index)) {
  1331. wasm_runtime_free(new_argv);
  1332. return false;
  1333. }
  1334. new_argv[new_argv_i] = externref_index;
  1335. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1336. new_argv_i++;
  1337. }
  1338. else {
  1339. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  1340. uint32 param_size = sizeof(uint32) * param_cell_num;
  1341. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  1342. param_size);
  1343. argv_i += param_cell_num;
  1344. new_argv_i += param_cell_num;
  1345. }
  1346. }
  1347. }
  1348. *ret_argv = new_argv;
  1349. return true;
  1350. }
  1351. /* (uintptr_t)externref <- (uint32)index */
  1352. /* argv <- new_argv */
  1353. static bool
  1354. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  1355. WASMFunctionInstanceCommon *function,
  1356. uint32 *argv, uint32 argc, uint32 *ret_argv)
  1357. {
  1358. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  1359. WASMType *func_type;
  1360. bh_assert((argv && ret_argv) || (argc == 0));
  1361. if (argv == ret_argv) {
  1362. /* no need to transfrom externref results */
  1363. return true;
  1364. }
  1365. func_type = wasm_runtime_get_function_type(
  1366. function, exec_env->module_inst->module_type);
  1367. bh_assert(func_type);
  1368. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  1369. result_i++) {
  1370. uint8 result_type = func_type->types[func_type->param_count + result_i];
  1371. if (result_type == VALUE_TYPE_EXTERNREF) {
  1372. void *externref_obj;
  1373. #if UINTPTR_MAX != UINT32_MAX
  1374. union {
  1375. uintptr_t val;
  1376. uint32 parts[2];
  1377. } u;
  1378. #endif
  1379. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  1380. wasm_runtime_free(argv);
  1381. return false;
  1382. }
  1383. #if UINTPTR_MAX == UINT32_MAX
  1384. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  1385. #else
  1386. u.val = (uintptr_t)externref_obj;
  1387. ret_argv[ret_argv_i] = u.parts[0];
  1388. ret_argv[ret_argv_i + 1] = u.parts[1];
  1389. #endif
  1390. argv_i += 1;
  1391. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1392. }
  1393. else {
  1394. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  1395. uint32 result_size = sizeof(uint32) * result_cell_num;
  1396. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  1397. result_size);
  1398. argv_i += result_cell_num;
  1399. ret_argv_i += result_cell_num;
  1400. }
  1401. }
  1402. wasm_runtime_free(argv);
  1403. return true;
  1404. }
  1405. #endif
  1406. bool
  1407. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  1408. WASMFunctionInstanceCommon *function, uint32 argc,
  1409. uint32 argv[])
  1410. {
  1411. bool ret = false;
  1412. uint32 *new_argv = NULL, param_argc;
  1413. #if WASM_ENABLE_REF_TYPES != 0
  1414. uint32 result_argc = 0;
  1415. #endif
  1416. if (!wasm_runtime_exec_env_check(exec_env)) {
  1417. LOG_ERROR("Invalid exec env stack info.");
  1418. return false;
  1419. }
  1420. #if WASM_ENABLE_REF_TYPES != 0
  1421. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  1422. &new_argv, &param_argc,
  1423. &result_argc)) {
  1424. wasm_runtime_set_exception(exec_env->module_inst,
  1425. "the arguments conversion is failed");
  1426. return false;
  1427. }
  1428. #else
  1429. new_argv = argv;
  1430. param_argc = argc;
  1431. #endif
  1432. #if WASM_ENABLE_INTERP != 0
  1433. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  1434. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  1435. param_argc, new_argv);
  1436. #endif
  1437. #if WASM_ENABLE_AOT != 0
  1438. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  1439. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  1440. param_argc, new_argv);
  1441. #endif
  1442. if (!ret) {
  1443. if (new_argv != argv) {
  1444. wasm_runtime_free(new_argv);
  1445. }
  1446. return false;
  1447. }
  1448. #if WASM_ENABLE_REF_TYPES != 0
  1449. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  1450. result_argc, argv)) {
  1451. wasm_runtime_set_exception(exec_env->module_inst,
  1452. "the result conversion is failed");
  1453. return false;
  1454. }
  1455. #endif
  1456. return ret;
  1457. }
  1458. static void
  1459. parse_args_to_uint32_array(WASMType *type, wasm_val_t *args, uint32 *out_argv)
  1460. {
  1461. uint32 i, p;
  1462. for (i = 0, p = 0; i < type->param_count; i++) {
  1463. switch (args[i].kind) {
  1464. case WASM_I32:
  1465. out_argv[p++] = args[i].of.i32;
  1466. break;
  1467. case WASM_I64:
  1468. {
  1469. union {
  1470. uint64 val;
  1471. uint32 parts[2];
  1472. } u;
  1473. u.val = args[i].of.i64;
  1474. out_argv[p++] = u.parts[0];
  1475. out_argv[p++] = u.parts[1];
  1476. break;
  1477. }
  1478. case WASM_F32:
  1479. {
  1480. union {
  1481. float32 val;
  1482. uint32 part;
  1483. } u;
  1484. u.val = args[i].of.f32;
  1485. out_argv[p++] = u.part;
  1486. break;
  1487. }
  1488. case WASM_F64:
  1489. {
  1490. union {
  1491. float64 val;
  1492. uint32 parts[2];
  1493. } u;
  1494. u.val = args[i].of.f64;
  1495. out_argv[p++] = u.parts[0];
  1496. out_argv[p++] = u.parts[1];
  1497. break;
  1498. }
  1499. #if WASM_ENABLE_REF_TYPES != 0
  1500. case WASM_FUNCREF:
  1501. {
  1502. out_argv[p++] = args[i].of.i32;
  1503. break;
  1504. }
  1505. case WASM_ANYREF:
  1506. {
  1507. #if UINTPTR_MAX == UINT32_MAX
  1508. out_argv[p++] = args[i].of.foreign;
  1509. #else
  1510. union {
  1511. uintptr_t val;
  1512. uint32 parts[2];
  1513. } u;
  1514. u.val = (uintptr_t)args[i].of.foreign;
  1515. out_argv[p++] = u.parts[0];
  1516. out_argv[p++] = u.parts[1];
  1517. #endif
  1518. break;
  1519. }
  1520. #endif
  1521. default:
  1522. bh_assert(0);
  1523. break;
  1524. }
  1525. }
  1526. }
  1527. static void
  1528. parse_uint32_array_to_results(WASMType *type, uint32 *argv,
  1529. wasm_val_t *out_results)
  1530. {
  1531. uint32 i, p;
  1532. for (i = 0, p = 0; i < type->result_count; i++) {
  1533. switch (type->types[type->param_count + i]) {
  1534. case VALUE_TYPE_I32:
  1535. out_results[i].kind = WASM_I32;
  1536. out_results[i].of.i32 = (int32)argv[p++];
  1537. break;
  1538. case VALUE_TYPE_I64:
  1539. {
  1540. union {
  1541. uint64 val;
  1542. uint32 parts[2];
  1543. } u;
  1544. u.parts[0] = argv[p++];
  1545. u.parts[1] = argv[p++];
  1546. out_results[i].kind = WASM_I64;
  1547. out_results[i].of.i64 = u.val;
  1548. break;
  1549. }
  1550. case VALUE_TYPE_F32:
  1551. {
  1552. union {
  1553. float32 val;
  1554. uint32 part;
  1555. } u;
  1556. u.part = argv[p++];
  1557. out_results[i].kind = WASM_F32;
  1558. out_results[i].of.f32 = u.val;
  1559. break;
  1560. }
  1561. case VALUE_TYPE_F64:
  1562. {
  1563. union {
  1564. float64 val;
  1565. uint32 parts[2];
  1566. } u;
  1567. u.parts[0] = argv[p++];
  1568. u.parts[1] = argv[p++];
  1569. out_results[i].kind = WASM_F64;
  1570. out_results[i].of.f64 = u.val;
  1571. break;
  1572. }
  1573. #if WASM_ENABLE_REF_TYPES != 0
  1574. case VALUE_TYPE_FUNCREF:
  1575. {
  1576. out_results[i].kind = WASM_I32;
  1577. out_results[i].of.i32 = (int32)argv[p++];
  1578. break;
  1579. }
  1580. case VALUE_TYPE_EXTERNREF:
  1581. {
  1582. #if UINTPTR_MAX == UINT32_MAX
  1583. out_results[i].kind = WASM_ANYREF;
  1584. out_results[i].of.foreign = (uintptr_t)argv[p++];
  1585. #else
  1586. union {
  1587. uintptr_t val;
  1588. uint32 parts[2];
  1589. } u;
  1590. u.parts[0] = argv[p++];
  1591. u.parts[1] = argv[p++];
  1592. out_results[i].kind = WASM_ANYREF;
  1593. out_results[i].of.foreign = u.val;
  1594. #endif
  1595. break;
  1596. }
  1597. #endif
  1598. default:
  1599. bh_assert(0);
  1600. break;
  1601. }
  1602. }
  1603. }
  1604. bool
  1605. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  1606. WASMFunctionInstanceCommon *function,
  1607. uint32 num_results, wasm_val_t results[],
  1608. uint32 num_args, wasm_val_t args[])
  1609. {
  1610. uint32 argc, argv_buf[16] = { 0 }, *argv = argv_buf, cell_num, module_type;
  1611. #if WASM_ENABLE_REF_TYPES != 0
  1612. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  1613. #endif
  1614. uint64 total_size;
  1615. WASMType *type;
  1616. bool ret = false;
  1617. module_type = exec_env->module_inst->module_type;
  1618. type = wasm_runtime_get_function_type(function, module_type);
  1619. if (!type) {
  1620. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  1621. "enabled at least one.");
  1622. goto fail1;
  1623. }
  1624. #if WASM_ENABLE_REF_TYPES != 0
  1625. for (i = 0; i < type->param_count; i++) {
  1626. param_size_in_double_world +=
  1627. wasm_value_type_cell_num_outside(type->types[i]);
  1628. }
  1629. for (i = 0; i < type->result_count; i++) {
  1630. result_size_in_double_world += wasm_value_type_cell_num_outside(
  1631. type->types[type->param_count + i]);
  1632. }
  1633. argc = param_size_in_double_world;
  1634. cell_num = (argc >= result_size_in_double_world)
  1635. ? argc
  1636. : result_size_in_double_world;
  1637. #else
  1638. argc = type->param_cell_num;
  1639. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  1640. #endif
  1641. if (num_results != type->result_count) {
  1642. LOG_ERROR(
  1643. "The result value number does not match the function declaration.");
  1644. goto fail1;
  1645. }
  1646. if (num_args != type->param_count) {
  1647. LOG_ERROR("The argument value number does not match the function "
  1648. "declaration.");
  1649. goto fail1;
  1650. }
  1651. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1652. if (total_size > sizeof(argv_buf)) {
  1653. if (!(argv =
  1654. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  1655. goto fail1;
  1656. }
  1657. }
  1658. parse_args_to_uint32_array(type, args, argv);
  1659. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  1660. goto fail2;
  1661. parse_uint32_array_to_results(type, argv, results);
  1662. fail2:
  1663. if (argv != argv_buf)
  1664. wasm_runtime_free(argv);
  1665. fail1:
  1666. return ret;
  1667. }
  1668. bool
  1669. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  1670. WASMFunctionInstanceCommon *function,
  1671. uint32 num_results, wasm_val_t results[],
  1672. uint32 num_args, ...)
  1673. {
  1674. wasm_val_t args_buf[8] = { 0 }, *args = args_buf;
  1675. WASMType *type = NULL;
  1676. bool ret = false;
  1677. uint64 total_size;
  1678. uint32 i = 0, module_type;
  1679. va_list vargs;
  1680. module_type = exec_env->module_inst->module_type;
  1681. type = wasm_runtime_get_function_type(function, module_type);
  1682. if (!type) {
  1683. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  1684. "must be enabled at least one.");
  1685. goto fail1;
  1686. }
  1687. if (num_args != type->param_count) {
  1688. LOG_ERROR("The argument value number does not match the "
  1689. "function declaration.");
  1690. goto fail1;
  1691. }
  1692. total_size = sizeof(wasm_val_t) * (uint64)num_args;
  1693. if (total_size > sizeof(args_buf)) {
  1694. if (!(args =
  1695. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  1696. goto fail1;
  1697. }
  1698. }
  1699. va_start(vargs, num_args);
  1700. for (i = 0; i < num_args; i++) {
  1701. switch (type->types[i]) {
  1702. case VALUE_TYPE_I32:
  1703. args[i].kind = WASM_I32;
  1704. args[i].of.i32 = va_arg(vargs, uint32);
  1705. break;
  1706. case VALUE_TYPE_I64:
  1707. args[i].kind = WASM_I64;
  1708. args[i].of.i64 = va_arg(vargs, uint64);
  1709. break;
  1710. case VALUE_TYPE_F32:
  1711. args[i].kind = WASM_F32;
  1712. args[i].of.f32 = (float32)va_arg(vargs, float64);
  1713. break;
  1714. case VALUE_TYPE_F64:
  1715. args[i].kind = WASM_F64;
  1716. args[i].of.f64 = va_arg(vargs, float64);
  1717. break;
  1718. #if WASM_ENABLE_REF_TYPES != 0
  1719. case VALUE_TYPE_FUNCREF:
  1720. {
  1721. args[i].kind = WASM_FUNCREF;
  1722. args[i].of.i32 = va_arg(vargs, uint32);
  1723. break;
  1724. }
  1725. case VALUE_TYPE_EXTERNREF:
  1726. {
  1727. args[i].kind = WASM_ANYREF;
  1728. args[i].of.foreign = va_arg(vargs, uintptr_t);
  1729. break;
  1730. }
  1731. #endif
  1732. default:
  1733. bh_assert(0);
  1734. break;
  1735. }
  1736. }
  1737. va_end(vargs);
  1738. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  1739. num_args, args);
  1740. if (args != args_buf)
  1741. wasm_runtime_free(args);
  1742. fail1:
  1743. return ret;
  1744. }
  1745. bool
  1746. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
  1747. {
  1748. #if WASM_ENABLE_INTERP != 0
  1749. if (module_inst->module_type == Wasm_Module_Bytecode)
  1750. return wasm_create_exec_env_singleton(
  1751. (WASMModuleInstance *)module_inst);
  1752. #endif
  1753. #if WASM_ENABLE_AOT != 0
  1754. if (module_inst->module_type == Wasm_Module_AoT)
  1755. return aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
  1756. #endif
  1757. return false;
  1758. }
  1759. WASMExecEnv *
  1760. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
  1761. {
  1762. #if WASM_ENABLE_INTERP != 0
  1763. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1764. if (!((WASMModuleInstance *)module_inst)->exec_env_singleton) {
  1765. wasm_create_exec_env_singleton((WASMModuleInstance *)module_inst);
  1766. }
  1767. return ((WASMModuleInstance *)module_inst)->exec_env_singleton;
  1768. }
  1769. #endif
  1770. #if WASM_ENABLE_AOT != 0
  1771. if (module_inst->module_type == Wasm_Module_AoT) {
  1772. if (!((AOTModuleInstance *)module_inst)->exec_env_singleton.ptr) {
  1773. aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
  1774. }
  1775. return (WASMExecEnv *)((AOTModuleInstance *)module_inst)
  1776. ->exec_env_singleton.ptr;
  1777. }
  1778. #endif
  1779. return NULL;
  1780. }
  1781. void
  1782. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  1783. const char *exception)
  1784. {
  1785. #if WASM_ENABLE_INTERP != 0
  1786. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1787. wasm_set_exception((WASMModuleInstance *)module_inst, exception);
  1788. return;
  1789. }
  1790. #endif
  1791. #if WASM_ENABLE_AOT != 0
  1792. if (module_inst->module_type == Wasm_Module_AoT) {
  1793. aot_set_exception((AOTModuleInstance *)module_inst, exception);
  1794. return;
  1795. }
  1796. #endif
  1797. }
  1798. const char *
  1799. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  1800. {
  1801. #if WASM_ENABLE_INTERP != 0
  1802. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1803. return wasm_get_exception((WASMModuleInstance *)module_inst);
  1804. }
  1805. #endif
  1806. #if WASM_ENABLE_AOT != 0
  1807. if (module_inst->module_type == Wasm_Module_AoT) {
  1808. return aot_get_exception((AOTModuleInstance *)module_inst);
  1809. }
  1810. #endif
  1811. return NULL;
  1812. }
  1813. void
  1814. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  1815. {
  1816. wasm_runtime_set_exception(module_inst, NULL);
  1817. }
  1818. void
  1819. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  1820. void *custom_data)
  1821. {
  1822. #if WASM_ENABLE_INTERP != 0
  1823. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1824. ((WASMModuleInstance *)module_inst)->custom_data = custom_data;
  1825. return;
  1826. }
  1827. #endif
  1828. #if WASM_ENABLE_AOT != 0
  1829. if (module_inst->module_type == Wasm_Module_AoT) {
  1830. ((AOTModuleInstance *)module_inst)->custom_data.ptr = custom_data;
  1831. return;
  1832. }
  1833. #endif
  1834. }
  1835. void
  1836. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  1837. void *custom_data)
  1838. {
  1839. #if WASM_ENABLE_THREAD_MGR != 0
  1840. wasm_cluster_spread_custom_data(module_inst, custom_data);
  1841. #else
  1842. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  1843. #endif
  1844. }
  1845. void *
  1846. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  1847. {
  1848. #if WASM_ENABLE_INTERP != 0
  1849. if (module_inst->module_type == Wasm_Module_Bytecode)
  1850. return ((WASMModuleInstance *)module_inst)->custom_data;
  1851. #endif
  1852. #if WASM_ENABLE_AOT != 0
  1853. if (module_inst->module_type == Wasm_Module_AoT)
  1854. return ((AOTModuleInstance *)module_inst)->custom_data.ptr;
  1855. #endif
  1856. return NULL;
  1857. }
  1858. uint32
  1859. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  1860. void **p_native_addr)
  1861. {
  1862. #if WASM_ENABLE_INTERP != 0
  1863. if (module_inst->module_type == Wasm_Module_Bytecode)
  1864. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  1865. p_native_addr);
  1866. #endif
  1867. #if WASM_ENABLE_AOT != 0
  1868. if (module_inst->module_type == Wasm_Module_AoT)
  1869. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  1870. p_native_addr);
  1871. #endif
  1872. return 0;
  1873. }
  1874. uint32
  1875. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  1876. uint32 size, void **p_native_addr)
  1877. {
  1878. #if WASM_ENABLE_INTERP != 0
  1879. if (module_inst->module_type == Wasm_Module_Bytecode)
  1880. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  1881. p_native_addr);
  1882. #endif
  1883. #if WASM_ENABLE_AOT != 0
  1884. if (module_inst->module_type == Wasm_Module_AoT)
  1885. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  1886. p_native_addr);
  1887. #endif
  1888. return 0;
  1889. }
  1890. void
  1891. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  1892. {
  1893. #if WASM_ENABLE_INTERP != 0
  1894. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1895. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  1896. return;
  1897. }
  1898. #endif
  1899. #if WASM_ENABLE_AOT != 0
  1900. if (module_inst->module_type == Wasm_Module_AoT) {
  1901. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  1902. return;
  1903. }
  1904. #endif
  1905. }
  1906. uint32
  1907. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  1908. const char *src, uint32 size)
  1909. {
  1910. #if WASM_ENABLE_INTERP != 0
  1911. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1912. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  1913. size);
  1914. }
  1915. #endif
  1916. #if WASM_ENABLE_AOT != 0
  1917. if (module_inst->module_type == Wasm_Module_AoT) {
  1918. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  1919. }
  1920. #endif
  1921. return 0;
  1922. }
  1923. bool
  1924. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  1925. uint32 app_offset, uint32 size)
  1926. {
  1927. #if WASM_ENABLE_INTERP != 0
  1928. if (module_inst->module_type == Wasm_Module_Bytecode)
  1929. return wasm_validate_app_addr((WASMModuleInstance *)module_inst,
  1930. app_offset, size);
  1931. #endif
  1932. #if WASM_ENABLE_AOT != 0
  1933. if (module_inst->module_type == Wasm_Module_AoT)
  1934. return aot_validate_app_addr((AOTModuleInstance *)module_inst,
  1935. app_offset, size);
  1936. #endif
  1937. return false;
  1938. }
  1939. bool
  1940. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  1941. uint32 app_str_offset)
  1942. {
  1943. uint32 app_end_offset;
  1944. char *str, *str_end;
  1945. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset, NULL,
  1946. &app_end_offset))
  1947. goto fail;
  1948. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  1949. str_end = str + (app_end_offset - app_str_offset);
  1950. while (str < str_end && *str != '\0')
  1951. str++;
  1952. if (str == str_end)
  1953. goto fail;
  1954. return true;
  1955. fail:
  1956. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1957. return false;
  1958. }
  1959. bool
  1960. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  1961. void *native_ptr, uint32 size)
  1962. {
  1963. #if WASM_ENABLE_INTERP != 0
  1964. if (module_inst->module_type == Wasm_Module_Bytecode)
  1965. return wasm_validate_native_addr((WASMModuleInstance *)module_inst,
  1966. native_ptr, size);
  1967. #endif
  1968. #if WASM_ENABLE_AOT != 0
  1969. if (module_inst->module_type == Wasm_Module_AoT)
  1970. return aot_validate_native_addr((AOTModuleInstance *)module_inst,
  1971. native_ptr, size);
  1972. #endif
  1973. return false;
  1974. }
  1975. void *
  1976. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  1977. uint32 app_offset)
  1978. {
  1979. #if WASM_ENABLE_INTERP != 0
  1980. if (module_inst->module_type == Wasm_Module_Bytecode)
  1981. return wasm_addr_app_to_native((WASMModuleInstance *)module_inst,
  1982. app_offset);
  1983. #endif
  1984. #if WASM_ENABLE_AOT != 0
  1985. if (module_inst->module_type == Wasm_Module_AoT)
  1986. return aot_addr_app_to_native((AOTModuleInstance *)module_inst,
  1987. app_offset);
  1988. #endif
  1989. return NULL;
  1990. }
  1991. uint32
  1992. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  1993. void *native_ptr)
  1994. {
  1995. #if WASM_ENABLE_INTERP != 0
  1996. if (module_inst->module_type == Wasm_Module_Bytecode)
  1997. return wasm_addr_native_to_app((WASMModuleInstance *)module_inst,
  1998. native_ptr);
  1999. #endif
  2000. #if WASM_ENABLE_AOT != 0
  2001. if (module_inst->module_type == Wasm_Module_AoT)
  2002. return aot_addr_native_to_app((AOTModuleInstance *)module_inst,
  2003. native_ptr);
  2004. #endif
  2005. return 0;
  2006. }
  2007. bool
  2008. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  2009. uint32 app_offset, uint32 *p_app_start_offset,
  2010. uint32 *p_app_end_offset)
  2011. {
  2012. #if WASM_ENABLE_INTERP != 0
  2013. if (module_inst->module_type == Wasm_Module_Bytecode)
  2014. return wasm_get_app_addr_range((WASMModuleInstance *)module_inst,
  2015. app_offset, p_app_start_offset,
  2016. p_app_end_offset);
  2017. #endif
  2018. #if WASM_ENABLE_AOT != 0
  2019. if (module_inst->module_type == Wasm_Module_AoT)
  2020. return aot_get_app_addr_range((AOTModuleInstance *)module_inst,
  2021. app_offset, p_app_start_offset,
  2022. p_app_end_offset);
  2023. #endif
  2024. return false;
  2025. }
  2026. bool
  2027. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  2028. uint8 *native_ptr,
  2029. uint8 **p_native_start_addr,
  2030. uint8 **p_native_end_addr)
  2031. {
  2032. #if WASM_ENABLE_INTERP != 0
  2033. if (module_inst->module_type == Wasm_Module_Bytecode)
  2034. return wasm_get_native_addr_range((WASMModuleInstance *)module_inst,
  2035. native_ptr, p_native_start_addr,
  2036. p_native_end_addr);
  2037. #endif
  2038. #if WASM_ENABLE_AOT != 0
  2039. if (module_inst->module_type == Wasm_Module_AoT)
  2040. return aot_get_native_addr_range((AOTModuleInstance *)module_inst,
  2041. native_ptr, p_native_start_addr,
  2042. p_native_end_addr);
  2043. #endif
  2044. return false;
  2045. }
  2046. bool
  2047. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  2048. uint32 inc_page_count)
  2049. {
  2050. #if WASM_ENABLE_INTERP != 0
  2051. if (module->module_type == Wasm_Module_Bytecode)
  2052. return wasm_enlarge_memory((WASMModuleInstance *)module,
  2053. inc_page_count);
  2054. #endif
  2055. #if WASM_ENABLE_AOT != 0
  2056. if (module->module_type == Wasm_Module_AoT)
  2057. return aot_enlarge_memory((AOTModuleInstance *)module, inc_page_count);
  2058. #endif
  2059. return false;
  2060. }
  2061. #if WASM_ENABLE_LIBC_WASI != 0
  2062. static WASIArguments *
  2063. get_wasi_args_from_module(wasm_module_t module)
  2064. {
  2065. WASIArguments *wasi_args = NULL;
  2066. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  2067. if (module->module_type == Wasm_Module_Bytecode)
  2068. wasi_args = &((WASMModule *)module)->wasi_args;
  2069. #endif
  2070. #if WASM_ENABLE_AOT != 0
  2071. if (module->module_type == Wasm_Module_AoT)
  2072. wasi_args = &((AOTModule *)module)->wasi_args;
  2073. #endif
  2074. return wasi_args;
  2075. }
  2076. void
  2077. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  2078. uint32 dir_count, const char *map_dir_list[],
  2079. uint32 map_dir_count, const char *env_list[],
  2080. uint32 env_count, char *argv[], int argc,
  2081. int stdinfd, int stdoutfd, int stderrfd)
  2082. {
  2083. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2084. if (wasi_args) {
  2085. wasi_args->dir_list = dir_list;
  2086. wasi_args->dir_count = dir_count;
  2087. wasi_args->map_dir_list = map_dir_list;
  2088. wasi_args->map_dir_count = map_dir_count;
  2089. wasi_args->env = env_list;
  2090. wasi_args->env_count = env_count;
  2091. wasi_args->argv = argv;
  2092. wasi_args->argc = (uint32)argc;
  2093. wasi_args->stdio[0] = stdinfd;
  2094. wasi_args->stdio[1] = stdoutfd;
  2095. wasi_args->stdio[2] = stderrfd;
  2096. }
  2097. }
  2098. void
  2099. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  2100. uint32 dir_count, const char *map_dir_list[],
  2101. uint32 map_dir_count, const char *env_list[],
  2102. uint32 env_count, char *argv[], int argc)
  2103. {
  2104. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  2105. map_dir_count, env_list, env_count, argv,
  2106. argc, -1, -1, -1);
  2107. }
  2108. void
  2109. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  2110. uint32 addr_pool_size)
  2111. {
  2112. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2113. if (wasi_args) {
  2114. wasi_args->addr_pool = addr_pool;
  2115. wasi_args->addr_count = addr_pool_size;
  2116. }
  2117. }
  2118. void
  2119. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  2120. const char *ns_lookup_pool[],
  2121. uint32 ns_lookup_pool_size)
  2122. {
  2123. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  2124. if (wasi_args) {
  2125. wasi_args->ns_lookup_pool = ns_lookup_pool;
  2126. wasi_args->ns_lookup_count = ns_lookup_pool_size;
  2127. }
  2128. }
  2129. #if WASM_ENABLE_UVWASI == 0
  2130. static bool
  2131. copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
  2132. char ***list_ptr, uint64 *out_buf_size)
  2133. {
  2134. uint64 buf_size = 0, total_size;
  2135. uint32 buf_offset = 0, i;
  2136. char *buf = NULL, **list = NULL;
  2137. for (i = 0; i < array_size; i++)
  2138. buf_size += strlen(array[i]) + 1;
  2139. /* We add +1 to generate null-terminated array of strings */
  2140. total_size = sizeof(char *) * (uint64)array_size + 1;
  2141. if (total_size >= UINT32_MAX
  2142. || (total_size > 0 && !(list = wasm_runtime_malloc((uint32)total_size)))
  2143. || buf_size >= UINT32_MAX
  2144. || (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
  2145. if (buf)
  2146. wasm_runtime_free(buf);
  2147. if (list)
  2148. wasm_runtime_free(list);
  2149. return false;
  2150. }
  2151. for (i = 0; i < array_size; i++) {
  2152. list[i] = buf + buf_offset;
  2153. bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
  2154. buf_offset += (uint32)(strlen(array[i]) + 1);
  2155. }
  2156. list[array_size] = NULL;
  2157. *list_ptr = list;
  2158. *buf_ptr = buf;
  2159. if (out_buf_size)
  2160. *out_buf_size = buf_size;
  2161. return true;
  2162. }
  2163. bool
  2164. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2165. const char *dir_list[], uint32 dir_count,
  2166. const char *map_dir_list[], uint32 map_dir_count,
  2167. const char *env[], uint32 env_count,
  2168. const char *addr_pool[], uint32 addr_pool_size,
  2169. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  2170. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  2171. int stderrfd, char *error_buf, uint32 error_buf_size)
  2172. {
  2173. WASIContext *wasi_ctx;
  2174. char *argv_buf = NULL;
  2175. char **argv_list = NULL;
  2176. char *env_buf = NULL;
  2177. char **env_list = NULL;
  2178. char *ns_lookup_buf = NULL;
  2179. char **ns_lookup_list = NULL;
  2180. uint64 argv_buf_size = 0, env_buf_size = 0;
  2181. struct fd_table *curfds = NULL;
  2182. struct fd_prestats *prestats = NULL;
  2183. struct argv_environ_values *argv_environ = NULL;
  2184. struct addr_pool *apool = NULL;
  2185. bool fd_table_inited = false, fd_prestats_inited = false;
  2186. bool argv_environ_inited = false;
  2187. bool addr_pool_inited = false;
  2188. __wasi_fd_t wasm_fd = 3;
  2189. int32 raw_fd;
  2190. char *path, resolved_path[PATH_MAX];
  2191. uint32 i;
  2192. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  2193. error_buf_size))) {
  2194. return false;
  2195. }
  2196. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  2197. #if WASM_ENABLE_INTERP != 0
  2198. if (module_inst->module_type == Wasm_Module_Bytecode
  2199. && !((WASMModuleInstance *)module_inst)->default_memory)
  2200. return true;
  2201. #endif
  2202. #if WASM_ENABLE_AOT != 0
  2203. if (module_inst->module_type == Wasm_Module_AoT
  2204. && !((AOTModuleInstance *)module_inst)
  2205. ->global_table_data.memory_instances[0]
  2206. .memory_data.ptr)
  2207. return true;
  2208. #endif
  2209. /* process argv[0], trip the path and suffix, only keep the program name */
  2210. if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
  2211. &argv_buf_size)) {
  2212. set_error_buf(error_buf, error_buf_size,
  2213. "Init wasi environment failed: allocate memory failed");
  2214. goto fail;
  2215. }
  2216. if (!copy_string_array(env, env_count, &env_buf, &env_list,
  2217. &env_buf_size)) {
  2218. set_error_buf(error_buf, error_buf_size,
  2219. "Init wasi environment failed: allocate memory failed");
  2220. goto fail;
  2221. }
  2222. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  2223. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  2224. || !(argv_environ =
  2225. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  2226. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  2227. set_error_buf(error_buf, error_buf_size,
  2228. "Init wasi environment failed: allocate memory failed");
  2229. goto fail;
  2230. }
  2231. if (!fd_table_init(curfds)) {
  2232. set_error_buf(error_buf, error_buf_size,
  2233. "Init wasi environment failed: "
  2234. "init fd table failed");
  2235. goto fail;
  2236. }
  2237. fd_table_inited = true;
  2238. if (!fd_prestats_init(prestats)) {
  2239. set_error_buf(error_buf, error_buf_size,
  2240. "Init wasi environment failed: "
  2241. "init fd prestats failed");
  2242. goto fail;
  2243. }
  2244. fd_prestats_inited = true;
  2245. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  2246. argc, env_buf, env_buf_size, env_list, env_count)) {
  2247. set_error_buf(error_buf, error_buf_size,
  2248. "Init wasi environment failed: "
  2249. "init argument environment failed");
  2250. goto fail;
  2251. }
  2252. argv_environ_inited = true;
  2253. if (!addr_pool_init(apool)) {
  2254. set_error_buf(error_buf, error_buf_size,
  2255. "Init wasi environment failed: "
  2256. "init the address pool failed");
  2257. goto fail;
  2258. }
  2259. addr_pool_inited = true;
  2260. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  2261. if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
  2262. || !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
  2263. || !fd_table_insert_existing(curfds, 2,
  2264. (stderrfd != -1) ? stderrfd : 2)) {
  2265. set_error_buf(error_buf, error_buf_size,
  2266. "Init wasi environment failed: init fd table failed");
  2267. goto fail;
  2268. }
  2269. wasm_fd = 3;
  2270. for (i = 0; i < dir_count; i++, wasm_fd++) {
  2271. path = realpath(dir_list[i], resolved_path);
  2272. if (!path) {
  2273. if (error_buf)
  2274. snprintf(error_buf, error_buf_size,
  2275. "error while pre-opening directory %s: %d\n",
  2276. dir_list[i], errno);
  2277. goto fail;
  2278. }
  2279. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  2280. if (raw_fd == -1) {
  2281. if (error_buf)
  2282. snprintf(error_buf, error_buf_size,
  2283. "error while pre-opening directory %s: %d\n",
  2284. dir_list[i], errno);
  2285. goto fail;
  2286. }
  2287. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  2288. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  2289. }
  2290. /* addr_pool(textual) -> apool */
  2291. for (i = 0; i < addr_pool_size; i++) {
  2292. char *cp, *address, *mask;
  2293. bool ret = false;
  2294. cp = bh_strdup(addr_pool[i]);
  2295. if (!cp) {
  2296. set_error_buf(error_buf, error_buf_size,
  2297. "Init wasi environment failed: copy address failed");
  2298. goto fail;
  2299. }
  2300. address = strtok(cp, "/");
  2301. mask = strtok(NULL, "/");
  2302. ret = addr_pool_insert(apool, address, (uint8)(mask ? atoi(mask) : 0));
  2303. wasm_runtime_free(cp);
  2304. if (!ret) {
  2305. set_error_buf(error_buf, error_buf_size,
  2306. "Init wasi environment failed: store address failed");
  2307. goto fail;
  2308. }
  2309. }
  2310. if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
  2311. &ns_lookup_list, NULL)) {
  2312. set_error_buf(error_buf, error_buf_size,
  2313. "Init wasi environment failed: allocate memory failed");
  2314. goto fail;
  2315. }
  2316. wasi_ctx->curfds = curfds;
  2317. wasi_ctx->prestats = prestats;
  2318. wasi_ctx->argv_environ = argv_environ;
  2319. wasi_ctx->addr_pool = apool;
  2320. wasi_ctx->argv_buf = argv_buf;
  2321. wasi_ctx->argv_list = argv_list;
  2322. wasi_ctx->env_buf = env_buf;
  2323. wasi_ctx->env_list = env_list;
  2324. wasi_ctx->ns_lookup_buf = ns_lookup_buf;
  2325. wasi_ctx->ns_lookup_list = ns_lookup_list;
  2326. return true;
  2327. fail:
  2328. if (argv_environ_inited)
  2329. argv_environ_destroy(argv_environ);
  2330. if (fd_prestats_inited)
  2331. fd_prestats_destroy(prestats);
  2332. if (fd_table_inited)
  2333. fd_table_destroy(curfds);
  2334. if (addr_pool_inited)
  2335. addr_pool_destroy(apool);
  2336. if (curfds)
  2337. wasm_runtime_free(curfds);
  2338. if (prestats)
  2339. wasm_runtime_free(prestats);
  2340. if (argv_environ)
  2341. wasm_runtime_free(argv_environ);
  2342. if (apool)
  2343. wasm_runtime_free(apool);
  2344. if (argv_buf)
  2345. wasm_runtime_free(argv_buf);
  2346. if (argv_list)
  2347. wasm_runtime_free(argv_list);
  2348. if (env_buf)
  2349. wasm_runtime_free(env_buf);
  2350. if (env_list)
  2351. wasm_runtime_free(env_list);
  2352. if (ns_lookup_buf)
  2353. wasm_runtime_free(ns_lookup_buf);
  2354. if (ns_lookup_list)
  2355. wasm_runtime_free(ns_lookup_list);
  2356. return false;
  2357. }
  2358. #else /* else of WASM_ENABLE_UVWASI == 0 */
  2359. static void *
  2360. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  2361. {
  2362. return runtime_malloc(size, NULL, NULL, 0);
  2363. (void)mem_user_data;
  2364. }
  2365. static void
  2366. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  2367. {
  2368. if (ptr)
  2369. wasm_runtime_free(ptr);
  2370. (void)mem_user_data;
  2371. }
  2372. static void *
  2373. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  2374. {
  2375. uint64 total_size = (uint64)nmemb * size;
  2376. return runtime_malloc(total_size, NULL, NULL, 0);
  2377. (void)mem_user_data;
  2378. }
  2379. static void *
  2380. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  2381. {
  2382. if (size >= UINT32_MAX) {
  2383. return NULL;
  2384. }
  2385. return wasm_runtime_realloc(ptr, (uint32)size);
  2386. }
  2387. /* clang-format off */
  2388. static uvwasi_mem_t uvwasi_allocator = {
  2389. .mem_user_data = 0,
  2390. .malloc = wasm_uvwasi_malloc,
  2391. .free = wasm_uvwasi_free,
  2392. .calloc = wasm_uvwasi_calloc,
  2393. .realloc = wasm_uvwasi_realloc
  2394. };
  2395. /* clang-format on */
  2396. bool
  2397. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2398. const char *dir_list[], uint32 dir_count,
  2399. const char *map_dir_list[], uint32 map_dir_count,
  2400. const char *env[], uint32 env_count,
  2401. const char *addr_pool[], uint32 addr_pool_size,
  2402. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  2403. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  2404. int stderrfd, char *error_buf, uint32 error_buf_size)
  2405. {
  2406. uvwasi_t *uvwasi = NULL;
  2407. uvwasi_options_t init_options;
  2408. const char **envp = NULL;
  2409. uint64 total_size;
  2410. uint32 i;
  2411. bool ret = false;
  2412. uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, error_buf,
  2413. error_buf_size);
  2414. if (!uvwasi)
  2415. return false;
  2416. /* Setup the initialization options */
  2417. uvwasi_options_init(&init_options);
  2418. init_options.allocator = &uvwasi_allocator;
  2419. init_options.argc = argc;
  2420. init_options.argv = (const char **)argv;
  2421. init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
  2422. init_options.out =
  2423. (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
  2424. init_options.err =
  2425. (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
  2426. if (dir_count > 0) {
  2427. init_options.preopenc = dir_count;
  2428. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  2429. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  2430. total_size, module_inst, error_buf, error_buf_size);
  2431. if (init_options.preopens == NULL)
  2432. goto fail;
  2433. for (i = 0; i < init_options.preopenc; i++) {
  2434. init_options.preopens[i].real_path = dir_list[i];
  2435. init_options.preopens[i].mapped_path =
  2436. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  2437. }
  2438. }
  2439. if (env_count > 0) {
  2440. total_size = sizeof(char *) * (uint64)(env_count + 1);
  2441. envp =
  2442. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  2443. if (envp == NULL)
  2444. goto fail;
  2445. for (i = 0; i < env_count; i++) {
  2446. envp[i] = env[i];
  2447. }
  2448. envp[env_count] = NULL;
  2449. init_options.envp = envp;
  2450. }
  2451. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  2452. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  2453. goto fail;
  2454. }
  2455. wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
  2456. ret = true;
  2457. fail:
  2458. if (envp)
  2459. wasm_runtime_free((void *)envp);
  2460. if (init_options.preopens)
  2461. wasm_runtime_free(init_options.preopens);
  2462. if (!ret && uvwasi)
  2463. wasm_runtime_free(uvwasi);
  2464. return ret;
  2465. }
  2466. #endif /* end of WASM_ENABLE_UVWASI */
  2467. bool
  2468. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  2469. {
  2470. #if WASM_ENABLE_INTERP != 0
  2471. if (module_inst->module_type == Wasm_Module_Bytecode
  2472. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  2473. return true;
  2474. #endif
  2475. #if WASM_ENABLE_AOT != 0
  2476. if (module_inst->module_type == Wasm_Module_AoT
  2477. && ((AOTModule *)((AOTModuleInstance *)module_inst)->aot_module.ptr)
  2478. ->import_wasi_api)
  2479. return true;
  2480. #endif
  2481. return false;
  2482. }
  2483. WASMFunctionInstanceCommon *
  2484. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  2485. {
  2486. uint32 i;
  2487. #if WASM_ENABLE_INTERP != 0
  2488. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2489. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  2490. WASMFunctionInstance *func;
  2491. for (i = 0; i < wasm_inst->export_func_count; i++) {
  2492. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  2493. func = wasm_inst->export_functions[i].function;
  2494. if (func->u.func->func_type->param_count != 0
  2495. || func->u.func->func_type->result_count != 0) {
  2496. LOG_ERROR("Lookup wasi _start function failed: "
  2497. "invalid function type.\n");
  2498. return NULL;
  2499. }
  2500. return (WASMFunctionInstanceCommon *)func;
  2501. }
  2502. }
  2503. return NULL;
  2504. }
  2505. #endif
  2506. #if WASM_ENABLE_AOT != 0
  2507. if (module_inst->module_type == Wasm_Module_AoT) {
  2508. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  2509. AOTFunctionInstance *export_funcs =
  2510. (AOTFunctionInstance *)aot_inst->export_funcs.ptr;
  2511. for (i = 0; i < aot_inst->export_func_count; i++) {
  2512. if (!strcmp(export_funcs[i].func_name, "_start")) {
  2513. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  2514. if (func_type->param_count != 0
  2515. || func_type->result_count != 0) {
  2516. LOG_ERROR("Lookup wasi _start function failed: "
  2517. "invalid function type.\n");
  2518. return NULL;
  2519. }
  2520. return (WASMFunctionInstanceCommon *)&export_funcs[i];
  2521. }
  2522. }
  2523. return NULL;
  2524. }
  2525. #endif /* end of WASM_ENABLE_AOT */
  2526. return NULL;
  2527. }
  2528. #if WASM_ENABLE_UVWASI == 0
  2529. void
  2530. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  2531. {
  2532. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  2533. if (wasi_ctx) {
  2534. if (wasi_ctx->argv_environ) {
  2535. argv_environ_destroy(wasi_ctx->argv_environ);
  2536. wasm_runtime_free(wasi_ctx->argv_environ);
  2537. }
  2538. if (wasi_ctx->curfds) {
  2539. fd_table_destroy(wasi_ctx->curfds);
  2540. wasm_runtime_free(wasi_ctx->curfds);
  2541. }
  2542. if (wasi_ctx->prestats) {
  2543. fd_prestats_destroy(wasi_ctx->prestats);
  2544. wasm_runtime_free(wasi_ctx->prestats);
  2545. }
  2546. if (wasi_ctx->addr_pool) {
  2547. addr_pool_destroy(wasi_ctx->addr_pool);
  2548. wasm_runtime_free(wasi_ctx->addr_pool);
  2549. }
  2550. if (wasi_ctx->argv_buf)
  2551. wasm_runtime_free(wasi_ctx->argv_buf);
  2552. if (wasi_ctx->argv_list)
  2553. wasm_runtime_free(wasi_ctx->argv_list);
  2554. if (wasi_ctx->env_buf)
  2555. wasm_runtime_free(wasi_ctx->env_buf);
  2556. if (wasi_ctx->env_list)
  2557. wasm_runtime_free(wasi_ctx->env_list);
  2558. if (wasi_ctx->ns_lookup_buf)
  2559. wasm_runtime_free(wasi_ctx->ns_lookup_buf);
  2560. if (wasi_ctx->ns_lookup_list)
  2561. wasm_runtime_free(wasi_ctx->ns_lookup_list);
  2562. wasm_runtime_free(wasi_ctx);
  2563. }
  2564. }
  2565. #else
  2566. void
  2567. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  2568. {
  2569. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  2570. if (wasi_ctx) {
  2571. uvwasi_destroy(wasi_ctx);
  2572. wasm_runtime_free(wasi_ctx);
  2573. }
  2574. }
  2575. #endif
  2576. WASIContext *
  2577. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  2578. {
  2579. #if WASM_ENABLE_INTERP != 0
  2580. if (module_inst->module_type == Wasm_Module_Bytecode)
  2581. return ((WASMModuleInstance *)module_inst)->wasi_ctx;
  2582. #endif
  2583. #if WASM_ENABLE_AOT != 0
  2584. if (module_inst->module_type == Wasm_Module_AoT)
  2585. return ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr;
  2586. #endif
  2587. return NULL;
  2588. }
  2589. void
  2590. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  2591. WASIContext *wasi_ctx)
  2592. {
  2593. #if WASM_ENABLE_INTERP != 0
  2594. if (module_inst->module_type == Wasm_Module_Bytecode)
  2595. ((WASMModuleInstance *)module_inst)->wasi_ctx = wasi_ctx;
  2596. #endif
  2597. #if WASM_ENABLE_AOT != 0
  2598. if (module_inst->module_type == Wasm_Module_AoT)
  2599. ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr = wasi_ctx;
  2600. #endif
  2601. }
  2602. #endif /* end of WASM_ENABLE_LIBC_WASI */
  2603. WASMModuleCommon *
  2604. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  2605. {
  2606. WASMModuleInstanceCommon *module_inst =
  2607. wasm_runtime_get_module_inst(exec_env);
  2608. #if WASM_ENABLE_INTERP != 0
  2609. if (module_inst->module_type == Wasm_Module_Bytecode)
  2610. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  2611. #endif
  2612. #if WASM_ENABLE_AOT != 0
  2613. if (module_inst->module_type == Wasm_Module_AoT)
  2614. return (WASMModuleCommon *)((AOTModuleInstance *)module_inst)
  2615. ->aot_module.ptr;
  2616. #endif
  2617. return NULL;
  2618. }
  2619. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  2620. const uint8 *
  2621. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  2622. const char *name, uint32 *len)
  2623. {
  2624. #if WASM_ENABLE_INTERP != 0
  2625. if (module_comm->module_type == Wasm_Module_Bytecode)
  2626. return wasm_loader_get_custom_section((WASMModule *)module_comm, name,
  2627. len);
  2628. #endif
  2629. #if WASM_ENABLE_AOT != 0
  2630. if (module_comm->module_type == Wasm_Module_AoT)
  2631. return aot_get_custom_section((AOTModule *)module_comm, name, len);
  2632. #endif
  2633. return NULL;
  2634. }
  2635. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  2636. static union {
  2637. int a;
  2638. char b;
  2639. } __ue = { .a = 1 };
  2640. #define is_little_endian() (__ue.b == 1)
  2641. bool
  2642. wasm_runtime_register_natives(const char *module_name,
  2643. NativeSymbol *native_symbols,
  2644. uint32 n_native_symbols)
  2645. {
  2646. return wasm_native_register_natives(module_name, native_symbols,
  2647. n_native_symbols);
  2648. }
  2649. bool
  2650. wasm_runtime_register_natives_raw(const char *module_name,
  2651. NativeSymbol *native_symbols,
  2652. uint32 n_native_symbols)
  2653. {
  2654. return wasm_native_register_natives_raw(module_name, native_symbols,
  2655. n_native_symbols);
  2656. }
  2657. bool
  2658. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  2659. const WASMType *func_type, const char *signature,
  2660. void *attachment, uint32 *argv, uint32 argc,
  2661. uint32 *argv_ret)
  2662. {
  2663. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2664. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  2665. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  2666. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  2667. uint32 *argv_src = argv, i, argc1, ptr_len;
  2668. uint32 arg_i32;
  2669. bool ret = false;
  2670. argc1 = func_type->param_count;
  2671. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2672. size = sizeof(uint64) * (uint64)argc1;
  2673. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  2674. 0))) {
  2675. return false;
  2676. }
  2677. }
  2678. argv_dst = argv1;
  2679. /* Traverse secondly to fill in each argument */
  2680. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  2681. switch (func_type->types[i]) {
  2682. case VALUE_TYPE_I32:
  2683. #if WASM_ENABLE_REF_TYPES != 0
  2684. case VALUE_TYPE_FUNCREF:
  2685. #endif
  2686. {
  2687. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  2688. if (signature) {
  2689. if (signature[i + 1] == '*') {
  2690. /* param is a pointer */
  2691. if (signature[i + 2] == '~')
  2692. /* pointer with length followed */
  2693. ptr_len = *argv_src;
  2694. else
  2695. /* pointer without length followed */
  2696. ptr_len = 1;
  2697. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  2698. ptr_len))
  2699. goto fail;
  2700. *(uintptr_t *)argv_dst =
  2701. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  2702. arg_i32);
  2703. }
  2704. else if (signature[i + 1] == '$') {
  2705. /* param is a string */
  2706. if (!wasm_runtime_validate_app_str_addr(module,
  2707. arg_i32))
  2708. goto fail;
  2709. *(uintptr_t *)argv_dst =
  2710. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  2711. arg_i32);
  2712. }
  2713. }
  2714. break;
  2715. }
  2716. case VALUE_TYPE_I64:
  2717. case VALUE_TYPE_F64:
  2718. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  2719. sizeof(uint32) * 2);
  2720. argv_src += 2;
  2721. break;
  2722. case VALUE_TYPE_F32:
  2723. *(float32 *)argv_dst = *(float32 *)argv_src++;
  2724. break;
  2725. #if WASM_ENABLE_REF_TYPES != 0
  2726. case VALUE_TYPE_EXTERNREF:
  2727. {
  2728. uint32 externref_idx = *argv_src++;
  2729. void *externref_obj;
  2730. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  2731. goto fail;
  2732. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  2733. sizeof(uintptr_t));
  2734. break;
  2735. }
  2736. #endif
  2737. default:
  2738. bh_assert(0);
  2739. break;
  2740. }
  2741. }
  2742. exec_env->attachment = attachment;
  2743. invokeNativeRaw(exec_env, argv1);
  2744. exec_env->attachment = NULL;
  2745. if (func_type->result_count > 0) {
  2746. switch (func_type->types[func_type->param_count]) {
  2747. case VALUE_TYPE_I32:
  2748. #if WASM_ENABLE_REF_TYPES != 0
  2749. case VALUE_TYPE_FUNCREF:
  2750. #endif
  2751. argv_ret[0] = *(uint32 *)argv1;
  2752. break;
  2753. case VALUE_TYPE_F32:
  2754. *(float32 *)argv_ret = *(float32 *)argv1;
  2755. break;
  2756. case VALUE_TYPE_I64:
  2757. case VALUE_TYPE_F64:
  2758. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  2759. sizeof(uint64));
  2760. break;
  2761. #if WASM_ENABLE_REF_TYPES != 0
  2762. case VALUE_TYPE_EXTERNREF:
  2763. {
  2764. uint32 externref_idx;
  2765. uint64 externref_obj;
  2766. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  2767. sizeof(uint64));
  2768. if (!wasm_externref_obj2ref(exec_env->module_inst,
  2769. (void *)(uintptr_t)externref_obj,
  2770. &externref_idx))
  2771. goto fail;
  2772. argv_ret[0] = externref_idx;
  2773. break;
  2774. }
  2775. #endif
  2776. default:
  2777. bh_assert(0);
  2778. break;
  2779. }
  2780. }
  2781. ret = !wasm_runtime_get_exception(module) ? true : false;
  2782. fail:
  2783. if (argv1 != argv_buf)
  2784. wasm_runtime_free(argv1);
  2785. return ret;
  2786. }
  2787. /**
  2788. * Implementation of wasm_runtime_invoke_native()
  2789. */
  2790. /* The invoke native implementation on ARM platform with VFP co-processor */
  2791. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  2792. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  2793. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2794. typedef void (*GenericFunctionPointer)();
  2795. void
  2796. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2797. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2798. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2799. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2800. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2801. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2802. static volatile Float64FuncPtr invokeNative_Float64 =
  2803. (Float64FuncPtr)(uintptr_t)invokeNative;
  2804. static volatile Float32FuncPtr invokeNative_Float32 =
  2805. (Float32FuncPtr)(uintptr_t)invokeNative;
  2806. static volatile Int64FuncPtr invokeNative_Int64 =
  2807. (Int64FuncPtr)(uintptr_t)invokeNative;
  2808. static volatile Int32FuncPtr invokeNative_Int32 =
  2809. (Int32FuncPtr)(uintptr_t)invokeNative;
  2810. static volatile VoidFuncPtr invokeNative_Void =
  2811. (VoidFuncPtr)(uintptr_t)invokeNative;
  2812. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2813. #define MAX_REG_INTS 4
  2814. #define MAX_REG_FLOATS 16
  2815. #else
  2816. #define MAX_REG_INTS 8
  2817. #define MAX_REG_FLOATS 8
  2818. #endif
  2819. bool
  2820. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2821. const WASMType *func_type, const char *signature,
  2822. void *attachment, uint32 *argv, uint32 argc,
  2823. uint32 *argv_ret)
  2824. {
  2825. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2826. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2827. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  2828. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2829. uint32 arg_i32, ptr_len;
  2830. uint32 result_count = func_type->result_count;
  2831. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2832. bool ret = false;
  2833. #if WASM_ENABLE_REF_TYPES != 0
  2834. bool is_aot_func = (NULL == signature);
  2835. #endif
  2836. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2837. uint32 *fps;
  2838. int n_fps = 0;
  2839. #else
  2840. #define fps ints
  2841. #define n_fps n_ints
  2842. #endif
  2843. n_ints++; /* exec env */
  2844. /* Traverse firstly to calculate stack args count */
  2845. for (i = 0; i < func_type->param_count; i++) {
  2846. switch (func_type->types[i]) {
  2847. case VALUE_TYPE_I32:
  2848. #if WASM_ENABLE_REF_TYPES != 0
  2849. case VALUE_TYPE_FUNCREF:
  2850. case VALUE_TYPE_EXTERNREF:
  2851. #endif
  2852. if (n_ints < MAX_REG_INTS)
  2853. n_ints++;
  2854. else
  2855. n_stacks++;
  2856. break;
  2857. case VALUE_TYPE_I64:
  2858. if (n_ints < MAX_REG_INTS - 1) {
  2859. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2860. /* 64-bit data must be 8 bytes aligned in arm */
  2861. if (n_ints & 1)
  2862. n_ints++;
  2863. #endif
  2864. n_ints += 2;
  2865. }
  2866. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  2867. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  2868. /* part in register, part in stack */
  2869. else if (n_ints == MAX_REG_INTS - 1) {
  2870. n_ints++;
  2871. n_stacks++;
  2872. }
  2873. #endif
  2874. else {
  2875. /* 64-bit data in stack must be 8 bytes aligned
  2876. in arm and riscv32 */
  2877. #if !defined(BUILD_TARGET_ARC)
  2878. if (n_stacks & 1)
  2879. n_stacks++;
  2880. #endif
  2881. n_stacks += 2;
  2882. }
  2883. break;
  2884. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2885. case VALUE_TYPE_F32:
  2886. if (n_fps < MAX_REG_FLOATS)
  2887. n_fps++;
  2888. else
  2889. n_stacks++;
  2890. break;
  2891. case VALUE_TYPE_F64:
  2892. if (n_fps < MAX_REG_FLOATS - 1) {
  2893. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2894. /* 64-bit data must be 8 bytes aligned in arm */
  2895. if (n_fps & 1)
  2896. n_fps++;
  2897. #endif
  2898. n_fps += 2;
  2899. }
  2900. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2901. else if (n_fps == MAX_REG_FLOATS - 1) {
  2902. n_fps++;
  2903. n_stacks++;
  2904. }
  2905. #endif
  2906. else {
  2907. /* 64-bit data in stack must be 8 bytes aligned
  2908. in arm and riscv32 */
  2909. #if !defined(BUILD_TARGET_ARC)
  2910. if (n_stacks & 1)
  2911. n_stacks++;
  2912. #endif
  2913. n_stacks += 2;
  2914. }
  2915. break;
  2916. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2917. case VALUE_TYPE_F32:
  2918. case VALUE_TYPE_F64:
  2919. if (n_fps < MAX_REG_FLOATS) {
  2920. n_fps++;
  2921. }
  2922. else if (func_type->types[i] == VALUE_TYPE_F32
  2923. && n_ints < MAX_REG_INTS) {
  2924. /* use int reg firstly if available */
  2925. n_ints++;
  2926. }
  2927. else if (func_type->types[i] == VALUE_TYPE_F64
  2928. && n_ints < MAX_REG_INTS - 1) {
  2929. /* use int regs firstly if available */
  2930. if (n_ints & 1)
  2931. n_ints++;
  2932. ints += 2;
  2933. }
  2934. else {
  2935. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  2936. */
  2937. if (n_stacks & 1)
  2938. n_stacks++;
  2939. n_stacks += 2;
  2940. }
  2941. break;
  2942. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2943. default:
  2944. bh_assert(0);
  2945. break;
  2946. }
  2947. }
  2948. for (i = 0; i < ext_ret_count; i++) {
  2949. if (n_ints < MAX_REG_INTS)
  2950. n_ints++;
  2951. else
  2952. n_stacks++;
  2953. }
  2954. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2955. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2956. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2957. argc1 = MAX_REG_INTS + n_stacks;
  2958. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  2959. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  2960. #endif
  2961. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2962. size = sizeof(uint32) * (uint32)argc1;
  2963. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  2964. 0))) {
  2965. return false;
  2966. }
  2967. }
  2968. ints = argv1;
  2969. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2970. fps = ints + MAX_REG_INTS;
  2971. stacks = fps + MAX_REG_FLOATS;
  2972. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2973. stacks = ints + MAX_REG_INTS;
  2974. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  2975. fps = ints + MAX_REG_INTS;
  2976. stacks = fps + MAX_REG_FLOATS * 2;
  2977. #endif
  2978. n_ints = 0;
  2979. n_fps = 0;
  2980. n_stacks = 0;
  2981. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2982. /* Traverse secondly to fill in each argument */
  2983. for (i = 0; i < func_type->param_count; i++) {
  2984. switch (func_type->types[i]) {
  2985. case VALUE_TYPE_I32:
  2986. #if WASM_ENABLE_REF_TYPES != 0
  2987. case VALUE_TYPE_FUNCREF:
  2988. #endif
  2989. {
  2990. arg_i32 = *argv_src++;
  2991. if (signature) {
  2992. if (signature[i + 1] == '*') {
  2993. /* param is a pointer */
  2994. if (signature[i + 2] == '~')
  2995. /* pointer with length followed */
  2996. ptr_len = *argv_src;
  2997. else
  2998. /* pointer without length followed */
  2999. ptr_len = 1;
  3000. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3001. ptr_len))
  3002. goto fail;
  3003. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3004. module, arg_i32);
  3005. }
  3006. else if (signature[i + 1] == '$') {
  3007. /* param is a string */
  3008. if (!wasm_runtime_validate_app_str_addr(module,
  3009. arg_i32))
  3010. goto fail;
  3011. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3012. module, arg_i32);
  3013. }
  3014. }
  3015. if (n_ints < MAX_REG_INTS)
  3016. ints[n_ints++] = arg_i32;
  3017. else
  3018. stacks[n_stacks++] = arg_i32;
  3019. break;
  3020. }
  3021. case VALUE_TYPE_I64:
  3022. {
  3023. if (n_ints < MAX_REG_INTS - 1) {
  3024. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  3025. /* 64-bit data must be 8 bytes aligned in arm */
  3026. if (n_ints & 1)
  3027. n_ints++;
  3028. #endif
  3029. ints[n_ints++] = *argv_src++;
  3030. ints[n_ints++] = *argv_src++;
  3031. }
  3032. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  3033. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  3034. else if (n_ints == MAX_REG_INTS - 1) {
  3035. ints[n_ints++] = *argv_src++;
  3036. stacks[n_stacks++] = *argv_src++;
  3037. }
  3038. #endif
  3039. else {
  3040. /* 64-bit data in stack must be 8 bytes aligned
  3041. in arm and riscv32 */
  3042. #if !defined(BUILD_TARGET_ARC)
  3043. if (n_stacks & 1)
  3044. n_stacks++;
  3045. #endif
  3046. stacks[n_stacks++] = *argv_src++;
  3047. stacks[n_stacks++] = *argv_src++;
  3048. }
  3049. break;
  3050. }
  3051. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  3052. case VALUE_TYPE_F32:
  3053. {
  3054. if (n_fps < MAX_REG_FLOATS)
  3055. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  3056. else
  3057. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  3058. break;
  3059. }
  3060. case VALUE_TYPE_F64:
  3061. {
  3062. if (n_fps < MAX_REG_FLOATS - 1) {
  3063. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  3064. /* 64-bit data must be 8 bytes aligned in arm */
  3065. if (n_fps & 1)
  3066. n_fps++;
  3067. #endif
  3068. fps[n_fps++] = *argv_src++;
  3069. fps[n_fps++] = *argv_src++;
  3070. }
  3071. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  3072. else if (n_fps == MAX_REG_FLOATS - 1) {
  3073. fps[n_fps++] = *argv_src++;
  3074. stacks[n_stacks++] = *argv_src++;
  3075. }
  3076. #endif
  3077. else {
  3078. /* 64-bit data in stack must be 8 bytes aligned
  3079. in arm and riscv32 */
  3080. #if !defined(BUILD_TARGET_ARC)
  3081. if (n_stacks & 1)
  3082. n_stacks++;
  3083. #endif
  3084. stacks[n_stacks++] = *argv_src++;
  3085. stacks[n_stacks++] = *argv_src++;
  3086. }
  3087. break;
  3088. }
  3089. #else /* BUILD_TARGET_RISCV32_ILP32D */
  3090. case VALUE_TYPE_F32:
  3091. case VALUE_TYPE_F64:
  3092. {
  3093. if (n_fps < MAX_REG_FLOATS) {
  3094. if (func_type->types[i] == VALUE_TYPE_F32) {
  3095. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  3096. /* NaN boxing, the upper bits of a valid NaN-boxed
  3097. value must be all 1s. */
  3098. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  3099. }
  3100. else {
  3101. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  3102. argv_src += 2;
  3103. }
  3104. n_fps++;
  3105. }
  3106. else if (func_type->types[i] == VALUE_TYPE_F32
  3107. && n_ints < MAX_REG_INTS) {
  3108. /* use int reg firstly if available */
  3109. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  3110. }
  3111. else if (func_type->types[i] == VALUE_TYPE_F64
  3112. && n_ints < MAX_REG_INTS - 1) {
  3113. /* use int regs firstly if available */
  3114. if (n_ints & 1)
  3115. n_ints++;
  3116. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  3117. n_ints += 2;
  3118. argv_src += 2;
  3119. }
  3120. else {
  3121. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  3122. */
  3123. if (n_stacks & 1)
  3124. n_stacks++;
  3125. if (func_type->types[i] == VALUE_TYPE_F32) {
  3126. *(float32 *)&stacks[n_stacks] = *(float32 *)argv_src++;
  3127. /* NaN boxing, the upper bits of a valid NaN-boxed
  3128. value must be all 1s. */
  3129. stacks[n_stacks + 1] = 0xFFFFFFFF;
  3130. }
  3131. else {
  3132. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  3133. argv_src += 2;
  3134. }
  3135. n_stacks += 2;
  3136. }
  3137. break;
  3138. }
  3139. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3140. #if WASM_ENABLE_REF_TYPES != 0
  3141. case VALUE_TYPE_EXTERNREF:
  3142. {
  3143. uint32 externref_idx = *argv_src++;
  3144. if (is_aot_func) {
  3145. if (n_ints < MAX_REG_INTS)
  3146. ints[n_ints++] = externref_idx;
  3147. else
  3148. stacks[n_stacks++] = externref_idx;
  3149. }
  3150. else {
  3151. void *externref_obj;
  3152. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3153. goto fail;
  3154. if (n_ints < MAX_REG_INTS)
  3155. ints[n_ints++] = (uintptr_t)externref_obj;
  3156. else
  3157. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3158. }
  3159. break;
  3160. }
  3161. #endif
  3162. default:
  3163. bh_assert(0);
  3164. break;
  3165. }
  3166. }
  3167. /* Save extra result values' address to argv1 */
  3168. for (i = 0; i < ext_ret_count; i++) {
  3169. if (n_ints < MAX_REG_INTS)
  3170. ints[n_ints++] = *(uint32 *)argv_src++;
  3171. else
  3172. stacks[n_stacks++] = *(uint32 *)argv_src++;
  3173. }
  3174. exec_env->attachment = attachment;
  3175. if (func_type->result_count == 0) {
  3176. invokeNative_Void(func_ptr, argv1, n_stacks);
  3177. }
  3178. else {
  3179. switch (func_type->types[func_type->param_count]) {
  3180. case VALUE_TYPE_I32:
  3181. #if WASM_ENABLE_REF_TYPES != 0
  3182. case VALUE_TYPE_FUNCREF:
  3183. #endif
  3184. argv_ret[0] =
  3185. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3186. break;
  3187. case VALUE_TYPE_I64:
  3188. PUT_I64_TO_ADDR(argv_ret,
  3189. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3190. break;
  3191. case VALUE_TYPE_F32:
  3192. *(float32 *)argv_ret =
  3193. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3194. break;
  3195. case VALUE_TYPE_F64:
  3196. PUT_F64_TO_ADDR(
  3197. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3198. break;
  3199. #if WASM_ENABLE_REF_TYPES != 0
  3200. case VALUE_TYPE_EXTERNREF:
  3201. {
  3202. if (is_aot_func) {
  3203. uint32 externref_idx =
  3204. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3205. argv_ret[0] = externref_idx;
  3206. }
  3207. else {
  3208. uint32 externref_idx;
  3209. void *externref_obj;
  3210. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3211. func_ptr, argv1, argc1);
  3212. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3213. externref_obj, &externref_idx))
  3214. goto fail;
  3215. argv_ret[0] = externref_idx;
  3216. }
  3217. break;
  3218. }
  3219. #endif
  3220. default:
  3221. bh_assert(0);
  3222. break;
  3223. }
  3224. }
  3225. exec_env->attachment = NULL;
  3226. ret = !wasm_runtime_get_exception(module) ? true : false;
  3227. fail:
  3228. if (argv1 != argv_buf)
  3229. wasm_runtime_free(argv1);
  3230. return ret;
  3231. }
  3232. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  3233. || defined(BUILD_TARGET_THUMB_VFP) \
  3234. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3235. || defined(BUILD_TARGET_RISCV32_ILP32) \
  3236. || defined(BUILD_TARGET_ARC) */
  3237. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  3238. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  3239. || defined(BUILD_TARGET_XTENSA)
  3240. typedef void (*GenericFunctionPointer)();
  3241. void
  3242. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  3243. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3244. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3245. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3246. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3247. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3248. static volatile Int64FuncPtr invokeNative_Int64 =
  3249. (Int64FuncPtr)(uintptr_t)invokeNative;
  3250. static volatile Int32FuncPtr invokeNative_Int32 =
  3251. (Int32FuncPtr)(uintptr_t)invokeNative;
  3252. static volatile Float64FuncPtr invokeNative_Float64 =
  3253. (Float64FuncPtr)(uintptr_t)invokeNative;
  3254. static volatile Float32FuncPtr invokeNative_Float32 =
  3255. (Float32FuncPtr)(uintptr_t)invokeNative;
  3256. static volatile VoidFuncPtr invokeNative_Void =
  3257. (VoidFuncPtr)(uintptr_t)invokeNative;
  3258. static inline void
  3259. word_copy(uint32 *dest, uint32 *src, unsigned num)
  3260. {
  3261. for (; num > 0; num--)
  3262. *dest++ = *src++;
  3263. }
  3264. bool
  3265. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3266. const WASMType *func_type, const char *signature,
  3267. void *attachment, uint32 *argv, uint32 argc,
  3268. uint32 *argv_ret)
  3269. {
  3270. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3271. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  3272. uint32 arg_i32, ptr_len;
  3273. uint32 result_count = func_type->result_count;
  3274. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3275. uint64 size;
  3276. bool ret = false;
  3277. #if WASM_ENABLE_REF_TYPES != 0
  3278. bool is_aot_func = (NULL == signature);
  3279. #endif
  3280. #if defined(BUILD_TARGET_X86_32)
  3281. argc1 = argc + ext_ret_count + 2;
  3282. #else
  3283. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  3284. so we need to allocate more memory. */
  3285. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  3286. #endif
  3287. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3288. size = sizeof(uint32) * (uint64)argc1;
  3289. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3290. 0))) {
  3291. return false;
  3292. }
  3293. }
  3294. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  3295. argv1[j++] = ((uint32 *)&exec_env)[i];
  3296. for (i = 0; i < func_type->param_count; i++) {
  3297. switch (func_type->types[i]) {
  3298. case VALUE_TYPE_I32:
  3299. #if WASM_ENABLE_REF_TYPES != 0
  3300. case VALUE_TYPE_FUNCREF:
  3301. #endif
  3302. {
  3303. arg_i32 = *argv++;
  3304. if (signature) {
  3305. if (signature[i + 1] == '*') {
  3306. /* param is a pointer */
  3307. if (signature[i + 2] == '~')
  3308. /* pointer with length followed */
  3309. ptr_len = *argv;
  3310. else
  3311. /* pointer without length followed */
  3312. ptr_len = 1;
  3313. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3314. ptr_len))
  3315. goto fail;
  3316. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3317. module, arg_i32);
  3318. }
  3319. else if (signature[i + 1] == '$') {
  3320. /* param is a string */
  3321. if (!wasm_runtime_validate_app_str_addr(module,
  3322. arg_i32))
  3323. goto fail;
  3324. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3325. module, arg_i32);
  3326. }
  3327. }
  3328. argv1[j++] = arg_i32;
  3329. break;
  3330. }
  3331. case VALUE_TYPE_I64:
  3332. case VALUE_TYPE_F64:
  3333. #if !defined(BUILD_TARGET_X86_32)
  3334. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  3335. and xtensa */
  3336. if (j & 1)
  3337. j++;
  3338. #endif
  3339. argv1[j++] = *argv++;
  3340. argv1[j++] = *argv++;
  3341. break;
  3342. case VALUE_TYPE_F32:
  3343. argv1[j++] = *argv++;
  3344. break;
  3345. #if WASM_ENABLE_REF_TYPES != 0
  3346. case VALUE_TYPE_EXTERNREF:
  3347. {
  3348. uint32 externref_idx = *argv++;
  3349. if (is_aot_func) {
  3350. argv1[j++] = externref_idx;
  3351. }
  3352. else {
  3353. void *externref_obj;
  3354. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3355. goto fail;
  3356. argv1[j++] = (uintptr_t)externref_obj;
  3357. }
  3358. break;
  3359. }
  3360. #endif
  3361. default:
  3362. bh_assert(0);
  3363. break;
  3364. }
  3365. }
  3366. /* Save extra result values' address to argv1 */
  3367. word_copy(argv1 + j, argv, ext_ret_count);
  3368. argc1 = j + ext_ret_count;
  3369. exec_env->attachment = attachment;
  3370. if (func_type->result_count == 0) {
  3371. invokeNative_Void(func_ptr, argv1, argc1);
  3372. }
  3373. else {
  3374. switch (func_type->types[func_type->param_count]) {
  3375. case VALUE_TYPE_I32:
  3376. #if WASM_ENABLE_REF_TYPES != 0
  3377. case VALUE_TYPE_FUNCREF:
  3378. #endif
  3379. argv_ret[0] =
  3380. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3381. break;
  3382. case VALUE_TYPE_I64:
  3383. PUT_I64_TO_ADDR(argv_ret,
  3384. invokeNative_Int64(func_ptr, argv1, argc1));
  3385. break;
  3386. case VALUE_TYPE_F32:
  3387. *(float32 *)argv_ret =
  3388. invokeNative_Float32(func_ptr, argv1, argc1);
  3389. break;
  3390. case VALUE_TYPE_F64:
  3391. PUT_F64_TO_ADDR(argv_ret,
  3392. invokeNative_Float64(func_ptr, argv1, argc1));
  3393. break;
  3394. #if WASM_ENABLE_REF_TYPES != 0
  3395. case VALUE_TYPE_EXTERNREF:
  3396. {
  3397. if (is_aot_func) {
  3398. uint32 externref_idx =
  3399. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3400. argv_ret[0] = externref_idx;
  3401. }
  3402. else {
  3403. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3404. func_ptr, argv1, argc1);
  3405. uint32 externref_idx;
  3406. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3407. externref_obj, &externref_idx))
  3408. goto fail;
  3409. argv_ret[0] = externref_idx;
  3410. }
  3411. break;
  3412. }
  3413. #endif
  3414. default:
  3415. bh_assert(0);
  3416. break;
  3417. }
  3418. }
  3419. exec_env->attachment = NULL;
  3420. ret = !wasm_runtime_get_exception(module) ? true : false;
  3421. fail:
  3422. if (argv1 != argv_buf)
  3423. wasm_runtime_free(argv1);
  3424. return ret;
  3425. }
  3426. #endif /* end of defined(BUILD_TARGET_X86_32) \
  3427. || defined(BUILD_TARGET_ARM) \
  3428. || defined(BUILD_TARGET_THUMB) \
  3429. || defined(BUILD_TARGET_MIPS) \
  3430. || defined(BUILD_TARGET_XTENSA) */
  3431. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  3432. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  3433. || defined(BUILD_TARGET_RISCV64_LP64)
  3434. #if WASM_ENABLE_SIMD != 0
  3435. #ifdef v128
  3436. #undef v128
  3437. #endif
  3438. #if defined(_WIN32) || defined(_WIN32_)
  3439. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  3440. __int8 m128i_i8[16];
  3441. __int16 m128i_i16[8];
  3442. __int32 m128i_i32[4];
  3443. __int64 m128i_i64[2];
  3444. unsigned __int8 m128i_u8[16];
  3445. unsigned __int16 m128i_u16[8];
  3446. unsigned __int32 m128i_u32[4];
  3447. unsigned __int64 m128i_u64[2];
  3448. } v128;
  3449. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  3450. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3451. || defined(BUILD_TARGET_RISCV64_LP64)
  3452. typedef long long v128
  3453. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  3454. #elif defined(BUILD_TARGET_AARCH64)
  3455. #include <arm_neon.h>
  3456. typedef uint32x4_t __m128i;
  3457. #define v128 __m128i
  3458. #endif
  3459. #endif /* end of WASM_ENABLE_SIMD != 0 */
  3460. typedef void (*GenericFunctionPointer)();
  3461. void
  3462. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3463. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3464. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3465. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3466. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3467. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3468. static volatile Float64FuncPtr invokeNative_Float64 =
  3469. (Float64FuncPtr)(uintptr_t)invokeNative;
  3470. static volatile Float32FuncPtr invokeNative_Float32 =
  3471. (Float32FuncPtr)(uintptr_t)invokeNative;
  3472. static volatile Int64FuncPtr invokeNative_Int64 =
  3473. (Int64FuncPtr)(uintptr_t)invokeNative;
  3474. static volatile Int32FuncPtr invokeNative_Int32 =
  3475. (Int32FuncPtr)(uintptr_t)invokeNative;
  3476. static volatile VoidFuncPtr invokeNative_Void =
  3477. (VoidFuncPtr)(uintptr_t)invokeNative;
  3478. #if WASM_ENABLE_SIMD != 0
  3479. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3480. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  3481. #endif
  3482. #if defined(_WIN32) || defined(_WIN32_)
  3483. #define MAX_REG_FLOATS 4
  3484. #define MAX_REG_INTS 4
  3485. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  3486. #define MAX_REG_FLOATS 8
  3487. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  3488. || defined(BUILD_TARGET_RISCV64_LP64)
  3489. #define MAX_REG_INTS 8
  3490. #else
  3491. #define MAX_REG_INTS 6
  3492. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  3493. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3494. || defined(BUILD_TARGET_RISCV64_LP64) */
  3495. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3496. bool
  3497. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3498. const WASMType *func_type, const char *signature,
  3499. void *attachment, uint32 *argv, uint32 argc,
  3500. uint32 *argv_ret)
  3501. {
  3502. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3503. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  3504. arg_i64;
  3505. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3506. uint32 arg_i32, ptr_len;
  3507. uint32 result_count = func_type->result_count;
  3508. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3509. bool ret = false;
  3510. #if WASM_ENABLE_REF_TYPES != 0
  3511. bool is_aot_func = (NULL == signature);
  3512. #endif
  3513. #ifndef BUILD_TARGET_RISCV64_LP64
  3514. #if WASM_ENABLE_SIMD == 0
  3515. uint64 *fps;
  3516. #else
  3517. v128 *fps;
  3518. #endif
  3519. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3520. #define fps ints
  3521. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3522. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  3523. /* important difference in calling conventions */
  3524. #define n_fps n_ints
  3525. #else
  3526. int n_fps = 0;
  3527. #endif
  3528. #if WASM_ENABLE_SIMD == 0
  3529. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  3530. #else
  3531. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  3532. + ext_ret_count;
  3533. #endif
  3534. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3535. size = sizeof(uint64) * (uint64)argc1;
  3536. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3537. 0))) {
  3538. return false;
  3539. }
  3540. }
  3541. #ifndef BUILD_TARGET_RISCV64_LP64
  3542. #if WASM_ENABLE_SIMD == 0
  3543. fps = argv1;
  3544. ints = fps + MAX_REG_FLOATS;
  3545. #else
  3546. fps = (v128 *)argv1;
  3547. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  3548. #endif
  3549. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3550. ints = argv1;
  3551. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3552. stacks = ints + MAX_REG_INTS;
  3553. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  3554. for (i = 0; i < func_type->param_count; i++) {
  3555. switch (func_type->types[i]) {
  3556. case VALUE_TYPE_I32:
  3557. #if WASM_ENABLE_REF_TYPES != 0
  3558. case VALUE_TYPE_FUNCREF:
  3559. #endif
  3560. {
  3561. arg_i32 = *argv_src++;
  3562. arg_i64 = arg_i32;
  3563. if (signature) {
  3564. if (signature[i + 1] == '*') {
  3565. /* param is a pointer */
  3566. if (signature[i + 2] == '~')
  3567. /* pointer with length followed */
  3568. ptr_len = *argv_src;
  3569. else
  3570. /* pointer without length followed */
  3571. ptr_len = 1;
  3572. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3573. ptr_len))
  3574. goto fail;
  3575. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3576. module, arg_i32);
  3577. }
  3578. else if (signature[i + 1] == '$') {
  3579. /* param is a string */
  3580. if (!wasm_runtime_validate_app_str_addr(module,
  3581. arg_i32))
  3582. goto fail;
  3583. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3584. module, arg_i32);
  3585. }
  3586. }
  3587. if (n_ints < MAX_REG_INTS)
  3588. ints[n_ints++] = arg_i64;
  3589. else
  3590. stacks[n_stacks++] = arg_i64;
  3591. break;
  3592. }
  3593. case VALUE_TYPE_I64:
  3594. if (n_ints < MAX_REG_INTS)
  3595. ints[n_ints++] = *(uint64 *)argv_src;
  3596. else
  3597. stacks[n_stacks++] = *(uint64 *)argv_src;
  3598. argv_src += 2;
  3599. break;
  3600. case VALUE_TYPE_F32:
  3601. if (n_fps < MAX_REG_FLOATS) {
  3602. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  3603. }
  3604. else {
  3605. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  3606. }
  3607. break;
  3608. case VALUE_TYPE_F64:
  3609. if (n_fps < MAX_REG_FLOATS) {
  3610. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  3611. }
  3612. else {
  3613. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  3614. }
  3615. argv_src += 2;
  3616. break;
  3617. #if WASM_ENABLE_REF_TYPES != 0
  3618. case VALUE_TYPE_EXTERNREF:
  3619. {
  3620. uint32 externref_idx = *argv_src++;
  3621. if (is_aot_func) {
  3622. if (n_ints < MAX_REG_INTS)
  3623. ints[n_ints++] = externref_idx;
  3624. else
  3625. stacks[n_stacks++] = externref_idx;
  3626. }
  3627. else {
  3628. void *externref_obj;
  3629. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3630. goto fail;
  3631. if (n_ints < MAX_REG_INTS)
  3632. ints[n_ints++] = (uintptr_t)externref_obj;
  3633. else
  3634. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3635. }
  3636. break;
  3637. }
  3638. #endif
  3639. #if WASM_ENABLE_SIMD != 0
  3640. case VALUE_TYPE_V128:
  3641. if (n_fps < MAX_REG_FLOATS) {
  3642. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  3643. }
  3644. else {
  3645. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  3646. n_stacks++;
  3647. }
  3648. argv_src += 4;
  3649. break;
  3650. #endif
  3651. default:
  3652. bh_assert(0);
  3653. break;
  3654. }
  3655. }
  3656. /* Save extra result values' address to argv1 */
  3657. for (i = 0; i < ext_ret_count; i++) {
  3658. if (n_ints < MAX_REG_INTS)
  3659. ints[n_ints++] = *(uint64 *)argv_src;
  3660. else
  3661. stacks[n_stacks++] = *(uint64 *)argv_src;
  3662. argv_src += 2;
  3663. }
  3664. exec_env->attachment = attachment;
  3665. if (result_count == 0) {
  3666. invokeNative_Void(func_ptr, argv1, n_stacks);
  3667. }
  3668. else {
  3669. /* Invoke the native function and get the first result value */
  3670. switch (func_type->types[func_type->param_count]) {
  3671. case VALUE_TYPE_I32:
  3672. #if WASM_ENABLE_REF_TYPES != 0
  3673. case VALUE_TYPE_FUNCREF:
  3674. #endif
  3675. argv_ret[0] =
  3676. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3677. break;
  3678. case VALUE_TYPE_I64:
  3679. PUT_I64_TO_ADDR(argv_ret,
  3680. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3681. break;
  3682. case VALUE_TYPE_F32:
  3683. *(float32 *)argv_ret =
  3684. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3685. break;
  3686. case VALUE_TYPE_F64:
  3687. PUT_F64_TO_ADDR(
  3688. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3689. break;
  3690. #if WASM_ENABLE_REF_TYPES != 0
  3691. case VALUE_TYPE_EXTERNREF:
  3692. {
  3693. if (is_aot_func) {
  3694. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  3695. }
  3696. else {
  3697. uint32 externref_idx;
  3698. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  3699. func_ptr, argv1, n_stacks);
  3700. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3701. externref_obj, &externref_idx))
  3702. goto fail;
  3703. argv_ret[0] = externref_idx;
  3704. }
  3705. break;
  3706. }
  3707. #endif
  3708. #if WASM_ENABLE_SIMD != 0
  3709. case VALUE_TYPE_V128:
  3710. *(v128 *)argv_ret =
  3711. invokeNative_V128(func_ptr, argv1, n_stacks);
  3712. break;
  3713. #endif
  3714. default:
  3715. bh_assert(0);
  3716. break;
  3717. }
  3718. }
  3719. exec_env->attachment = NULL;
  3720. ret = !wasm_runtime_get_exception(module) ? true : false;
  3721. fail:
  3722. if (argv1 != argv_buf)
  3723. wasm_runtime_free(argv1);
  3724. return ret;
  3725. }
  3726. #endif /* end of defined(BUILD_TARGET_X86_64) \
  3727. || defined(BUILD_TARGET_AMD_64) \
  3728. || defined(BUILD_TARGET_AARCH64) \
  3729. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3730. || defined(BUILD_TARGET_RISCV64_LP64) */
  3731. bool
  3732. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
  3733. uint32 argc, uint32 argv[])
  3734. {
  3735. if (!wasm_runtime_exec_env_check(exec_env)) {
  3736. LOG_ERROR("Invalid exec env stack info.");
  3737. return false;
  3738. }
  3739. /* this function is called from native code, so exec_env->handle and
  3740. exec_env->native_stack_boundary must have been set, we don't set
  3741. it again */
  3742. #if WASM_ENABLE_INTERP != 0
  3743. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  3744. return wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
  3745. #endif
  3746. #if WASM_ENABLE_AOT != 0
  3747. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  3748. return aot_call_indirect(exec_env, 0, element_indices, argc, argv);
  3749. #endif
  3750. return false;
  3751. }
  3752. static void
  3753. exchange_uint32(uint8 *p_data)
  3754. {
  3755. uint8 value = *p_data;
  3756. *p_data = *(p_data + 3);
  3757. *(p_data + 3) = value;
  3758. value = *(p_data + 1);
  3759. *(p_data + 1) = *(p_data + 2);
  3760. *(p_data + 2) = value;
  3761. }
  3762. static void
  3763. exchange_uint64(uint8 *p_data)
  3764. {
  3765. uint32 value;
  3766. value = *(uint32 *)p_data;
  3767. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  3768. *(uint32 *)(p_data + 4) = value;
  3769. exchange_uint32(p_data);
  3770. exchange_uint32(p_data + 4);
  3771. }
  3772. void
  3773. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  3774. {
  3775. uint64 u1, u2;
  3776. bh_memcpy_s(&u1, 8, bytes, 8);
  3777. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  3778. if (!is_little_endian()) {
  3779. exchange_uint64((uint8 *)&u1);
  3780. exchange_uint64((uint8 *)&u2);
  3781. *ret1 = u2;
  3782. *ret2 = u1;
  3783. }
  3784. else {
  3785. *ret1 = u1;
  3786. *ret2 = u2;
  3787. }
  3788. }
  3789. #if WASM_ENABLE_THREAD_MGR != 0
  3790. typedef struct WASMThreadArg {
  3791. WASMExecEnv *new_exec_env;
  3792. wasm_thread_callback_t callback;
  3793. void *arg;
  3794. } WASMThreadArg;
  3795. WASMExecEnv *
  3796. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  3797. {
  3798. return wasm_cluster_spawn_exec_env(exec_env);
  3799. }
  3800. void
  3801. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  3802. {
  3803. wasm_cluster_destroy_spawned_exec_env(exec_env);
  3804. }
  3805. static void *
  3806. wasm_runtime_thread_routine(void *arg)
  3807. {
  3808. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  3809. void *ret;
  3810. bh_assert(thread_arg->new_exec_env);
  3811. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  3812. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  3813. wasm_runtime_free(thread_arg);
  3814. os_thread_exit(ret);
  3815. return ret;
  3816. }
  3817. int32
  3818. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  3819. wasm_thread_callback_t callback, void *arg)
  3820. {
  3821. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  3822. WASMThreadArg *thread_arg;
  3823. int32 ret;
  3824. if (!new_exec_env)
  3825. return -1;
  3826. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  3827. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3828. return -1;
  3829. }
  3830. thread_arg->new_exec_env = new_exec_env;
  3831. thread_arg->callback = callback;
  3832. thread_arg->arg = arg;
  3833. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  3834. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  3835. if (ret != 0) {
  3836. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3837. wasm_runtime_free(thread_arg);
  3838. }
  3839. return ret;
  3840. }
  3841. int32
  3842. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  3843. {
  3844. return os_thread_join((korp_tid)tid, retval);
  3845. }
  3846. #endif /* end of WASM_ENABLE_THREAD_MGR */
  3847. #if WASM_ENABLE_REF_TYPES != 0
  3848. static korp_mutex externref_lock;
  3849. static uint32 externref_global_id = 1;
  3850. static HashMap *externref_map;
  3851. typedef struct ExternRefMapNode {
  3852. /* The extern object from runtime embedder */
  3853. void *extern_obj;
  3854. /* The module instance it belongs to */
  3855. WASMModuleInstanceCommon *module_inst;
  3856. /* Whether it is retained */
  3857. bool retained;
  3858. /* Whether it is marked by runtime */
  3859. bool marked;
  3860. } ExternRefMapNode;
  3861. static uint32
  3862. wasm_externref_hash(const void *key)
  3863. {
  3864. uint32 externref_idx = (uint32)(uintptr_t)key;
  3865. return externref_idx;
  3866. }
  3867. static bool
  3868. wasm_externref_equal(void *key1, void *key2)
  3869. {
  3870. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  3871. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  3872. return externref_idx1 == externref_idx2 ? true : false;
  3873. }
  3874. static bool
  3875. wasm_externref_map_init()
  3876. {
  3877. if (os_mutex_init(&externref_lock) != 0)
  3878. return false;
  3879. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  3880. wasm_externref_equal, NULL,
  3881. wasm_runtime_free))) {
  3882. os_mutex_destroy(&externref_lock);
  3883. return false;
  3884. }
  3885. externref_global_id = 1;
  3886. return true;
  3887. }
  3888. static void
  3889. wasm_externref_map_destroy()
  3890. {
  3891. bh_hash_map_destroy(externref_map);
  3892. os_mutex_destroy(&externref_lock);
  3893. }
  3894. typedef struct LookupExtObj_UserData {
  3895. ExternRefMapNode node;
  3896. bool found;
  3897. uint32 externref_idx;
  3898. } LookupExtObj_UserData;
  3899. static void
  3900. lookup_extobj_callback(void *key, void *value, void *user_data)
  3901. {
  3902. uint32 externref_idx = (uint32)(uintptr_t)key;
  3903. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3904. LookupExtObj_UserData *user_data_lookup =
  3905. (LookupExtObj_UserData *)user_data;
  3906. if (node->extern_obj == user_data_lookup->node.extern_obj
  3907. && node->module_inst == user_data_lookup->node.module_inst) {
  3908. user_data_lookup->found = true;
  3909. user_data_lookup->externref_idx = externref_idx;
  3910. }
  3911. }
  3912. bool
  3913. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  3914. uint32 *p_externref_idx)
  3915. {
  3916. LookupExtObj_UserData lookup_user_data = { 0 };
  3917. ExternRefMapNode *node;
  3918. uint32 externref_idx;
  3919. /*
  3920. * to catch a parameter from `wasm_application_execute_func`,
  3921. * which represents a string 'null'
  3922. */
  3923. #if UINTPTR_MAX == UINT32_MAX
  3924. if ((uint32)-1 == (uintptr_t)extern_obj) {
  3925. #else
  3926. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  3927. #endif
  3928. *p_externref_idx = NULL_REF;
  3929. return true;
  3930. }
  3931. /* in a wrapper, extern_obj could be any value */
  3932. lookup_user_data.node.extern_obj = extern_obj;
  3933. lookup_user_data.node.module_inst = module_inst;
  3934. lookup_user_data.found = false;
  3935. os_mutex_lock(&externref_lock);
  3936. /* Lookup hashmap firstly */
  3937. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  3938. (void *)&lookup_user_data);
  3939. if (lookup_user_data.found) {
  3940. *p_externref_idx = lookup_user_data.externref_idx;
  3941. os_mutex_unlock(&externref_lock);
  3942. return true;
  3943. }
  3944. /* Not found in hashmap */
  3945. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  3946. goto fail1;
  3947. }
  3948. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  3949. goto fail1;
  3950. }
  3951. memset(node, 0, sizeof(ExternRefMapNode));
  3952. node->extern_obj = extern_obj;
  3953. node->module_inst = module_inst;
  3954. externref_idx = externref_global_id;
  3955. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  3956. (void *)node)) {
  3957. goto fail2;
  3958. }
  3959. externref_global_id++;
  3960. *p_externref_idx = externref_idx;
  3961. os_mutex_unlock(&externref_lock);
  3962. return true;
  3963. fail2:
  3964. wasm_runtime_free(node);
  3965. fail1:
  3966. os_mutex_unlock(&externref_lock);
  3967. return false;
  3968. }
  3969. bool
  3970. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  3971. {
  3972. ExternRefMapNode *node;
  3973. /* catch a `ref.null` vairable */
  3974. if (externref_idx == NULL_REF) {
  3975. *p_extern_obj = NULL;
  3976. return true;
  3977. }
  3978. os_mutex_lock(&externref_lock);
  3979. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  3980. os_mutex_unlock(&externref_lock);
  3981. if (!node)
  3982. return false;
  3983. *p_extern_obj = node->extern_obj;
  3984. return true;
  3985. }
  3986. static void
  3987. reclaim_extobj_callback(void *key, void *value, void *user_data)
  3988. {
  3989. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3990. WASMModuleInstanceCommon *module_inst =
  3991. (WASMModuleInstanceCommon *)user_data;
  3992. if (node->module_inst == module_inst) {
  3993. if (!node->marked && !node->retained) {
  3994. bh_hash_map_remove(externref_map, key, NULL, NULL);
  3995. wasm_runtime_free(value);
  3996. }
  3997. else {
  3998. node->marked = false;
  3999. }
  4000. }
  4001. }
  4002. static void
  4003. mark_externref(uint32 externref_idx)
  4004. {
  4005. ExternRefMapNode *node;
  4006. if (externref_idx != NULL_REF) {
  4007. node =
  4008. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4009. if (node) {
  4010. node->marked = true;
  4011. }
  4012. }
  4013. }
  4014. #if WASM_ENABLE_INTERP != 0
  4015. static void
  4016. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  4017. {
  4018. uint32 i, j, externref_idx, *table_data;
  4019. uint8 *global_data = module_inst->global_data;
  4020. WASMGlobalInstance *global;
  4021. WASMTableInstance *table;
  4022. global = module_inst->globals;
  4023. for (i = 0; i < module_inst->global_count; i++, global++) {
  4024. if (global->type == VALUE_TYPE_EXTERNREF) {
  4025. externref_idx = *(uint32 *)(global_data + global->data_offset);
  4026. mark_externref(externref_idx);
  4027. }
  4028. }
  4029. for (i = 0; i < module_inst->table_count; i++) {
  4030. table = wasm_get_table_inst(module_inst, i);
  4031. if (table->elem_type == VALUE_TYPE_EXTERNREF) {
  4032. table_data = (uint32 *)table->base_addr;
  4033. for (j = 0; j < table->cur_size; j++) {
  4034. externref_idx = table_data[j];
  4035. mark_externref(externref_idx);
  4036. }
  4037. }
  4038. }
  4039. }
  4040. #endif
  4041. #if WASM_ENABLE_AOT != 0
  4042. static void
  4043. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  4044. {
  4045. uint32 i = 0, j = 0;
  4046. const AOTModule *module = (AOTModule *)(module_inst->aot_module.ptr);
  4047. const AOTTable *table = module->tables;
  4048. const AOTGlobal *global = module->globals;
  4049. const AOTTableInstance *table_inst =
  4050. (AOTTableInstance *)module_inst->tables.ptr;
  4051. for (i = 0; i < module->global_count; i++, global++) {
  4052. if (global->type == VALUE_TYPE_EXTERNREF) {
  4053. mark_externref(*(uint32 *)((uint8 *)module_inst->global_data.ptr
  4054. + global->data_offset));
  4055. }
  4056. }
  4057. for (i = 0; i < module->table_count;
  4058. i++, table_inst = aot_next_tbl_inst(table_inst)) {
  4059. if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
  4060. while (j < table_inst->cur_size) {
  4061. mark_externref(table_inst->data[j++]);
  4062. }
  4063. }
  4064. }
  4065. }
  4066. #endif
  4067. void
  4068. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  4069. {
  4070. os_mutex_lock(&externref_lock);
  4071. #if WASM_ENABLE_INTERP != 0
  4072. if (module_inst->module_type == Wasm_Module_Bytecode)
  4073. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  4074. #endif
  4075. #if WASM_ENABLE_AOT != 0
  4076. if (module_inst->module_type == Wasm_Module_AoT)
  4077. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  4078. #endif
  4079. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  4080. (void *)module_inst);
  4081. os_mutex_unlock(&externref_lock);
  4082. }
  4083. static void
  4084. cleanup_extobj_callback(void *key, void *value, void *user_data)
  4085. {
  4086. ExternRefMapNode *node = (ExternRefMapNode *)value;
  4087. WASMModuleInstanceCommon *module_inst =
  4088. (WASMModuleInstanceCommon *)user_data;
  4089. if (node->module_inst == module_inst) {
  4090. bh_hash_map_remove(externref_map, key, NULL, NULL);
  4091. wasm_runtime_free(value);
  4092. }
  4093. }
  4094. void
  4095. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  4096. {
  4097. os_mutex_lock(&externref_lock);
  4098. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  4099. (void *)module_inst);
  4100. os_mutex_unlock(&externref_lock);
  4101. }
  4102. bool
  4103. wasm_externref_retain(uint32 externref_idx)
  4104. {
  4105. ExternRefMapNode *node;
  4106. os_mutex_lock(&externref_lock);
  4107. if (externref_idx != NULL_REF) {
  4108. node =
  4109. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4110. if (node) {
  4111. node->retained = true;
  4112. os_mutex_unlock(&externref_lock);
  4113. return true;
  4114. }
  4115. }
  4116. os_mutex_unlock(&externref_lock);
  4117. return false;
  4118. }
  4119. #endif /* end of WASM_ENABLE_REF_TYPES */
  4120. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  4121. uint32
  4122. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  4123. char **buf, uint32 *len)
  4124. {
  4125. if (dump_or_print) {
  4126. return (uint32)os_printf("%s", line_buf);
  4127. }
  4128. else if (*buf) {
  4129. uint32 dump_len;
  4130. dump_len = snprintf(*buf, *len, "%s", line_buf);
  4131. if (dump_len >= *len) {
  4132. dump_len = *len;
  4133. }
  4134. *len = *len - dump_len;
  4135. *buf = *buf + dump_len;
  4136. return dump_len;
  4137. }
  4138. else {
  4139. return strlen(line_buf);
  4140. }
  4141. }
  4142. void
  4143. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  4144. {
  4145. WASMModuleInstanceCommon *module_inst =
  4146. wasm_exec_env_get_module_inst(exec_env);
  4147. #if WASM_ENABLE_INTERP != 0
  4148. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4149. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  4150. }
  4151. #endif
  4152. #if WASM_ENABLE_AOT != 0
  4153. if (module_inst->module_type == Wasm_Module_AoT) {
  4154. aot_dump_call_stack(exec_env, true, NULL, 0);
  4155. }
  4156. #endif
  4157. }
  4158. uint32
  4159. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env)
  4160. {
  4161. WASMModuleInstanceCommon *module_inst =
  4162. wasm_exec_env_get_module_inst(exec_env);
  4163. #if WASM_ENABLE_INTERP != 0
  4164. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4165. return wasm_interp_dump_call_stack(exec_env, false, NULL, 0);
  4166. }
  4167. #endif
  4168. #if WASM_ENABLE_AOT != 0
  4169. if (module_inst->module_type == Wasm_Module_AoT) {
  4170. return aot_dump_call_stack(exec_env, false, NULL, 0);
  4171. }
  4172. #endif
  4173. return 0;
  4174. }
  4175. uint32
  4176. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  4177. uint32 len)
  4178. {
  4179. WASMModuleInstanceCommon *module_inst =
  4180. wasm_exec_env_get_module_inst(exec_env);
  4181. #if WASM_ENABLE_INTERP != 0
  4182. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4183. return wasm_interp_dump_call_stack(exec_env, false, buf, len);
  4184. }
  4185. #endif
  4186. #if WASM_ENABLE_AOT != 0
  4187. if (module_inst->module_type == Wasm_Module_AoT) {
  4188. return aot_dump_call_stack(exec_env, false, buf, len);
  4189. }
  4190. #endif
  4191. return 0;
  4192. }
  4193. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  4194. bool
  4195. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  4196. const WASMExport *export, WASMType **out)
  4197. {
  4198. #if WASM_ENABLE_INTERP != 0
  4199. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4200. WASMModule *module = (WASMModule *)module_comm;
  4201. if (export->index < module->import_function_count) {
  4202. *out = module->import_functions[export->index].u.function.func_type;
  4203. }
  4204. else {
  4205. *out =
  4206. module->functions[export->index - module->import_function_count]
  4207. ->func_type;
  4208. }
  4209. return true;
  4210. }
  4211. #endif
  4212. #if WASM_ENABLE_AOT != 0
  4213. if (module_comm->module_type == Wasm_Module_AoT) {
  4214. AOTModule *module = (AOTModule *)module_comm;
  4215. if (export->index < module->import_func_count) {
  4216. *out = module->func_types[module->import_funcs[export->index]
  4217. .func_type_index];
  4218. }
  4219. else {
  4220. *out = module->func_types
  4221. [module->func_type_indexes[export->index
  4222. - module->import_func_count]];
  4223. }
  4224. return true;
  4225. }
  4226. #endif
  4227. return false;
  4228. }
  4229. bool
  4230. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  4231. const WASMExport *export,
  4232. uint8 *out_val_type, bool *out_mutability)
  4233. {
  4234. #if WASM_ENABLE_INTERP != 0
  4235. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4236. WASMModule *module = (WASMModule *)module_comm;
  4237. if (export->index < module->import_global_count) {
  4238. WASMGlobalImport *import_global =
  4239. &((module->import_globals + export->index)->u.global);
  4240. *out_val_type = import_global->type;
  4241. *out_mutability = import_global->is_mutable;
  4242. }
  4243. else {
  4244. WASMGlobal *global =
  4245. module->globals + (export->index - module->import_global_count);
  4246. *out_val_type = global->type;
  4247. *out_mutability = global->is_mutable;
  4248. }
  4249. return true;
  4250. }
  4251. #endif
  4252. #if WASM_ENABLE_AOT != 0
  4253. if (module_comm->module_type == Wasm_Module_AoT) {
  4254. AOTModule *module = (AOTModule *)module_comm;
  4255. if (export->index < module->import_global_count) {
  4256. AOTImportGlobal *import_global =
  4257. module->import_globals + export->index;
  4258. *out_val_type = import_global->type;
  4259. *out_mutability = import_global->is_mutable;
  4260. }
  4261. else {
  4262. AOTGlobal *global =
  4263. module->globals + (export->index - module->import_global_count);
  4264. *out_val_type = global->type;
  4265. *out_mutability = global->is_mutable;
  4266. }
  4267. return true;
  4268. }
  4269. #endif
  4270. return false;
  4271. }
  4272. bool
  4273. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  4274. const WASMExport *export,
  4275. uint32 *out_min_page, uint32 *out_max_page)
  4276. {
  4277. #if WASM_ENABLE_INTERP != 0
  4278. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4279. WASMModule *module = (WASMModule *)module_comm;
  4280. if (export->index < module->import_memory_count) {
  4281. WASMMemoryImport *import_memory =
  4282. &((module->import_memories + export->index)->u.memory);
  4283. *out_min_page = import_memory->init_page_count;
  4284. *out_max_page = import_memory->max_page_count;
  4285. }
  4286. else {
  4287. WASMMemory *memory =
  4288. module->memories
  4289. + (export->index - module->import_memory_count);
  4290. *out_min_page = memory->init_page_count;
  4291. *out_max_page = memory->max_page_count;
  4292. }
  4293. return true;
  4294. }
  4295. #endif
  4296. #if WASM_ENABLE_AOT != 0
  4297. if (module_comm->module_type == Wasm_Module_AoT) {
  4298. AOTModule *module = (AOTModule *)module_comm;
  4299. if (export->index < module->import_memory_count) {
  4300. AOTImportMemory *import_memory =
  4301. module->import_memories + export->index;
  4302. *out_min_page = import_memory->mem_init_page_count;
  4303. *out_max_page = import_memory->mem_max_page_count;
  4304. }
  4305. else {
  4306. AOTMemory *memory = module->memories
  4307. + (export->index - module->import_memory_count);
  4308. *out_min_page = memory->mem_init_page_count;
  4309. *out_max_page = memory->mem_max_page_count;
  4310. }
  4311. return true;
  4312. }
  4313. #endif
  4314. return false;
  4315. }
  4316. bool
  4317. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  4318. const WASMExport *export,
  4319. uint8 *out_elem_type, uint32 *out_min_size,
  4320. uint32 *out_max_size)
  4321. {
  4322. #if WASM_ENABLE_INTERP != 0
  4323. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4324. WASMModule *module = (WASMModule *)module_comm;
  4325. if (export->index < module->import_table_count) {
  4326. WASMTableImport *import_table =
  4327. &((module->import_tables + export->index)->u.table);
  4328. *out_elem_type = import_table->elem_type;
  4329. *out_min_size = import_table->init_size;
  4330. *out_max_size = import_table->max_size;
  4331. }
  4332. else {
  4333. WASMTable *table =
  4334. module->tables + (export->index - module->import_table_count);
  4335. *out_elem_type = table->elem_type;
  4336. *out_min_size = table->init_size;
  4337. *out_max_size = table->max_size;
  4338. }
  4339. return true;
  4340. }
  4341. #endif
  4342. #if WASM_ENABLE_AOT != 0
  4343. if (module_comm->module_type == Wasm_Module_AoT) {
  4344. AOTModule *module = (AOTModule *)module_comm;
  4345. if (export->index < module->import_table_count) {
  4346. AOTImportTable *import_table =
  4347. module->import_tables + export->index;
  4348. *out_elem_type = VALUE_TYPE_FUNCREF;
  4349. *out_min_size = import_table->table_init_size;
  4350. *out_max_size = import_table->table_max_size;
  4351. }
  4352. else {
  4353. AOTTable *table =
  4354. module->tables + (export->index - module->import_table_count);
  4355. *out_elem_type = table->elem_type;
  4356. *out_min_size = table->table_init_size;
  4357. *out_max_size = table->table_max_size;
  4358. }
  4359. return true;
  4360. }
  4361. #endif
  4362. return false;
  4363. }
  4364. static inline bool
  4365. argv_to_params(wasm_val_t *out_params, const uint32 *argv, WASMType *func_type)
  4366. {
  4367. wasm_val_t *param = out_params;
  4368. uint32 i = 0, *u32;
  4369. for (i = 0; i < func_type->param_count; i++, param++) {
  4370. switch (func_type->types[i]) {
  4371. case VALUE_TYPE_I32:
  4372. param->kind = WASM_I32;
  4373. param->of.i32 = *argv++;
  4374. break;
  4375. case VALUE_TYPE_I64:
  4376. param->kind = WASM_I64;
  4377. u32 = (uint32 *)&param->of.i64;
  4378. u32[0] = *argv++;
  4379. u32[1] = *argv++;
  4380. break;
  4381. case VALUE_TYPE_F32:
  4382. param->kind = WASM_F32;
  4383. param->of.f32 = *(float32 *)argv++;
  4384. break;
  4385. case VALUE_TYPE_F64:
  4386. param->kind = WASM_F64;
  4387. u32 = (uint32 *)&param->of.i64;
  4388. u32[0] = *argv++;
  4389. u32[1] = *argv++;
  4390. break;
  4391. #if WASM_ENABLE_REF_TYPES != 0
  4392. case VALUE_TYPE_EXTERNREF:
  4393. param->kind = WASM_ANYREF;
  4394. if (!wasm_externref_ref2obj(*argv,
  4395. (void **)&param->of.foreign)) {
  4396. return false;
  4397. }
  4398. argv++;
  4399. break;
  4400. #endif
  4401. default:
  4402. return false;
  4403. }
  4404. }
  4405. return true;
  4406. }
  4407. static inline bool
  4408. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  4409. const wasm_val_t *results, WASMType *func_type)
  4410. {
  4411. const wasm_val_t *result = results;
  4412. uint32 *argv = out_argv, *u32, i;
  4413. uint8 *result_types = func_type->types + func_type->param_count;
  4414. for (i = 0; i < func_type->result_count; i++, result++) {
  4415. switch (result_types[i]) {
  4416. case VALUE_TYPE_I32:
  4417. case VALUE_TYPE_F32:
  4418. *(int32 *)argv++ = result->of.i32;
  4419. break;
  4420. case VALUE_TYPE_I64:
  4421. case VALUE_TYPE_F64:
  4422. u32 = (uint32 *)&result->of.i64;
  4423. *argv++ = u32[0];
  4424. *argv++ = u32[1];
  4425. break;
  4426. #if WASM_ENABLE_REF_TYPES != 0
  4427. case VALUE_TYPE_EXTERNREF:
  4428. if (!wasm_externref_obj2ref(module_inst,
  4429. (void *)result->of.foreign, argv)) {
  4430. return false;
  4431. }
  4432. argv++;
  4433. break;
  4434. #endif
  4435. default:
  4436. return false;
  4437. }
  4438. }
  4439. return true;
  4440. }
  4441. bool
  4442. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  4443. void *func_ptr, WASMType *func_type,
  4444. uint32 argc, uint32 *argv, bool with_env,
  4445. void *wasm_c_api_env)
  4446. {
  4447. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  4448. wasm_val_t *params = params_buf, *results = results_buf;
  4449. wasm_trap_t *trap = NULL;
  4450. bool ret = false;
  4451. wasm_val_vec_t params_vec, results_vec;
  4452. if (func_type->param_count > 16) {
  4453. if (!(params =
  4454. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  4455. module_inst, NULL, 0))) {
  4456. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  4457. return false;
  4458. }
  4459. }
  4460. if (!argv_to_params(params, argv, func_type)) {
  4461. wasm_runtime_set_exception(module_inst, "unsupported param type");
  4462. goto fail;
  4463. }
  4464. if (func_type->result_count > 4) {
  4465. if (!(results =
  4466. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  4467. module_inst, NULL, 0))) {
  4468. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  4469. goto fail;
  4470. }
  4471. }
  4472. params_vec.data = params;
  4473. params_vec.num_elems = func_type->param_count;
  4474. params_vec.size = func_type->param_count;
  4475. params_vec.size_of_elem = sizeof(wasm_val_t);
  4476. results_vec.data = results;
  4477. results_vec.num_elems = 0;
  4478. results_vec.size = func_type->result_count;
  4479. results_vec.size_of_elem = sizeof(wasm_val_t);
  4480. if (!with_env) {
  4481. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  4482. trap = callback(&params_vec, &results_vec);
  4483. }
  4484. else {
  4485. wasm_func_callback_with_env_t callback =
  4486. (wasm_func_callback_with_env_t)func_ptr;
  4487. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  4488. }
  4489. if (trap) {
  4490. if (trap->message->data) {
  4491. /* since trap->message->data does not end with '\0' */
  4492. char trap_message[128] = { 0 };
  4493. bh_memcpy_s(trap_message, 127, trap->message->data,
  4494. (trap->message->size < 127 ? (uint32)trap->message->size
  4495. : 127));
  4496. wasm_runtime_set_exception(module_inst, trap_message);
  4497. }
  4498. else {
  4499. wasm_runtime_set_exception(
  4500. module_inst, "native function throw unknown exception");
  4501. }
  4502. wasm_trap_delete(trap);
  4503. goto fail;
  4504. }
  4505. if (!results_to_argv(module_inst, argv, results, func_type)) {
  4506. wasm_runtime_set_exception(module_inst, "unsupported result type");
  4507. goto fail;
  4508. }
  4509. results_vec.num_elems = func_type->result_count;
  4510. ret = true;
  4511. fail:
  4512. if (params != params_buf)
  4513. wasm_runtime_free(params);
  4514. if (results != results_buf)
  4515. wasm_runtime_free(results);
  4516. return ret;
  4517. }
  4518. void
  4519. wasm_runtime_show_app_heap_corrupted_prompt()
  4520. {
  4521. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  4522. "is compiled by wasi-sdk-12.0 or higher version, "
  4523. "please add -Wl,--export=malloc -Wl,--export=free "
  4524. "to export malloc and free functions. If it is "
  4525. "compiled by asc, please add --exportRuntime to "
  4526. "export the runtime helpers.");
  4527. }
  4528. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4529. void
  4530. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list)
  4531. {
  4532. WASMCustomSection *section = section_list, *next;
  4533. while (section) {
  4534. next = section->next;
  4535. wasm_runtime_free(section);
  4536. section = next;
  4537. }
  4538. }
  4539. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */
  4540. void
  4541. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch)
  4542. {
  4543. *major = WAMR_VERSION_MAJOR;
  4544. *minor = WAMR_VERSION_MINOR;
  4545. *patch = WAMR_VERSION_PATCH;
  4546. }