wasm_runtime.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262
  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_loader.h"
  7. #include "wasm_interp.h"
  8. #include "bh_common.h"
  9. #include "bh_log.h"
  10. #include "mem_alloc.h"
  11. #include "../common/wasm_runtime_common.h"
  12. #if WASM_ENABLE_SHARED_MEMORY != 0
  13. #include "../common/wasm_shared_memory.h"
  14. #endif
  15. #if WASM_ENABLE_THREAD_MGR != 0
  16. #include "../libraries/thread-mgr/thread_manager.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../aot/aot_runtime.h"
  26. #endif
  27. static void
  28. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  29. {
  30. if (error_buf != NULL) {
  31. snprintf(error_buf, error_buf_size,
  32. "WASM module instantiate failed: %s", string);
  33. }
  34. }
  35. static void
  36. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  37. {
  38. va_list args;
  39. char buf[128];
  40. if (error_buf != NULL) {
  41. va_start(args, format);
  42. vsnprintf(buf, sizeof(buf), format, args);
  43. va_end(args);
  44. snprintf(error_buf, error_buf_size,
  45. "WASM module instantiate failed: %s", buf);
  46. }
  47. }
  48. WASMModule *
  49. wasm_load(uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  50. {
  51. return wasm_loader_load(buf, size,
  52. #if WASM_ENABLE_MULTI_MODULE != 0
  53. true,
  54. #endif
  55. error_buf, error_buf_size);
  56. }
  57. WASMModule *
  58. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  59. uint32 error_buf_size)
  60. {
  61. return wasm_loader_load_from_sections(section_list, error_buf,
  62. error_buf_size);
  63. }
  64. void
  65. wasm_unload(WASMModule *module)
  66. {
  67. wasm_loader_unload(module);
  68. }
  69. static void *
  70. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  71. {
  72. void *mem;
  73. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  74. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  75. return NULL;
  76. }
  77. memset(mem, 0, (uint32)size);
  78. return mem;
  79. }
  80. #if WASM_ENABLE_MULTI_MODULE != 0
  81. static WASMModuleInstance *
  82. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  83. const WASMModule *sub_module)
  84. {
  85. bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
  86. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  87. while (node && sub_module != node->module_inst->module) {
  88. node = bh_list_elem_next(node);
  89. }
  90. return node ? node->module_inst : NULL;
  91. }
  92. #endif
  93. /**
  94. * Destroy memory instances.
  95. */
  96. static void
  97. memories_deinstantiate(WASMModuleInstance *module_inst,
  98. WASMMemoryInstance **memories, uint32 count)
  99. {
  100. uint32 i;
  101. if (memories) {
  102. for (i = 0; i < count; i++) {
  103. if (memories[i]) {
  104. #if WASM_ENABLE_MULTI_MODULE != 0
  105. WASMModule *module = module_inst->module;
  106. if (i < module->import_memory_count
  107. && module->import_memories[i].u.memory.import_module) {
  108. continue;
  109. }
  110. #endif
  111. #if WASM_ENABLE_SHARED_MEMORY != 0
  112. if (memories[i]->is_shared) {
  113. int32 ref_count = shared_memory_dec_reference(
  114. (WASMModuleCommon *)module_inst->module);
  115. bh_assert(ref_count >= 0);
  116. /* if the reference count is not zero,
  117. don't free the memory */
  118. if (ref_count > 0)
  119. continue;
  120. }
  121. #endif
  122. if (memories[i]->heap_handle) {
  123. mem_allocator_destroy(memories[i]->heap_handle);
  124. wasm_runtime_free(memories[i]->heap_handle);
  125. memories[i]->heap_handle = NULL;
  126. }
  127. if (memories[i]->memory_data) {
  128. #ifndef OS_ENABLE_HW_BOUND_CHECK
  129. wasm_runtime_free(memories[i]->memory_data);
  130. #else
  131. #ifdef BH_PLATFORM_WINDOWS
  132. os_mem_decommit(memories[i]->memory_data,
  133. memories[i]->num_bytes_per_page
  134. * memories[i]->cur_page_count);
  135. #endif
  136. os_munmap((uint8 *)memories[i]->memory_data,
  137. 8 * (uint64)BH_GB);
  138. #endif
  139. }
  140. }
  141. }
  142. wasm_runtime_free(memories);
  143. }
  144. (void)module_inst;
  145. }
  146. static WASMMemoryInstance *
  147. memory_instantiate(WASMModuleInstance *module_inst, WASMMemoryInstance *memory,
  148. uint32 num_bytes_per_page, uint32 init_page_count,
  149. uint32 max_page_count, uint32 heap_size, uint32 flags,
  150. char *error_buf, uint32 error_buf_size)
  151. {
  152. WASMModule *module = module_inst->module;
  153. uint64 memory_data_size;
  154. uint32 heap_offset = num_bytes_per_page * init_page_count;
  155. uint32 inc_page_count, aux_heap_base, global_idx;
  156. uint32 bytes_of_last_page, bytes_to_page_end;
  157. uint8 *global_addr;
  158. #ifdef OS_ENABLE_HW_BOUND_CHECK
  159. uint8 *mapped_mem;
  160. uint64 map_size = 8 * (uint64)BH_GB;
  161. uint64 page_size = os_getpagesize();
  162. #endif
  163. #if WASM_ENABLE_SHARED_MEMORY != 0
  164. bool is_shared_memory = flags & 0x02 ? true : false;
  165. /* shared memory */
  166. if (is_shared_memory) {
  167. WASMSharedMemNode *node = wasm_module_get_shared_memory(
  168. (WASMModuleCommon *)module_inst->module);
  169. /* If the memory of this module has been instantiated,
  170. return the memory instance directly */
  171. if (node) {
  172. uint32 ref_count;
  173. ref_count = shared_memory_inc_reference(
  174. (WASMModuleCommon *)module_inst->module);
  175. bh_assert(ref_count > 0);
  176. memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node);
  177. bh_assert(memory);
  178. (void)ref_count;
  179. return memory;
  180. }
  181. }
  182. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  183. if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
  184. && module_inst->module->free_function != (uint32)-1) {
  185. /* Disable app heap, use malloc/free function exported
  186. by wasm app to allocate/free memory instead */
  187. heap_size = 0;
  188. }
  189. if (init_page_count == max_page_count && init_page_count == 1) {
  190. /* If only one page and at most one page, we just append
  191. the app heap to the end of linear memory, enlarge the
  192. num_bytes_per_page, and don't change the page count */
  193. heap_offset = num_bytes_per_page;
  194. num_bytes_per_page += heap_size;
  195. if (num_bytes_per_page < heap_size) {
  196. set_error_buf(error_buf, error_buf_size,
  197. "failed to insert app heap into linear memory, "
  198. "try using `--heap_size=0` option");
  199. return NULL;
  200. }
  201. }
  202. else if (heap_size > 0) {
  203. if (init_page_count == max_page_count && init_page_count == 0) {
  204. /* If the memory data size is always 0, we resize it to
  205. one page for app heap */
  206. num_bytes_per_page = heap_size;
  207. heap_offset = 0;
  208. inc_page_count = 1;
  209. }
  210. else if (module->aux_heap_base_global_index != (uint32)-1
  211. && module->aux_heap_base
  212. < num_bytes_per_page * init_page_count) {
  213. /* Insert app heap before __heap_base */
  214. aux_heap_base = module->aux_heap_base;
  215. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  216. if (bytes_of_last_page == 0)
  217. bytes_of_last_page = num_bytes_per_page;
  218. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  219. inc_page_count =
  220. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  221. / num_bytes_per_page;
  222. heap_offset = aux_heap_base;
  223. aux_heap_base += heap_size;
  224. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  225. if (bytes_of_last_page == 0)
  226. bytes_of_last_page = num_bytes_per_page;
  227. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  228. if (bytes_to_page_end < 1 * BH_KB) {
  229. aux_heap_base += 1 * BH_KB;
  230. inc_page_count++;
  231. }
  232. /* Adjust __heap_base global value */
  233. global_idx = module->aux_heap_base_global_index;
  234. bh_assert(module_inst->e->globals
  235. && global_idx < module_inst->e->global_count);
  236. global_addr = module_inst->global_data
  237. + module_inst->e->globals[global_idx].data_offset;
  238. *(uint32 *)global_addr = aux_heap_base;
  239. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  240. }
  241. else {
  242. /* Insert app heap before new page */
  243. inc_page_count =
  244. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  245. heap_offset = num_bytes_per_page * init_page_count;
  246. heap_size = num_bytes_per_page * inc_page_count;
  247. if (heap_size > 0)
  248. heap_size -= 1 * BH_KB;
  249. }
  250. init_page_count += inc_page_count;
  251. max_page_count += inc_page_count;
  252. if (init_page_count > DEFAULT_MAX_PAGES) {
  253. set_error_buf(error_buf, error_buf_size,
  254. "failed to insert app heap into linear memory, "
  255. "try using `--heap_size=0` option");
  256. return NULL;
  257. }
  258. else if (init_page_count == DEFAULT_MAX_PAGES) {
  259. num_bytes_per_page = UINT32_MAX;
  260. init_page_count = max_page_count = 1;
  261. }
  262. if (max_page_count > DEFAULT_MAX_PAGES)
  263. max_page_count = DEFAULT_MAX_PAGES;
  264. }
  265. LOG_VERBOSE("Memory instantiate:");
  266. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  267. num_bytes_per_page, init_page_count, max_page_count);
  268. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  269. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  270. #if WASM_ENABLE_SHARED_MEMORY != 0
  271. if (is_shared_memory) {
  272. /* Allocate max page for shared memory */
  273. memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  274. }
  275. #endif
  276. bh_assert(memory_data_size <= 4 * (uint64)BH_GB);
  277. bh_assert(memory != NULL);
  278. #ifndef OS_ENABLE_HW_BOUND_CHECK
  279. if (memory_data_size > 0
  280. && !(memory->memory_data =
  281. runtime_malloc(memory_data_size, error_buf, error_buf_size))) {
  282. goto fail1;
  283. }
  284. #else
  285. memory_data_size = (memory_data_size + page_size - 1) & ~(page_size - 1);
  286. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  287. * ea = i + memarg.offset
  288. * both i and memarg.offset are u32 in range 0 to 4G
  289. * so the range of ea is 0 to 8G
  290. */
  291. if (!(memory->memory_data = mapped_mem =
  292. os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
  293. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  294. goto fail1;
  295. }
  296. #ifdef BH_PLATFORM_WINDOWS
  297. if (!os_mem_commit(mapped_mem, memory_data_size,
  298. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  299. set_error_buf(error_buf, error_buf_size, "commit memory failed");
  300. os_munmap(mapped_mem, map_size);
  301. goto fail1;
  302. }
  303. #endif
  304. if (os_mprotect(mapped_mem, memory_data_size,
  305. MMAP_PROT_READ | MMAP_PROT_WRITE)
  306. != 0) {
  307. set_error_buf(error_buf, error_buf_size, "mprotect memory failed");
  308. goto fail2;
  309. }
  310. /* Newly allocated pages are filled with zero by the OS, we don't fill it
  311. * again here */
  312. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  313. if (memory_data_size > UINT32_MAX)
  314. memory_data_size = (uint32)memory_data_size;
  315. memory->module_type = Wasm_Module_Bytecode;
  316. memory->num_bytes_per_page = num_bytes_per_page;
  317. memory->cur_page_count = init_page_count;
  318. memory->max_page_count = max_page_count;
  319. memory->memory_data_size = (uint32)memory_data_size;
  320. memory->heap_data = memory->memory_data + heap_offset;
  321. memory->heap_data_end = memory->heap_data + heap_size;
  322. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  323. /* Initialize heap */
  324. if (heap_size > 0) {
  325. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  326. if (!(memory->heap_handle = runtime_malloc(
  327. (uint64)heap_struct_size, error_buf, error_buf_size))) {
  328. goto fail2;
  329. }
  330. if (!mem_allocator_create_with_struct_and_pool(
  331. memory->heap_handle, heap_struct_size, memory->heap_data,
  332. heap_size)) {
  333. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  334. goto fail3;
  335. }
  336. }
  337. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  338. if (memory_data_size > 0) {
  339. #if UINTPTR_MAX == UINT64_MAX
  340. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  341. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  342. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  343. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  344. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  345. #else
  346. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  347. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  348. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  349. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  350. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  351. #endif
  352. }
  353. #endif
  354. #if WASM_ENABLE_SHARED_MEMORY != 0
  355. if (is_shared_memory) {
  356. memory->is_shared = true;
  357. if (!shared_memory_set_memory_inst(
  358. (WASMModuleCommon *)module_inst->module,
  359. (WASMMemoryInstanceCommon *)memory)) {
  360. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  361. goto fail4;
  362. }
  363. }
  364. #endif
  365. LOG_VERBOSE("Memory instantiate success.");
  366. return memory;
  367. #if WASM_ENABLE_SHARED_MEMORY != 0
  368. fail4:
  369. if (heap_size > 0)
  370. mem_allocator_destroy(memory->heap_handle);
  371. #endif
  372. fail3:
  373. if (heap_size > 0)
  374. wasm_runtime_free(memory->heap_handle);
  375. fail2:
  376. #ifndef OS_ENABLE_HW_BOUND_CHECK
  377. if (memory->memory_data)
  378. wasm_runtime_free(memory->memory_data);
  379. #else
  380. #ifdef BH_PLATFORM_WINDOWS
  381. os_mem_decommit(mapped_mem, memory_data_size);
  382. #endif
  383. os_munmap(mapped_mem, map_size);
  384. #endif
  385. fail1:
  386. return NULL;
  387. }
  388. /**
  389. * Instantiate memories in a module.
  390. */
  391. static WASMMemoryInstance **
  392. memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  393. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  394. {
  395. WASMImport *import;
  396. uint32 mem_index = 0, i,
  397. memory_count = module->import_memory_count + module->memory_count;
  398. uint64 total_size;
  399. WASMMemoryInstance **memories, *memory;
  400. total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
  401. if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
  402. return NULL;
  403. }
  404. memory = module_inst->global_table_data.memory_instances;
  405. /* instantiate memories from import section */
  406. import = module->import_memories;
  407. for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
  408. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  409. uint32 init_page_count = import->u.memory.init_page_count;
  410. uint32 max_page_count = import->u.memory.max_page_count;
  411. uint32 flags = import->u.memory.flags;
  412. uint32 actual_heap_size = heap_size;
  413. #if WASM_ENABLE_MULTI_MODULE != 0
  414. if (import->u.memory.import_module != NULL) {
  415. WASMModuleInstance *module_inst_linked;
  416. if (!(module_inst_linked = get_sub_module_inst(
  417. module_inst, import->u.memory.import_module))) {
  418. set_error_buf(error_buf, error_buf_size, "unknown memory");
  419. memories_deinstantiate(module_inst, memories, memory_count);
  420. return NULL;
  421. }
  422. if (!(memories[mem_index++] = wasm_lookup_memory(
  423. module_inst_linked, import->u.memory.field_name))) {
  424. set_error_buf(error_buf, error_buf_size, "unknown memory");
  425. memories_deinstantiate(module_inst, memories, memory_count);
  426. return NULL;
  427. }
  428. }
  429. else
  430. #endif
  431. {
  432. if (!(memories[mem_index++] = memory_instantiate(
  433. module_inst, memory, num_bytes_per_page, init_page_count,
  434. max_page_count, actual_heap_size, flags, error_buf,
  435. error_buf_size))) {
  436. memories_deinstantiate(module_inst, memories, memory_count);
  437. return NULL;
  438. }
  439. }
  440. }
  441. /* instantiate memories from memory section */
  442. for (i = 0; i < module->memory_count; i++, memory++) {
  443. if (!(memories[mem_index++] = memory_instantiate(
  444. module_inst, memory, module->memories[i].num_bytes_per_page,
  445. module->memories[i].init_page_count,
  446. module->memories[i].max_page_count, heap_size,
  447. module->memories[i].flags, error_buf, error_buf_size))) {
  448. memories_deinstantiate(module_inst, memories, memory_count);
  449. return NULL;
  450. }
  451. }
  452. bh_assert(mem_index == memory_count);
  453. (void)module_inst;
  454. return memories;
  455. }
  456. /**
  457. * Destroy table instances.
  458. */
  459. static void
  460. tables_deinstantiate(WASMModuleInstance *module_inst)
  461. {
  462. if (module_inst->tables) {
  463. wasm_runtime_free(module_inst->tables);
  464. }
  465. #if WASM_ENABLE_MULTI_MODULE != 0
  466. if (module_inst->e->table_insts_linked) {
  467. wasm_runtime_free(module_inst->e->table_insts_linked);
  468. }
  469. #endif
  470. }
  471. /**
  472. * Instantiate tables in a module.
  473. */
  474. static WASMTableInstance **
  475. tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  476. WASMTableInstance *first_table, char *error_buf,
  477. uint32 error_buf_size)
  478. {
  479. WASMImport *import;
  480. uint32 table_index = 0, i;
  481. uint32 table_count = module->import_table_count + module->table_count;
  482. uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
  483. WASMTableInstance **tables, *table = first_table;
  484. #if WASM_ENABLE_MULTI_MODULE != 0
  485. uint64 total_size_of_tables_linked =
  486. (uint64)sizeof(WASMTableInstance *) * module->import_table_count;
  487. WASMTableInstance **table_linked = NULL;
  488. #endif
  489. if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
  490. return NULL;
  491. }
  492. #if WASM_ENABLE_MULTI_MODULE != 0
  493. if (module->import_table_count > 0
  494. && !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
  495. total_size_of_tables_linked, error_buf, error_buf_size))) {
  496. goto fail;
  497. }
  498. #endif
  499. /* instantiate tables from import section */
  500. import = module->import_tables;
  501. for (i = 0; i < module->import_table_count; i++, import++) {
  502. uint32 max_size_fixed = 0;
  503. #if WASM_ENABLE_MULTI_MODULE != 0
  504. WASMTableInstance *table_inst_linked = NULL;
  505. WASMModuleInstance *module_inst_linked = NULL;
  506. if (import->u.table.import_module) {
  507. if (!(module_inst_linked = get_sub_module_inst(
  508. module_inst, import->u.table.import_module))) {
  509. set_error_buf(error_buf, error_buf_size, "unknown table");
  510. goto fail;
  511. }
  512. if (!(table_inst_linked = wasm_lookup_table(
  513. module_inst_linked, import->u.table.field_name))) {
  514. set_error_buf(error_buf, error_buf_size, "unknown table");
  515. goto fail;
  516. }
  517. total_size = offsetof(WASMTableInstance, elems);
  518. }
  519. else
  520. #endif
  521. {
  522. /* in order to save memory, alloc resource as few as possible */
  523. max_size_fixed = import->u.table.possible_grow
  524. ? import->u.table.max_size
  525. : import->u.table.init_size;
  526. /* it is a built-in table, every module has its own */
  527. total_size = offsetof(WASMTableInstance, elems);
  528. total_size += (uint64)max_size_fixed * sizeof(uint32);
  529. }
  530. tables[table_index++] = table;
  531. /* Set all elements to -1 to mark them as uninitialized elements */
  532. memset(table, -1, (uint32)total_size);
  533. #if WASM_ENABLE_MULTI_MODULE != 0
  534. *table_linked = table_inst_linked;
  535. if (table_inst_linked != NULL) {
  536. table->cur_size = table_inst_linked->cur_size;
  537. table->max_size = table_inst_linked->max_size;
  538. }
  539. else
  540. #endif
  541. {
  542. table->cur_size = import->u.table.init_size;
  543. table->max_size = max_size_fixed;
  544. }
  545. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  546. #if WASM_ENABLE_MULTI_MODULE != 0
  547. table_linked++;
  548. #endif
  549. }
  550. /* instantiate tables from table section */
  551. for (i = 0; i < module->table_count; i++) {
  552. uint32 max_size_fixed = 0;
  553. total_size = offsetof(WASMTableInstance, elems);
  554. #if WASM_ENABLE_MULTI_MODULE != 0
  555. /* in case, a module which imports this table will grow it */
  556. max_size_fixed = module->tables[i].max_size;
  557. #else
  558. max_size_fixed = module->tables[i].possible_grow
  559. ? module->tables[i].max_size
  560. : module->tables[i].init_size;
  561. #endif
  562. total_size += sizeof(uint32) * (uint64)max_size_fixed;
  563. tables[table_index++] = table;
  564. /* Set all elements to -1 to mark them as uninitialized elements */
  565. memset(table, -1, (uint32)total_size);
  566. table->cur_size = module->tables[i].init_size;
  567. table->max_size = max_size_fixed;
  568. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  569. }
  570. bh_assert(table_index == table_count);
  571. (void)module_inst;
  572. return tables;
  573. #if WASM_ENABLE_MULTI_MODULE != 0
  574. fail:
  575. wasm_runtime_free(tables);
  576. return NULL;
  577. #endif
  578. }
  579. /**
  580. * Destroy function instances.
  581. */
  582. static void
  583. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  584. {
  585. if (functions) {
  586. wasm_runtime_free(functions);
  587. }
  588. }
  589. /**
  590. * Instantiate functions in a module.
  591. */
  592. static WASMFunctionInstance *
  593. functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  594. char *error_buf, uint32 error_buf_size)
  595. {
  596. WASMImport *import;
  597. uint32 i,
  598. function_count = module->import_function_count + module->function_count;
  599. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  600. WASMFunctionInstance *functions, *function;
  601. if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
  602. return NULL;
  603. }
  604. total_size = sizeof(void *) * (uint64)module->import_function_count;
  605. if (total_size > 0
  606. && !(module_inst->import_func_ptrs =
  607. runtime_malloc(total_size, error_buf, error_buf_size))) {
  608. wasm_runtime_free(functions);
  609. return NULL;
  610. }
  611. /* instantiate functions from import section */
  612. function = functions;
  613. import = module->import_functions;
  614. for (i = 0; i < module->import_function_count; i++, import++) {
  615. function->is_import_func = true;
  616. #if WASM_ENABLE_MULTI_MODULE != 0
  617. if (import->u.function.import_module) {
  618. function->import_module_inst = get_sub_module_inst(
  619. module_inst, import->u.function.import_module);
  620. if (function->import_module_inst) {
  621. function->import_func_inst =
  622. wasm_lookup_function(function->import_module_inst,
  623. import->u.function.field_name, NULL);
  624. }
  625. }
  626. #endif /* WASM_ENABLE_MULTI_MODULE */
  627. function->u.func_import = &import->u.function;
  628. function->param_cell_num = import->u.function.func_type->param_cell_num;
  629. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  630. function->param_count =
  631. (uint16)function->u.func_import->func_type->param_count;
  632. function->param_types = function->u.func_import->func_type->types;
  633. function->local_cell_num = 0;
  634. function->local_count = 0;
  635. function->local_types = NULL;
  636. /* Copy the function pointer to current instance */
  637. module_inst->import_func_ptrs[i] =
  638. function->u.func_import->func_ptr_linked;
  639. function++;
  640. }
  641. /* instantiate functions from function section */
  642. for (i = 0; i < module->function_count; i++) {
  643. function->is_import_func = false;
  644. function->u.func = module->functions[i];
  645. function->param_cell_num = function->u.func->param_cell_num;
  646. function->ret_cell_num = function->u.func->ret_cell_num;
  647. function->local_cell_num = function->u.func->local_cell_num;
  648. function->param_count =
  649. (uint16)function->u.func->func_type->param_count;
  650. function->local_count = (uint16)function->u.func->local_count;
  651. function->param_types = function->u.func->func_type->types;
  652. function->local_types = function->u.func->local_types;
  653. function->local_offsets = function->u.func->local_offsets;
  654. #if WASM_ENABLE_FAST_INTERP != 0
  655. function->const_cell_num = function->u.func->const_cell_num;
  656. #endif
  657. function++;
  658. }
  659. #if WASM_ENABLE_FAST_JIT != 0
  660. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  661. #endif
  662. bh_assert((uint32)(function - functions) == function_count);
  663. (void)module_inst;
  664. return functions;
  665. }
  666. /**
  667. * Destroy global instances.
  668. */
  669. static void
  670. globals_deinstantiate(WASMGlobalInstance *globals)
  671. {
  672. if (globals)
  673. wasm_runtime_free(globals);
  674. }
  675. static bool
  676. check_global_init_expr(const WASMModule *module, uint32 global_index,
  677. char *error_buf, uint32 error_buf_size)
  678. {
  679. if (global_index >= module->import_global_count + module->global_count) {
  680. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  681. global_index);
  682. return false;
  683. }
  684. /**
  685. * Currently, constant expressions occurring as initializers of
  686. * globals are further constrained in that contained global.get
  687. * instructions are only allowed to refer to imported globals.
  688. *
  689. * And initializer expression cannot reference a mutable global.
  690. */
  691. if (global_index >= module->import_global_count
  692. || (module->import_globals + global_index)->u.global.is_mutable) {
  693. set_error_buf(error_buf, error_buf_size,
  694. "constant expression required");
  695. return false;
  696. }
  697. return true;
  698. }
  699. /**
  700. * Instantiate globals in a module.
  701. */
  702. static WASMGlobalInstance *
  703. globals_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  704. char *error_buf, uint32 error_buf_size)
  705. {
  706. WASMImport *import;
  707. uint32 global_data_offset = 0;
  708. uint32 i, global_count = module->import_global_count + module->global_count;
  709. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  710. WASMGlobalInstance *globals, *global;
  711. if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
  712. return NULL;
  713. }
  714. /* instantiate globals from import section */
  715. global = globals;
  716. import = module->import_globals;
  717. for (i = 0; i < module->import_global_count; i++, import++) {
  718. WASMGlobalImport *global_import = &import->u.global;
  719. global->type = global_import->type;
  720. global->is_mutable = global_import->is_mutable;
  721. #if WASM_ENABLE_MULTI_MODULE != 0
  722. if (global_import->import_module) {
  723. if (!(global->import_module_inst = get_sub_module_inst(
  724. module_inst, global_import->import_module))) {
  725. set_error_buf(error_buf, error_buf_size, "unknown global");
  726. goto fail;
  727. }
  728. if (!(global->import_global_inst = wasm_lookup_global(
  729. global->import_module_inst, global_import->field_name))) {
  730. set_error_buf(error_buf, error_buf_size, "unknown global");
  731. goto fail;
  732. }
  733. /* The linked global instance has been initialized, we
  734. just need to copy the value. */
  735. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  736. &(global_import->import_global_linked->init_expr),
  737. sizeof(WASMValue));
  738. }
  739. else
  740. #endif
  741. {
  742. /* native globals share their initial_values in one module */
  743. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  744. &(global_import->global_data_linked),
  745. sizeof(WASMValue));
  746. }
  747. #if WASM_ENABLE_FAST_JIT != 0
  748. bh_assert(global_data_offset == global_import->data_offset);
  749. #endif
  750. global->data_offset = global_data_offset;
  751. global_data_offset += wasm_value_type_size(global->type);
  752. global++;
  753. }
  754. /* instantiate globals from global section */
  755. for (i = 0; i < module->global_count; i++) {
  756. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  757. global->type = module->globals[i].type;
  758. global->is_mutable = module->globals[i].is_mutable;
  759. #if WASM_ENABLE_FAST_JIT != 0
  760. bh_assert(global_data_offset == module->globals[i].data_offset);
  761. #endif
  762. global->data_offset = global_data_offset;
  763. global_data_offset += wasm_value_type_size(global->type);
  764. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  765. if (!check_global_init_expr(module, init_expr->u.global_index,
  766. error_buf, error_buf_size)) {
  767. goto fail;
  768. }
  769. bh_memcpy_s(
  770. &(global->initial_value), sizeof(WASMValue),
  771. &(globals[init_expr->u.global_index].initial_value),
  772. sizeof(globals[init_expr->u.global_index].initial_value));
  773. }
  774. #if WASM_ENABLE_REF_TYPES != 0
  775. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  776. global->initial_value.u32 = (uint32)NULL_REF;
  777. }
  778. #endif
  779. else {
  780. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  781. &(init_expr->u), sizeof(init_expr->u));
  782. }
  783. global++;
  784. }
  785. bh_assert((uint32)(global - globals) == global_count);
  786. bh_assert(global_data_offset == module->global_data_size);
  787. (void)module_inst;
  788. return globals;
  789. fail:
  790. wasm_runtime_free(globals);
  791. return NULL;
  792. }
  793. /**
  794. * Return export function count in module export section.
  795. */
  796. static uint32
  797. get_export_count(const WASMModule *module, uint8 kind)
  798. {
  799. WASMExport *export = module->exports;
  800. uint32 count = 0, i;
  801. for (i = 0; i < module->export_count; i++, export ++)
  802. if (export->kind == kind)
  803. count++;
  804. return count;
  805. }
  806. /**
  807. * Destroy export function instances.
  808. */
  809. static void
  810. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  811. {
  812. if (functions)
  813. wasm_runtime_free(functions);
  814. }
  815. /**
  816. * Instantiate export functions in a module.
  817. */
  818. static WASMExportFuncInstance *
  819. export_functions_instantiate(const WASMModule *module,
  820. WASMModuleInstance *module_inst,
  821. uint32 export_func_count, char *error_buf,
  822. uint32 error_buf_size)
  823. {
  824. WASMExportFuncInstance *export_funcs, *export_func;
  825. WASMExport *export = module->exports;
  826. uint32 i;
  827. uint64 total_size =
  828. sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  829. if (!(export_func = export_funcs =
  830. runtime_malloc(total_size, error_buf, error_buf_size))) {
  831. return NULL;
  832. }
  833. for (i = 0; i < module->export_count; i++, export ++)
  834. if (export->kind == EXPORT_KIND_FUNC) {
  835. export_func->name = export->name;
  836. export_func->function = &module_inst->e->functions[export->index];
  837. export_func++;
  838. }
  839. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  840. return export_funcs;
  841. }
  842. #if WASM_ENABLE_MULTI_MODULE != 0
  843. static void
  844. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  845. {
  846. if (globals)
  847. wasm_runtime_free(globals);
  848. }
  849. static WASMExportGlobInstance *
  850. export_globals_instantiate(const WASMModule *module,
  851. WASMModuleInstance *module_inst,
  852. uint32 export_glob_count, char *error_buf,
  853. uint32 error_buf_size)
  854. {
  855. WASMExportGlobInstance *export_globals, *export_global;
  856. WASMExport *export = module->exports;
  857. uint32 i;
  858. uint64 total_size =
  859. sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  860. if (!(export_global = export_globals =
  861. runtime_malloc(total_size, error_buf, error_buf_size))) {
  862. return NULL;
  863. }
  864. for (i = 0; i < module->export_count; i++, export ++)
  865. if (export->kind == EXPORT_KIND_GLOBAL) {
  866. export_global->name = export->name;
  867. export_global->global = &module_inst->e->globals[export->index];
  868. export_global++;
  869. }
  870. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  871. return export_globals;
  872. }
  873. #endif
  874. #if WASM_ENABLE_LIBC_WASI != 0
  875. static bool
  876. execute_initialize_function(WASMModuleInstance *module_inst)
  877. {
  878. WASMFunctionInstance *initialize =
  879. wasm_lookup_function(module_inst, "_initialize", NULL);
  880. return !initialize
  881. || wasm_create_exec_env_and_call_function(module_inst, initialize, 0,
  882. NULL);
  883. }
  884. #endif
  885. static bool
  886. execute_post_inst_function(WASMModuleInstance *module_inst)
  887. {
  888. WASMFunctionInstance *post_inst_func = NULL;
  889. WASMType *post_inst_func_type;
  890. uint32 i;
  891. for (i = 0; i < module_inst->export_func_count; i++)
  892. if (!strcmp(module_inst->export_functions[i].name,
  893. "__post_instantiate")) {
  894. post_inst_func = module_inst->export_functions[i].function;
  895. break;
  896. }
  897. if (!post_inst_func)
  898. /* Not found */
  899. return true;
  900. post_inst_func_type = post_inst_func->u.func->func_type;
  901. if (post_inst_func_type->param_count != 0
  902. || post_inst_func_type->result_count != 0)
  903. /* Not a valid function type, ignore it */
  904. return true;
  905. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  906. 0, NULL);
  907. }
  908. #if WASM_ENABLE_BULK_MEMORY != 0
  909. static bool
  910. execute_memory_init_function(WASMModuleInstance *module_inst)
  911. {
  912. WASMFunctionInstance *memory_init_func = NULL;
  913. WASMType *memory_init_func_type;
  914. uint32 i;
  915. for (i = 0; i < module_inst->export_func_count; i++)
  916. if (!strcmp(module_inst->export_functions[i].name,
  917. "__wasm_call_ctors")) {
  918. memory_init_func = module_inst->export_functions[i].function;
  919. break;
  920. }
  921. if (!memory_init_func)
  922. /* Not found */
  923. return true;
  924. memory_init_func_type = memory_init_func->u.func->func_type;
  925. if (memory_init_func_type->param_count != 0
  926. || memory_init_func_type->result_count != 0)
  927. /* Not a valid function type, ignore it */
  928. return true;
  929. return wasm_create_exec_env_and_call_function(module_inst, memory_init_func,
  930. 0, NULL);
  931. }
  932. #endif
  933. static bool
  934. execute_start_function(WASMModuleInstance *module_inst)
  935. {
  936. WASMFunctionInstance *func = module_inst->e->start_function;
  937. if (!func)
  938. return true;
  939. bh_assert(!func->is_import_func && func->param_cell_num == 0
  940. && func->ret_cell_num == 0);
  941. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  942. }
  943. static bool
  944. execute_malloc_function(WASMModuleInstance *module_inst,
  945. WASMFunctionInstance *malloc_func,
  946. WASMFunctionInstance *retain_func, uint32 size,
  947. uint32 *p_result)
  948. {
  949. #ifdef OS_ENABLE_HW_BOUND_CHECK
  950. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  951. #endif
  952. uint32 argv[2], argc;
  953. bool ret;
  954. argv[0] = size;
  955. argc = 1;
  956. /* if __retain is exported, then this module is compiled by
  957. assemblyscript, the memory should be managed by as's runtime,
  958. in this case we need to call the retain function after malloc
  959. the memory */
  960. if (retain_func) {
  961. /* the malloc functino from assemblyscript is:
  962. function __new(size: usize, id: u32)
  963. id = 0 means this is an ArrayBuffer object */
  964. argv[1] = 0;
  965. argc = 2;
  966. }
  967. #ifdef OS_ENABLE_HW_BOUND_CHECK
  968. if (exec_env_tls != NULL) {
  969. bh_assert(exec_env_tls->module_inst
  970. == (WASMModuleInstanceCommon *)module_inst);
  971. ret = wasm_call_function(exec_env_tls, malloc_func, argc, argv);
  972. if (retain_func && ret) {
  973. ret = wasm_call_function(exec_env_tls, retain_func, 1, argv);
  974. }
  975. }
  976. else
  977. #endif
  978. {
  979. ret = wasm_create_exec_env_and_call_function(module_inst, malloc_func,
  980. argc, argv);
  981. if (retain_func && ret) {
  982. ret = wasm_create_exec_env_and_call_function(module_inst,
  983. retain_func, 1, argv);
  984. }
  985. }
  986. if (ret)
  987. *p_result = argv[0];
  988. return ret;
  989. }
  990. static bool
  991. execute_free_function(WASMModuleInstance *module_inst,
  992. WASMFunctionInstance *free_func, uint32 offset)
  993. {
  994. #ifdef OS_ENABLE_HW_BOUND_CHECK
  995. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  996. #endif
  997. uint32 argv[2];
  998. argv[0] = offset;
  999. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1000. if (exec_env_tls != NULL) {
  1001. bh_assert(exec_env_tls->module_inst
  1002. == (WASMModuleInstanceCommon *)module_inst);
  1003. return wasm_call_function(exec_env_tls, free_func, 1, argv);
  1004. }
  1005. else
  1006. #endif
  1007. {
  1008. return wasm_create_exec_env_and_call_function(module_inst, free_func, 1,
  1009. argv);
  1010. }
  1011. }
  1012. #if WASM_ENABLE_MULTI_MODULE != 0
  1013. static bool
  1014. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  1015. uint32 stack_size, uint32 heap_size, char *error_buf,
  1016. uint32 error_buf_size)
  1017. {
  1018. bh_list *sub_module_inst_list = module_inst->e->sub_module_inst_list;
  1019. WASMRegisteredModule *sub_module_list_node =
  1020. bh_list_first_elem(module->import_module_list);
  1021. while (sub_module_list_node) {
  1022. WASMSubModInstNode *sub_module_inst_list_node = NULL;
  1023. WASMModule *sub_module = (WASMModule *)sub_module_list_node->module;
  1024. WASMModuleInstance *sub_module_inst = NULL;
  1025. sub_module_inst =
  1026. wasm_instantiate(sub_module, false, stack_size, heap_size,
  1027. error_buf, error_buf_size);
  1028. if (!sub_module_inst) {
  1029. LOG_DEBUG("instantiate %s failed",
  1030. sub_module_list_node->module_name);
  1031. goto failed;
  1032. }
  1033. sub_module_inst_list_node = runtime_malloc(sizeof(WASMSubModInstNode),
  1034. error_buf, error_buf_size);
  1035. if (!sub_module_inst_list_node) {
  1036. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  1037. sizeof(WASMSubModInstNode));
  1038. goto failed;
  1039. }
  1040. sub_module_inst_list_node->module_inst = sub_module_inst;
  1041. sub_module_inst_list_node->module_name =
  1042. sub_module_list_node->module_name;
  1043. bh_list_status ret =
  1044. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  1045. bh_assert(BH_LIST_SUCCESS == ret);
  1046. (void)ret;
  1047. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  1048. continue;
  1049. failed:
  1050. if (sub_module_inst_list_node) {
  1051. bh_list_remove(sub_module_inst_list, sub_module_inst_list_node);
  1052. wasm_runtime_free(sub_module_inst_list_node);
  1053. }
  1054. if (sub_module_inst)
  1055. wasm_deinstantiate(sub_module_inst, false);
  1056. return false;
  1057. }
  1058. return true;
  1059. }
  1060. static void
  1061. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  1062. {
  1063. bh_list *list = module_inst->e->sub_module_inst_list;
  1064. WASMSubModInstNode *node = bh_list_first_elem(list);
  1065. while (node) {
  1066. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  1067. bh_list_remove(list, node);
  1068. wasm_deinstantiate(node->module_inst, false);
  1069. wasm_runtime_free(node);
  1070. node = next_node;
  1071. }
  1072. }
  1073. #endif
  1074. static bool
  1075. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  1076. uint32 error_buf_size)
  1077. {
  1078. WASMModule *module = module_inst->module;
  1079. uint32 i;
  1080. for (i = 0; i < module->import_function_count; i++) {
  1081. WASMFunctionImport *func =
  1082. &((module->import_functions + i)->u.function);
  1083. if (!func->func_ptr_linked
  1084. #if WASM_ENABLE_MULTI_MODULE != 0
  1085. && !func->import_func_linked
  1086. #endif
  1087. ) {
  1088. #if WASM_ENABLE_WAMR_COMPILER == 0
  1089. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1090. func->module_name, func->field_name);
  1091. /* will throw exception only if calling */
  1092. #else
  1093. /* do nothing to avoid confused message */
  1094. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1095. }
  1096. }
  1097. for (i = 0; i < module->import_global_count; i++) {
  1098. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  1099. if (!global->is_linked) {
  1100. #if WASM_ENABLE_SPEC_TEST != 0
  1101. set_error_buf(error_buf, error_buf_size,
  1102. "unknown import or incompatible import type");
  1103. return false;
  1104. #else
  1105. #if WASM_ENABLE_WAMR_COMPILER == 0
  1106. set_error_buf_v(error_buf, error_buf_size,
  1107. "warning: failed to link import global (%s, %s)",
  1108. global->module_name, global->field_name);
  1109. return false;
  1110. #else
  1111. /* do nothing to avoid confused message */
  1112. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1113. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1114. }
  1115. }
  1116. return true;
  1117. }
  1118. #if WASM_ENABLE_JIT != 0
  1119. static bool
  1120. init_func_ptrs(WASMModuleInstance *module_inst, WASMModule *module,
  1121. char *error_buf, uint32 error_buf_size)
  1122. {
  1123. uint32 i;
  1124. void **func_ptrs;
  1125. uint64 total_size = (uint64)sizeof(void *) * module_inst->e->function_count;
  1126. /* Allocate memory */
  1127. if (!(func_ptrs = module_inst->func_ptrs =
  1128. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1129. return false;
  1130. }
  1131. /* Set import function pointers */
  1132. for (i = 0; i < module->import_function_count; i++, func_ptrs++) {
  1133. WASMFunctionImport *import_func =
  1134. &module->import_functions[i].u.function;
  1135. /* TODO: handle multi module */
  1136. *func_ptrs = import_func->func_ptr_linked;
  1137. }
  1138. /* Set defined function pointers */
  1139. bh_memcpy_s(func_ptrs, sizeof(void *) * module->function_count,
  1140. module->func_ptrs, sizeof(void *) * module->function_count);
  1141. return true;
  1142. }
  1143. #endif /* end of WASM_ENABLE_JIT != 0 */
  1144. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1145. static uint32
  1146. get_smallest_type_idx(WASMModule *module, WASMType *func_type)
  1147. {
  1148. uint32 i;
  1149. for (i = 0; i < module->type_count; i++) {
  1150. if (func_type == module->types[i])
  1151. return i;
  1152. }
  1153. bh_assert(0);
  1154. return -1;
  1155. }
  1156. static bool
  1157. init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
  1158. uint32 error_buf_size)
  1159. {
  1160. uint32 i;
  1161. uint64 total_size = (uint64)sizeof(uint32) * module_inst->e->function_count;
  1162. /* Allocate memory */
  1163. if (!(module_inst->func_type_indexes =
  1164. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1165. return false;
  1166. }
  1167. for (i = 0; i < module_inst->e->function_count; i++) {
  1168. WASMFunctionInstance *func_inst = module_inst->e->functions + i;
  1169. WASMType *func_type = func_inst->is_import_func
  1170. ? func_inst->u.func_import->func_type
  1171. : func_inst->u.func->func_type;
  1172. module_inst->func_type_indexes[i] =
  1173. get_smallest_type_idx(module_inst->module, func_type);
  1174. }
  1175. return true;
  1176. }
  1177. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1178. /**
  1179. * Instantiate module
  1180. */
  1181. WASMModuleInstance *
  1182. wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
  1183. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  1184. {
  1185. WASMModuleInstance *module_inst;
  1186. WASMGlobalInstance *globals = NULL, *global;
  1187. WASMTableInstance *first_table;
  1188. uint32 global_count, i;
  1189. uint32 base_offset, length, extra_info_offset;
  1190. uint32 module_inst_struct_size =
  1191. offsetof(WASMModuleInstance, global_table_data.bytes);
  1192. uint64 module_inst_mem_inst_size;
  1193. uint64 total_size, table_size = 0;
  1194. uint8 *global_data, *global_data_end;
  1195. #if WASM_ENABLE_MULTI_MODULE != 0
  1196. bool ret = false;
  1197. #endif
  1198. if (!module)
  1199. return NULL;
  1200. /* Check the heap size */
  1201. heap_size = align_uint(heap_size, 8);
  1202. if (heap_size > APP_HEAP_SIZE_MAX)
  1203. heap_size = APP_HEAP_SIZE_MAX;
  1204. module_inst_mem_inst_size =
  1205. (uint64)sizeof(WASMMemoryInstance)
  1206. * (module->import_memory_count + module->memory_count);
  1207. #if WASM_ENABLE_JIT != 0
  1208. /* If the module dosen't have memory, reserve one mem_info space
  1209. with empty content to align with llvm jit compiler */
  1210. if (module_inst_mem_inst_size == 0)
  1211. module_inst_mem_inst_size = (uint64)sizeof(WASMMemoryInstance);
  1212. #endif
  1213. /* Size of module inst, memory instances and global data */
  1214. total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
  1215. + module->global_data_size;
  1216. /* Calculate the size of table data */
  1217. for (i = 0; i < module->import_table_count; i++) {
  1218. WASMTableImport *import_table = &module->import_tables[i].u.table;
  1219. table_size += offsetof(WASMTableInstance, elems);
  1220. #if WASM_ENABLE_MULTI_MODULE != 0
  1221. table_size += (uint64)sizeof(uint32) * import_table->max_size;
  1222. #else
  1223. table_size += (uint64)sizeof(uint32)
  1224. * (import_table->possible_grow ? import_table->max_size
  1225. : import_table->init_size);
  1226. #endif
  1227. }
  1228. for (i = 0; i < module->table_count; i++) {
  1229. WASMTable *table = module->tables + i;
  1230. table_size += offsetof(WASMTableInstance, elems);
  1231. #if WASM_ENABLE_MULTI_MODULE != 0
  1232. table_size += (uint64)sizeof(uint32) * table->max_size;
  1233. #else
  1234. table_size +=
  1235. (uint64)sizeof(uint32)
  1236. * (table->possible_grow ? table->max_size : table->init_size);
  1237. #endif
  1238. }
  1239. total_size += table_size;
  1240. /* The offset of WASMModuleInstanceExtra, make it 8-byte aligned */
  1241. total_size = (total_size + 7LL) & ~7LL;
  1242. extra_info_offset = (uint32)total_size;
  1243. total_size += sizeof(WASMModuleInstanceExtra);
  1244. /* Allocate the memory for module instance with memory instances,
  1245. global data, table data appended at the end */
  1246. if (!(module_inst =
  1247. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1248. return NULL;
  1249. }
  1250. module_inst->module_type = Wasm_Module_Bytecode;
  1251. module_inst->module = module;
  1252. module_inst->e =
  1253. (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
  1254. #if WASM_ENABLE_SHARED_MEMORY != 0
  1255. if (os_mutex_init(&module_inst->e->mem_lock) != 0) {
  1256. set_error_buf(error_buf, error_buf_size,
  1257. "create shared memory lock failed");
  1258. goto fail;
  1259. }
  1260. module_inst->e->mem_lock_inited = true;
  1261. #endif
  1262. #if WASM_ENABLE_MULTI_MODULE != 0
  1263. module_inst->e->sub_module_inst_list =
  1264. &module_inst->e->sub_module_inst_list_head;
  1265. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1266. error_buf, error_buf_size);
  1267. if (!ret) {
  1268. LOG_DEBUG("build a sub module list failed");
  1269. goto fail;
  1270. }
  1271. #endif
  1272. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1273. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1274. error_buf, error_buf_size))) {
  1275. goto fail;
  1276. }
  1277. #endif
  1278. /* Instantiate global firstly to get the mutable data size */
  1279. global_count = module->import_global_count + module->global_count;
  1280. if (global_count
  1281. && !(globals = globals_instantiate(module, module_inst, error_buf,
  1282. error_buf_size))) {
  1283. goto fail;
  1284. }
  1285. module_inst->e->global_count = global_count;
  1286. module_inst->e->globals = globals;
  1287. module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
  1288. + module_inst_mem_inst_size;
  1289. module_inst->global_data_size = module->global_data_size;
  1290. first_table = (WASMTableInstance *)(module_inst->global_data
  1291. + module->global_data_size);
  1292. module_inst->memory_count =
  1293. module->import_memory_count + module->memory_count;
  1294. module_inst->table_count = module->import_table_count + module->table_count;
  1295. module_inst->e->function_count =
  1296. module->import_function_count + module->function_count;
  1297. /* export */
  1298. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1299. #if WASM_ENABLE_MULTI_MODULE != 0
  1300. module_inst->export_table_count =
  1301. get_export_count(module, EXPORT_KIND_TABLE);
  1302. module_inst->export_memory_count =
  1303. get_export_count(module, EXPORT_KIND_MEMORY);
  1304. module_inst->export_global_count =
  1305. get_export_count(module, EXPORT_KIND_GLOBAL);
  1306. #endif
  1307. /* Instantiate memories/tables/functions */
  1308. if ((module_inst->memory_count > 0
  1309. && !(module_inst->memories = memories_instantiate(
  1310. module, module_inst, heap_size, error_buf, error_buf_size)))
  1311. || (module_inst->table_count > 0
  1312. && !(module_inst->tables =
  1313. tables_instantiate(module, module_inst, first_table,
  1314. error_buf, error_buf_size)))
  1315. || (module_inst->e->function_count > 0
  1316. && !(module_inst->e->functions = functions_instantiate(
  1317. module, module_inst, error_buf, error_buf_size)))
  1318. || (module_inst->export_func_count > 0
  1319. && !(module_inst->export_functions = export_functions_instantiate(
  1320. module, module_inst, module_inst->export_func_count,
  1321. error_buf, error_buf_size)))
  1322. #if WASM_ENABLE_MULTI_MODULE != 0
  1323. || (module_inst->export_global_count > 0
  1324. && !(module_inst->export_globals = export_globals_instantiate(
  1325. module, module_inst, module_inst->export_global_count,
  1326. error_buf, error_buf_size)))
  1327. #endif
  1328. #if WASM_ENABLE_JIT != 0
  1329. || (module_inst->e->function_count > 0
  1330. && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  1331. #endif
  1332. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1333. || (module_inst->e->function_count > 0
  1334. && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
  1335. #endif
  1336. ) {
  1337. goto fail;
  1338. }
  1339. if (global_count > 0) {
  1340. /* Initialize the global data */
  1341. global_data = module_inst->global_data;
  1342. global_data_end = global_data + module->global_data_size;
  1343. global = globals;
  1344. for (i = 0; i < global_count; i++, global++) {
  1345. switch (global->type) {
  1346. case VALUE_TYPE_I32:
  1347. case VALUE_TYPE_F32:
  1348. #if WASM_ENABLE_REF_TYPES != 0
  1349. case VALUE_TYPE_FUNCREF:
  1350. case VALUE_TYPE_EXTERNREF:
  1351. #endif
  1352. *(int32 *)global_data = global->initial_value.i32;
  1353. global_data += sizeof(int32);
  1354. break;
  1355. case VALUE_TYPE_I64:
  1356. case VALUE_TYPE_F64:
  1357. bh_memcpy_s(global_data,
  1358. (uint32)(global_data_end - global_data),
  1359. &global->initial_value.i64, sizeof(int64));
  1360. global_data += sizeof(int64);
  1361. break;
  1362. #if WASM_ENABLE_SIMD != 0
  1363. case VALUE_TYPE_V128:
  1364. bh_memcpy_s(global_data, (uint32)sizeof(V128),
  1365. &global->initial_value.v128, sizeof(V128));
  1366. global_data += sizeof(V128);
  1367. break;
  1368. #endif
  1369. default:
  1370. bh_assert(0);
  1371. }
  1372. }
  1373. bh_assert(global_data == global_data_end);
  1374. }
  1375. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1376. goto fail;
  1377. }
  1378. /* Initialize the memory data with data segment section */
  1379. for (i = 0; i < module->data_seg_count; i++) {
  1380. WASMMemoryInstance *memory = NULL;
  1381. uint8 *memory_data = NULL;
  1382. uint32 memory_size = 0;
  1383. WASMDataSeg *data_seg = module->data_segments[i];
  1384. #if WASM_ENABLE_BULK_MEMORY != 0
  1385. if (data_seg->is_passive)
  1386. continue;
  1387. #endif
  1388. /* has check it in loader */
  1389. memory = module_inst->memories[data_seg->memory_index];
  1390. bh_assert(memory);
  1391. memory_data = memory->memory_data;
  1392. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1393. bh_assert(memory_data || memory_size == 0);
  1394. bh_assert(data_seg->base_offset.init_expr_type
  1395. == INIT_EXPR_TYPE_I32_CONST
  1396. || data_seg->base_offset.init_expr_type
  1397. == INIT_EXPR_TYPE_GET_GLOBAL);
  1398. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1399. if (!check_global_init_expr(module,
  1400. data_seg->base_offset.u.global_index,
  1401. error_buf, error_buf_size)) {
  1402. goto fail;
  1403. }
  1404. if (!globals
  1405. || globals[data_seg->base_offset.u.global_index].type
  1406. != VALUE_TYPE_I32) {
  1407. set_error_buf(error_buf, error_buf_size,
  1408. "data segment does not fit");
  1409. goto fail;
  1410. }
  1411. base_offset =
  1412. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  1413. }
  1414. else {
  1415. base_offset = (uint32)data_seg->base_offset.u.i32;
  1416. }
  1417. /* check offset */
  1418. if (base_offset > memory_size) {
  1419. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1420. memory_size);
  1421. #if WASM_ENABLE_REF_TYPES != 0
  1422. set_error_buf(error_buf, error_buf_size,
  1423. "out of bounds memory access");
  1424. #else
  1425. set_error_buf(error_buf, error_buf_size,
  1426. "data segment does not fit");
  1427. #endif
  1428. goto fail;
  1429. }
  1430. /* check offset + length(could be zero) */
  1431. length = data_seg->data_length;
  1432. if (base_offset + length > memory_size) {
  1433. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1434. base_offset, length, memory_size);
  1435. #if WASM_ENABLE_REF_TYPES != 0
  1436. set_error_buf(error_buf, error_buf_size,
  1437. "out of bounds memory access");
  1438. #else
  1439. set_error_buf(error_buf, error_buf_size,
  1440. "data segment does not fit");
  1441. #endif
  1442. goto fail;
  1443. }
  1444. if (memory_data) {
  1445. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1446. data_seg->data, length);
  1447. }
  1448. }
  1449. /* Initialize the table data with table segment section */
  1450. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1451. i++) {
  1452. WASMTableSeg *table_seg = module->table_segments + i;
  1453. /* has check it in loader */
  1454. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1455. uint32 *table_data;
  1456. #if WASM_ENABLE_REF_TYPES != 0
  1457. uint8 tbl_elem_type;
  1458. uint32 tbl_init_size, tbl_max_size;
  1459. #endif
  1460. bh_assert(table);
  1461. #if WASM_ENABLE_REF_TYPES != 0
  1462. (void)wasm_runtime_get_table_inst_elem_type(
  1463. (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
  1464. &tbl_elem_type, &tbl_init_size, &tbl_max_size);
  1465. if (tbl_elem_type != VALUE_TYPE_FUNCREF
  1466. && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
  1467. set_error_buf(error_buf, error_buf_size,
  1468. "elements segment does not fit");
  1469. goto fail;
  1470. }
  1471. (void)tbl_init_size;
  1472. (void)tbl_max_size;
  1473. #endif
  1474. table_data = table->elems;
  1475. #if WASM_ENABLE_MULTI_MODULE != 0
  1476. if (table_seg->table_index < module->import_table_count
  1477. && module_inst->e->table_insts_linked[table_seg->table_index]) {
  1478. table_data =
  1479. module_inst->e->table_insts_linked[table_seg->table_index]
  1480. ->elems;
  1481. }
  1482. #endif
  1483. bh_assert(table_data);
  1484. #if WASM_ENABLE_REF_TYPES != 0
  1485. if (!wasm_elem_is_active(table_seg->mode))
  1486. continue;
  1487. #endif
  1488. #if WASM_ENABLE_REF_TYPES != 0
  1489. bh_assert(table_seg->base_offset.init_expr_type
  1490. == INIT_EXPR_TYPE_I32_CONST
  1491. || table_seg->base_offset.init_expr_type
  1492. == INIT_EXPR_TYPE_GET_GLOBAL
  1493. || table_seg->base_offset.init_expr_type
  1494. == INIT_EXPR_TYPE_FUNCREF_CONST
  1495. || table_seg->base_offset.init_expr_type
  1496. == INIT_EXPR_TYPE_REFNULL_CONST);
  1497. #else
  1498. bh_assert(table_seg->base_offset.init_expr_type
  1499. == INIT_EXPR_TYPE_I32_CONST
  1500. || table_seg->base_offset.init_expr_type
  1501. == INIT_EXPR_TYPE_GET_GLOBAL);
  1502. #endif
  1503. /* init vec(funcidx) or vec(expr) */
  1504. if (table_seg->base_offset.init_expr_type
  1505. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1506. if (!check_global_init_expr(module,
  1507. table_seg->base_offset.u.global_index,
  1508. error_buf, error_buf_size)) {
  1509. goto fail;
  1510. }
  1511. if (!globals
  1512. || globals[table_seg->base_offset.u.global_index].type
  1513. != VALUE_TYPE_I32) {
  1514. set_error_buf(error_buf, error_buf_size,
  1515. "elements segment does not fit");
  1516. goto fail;
  1517. }
  1518. table_seg->base_offset.u.i32 =
  1519. globals[table_seg->base_offset.u.global_index]
  1520. .initial_value.i32;
  1521. }
  1522. /* check offset since length might negative */
  1523. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1524. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1525. table_seg->base_offset.u.i32, table->cur_size);
  1526. #if WASM_ENABLE_REF_TYPES != 0
  1527. set_error_buf(error_buf, error_buf_size,
  1528. "out of bounds table access");
  1529. #else
  1530. set_error_buf(error_buf, error_buf_size,
  1531. "elements segment does not fit");
  1532. #endif
  1533. goto fail;
  1534. }
  1535. /* check offset + length(could be zero) */
  1536. length = table_seg->function_count;
  1537. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1538. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1539. table_seg->base_offset.u.i32, length, table->cur_size);
  1540. #if WASM_ENABLE_REF_TYPES != 0
  1541. set_error_buf(error_buf, error_buf_size,
  1542. "out of bounds table access");
  1543. #else
  1544. set_error_buf(error_buf, error_buf_size,
  1545. "elements segment does not fit");
  1546. #endif
  1547. goto fail;
  1548. }
  1549. /**
  1550. * Check function index in the current module inst for now.
  1551. * will check the linked table inst owner in future.
  1552. * so loader check is enough
  1553. */
  1554. bh_memcpy_s(
  1555. table_data + table_seg->base_offset.u.i32,
  1556. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1557. * sizeof(uint32)),
  1558. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1559. }
  1560. /* Initialize the thread related data */
  1561. if (stack_size == 0)
  1562. stack_size = DEFAULT_WASM_STACK_SIZE;
  1563. #if WASM_ENABLE_SPEC_TEST != 0
  1564. if (stack_size < 128 * 1024)
  1565. stack_size = 128 * 1024;
  1566. #endif
  1567. module_inst->default_wasm_stack_size = stack_size;
  1568. if (module->malloc_function != (uint32)-1) {
  1569. module_inst->e->malloc_function =
  1570. &module_inst->e->functions[module->malloc_function];
  1571. }
  1572. if (module->free_function != (uint32)-1) {
  1573. module_inst->e->free_function =
  1574. &module_inst->e->functions[module->free_function];
  1575. }
  1576. if (module->retain_function != (uint32)-1) {
  1577. module_inst->e->retain_function =
  1578. &module_inst->e->functions[module->retain_function];
  1579. }
  1580. #if WASM_ENABLE_LIBC_WASI != 0
  1581. /* The sub-instance will get the wasi_ctx from main-instance */
  1582. if (!is_sub_inst) {
  1583. if (!wasm_runtime_init_wasi(
  1584. (WASMModuleInstanceCommon *)module_inst,
  1585. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1586. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1587. module->wasi_args.env, module->wasi_args.env_count,
  1588. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  1589. module->wasi_args.ns_lookup_pool,
  1590. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  1591. module->wasi_args.argc, module->wasi_args.stdio[0],
  1592. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  1593. error_buf, error_buf_size)) {
  1594. goto fail;
  1595. }
  1596. }
  1597. #endif
  1598. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  1599. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1600. && WASM_ENABLE_LAZY_JIT != 0)
  1601. if (!is_sub_inst) {
  1602. /* Add module instance into module's instance list */
  1603. os_mutex_lock(&module->instance_list_lock);
  1604. #if WASM_ENABLE_DEBUG_INTERP != 0
  1605. if (module->instance_list) {
  1606. LOG_WARNING(
  1607. "warning: multiple instances referencing to the same module "
  1608. "may cause unexpected behaviour during debugging");
  1609. }
  1610. #endif
  1611. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1612. && WASM_ENABLE_LAZY_JIT != 0
  1613. /* Copy llvm func ptrs again in case that they were updated
  1614. after the module instance was created */
  1615. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1616. sizeof(void *) * module->function_count, module->func_ptrs,
  1617. sizeof(void *) * module->function_count);
  1618. #endif
  1619. module_inst->e->next = module->instance_list;
  1620. module->instance_list = module_inst;
  1621. os_mutex_unlock(&module->instance_list_lock);
  1622. }
  1623. #endif
  1624. if (module->start_function != (uint32)-1) {
  1625. /* TODO: fix start function can be import function issue */
  1626. if (module->start_function >= module->import_function_count)
  1627. module_inst->e->start_function =
  1628. &module_inst->e->functions[module->start_function];
  1629. }
  1630. if (
  1631. #if WASM_ENABLE_LIBC_WASI != 0
  1632. /*
  1633. * reactor instances may assume that _initialize will be called by
  1634. * the environment at most once, and that none of their other
  1635. * exports are accessed before that call.
  1636. *
  1637. * let the loader decide how to act if there is no _initialize
  1638. * in a reactor
  1639. */
  1640. !execute_initialize_function(module_inst) ||
  1641. #endif
  1642. /* Execute __post_instantiate function */
  1643. !execute_post_inst_function(module_inst)
  1644. /* Execute the function in "start" section */
  1645. || !execute_start_function(module_inst)) {
  1646. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1647. goto fail;
  1648. }
  1649. #if WASM_ENABLE_BULK_MEMORY != 0
  1650. #if WASM_ENABLE_LIBC_WASI != 0
  1651. if (!module->import_wasi_api) {
  1652. #endif
  1653. /* Only execute the memory init function for main instance because
  1654. the data segments will be dropped once initialized.
  1655. */
  1656. if (!is_sub_inst) {
  1657. if (!execute_memory_init_function(module_inst)) {
  1658. set_error_buf(error_buf, error_buf_size,
  1659. module_inst->cur_exception);
  1660. goto fail;
  1661. }
  1662. }
  1663. #if WASM_ENABLE_LIBC_WASI != 0
  1664. }
  1665. #endif
  1666. #endif
  1667. #if WASM_ENABLE_MEMORY_TRACING != 0
  1668. wasm_runtime_dump_module_inst_mem_consumption(
  1669. (WASMModuleInstanceCommon *)module_inst);
  1670. #endif
  1671. (void)global_data_end;
  1672. return module_inst;
  1673. fail:
  1674. wasm_deinstantiate(module_inst, false);
  1675. return NULL;
  1676. }
  1677. void
  1678. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1679. {
  1680. if (!module_inst)
  1681. return;
  1682. #if WASM_ENABLE_JIT != 0
  1683. if (module_inst->func_ptrs)
  1684. wasm_runtime_free(module_inst->func_ptrs);
  1685. #endif
  1686. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1687. if (module_inst->func_type_indexes)
  1688. wasm_runtime_free(module_inst->func_type_indexes);
  1689. #endif
  1690. #if WASM_ENABLE_MULTI_MODULE != 0
  1691. sub_module_deinstantiate(module_inst);
  1692. #endif
  1693. #if WASM_ENABLE_LIBC_WASI != 0
  1694. /* Destroy wasi resource before freeing app heap, since some fields of
  1695. wasi contex are allocated from app heap, and if app heap is freed,
  1696. these fields will be set to NULL, we cannot free their internal data
  1697. which may allocated from global heap. */
  1698. /* Only destroy wasi ctx in the main module instance */
  1699. if (!is_sub_inst)
  1700. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst);
  1701. #endif
  1702. if (module_inst->memory_count > 0)
  1703. memories_deinstantiate(module_inst, module_inst->memories,
  1704. module_inst->memory_count);
  1705. if (module_inst->import_func_ptrs) {
  1706. wasm_runtime_free(module_inst->import_func_ptrs);
  1707. }
  1708. tables_deinstantiate(module_inst);
  1709. functions_deinstantiate(module_inst->e->functions,
  1710. module_inst->e->function_count);
  1711. globals_deinstantiate(module_inst->e->globals);
  1712. export_functions_deinstantiate(module_inst->export_functions);
  1713. #if WASM_ENABLE_MULTI_MODULE != 0
  1714. export_globals_deinstantiate(module_inst->export_globals);
  1715. #endif
  1716. #if WASM_ENABLE_REF_TYPES != 0
  1717. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  1718. #endif
  1719. if (module_inst->exec_env_singleton)
  1720. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1721. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1722. if (module_inst->frames) {
  1723. bh_vector_destroy(module_inst->frames);
  1724. wasm_runtime_free(module_inst->frames);
  1725. module_inst->frames = NULL;
  1726. }
  1727. #endif
  1728. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  1729. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1730. && WASM_ENABLE_LAZY_JIT != 0)
  1731. if (!is_sub_inst) {
  1732. WASMModule *module = module_inst->module;
  1733. WASMModuleInstance *instance_prev = NULL, *instance;
  1734. os_mutex_lock(&module->instance_list_lock);
  1735. instance = module->instance_list;
  1736. while (instance) {
  1737. if (instance == module_inst) {
  1738. if (!instance_prev)
  1739. module->instance_list = instance->e->next;
  1740. else
  1741. instance_prev->e->next = instance->e->next;
  1742. break;
  1743. }
  1744. instance_prev = instance;
  1745. instance = instance->e->next;
  1746. }
  1747. os_mutex_unlock(&module->instance_list_lock);
  1748. }
  1749. #endif
  1750. #if WASM_ENABLE_SHARED_MEMORY != 0
  1751. if (module_inst->e->mem_lock_inited)
  1752. os_mutex_destroy(&module_inst->e->mem_lock);
  1753. #endif
  1754. if (module_inst->e->c_api_func_imports)
  1755. wasm_runtime_free(module_inst->e->c_api_func_imports);
  1756. wasm_runtime_free(module_inst);
  1757. }
  1758. WASMFunctionInstance *
  1759. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  1760. const char *signature)
  1761. {
  1762. uint32 i;
  1763. for (i = 0; i < module_inst->export_func_count; i++)
  1764. if (!strcmp(module_inst->export_functions[i].name, name))
  1765. return module_inst->export_functions[i].function;
  1766. (void)signature;
  1767. return NULL;
  1768. }
  1769. #if WASM_ENABLE_MULTI_MODULE != 0
  1770. WASMGlobalInstance *
  1771. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1772. {
  1773. uint32 i;
  1774. for (i = 0; i < module_inst->export_global_count; i++)
  1775. if (!strcmp(module_inst->export_globals[i].name, name))
  1776. return module_inst->export_globals[i].global;
  1777. return NULL;
  1778. }
  1779. WASMMemoryInstance *
  1780. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1781. {
  1782. /**
  1783. * using a strong assumption that one module instance only has
  1784. * one memory instance
  1785. */
  1786. (void)module_inst->export_memories;
  1787. return module_inst->memories[0];
  1788. }
  1789. WASMTableInstance *
  1790. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1791. {
  1792. /**
  1793. * using a strong assumption that one module instance only has
  1794. * one table instance
  1795. */
  1796. (void)module_inst->export_tables;
  1797. return module_inst->tables[0];
  1798. }
  1799. #endif
  1800. static bool
  1801. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1802. {
  1803. #if WASM_ENABLE_LIBC_WASI != 0
  1804. const char *exception = wasm_get_exception(module_inst);
  1805. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1806. /* The "wasi proc exit" exception is thrown by native lib to
  1807. let wasm app exit, which is a normal behavior, we clear
  1808. the exception here. */
  1809. wasm_set_exception(module_inst, NULL);
  1810. return true;
  1811. }
  1812. return false;
  1813. #else
  1814. return false;
  1815. #endif
  1816. }
  1817. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1818. static void
  1819. call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
  1820. WASMExecEnv *exec_env,
  1821. WASMFunctionInstance *function, unsigned argc,
  1822. uint32 argv[])
  1823. {
  1824. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1825. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  1826. uint32 page_size = os_getpagesize();
  1827. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  1828. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  1829. uint8 *prev_top = exec_env->wasm_stack.s.top;
  1830. #ifdef BH_PLATFORM_WINDOWS
  1831. const char *exce;
  1832. int result;
  1833. #endif
  1834. bool ret = true;
  1835. /* Check native stack overflow firstly to ensure we have enough
  1836. native stack to run the following codes before actually calling
  1837. the aot function in invokeNative function. */
  1838. if ((uint8 *)&exec_env_tls < exec_env->native_stack_boundary
  1839. + page_size * (guard_page_count + 1)) {
  1840. wasm_set_exception(module_inst, "native stack overflow");
  1841. return;
  1842. }
  1843. if (exec_env_tls && (exec_env_tls != exec_env)) {
  1844. wasm_set_exception(module_inst, "invalid exec env");
  1845. return;
  1846. }
  1847. if (!os_thread_signal_inited()) {
  1848. wasm_set_exception(module_inst, "thread signal env not inited");
  1849. return;
  1850. }
  1851. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  1852. wasm_runtime_set_exec_env_tls(exec_env);
  1853. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  1854. #ifndef BH_PLATFORM_WINDOWS
  1855. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1856. #else
  1857. __try {
  1858. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1859. } __except (wasm_get_exception(module_inst)
  1860. ? EXCEPTION_EXECUTE_HANDLER
  1861. : EXCEPTION_CONTINUE_SEARCH) {
  1862. /* exception was thrown in wasm_exception_handler */
  1863. ret = false;
  1864. }
  1865. if ((exce = wasm_get_exception(module_inst))
  1866. && strstr(exce, "native stack overflow")) {
  1867. /* After a stack overflow, the stack was left
  1868. in a damaged state, let the CRT repair it */
  1869. result = _resetstkoflw();
  1870. bh_assert(result != 0);
  1871. }
  1872. #endif
  1873. }
  1874. else {
  1875. /* Exception has been set in signal handler before calling longjmp */
  1876. ret = false;
  1877. }
  1878. /* Note: can't check wasm_get_exception(module_inst) here, there may be
  1879. * exception which is not caught by hardware (e.g. uninitialized elements),
  1880. * then the stack-frame is already freed inside wasm_interp_call_wasm */
  1881. if (!ret) {
  1882. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1883. if (wasm_interp_create_call_stack(exec_env)) {
  1884. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  1885. }
  1886. #endif
  1887. /* Restore operand frames */
  1888. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  1889. exec_env->wasm_stack.s.top = prev_top;
  1890. }
  1891. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  1892. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  1893. if (!exec_env->jmpbuf_stack_top) {
  1894. wasm_runtime_set_exec_env_tls(NULL);
  1895. }
  1896. if (!ret) {
  1897. os_sigreturn();
  1898. os_signal_unmask();
  1899. }
  1900. (void)jmpbuf_node_pop;
  1901. }
  1902. #define interp_call_wasm call_wasm_with_hw_bound_check
  1903. #else
  1904. #define interp_call_wasm wasm_interp_call_wasm
  1905. #endif
  1906. bool
  1907. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  1908. unsigned argc, uint32 argv[])
  1909. {
  1910. WASMModuleInstance *module_inst =
  1911. (WASMModuleInstance *)exec_env->module_inst;
  1912. /* set thread handle and stack boundary */
  1913. wasm_exec_env_set_thread_info(exec_env);
  1914. interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1915. (void)clear_wasi_proc_exit_exception(module_inst);
  1916. return !wasm_get_exception(module_inst) ? true : false;
  1917. }
  1918. bool
  1919. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1920. WASMFunctionInstance *func,
  1921. unsigned argc, uint32 argv[])
  1922. {
  1923. WASMExecEnv *exec_env = NULL, *existing_exec_env = NULL;
  1924. bool ret;
  1925. #if defined(OS_ENABLE_HW_BOUND_CHECK)
  1926. existing_exec_env = exec_env = wasm_runtime_get_exec_env_tls();
  1927. #elif WASM_ENABLE_THREAD_MGR != 0
  1928. existing_exec_env = exec_env =
  1929. wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
  1930. #endif
  1931. if (!existing_exec_env) {
  1932. if (!(exec_env =
  1933. wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  1934. module_inst->default_wasm_stack_size))) {
  1935. wasm_set_exception(module_inst, "allocate memory failed");
  1936. return false;
  1937. }
  1938. }
  1939. ret = wasm_call_function(exec_env, func, argc, argv);
  1940. /* don't destroy the exec_env if it isn't created in this function */
  1941. if (!existing_exec_env)
  1942. wasm_exec_env_destroy(exec_env);
  1943. return ret;
  1944. }
  1945. #if WASM_ENABLE_PERF_PROFILING != 0
  1946. void
  1947. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  1948. {
  1949. WASMExportFuncInstance *export_func;
  1950. WASMFunctionInstance *func_inst;
  1951. char *func_name;
  1952. uint32 i, j;
  1953. os_printf("Performance profiler data:\n");
  1954. for (i = 0; i < module_inst->e->function_count; i++) {
  1955. func_inst = module_inst->e->functions + i;
  1956. if (func_inst->is_import_func) {
  1957. func_name = func_inst->u.func_import->field_name;
  1958. }
  1959. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1960. else if (func_inst->u.func->field_name) {
  1961. func_name = func_inst->u.func->field_name;
  1962. }
  1963. #endif
  1964. else {
  1965. func_name = NULL;
  1966. for (j = 0; j < module_inst->export_func_count; j++) {
  1967. export_func = module_inst->export_functions + j;
  1968. if (export_func->function == func_inst) {
  1969. func_name = export_func->name;
  1970. break;
  1971. }
  1972. }
  1973. }
  1974. if (func_name)
  1975. os_printf(" func %s, execution time: %.3f ms, execution count: %d "
  1976. "times\n",
  1977. func_name,
  1978. module_inst->e->functions[i].total_exec_time / 1000.0f,
  1979. module_inst->e->functions[i].total_exec_cnt);
  1980. else
  1981. os_printf(" func %d, execution time: %.3f ms, execution count: %d "
  1982. "times\n",
  1983. i, module_inst->e->functions[i].total_exec_time / 1000.0f,
  1984. module_inst->e->functions[i].total_exec_cnt);
  1985. }
  1986. }
  1987. #endif
  1988. uint32
  1989. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1990. void **p_native_addr)
  1991. {
  1992. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  1993. uint8 *addr = NULL;
  1994. uint32 offset = 0;
  1995. if (!memory) {
  1996. wasm_set_exception(module_inst, "uninitialized memory");
  1997. return 0;
  1998. }
  1999. if (memory->heap_handle) {
  2000. addr = mem_allocator_malloc(memory->heap_handle, size);
  2001. }
  2002. else if (module_inst->e->malloc_function && module_inst->e->free_function) {
  2003. if (!execute_malloc_function(
  2004. module_inst, module_inst->e->malloc_function,
  2005. module_inst->e->retain_function, size, &offset)) {
  2006. return 0;
  2007. }
  2008. /* If we use app's malloc function,
  2009. the default memory may be changed while memory growing */
  2010. memory = wasm_get_default_memory(module_inst);
  2011. addr = offset ? memory->memory_data + offset : NULL;
  2012. }
  2013. if (!addr) {
  2014. if (memory->heap_handle
  2015. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2016. wasm_runtime_show_app_heap_corrupted_prompt();
  2017. wasm_set_exception(module_inst, "app heap corrupted");
  2018. }
  2019. else {
  2020. LOG_WARNING("warning: allocate %u bytes memory failed", size);
  2021. }
  2022. return 0;
  2023. }
  2024. if (p_native_addr)
  2025. *p_native_addr = addr;
  2026. return (uint32)(addr - memory->memory_data);
  2027. }
  2028. uint32
  2029. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  2030. void **p_native_addr)
  2031. {
  2032. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2033. uint8 *addr = NULL;
  2034. if (!memory) {
  2035. wasm_set_exception(module_inst, "uninitialized memory");
  2036. return 0;
  2037. }
  2038. if (memory->heap_handle) {
  2039. addr = mem_allocator_realloc(
  2040. memory->heap_handle, ptr ? memory->memory_data + ptr : NULL, size);
  2041. }
  2042. /* Only support realloc in WAMR's app heap */
  2043. if (!addr) {
  2044. if (memory->heap_handle
  2045. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2046. wasm_set_exception(module_inst, "app heap corrupted");
  2047. }
  2048. else {
  2049. wasm_set_exception(module_inst, "out of memory");
  2050. }
  2051. return 0;
  2052. }
  2053. if (p_native_addr)
  2054. *p_native_addr = addr;
  2055. return (uint32)(addr - memory->memory_data);
  2056. }
  2057. void
  2058. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  2059. {
  2060. if (ptr) {
  2061. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2062. uint8 *addr;
  2063. if (!memory) {
  2064. return;
  2065. }
  2066. addr = memory->memory_data + ptr;
  2067. if (memory->heap_handle && memory->heap_data <= addr
  2068. && addr < memory->heap_data_end) {
  2069. mem_allocator_free(memory->heap_handle, addr);
  2070. }
  2071. else if (module_inst->e->malloc_function
  2072. && module_inst->e->free_function && memory->memory_data <= addr
  2073. && addr < memory->memory_data_end) {
  2074. execute_free_function(module_inst, module_inst->e->free_function,
  2075. ptr);
  2076. }
  2077. }
  2078. }
  2079. uint32
  2080. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  2081. uint32 size)
  2082. {
  2083. char *buffer;
  2084. uint32 buffer_offset =
  2085. wasm_module_malloc(module_inst, size, (void **)&buffer);
  2086. if (buffer_offset != 0) {
  2087. buffer = wasm_runtime_addr_app_to_native(
  2088. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  2089. bh_memcpy_s(buffer, size, src, size);
  2090. }
  2091. return buffer_offset;
  2092. }
  2093. #if WASM_ENABLE_REF_TYPES != 0
  2094. bool
  2095. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  2096. uint32 inc_size, uint32 init_val)
  2097. {
  2098. uint32 total_size, *new_table_data_start, i;
  2099. WASMTableInstance *table_inst;
  2100. if (!inc_size) {
  2101. return true;
  2102. }
  2103. bh_assert(table_idx < module_inst->table_count);
  2104. table_inst = wasm_get_table_inst(module_inst, table_idx);
  2105. if (!table_inst) {
  2106. return false;
  2107. }
  2108. if (inc_size > UINT32_MAX - table_inst->cur_size) {
  2109. return false;
  2110. }
  2111. total_size = table_inst->cur_size + inc_size;
  2112. if (total_size > table_inst->max_size) {
  2113. return false;
  2114. }
  2115. /* fill in */
  2116. new_table_data_start = table_inst->elems + table_inst->cur_size;
  2117. for (i = 0; i < inc_size; ++i) {
  2118. new_table_data_start[i] = init_val;
  2119. }
  2120. table_inst->cur_size = total_size;
  2121. return true;
  2122. }
  2123. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2124. static bool
  2125. call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2126. uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
  2127. {
  2128. WASMModuleInstance *module_inst = NULL;
  2129. WASMTableInstance *table_inst = NULL;
  2130. uint32 func_idx = 0;
  2131. WASMFunctionInstance *func_inst = NULL;
  2132. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2133. bh_assert(module_inst);
  2134. table_inst = module_inst->tables[tbl_idx];
  2135. if (!table_inst) {
  2136. wasm_set_exception(module_inst, "unknown table");
  2137. goto got_exception;
  2138. }
  2139. if (elem_idx >= table_inst->cur_size) {
  2140. wasm_set_exception(module_inst, "undefined element");
  2141. goto got_exception;
  2142. }
  2143. func_idx = table_inst->elems[elem_idx];
  2144. if (func_idx == NULL_REF) {
  2145. wasm_set_exception(module_inst, "uninitialized element");
  2146. goto got_exception;
  2147. }
  2148. /**
  2149. * we insist to call functions owned by the module itself
  2150. **/
  2151. if (func_idx >= module_inst->e->function_count) {
  2152. wasm_set_exception(module_inst, "unknown function");
  2153. goto got_exception;
  2154. }
  2155. func_inst = module_inst->e->functions + func_idx;
  2156. if (check_type_idx) {
  2157. WASMType *cur_type = module_inst->module->types[type_idx];
  2158. WASMType *cur_func_type;
  2159. if (func_inst->is_import_func)
  2160. cur_func_type = func_inst->u.func_import->func_type;
  2161. else
  2162. cur_func_type = func_inst->u.func->func_type;
  2163. if (cur_type != cur_func_type) {
  2164. wasm_set_exception(module_inst, "indirect call type mismatch");
  2165. goto got_exception;
  2166. }
  2167. }
  2168. interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
  2169. (void)clear_wasi_proc_exit_exception(module_inst);
  2170. return !wasm_get_exception(module_inst) ? true : false;
  2171. got_exception:
  2172. return false;
  2173. }
  2174. bool
  2175. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2176. uint32 argc, uint32 argv[])
  2177. {
  2178. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2179. }
  2180. #if WASM_ENABLE_THREAD_MGR != 0
  2181. bool
  2182. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  2183. {
  2184. WASMModuleInstance *module_inst =
  2185. (WASMModuleInstance *)exec_env->module_inst;
  2186. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2187. uint32 data_end = module_inst->module->aux_data_end;
  2188. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2189. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2190. /* Check the aux stack space, currently we don't allocate space in heap */
  2191. if ((is_stack_before_data && (size > start_offset))
  2192. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2193. return false;
  2194. if (stack_top_idx != (uint32)-1) {
  2195. /* The aux stack top is a wasm global,
  2196. set the initial value for the global */
  2197. uint8 *global_addr =
  2198. module_inst->global_data
  2199. + module_inst->e->globals[stack_top_idx].data_offset;
  2200. *(int32 *)global_addr = start_offset;
  2201. /* The aux stack boundary is a constant value,
  2202. set the value to exec_env */
  2203. exec_env->aux_stack_boundary.boundary = start_offset - size;
  2204. exec_env->aux_stack_bottom.bottom = start_offset;
  2205. return true;
  2206. }
  2207. return false;
  2208. }
  2209. bool
  2210. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  2211. {
  2212. WASMModuleInstance *module_inst =
  2213. (WASMModuleInstance *)exec_env->module_inst;
  2214. /* The aux stack information is resolved in loader
  2215. and store in module */
  2216. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2217. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  2218. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2219. if (start_offset)
  2220. *start_offset = stack_bottom;
  2221. if (size)
  2222. *size = total_aux_stack_size;
  2223. return true;
  2224. }
  2225. return false;
  2226. }
  2227. #endif
  2228. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2229. void
  2230. wasm_get_module_mem_consumption(const WASMModule *module,
  2231. WASMModuleMemConsumption *mem_conspn)
  2232. {
  2233. uint32 i, size;
  2234. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2235. mem_conspn->module_struct_size = sizeof(WASMModule);
  2236. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2237. for (i = 0; i < module->type_count; i++) {
  2238. WASMType *type = module->types[i];
  2239. size = offsetof(WASMType, types)
  2240. + sizeof(uint8) * (type->param_count + type->result_count);
  2241. mem_conspn->types_size += size;
  2242. }
  2243. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2244. mem_conspn->functions_size =
  2245. sizeof(WASMFunction *) * module->function_count;
  2246. for (i = 0; i < module->function_count; i++) {
  2247. WASMFunction *func = module->functions[i];
  2248. WASMType *type = func->func_type;
  2249. size = sizeof(WASMFunction) + func->local_count
  2250. + sizeof(uint16) * (type->param_count + func->local_count);
  2251. #if WASM_ENABLE_FAST_INTERP != 0
  2252. size +=
  2253. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  2254. #endif
  2255. mem_conspn->functions_size += size;
  2256. }
  2257. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2258. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2259. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2260. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2261. mem_conspn->table_segs_size =
  2262. sizeof(WASMTableSeg) * module->table_seg_count;
  2263. for (i = 0; i < module->table_seg_count; i++) {
  2264. WASMTableSeg *table_seg = &module->table_segments[i];
  2265. mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count;
  2266. }
  2267. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  2268. for (i = 0; i < module->data_seg_count; i++) {
  2269. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2270. }
  2271. if (module->const_str_list) {
  2272. StringNode *node = module->const_str_list, *node_next;
  2273. while (node) {
  2274. node_next = node->next;
  2275. mem_conspn->const_strs_size +=
  2276. sizeof(StringNode) + strlen(node->str) + 1;
  2277. node = node_next;
  2278. }
  2279. }
  2280. mem_conspn->total_size += mem_conspn->module_struct_size;
  2281. mem_conspn->total_size += mem_conspn->types_size;
  2282. mem_conspn->total_size += mem_conspn->imports_size;
  2283. mem_conspn->total_size += mem_conspn->functions_size;
  2284. mem_conspn->total_size += mem_conspn->tables_size;
  2285. mem_conspn->total_size += mem_conspn->memories_size;
  2286. mem_conspn->total_size += mem_conspn->globals_size;
  2287. mem_conspn->total_size += mem_conspn->exports_size;
  2288. mem_conspn->total_size += mem_conspn->table_segs_size;
  2289. mem_conspn->total_size += mem_conspn->data_segs_size;
  2290. mem_conspn->total_size += mem_conspn->const_strs_size;
  2291. }
  2292. void
  2293. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2294. WASMModuleInstMemConsumption *mem_conspn)
  2295. {
  2296. uint32 i, size;
  2297. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2298. mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
  2299. - (uint8 *)module_inst
  2300. + sizeof(WASMModuleInstanceExtra);
  2301. mem_conspn->memories_size =
  2302. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  2303. for (i = 0; i < module_inst->memory_count; i++) {
  2304. WASMMemoryInstance *memory = module_inst->memories[i];
  2305. size = memory->num_bytes_per_page * memory->cur_page_count;
  2306. mem_conspn->memories_size += size;
  2307. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  2308. /* size of app heap structure */
  2309. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  2310. /* Module instance structures have been appened into the end of
  2311. module instance */
  2312. }
  2313. mem_conspn->tables_size =
  2314. sizeof(WASMTableInstance *) * module_inst->table_count;
  2315. /* Table instance structures and table elements have been appened into
  2316. the end of module instance */
  2317. mem_conspn->functions_size =
  2318. sizeof(WASMFunctionInstance) * module_inst->e->function_count;
  2319. mem_conspn->globals_size =
  2320. sizeof(WASMGlobalInstance) * module_inst->e->global_count;
  2321. /* Global data has been appened into the end of module instance */
  2322. mem_conspn->exports_size =
  2323. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  2324. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2325. mem_conspn->total_size += mem_conspn->memories_size;
  2326. mem_conspn->total_size += mem_conspn->functions_size;
  2327. mem_conspn->total_size += mem_conspn->tables_size;
  2328. mem_conspn->total_size += mem_conspn->globals_size;
  2329. mem_conspn->total_size += mem_conspn->exports_size;
  2330. }
  2331. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  2332. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2333. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2334. bool
  2335. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
  2336. {
  2337. WASMModuleInstance *module_inst =
  2338. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2339. WASMInterpFrame *first_frame,
  2340. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2341. uint32 n = 0;
  2342. /* count frames includes a function */
  2343. first_frame = cur_frame;
  2344. while (cur_frame) {
  2345. if (cur_frame->function) {
  2346. n++;
  2347. }
  2348. cur_frame = cur_frame->prev_frame;
  2349. }
  2350. /* release previous stack frames and create new ones */
  2351. if (!bh_vector_destroy(module_inst->frames)
  2352. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame),
  2353. false)) {
  2354. return false;
  2355. }
  2356. cur_frame = first_frame;
  2357. n = 0;
  2358. while (cur_frame) {
  2359. WASMCApiFrame frame = { 0 };
  2360. WASMFunctionInstance *func_inst = cur_frame->function;
  2361. const char *func_name = NULL;
  2362. const uint8 *func_code_base = NULL;
  2363. if (!func_inst) {
  2364. cur_frame = cur_frame->prev_frame;
  2365. continue;
  2366. }
  2367. /* place holder, will overwrite it in wasm_c_api */
  2368. frame.instance = module_inst;
  2369. frame.module_offset = 0;
  2370. frame.func_index = (uint32)(func_inst - module_inst->e->functions);
  2371. func_code_base = wasm_get_func_code(func_inst);
  2372. if (!cur_frame->ip || !func_code_base) {
  2373. frame.func_offset = 0;
  2374. }
  2375. else {
  2376. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  2377. }
  2378. /* look for the function name */
  2379. if (func_inst->is_import_func) {
  2380. func_name = func_inst->u.func_import->field_name;
  2381. }
  2382. else {
  2383. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2384. func_name = func_inst->u.func->field_name;
  2385. #endif
  2386. /* if custom name section is not generated,
  2387. search symbols from export table */
  2388. if (!func_name) {
  2389. uint32 i;
  2390. for (i = 0; i < module_inst->export_func_count; i++) {
  2391. WASMExportFuncInstance *export_func =
  2392. module_inst->export_functions + i;
  2393. if (export_func->function == func_inst) {
  2394. func_name = export_func->name;
  2395. break;
  2396. }
  2397. }
  2398. }
  2399. }
  2400. frame.func_name_wp = func_name;
  2401. if (!bh_vector_append(module_inst->frames, &frame)) {
  2402. bh_vector_destroy(module_inst->frames);
  2403. return false;
  2404. }
  2405. cur_frame = cur_frame->prev_frame;
  2406. n++;
  2407. }
  2408. return true;
  2409. }
  2410. #define PRINT_OR_DUMP() \
  2411. do { \
  2412. total_len += \
  2413. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  2414. if ((!print) && buf && (len == 0)) { \
  2415. return total_len; \
  2416. } \
  2417. } while (0)
  2418. uint32
  2419. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  2420. uint32 len)
  2421. {
  2422. WASMModuleInstance *module_inst =
  2423. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2424. uint32 n = 0, total_len = 0, total_frames;
  2425. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  2426. * will be truncated */
  2427. char line_buf[256];
  2428. if (!module_inst->frames) {
  2429. return 0;
  2430. }
  2431. total_frames = (uint32)bh_vector_size(module_inst->frames);
  2432. if (total_frames == 0) {
  2433. return 0;
  2434. }
  2435. snprintf(line_buf, sizeof(line_buf), "\n");
  2436. PRINT_OR_DUMP();
  2437. while (n < total_frames) {
  2438. WASMCApiFrame frame = { 0 };
  2439. uint32 line_length, i;
  2440. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  2441. return 0;
  2442. }
  2443. /* function name not exported, print number instead */
  2444. if (frame.func_name_wp == NULL) {
  2445. line_length = snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n",
  2446. n, frame.func_index);
  2447. }
  2448. else {
  2449. line_length = snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n,
  2450. frame.func_name_wp);
  2451. }
  2452. if (line_length >= sizeof(line_buf)) {
  2453. uint32 line_buffer_len = sizeof(line_buf);
  2454. /* If line too long, ensure the last character is '\n' */
  2455. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  2456. line_buf[i] = '.';
  2457. }
  2458. line_buf[line_buffer_len - 2] = '\n';
  2459. }
  2460. PRINT_OR_DUMP();
  2461. n++;
  2462. }
  2463. snprintf(line_buf, sizeof(line_buf), "\n");
  2464. PRINT_OR_DUMP();
  2465. return total_len + 1;
  2466. }
  2467. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  2468. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2469. || WASM_ENABLE_WAMR_COMPILER != 0
  2470. void
  2471. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2472. {
  2473. if (id != EXCE_ALREADY_THROWN)
  2474. wasm_set_exception_with_id(module_inst, id);
  2475. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2476. wasm_runtime_access_exce_check_guard_page();
  2477. #endif
  2478. }
  2479. bool
  2480. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  2481. uint32 app_buf_addr, uint32 app_buf_size,
  2482. void **p_native_addr)
  2483. {
  2484. bool ret = wasm_check_app_addr_and_convert(
  2485. module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
  2486. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2487. if (!ret)
  2488. wasm_runtime_access_exce_check_guard_page();
  2489. #endif
  2490. return ret;
  2491. }
  2492. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2493. || WASM_ENABLE_WAMR_COMPILER != 0 */
  2494. #if WASM_ENABLE_FAST_JIT != 0
  2495. bool
  2496. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2497. uint32 type_idx, uint32 argc, uint32 *argv)
  2498. {
  2499. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
  2500. type_idx);
  2501. }
  2502. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2503. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  2504. bool
  2505. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2506. uint32 argc, uint32 *argv)
  2507. {
  2508. bool ret;
  2509. #if WASM_ENABLE_JIT != 0
  2510. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  2511. return aot_call_indirect(exec_env, tbl_idx, elem_idx, argc, argv);
  2512. }
  2513. #endif
  2514. ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2515. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2516. if (!ret)
  2517. wasm_runtime_access_exce_check_guard_page();
  2518. #endif
  2519. return ret;
  2520. }
  2521. bool
  2522. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  2523. uint32 *argv)
  2524. {
  2525. WASMModuleInstance *module_inst;
  2526. WASMModule *module;
  2527. uint32 *func_type_indexes;
  2528. uint32 func_type_idx;
  2529. WASMType *func_type;
  2530. void *func_ptr;
  2531. WASMFunctionImport *import_func;
  2532. CApiFuncImport *c_api_func_import = NULL;
  2533. const char *signature;
  2534. void *attachment;
  2535. char buf[96];
  2536. bool ret = false;
  2537. #if WASM_ENABLE_JIT != 0
  2538. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  2539. return aot_invoke_native(exec_env, func_idx, argc, argv);
  2540. }
  2541. #endif
  2542. module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  2543. module = module_inst->module;
  2544. func_type_indexes = module_inst->func_type_indexes;
  2545. func_type_idx = func_type_indexes[func_idx];
  2546. func_type = module->types[func_type_idx];
  2547. func_ptr = module_inst->func_ptrs[func_idx];
  2548. bh_assert(func_idx < module->import_function_count);
  2549. import_func = &module->import_functions[func_idx].u.function;
  2550. if (import_func->call_conv_wasm_c_api) {
  2551. c_api_func_import = module_inst->e->c_api_func_imports + func_idx;
  2552. func_ptr = c_api_func_import->func_ptr_linked;
  2553. }
  2554. if (!func_ptr) {
  2555. snprintf(buf, sizeof(buf),
  2556. "failed to call unlinked import function (%s, %s)",
  2557. import_func->module_name, import_func->field_name);
  2558. wasm_set_exception(module_inst, buf);
  2559. goto fail;
  2560. }
  2561. attachment = import_func->attachment;
  2562. if (import_func->call_conv_wasm_c_api) {
  2563. ret = wasm_runtime_invoke_c_api_native(
  2564. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  2565. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  2566. }
  2567. else if (!import_func->call_conv_raw) {
  2568. signature = import_func->signature;
  2569. ret =
  2570. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  2571. attachment, argv, argc, argv);
  2572. }
  2573. else {
  2574. signature = import_func->signature;
  2575. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  2576. signature, attachment, argv, argc,
  2577. argv);
  2578. }
  2579. fail:
  2580. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2581. if (!ret)
  2582. wasm_runtime_access_exce_check_guard_page();
  2583. #endif
  2584. return ret;
  2585. }
  2586. #if WASM_ENABLE_BULK_MEMORY != 0
  2587. bool
  2588. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  2589. uint32 offset, uint32 len, uint32 dst)
  2590. {
  2591. WASMMemoryInstance *memory_inst;
  2592. WASMModule *module;
  2593. uint8 *data = NULL;
  2594. uint8 *maddr;
  2595. uint64 seg_len = 0;
  2596. #if WASM_ENABLE_JIT != 0
  2597. if (Wasm_Module_AoT == module_inst->module_type) {
  2598. return aot_memory_init(module_inst, seg_index, offset, len, dst);
  2599. }
  2600. #endif
  2601. memory_inst = wasm_get_default_memory(module_inst);
  2602. module = module_inst->module;
  2603. seg_len = module->data_segments[seg_index]->data_length;
  2604. data = module->data_segments[seg_index]->data;
  2605. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  2606. dst, len))
  2607. return false;
  2608. if ((uint64)offset + (uint64)len > seg_len) {
  2609. wasm_set_exception(module_inst, "out of bounds memory access");
  2610. return false;
  2611. }
  2612. maddr = wasm_runtime_addr_app_to_native(
  2613. (WASMModuleInstanceCommon *)module_inst, dst);
  2614. bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len);
  2615. return true;
  2616. }
  2617. bool
  2618. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
  2619. {
  2620. #if WASM_ENABLE_JIT != 0
  2621. if (Wasm_Module_AoT == module_inst->module_type) {
  2622. return aot_data_drop(module_inst, seg_index);
  2623. }
  2624. #endif
  2625. module_inst->module->data_segments[seg_index]->data_length = 0;
  2626. /* Currently we can't free the dropped data segment
  2627. as they are stored in wasm bytecode */
  2628. return true;
  2629. }
  2630. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
  2631. #if WASM_ENABLE_REF_TYPES != 0
  2632. void
  2633. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
  2634. {
  2635. WASMTableSeg *tbl_segs;
  2636. #if WASM_ENABLE_JIT != 0
  2637. if (Wasm_Module_AoT == module_inst->module_type) {
  2638. return aot_drop_table_seg(module_inst, tbl_seg_idx);
  2639. }
  2640. #endif
  2641. tbl_segs = module_inst->module->table_segments;
  2642. tbl_segs[tbl_seg_idx].is_dropped = true;
  2643. }
  2644. void
  2645. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2646. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  2647. uint32 dst_offset)
  2648. {
  2649. WASMTableInstance *tbl_inst;
  2650. WASMTableSeg *tbl_seg;
  2651. #if WASM_ENABLE_JIT != 0
  2652. if (Wasm_Module_AoT == module_inst->module_type) {
  2653. return aot_table_init(module_inst, tbl_idx, tbl_seg_idx, length,
  2654. src_offset, dst_offset);
  2655. }
  2656. #endif
  2657. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2658. tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
  2659. bh_assert(tbl_inst);
  2660. bh_assert(tbl_seg);
  2661. if (!length) {
  2662. return;
  2663. }
  2664. if (length + src_offset > tbl_seg->function_count
  2665. || dst_offset + length > tbl_inst->cur_size) {
  2666. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2667. return;
  2668. }
  2669. if (tbl_seg->is_dropped) {
  2670. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2671. return;
  2672. }
  2673. if (!wasm_elem_is_passive(tbl_seg->mode)) {
  2674. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2675. return;
  2676. }
  2677. bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems)
  2678. + dst_offset * sizeof(uint32),
  2679. (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset),
  2680. tbl_seg->func_indexes + src_offset,
  2681. (uint32)(length * sizeof(uint32)));
  2682. }
  2683. void
  2684. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  2685. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  2686. uint32 dst_offset)
  2687. {
  2688. WASMTableInstance *src_tbl_inst;
  2689. WASMTableInstance *dst_tbl_inst;
  2690. #if WASM_ENABLE_JIT != 0
  2691. if (Wasm_Module_AoT == module_inst->module_type) {
  2692. aot_table_copy(module_inst, src_tbl_idx, dst_tbl_idx, length,
  2693. src_offset, dst_offset);
  2694. return;
  2695. }
  2696. #endif
  2697. src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
  2698. dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
  2699. bh_assert(src_tbl_inst);
  2700. bh_assert(dst_tbl_inst);
  2701. if ((uint64)dst_offset + length > dst_tbl_inst->cur_size
  2702. || (uint64)src_offset + length > src_tbl_inst->cur_size) {
  2703. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2704. return;
  2705. }
  2706. /* if src_offset >= dst_offset, copy from front to back */
  2707. /* if src_offset < dst_offset, copy from back to front */
  2708. /* merge all together */
  2709. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
  2710. + sizeof(uint32) * dst_offset,
  2711. (uint32)sizeof(uint32) * (dst_tbl_inst->cur_size - dst_offset),
  2712. (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
  2713. + sizeof(uint32) * src_offset,
  2714. (uint32)sizeof(uint32) * length);
  2715. }
  2716. void
  2717. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2718. uint32 length, uint32 val, uint32 data_offset)
  2719. {
  2720. WASMTableInstance *tbl_inst;
  2721. #if WASM_ENABLE_JIT != 0
  2722. if (Wasm_Module_AoT == module_inst->module_type) {
  2723. aot_table_fill(module_inst, tbl_idx, length, val, data_offset);
  2724. return;
  2725. }
  2726. #endif
  2727. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2728. bh_assert(tbl_inst);
  2729. if (data_offset + length > tbl_inst->cur_size) {
  2730. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2731. return;
  2732. }
  2733. for (; length != 0; data_offset++, length--) {
  2734. tbl_inst->elems[data_offset] = val;
  2735. }
  2736. }
  2737. uint32
  2738. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2739. uint32 inc_size, uint32 init_val)
  2740. {
  2741. WASMTableInstance *tbl_inst;
  2742. uint32 i, orig_size, total_size;
  2743. #if WASM_ENABLE_JIT != 0
  2744. if (Wasm_Module_AoT == module_inst->module_type) {
  2745. return aot_table_grow(module_inst, tbl_idx, inc_size, init_val);
  2746. }
  2747. #endif
  2748. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2749. if (!tbl_inst) {
  2750. return (uint32)-1;
  2751. }
  2752. orig_size = tbl_inst->cur_size;
  2753. if (!inc_size) {
  2754. return orig_size;
  2755. }
  2756. if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
  2757. return (uint32)-1;
  2758. }
  2759. total_size = tbl_inst->cur_size + inc_size;
  2760. if (total_size > tbl_inst->max_size) {
  2761. return (uint32)-1;
  2762. }
  2763. /* fill in */
  2764. for (i = 0; i < inc_size; ++i) {
  2765. tbl_inst->elems[tbl_inst->cur_size + i] = init_val;
  2766. }
  2767. tbl_inst->cur_size = total_size;
  2768. return orig_size;
  2769. }
  2770. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  2771. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  2772. bool
  2773. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  2774. {
  2775. WASMModuleInstance *module_inst;
  2776. WASMInterpFrame *frame;
  2777. uint32 size;
  2778. #if WASM_ENABLE_JIT != 0
  2779. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  2780. return aot_alloc_frame(exec_env, func_index);
  2781. }
  2782. #endif
  2783. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2784. size = wasm_interp_interp_frame_size(0);
  2785. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  2786. if (!frame) {
  2787. wasm_set_exception(module_inst, "wasm operand stack overflow");
  2788. return false;
  2789. }
  2790. frame->function = module_inst->e->functions + func_index;
  2791. frame->ip = NULL;
  2792. frame->sp = frame->lp;
  2793. #if WASM_ENABLE_PERF_PROFILING != 0
  2794. frame->time_started = os_time_get_boot_microsecond();
  2795. #endif
  2796. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  2797. wasm_exec_env_set_cur_frame(exec_env, frame);
  2798. return true;
  2799. }
  2800. void
  2801. llvm_jit_free_frame(WASMExecEnv *exec_env)
  2802. {
  2803. WASMInterpFrame *frame;
  2804. WASMInterpFrame *prev_frame;
  2805. #if WASM_ENABLE_JIT != 0
  2806. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  2807. aot_free_frame(exec_env);
  2808. return;
  2809. }
  2810. #endif
  2811. frame = wasm_exec_env_get_cur_frame(exec_env);
  2812. prev_frame = frame->prev_frame;
  2813. #if WASM_ENABLE_PERF_PROFILING != 0
  2814. if (frame->function) {
  2815. frame->function->total_exec_time +=
  2816. os_time_get_boot_microsecond() - frame->time_started;
  2817. frame->function->total_exec_cnt++;
  2818. }
  2819. #endif
  2820. wasm_exec_env_free_wasm_frame(exec_env, frame);
  2821. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  2822. }
  2823. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  2824. || WASM_ENABLE_PERF_PROFILING != 0 */
  2825. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  2826. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  2827. void
  2828. wasm_propagate_wasi_args(WASMModule *module)
  2829. {
  2830. if (!module->import_count)
  2831. return;
  2832. bh_assert(&module->import_module_list_head);
  2833. WASMRegisteredModule *node =
  2834. bh_list_first_elem(&module->import_module_list_head);
  2835. while (node) {
  2836. WASIArguments *wasi_args_impt_mod =
  2837. &((WASMModule *)(node->module))->wasi_args;
  2838. bh_assert(wasi_args_impt_mod);
  2839. bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
  2840. &module->wasi_args, sizeof(WASIArguments));
  2841. node = bh_list_elem_next(node);
  2842. }
  2843. }
  2844. #endif