wasm_runtime_common.c 160 KB

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