wasm_runtime.c 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime.h"
  6. #include "wasm.h"
  7. #include "wasm_loader.h"
  8. #include "wasm_interp.h"
  9. #include "bh_common.h"
  10. #include "bh_log.h"
  11. #include "mem_alloc.h"
  12. #include "../common/wasm_runtime_common.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_SHARED_MEMORY != 0
  18. #include "../common/wasm_shared_memory.h"
  19. #endif
  20. #if WASM_ENABLE_THREAD_MGR != 0
  21. #include "../libraries/thread-mgr/thread_manager.h"
  22. #endif
  23. #if WASM_ENABLE_DEBUG_INTERP != 0
  24. #include "../libraries/debug-engine/debug_engine.h"
  25. #endif
  26. #if WASM_ENABLE_FAST_JIT != 0
  27. #include "../fast-jit/jit_compiler.h"
  28. #endif
  29. #if WASM_ENABLE_JIT != 0
  30. #include "../aot/aot_runtime.h"
  31. #endif
  32. static void
  33. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  34. {
  35. if (error_buf != NULL) {
  36. snprintf(error_buf, error_buf_size,
  37. "WASM module instantiate failed: %s", string);
  38. }
  39. }
  40. static void
  41. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  42. {
  43. va_list args;
  44. char buf[128];
  45. if (error_buf != NULL) {
  46. va_start(args, format);
  47. vsnprintf(buf, sizeof(buf), format, args);
  48. va_end(args);
  49. snprintf(error_buf, error_buf_size,
  50. "WASM module instantiate failed: %s", buf);
  51. }
  52. }
  53. WASMModule *
  54. wasm_load(uint8 *buf, uint32 size,
  55. #if WASM_ENABLE_MULTI_MODULE != 0
  56. bool main_module,
  57. #endif
  58. char *error_buf, uint32 error_buf_size)
  59. {
  60. return wasm_loader_load(buf, size,
  61. #if WASM_ENABLE_MULTI_MODULE != 0
  62. main_module,
  63. #endif
  64. error_buf, error_buf_size);
  65. }
  66. WASMModule *
  67. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  68. uint32 error_buf_size)
  69. {
  70. return wasm_loader_load_from_sections(section_list, error_buf,
  71. error_buf_size);
  72. }
  73. void
  74. wasm_unload(WASMModule *module)
  75. {
  76. wasm_loader_unload(module);
  77. }
  78. static void *
  79. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  80. {
  81. void *mem;
  82. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  83. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  84. return NULL;
  85. }
  86. memset(mem, 0, (uint32)size);
  87. return mem;
  88. }
  89. #if WASM_ENABLE_MULTI_MODULE != 0
  90. static WASMModuleInstance *
  91. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  92. const WASMModule *sub_module)
  93. {
  94. bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
  95. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  96. while (node && sub_module != node->module_inst->module) {
  97. node = bh_list_elem_next(node);
  98. }
  99. return node ? node->module_inst : NULL;
  100. }
  101. #endif
  102. /**
  103. * Destroy memory instances.
  104. */
  105. static void
  106. memories_deinstantiate(WASMModuleInstance *module_inst,
  107. WASMMemoryInstance **memories, uint32 count)
  108. {
  109. uint32 i;
  110. if (memories) {
  111. for (i = 0; i < count; i++) {
  112. if (memories[i]) {
  113. #if WASM_ENABLE_MULTI_MODULE != 0
  114. WASMModule *module = module_inst->module;
  115. if (i < module->import_memory_count
  116. && module->import_memories[i].u.memory.import_module) {
  117. continue;
  118. }
  119. #endif
  120. #if WASM_ENABLE_SHARED_MEMORY != 0
  121. if (shared_memory_is_shared(memories[i])) {
  122. uint32 ref_count = shared_memory_dec_reference(memories[i]);
  123. /* if the reference count is not zero,
  124. don't free the memory */
  125. if (ref_count > 0)
  126. continue;
  127. }
  128. #endif
  129. if (memories[i]->heap_handle) {
  130. mem_allocator_destroy(memories[i]->heap_handle);
  131. wasm_runtime_free(memories[i]->heap_handle);
  132. memories[i]->heap_handle = NULL;
  133. }
  134. if (memories[i]->memory_data) {
  135. #ifndef OS_ENABLE_HW_BOUND_CHECK
  136. wasm_runtime_free(memories[i]->memory_data);
  137. #else
  138. #ifdef BH_PLATFORM_WINDOWS
  139. os_mem_decommit(memories[i]->memory_data,
  140. memories[i]->num_bytes_per_page
  141. * memories[i]->cur_page_count);
  142. #endif
  143. os_munmap((uint8 *)memories[i]->memory_data,
  144. 8 * (uint64)BH_GB);
  145. #endif
  146. }
  147. }
  148. }
  149. wasm_runtime_free(memories);
  150. }
  151. (void)module_inst;
  152. }
  153. static WASMMemoryInstance *
  154. memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
  155. WASMMemoryInstance *memory, uint32 memory_idx,
  156. uint32 num_bytes_per_page, uint32 init_page_count,
  157. uint32 max_page_count, uint32 heap_size, uint32 flags,
  158. char *error_buf, uint32 error_buf_size)
  159. {
  160. WASMModule *module = module_inst->module;
  161. uint64 memory_data_size, max_memory_data_size;
  162. uint32 heap_offset = num_bytes_per_page * init_page_count;
  163. uint32 inc_page_count, aux_heap_base, global_idx;
  164. uint32 bytes_of_last_page, bytes_to_page_end;
  165. uint8 *global_addr;
  166. #ifdef OS_ENABLE_HW_BOUND_CHECK
  167. uint8 *mapped_mem;
  168. uint64 map_size = 8 * (uint64)BH_GB;
  169. uint64 page_size = os_getpagesize();
  170. #endif
  171. #if WASM_ENABLE_SHARED_MEMORY != 0
  172. bool is_shared_memory = flags & 0x02 ? true : false;
  173. /* shared memory */
  174. if (is_shared_memory && parent != NULL) {
  175. bh_assert(parent->memory_count > memory_idx);
  176. memory = parent->memories[memory_idx];
  177. shared_memory_inc_reference(memory);
  178. return memory;
  179. }
  180. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  181. if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
  182. && module_inst->module->free_function != (uint32)-1) {
  183. /* Disable app heap, use malloc/free function exported
  184. by wasm app to allocate/free memory instead */
  185. heap_size = 0;
  186. }
  187. if (init_page_count == max_page_count && init_page_count == 1) {
  188. /* If only one page and at most one page, we just append
  189. the app heap to the end of linear memory, enlarge the
  190. num_bytes_per_page, and don't change the page count */
  191. heap_offset = num_bytes_per_page;
  192. num_bytes_per_page += heap_size;
  193. if (num_bytes_per_page < heap_size) {
  194. set_error_buf(error_buf, error_buf_size,
  195. "failed to insert app heap into linear memory, "
  196. "try using `--heap-size=0` option");
  197. return NULL;
  198. }
  199. }
  200. else if (heap_size > 0) {
  201. if (init_page_count == max_page_count && init_page_count == 0) {
  202. /* If the memory data size is always 0, we resize it to
  203. one page for app heap */
  204. num_bytes_per_page = heap_size;
  205. heap_offset = 0;
  206. inc_page_count = 1;
  207. }
  208. else if (module->aux_heap_base_global_index != (uint32)-1
  209. && module->aux_heap_base
  210. < num_bytes_per_page * init_page_count) {
  211. /* Insert app heap before __heap_base */
  212. aux_heap_base = module->aux_heap_base;
  213. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  214. if (bytes_of_last_page == 0)
  215. bytes_of_last_page = num_bytes_per_page;
  216. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  217. inc_page_count =
  218. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  219. / num_bytes_per_page;
  220. heap_offset = aux_heap_base;
  221. aux_heap_base += heap_size;
  222. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  223. if (bytes_of_last_page == 0)
  224. bytes_of_last_page = num_bytes_per_page;
  225. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  226. if (bytes_to_page_end < 1 * BH_KB) {
  227. aux_heap_base += 1 * BH_KB;
  228. inc_page_count++;
  229. }
  230. /* Adjust __heap_base global value */
  231. global_idx = module->aux_heap_base_global_index;
  232. bh_assert(module_inst->e->globals
  233. && global_idx < module_inst->e->global_count);
  234. global_addr = module_inst->global_data
  235. + module_inst->e->globals[global_idx].data_offset;
  236. *(uint32 *)global_addr = aux_heap_base;
  237. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  238. }
  239. else {
  240. /* Insert app heap before new page */
  241. inc_page_count =
  242. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  243. heap_offset = num_bytes_per_page * init_page_count;
  244. heap_size = num_bytes_per_page * inc_page_count;
  245. if (heap_size > 0)
  246. heap_size -= 1 * BH_KB;
  247. }
  248. init_page_count += inc_page_count;
  249. max_page_count += inc_page_count;
  250. if (init_page_count > DEFAULT_MAX_PAGES) {
  251. set_error_buf(error_buf, error_buf_size,
  252. "failed to insert app heap into linear memory, "
  253. "try using `--heap-size=0` option");
  254. return NULL;
  255. }
  256. else if (init_page_count == DEFAULT_MAX_PAGES) {
  257. num_bytes_per_page = UINT32_MAX;
  258. init_page_count = max_page_count = 1;
  259. }
  260. if (max_page_count > DEFAULT_MAX_PAGES)
  261. max_page_count = DEFAULT_MAX_PAGES;
  262. }
  263. else { /* heap_size == 0 */
  264. if (init_page_count == DEFAULT_MAX_PAGES) {
  265. num_bytes_per_page = UINT32_MAX;
  266. init_page_count = max_page_count = 1;
  267. }
  268. }
  269. LOG_VERBOSE("Memory instantiate:");
  270. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  271. num_bytes_per_page, init_page_count, max_page_count);
  272. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  273. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  274. max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  275. bh_assert(memory_data_size <= UINT32_MAX);
  276. bh_assert(max_memory_data_size <= 4 * (uint64)BH_GB);
  277. (void)max_memory_data_size;
  278. bh_assert(memory != NULL);
  279. #ifndef OS_ENABLE_HW_BOUND_CHECK
  280. #if WASM_ENABLE_SHARED_MEMORY != 0
  281. if (is_shared_memory) {
  282. /* Allocate maximum memory size when memory is shared */
  283. if (max_memory_data_size > 0
  284. && !(memory->memory_data = runtime_malloc(
  285. max_memory_data_size, error_buf, error_buf_size))) {
  286. goto fail1;
  287. }
  288. }
  289. else
  290. #endif
  291. {
  292. /* Allocate initial memory size when memory is not shared */
  293. if (memory_data_size > 0
  294. && !(memory->memory_data = runtime_malloc(
  295. memory_data_size, error_buf, error_buf_size))) {
  296. goto fail1;
  297. }
  298. }
  299. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  300. memory_data_size = (memory_data_size + page_size - 1) & ~(page_size - 1);
  301. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  302. * ea = i + memarg.offset
  303. * both i and memarg.offset are u32 in range 0 to 4G
  304. * so the range of ea is 0 to 8G
  305. */
  306. if (!(memory->memory_data = mapped_mem =
  307. os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  308. os_get_invalid_handle()))) {
  309. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  310. goto fail1;
  311. }
  312. #ifdef BH_PLATFORM_WINDOWS
  313. if (memory_data_size > 0
  314. && !os_mem_commit(mapped_mem, memory_data_size,
  315. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  316. set_error_buf(error_buf, error_buf_size, "commit memory failed");
  317. os_munmap(mapped_mem, map_size);
  318. goto fail1;
  319. }
  320. #endif
  321. if (os_mprotect(mapped_mem, memory_data_size,
  322. MMAP_PROT_READ | MMAP_PROT_WRITE)
  323. != 0) {
  324. set_error_buf(error_buf, error_buf_size, "mprotect memory failed");
  325. goto fail2;
  326. }
  327. /* Newly allocated pages are filled with zero by the OS, we don't fill it
  328. * again here */
  329. if (memory_data_size > UINT32_MAX)
  330. memory_data_size = UINT32_MAX;
  331. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  332. memory->module_type = Wasm_Module_Bytecode;
  333. memory->num_bytes_per_page = num_bytes_per_page;
  334. memory->cur_page_count = init_page_count;
  335. memory->max_page_count = max_page_count;
  336. memory->memory_data_size = (uint32)memory_data_size;
  337. memory->heap_data = memory->memory_data + heap_offset;
  338. memory->heap_data_end = memory->heap_data + heap_size;
  339. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  340. /* Initialize heap */
  341. if (heap_size > 0) {
  342. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  343. if (!(memory->heap_handle = runtime_malloc(
  344. (uint64)heap_struct_size, error_buf, error_buf_size))) {
  345. goto fail2;
  346. }
  347. if (!mem_allocator_create_with_struct_and_pool(
  348. memory->heap_handle, heap_struct_size, memory->heap_data,
  349. heap_size)) {
  350. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  351. goto fail3;
  352. }
  353. }
  354. if (memory_data_size > 0) {
  355. wasm_runtime_set_mem_bound_check_bytes(memory, memory_data_size);
  356. }
  357. #if WASM_ENABLE_SHARED_MEMORY != 0
  358. if (is_shared_memory) {
  359. memory->is_shared_memory = 1;
  360. memory->ref_count = 1;
  361. }
  362. #endif
  363. LOG_VERBOSE("Memory instantiate success.");
  364. return memory;
  365. fail3:
  366. if (heap_size > 0)
  367. wasm_runtime_free(memory->heap_handle);
  368. fail2:
  369. #ifndef OS_ENABLE_HW_BOUND_CHECK
  370. if (memory->memory_data)
  371. wasm_runtime_free(memory->memory_data);
  372. #else
  373. #ifdef BH_PLATFORM_WINDOWS
  374. os_mem_decommit(mapped_mem, memory_data_size);
  375. #endif
  376. os_munmap(mapped_mem, map_size);
  377. #endif
  378. fail1:
  379. return NULL;
  380. }
  381. /**
  382. * Instantiate memories in a module.
  383. */
  384. static WASMMemoryInstance **
  385. memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  386. WASMModuleInstance *parent, uint32 heap_size,
  387. char *error_buf, uint32 error_buf_size)
  388. {
  389. WASMImport *import;
  390. uint32 mem_index = 0, i,
  391. memory_count = module->import_memory_count + module->memory_count;
  392. uint64 total_size;
  393. WASMMemoryInstance **memories, *memory;
  394. total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
  395. if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
  396. return NULL;
  397. }
  398. memory = module_inst->global_table_data.memory_instances;
  399. /* instantiate memories from import section */
  400. import = module->import_memories;
  401. for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
  402. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  403. uint32 init_page_count = import->u.memory.init_page_count;
  404. uint32 max_page_count = import->u.memory.max_page_count;
  405. uint32 flags = import->u.memory.flags;
  406. uint32 actual_heap_size = heap_size;
  407. #if WASM_ENABLE_MULTI_MODULE != 0
  408. if (import->u.memory.import_module != NULL) {
  409. WASMModuleInstance *module_inst_linked;
  410. if (!(module_inst_linked = get_sub_module_inst(
  411. module_inst, import->u.memory.import_module))) {
  412. set_error_buf(error_buf, error_buf_size, "unknown memory");
  413. memories_deinstantiate(module_inst, memories, memory_count);
  414. return NULL;
  415. }
  416. if (!(memories[mem_index++] = wasm_lookup_memory(
  417. module_inst_linked, import->u.memory.field_name))) {
  418. set_error_buf(error_buf, error_buf_size, "unknown memory");
  419. memories_deinstantiate(module_inst, memories, memory_count);
  420. return NULL;
  421. }
  422. }
  423. else
  424. #endif
  425. {
  426. if (!(memories[mem_index] = memory_instantiate(
  427. module_inst, parent, memory, mem_index,
  428. num_bytes_per_page, init_page_count, max_page_count,
  429. actual_heap_size, flags, error_buf, error_buf_size))) {
  430. memories_deinstantiate(module_inst, memories, memory_count);
  431. return NULL;
  432. }
  433. mem_index++;
  434. }
  435. }
  436. /* instantiate memories from memory section */
  437. for (i = 0; i < module->memory_count; i++, memory++) {
  438. if (!(memories[mem_index] = memory_instantiate(
  439. module_inst, parent, memory, mem_index,
  440. module->memories[i].num_bytes_per_page,
  441. module->memories[i].init_page_count,
  442. module->memories[i].max_page_count, heap_size,
  443. module->memories[i].flags, error_buf, error_buf_size))) {
  444. memories_deinstantiate(module_inst, memories, memory_count);
  445. return NULL;
  446. }
  447. mem_index++;
  448. }
  449. bh_assert(mem_index == memory_count);
  450. (void)module_inst;
  451. return memories;
  452. }
  453. /**
  454. * Destroy table instances.
  455. */
  456. static void
  457. tables_deinstantiate(WASMModuleInstance *module_inst)
  458. {
  459. if (module_inst->tables) {
  460. wasm_runtime_free(module_inst->tables);
  461. }
  462. #if WASM_ENABLE_MULTI_MODULE != 0
  463. if (module_inst->e->table_insts_linked) {
  464. wasm_runtime_free(module_inst->e->table_insts_linked);
  465. }
  466. #endif
  467. }
  468. /**
  469. * Instantiate tables in a module.
  470. */
  471. static WASMTableInstance **
  472. tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  473. WASMTableInstance *first_table, char *error_buf,
  474. uint32 error_buf_size)
  475. {
  476. WASMImport *import;
  477. uint32 table_index = 0, i;
  478. uint32 table_count = module->import_table_count + module->table_count;
  479. uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
  480. WASMTableInstance **tables, *table = first_table;
  481. #if WASM_ENABLE_MULTI_MODULE != 0
  482. uint64 total_size_of_tables_linked =
  483. (uint64)sizeof(WASMTableInstance *) * module->import_table_count;
  484. WASMTableInstance **table_linked = NULL;
  485. #endif
  486. if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
  487. return NULL;
  488. }
  489. #if WASM_ENABLE_MULTI_MODULE != 0
  490. if (module->import_table_count > 0
  491. && !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
  492. total_size_of_tables_linked, error_buf, error_buf_size))) {
  493. goto fail;
  494. }
  495. #endif
  496. /* instantiate tables from import section */
  497. import = module->import_tables;
  498. for (i = 0; i < module->import_table_count; i++, import++) {
  499. uint32 max_size_fixed = 0;
  500. #if WASM_ENABLE_MULTI_MODULE != 0
  501. WASMTableInstance *table_inst_linked = NULL;
  502. WASMModuleInstance *module_inst_linked = NULL;
  503. if (import->u.table.import_module) {
  504. if (!(module_inst_linked = get_sub_module_inst(
  505. module_inst, import->u.table.import_module))) {
  506. set_error_buf(error_buf, error_buf_size, "unknown table");
  507. goto fail;
  508. }
  509. if (!(table_inst_linked = wasm_lookup_table(
  510. module_inst_linked, import->u.table.field_name))) {
  511. set_error_buf(error_buf, error_buf_size, "unknown table");
  512. goto fail;
  513. }
  514. total_size = offsetof(WASMTableInstance, elems);
  515. }
  516. else
  517. #endif
  518. {
  519. /* in order to save memory, alloc resource as few as possible */
  520. max_size_fixed = import->u.table.possible_grow
  521. ? import->u.table.max_size
  522. : import->u.table.init_size;
  523. /* it is a built-in table, every module has its own */
  524. total_size = offsetof(WASMTableInstance, elems);
  525. /* store function indexes for non-gc, object pointers for gc */
  526. total_size += (uint64)sizeof(table_elem_type_t) * max_size_fixed;
  527. }
  528. tables[table_index++] = table;
  529. #if WASM_ENABLE_GC == 0
  530. /* Set all elements to -1 to mark them as uninitialized elements */
  531. memset(table, -1, (uint32)total_size);
  532. #else
  533. /* For GC, all elements have already been set to NULL_REF (0) as
  534. uninitialized elements */
  535. #endif
  536. #if WASM_ENABLE_MULTI_MODULE != 0
  537. *table_linked = table_inst_linked;
  538. if (table_inst_linked != NULL) {
  539. #if WASM_ENABLE_GC != 0
  540. table->elem_type = table_inst_linked->elem_type;
  541. table->elem_ref_type = table_inst_linked->elem_ref_type;
  542. #endif
  543. table->cur_size = table_inst_linked->cur_size;
  544. table->max_size = table_inst_linked->max_size;
  545. }
  546. else
  547. #endif
  548. {
  549. #if WASM_ENABLE_GC != 0
  550. table->elem_type = import->u.table.elem_type;
  551. table->elem_ref_type.elem_ref_type = import->u.table.elem_ref_type;
  552. #endif
  553. table->cur_size = import->u.table.init_size;
  554. table->max_size = max_size_fixed;
  555. }
  556. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  557. #if WASM_ENABLE_MULTI_MODULE != 0
  558. table_linked++;
  559. #endif
  560. }
  561. /* instantiate tables from table section */
  562. for (i = 0; i < module->table_count; i++) {
  563. uint32 max_size_fixed = 0;
  564. total_size = offsetof(WASMTableInstance, elems);
  565. #if WASM_ENABLE_MULTI_MODULE != 0
  566. /* in case, a module which imports this table will grow it */
  567. max_size_fixed = module->tables[i].max_size;
  568. #else
  569. max_size_fixed = module->tables[i].possible_grow
  570. ? module->tables[i].max_size
  571. : module->tables[i].init_size;
  572. #endif
  573. #if WASM_ENABLE_GC == 0
  574. /* Store function indexes */
  575. total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
  576. #else
  577. /* Store object pointers */
  578. total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
  579. #endif
  580. tables[table_index++] = table;
  581. #if WASM_ENABLE_GC == 0
  582. /* Set all elements to -1 to mark them as uninitialized elements */
  583. memset(table, -1, (uint32)total_size);
  584. #else
  585. /* For GC, all elements have already been set to NULL_REF (0) as
  586. uninitialized elements */
  587. #endif
  588. #if WASM_ENABLE_GC != 0
  589. table->elem_type = module->tables[i].elem_type;
  590. table->elem_ref_type.elem_ref_type = module->tables[i].elem_ref_type;
  591. #endif
  592. table->cur_size = module->tables[i].init_size;
  593. table->max_size = max_size_fixed;
  594. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  595. }
  596. bh_assert(table_index == table_count);
  597. (void)module_inst;
  598. return tables;
  599. #if WASM_ENABLE_MULTI_MODULE != 0
  600. fail:
  601. wasm_runtime_free(tables);
  602. return NULL;
  603. #endif
  604. }
  605. /**
  606. * Destroy function instances.
  607. */
  608. static void
  609. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  610. {
  611. if (functions) {
  612. wasm_runtime_free(functions);
  613. }
  614. }
  615. /**
  616. * Instantiate functions in a module.
  617. */
  618. static WASMFunctionInstance *
  619. functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  620. char *error_buf, uint32 error_buf_size)
  621. {
  622. WASMImport *import;
  623. uint32 i,
  624. function_count = module->import_function_count + module->function_count;
  625. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  626. WASMFunctionInstance *functions, *function;
  627. if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
  628. return NULL;
  629. }
  630. total_size = sizeof(void *) * (uint64)module->import_function_count;
  631. if (total_size > 0
  632. && !(module_inst->import_func_ptrs =
  633. runtime_malloc(total_size, error_buf, error_buf_size))) {
  634. wasm_runtime_free(functions);
  635. return NULL;
  636. }
  637. /* instantiate functions from import section */
  638. function = functions;
  639. import = module->import_functions;
  640. for (i = 0; i < module->import_function_count; i++, import++) {
  641. function->is_import_func = true;
  642. #if WASM_ENABLE_MULTI_MODULE != 0
  643. if (import->u.function.import_module) {
  644. function->import_module_inst = get_sub_module_inst(
  645. module_inst, import->u.function.import_module);
  646. if (function->import_module_inst) {
  647. function->import_func_inst =
  648. wasm_lookup_function(function->import_module_inst,
  649. import->u.function.field_name, NULL);
  650. }
  651. }
  652. #endif /* WASM_ENABLE_MULTI_MODULE */
  653. function->u.func_import = &import->u.function;
  654. function->param_cell_num = import->u.function.func_type->param_cell_num;
  655. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  656. function->param_count =
  657. (uint16)function->u.func_import->func_type->param_count;
  658. function->param_types = function->u.func_import->func_type->types;
  659. function->local_cell_num = 0;
  660. function->local_count = 0;
  661. function->local_types = NULL;
  662. /* Copy the function pointer to current instance */
  663. module_inst->import_func_ptrs[i] =
  664. function->u.func_import->func_ptr_linked;
  665. function++;
  666. }
  667. /* instantiate functions from function section */
  668. for (i = 0; i < module->function_count; i++) {
  669. function->is_import_func = false;
  670. function->u.func = module->functions[i];
  671. function->param_cell_num = function->u.func->param_cell_num;
  672. function->ret_cell_num = function->u.func->ret_cell_num;
  673. function->local_cell_num = function->u.func->local_cell_num;
  674. function->param_count =
  675. (uint16)function->u.func->func_type->param_count;
  676. function->local_count = (uint16)function->u.func->local_count;
  677. function->param_types = function->u.func->func_type->types;
  678. function->local_types = function->u.func->local_types;
  679. function->local_offsets = function->u.func->local_offsets;
  680. #if WASM_ENABLE_FAST_INTERP != 0
  681. function->const_cell_num = function->u.func->const_cell_num;
  682. #endif
  683. function++;
  684. }
  685. bh_assert((uint32)(function - functions) == function_count);
  686. #if WASM_ENABLE_FAST_JIT != 0
  687. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  688. #endif
  689. return functions;
  690. }
  691. /**
  692. * Destroy global instances.
  693. */
  694. static void
  695. globals_deinstantiate(WASMGlobalInstance *globals)
  696. {
  697. if (globals)
  698. wasm_runtime_free(globals);
  699. }
  700. static bool
  701. check_global_init_expr(const WASMModule *module, uint32 global_index,
  702. char *error_buf, uint32 error_buf_size)
  703. {
  704. if (global_index >= module->import_global_count + module->global_count) {
  705. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  706. global_index);
  707. return false;
  708. }
  709. #if WASM_ENABLE_GC == 0
  710. /**
  711. * Currently, constant expressions occurring as initializers of
  712. * globals are further constrained in that contained global.get
  713. * instructions are only allowed to refer to imported globals.
  714. *
  715. * And initializer expression cannot reference a mutable global.
  716. */
  717. if (global_index >= module->import_global_count
  718. || (module->import_globals + global_index)->u.global.is_mutable) {
  719. set_error_buf(error_buf, error_buf_size,
  720. "constant expression required");
  721. return false;
  722. }
  723. #endif
  724. return true;
  725. }
  726. /**
  727. * Instantiate globals in a module.
  728. */
  729. static WASMGlobalInstance *
  730. globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  731. char *error_buf, uint32 error_buf_size)
  732. {
  733. WASMImport *import;
  734. uint32 global_data_offset = 0;
  735. uint32 i, global_count = module->import_global_count + module->global_count;
  736. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  737. WASMGlobalInstance *globals, *global;
  738. if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
  739. return NULL;
  740. }
  741. /* instantiate globals from import section */
  742. global = globals;
  743. import = module->import_globals;
  744. for (i = 0; i < module->import_global_count; i++, import++) {
  745. WASMGlobalImport *global_import = &import->u.global;
  746. global->type = global_import->type;
  747. global->is_mutable = global_import->is_mutable;
  748. #if WASM_ENABLE_GC != 0
  749. global->ref_type = global_import->ref_type;
  750. #endif
  751. #if WASM_ENABLE_MULTI_MODULE != 0
  752. if (global_import->import_module) {
  753. if (!(global->import_module_inst = get_sub_module_inst(
  754. module_inst, global_import->import_module))) {
  755. set_error_buf(error_buf, error_buf_size, "unknown global");
  756. goto fail;
  757. }
  758. if (!(global->import_global_inst = wasm_lookup_global(
  759. global->import_module_inst, global_import->field_name))) {
  760. set_error_buf(error_buf, error_buf_size, "unknown global");
  761. goto fail;
  762. }
  763. /* The linked global instance has been initialized, we
  764. just need to copy the value. */
  765. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  766. &(global_import->import_global_linked->init_expr),
  767. sizeof(WASMValue));
  768. }
  769. else
  770. #endif
  771. {
  772. /* native globals share their initial_values in one module */
  773. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  774. &(global_import->global_data_linked),
  775. sizeof(WASMValue));
  776. }
  777. #if WASM_ENABLE_FAST_JIT != 0
  778. bh_assert(global_data_offset == global_import->data_offset);
  779. #endif
  780. global->data_offset = global_data_offset;
  781. global_data_offset += wasm_value_type_size(global->type);
  782. global++;
  783. }
  784. /* instantiate globals from global section */
  785. for (i = 0; i < module->global_count; i++) {
  786. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  787. uint8 flag = init_expr->init_expr_type;
  788. global->type = module->globals[i].type;
  789. global->is_mutable = module->globals[i].is_mutable;
  790. #if WASM_ENABLE_FAST_JIT != 0
  791. bh_assert(global_data_offset == module->globals[i].data_offset);
  792. #endif
  793. global->data_offset = global_data_offset;
  794. global_data_offset += wasm_value_type_size(global->type);
  795. #if WASM_ENABLE_GC != 0
  796. global->ref_type = module->globals[i].ref_type;
  797. #endif
  798. switch (flag) {
  799. case INIT_EXPR_TYPE_GET_GLOBAL:
  800. {
  801. if (!check_global_init_expr(module, init_expr->u.global_index,
  802. error_buf, error_buf_size)) {
  803. goto fail;
  804. }
  805. bh_memcpy_s(
  806. &(global->initial_value), sizeof(WASMValue),
  807. &(globals[init_expr->u.global_index].initial_value),
  808. sizeof(globals[init_expr->u.global_index].initial_value));
  809. break;
  810. }
  811. #if WASM_ENABLE_GC != 0
  812. case INIT_EXPR_TYPE_STRUCT_NEW:
  813. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  814. {
  815. WASMRttType *rtt_type;
  816. WASMStructObjectRef struct_obj;
  817. WASMStructType *struct_type;
  818. WASMStructNewInitValues *init_values = NULL;
  819. uint32 type_idx;
  820. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  821. init_values = (WASMStructNewInitValues *)init_expr->u.data;
  822. type_idx = init_values->type_idx;
  823. }
  824. else {
  825. type_idx = init_expr->u.type_index;
  826. }
  827. struct_type = (WASMStructType *)module->types[type_idx];
  828. if (!(rtt_type = wasm_rtt_type_new(
  829. (WASMType *)struct_type, type_idx, module->rtt_types,
  830. module->type_count, &module->rtt_type_lock))) {
  831. set_error_buf(error_buf, error_buf_size,
  832. "create rtt object failed");
  833. goto fail;
  834. }
  835. if (!(struct_obj = wasm_struct_obj_new_internal(
  836. module_inst->e->common.gc_heap_handle, rtt_type))) {
  837. set_error_buf(error_buf, error_buf_size,
  838. "create struct object failed");
  839. goto fail;
  840. }
  841. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  842. uint32 field_idx;
  843. bh_assert(init_values->count == struct_type->field_count);
  844. for (field_idx = 0; field_idx < init_values->count;
  845. field_idx++) {
  846. wasm_struct_obj_set_field(
  847. struct_obj, field_idx,
  848. &init_values->fields[field_idx]);
  849. }
  850. }
  851. global->initial_value.gc_obj = (void *)struct_obj;
  852. break;
  853. }
  854. case INIT_EXPR_TYPE_ARRAY_NEW:
  855. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  856. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  857. {
  858. WASMRttType *rtt_type;
  859. WASMArrayObjectRef array_obj;
  860. WASMArrayType *array_type;
  861. WASMArrayNewInitValues *init_values = NULL;
  862. WASMValue *arr_init_val = NULL, empty_val = { 0 };
  863. uint32 type_idx, len;
  864. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  865. type_idx = init_expr->u.array_new_default.type_index;
  866. len = init_expr->u.array_new_default.length;
  867. arr_init_val = &empty_val;
  868. }
  869. else {
  870. init_values = (WASMArrayNewInitValues *)init_expr->u.data;
  871. type_idx = init_values->type_idx;
  872. len = init_values->length;
  873. if (flag == INIT_EXPR_TYPE_ARRAY_NEW) {
  874. arr_init_val = init_values->elem_data;
  875. }
  876. }
  877. array_type = (WASMArrayType *)module->types[type_idx];
  878. if (!(rtt_type = wasm_rtt_type_new(
  879. (WASMType *)array_type, type_idx, module->rtt_types,
  880. module->type_count, &module->rtt_type_lock))) {
  881. set_error_buf(error_buf, error_buf_size,
  882. "create rtt object failed");
  883. goto fail;
  884. }
  885. if (!(array_obj = wasm_array_obj_new_internal(
  886. module_inst->e->common.gc_heap_handle, rtt_type, len,
  887. arr_init_val))) {
  888. set_error_buf(error_buf, error_buf_size,
  889. "create array object failed");
  890. goto fail;
  891. }
  892. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  893. uint32 elem_idx;
  894. bh_assert(init_values);
  895. for (elem_idx = 0; elem_idx < len; elem_idx++) {
  896. wasm_array_obj_set_elem(
  897. array_obj, elem_idx,
  898. &init_values->elem_data[elem_idx]);
  899. }
  900. }
  901. global->initial_value.gc_obj = (void *)array_obj;
  902. break;
  903. }
  904. case INIT_EXPR_TYPE_I31_NEW:
  905. {
  906. global->initial_value.gc_obj =
  907. (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
  908. break;
  909. }
  910. #endif /* end of WASM_ENABLE_GC != 0 */
  911. default:
  912. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  913. &(init_expr->u), sizeof(init_expr->u));
  914. break;
  915. }
  916. global++;
  917. }
  918. bh_assert((uint32)(global - globals) == global_count);
  919. bh_assert(global_data_offset == module->global_data_size);
  920. (void)module_inst;
  921. return globals;
  922. fail:
  923. wasm_runtime_free(globals);
  924. return NULL;
  925. }
  926. /**
  927. * Return export function count in module export section.
  928. */
  929. static uint32
  930. get_export_count(const WASMModule *module, uint8 kind)
  931. {
  932. WASMExport *export = module->exports;
  933. uint32 count = 0, i;
  934. for (i = 0; i < module->export_count; i++, export ++)
  935. if (export->kind == kind)
  936. count++;
  937. return count;
  938. }
  939. /**
  940. * Destroy export function instances.
  941. */
  942. static void
  943. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  944. {
  945. if (functions)
  946. wasm_runtime_free(functions);
  947. }
  948. /**
  949. * Instantiate export functions in a module.
  950. */
  951. static WASMExportFuncInstance *
  952. export_functions_instantiate(const WASMModule *module,
  953. WASMModuleInstance *module_inst,
  954. uint32 export_func_count, char *error_buf,
  955. uint32 error_buf_size)
  956. {
  957. WASMExportFuncInstance *export_funcs, *export_func;
  958. WASMExport *export = module->exports;
  959. uint32 i;
  960. uint64 total_size =
  961. sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  962. if (!(export_func = export_funcs =
  963. runtime_malloc(total_size, error_buf, error_buf_size))) {
  964. return NULL;
  965. }
  966. for (i = 0; i < module->export_count; i++, export ++)
  967. if (export->kind == EXPORT_KIND_FUNC) {
  968. export_func->name = export->name;
  969. export_func->function = &module_inst->e->functions[export->index];
  970. export_func++;
  971. }
  972. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  973. return export_funcs;
  974. }
  975. #if WASM_ENABLE_MULTI_MODULE != 0
  976. static void
  977. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  978. {
  979. if (globals)
  980. wasm_runtime_free(globals);
  981. }
  982. static WASMExportGlobInstance *
  983. export_globals_instantiate(const WASMModule *module,
  984. WASMModuleInstance *module_inst,
  985. uint32 export_glob_count, char *error_buf,
  986. uint32 error_buf_size)
  987. {
  988. WASMExportGlobInstance *export_globals, *export_global;
  989. WASMExport *export = module->exports;
  990. uint32 i;
  991. uint64 total_size =
  992. sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  993. if (!(export_global = export_globals =
  994. runtime_malloc(total_size, error_buf, error_buf_size))) {
  995. return NULL;
  996. }
  997. for (i = 0; i < module->export_count; i++, export ++)
  998. if (export->kind == EXPORT_KIND_GLOBAL) {
  999. export_global->name = export->name;
  1000. export_global->global = &module_inst->e->globals[export->index];
  1001. export_global++;
  1002. }
  1003. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  1004. return export_globals;
  1005. }
  1006. #endif
  1007. static WASMFunctionInstance *
  1008. lookup_post_instantiate_func(WASMModuleInstance *module_inst,
  1009. const char *func_name)
  1010. {
  1011. WASMFunctionInstance *func;
  1012. WASMFuncType *func_type;
  1013. if (!(func = wasm_lookup_function(module_inst, func_name, NULL)))
  1014. /* Not found */
  1015. return NULL;
  1016. func_type = func->u.func->func_type;
  1017. if (!(func_type->param_count == 0 && func_type->result_count == 0))
  1018. /* Not a valid function type, ignore it */
  1019. return NULL;
  1020. return func;
  1021. }
  1022. static bool
  1023. execute_post_instantiate_functions(WASMModuleInstance *module_inst,
  1024. bool is_sub_inst, WASMExecEnv *exec_env_main)
  1025. {
  1026. WASMFunctionInstance *start_func = module_inst->e->start_function;
  1027. WASMFunctionInstance *initialize_func = NULL;
  1028. WASMFunctionInstance *post_inst_func = NULL;
  1029. WASMFunctionInstance *call_ctors_func = NULL;
  1030. #if WASM_ENABLE_LIBC_WASI != 0
  1031. WASMModule *module = module_inst->module;
  1032. #endif
  1033. WASMModuleInstanceCommon *module_inst_main = NULL;
  1034. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1035. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1036. #endif
  1037. WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
  1038. bool ret = false;
  1039. #if WASM_ENABLE_LIBC_WASI != 0
  1040. /*
  1041. * WASI reactor instances may assume that _initialize will be called by
  1042. * the environment at most once, and that none of their other exports
  1043. * are accessed before that call.
  1044. */
  1045. if (!is_sub_inst && module->import_wasi_api) {
  1046. initialize_func =
  1047. lookup_post_instantiate_func(module_inst, "_initialize");
  1048. }
  1049. #endif
  1050. /* Execute possible "__post_instantiate" function if wasm app is
  1051. compiled by emsdk's early version */
  1052. if (!is_sub_inst) {
  1053. post_inst_func =
  1054. lookup_post_instantiate_func(module_inst, "__post_instantiate");
  1055. }
  1056. #if WASM_ENABLE_BULK_MEMORY != 0
  1057. /* Only execute the memory init function for main instance since
  1058. the data segments will be dropped once initialized */
  1059. if (!is_sub_inst
  1060. #if WASM_ENABLE_LIBC_WASI != 0
  1061. && !module->import_wasi_api
  1062. #endif
  1063. ) {
  1064. call_ctors_func =
  1065. lookup_post_instantiate_func(module_inst, "__wasm_call_ctors");
  1066. }
  1067. #endif
  1068. if (!start_func && !initialize_func && !post_inst_func
  1069. && !call_ctors_func) {
  1070. /* No post instantiation functions to call */
  1071. return true;
  1072. }
  1073. if (is_sub_inst) {
  1074. bh_assert(exec_env_main);
  1075. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1076. bh_assert(exec_env_tls == exec_env_main);
  1077. (void)exec_env_tls;
  1078. #endif
  1079. exec_env = exec_env_main;
  1080. /* Temporarily replace parent exec_env's module inst to current
  1081. module inst to avoid checking failure when calling the
  1082. wasm functions, and ensure that the exec_env's module inst
  1083. is the correct one. */
  1084. module_inst_main = exec_env_main->module_inst;
  1085. wasm_exec_env_set_module_inst(exec_env,
  1086. (WASMModuleInstanceCommon *)module_inst);
  1087. }
  1088. else {
  1089. /* Try using the existing exec_env */
  1090. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1091. exec_env = exec_env_tls;
  1092. #endif
  1093. #if WASM_ENABLE_THREAD_MGR != 0
  1094. if (!exec_env)
  1095. exec_env = wasm_clusters_search_exec_env(
  1096. (WASMModuleInstanceCommon *)module_inst);
  1097. #endif
  1098. if (!exec_env) {
  1099. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1100. (WASMModuleInstanceCommon *)module_inst,
  1101. module_inst->default_wasm_stack_size))) {
  1102. wasm_set_exception(module_inst, "allocate memory failed");
  1103. return false;
  1104. }
  1105. }
  1106. else {
  1107. /* Temporarily replace exec_env's module inst with current
  1108. module inst to ensure that the exec_env's module inst
  1109. is the correct one. */
  1110. module_inst_main = exec_env->module_inst;
  1111. wasm_exec_env_set_module_inst(
  1112. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1113. }
  1114. }
  1115. /* Execute start function for both main insance and sub instance */
  1116. if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
  1117. goto fail;
  1118. }
  1119. #if WASM_ENABLE_LIBC_WASI != 0
  1120. if (initialize_func
  1121. && !wasm_call_function(exec_env, initialize_func, 0, NULL)) {
  1122. goto fail;
  1123. }
  1124. #else
  1125. (void)initialize_func;
  1126. #endif
  1127. if (post_inst_func
  1128. && !wasm_call_function(exec_env, post_inst_func, 0, NULL)) {
  1129. goto fail;
  1130. }
  1131. if (call_ctors_func
  1132. && !wasm_call_function(exec_env, call_ctors_func, 0, NULL)) {
  1133. goto fail;
  1134. }
  1135. ret = true;
  1136. fail:
  1137. if (is_sub_inst) {
  1138. /* Restore the parent exec_env's module inst */
  1139. wasm_exec_env_restore_module_inst(exec_env_main, module_inst_main);
  1140. }
  1141. else {
  1142. if (module_inst_main)
  1143. /* Restore the existing exec_env's module inst */
  1144. wasm_exec_env_restore_module_inst(exec_env, module_inst_main);
  1145. if (exec_env_created)
  1146. wasm_exec_env_destroy(exec_env_created);
  1147. }
  1148. return ret;
  1149. }
  1150. static bool
  1151. execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1152. WASMFunctionInstance *malloc_func,
  1153. WASMFunctionInstance *retain_func, uint32 size,
  1154. uint32 *p_result)
  1155. {
  1156. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1157. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1158. #endif
  1159. WASMExecEnv *exec_env_created = NULL;
  1160. WASMModuleInstanceCommon *module_inst_old = NULL;
  1161. uint32 argv[2], argc;
  1162. bool ret;
  1163. argv[0] = size;
  1164. argc = 1;
  1165. /* if __retain is exported, then this module is compiled by
  1166. assemblyscript, the memory should be managed by as's runtime,
  1167. in this case we need to call the retain function after malloc
  1168. the memory */
  1169. if (retain_func) {
  1170. /* the malloc functino from assemblyscript is:
  1171. function __new(size: usize, id: u32)
  1172. id = 0 means this is an ArrayBuffer object */
  1173. argv[1] = 0;
  1174. argc = 2;
  1175. }
  1176. if (exec_env) {
  1177. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1178. if (exec_env_tls) {
  1179. bh_assert(exec_env_tls == exec_env);
  1180. }
  1181. #endif
  1182. bh_assert(exec_env->module_inst
  1183. == (WASMModuleInstanceCommon *)module_inst);
  1184. }
  1185. else {
  1186. /* Try using the existing exec_env */
  1187. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1188. exec_env = exec_env_tls;
  1189. #endif
  1190. #if WASM_ENABLE_THREAD_MGR != 0
  1191. if (!exec_env)
  1192. exec_env = wasm_clusters_search_exec_env(
  1193. (WASMModuleInstanceCommon *)module_inst);
  1194. #endif
  1195. if (!exec_env) {
  1196. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1197. (WASMModuleInstanceCommon *)module_inst,
  1198. module_inst->default_wasm_stack_size))) {
  1199. wasm_set_exception(module_inst, "allocate memory failed");
  1200. return false;
  1201. }
  1202. }
  1203. else {
  1204. /* Temporarily replace exec_env's module inst with current
  1205. module inst to ensure that the exec_env's module inst
  1206. is the correct one. */
  1207. module_inst_old = exec_env->module_inst;
  1208. wasm_exec_env_set_module_inst(
  1209. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1210. }
  1211. }
  1212. ret = wasm_call_function(exec_env, malloc_func, argc, argv);
  1213. if (retain_func && ret)
  1214. ret = wasm_call_function(exec_env, retain_func, 1, argv);
  1215. if (module_inst_old)
  1216. /* Restore the existing exec_env's module inst */
  1217. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  1218. if (exec_env_created)
  1219. wasm_exec_env_destroy(exec_env_created);
  1220. if (ret)
  1221. *p_result = argv[0];
  1222. return ret;
  1223. }
  1224. static bool
  1225. execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1226. WASMFunctionInstance *free_func, uint32 offset)
  1227. {
  1228. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1229. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1230. #endif
  1231. WASMExecEnv *exec_env_created = NULL;
  1232. WASMModuleInstanceCommon *module_inst_old = NULL;
  1233. uint32 argv[2];
  1234. bool ret;
  1235. argv[0] = offset;
  1236. if (exec_env) {
  1237. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1238. if (exec_env_tls) {
  1239. bh_assert(exec_env_tls == exec_env);
  1240. }
  1241. #endif
  1242. bh_assert(exec_env->module_inst
  1243. == (WASMModuleInstanceCommon *)module_inst);
  1244. }
  1245. else {
  1246. /* Try using the existing exec_env */
  1247. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1248. exec_env = exec_env_tls;
  1249. #endif
  1250. #if WASM_ENABLE_THREAD_MGR != 0
  1251. if (!exec_env)
  1252. exec_env = wasm_clusters_search_exec_env(
  1253. (WASMModuleInstanceCommon *)module_inst);
  1254. #endif
  1255. if (!exec_env) {
  1256. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1257. (WASMModuleInstanceCommon *)module_inst,
  1258. module_inst->default_wasm_stack_size))) {
  1259. wasm_set_exception(module_inst, "allocate memory failed");
  1260. return false;
  1261. }
  1262. }
  1263. else {
  1264. /* Temporarily replace exec_env's module inst with current
  1265. module inst to ensure that the exec_env's module inst
  1266. is the correct one. */
  1267. module_inst_old = exec_env->module_inst;
  1268. wasm_exec_env_set_module_inst(
  1269. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1270. }
  1271. }
  1272. ret = wasm_call_function(exec_env, free_func, 1, argv);
  1273. if (module_inst_old)
  1274. /* Restore the existing exec_env's module inst */
  1275. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  1276. if (exec_env_created)
  1277. wasm_exec_env_destroy(exec_env_created);
  1278. return ret;
  1279. }
  1280. static bool
  1281. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  1282. uint32 error_buf_size)
  1283. {
  1284. WASMModule *module = module_inst->module;
  1285. uint32 i;
  1286. for (i = 0; i < module->import_function_count; i++) {
  1287. WASMFunctionImport *func =
  1288. &((module->import_functions + i)->u.function);
  1289. if (!func->func_ptr_linked
  1290. #if WASM_ENABLE_MULTI_MODULE != 0
  1291. && !func->import_func_linked
  1292. #endif
  1293. ) {
  1294. #if WASM_ENABLE_WAMR_COMPILER == 0
  1295. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1296. func->module_name, func->field_name);
  1297. /* will throw exception only if calling */
  1298. #else
  1299. /* do nothing to avoid confused message */
  1300. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1301. }
  1302. }
  1303. for (i = 0; i < module->import_global_count; i++) {
  1304. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  1305. if (!global->is_linked) {
  1306. #if WASM_ENABLE_SPEC_TEST != 0
  1307. set_error_buf(error_buf, error_buf_size,
  1308. "unknown import or incompatible import type");
  1309. return false;
  1310. #else
  1311. #if WASM_ENABLE_WAMR_COMPILER == 0
  1312. set_error_buf_v(error_buf, error_buf_size,
  1313. "failed to link import global (%s, %s)",
  1314. global->module_name, global->field_name);
  1315. return false;
  1316. #else
  1317. /* do nothing to avoid confused message */
  1318. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1319. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1320. }
  1321. }
  1322. return true;
  1323. }
  1324. #if WASM_ENABLE_JIT != 0
  1325. static bool
  1326. init_func_ptrs(WASMModuleInstance *module_inst, WASMModule *module,
  1327. char *error_buf, uint32 error_buf_size)
  1328. {
  1329. uint32 i;
  1330. void **func_ptrs;
  1331. uint64 total_size = (uint64)sizeof(void *) * module_inst->e->function_count;
  1332. /* Allocate memory */
  1333. if (!(func_ptrs = module_inst->func_ptrs =
  1334. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1335. return false;
  1336. }
  1337. /* Set import function pointers */
  1338. for (i = 0; i < module->import_function_count; i++, func_ptrs++) {
  1339. WASMFunctionImport *import_func =
  1340. &module->import_functions[i].u.function;
  1341. /* TODO: handle multi module */
  1342. *func_ptrs = import_func->func_ptr_linked;
  1343. }
  1344. /* The defined function pointers will be set in
  1345. wasm_runtime_set_running_mode, no need to set them here */
  1346. return true;
  1347. }
  1348. #endif /* end of WASM_ENABLE_JIT != 0 */
  1349. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1350. static uint32
  1351. get_smallest_type_idx(WASMModule *module, WASMFuncType *func_type)
  1352. {
  1353. uint32 i;
  1354. for (i = 0; i < module->type_count; i++) {
  1355. if (func_type == (WASMFuncType *)module->types[i])
  1356. return i;
  1357. }
  1358. bh_assert(0);
  1359. return -1;
  1360. }
  1361. static bool
  1362. init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
  1363. uint32 error_buf_size)
  1364. {
  1365. uint32 i;
  1366. uint64 total_size = (uint64)sizeof(uint32) * module_inst->e->function_count;
  1367. /* Allocate memory */
  1368. if (!(module_inst->func_type_indexes =
  1369. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1370. return false;
  1371. }
  1372. for (i = 0; i < module_inst->e->function_count; i++) {
  1373. WASMFunctionInstance *func_inst = module_inst->e->functions + i;
  1374. WASMFuncType *func_type = func_inst->is_import_func
  1375. ? func_inst->u.func_import->func_type
  1376. : func_inst->u.func->func_type;
  1377. module_inst->func_type_indexes[i] =
  1378. get_smallest_type_idx(module_inst->module, func_type);
  1379. }
  1380. return true;
  1381. }
  1382. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1383. #if WASM_ENABLE_GC != 0
  1384. void *
  1385. wasm_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
  1386. bool throw_exce, char *error_buf, uint32 error_buf_size)
  1387. {
  1388. WASMModule *module = module_inst->module;
  1389. WASMRttTypeRef rtt_type;
  1390. WASMFuncObjectRef func_obj;
  1391. WASMFunctionInstance *func_inst;
  1392. WASMFuncType *func_type;
  1393. uint32 type_idx;
  1394. if (throw_exce) {
  1395. error_buf = module_inst->cur_exception;
  1396. error_buf_size = sizeof(module_inst->cur_exception);
  1397. }
  1398. if (func_idx >= module_inst->e->function_count) {
  1399. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  1400. func_idx);
  1401. return NULL;
  1402. }
  1403. func_inst = &module_inst->e->functions[func_idx];
  1404. func_type = func_inst->is_import_func ? func_inst->u.func_import->func_type
  1405. : func_inst->u.func->func_type;
  1406. type_idx = func_inst->is_import_func ? func_inst->u.func_import->type_idx
  1407. : func_inst->u.func->type_idx;
  1408. if (!(rtt_type = wasm_rtt_type_new((WASMType *)func_type, type_idx,
  1409. module->rtt_types, module->type_count,
  1410. &module->rtt_type_lock))) {
  1411. set_error_buf(error_buf, error_buf_size, "create rtt object failed");
  1412. return NULL;
  1413. }
  1414. if (!(func_obj = wasm_func_obj_new_internal(
  1415. module_inst->e->common.gc_heap_handle, rtt_type, func_idx))) {
  1416. set_error_buf(error_buf, error_buf_size, "create func object failed");
  1417. return NULL;
  1418. }
  1419. return func_obj;
  1420. }
  1421. static bool
  1422. wasm_global_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
  1423. {
  1424. WASMGlobalInstance *global = module_inst->e->globals;
  1425. WASMGlobalInstance *global_end = global + module_inst->e->global_count;
  1426. uint8 *global_data = module_inst->global_data;
  1427. WASMObjectRef gc_obj;
  1428. while (global < global_end) {
  1429. if (wasm_is_type_reftype(global->type)) {
  1430. gc_obj = GET_REF_FROM_ADDR(
  1431. (uint32 *)(global_data + global->data_offset));
  1432. if (wasm_obj_is_created_from_heap(gc_obj)) {
  1433. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  1434. return false;
  1435. }
  1436. }
  1437. global++;
  1438. }
  1439. return true;
  1440. }
  1441. static bool
  1442. wasm_table_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
  1443. {
  1444. WASMTableInstance **tables = module_inst->tables, *table;
  1445. uint32 table_count = module_inst->table_count, i, j;
  1446. WASMObjectRef gc_obj, *table_elems;
  1447. for (i = 0; i < table_count; i++) {
  1448. table = tables[i];
  1449. table_elems = (WASMObjectRef *)table->elems;
  1450. for (j = 0; j < table->cur_size; j++) {
  1451. gc_obj = table_elems[j];
  1452. if (wasm_obj_is_created_from_heap(gc_obj)) {
  1453. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  1454. return false;
  1455. }
  1456. }
  1457. }
  1458. return true;
  1459. }
  1460. static bool
  1461. local_object_refs_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  1462. {
  1463. WASMLocalObjectRef *r;
  1464. WASMObjectRef gc_obj;
  1465. for (r = exec_env->cur_local_object_ref; r; r = r->prev) {
  1466. gc_obj = r->val;
  1467. if (wasm_obj_is_created_from_heap(gc_obj)) {
  1468. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  1469. return false;
  1470. }
  1471. }
  1472. return true;
  1473. }
  1474. bool
  1475. wasm_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  1476. {
  1477. WASMModuleInstance *module_inst =
  1478. (WASMModuleInstance *)exec_env->module_inst;
  1479. bool ret;
  1480. ret = wasm_global_traverse_gc_rootset(module_inst, heap);
  1481. if (!ret)
  1482. return ret;
  1483. ret = wasm_table_traverse_gc_rootset(module_inst, heap);
  1484. if (!ret)
  1485. return ret;
  1486. ret = local_object_refs_traverse_gc_rootset(exec_env, heap);
  1487. if (!ret)
  1488. return ret;
  1489. return wasm_interp_traverse_gc_rootset(exec_env, heap);
  1490. }
  1491. #endif /* end of WASM_ENABLE_GC != 0 */
  1492. static bool
  1493. set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode,
  1494. bool first_time_set)
  1495. {
  1496. WASMModule *module = module_inst->module;
  1497. if (running_mode == Mode_Default) {
  1498. #if WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT == 0
  1499. running_mode = Mode_Interp;
  1500. #elif WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT == 0
  1501. running_mode = Mode_Fast_JIT;
  1502. #elif WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT != 0
  1503. running_mode = Mode_LLVM_JIT;
  1504. #else /* WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 */
  1505. #if WASM_ENABLE_LAZY_JIT == 0
  1506. running_mode = Mode_LLVM_JIT;
  1507. #else
  1508. running_mode = Mode_Multi_Tier_JIT;
  1509. #endif
  1510. #endif
  1511. }
  1512. if (!wasm_runtime_is_running_mode_supported(running_mode))
  1513. return false;
  1514. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1515. && WASM_ENABLE_LAZY_JIT != 0) /* No possible multi-tier JIT */
  1516. module_inst->e->running_mode = running_mode;
  1517. if (running_mode == Mode_Interp) {
  1518. /* Do nothing for Mode_Interp */
  1519. }
  1520. else if (running_mode == Mode_Fast_JIT) {
  1521. /* Do nothing for Mode_Fast_JIT since
  1522. module_inst->fast_jit_func_ptrs is same as
  1523. module->fast_jit_func_ptrs */
  1524. }
  1525. #if WASM_ENABLE_JIT != 0
  1526. else if (running_mode == Mode_LLVM_JIT) {
  1527. /* Set defined function pointers */
  1528. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1529. sizeof(void *) * module->function_count, module->func_ptrs,
  1530. sizeof(void *) * module->function_count);
  1531. }
  1532. #endif
  1533. else {
  1534. bh_assert(0);
  1535. }
  1536. #else /* Possible multi-tier JIT */
  1537. os_mutex_lock(&module->instance_list_lock);
  1538. module_inst->e->running_mode = running_mode;
  1539. if (running_mode == Mode_Interp) {
  1540. /* Do nothing for Mode_Interp */
  1541. }
  1542. #if WASM_ENABLE_FAST_JIT != 0
  1543. else if (running_mode == Mode_Fast_JIT) {
  1544. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  1545. uint32 i;
  1546. /* Allocate memory for fast_jit_func_ptrs if needed */
  1547. if (!module_inst->fast_jit_func_ptrs
  1548. || module_inst->fast_jit_func_ptrs == module->fast_jit_func_ptrs) {
  1549. uint64 total_size = (uint64)sizeof(void *) * module->function_count;
  1550. if (!(module_inst->fast_jit_func_ptrs =
  1551. runtime_malloc(total_size, NULL, 0))) {
  1552. os_mutex_unlock(&module->instance_list_lock);
  1553. return false;
  1554. }
  1555. }
  1556. for (i = 0; i < module->function_count; i++) {
  1557. if (module->functions[i]->fast_jit_jitted_code) {
  1558. /* current fast jit function has been compiled */
  1559. module_inst->fast_jit_func_ptrs[i] =
  1560. module->functions[i]->fast_jit_jitted_code;
  1561. }
  1562. else {
  1563. module_inst->fast_jit_func_ptrs[i] =
  1564. jit_globals->compile_fast_jit_and_then_call;
  1565. }
  1566. }
  1567. }
  1568. #endif
  1569. #if WASM_ENABLE_JIT != 0
  1570. else if (running_mode == Mode_LLVM_JIT) {
  1571. void **llvm_jit_func_ptrs;
  1572. uint32 i;
  1573. /* Notify backend threads to start llvm jit compilation */
  1574. module->enable_llvm_jit_compilation = true;
  1575. /* Wait until llvm jit finishes initialization */
  1576. os_mutex_lock(&module->tierup_wait_lock);
  1577. while (!module->llvm_jit_inited) {
  1578. os_cond_reltimedwait(&module->tierup_wait_cond,
  1579. &module->tierup_wait_lock, 10000);
  1580. if (module->orcjit_stop_compiling) {
  1581. /* init_llvm_jit_functions_stage2 failed */
  1582. os_mutex_unlock(&module->tierup_wait_lock);
  1583. os_mutex_unlock(&module->instance_list_lock);
  1584. return false;
  1585. }
  1586. }
  1587. os_mutex_unlock(&module->tierup_wait_lock);
  1588. llvm_jit_func_ptrs =
  1589. module_inst->func_ptrs + module->import_function_count;
  1590. for (i = 0; i < module->function_count; i++) {
  1591. llvm_jit_func_ptrs[i] = module->functions[i]->llvm_jit_func_ptr;
  1592. }
  1593. }
  1594. #endif
  1595. else if (running_mode == Mode_Multi_Tier_JIT) {
  1596. /* Notify backend threads to start llvm jit compilation */
  1597. module->enable_llvm_jit_compilation = true;
  1598. /* Free fast_jit_func_ptrs if it is allocated before */
  1599. if (module_inst->fast_jit_func_ptrs
  1600. && module_inst->fast_jit_func_ptrs != module->fast_jit_func_ptrs) {
  1601. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1602. }
  1603. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  1604. /* Copy all llvm jit func ptrs from the module */
  1605. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1606. sizeof(void *) * module->function_count, module->func_ptrs,
  1607. sizeof(void *) * module->function_count);
  1608. }
  1609. else {
  1610. bh_assert(0);
  1611. }
  1612. /* Add module instance into module's instance list if not added */
  1613. if (first_time_set) {
  1614. bool found = false;
  1615. WASMModuleInstance *node = module->instance_list;
  1616. while (node) {
  1617. if (node == module_inst) {
  1618. found = true;
  1619. break;
  1620. }
  1621. node = node->e->next;
  1622. }
  1623. if (!found) {
  1624. module_inst->e->next = module->instance_list;
  1625. module->instance_list = module_inst;
  1626. }
  1627. }
  1628. os_mutex_unlock(&module->instance_list_lock);
  1629. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1630. && WASM_ENABLE_LAZY_JIT != 0) */
  1631. (void)module;
  1632. return true;
  1633. }
  1634. bool
  1635. wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
  1636. {
  1637. return set_running_mode(module_inst, running_mode, false);
  1638. }
  1639. /**
  1640. * Instantiate module
  1641. */
  1642. WASMModuleInstance *
  1643. wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
  1644. WASMExecEnv *exec_env_main, uint32 stack_size,
  1645. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  1646. {
  1647. WASMModuleInstance *module_inst;
  1648. WASMGlobalInstance *globals = NULL, *global;
  1649. WASMTableInstance *first_table;
  1650. uint32 global_count, i;
  1651. uint32 base_offset, length, extra_info_offset;
  1652. uint32 module_inst_struct_size =
  1653. offsetof(WASMModuleInstance, global_table_data.bytes);
  1654. uint64 module_inst_mem_inst_size;
  1655. uint64 total_size, table_size = 0;
  1656. uint8 *global_data, *global_data_end;
  1657. #if WASM_ENABLE_MULTI_MODULE != 0
  1658. bool ret = false;
  1659. #endif
  1660. const bool is_sub_inst = parent != NULL;
  1661. if (!module)
  1662. return NULL;
  1663. /* Check the heap size */
  1664. heap_size = align_uint(heap_size, 8);
  1665. if (heap_size > APP_HEAP_SIZE_MAX)
  1666. heap_size = APP_HEAP_SIZE_MAX;
  1667. module_inst_mem_inst_size =
  1668. (uint64)sizeof(WASMMemoryInstance)
  1669. * (module->import_memory_count + module->memory_count);
  1670. #if WASM_ENABLE_JIT != 0
  1671. /* If the module dosen't have memory, reserve one mem_info space
  1672. with empty content to align with llvm jit compiler */
  1673. if (module_inst_mem_inst_size == 0)
  1674. module_inst_mem_inst_size = (uint64)sizeof(WASMMemoryInstance);
  1675. #endif
  1676. /* Size of module inst, memory instances and global data */
  1677. total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
  1678. + module->global_data_size;
  1679. /* Calculate the size of table data */
  1680. for (i = 0; i < module->import_table_count; i++) {
  1681. WASMTableImport *import_table = &module->import_tables[i].u.table;
  1682. table_size += offsetof(WASMTableInstance, elems);
  1683. #if WASM_ENABLE_MULTI_MODULE != 0
  1684. table_size +=
  1685. (uint64)sizeof(table_elem_type_t) * import_table->max_size;
  1686. #else
  1687. table_size += (uint64)sizeof(table_elem_type_t)
  1688. * (import_table->possible_grow ? import_table->max_size
  1689. : import_table->init_size);
  1690. #endif
  1691. }
  1692. for (i = 0; i < module->table_count; i++) {
  1693. WASMTable *table = module->tables + i;
  1694. table_size += offsetof(WASMTableInstance, elems);
  1695. #if WASM_ENABLE_MULTI_MODULE != 0
  1696. table_size += (uint64)sizeof(table_elem_type_t) * table->max_size;
  1697. #else
  1698. table_size +=
  1699. (uint64)sizeof(table_elem_type_t)
  1700. * (table->possible_grow ? table->max_size : table->init_size);
  1701. #endif
  1702. }
  1703. total_size += table_size;
  1704. /* The offset of WASMModuleInstanceExtra, make it 8-byte aligned */
  1705. total_size = (total_size + 7LL) & ~7LL;
  1706. extra_info_offset = (uint32)total_size;
  1707. total_size += sizeof(WASMModuleInstanceExtra);
  1708. /* Allocate the memory for module instance with memory instances,
  1709. global data, table data appended at the end */
  1710. if (!(module_inst =
  1711. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1712. return NULL;
  1713. }
  1714. module_inst->module_type = Wasm_Module_Bytecode;
  1715. module_inst->module = module;
  1716. module_inst->e =
  1717. (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
  1718. #if WASM_ENABLE_MULTI_MODULE != 0
  1719. module_inst->e->sub_module_inst_list =
  1720. &module_inst->e->sub_module_inst_list_head;
  1721. ret = wasm_runtime_sub_module_instantiate(
  1722. (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
  1723. stack_size, heap_size, error_buf, error_buf_size);
  1724. if (!ret) {
  1725. LOG_DEBUG("build a sub module list failed");
  1726. goto fail;
  1727. }
  1728. #endif
  1729. #if WASM_ENABLE_BULK_MEMORY != 0
  1730. if (module->data_seg_count > 0) {
  1731. module_inst->e->common.data_dropped =
  1732. bh_bitmap_new(0, module->data_seg_count);
  1733. if (module_inst->e->common.data_dropped == NULL) {
  1734. LOG_DEBUG("failed to allocate bitmaps");
  1735. set_error_buf(error_buf, error_buf_size,
  1736. "failed to allocate bitmaps");
  1737. goto fail;
  1738. }
  1739. }
  1740. #endif
  1741. #if WASM_ENABLE_REF_TYPES != 0
  1742. if (module->table_seg_count > 0) {
  1743. module_inst->e->common.elem_dropped =
  1744. bh_bitmap_new(0, module->table_seg_count);
  1745. if (module_inst->e->common.elem_dropped == NULL) {
  1746. LOG_DEBUG("failed to allocate bitmaps");
  1747. set_error_buf(error_buf, error_buf_size,
  1748. "failed to allocate bitmaps");
  1749. goto fail;
  1750. }
  1751. }
  1752. #endif
  1753. #if WASM_ENABLE_GC != 0
  1754. if (!is_sub_inst) {
  1755. uint32 gc_heap_size = wasm_runtime_get_gc_heap_size_default();
  1756. if (gc_heap_size < GC_HEAP_SIZE_MIN)
  1757. gc_heap_size = GC_HEAP_SIZE_MIN;
  1758. if (gc_heap_size > GC_HEAP_SIZE_MAX)
  1759. gc_heap_size = GC_HEAP_SIZE_MAX;
  1760. module_inst->e->common.gc_heap_pool =
  1761. runtime_malloc(gc_heap_size, error_buf, error_buf_size);
  1762. if (!module_inst->e->common.gc_heap_pool)
  1763. goto fail;
  1764. module_inst->e->common.gc_heap_handle = mem_allocator_create(
  1765. module_inst->e->common.gc_heap_pool, gc_heap_size);
  1766. if (!module_inst->e->common.gc_heap_handle)
  1767. goto fail;
  1768. }
  1769. #endif
  1770. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1771. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1772. error_buf, error_buf_size))) {
  1773. goto fail;
  1774. }
  1775. #endif
  1776. /* Instantiate global firstly to get the mutable data size */
  1777. global_count = module->import_global_count + module->global_count;
  1778. if (global_count
  1779. && !(globals = globals_instantiate(module, module_inst, error_buf,
  1780. error_buf_size))) {
  1781. goto fail;
  1782. }
  1783. module_inst->e->global_count = global_count;
  1784. module_inst->e->globals = globals;
  1785. module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
  1786. + module_inst_mem_inst_size;
  1787. module_inst->global_data_size = module->global_data_size;
  1788. first_table = (WASMTableInstance *)(module_inst->global_data
  1789. + module->global_data_size);
  1790. module_inst->memory_count =
  1791. module->import_memory_count + module->memory_count;
  1792. module_inst->table_count = module->import_table_count + module->table_count;
  1793. module_inst->e->function_count =
  1794. module->import_function_count + module->function_count;
  1795. /* export */
  1796. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1797. #if WASM_ENABLE_MULTI_MODULE != 0
  1798. module_inst->export_table_count =
  1799. get_export_count(module, EXPORT_KIND_TABLE);
  1800. module_inst->export_memory_count =
  1801. get_export_count(module, EXPORT_KIND_MEMORY);
  1802. module_inst->export_global_count =
  1803. get_export_count(module, EXPORT_KIND_GLOBAL);
  1804. #endif
  1805. /* Instantiate memories/tables/functions */
  1806. if ((module_inst->memory_count > 0
  1807. && !(module_inst->memories =
  1808. memories_instantiate(module, module_inst, parent, heap_size,
  1809. error_buf, error_buf_size)))
  1810. || (module_inst->table_count > 0
  1811. && !(module_inst->tables =
  1812. tables_instantiate(module, module_inst, first_table,
  1813. error_buf, error_buf_size)))
  1814. || (module_inst->e->function_count > 0
  1815. && !(module_inst->e->functions = functions_instantiate(
  1816. module, module_inst, error_buf, error_buf_size)))
  1817. || (module_inst->export_func_count > 0
  1818. && !(module_inst->export_functions = export_functions_instantiate(
  1819. module, module_inst, module_inst->export_func_count,
  1820. error_buf, error_buf_size)))
  1821. #if WASM_ENABLE_MULTI_MODULE != 0
  1822. || (module_inst->export_global_count > 0
  1823. && !(module_inst->export_globals = export_globals_instantiate(
  1824. module, module_inst, module_inst->export_global_count,
  1825. error_buf, error_buf_size)))
  1826. #endif
  1827. #if WASM_ENABLE_JIT != 0
  1828. || (module_inst->e->function_count > 0
  1829. && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  1830. #endif
  1831. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1832. || (module_inst->e->function_count > 0
  1833. && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
  1834. #endif
  1835. ) {
  1836. goto fail;
  1837. }
  1838. if (global_count > 0) {
  1839. /* Initialize the global data */
  1840. global_data = module_inst->global_data;
  1841. global_data_end = global_data + module->global_data_size;
  1842. global = globals;
  1843. for (i = 0; i < global_count; i++, global++) {
  1844. switch (global->type) {
  1845. case VALUE_TYPE_I32:
  1846. case VALUE_TYPE_F32:
  1847. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1848. case VALUE_TYPE_FUNCREF:
  1849. case VALUE_TYPE_EXTERNREF:
  1850. #endif
  1851. *(int32 *)global_data = global->initial_value.i32;
  1852. global_data += sizeof(int32);
  1853. break;
  1854. case VALUE_TYPE_I64:
  1855. case VALUE_TYPE_F64:
  1856. bh_memcpy_s(global_data,
  1857. (uint32)(global_data_end - global_data),
  1858. &global->initial_value.i64, sizeof(int64));
  1859. global_data += sizeof(int64);
  1860. break;
  1861. #if WASM_ENABLE_SIMD != 0
  1862. case VALUE_TYPE_V128:
  1863. bh_memcpy_s(global_data, (uint32)sizeof(V128),
  1864. &global->initial_value.v128, sizeof(V128));
  1865. global_data += sizeof(V128);
  1866. break;
  1867. #endif
  1868. #if WASM_ENABLE_GC != 0
  1869. case VALUE_TYPE_EXTERNREF:
  1870. /* the initial value should be a null reference */
  1871. bh_assert(global->initial_value.gc_obj == NULL_REF);
  1872. STORE_PTR((void **)global_data, NULL_REF);
  1873. global_data += sizeof(void *);
  1874. break;
  1875. #endif
  1876. default:
  1877. {
  1878. #if WASM_ENABLE_GC != 0
  1879. InitializerExpression *global_init = NULL;
  1880. bh_assert(wasm_is_type_reftype(global->type));
  1881. if (i >= module->import_global_count) {
  1882. global_init =
  1883. &module->globals[i - module->import_global_count]
  1884. .init_expr;
  1885. }
  1886. if (global->type == REF_TYPE_NULLFUNCREF
  1887. || global->type == REF_TYPE_NULLEXTERNREF
  1888. || global->type == REF_TYPE_NULLREF) {
  1889. STORE_PTR((void **)global_data, NULL_REF);
  1890. global_data += sizeof(void *);
  1891. break;
  1892. }
  1893. /* We can't create funcref obj during global instantiation
  1894. * since the functions are not instantiated yet, so we need
  1895. * to defer the initialization here */
  1896. if (global_init
  1897. && (global_init->init_expr_type
  1898. == INIT_EXPR_TYPE_FUNCREF_CONST)
  1899. && wasm_reftype_is_subtype_of(
  1900. global->type, global->ref_type, REF_TYPE_FUNCREF,
  1901. NULL, module_inst->module->types,
  1902. module_inst->module->type_count)) {
  1903. WASMFuncObjectRef func_obj = NULL;
  1904. /* UINT32_MAX indicates that it is a null reference */
  1905. if ((uint32)global->initial_value.i32 != UINT32_MAX) {
  1906. if (!(func_obj = wasm_create_func_obj(
  1907. module_inst, global->initial_value.i32,
  1908. false, error_buf, error_buf_size)))
  1909. goto fail;
  1910. }
  1911. STORE_PTR((void **)global_data, func_obj);
  1912. global_data += sizeof(void *);
  1913. /* Also update the inital_value since other globals may
  1914. * refer to this */
  1915. global->initial_value.gc_obj = (wasm_obj_t)func_obj;
  1916. break;
  1917. }
  1918. else {
  1919. STORE_PTR((void **)global_data,
  1920. global->initial_value.gc_obj);
  1921. global_data += sizeof(void *);
  1922. break;
  1923. }
  1924. #endif
  1925. bh_assert(0);
  1926. break;
  1927. }
  1928. }
  1929. }
  1930. bh_assert(global_data == global_data_end);
  1931. }
  1932. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1933. goto fail;
  1934. }
  1935. /* Initialize the memory data with data segment section */
  1936. for (i = 0; i < module->data_seg_count; i++) {
  1937. WASMMemoryInstance *memory = NULL;
  1938. uint8 *memory_data = NULL;
  1939. uint64 memory_size = 0;
  1940. WASMDataSeg *data_seg = module->data_segments[i];
  1941. #if WASM_ENABLE_BULK_MEMORY != 0
  1942. if (data_seg->is_passive)
  1943. continue;
  1944. #endif
  1945. if (is_sub_inst)
  1946. /* Ignore setting memory init data if the memory has been
  1947. initialized */
  1948. continue;
  1949. /* has check it in loader */
  1950. memory = module_inst->memories[data_seg->memory_index];
  1951. bh_assert(memory);
  1952. memory_data = memory->memory_data;
  1953. memory_size =
  1954. (uint64)memory->num_bytes_per_page * memory->cur_page_count;
  1955. bh_assert(memory_data || memory_size == 0);
  1956. bh_assert(data_seg->base_offset.init_expr_type
  1957. == INIT_EXPR_TYPE_I32_CONST
  1958. || data_seg->base_offset.init_expr_type
  1959. == INIT_EXPR_TYPE_GET_GLOBAL);
  1960. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1961. if (!check_global_init_expr(module,
  1962. data_seg->base_offset.u.global_index,
  1963. error_buf, error_buf_size)) {
  1964. goto fail;
  1965. }
  1966. if (!globals
  1967. || globals[data_seg->base_offset.u.global_index].type
  1968. != VALUE_TYPE_I32) {
  1969. set_error_buf(error_buf, error_buf_size,
  1970. "data segment does not fit");
  1971. goto fail;
  1972. }
  1973. base_offset =
  1974. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  1975. }
  1976. else {
  1977. base_offset = (uint32)data_seg->base_offset.u.i32;
  1978. }
  1979. /* check offset */
  1980. if (base_offset > memory_size) {
  1981. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1982. memory_size);
  1983. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1984. set_error_buf(error_buf, error_buf_size,
  1985. "out of bounds memory access");
  1986. #else
  1987. set_error_buf(error_buf, error_buf_size,
  1988. "data segment does not fit");
  1989. #endif
  1990. goto fail;
  1991. }
  1992. /* check offset + length(could be zero) */
  1993. length = data_seg->data_length;
  1994. if ((uint64)base_offset + length > memory_size) {
  1995. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1996. base_offset, length, memory_size);
  1997. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1998. set_error_buf(error_buf, error_buf_size,
  1999. "out of bounds memory access");
  2000. #else
  2001. set_error_buf(error_buf, error_buf_size,
  2002. "data segment does not fit");
  2003. #endif
  2004. goto fail;
  2005. }
  2006. if (memory_data) {
  2007. bh_memcpy_s(memory_data + base_offset,
  2008. (uint32)memory_size - base_offset, data_seg->data,
  2009. length);
  2010. }
  2011. }
  2012. #if WASM_ENABLE_GC != 0
  2013. /* Initialize the table data with init expr */
  2014. for (i = 0; i < module->table_count; i++) {
  2015. WASMTable *table = module->tables + i;
  2016. WASMTableInstance *table_inst = module_inst->tables[i];
  2017. table_elem_type_t *table_data;
  2018. uint32 j;
  2019. if (table->init_expr.init_expr_type == 0) {
  2020. /* No table initializer */
  2021. continue;
  2022. }
  2023. table_data = table_inst->elems;
  2024. bh_assert(
  2025. table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  2026. || table->init_expr.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  2027. || table->init_expr.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST);
  2028. if (table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  2029. if (!check_global_init_expr(module, table->init_expr.u.global_index,
  2030. error_buf, error_buf_size)) {
  2031. goto fail;
  2032. }
  2033. table->init_expr.u.gc_obj =
  2034. globals[table->init_expr.u.global_index].initial_value.gc_obj;
  2035. }
  2036. else if (table->init_expr.init_expr_type
  2037. == INIT_EXPR_TYPE_FUNCREF_CONST) {
  2038. uint32 func_idx = table->init_expr.u.ref_index;
  2039. if (func_idx != UINT32_MAX) {
  2040. if (!(table->init_expr.u.gc_obj =
  2041. wasm_create_func_obj(module_inst, func_idx, false,
  2042. error_buf, error_buf_size)))
  2043. goto fail;
  2044. }
  2045. else {
  2046. table->init_expr.u.gc_obj = NULL_REF;
  2047. }
  2048. }
  2049. else if (table->init_expr.init_expr_type
  2050. == INIT_EXPR_TYPE_REFNULL_CONST) {
  2051. table->init_expr.u.gc_obj = NULL_REF;
  2052. }
  2053. LOG_DEBUG("Init table [%d] elements from [%d] to [%d] as: %p", i, 0,
  2054. table_inst->cur_size, (void *)table->init_expr.u.gc_obj);
  2055. for (j = 0; j < table_inst->cur_size; j++) {
  2056. *(table_data + j) = table->init_expr.u.gc_obj;
  2057. }
  2058. }
  2059. #endif /* end of WASM_ENABLE_GC != 0 */
  2060. /* Initialize the table data with table segment section */
  2061. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  2062. i++) {
  2063. WASMTableSeg *table_seg = module->table_segments + i;
  2064. /* has check it in loader */
  2065. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  2066. table_elem_type_t *table_data;
  2067. uint32 j;
  2068. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2069. uint8 tbl_elem_type;
  2070. uint32 tbl_init_size, tbl_max_size;
  2071. #endif
  2072. #if WASM_ENABLE_GC != 0
  2073. WASMRefType *tbl_elem_ref_type;
  2074. #endif
  2075. bh_assert(table);
  2076. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2077. (void)wasm_runtime_get_table_inst_elem_type(
  2078. (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
  2079. &tbl_elem_type,
  2080. #if WASM_ENABLE_GC != 0
  2081. &tbl_elem_ref_type,
  2082. #endif
  2083. &tbl_init_size, &tbl_max_size);
  2084. #if WASM_ENABLE_GC == 0
  2085. if (tbl_elem_type != VALUE_TYPE_FUNCREF
  2086. && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
  2087. set_error_buf(error_buf, error_buf_size,
  2088. "type mismatch: elements segment does not fit");
  2089. goto fail;
  2090. }
  2091. #elif WASM_ENABLE_GC != 0
  2092. if (!wasm_elem_is_declarative(table_seg->mode)
  2093. && !wasm_reftype_is_subtype_of(
  2094. table_seg->elem_type, table_seg->elem_ref_type,
  2095. table->elem_type, table->elem_ref_type.elem_ref_type,
  2096. module->types, module->type_count)) {
  2097. set_error_buf(error_buf, error_buf_size,
  2098. "type mismatch: elements segment does not fit");
  2099. goto fail;
  2100. }
  2101. #endif
  2102. (void)tbl_init_size;
  2103. (void)tbl_max_size;
  2104. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2105. table_data = table->elems;
  2106. #if WASM_ENABLE_MULTI_MODULE != 0
  2107. if (table_seg->table_index < module->import_table_count
  2108. && module_inst->e->table_insts_linked[table_seg->table_index]) {
  2109. table_data =
  2110. module_inst->e->table_insts_linked[table_seg->table_index]
  2111. ->elems;
  2112. }
  2113. #endif
  2114. bh_assert(table_data);
  2115. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2116. if (!wasm_elem_is_active(table_seg->mode))
  2117. continue;
  2118. #endif
  2119. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2120. bh_assert(table_seg->base_offset.init_expr_type
  2121. == INIT_EXPR_TYPE_I32_CONST
  2122. || table_seg->base_offset.init_expr_type
  2123. == INIT_EXPR_TYPE_GET_GLOBAL
  2124. || table_seg->base_offset.init_expr_type
  2125. == INIT_EXPR_TYPE_FUNCREF_CONST
  2126. || table_seg->base_offset.init_expr_type
  2127. == INIT_EXPR_TYPE_REFNULL_CONST);
  2128. #else
  2129. bh_assert(table_seg->base_offset.init_expr_type
  2130. == INIT_EXPR_TYPE_I32_CONST
  2131. || table_seg->base_offset.init_expr_type
  2132. == INIT_EXPR_TYPE_GET_GLOBAL);
  2133. #endif
  2134. /* init vec(funcidx) or vec(expr) */
  2135. if (table_seg->base_offset.init_expr_type
  2136. == INIT_EXPR_TYPE_GET_GLOBAL) {
  2137. if (!check_global_init_expr(module,
  2138. table_seg->base_offset.u.global_index,
  2139. error_buf, error_buf_size)) {
  2140. goto fail;
  2141. }
  2142. if (!globals
  2143. || globals[table_seg->base_offset.u.global_index].type
  2144. != VALUE_TYPE_I32) {
  2145. set_error_buf(error_buf, error_buf_size,
  2146. "type mismatch: elements segment does not fit");
  2147. goto fail;
  2148. }
  2149. table_seg->base_offset.u.i32 =
  2150. globals[table_seg->base_offset.u.global_index]
  2151. .initial_value.i32;
  2152. }
  2153. /* check offset since length might negative */
  2154. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  2155. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  2156. table_seg->base_offset.u.i32, table->cur_size);
  2157. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2158. set_error_buf(error_buf, error_buf_size,
  2159. "out of bounds table access");
  2160. #else
  2161. set_error_buf(error_buf, error_buf_size,
  2162. "type mismatch: elements segment does not fit");
  2163. #endif
  2164. goto fail;
  2165. }
  2166. /* check offset + length(could be zero) */
  2167. length = table_seg->value_count;
  2168. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  2169. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  2170. table_seg->base_offset.u.i32, length, table->cur_size);
  2171. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2172. set_error_buf(error_buf, error_buf_size,
  2173. "out of bounds table access");
  2174. #else
  2175. set_error_buf(error_buf, error_buf_size,
  2176. "type mismatch: elements segment does not fit");
  2177. #endif
  2178. goto fail;
  2179. }
  2180. for (j = 0; j < length; j++) {
  2181. InitializerExpression *init_expr = &table_seg->init_values[j];
  2182. uint8 flag = init_expr->init_expr_type;
  2183. void *ref = NULL;
  2184. /* const and get global init values should be resolved during
  2185. * loading */
  2186. bh_assert((flag == INIT_EXPR_TYPE_GET_GLOBAL)
  2187. || (flag == INIT_EXPR_TYPE_REFNULL_CONST)
  2188. || ((flag >= INIT_EXPR_TYPE_FUNCREF_CONST)
  2189. && (flag <= INIT_EXPR_TYPE_EXTERN_CONVERT_ANY)));
  2190. switch (flag) {
  2191. case INIT_EXPR_TYPE_REFNULL_CONST:
  2192. ref = NULL;
  2193. break;
  2194. case INIT_EXPR_TYPE_FUNCREF_CONST:
  2195. {
  2196. #if WASM_ENABLE_GC == 0
  2197. ref = (void *)(uintptr_t)init_expr->u.ref_index;
  2198. #else
  2199. WASMFuncObjectRef func_obj;
  2200. uint32 func_idx = init_expr->u.ref_index;
  2201. /* UINT32_MAX indicates that it is a null reference */
  2202. if (func_idx != UINT32_MAX) {
  2203. if (!(func_obj = wasm_create_func_obj(
  2204. module_inst, func_idx, false, error_buf,
  2205. error_buf_size))) {
  2206. goto fail;
  2207. }
  2208. ref = func_obj;
  2209. }
  2210. else {
  2211. ref = NULL_REF;
  2212. }
  2213. #endif /* end of WASM_ENABLE_GC != 0 */
  2214. break;
  2215. }
  2216. #if WASM_ENABLE_GC != 0
  2217. case INIT_EXPR_TYPE_GET_GLOBAL:
  2218. {
  2219. if (!check_global_init_expr(module,
  2220. init_expr->u.global_index,
  2221. error_buf, error_buf_size)) {
  2222. goto fail;
  2223. }
  2224. ref =
  2225. globals[init_expr->u.global_index].initial_value.gc_obj;
  2226. break;
  2227. }
  2228. case INIT_EXPR_TYPE_STRUCT_NEW:
  2229. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  2230. {
  2231. WASMRttType *rtt_type;
  2232. WASMStructObjectRef struct_obj;
  2233. WASMStructType *struct_type;
  2234. WASMStructNewInitValues *init_values = NULL;
  2235. uint32 type_idx;
  2236. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  2237. init_values =
  2238. (WASMStructNewInitValues *)init_expr->u.data;
  2239. type_idx = init_values->type_idx;
  2240. }
  2241. else {
  2242. type_idx = init_expr->u.type_index;
  2243. }
  2244. struct_type = (WASMStructType *)module->types[type_idx];
  2245. if (!(rtt_type = wasm_rtt_type_new(
  2246. (WASMType *)struct_type, type_idx,
  2247. module->rtt_types, module->type_count,
  2248. &module->rtt_type_lock))) {
  2249. set_error_buf(error_buf, error_buf_size,
  2250. "create rtt object failed");
  2251. goto fail;
  2252. }
  2253. if (!(struct_obj = wasm_struct_obj_new_internal(
  2254. module_inst->e->common.gc_heap_handle,
  2255. rtt_type))) {
  2256. set_error_buf(error_buf, error_buf_size,
  2257. "create struct object failed");
  2258. goto fail;
  2259. }
  2260. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  2261. uint32 field_idx;
  2262. bh_assert(init_values->count
  2263. == struct_type->field_count);
  2264. for (field_idx = 0; field_idx < init_values->count;
  2265. field_idx++) {
  2266. wasm_struct_obj_set_field(
  2267. struct_obj, field_idx,
  2268. &init_values->fields[field_idx]);
  2269. }
  2270. }
  2271. ref = struct_obj;
  2272. break;
  2273. }
  2274. case INIT_EXPR_TYPE_ARRAY_NEW:
  2275. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  2276. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  2277. {
  2278. WASMRttType *rtt_type;
  2279. WASMArrayObjectRef array_obj;
  2280. WASMArrayType *array_type;
  2281. WASMArrayNewInitValues *init_values = NULL;
  2282. WASMValue *arr_init_val = NULL, empty_val = { 0 };
  2283. uint32 type_idx, len;
  2284. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  2285. type_idx = init_expr->u.array_new_default.type_index;
  2286. len = init_expr->u.array_new_default.length;
  2287. arr_init_val = &empty_val;
  2288. }
  2289. else {
  2290. init_values =
  2291. (WASMArrayNewInitValues *)init_expr->u.data;
  2292. type_idx = init_values->type_idx;
  2293. len = init_values->length;
  2294. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  2295. arr_init_val = init_values->elem_data;
  2296. }
  2297. }
  2298. array_type = (WASMArrayType *)module->types[type_idx];
  2299. if (!(rtt_type = wasm_rtt_type_new(
  2300. (WASMType *)array_type, type_idx,
  2301. module->rtt_types, module->type_count,
  2302. &module->rtt_type_lock))) {
  2303. set_error_buf(error_buf, error_buf_size,
  2304. "create rtt object failed");
  2305. goto fail;
  2306. }
  2307. if (!(array_obj = wasm_array_obj_new_internal(
  2308. module_inst->e->common.gc_heap_handle, rtt_type,
  2309. len, arr_init_val))) {
  2310. set_error_buf(error_buf, error_buf_size,
  2311. "create array object failed");
  2312. goto fail;
  2313. }
  2314. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  2315. uint32 elem_idx;
  2316. bh_assert(init_values);
  2317. for (elem_idx = 0; elem_idx < len; elem_idx++) {
  2318. wasm_array_obj_set_elem(
  2319. array_obj, elem_idx,
  2320. &init_values->elem_data[elem_idx]);
  2321. }
  2322. }
  2323. ref = array_obj;
  2324. break;
  2325. }
  2326. case INIT_EXPR_TYPE_I31_NEW:
  2327. {
  2328. ref = (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
  2329. break;
  2330. }
  2331. #endif /* end of WASM_ENABLE_GC != 0 */
  2332. }
  2333. *(table_data + table_seg->base_offset.u.i32 + j) =
  2334. (table_elem_type_t)ref;
  2335. }
  2336. }
  2337. /* Initialize the thread related data */
  2338. if (stack_size == 0)
  2339. stack_size = DEFAULT_WASM_STACK_SIZE;
  2340. #if WASM_ENABLE_SPEC_TEST != 0
  2341. #if WASM_ENABLE_TAIL_CALL == 0
  2342. if (stack_size < 128 * 1024)
  2343. stack_size = 128 * 1024;
  2344. #else
  2345. /* Some tail-call cases require large operand stack */
  2346. if (stack_size < 10 * 1024 * 1024)
  2347. stack_size = 10 * 1024 * 1024;
  2348. #endif
  2349. #endif
  2350. module_inst->default_wasm_stack_size = stack_size;
  2351. if (module->malloc_function != (uint32)-1) {
  2352. module_inst->e->malloc_function =
  2353. &module_inst->e->functions[module->malloc_function];
  2354. }
  2355. if (module->free_function != (uint32)-1) {
  2356. module_inst->e->free_function =
  2357. &module_inst->e->functions[module->free_function];
  2358. }
  2359. if (module->retain_function != (uint32)-1) {
  2360. module_inst->e->retain_function =
  2361. &module_inst->e->functions[module->retain_function];
  2362. }
  2363. #if WASM_ENABLE_LIBC_WASI != 0
  2364. /* The sub-instance will get the wasi_ctx from main-instance */
  2365. if (!is_sub_inst) {
  2366. if (!wasm_runtime_init_wasi(
  2367. (WASMModuleInstanceCommon *)module_inst,
  2368. module->wasi_args.dir_list, module->wasi_args.dir_count,
  2369. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  2370. module->wasi_args.env, module->wasi_args.env_count,
  2371. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  2372. module->wasi_args.ns_lookup_pool,
  2373. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  2374. module->wasi_args.argc, module->wasi_args.stdio[0],
  2375. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  2376. error_buf, error_buf_size)) {
  2377. goto fail;
  2378. }
  2379. }
  2380. #endif
  2381. #if WASM_ENABLE_DEBUG_INTERP != 0
  2382. if (!is_sub_inst) {
  2383. /* Add module instance into module's instance list */
  2384. os_mutex_lock(&module->instance_list_lock);
  2385. if (module->instance_list) {
  2386. LOG_WARNING(
  2387. "warning: multiple instances referencing to the same module "
  2388. "may cause unexpected behaviour during debugging");
  2389. }
  2390. module_inst->e->next = module->instance_list;
  2391. module->instance_list = module_inst;
  2392. os_mutex_unlock(&module->instance_list_lock);
  2393. }
  2394. #endif
  2395. /* Set running mode before executing wasm functions */
  2396. if (!set_running_mode(module_inst, wasm_runtime_get_default_running_mode(),
  2397. true)) {
  2398. set_error_buf(error_buf, error_buf_size,
  2399. "set instance running mode failed");
  2400. goto fail;
  2401. }
  2402. if (module->start_function != (uint32)-1) {
  2403. /* TODO: fix start function can be import function issue */
  2404. if (module->start_function >= module->import_function_count)
  2405. module_inst->e->start_function =
  2406. &module_inst->e->functions[module->start_function];
  2407. }
  2408. if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
  2409. exec_env_main)) {
  2410. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  2411. goto fail;
  2412. }
  2413. #if WASM_ENABLE_MEMORY_TRACING != 0
  2414. wasm_runtime_dump_module_inst_mem_consumption(
  2415. (WASMModuleInstanceCommon *)module_inst);
  2416. #endif
  2417. (void)global_data_end;
  2418. return module_inst;
  2419. fail:
  2420. wasm_deinstantiate(module_inst, false);
  2421. return NULL;
  2422. }
  2423. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2424. static void
  2425. destroy_c_api_frames(Vector *frames)
  2426. {
  2427. WASMCApiFrame frame = { 0 };
  2428. uint32 i, total_frames, ret;
  2429. total_frames = (uint32)bh_vector_size(frames);
  2430. for (i = 0; i < total_frames; i++) {
  2431. ret = bh_vector_get(frames, i, &frame);
  2432. bh_assert(ret);
  2433. if (frame.lp)
  2434. wasm_runtime_free(frame.lp);
  2435. }
  2436. ret = bh_vector_destroy(frames);
  2437. bh_assert(ret);
  2438. (void)ret;
  2439. }
  2440. #endif
  2441. void
  2442. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  2443. {
  2444. if (!module_inst)
  2445. return;
  2446. if (module_inst->exec_env_singleton) {
  2447. /* wasm_exec_env_destroy will call
  2448. wasm_cluster_wait_for_all_except_self to wait for other
  2449. threads, so as to destroy their exec_envs and module
  2450. instances first, and avoid accessing the shared resources
  2451. of current module instance after it is deinstantiated. */
  2452. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  2453. }
  2454. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  2455. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2456. && WASM_ENABLE_LAZY_JIT != 0)
  2457. /* Remove instance from module's instance list before freeing
  2458. func_ptrs and fast_jit_func_ptrs of the instance, to avoid
  2459. accessing the freed memory in the jit backend compilation
  2460. threads */
  2461. {
  2462. WASMModule *module = module_inst->module;
  2463. WASMModuleInstance *instance_prev = NULL, *instance;
  2464. os_mutex_lock(&module->instance_list_lock);
  2465. instance = module->instance_list;
  2466. while (instance) {
  2467. if (instance == module_inst) {
  2468. if (!instance_prev)
  2469. module->instance_list = instance->e->next;
  2470. else
  2471. instance_prev->e->next = instance->e->next;
  2472. break;
  2473. }
  2474. instance_prev = instance;
  2475. instance = instance->e->next;
  2476. }
  2477. os_mutex_unlock(&module->instance_list_lock);
  2478. }
  2479. #endif
  2480. #if WASM_ENABLE_JIT != 0
  2481. if (module_inst->func_ptrs)
  2482. wasm_runtime_free(module_inst->func_ptrs);
  2483. #endif
  2484. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2485. && WASM_ENABLE_LAZY_JIT != 0
  2486. if (module_inst->fast_jit_func_ptrs
  2487. && module_inst->fast_jit_func_ptrs
  2488. != module_inst->module->fast_jit_func_ptrs)
  2489. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  2490. #endif
  2491. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2492. if (module_inst->func_type_indexes)
  2493. wasm_runtime_free(module_inst->func_type_indexes);
  2494. #endif
  2495. #if WASM_ENABLE_MULTI_MODULE != 0
  2496. wasm_runtime_sub_module_deinstantiate(
  2497. (WASMModuleInstanceCommon *)module_inst);
  2498. #endif
  2499. if (module_inst->memory_count > 0)
  2500. memories_deinstantiate(module_inst, module_inst->memories,
  2501. module_inst->memory_count);
  2502. if (module_inst->import_func_ptrs) {
  2503. wasm_runtime_free(module_inst->import_func_ptrs);
  2504. }
  2505. tables_deinstantiate(module_inst);
  2506. functions_deinstantiate(module_inst->e->functions,
  2507. module_inst->e->function_count);
  2508. globals_deinstantiate(module_inst->e->globals);
  2509. export_functions_deinstantiate(module_inst->export_functions);
  2510. #if WASM_ENABLE_MULTI_MODULE != 0
  2511. export_globals_deinstantiate(module_inst->export_globals);
  2512. #endif
  2513. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2514. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  2515. #endif
  2516. #if WASM_ENABLE_GC != 0
  2517. if (!is_sub_inst) {
  2518. if (module_inst->e->common.gc_heap_handle)
  2519. mem_allocator_destroy(module_inst->e->common.gc_heap_handle);
  2520. if (module_inst->e->common.gc_heap_pool)
  2521. wasm_runtime_free(module_inst->e->common.gc_heap_pool);
  2522. }
  2523. #endif
  2524. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2525. if (module_inst->frames) {
  2526. destroy_c_api_frames(module_inst->frames);
  2527. wasm_runtime_free(module_inst->frames);
  2528. module_inst->frames = NULL;
  2529. }
  2530. #endif
  2531. if (module_inst->e->common.c_api_func_imports)
  2532. wasm_runtime_free(module_inst->e->common.c_api_func_imports);
  2533. if (!is_sub_inst) {
  2534. #if WASM_ENABLE_WASI_NN != 0
  2535. wasi_nn_destroy(module_inst);
  2536. #endif
  2537. wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
  2538. }
  2539. #if WASM_ENABLE_BULK_MEMORY != 0
  2540. bh_bitmap_delete(module_inst->e->common.data_dropped);
  2541. #endif
  2542. #if WASM_ENABLE_REF_TYPES != 0
  2543. bh_bitmap_delete(module_inst->e->common.elem_dropped);
  2544. #endif
  2545. wasm_runtime_free(module_inst);
  2546. }
  2547. WASMFunctionInstance *
  2548. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  2549. const char *signature)
  2550. {
  2551. uint32 i;
  2552. for (i = 0; i < module_inst->export_func_count; i++)
  2553. if (!strcmp(module_inst->export_functions[i].name, name))
  2554. return module_inst->export_functions[i].function;
  2555. (void)signature;
  2556. return NULL;
  2557. }
  2558. #if WASM_ENABLE_MULTI_MODULE != 0
  2559. WASMGlobalInstance *
  2560. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  2561. {
  2562. uint32 i;
  2563. for (i = 0; i < module_inst->export_global_count; i++)
  2564. if (!strcmp(module_inst->export_globals[i].name, name))
  2565. return module_inst->export_globals[i].global;
  2566. return NULL;
  2567. }
  2568. WASMMemoryInstance *
  2569. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  2570. {
  2571. /**
  2572. * using a strong assumption that one module instance only has
  2573. * one memory instance
  2574. */
  2575. (void)module_inst->export_memories;
  2576. return module_inst->memories[0];
  2577. }
  2578. WASMTableInstance *
  2579. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  2580. {
  2581. /**
  2582. * using a strong assumption that one module instance only has
  2583. * one table instance
  2584. */
  2585. (void)module_inst->export_tables;
  2586. return module_inst->tables[0];
  2587. }
  2588. #endif
  2589. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2590. static void
  2591. call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
  2592. WASMExecEnv *exec_env,
  2593. WASMFunctionInstance *function, unsigned argc,
  2594. uint32 argv[])
  2595. {
  2596. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2597. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  2598. uint32 page_size = os_getpagesize();
  2599. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  2600. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  2601. uint8 *prev_top = exec_env->wasm_stack.top;
  2602. #ifdef BH_PLATFORM_WINDOWS
  2603. int result;
  2604. bool has_exception;
  2605. char exception[EXCEPTION_BUF_LEN];
  2606. #endif
  2607. bool ret = true;
  2608. /* Check native stack overflow firstly to ensure we have enough
  2609. native stack to run the following codes before actually calling
  2610. the aot function in invokeNative function. */
  2611. RECORD_STACK_USAGE(exec_env, (uint8 *)&exec_env_tls);
  2612. if ((uint8 *)&exec_env_tls < exec_env->native_stack_boundary
  2613. + page_size * (guard_page_count + 1)) {
  2614. wasm_set_exception(module_inst, "native stack overflow");
  2615. return;
  2616. }
  2617. if (!exec_env_tls) {
  2618. if (!os_thread_signal_inited()) {
  2619. wasm_set_exception(module_inst, "thread signal env not inited");
  2620. return;
  2621. }
  2622. /* Set thread handle and stack boundary if they haven't been set */
  2623. wasm_exec_env_set_thread_info(exec_env);
  2624. wasm_runtime_set_exec_env_tls(exec_env);
  2625. }
  2626. else {
  2627. if (exec_env_tls != exec_env) {
  2628. wasm_set_exception(module_inst, "invalid exec env");
  2629. return;
  2630. }
  2631. }
  2632. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  2633. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  2634. #ifndef BH_PLATFORM_WINDOWS
  2635. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2636. #else
  2637. __try {
  2638. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2639. } __except (wasm_copy_exception(module_inst, NULL)
  2640. ? EXCEPTION_EXECUTE_HANDLER
  2641. : EXCEPTION_CONTINUE_SEARCH) {
  2642. /* Exception was thrown in wasm_exception_handler */
  2643. ret = false;
  2644. }
  2645. has_exception = wasm_copy_exception(module_inst, exception);
  2646. if (has_exception && strstr(exception, "native stack overflow")) {
  2647. /* After a stack overflow, the stack was left
  2648. in a damaged state, let the CRT repair it */
  2649. result = _resetstkoflw();
  2650. bh_assert(result != 0);
  2651. }
  2652. #endif
  2653. }
  2654. else {
  2655. /* Exception has been set in signal handler before calling longjmp */
  2656. ret = false;
  2657. }
  2658. /* Note: can't check wasm_get_exception(module_inst) here, there may be
  2659. * exception which is not caught by hardware (e.g. uninitialized elements),
  2660. * then the stack-frame is already freed inside wasm_interp_call_wasm */
  2661. if (!ret) {
  2662. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2663. if (wasm_interp_create_call_stack(exec_env)) {
  2664. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  2665. }
  2666. #endif
  2667. /* Restore operand frames */
  2668. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  2669. exec_env->wasm_stack.top = prev_top;
  2670. }
  2671. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  2672. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  2673. if (!exec_env->jmpbuf_stack_top) {
  2674. wasm_runtime_set_exec_env_tls(NULL);
  2675. }
  2676. if (!ret) {
  2677. os_sigreturn();
  2678. os_signal_unmask();
  2679. }
  2680. (void)jmpbuf_node_pop;
  2681. }
  2682. #define interp_call_wasm call_wasm_with_hw_bound_check
  2683. #else
  2684. #define interp_call_wasm wasm_interp_call_wasm
  2685. #endif
  2686. bool
  2687. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  2688. unsigned argc, uint32 argv[])
  2689. {
  2690. WASMModuleInstance *module_inst =
  2691. (WASMModuleInstance *)exec_env->module_inst;
  2692. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2693. /* Set thread handle and stack boundary */
  2694. wasm_exec_env_set_thread_info(exec_env);
  2695. #else
  2696. /* Set thread info in call_wasm_with_hw_bound_check when
  2697. hw bound check is enabled */
  2698. #endif
  2699. /* Set exec env so it can be later retrieved from instance */
  2700. module_inst->e->common.cur_exec_env = exec_env;
  2701. interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2702. return !wasm_copy_exception(module_inst, NULL);
  2703. }
  2704. #if WASM_ENABLE_PERF_PROFILING != 0
  2705. void
  2706. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  2707. {
  2708. WASMExportFuncInstance *export_func;
  2709. WASMFunctionInstance *func_inst;
  2710. char *func_name;
  2711. uint32 i, j;
  2712. os_printf("Performance profiler data:\n");
  2713. for (i = 0; i < module_inst->e->function_count; i++) {
  2714. func_inst = module_inst->e->functions + i;
  2715. if (func_inst->is_import_func) {
  2716. func_name = func_inst->u.func_import->field_name;
  2717. }
  2718. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2719. else if (func_inst->u.func->field_name) {
  2720. func_name = func_inst->u.func->field_name;
  2721. }
  2722. #endif
  2723. else {
  2724. func_name = NULL;
  2725. for (j = 0; j < module_inst->export_func_count; j++) {
  2726. export_func = module_inst->export_functions + j;
  2727. if (export_func->function == func_inst) {
  2728. func_name = export_func->name;
  2729. break;
  2730. }
  2731. }
  2732. }
  2733. if (func_name)
  2734. os_printf(
  2735. " func %s, execution time: %.3f ms, execution count: %" PRIu32
  2736. " times\n",
  2737. func_name,
  2738. module_inst->e->functions[i].total_exec_time / 1000.0f,
  2739. module_inst->e->functions[i].total_exec_cnt);
  2740. else
  2741. os_printf(" func %" PRIu32
  2742. ", execution time: %.3f ms, execution count: %" PRIu32
  2743. " times\n",
  2744. i, module_inst->e->functions[i].total_exec_time / 1000.0f,
  2745. module_inst->e->functions[i].total_exec_cnt);
  2746. }
  2747. }
  2748. #endif
  2749. uint32
  2750. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  2751. WASMExecEnv *exec_env, uint32 size,
  2752. void **p_native_addr)
  2753. {
  2754. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2755. uint8 *addr = NULL;
  2756. uint32 offset = 0;
  2757. if (!memory) {
  2758. wasm_set_exception(module_inst, "uninitialized memory");
  2759. return 0;
  2760. }
  2761. if (memory->heap_handle) {
  2762. addr = mem_allocator_malloc(memory->heap_handle, size);
  2763. }
  2764. else if (module_inst->e->malloc_function && module_inst->e->free_function) {
  2765. if (!execute_malloc_function(
  2766. module_inst, exec_env, module_inst->e->malloc_function,
  2767. module_inst->e->retain_function, size, &offset)) {
  2768. return 0;
  2769. }
  2770. /* If we use app's malloc function,
  2771. the default memory may be changed while memory growing */
  2772. memory = wasm_get_default_memory(module_inst);
  2773. addr = offset ? memory->memory_data + offset : NULL;
  2774. }
  2775. if (!addr) {
  2776. if (memory->heap_handle
  2777. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2778. wasm_runtime_show_app_heap_corrupted_prompt();
  2779. wasm_set_exception(module_inst, "app heap corrupted");
  2780. }
  2781. else {
  2782. LOG_WARNING("warning: allocate %u bytes memory failed", size);
  2783. }
  2784. return 0;
  2785. }
  2786. if (p_native_addr)
  2787. *p_native_addr = addr;
  2788. return (uint32)(addr - memory->memory_data);
  2789. }
  2790. uint32
  2791. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  2792. WASMExecEnv *exec_env, uint32 ptr, uint32 size,
  2793. void **p_native_addr)
  2794. {
  2795. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2796. uint8 *addr = NULL;
  2797. if (!memory) {
  2798. wasm_set_exception(module_inst, "uninitialized memory");
  2799. return 0;
  2800. }
  2801. if (memory->heap_handle) {
  2802. addr = mem_allocator_realloc(
  2803. memory->heap_handle, ptr ? memory->memory_data + ptr : NULL, size);
  2804. }
  2805. /* Only support realloc in WAMR's app heap */
  2806. (void)exec_env;
  2807. if (!addr) {
  2808. if (memory->heap_handle
  2809. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2810. wasm_set_exception(module_inst, "app heap corrupted");
  2811. }
  2812. else {
  2813. wasm_set_exception(module_inst, "out of memory");
  2814. }
  2815. return 0;
  2816. }
  2817. if (p_native_addr)
  2818. *p_native_addr = addr;
  2819. return (uint32)(addr - memory->memory_data);
  2820. }
  2821. void
  2822. wasm_module_free_internal(WASMModuleInstance *module_inst,
  2823. WASMExecEnv *exec_env, uint32 ptr)
  2824. {
  2825. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2826. if (!memory) {
  2827. return;
  2828. }
  2829. if (ptr) {
  2830. uint8 *addr = memory->memory_data + ptr;
  2831. uint8 *memory_data_end;
  2832. /* memory->memory_data_end may be changed in memory grow */
  2833. SHARED_MEMORY_LOCK(memory);
  2834. memory_data_end = memory->memory_data_end;
  2835. SHARED_MEMORY_UNLOCK(memory);
  2836. if (memory->heap_handle && memory->heap_data <= addr
  2837. && addr < memory->heap_data_end) {
  2838. mem_allocator_free(memory->heap_handle, addr);
  2839. }
  2840. else if (module_inst->e->malloc_function
  2841. && module_inst->e->free_function && memory->memory_data <= addr
  2842. && addr < memory_data_end) {
  2843. execute_free_function(module_inst, exec_env,
  2844. module_inst->e->free_function, ptr);
  2845. }
  2846. }
  2847. }
  2848. uint32
  2849. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  2850. void **p_native_addr)
  2851. {
  2852. return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
  2853. }
  2854. uint32
  2855. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  2856. void **p_native_addr)
  2857. {
  2858. return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
  2859. p_native_addr);
  2860. }
  2861. void
  2862. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  2863. {
  2864. wasm_module_free_internal(module_inst, NULL, ptr);
  2865. }
  2866. uint32
  2867. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  2868. uint32 size)
  2869. {
  2870. char *buffer;
  2871. uint32 buffer_offset =
  2872. wasm_module_malloc(module_inst, size, (void **)&buffer);
  2873. if (buffer_offset != 0) {
  2874. buffer = wasm_runtime_addr_app_to_native(
  2875. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  2876. bh_memcpy_s(buffer, size, src, size);
  2877. }
  2878. return buffer_offset;
  2879. }
  2880. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2881. bool
  2882. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  2883. uint32 inc_size, table_elem_type_t init_val)
  2884. {
  2885. uint32 total_size, i;
  2886. table_elem_type_t *new_table_data_start;
  2887. WASMTableInstance *table_inst;
  2888. if (!inc_size) {
  2889. return true;
  2890. }
  2891. bh_assert(table_idx < module_inst->table_count);
  2892. table_inst = wasm_get_table_inst(module_inst, table_idx);
  2893. if (!table_inst) {
  2894. return false;
  2895. }
  2896. if (inc_size > UINT32_MAX - table_inst->cur_size) {
  2897. return false;
  2898. }
  2899. total_size = table_inst->cur_size + inc_size;
  2900. if (total_size > table_inst->max_size) {
  2901. return false;
  2902. }
  2903. /* fill in */
  2904. new_table_data_start = table_inst->elems + table_inst->cur_size;
  2905. for (i = 0; i < inc_size; ++i) {
  2906. new_table_data_start[i] = init_val;
  2907. }
  2908. table_inst->cur_size = total_size;
  2909. return true;
  2910. }
  2911. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2912. static bool
  2913. call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 tbl_elem_idx,
  2914. uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
  2915. {
  2916. WASMModuleInstance *module_inst = NULL;
  2917. WASMTableInstance *table_inst = NULL;
  2918. table_elem_type_t tbl_elem_val = NULL_REF;
  2919. uint32 func_idx = 0;
  2920. WASMFunctionInstance *func_inst = NULL;
  2921. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2922. bh_assert(module_inst);
  2923. table_inst = module_inst->tables[tbl_idx];
  2924. if (!table_inst) {
  2925. wasm_set_exception(module_inst, "unknown table");
  2926. goto got_exception;
  2927. }
  2928. if (tbl_elem_idx >= table_inst->cur_size) {
  2929. wasm_set_exception(module_inst, "undefined element");
  2930. goto got_exception;
  2931. }
  2932. tbl_elem_val = ((table_elem_type_t *)table_inst->elems)[tbl_elem_idx];
  2933. if (tbl_elem_val == NULL_REF) {
  2934. wasm_set_exception(module_inst, "uninitialized element");
  2935. goto got_exception;
  2936. }
  2937. #if WASM_ENABLE_GC == 0
  2938. func_idx = tbl_elem_val;
  2939. #else
  2940. func_idx =
  2941. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  2942. #endif
  2943. /**
  2944. * we insist to call functions owned by the module itself
  2945. **/
  2946. if (func_idx >= module_inst->e->function_count) {
  2947. wasm_set_exception(module_inst, "unknown function");
  2948. goto got_exception;
  2949. }
  2950. func_inst = module_inst->e->functions + func_idx;
  2951. if (check_type_idx) {
  2952. WASMType *cur_type = module_inst->module->types[type_idx];
  2953. WASMType *cur_func_type;
  2954. if (func_inst->is_import_func)
  2955. cur_func_type = (WASMType *)func_inst->u.func_import->func_type;
  2956. else
  2957. cur_func_type = (WASMType *)func_inst->u.func->func_type;
  2958. if (cur_type != cur_func_type) {
  2959. wasm_set_exception(module_inst, "indirect call type mismatch");
  2960. goto got_exception;
  2961. }
  2962. }
  2963. interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
  2964. return !wasm_copy_exception(module_inst, NULL);
  2965. got_exception:
  2966. return false;
  2967. }
  2968. bool
  2969. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2970. uint32 argc, uint32 argv[])
  2971. {
  2972. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2973. }
  2974. #if WASM_ENABLE_THREAD_MGR != 0
  2975. bool
  2976. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  2977. {
  2978. WASMModuleInstance *module_inst =
  2979. (WASMModuleInstance *)exec_env->module_inst;
  2980. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2981. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  2982. /* Check the aux stack space */
  2983. uint32 data_end = module_inst->module->aux_data_end;
  2984. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2985. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2986. if ((is_stack_before_data && (size > start_offset))
  2987. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2988. return false;
  2989. #endif
  2990. if (stack_top_idx != (uint32)-1) {
  2991. /* The aux stack top is a wasm global,
  2992. set the initial value for the global */
  2993. uint8 *global_addr =
  2994. module_inst->global_data
  2995. + module_inst->e->globals[stack_top_idx].data_offset;
  2996. *(int32 *)global_addr = start_offset;
  2997. /* The aux stack boundary is a constant value,
  2998. set the value to exec_env */
  2999. exec_env->aux_stack_boundary.boundary = start_offset - size;
  3000. exec_env->aux_stack_bottom.bottom = start_offset;
  3001. return true;
  3002. }
  3003. return false;
  3004. }
  3005. bool
  3006. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  3007. {
  3008. WASMModuleInstance *module_inst =
  3009. (WASMModuleInstance *)exec_env->module_inst;
  3010. /* The aux stack information is resolved in loader
  3011. and store in module */
  3012. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  3013. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  3014. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  3015. if (start_offset)
  3016. *start_offset = stack_bottom;
  3017. if (size)
  3018. *size = total_aux_stack_size;
  3019. return true;
  3020. }
  3021. return false;
  3022. }
  3023. #endif
  3024. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  3025. void
  3026. wasm_get_module_mem_consumption(const WASMModule *module,
  3027. WASMModuleMemConsumption *mem_conspn)
  3028. {
  3029. uint32 i, size;
  3030. memset(mem_conspn, 0, sizeof(*mem_conspn));
  3031. mem_conspn->module_struct_size = sizeof(WASMModule);
  3032. mem_conspn->types_size = sizeof(WASMFuncType *) * module->type_count;
  3033. for (i = 0; i < module->type_count; i++) {
  3034. WASMFuncType *type = module->types[i];
  3035. size = offsetof(WASMFuncType, types)
  3036. + sizeof(uint8) * (type->param_count + type->result_count);
  3037. mem_conspn->types_size += size;
  3038. }
  3039. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  3040. mem_conspn->functions_size =
  3041. sizeof(WASMFunction *) * module->function_count;
  3042. for (i = 0; i < module->function_count; i++) {
  3043. WASMFunction *func = module->functions[i];
  3044. WASMFuncType *type = func->func_type;
  3045. size = sizeof(WASMFunction) + func->local_count
  3046. + sizeof(uint16) * (type->param_count + func->local_count);
  3047. #if WASM_ENABLE_FAST_INTERP != 0
  3048. size +=
  3049. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  3050. #endif
  3051. mem_conspn->functions_size += size;
  3052. }
  3053. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  3054. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  3055. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  3056. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  3057. mem_conspn->table_segs_size =
  3058. sizeof(WASMTableSeg) * module->table_seg_count;
  3059. for (i = 0; i < module->table_seg_count; i++) {
  3060. WASMTableSeg *table_seg = &module->table_segments[i];
  3061. mem_conspn->tables_size +=
  3062. sizeof(InitializerExpression *) * table_seg->value_count;
  3063. }
  3064. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  3065. for (i = 0; i < module->data_seg_count; i++) {
  3066. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  3067. }
  3068. if (module->const_str_list) {
  3069. StringNode *node = module->const_str_list, *node_next;
  3070. while (node) {
  3071. node_next = node->next;
  3072. mem_conspn->const_strs_size +=
  3073. sizeof(StringNode) + strlen(node->str) + 1;
  3074. node = node_next;
  3075. }
  3076. }
  3077. mem_conspn->total_size += mem_conspn->module_struct_size;
  3078. mem_conspn->total_size += mem_conspn->types_size;
  3079. mem_conspn->total_size += mem_conspn->imports_size;
  3080. mem_conspn->total_size += mem_conspn->functions_size;
  3081. mem_conspn->total_size += mem_conspn->tables_size;
  3082. mem_conspn->total_size += mem_conspn->memories_size;
  3083. mem_conspn->total_size += mem_conspn->globals_size;
  3084. mem_conspn->total_size += mem_conspn->exports_size;
  3085. mem_conspn->total_size += mem_conspn->table_segs_size;
  3086. mem_conspn->total_size += mem_conspn->data_segs_size;
  3087. mem_conspn->total_size += mem_conspn->const_strs_size;
  3088. }
  3089. void
  3090. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  3091. WASMModuleInstMemConsumption *mem_conspn)
  3092. {
  3093. uint32 i, size;
  3094. memset(mem_conspn, 0, sizeof(*mem_conspn));
  3095. mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
  3096. - (uint8 *)module_inst
  3097. + sizeof(WASMModuleInstanceExtra);
  3098. mem_conspn->memories_size =
  3099. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  3100. for (i = 0; i < module_inst->memory_count; i++) {
  3101. WASMMemoryInstance *memory = module_inst->memories[i];
  3102. size = memory->num_bytes_per_page * memory->cur_page_count;
  3103. mem_conspn->memories_size += size;
  3104. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  3105. /* size of app heap structure */
  3106. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  3107. /* Module instance structures have been appened into the end of
  3108. module instance */
  3109. }
  3110. mem_conspn->tables_size =
  3111. sizeof(WASMTableInstance *) * module_inst->table_count;
  3112. /* Table instance structures and table elements have been appened into
  3113. the end of module instance */
  3114. mem_conspn->functions_size =
  3115. sizeof(WASMFunctionInstance) * module_inst->e->function_count;
  3116. mem_conspn->globals_size =
  3117. sizeof(WASMGlobalInstance) * module_inst->e->global_count;
  3118. /* Global data has been appened into the end of module instance */
  3119. mem_conspn->exports_size =
  3120. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  3121. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  3122. mem_conspn->total_size += mem_conspn->memories_size;
  3123. mem_conspn->total_size += mem_conspn->functions_size;
  3124. mem_conspn->total_size += mem_conspn->tables_size;
  3125. mem_conspn->total_size += mem_conspn->globals_size;
  3126. mem_conspn->total_size += mem_conspn->exports_size;
  3127. }
  3128. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  3129. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  3130. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3131. bool
  3132. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
  3133. {
  3134. WASMModuleInstance *module_inst =
  3135. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  3136. WASMModule *module = module_inst->module;
  3137. WASMInterpFrame *first_frame,
  3138. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  3139. uint32 n = 0;
  3140. /* count frames includes a function */
  3141. first_frame = cur_frame;
  3142. while (cur_frame) {
  3143. if (cur_frame->function) {
  3144. n++;
  3145. }
  3146. cur_frame = cur_frame->prev_frame;
  3147. }
  3148. /* release previous stack frames and create new ones */
  3149. destroy_c_api_frames(module_inst->frames);
  3150. if (!bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame), false)) {
  3151. return false;
  3152. }
  3153. cur_frame = first_frame;
  3154. n = 0;
  3155. while (cur_frame) {
  3156. WASMCApiFrame frame = { 0 };
  3157. WASMFunctionInstance *func_inst = cur_frame->function;
  3158. const char *func_name = NULL;
  3159. const uint8 *func_code_base = NULL;
  3160. uint32 max_local_cell_num, max_stack_cell_num;
  3161. uint32 all_cell_num, lp_size;
  3162. if (!func_inst) {
  3163. cur_frame = cur_frame->prev_frame;
  3164. continue;
  3165. }
  3166. /* place holder, will overwrite it in wasm_c_api */
  3167. frame.instance = module_inst;
  3168. frame.module_offset = 0;
  3169. frame.func_index = (uint32)(func_inst - module_inst->e->functions);
  3170. func_code_base = wasm_get_func_code(func_inst);
  3171. if (!cur_frame->ip || !func_code_base) {
  3172. frame.func_offset = 0;
  3173. }
  3174. else {
  3175. #if WASM_ENABLE_FAST_INTERP == 0
  3176. frame.func_offset = (uint32)(cur_frame->ip - module->load_addr);
  3177. #else
  3178. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  3179. #endif
  3180. }
  3181. /* look for the function name */
  3182. if (func_inst->is_import_func) {
  3183. func_name = func_inst->u.func_import->field_name;
  3184. }
  3185. else {
  3186. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3187. func_name = func_inst->u.func->field_name;
  3188. #endif
  3189. /* if custom name section is not generated,
  3190. search symbols from export table */
  3191. if (!func_name) {
  3192. uint32 i;
  3193. for (i = 0; i < module_inst->export_func_count; i++) {
  3194. WASMExportFuncInstance *export_func =
  3195. module_inst->export_functions + i;
  3196. if (export_func->function == func_inst) {
  3197. func_name = export_func->name;
  3198. break;
  3199. }
  3200. }
  3201. }
  3202. }
  3203. frame.func_name_wp = func_name;
  3204. if (frame.func_index >= module->import_function_count) {
  3205. uint32 wasm_func_idx =
  3206. frame.func_index - module->import_function_count;
  3207. max_local_cell_num =
  3208. module->functions[wasm_func_idx]->param_cell_num
  3209. + module->functions[wasm_func_idx]->local_cell_num;
  3210. max_stack_cell_num =
  3211. module->functions[wasm_func_idx]->max_stack_cell_num;
  3212. all_cell_num = max_local_cell_num + max_stack_cell_num;
  3213. #if WASM_ENABLE_FAST_INTERP != 0
  3214. all_cell_num += module->functions[wasm_func_idx]->const_cell_num;
  3215. #endif
  3216. }
  3217. else {
  3218. WASMFuncType *func_type =
  3219. module->import_functions[frame.func_index].u.function.func_type;
  3220. max_local_cell_num =
  3221. func_type->param_cell_num > 2 ? func_type->param_cell_num : 2;
  3222. max_stack_cell_num = 0;
  3223. all_cell_num = max_local_cell_num + max_stack_cell_num;
  3224. }
  3225. #if WASM_ENABLE_GC == 0
  3226. lp_size = all_cell_num * 4;
  3227. #else
  3228. lp_size = align_uint(all_cell_num * 5, 4);
  3229. #endif
  3230. if (lp_size > 0) {
  3231. if (!(frame.lp = wasm_runtime_malloc(lp_size))) {
  3232. destroy_c_api_frames(module_inst->frames);
  3233. return false;
  3234. }
  3235. bh_memcpy_s(frame.lp, lp_size, cur_frame->lp, lp_size);
  3236. #if WASM_ENABLE_GC != 0
  3237. #if WASM_ENABLE_FAST_INTERP == 0
  3238. frame.sp = frame.lp + (cur_frame->sp - cur_frame->lp);
  3239. #else
  3240. /* for fast-interp, let frame sp point to the end of the frame */
  3241. frame.sp = frame.lp + all_cell_num;
  3242. #endif
  3243. frame.frame_ref = (uint8 *)frame.lp
  3244. + (wasm_interp_get_frame_ref(cur_frame)
  3245. - (uint8 *)cur_frame->lp);
  3246. #endif
  3247. }
  3248. if (!bh_vector_append(module_inst->frames, &frame)) {
  3249. if (frame.lp)
  3250. wasm_runtime_free(frame.lp);
  3251. destroy_c_api_frames(module_inst->frames);
  3252. return false;
  3253. }
  3254. cur_frame = cur_frame->prev_frame;
  3255. n++;
  3256. }
  3257. return true;
  3258. }
  3259. #define PRINT_OR_DUMP() \
  3260. do { \
  3261. total_len += \
  3262. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  3263. if ((!print) && buf && (len == 0)) { \
  3264. exception_unlock(module_inst); \
  3265. return total_len; \
  3266. } \
  3267. } while (0)
  3268. uint32
  3269. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  3270. uint32 len)
  3271. {
  3272. WASMModuleInstance *module_inst =
  3273. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  3274. uint32 n = 0, total_len = 0, total_frames;
  3275. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  3276. * will be truncated */
  3277. char line_buf[256];
  3278. if (!module_inst->frames) {
  3279. return 0;
  3280. }
  3281. total_frames = (uint32)bh_vector_size(module_inst->frames);
  3282. if (total_frames == 0) {
  3283. return 0;
  3284. }
  3285. exception_lock(module_inst);
  3286. snprintf(line_buf, sizeof(line_buf), "\n");
  3287. PRINT_OR_DUMP();
  3288. while (n < total_frames) {
  3289. WASMCApiFrame frame = { 0 };
  3290. uint32 line_length, i;
  3291. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  3292. exception_unlock(module_inst);
  3293. return 0;
  3294. }
  3295. #if WASM_ENABLE_FAST_JIT != 0
  3296. /* Fast JIT doesn't support committing ip (instruction pointer) yet */
  3297. if (module_inst->e->running_mode == Mode_Fast_JIT
  3298. || module_inst->e->running_mode == Mode_Multi_Tier_JIT) {
  3299. /* function name not exported, print number instead */
  3300. if (frame.func_name_wp == NULL) {
  3301. line_length = snprintf(line_buf, sizeof(line_buf),
  3302. "#%02" PRIu32 " $f%" PRIu32 "\n", n,
  3303. frame.func_index);
  3304. }
  3305. else {
  3306. line_length =
  3307. snprintf(line_buf, sizeof(line_buf), "#%02" PRIu32 " %s\n",
  3308. n, frame.func_name_wp);
  3309. }
  3310. }
  3311. else
  3312. #endif
  3313. {
  3314. /* function name not exported, print number instead */
  3315. if (frame.func_name_wp == NULL) {
  3316. line_length =
  3317. snprintf(line_buf, sizeof(line_buf),
  3318. "#%02" PRIu32 ": 0x%04x - $f%" PRIu32 "\n", n,
  3319. frame.func_offset, frame.func_index);
  3320. }
  3321. else {
  3322. line_length = snprintf(line_buf, sizeof(line_buf),
  3323. "#%02" PRIu32 ": 0x%04x - %s\n", n,
  3324. frame.func_offset, frame.func_name_wp);
  3325. }
  3326. }
  3327. if (line_length >= sizeof(line_buf)) {
  3328. uint32 line_buffer_len = sizeof(line_buf);
  3329. /* If line too long, ensure the last character is '\n' */
  3330. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  3331. line_buf[i] = '.';
  3332. }
  3333. line_buf[line_buffer_len - 2] = '\n';
  3334. }
  3335. PRINT_OR_DUMP();
  3336. n++;
  3337. }
  3338. snprintf(line_buf, sizeof(line_buf), "\n");
  3339. PRINT_OR_DUMP();
  3340. exception_unlock(module_inst);
  3341. return total_len + 1;
  3342. }
  3343. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  3344. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  3345. || WASM_ENABLE_WAMR_COMPILER != 0
  3346. void
  3347. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  3348. {
  3349. if (id != EXCE_ALREADY_THROWN)
  3350. wasm_set_exception_with_id(module_inst, id);
  3351. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3352. wasm_runtime_access_exce_check_guard_page();
  3353. #endif
  3354. }
  3355. bool
  3356. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  3357. uint32 app_buf_addr, uint32 app_buf_size,
  3358. void **p_native_addr)
  3359. {
  3360. bool ret = wasm_check_app_addr_and_convert(
  3361. module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
  3362. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3363. if (!ret)
  3364. wasm_runtime_access_exce_check_guard_page();
  3365. #endif
  3366. return ret;
  3367. }
  3368. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  3369. || WASM_ENABLE_WAMR_COMPILER != 0 */
  3370. #if WASM_ENABLE_FAST_JIT != 0
  3371. bool
  3372. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  3373. uint32 type_idx, uint32 argc, uint32 *argv)
  3374. {
  3375. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
  3376. type_idx);
  3377. }
  3378. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  3379. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  3380. bool
  3381. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  3382. uint32 argc, uint32 *argv)
  3383. {
  3384. bool ret;
  3385. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  3386. ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  3387. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3388. if (!ret)
  3389. wasm_runtime_access_exce_check_guard_page();
  3390. #endif
  3391. return ret;
  3392. }
  3393. bool
  3394. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  3395. uint32 *argv)
  3396. {
  3397. WASMModuleInstance *module_inst;
  3398. WASMModule *module;
  3399. uint32 *func_type_indexes;
  3400. uint32 func_type_idx;
  3401. WASMFuncType *func_type;
  3402. void *func_ptr;
  3403. WASMFunctionImport *import_func;
  3404. CApiFuncImport *c_api_func_import = NULL;
  3405. const char *signature;
  3406. void *attachment;
  3407. char buf[96];
  3408. bool ret = false;
  3409. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  3410. module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  3411. module = module_inst->module;
  3412. func_type_indexes = module_inst->func_type_indexes;
  3413. func_type_idx = func_type_indexes[func_idx];
  3414. func_type = (WASMFuncType *)module->types[func_type_idx];
  3415. func_ptr = module_inst->func_ptrs[func_idx];
  3416. bh_assert(func_idx < module->import_function_count);
  3417. import_func = &module->import_functions[func_idx].u.function;
  3418. if (import_func->call_conv_wasm_c_api) {
  3419. if (module_inst->e->common.c_api_func_imports) {
  3420. c_api_func_import =
  3421. module_inst->e->common.c_api_func_imports + func_idx;
  3422. func_ptr = c_api_func_import->func_ptr_linked;
  3423. }
  3424. else {
  3425. c_api_func_import = NULL;
  3426. func_ptr = NULL;
  3427. }
  3428. }
  3429. if (!func_ptr) {
  3430. snprintf(buf, sizeof(buf),
  3431. "failed to call unlinked import function (%s, %s)",
  3432. import_func->module_name, import_func->field_name);
  3433. wasm_set_exception(module_inst, buf);
  3434. goto fail;
  3435. }
  3436. attachment = import_func->attachment;
  3437. if (import_func->call_conv_wasm_c_api) {
  3438. ret = wasm_runtime_invoke_c_api_native(
  3439. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  3440. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  3441. }
  3442. else if (!import_func->call_conv_raw) {
  3443. signature = import_func->signature;
  3444. ret =
  3445. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  3446. attachment, argv, argc, argv);
  3447. }
  3448. else {
  3449. signature = import_func->signature;
  3450. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  3451. signature, attachment, argv, argc,
  3452. argv);
  3453. }
  3454. fail:
  3455. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3456. if (!ret)
  3457. wasm_runtime_access_exce_check_guard_page();
  3458. #endif
  3459. return ret;
  3460. }
  3461. #if WASM_ENABLE_BULK_MEMORY != 0
  3462. bool
  3463. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  3464. uint32 offset, uint32 len, uint32 dst)
  3465. {
  3466. WASMMemoryInstance *memory_inst;
  3467. WASMModule *module;
  3468. uint8 *data;
  3469. uint8 *maddr;
  3470. uint64 seg_len;
  3471. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3472. memory_inst = wasm_get_default_memory(module_inst);
  3473. if (bh_bitmap_get_bit(module_inst->e->common.data_dropped, seg_index)) {
  3474. seg_len = 0;
  3475. data = NULL;
  3476. }
  3477. else {
  3478. module = module_inst->module;
  3479. seg_len = module->data_segments[seg_index]->data_length;
  3480. data = module->data_segments[seg_index]->data;
  3481. }
  3482. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  3483. dst, len))
  3484. return false;
  3485. if ((uint64)offset + (uint64)len > seg_len) {
  3486. wasm_set_exception(module_inst, "out of bounds memory access");
  3487. return false;
  3488. }
  3489. maddr = wasm_runtime_addr_app_to_native(
  3490. (WASMModuleInstanceCommon *)module_inst, dst);
  3491. SHARED_MEMORY_LOCK(memory_inst);
  3492. bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len);
  3493. SHARED_MEMORY_UNLOCK(memory_inst);
  3494. return true;
  3495. }
  3496. bool
  3497. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
  3498. {
  3499. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3500. bh_bitmap_set_bit(module_inst->e->common.data_dropped, seg_index);
  3501. /* Currently we can't free the dropped data segment
  3502. as they are stored in wasm bytecode */
  3503. return true;
  3504. }
  3505. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
  3506. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3507. void
  3508. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
  3509. {
  3510. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3511. bh_bitmap_set_bit(module_inst->e->common.elem_dropped, tbl_seg_idx);
  3512. }
  3513. void
  3514. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  3515. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  3516. uint32 dst_offset)
  3517. {
  3518. WASMTableInstance *tbl_inst;
  3519. WASMTableSeg *tbl_seg;
  3520. table_elem_type_t *table_elems;
  3521. InitializerExpression *init_values;
  3522. uint32 i;
  3523. #if WASM_ENABLE_GC != 0
  3524. void *func_obj;
  3525. #endif
  3526. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3527. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  3528. tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
  3529. bh_assert(tbl_inst);
  3530. bh_assert(tbl_seg);
  3531. if (offset_len_out_of_bounds(src_offset, length, tbl_seg->value_count)
  3532. || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) {
  3533. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3534. return;
  3535. }
  3536. if (!length) {
  3537. return;
  3538. }
  3539. if (bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) {
  3540. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3541. return;
  3542. }
  3543. if (!wasm_elem_is_passive(tbl_seg->mode)) {
  3544. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3545. return;
  3546. }
  3547. table_elems = tbl_inst->elems + dst_offset;
  3548. init_values = tbl_seg->init_values + src_offset;
  3549. for (i = 0; i < length; i++) {
  3550. #if WASM_ENABLE_GC != 0
  3551. /* UINT32_MAX indicates that it is a null ref */
  3552. if (init_values[i].u.ref_index != UINT32_MAX) {
  3553. if (!(func_obj = wasm_create_func_obj(module_inst,
  3554. init_values[i].u.ref_index,
  3555. true, NULL, 0))) {
  3556. wasm_set_exception(module_inst, "null function object");
  3557. return;
  3558. }
  3559. table_elems[i] = func_obj;
  3560. }
  3561. else {
  3562. table_elems[i] = NULL_REF;
  3563. }
  3564. #else
  3565. table_elems[i] = init_values[i].u.ref_index;
  3566. #endif
  3567. }
  3568. }
  3569. void
  3570. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  3571. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  3572. uint32 dst_offset)
  3573. {
  3574. WASMTableInstance *src_tbl_inst;
  3575. WASMTableInstance *dst_tbl_inst;
  3576. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3577. src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
  3578. dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
  3579. bh_assert(src_tbl_inst);
  3580. bh_assert(dst_tbl_inst);
  3581. if (offset_len_out_of_bounds(dst_offset, length, dst_tbl_inst->cur_size)
  3582. || offset_len_out_of_bounds(src_offset, length,
  3583. src_tbl_inst->cur_size)) {
  3584. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3585. return;
  3586. }
  3587. /* if src_offset >= dst_offset, copy from front to back */
  3588. /* if src_offset < dst_offset, copy from back to front */
  3589. /* merge all together */
  3590. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
  3591. + sizeof(table_elem_type_t) * dst_offset,
  3592. (uint32)sizeof(table_elem_type_t)
  3593. * (dst_tbl_inst->cur_size - dst_offset),
  3594. (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
  3595. + sizeof(table_elem_type_t) * src_offset,
  3596. (uint32)sizeof(table_elem_type_t) * length);
  3597. }
  3598. void
  3599. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  3600. uint32 length, uintptr_t val, uint32 data_offset)
  3601. {
  3602. WASMTableInstance *tbl_inst;
  3603. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3604. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  3605. bh_assert(tbl_inst);
  3606. if (offset_len_out_of_bounds(data_offset, length, tbl_inst->cur_size)) {
  3607. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3608. return;
  3609. }
  3610. for (; length != 0; data_offset++, length--) {
  3611. tbl_inst->elems[data_offset] = (table_elem_type_t)val;
  3612. }
  3613. }
  3614. uint32
  3615. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  3616. uint32 inc_size, uintptr_t init_val)
  3617. {
  3618. WASMTableInstance *tbl_inst;
  3619. uint32 i, orig_size, total_size;
  3620. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3621. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  3622. if (!tbl_inst) {
  3623. return (uint32)-1;
  3624. }
  3625. orig_size = tbl_inst->cur_size;
  3626. if (!inc_size) {
  3627. return orig_size;
  3628. }
  3629. if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
  3630. return (uint32)-1;
  3631. }
  3632. total_size = tbl_inst->cur_size + inc_size;
  3633. if (total_size > tbl_inst->max_size) {
  3634. return (uint32)-1;
  3635. }
  3636. /* fill in */
  3637. for (i = 0; i < inc_size; ++i) {
  3638. tbl_inst->elems[tbl_inst->cur_size + i] = (table_elem_type_t)init_val;
  3639. }
  3640. tbl_inst->cur_size = total_size;
  3641. return orig_size;
  3642. }
  3643. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3644. #if WASM_ENABLE_GC != 0
  3645. void *
  3646. llvm_jit_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
  3647. bool throw_exce, char *error_buf,
  3648. uint32 error_buf_size)
  3649. {
  3650. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3651. return wasm_create_func_obj(module_inst, func_idx, throw_exce, error_buf,
  3652. error_buf_size);
  3653. }
  3654. bool
  3655. llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
  3656. WASMObjectRef gc_obj, uint32 type_index)
  3657. {
  3658. WASMModule *module = module_inst->module;
  3659. WASMType **types = module->types;
  3660. uint32 type_count = module->type_count;
  3661. return wasm_obj_is_instance_of(gc_obj, type_index, types, type_count);
  3662. }
  3663. WASMRttTypeRef
  3664. llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index)
  3665. {
  3666. WASMModule *module = module_inst->module;
  3667. WASMType *defined_type = module->types[type_index];
  3668. WASMRttType **rtt_types = module->rtt_types;
  3669. uint32 rtt_type_count = module->type_count;
  3670. korp_mutex *rtt_type_lock = &module->rtt_type_lock;
  3671. return wasm_rtt_type_new(defined_type, type_index, rtt_types,
  3672. rtt_type_count, rtt_type_lock);
  3673. }
  3674. bool
  3675. llvm_array_init_with_data(WASMModuleInstance *module_inst, uint32 seg_index,
  3676. uint32 data_seg_offset, WASMArrayObjectRef array_obj,
  3677. uint32 elem_size, uint32 array_len)
  3678. {
  3679. WASMModule *wasm_module = module_inst->module;
  3680. WASMDataSeg *data_seg;
  3681. uint8 *array_elem_base;
  3682. uint64 total_size;
  3683. data_seg = wasm_module->data_segments[seg_index];
  3684. total_size = (int64)elem_size * array_len;
  3685. if (data_seg_offset >= data_seg->data_length
  3686. || total_size > data_seg->data_length - data_seg_offset) {
  3687. wasm_set_exception(module_inst, "out of bounds memory access");
  3688. return false;
  3689. }
  3690. array_elem_base = (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  3691. bh_memcpy_s(array_elem_base, (uint32)total_size,
  3692. data_seg->data + data_seg_offset, (uint32)total_size);
  3693. return true;
  3694. }
  3695. #endif /* end of WASM_ENABLE_GC != 0 */
  3696. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  3697. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  3698. void
  3699. wasm_propagate_wasi_args(WASMModule *module)
  3700. {
  3701. if (!module->import_count)
  3702. return;
  3703. bh_assert(&module->import_module_list_head);
  3704. WASMRegisteredModule *node =
  3705. bh_list_first_elem(&module->import_module_list_head);
  3706. while (node) {
  3707. WASIArguments *wasi_args_impt_mod =
  3708. &((WASMModule *)(node->module))->wasi_args;
  3709. bh_assert(wasi_args_impt_mod);
  3710. bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
  3711. &module->wasi_args, sizeof(WASIArguments));
  3712. node = bh_list_elem_next(node);
  3713. }
  3714. }
  3715. #endif