wasm_runtime.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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. static void
  16. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  17. {
  18. if (error_buf != NULL) {
  19. snprintf(error_buf, error_buf_size,
  20. "WASM module instantiate failed: %s", string);
  21. }
  22. }
  23. WASMModule*
  24. wasm_load(const uint8 *buf, uint32 size,
  25. char *error_buf, uint32 error_buf_size)
  26. {
  27. return wasm_loader_load(buf, size, error_buf, error_buf_size);
  28. }
  29. WASMModule*
  30. wasm_load_from_sections(WASMSection *section_list,
  31. char *error_buf, uint32_t error_buf_size)
  32. {
  33. return wasm_loader_load_from_sections(section_list,
  34. error_buf, error_buf_size);
  35. }
  36. void
  37. wasm_unload(WASMModule *module)
  38. {
  39. wasm_loader_unload(module);
  40. }
  41. static void *
  42. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  43. {
  44. void *mem;
  45. if (size >= UINT32_MAX
  46. || !(mem = wasm_runtime_malloc((uint32)size))) {
  47. set_error_buf(error_buf, error_buf_size,
  48. "allocate memory failed");
  49. return NULL;
  50. }
  51. memset(mem, 0, (uint32)size);
  52. return mem;
  53. }
  54. #if WASM_ENABLE_MULTI_MODULE != 0
  55. static WASMModuleInstance *
  56. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  57. const WASMModule *sub_module)
  58. {
  59. bh_list *sub_module_inst_list = parent_module_inst->sub_module_inst_list;
  60. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  61. while (node && sub_module != node->module_inst->module) {
  62. node = bh_list_elem_next(node);
  63. }
  64. return node ? node->module_inst : NULL;
  65. }
  66. #endif
  67. /**
  68. * Destroy memory instances.
  69. */
  70. static void
  71. memories_deinstantiate(WASMModuleInstance *module_inst,
  72. WASMMemoryInstance **memories,
  73. uint32 count)
  74. {
  75. uint32 i;
  76. if (memories) {
  77. for (i = 0; i < count; i++) {
  78. if (memories[i]) {
  79. #if WASM_ENABLE_MULTI_MODULE != 0
  80. if (memories[i]->owner != module_inst)
  81. continue;
  82. #endif
  83. #if WASM_ENABLE_SHARED_MEMORY != 0
  84. os_mutex_destroy(&memories[0]->mem_lock);
  85. if (memories[i]->is_shared) {
  86. int32 ref_count =
  87. shared_memory_dec_reference(
  88. (WASMModuleCommon *)module_inst->module);
  89. bh_assert(ref_count >= 0);
  90. /* if the reference count is not zero,
  91. don't free the memory */
  92. if (ref_count > 0)
  93. continue;
  94. }
  95. #endif
  96. if (memories[i]->heap_handle) {
  97. mem_allocator_destroy(memories[i]->heap_handle);
  98. wasm_runtime_free(memories[i]->heap_handle);
  99. memories[i]->heap_handle = NULL;
  100. }
  101. wasm_runtime_free(memories[i]->memory_data);
  102. wasm_runtime_free(memories[i]);
  103. }
  104. }
  105. wasm_runtime_free(memories);
  106. }
  107. (void)module_inst;
  108. }
  109. static WASMMemoryInstance*
  110. memory_instantiate(WASMModuleInstance *module_inst,
  111. uint32 num_bytes_per_page,
  112. uint32 init_page_count, uint32 max_page_count,
  113. uint32 heap_size, uint32 flags,
  114. char *error_buf, uint32 error_buf_size)
  115. {
  116. WASMModule *module = module_inst->module;
  117. WASMMemoryInstance *memory;
  118. uint64 memory_data_size;
  119. uint32 heap_offset = num_bytes_per_page * init_page_count;
  120. uint32 inc_page_count, aux_heap_base, global_idx;
  121. uint32 bytes_of_last_page, bytes_to_page_end;
  122. uint8 *global_addr;
  123. #if WASM_ENABLE_SHARED_MEMORY != 0
  124. bool is_shared_memory = flags & 0x02 ? true : false;
  125. /* shared memory */
  126. if (is_shared_memory) {
  127. WASMSharedMemNode *node =
  128. wasm_module_get_shared_memory(
  129. (WASMModuleCommon *)module_inst->module);
  130. /* If the memory of this module has been instantiated,
  131. return the memory instance directly */
  132. if (node) {
  133. uint32 ref_count;
  134. ref_count = shared_memory_inc_reference(
  135. (WASMModuleCommon *)module_inst->module);
  136. bh_assert(ref_count > 0);
  137. memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node);
  138. bh_assert(memory);
  139. (void)ref_count;
  140. return memory;
  141. }
  142. }
  143. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  144. if (heap_size > 0
  145. && module_inst->module->malloc_function != (uint32)-1
  146. && module_inst->module->free_function != (uint32)-1) {
  147. /* Disable app heap, use malloc/free function exported
  148. by wasm app to allocate/free memory instead */
  149. heap_size = 0;
  150. }
  151. if (init_page_count == max_page_count && init_page_count == 1) {
  152. /* If only one page and at most one page, we just append
  153. the app heap to the end of linear memory, enlarge the
  154. num_bytes_per_page, and don't change the page count*/
  155. heap_offset = num_bytes_per_page;
  156. num_bytes_per_page += heap_size;
  157. if (num_bytes_per_page < heap_size) {
  158. set_error_buf(error_buf, error_buf_size,
  159. "memory size must be at most 65536 pages (4GiB)");
  160. return NULL;
  161. }
  162. }
  163. else if (heap_size > 0) {
  164. if (module->aux_heap_base_global_index != (uint32)-1
  165. && module->aux_heap_base < num_bytes_per_page
  166. * init_page_count) {
  167. /* Insert app heap before __heap_base */
  168. aux_heap_base = module->aux_heap_base;
  169. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  170. if (bytes_of_last_page == 0)
  171. bytes_of_last_page = num_bytes_per_page;
  172. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  173. inc_page_count = (heap_size - bytes_to_page_end
  174. + num_bytes_per_page - 1) / num_bytes_per_page;
  175. heap_offset = aux_heap_base;
  176. aux_heap_base += heap_size;
  177. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  178. if (bytes_of_last_page == 0)
  179. bytes_of_last_page = num_bytes_per_page;
  180. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  181. if (bytes_to_page_end < 1 * BH_KB) {
  182. aux_heap_base += 1 * BH_KB;
  183. inc_page_count++;
  184. }
  185. /* Adjust __heap_base global value */
  186. global_idx = module->aux_heap_base_global_index;
  187. global_addr = module_inst->global_data +
  188. module_inst->globals[global_idx].data_offset;
  189. *(uint32 *)global_addr = aux_heap_base;
  190. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  191. }
  192. else {
  193. /* Insert app heap before new page */
  194. inc_page_count = (heap_size + num_bytes_per_page - 1)
  195. / num_bytes_per_page;
  196. heap_offset = num_bytes_per_page * init_page_count;
  197. heap_size = num_bytes_per_page * inc_page_count;
  198. if (heap_size > 0)
  199. heap_size -= 1 * BH_KB;
  200. }
  201. init_page_count += inc_page_count;
  202. max_page_count += inc_page_count;
  203. if (init_page_count > 65536) {
  204. set_error_buf(error_buf, error_buf_size,
  205. "memory size must be at most 65536 pages (4GiB)");
  206. return NULL;
  207. }
  208. if (max_page_count > 65536)
  209. max_page_count = 65536;
  210. }
  211. LOG_VERBOSE("Memory instantiate:");
  212. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  213. num_bytes_per_page, init_page_count, max_page_count);
  214. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  215. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  216. #if WASM_ENABLE_SHARED_MEMORY != 0
  217. if (is_shared_memory) {
  218. /* Allocate max page for shared memory */
  219. memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  220. }
  221. #endif
  222. /* Allocate memory space, addr data and global data */
  223. if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance),
  224. error_buf, error_buf_size))) {
  225. return NULL;
  226. }
  227. if (!(memory->memory_data =
  228. runtime_malloc(memory_data_size, error_buf, error_buf_size))) {
  229. goto fail1;
  230. }
  231. memory->module_type = Wasm_Module_Bytecode;
  232. memory->num_bytes_per_page = num_bytes_per_page;
  233. memory->cur_page_count = init_page_count;
  234. memory->max_page_count = max_page_count;
  235. memory->heap_data = memory->memory_data + heap_offset;
  236. memory->heap_data_end = memory->heap_data + heap_size;
  237. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  238. /* Initialize heap */
  239. if (heap_size > 0) {
  240. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  241. if (!(memory->heap_handle = runtime_malloc((uint64)heap_struct_size,
  242. error_buf, error_buf_size))) {
  243. goto fail2;
  244. }
  245. if (!mem_allocator_create_with_struct_and_pool
  246. (memory->heap_handle, heap_struct_size,
  247. memory->heap_data, heap_size)) {
  248. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  249. goto fail3;
  250. }
  251. }
  252. #if WASM_ENABLE_SHARED_MEMORY != 0
  253. if (0 != os_mutex_init(&memory->mem_lock)) {
  254. set_error_buf(error_buf, error_buf_size, "init mutex failed");
  255. goto fail4;
  256. }
  257. if (is_shared_memory) {
  258. memory->is_shared = true;
  259. if (!shared_memory_set_memory_inst(
  260. (WASMModuleCommon *)module_inst->module,
  261. (WASMMemoryInstanceCommon *)memory)) {
  262. set_error_buf(error_buf, error_buf_size,
  263. "allocate memory failed");
  264. goto fail5;
  265. }
  266. }
  267. #endif
  268. return memory;
  269. #if WASM_ENABLE_SHARED_MEMORY != 0
  270. fail5:
  271. os_mutex_destroy(&memory->mem_lock);
  272. fail4:
  273. if (heap_size > 0)
  274. mem_allocator_destroy(memory->heap_handle);
  275. #endif
  276. fail3:
  277. if (heap_size > 0)
  278. wasm_runtime_free(memory->heap_handle);
  279. fail2:
  280. wasm_runtime_free(memory->memory_data);
  281. fail1:
  282. wasm_runtime_free(memory);
  283. return NULL;
  284. }
  285. /**
  286. * Instantiate memories in a module.
  287. */
  288. static WASMMemoryInstance **
  289. memories_instantiate(const WASMModule *module,
  290. WASMModuleInstance *module_inst,
  291. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  292. {
  293. WASMImport *import;
  294. uint32 mem_index = 0, i, memory_count =
  295. module->import_memory_count + module->memory_count;
  296. uint64 total_size;
  297. WASMMemoryInstance **memories, *memory;
  298. total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
  299. if (!(memories = runtime_malloc(total_size,
  300. error_buf, error_buf_size))) {
  301. return NULL;
  302. }
  303. /* instantiate memories from import section */
  304. import = module->import_memories;
  305. for (i = 0; i < module->import_memory_count; i++, import++) {
  306. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  307. uint32 init_page_count = import->u.memory.init_page_count;
  308. uint32 max_page_count = import->u.memory.max_page_count;
  309. uint32 flags = import->u.memory.flags;
  310. uint32 actual_heap_size = heap_size;
  311. #if WASM_ENABLE_MULTI_MODULE != 0
  312. if (import->u.memory.import_module != NULL) {
  313. WASMModuleInstance *module_inst_linked;
  314. if (!(module_inst_linked = get_sub_module_inst(
  315. module_inst, import->u.memory.import_module))) {
  316. set_error_buf(error_buf, error_buf_size, "unknown memory");
  317. memories_deinstantiate(module_inst, memories, memory_count);
  318. return NULL;
  319. }
  320. if (!(memory = memories[mem_index++] = wasm_lookup_memory(
  321. module_inst_linked, import->u.memory.field_name))) {
  322. set_error_buf(error_buf, error_buf_size, "unknown memory");
  323. memories_deinstantiate(module_inst, memories, memory_count);
  324. return NULL;
  325. }
  326. }
  327. else
  328. #endif
  329. {
  330. if (!(memory = memories[mem_index++] = memory_instantiate(
  331. module_inst, num_bytes_per_page, init_page_count,
  332. max_page_count, actual_heap_size, flags,
  333. error_buf, error_buf_size))) {
  334. memories_deinstantiate(module_inst, memories, memory_count);
  335. return NULL;
  336. }
  337. #if WASM_ENABLE_MULTI_MODULE != 0
  338. /* The module of the import memory is a builtin module, and
  339. the memory is created by current module, set its owner
  340. to current module, so the memory can be destroyed in
  341. memories_deinstantiate. */
  342. memory->owner = module_inst;
  343. #endif
  344. }
  345. }
  346. /* instantiate memories from memory section */
  347. for (i = 0; i < module->memory_count; i++) {
  348. if (!(memory = memories[mem_index++] =
  349. memory_instantiate(module_inst,
  350. module->memories[i].num_bytes_per_page,
  351. module->memories[i].init_page_count,
  352. module->memories[i].max_page_count,
  353. heap_size, module->memories[i].flags,
  354. error_buf, error_buf_size))) {
  355. memories_deinstantiate(module_inst, memories, memory_count);
  356. return NULL;
  357. }
  358. #if WASM_ENABLE_MULTI_MODULE != 0
  359. memory->owner = module_inst;
  360. #endif
  361. }
  362. if (mem_index == 0) {
  363. /**
  364. * no import memory and define memory, but still need heap
  365. * for wasm code
  366. */
  367. if (!(memory = memories[mem_index++] =
  368. memory_instantiate(module_inst, 0, 0, 0, heap_size, 0,
  369. error_buf, error_buf_size))) {
  370. memories_deinstantiate(module_inst, memories, memory_count);
  371. return NULL;
  372. }
  373. }
  374. bh_assert(mem_index == memory_count);
  375. (void)module_inst;
  376. return memories;
  377. }
  378. /**
  379. * Destroy table instances.
  380. */
  381. static void
  382. tables_deinstantiate(WASMTableInstance **tables, uint32 count)
  383. {
  384. uint32 i;
  385. if (tables) {
  386. for (i = 0; i < count; i++)
  387. if (tables[i])
  388. wasm_runtime_free(tables[i]);
  389. wasm_runtime_free(tables);
  390. }
  391. }
  392. /**
  393. * Instantiate tables in a module.
  394. */
  395. static WASMTableInstance **
  396. tables_instantiate(const WASMModule *module,
  397. WASMModuleInstance *module_inst,
  398. char *error_buf, uint32 error_buf_size)
  399. {
  400. WASMImport *import;
  401. uint32 table_index = 0, i, table_count =
  402. module->import_table_count + module->table_count;
  403. uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  404. WASMTableInstance **tables, *table;
  405. if (!(tables = runtime_malloc(total_size,
  406. error_buf, error_buf_size))) {
  407. return NULL;
  408. }
  409. /* instantiate tables from import section */
  410. import = module->import_tables;
  411. for (i = 0; i < module->import_table_count; i++, import++) {
  412. #if WASM_ENABLE_MULTI_MODULE != 0
  413. WASMTableInstance *table_inst_linked = NULL;
  414. WASMModuleInstance *module_inst_linked = NULL;
  415. if (import->u.table.import_module) {
  416. if (!(module_inst_linked =
  417. get_sub_module_inst(module_inst, import->u.table.import_module))) {
  418. set_error_buf(error_buf, error_buf_size, "unknown table");
  419. tables_deinstantiate(tables, table_count);
  420. return NULL;
  421. }
  422. if (!(table_inst_linked = wasm_lookup_table(module_inst_linked,
  423. import->u.table.field_name))) {
  424. set_error_buf(error_buf, error_buf_size, "unknown table");
  425. tables_deinstantiate(tables, table_count);
  426. return NULL;
  427. }
  428. total_size = offsetof(WASMTableInstance, base_addr);
  429. }
  430. else
  431. #endif
  432. {
  433. /* it is a built-in table */
  434. total_size = offsetof(WASMTableInstance, base_addr)
  435. + sizeof(uint32) * (uint64)import->u.table.init_size;
  436. }
  437. if (!(table = tables[table_index++] = runtime_malloc
  438. (total_size, error_buf, error_buf_size))) {
  439. tables_deinstantiate(tables, table_count);
  440. return NULL;
  441. }
  442. /* Set all elements to -1 to mark them as uninitialized elements */
  443. memset(table, -1, (uint32)total_size);
  444. #if WASM_ENABLE_MULTI_MODULE != 0
  445. table->table_inst_linked = table_inst_linked;
  446. if (table_inst_linked != NULL) {
  447. table->elem_type = table_inst_linked->elem_type;
  448. table->cur_size = table_inst_linked->cur_size;
  449. table->max_size = table_inst_linked->max_size;
  450. }
  451. else
  452. #endif
  453. {
  454. table->elem_type = import->u.table.elem_type;
  455. table->cur_size = import->u.table.init_size;
  456. table->max_size = import->u.table.max_size;
  457. }
  458. }
  459. /* instantiate tables from table section */
  460. for (i = 0; i < module->table_count; i++) {
  461. total_size = offsetof(WASMTableInstance, base_addr) +
  462. sizeof(uint32) * (uint64)module->tables[i].init_size;
  463. if (!(table = tables[table_index++] = runtime_malloc
  464. (total_size, error_buf, error_buf_size))) {
  465. tables_deinstantiate(tables, table_count);
  466. return NULL;
  467. }
  468. /* Set all elements to -1 to mark them as uninitialized elements */
  469. memset(table, -1, (uint32)total_size);
  470. table->elem_type = module->tables[i].elem_type;
  471. table->cur_size = module->tables[i].init_size;
  472. table->max_size = module->tables[i].max_size;
  473. #if WASM_ENABLE_MULTI_MODULE != 0
  474. table->table_inst_linked = NULL;
  475. #endif
  476. }
  477. bh_assert(table_index == table_count);
  478. (void)module_inst;
  479. return tables;
  480. }
  481. /**
  482. * Destroy function instances.
  483. */
  484. static void
  485. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  486. {
  487. if (functions) {
  488. wasm_runtime_free(functions);
  489. }
  490. }
  491. /**
  492. * Instantiate functions in a module.
  493. */
  494. static WASMFunctionInstance *
  495. functions_instantiate(const WASMModule *module,
  496. WASMModuleInstance *module_inst,
  497. char *error_buf, uint32 error_buf_size)
  498. {
  499. WASMImport *import;
  500. uint32 i, function_count =
  501. module->import_function_count + module->function_count;
  502. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  503. WASMFunctionInstance *functions, *function;
  504. if (!(functions = runtime_malloc(total_size,
  505. error_buf, error_buf_size))) {
  506. return NULL;
  507. }
  508. /* instantiate functions from import section */
  509. function = functions;
  510. import = module->import_functions;
  511. for (i = 0; i < module->import_function_count; i++, import++) {
  512. function->is_import_func = true;
  513. #if WASM_ENABLE_MULTI_MODULE != 0
  514. if (import->u.function.import_module) {
  515. function->import_module_inst =
  516. get_sub_module_inst(module_inst,
  517. import->u.function.import_module);
  518. if (function->import_module_inst) {
  519. function->import_func_inst =
  520. wasm_lookup_function(function->import_module_inst,
  521. import->u.function.field_name, NULL);
  522. }
  523. }
  524. #endif /* WASM_ENABLE_MULTI_MODULE */
  525. function->u.func_import = &import->u.function;
  526. function->param_cell_num =
  527. import->u.function.func_type->param_cell_num;
  528. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  529. function->param_count =
  530. (uint16)function->u.func_import->func_type->param_count;
  531. function->param_types = function->u.func_import->func_type->types;
  532. function->local_cell_num = 0;
  533. function->local_count = 0;
  534. function->local_types = NULL;
  535. function++;
  536. }
  537. /* instantiate functions from function section */
  538. for (i = 0; i < module->function_count; i++) {
  539. function->is_import_func = false;
  540. function->u.func = module->functions[i];
  541. function->param_cell_num = function->u.func->param_cell_num;
  542. function->ret_cell_num = function->u.func->ret_cell_num;
  543. function->local_cell_num = function->u.func->local_cell_num;
  544. function->param_count = (uint16)function->u.func->func_type->param_count;
  545. function->local_count = (uint16)function->u.func->local_count;
  546. function->param_types = function->u.func->func_type->types;
  547. function->local_types = function->u.func->local_types;
  548. function->local_offsets = function->u.func->local_offsets;
  549. #if WASM_ENABLE_FAST_INTERP != 0
  550. function->const_cell_num = function->u.func->const_cell_num;
  551. #endif
  552. function++;
  553. }
  554. bh_assert((uint32)(function - functions) == function_count);
  555. (void)module_inst;
  556. return functions;
  557. }
  558. /**
  559. * Destroy global instances.
  560. */
  561. static void
  562. globals_deinstantiate(WASMGlobalInstance *globals)
  563. {
  564. if (globals)
  565. wasm_runtime_free(globals);
  566. }
  567. /**
  568. * init_expr->u ==> init_val
  569. */
  570. static bool
  571. parse_init_expr(const InitializerExpression *init_expr,
  572. const WASMGlobalInstance *global_inst_array,
  573. uint32 boundary, WASMValue *init_val)
  574. {
  575. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  576. uint32 target_global_index = init_expr->u.global_index;
  577. /**
  578. * a global gets the init value of another global
  579. */
  580. if (target_global_index >= boundary) {
  581. LOG_DEBUG("unknown target global, %d", target_global_index);
  582. return false;
  583. }
  584. /**
  585. * it will work if using WASMGlobalImport and WASMGlobal in
  586. * WASMModule, but will have to face complicated cases
  587. *
  588. * but we still have no sure the target global has been
  589. * initialized before
  590. */
  591. WASMValue target_value =
  592. global_inst_array[target_global_index].initial_value;
  593. bh_memcpy_s(init_val, sizeof(WASMValue), &target_value,
  594. sizeof(target_value));
  595. }
  596. else {
  597. bh_memcpy_s(init_val, sizeof(WASMValue), &init_expr->u,
  598. sizeof(init_expr->u));
  599. }
  600. return true;
  601. }
  602. /**
  603. * Instantiate globals in a module.
  604. */
  605. static WASMGlobalInstance *
  606. globals_instantiate(const WASMModule *module,
  607. WASMModuleInstance *module_inst,
  608. uint32 *p_global_data_size, char *error_buf,
  609. uint32 error_buf_size)
  610. {
  611. WASMImport *import;
  612. uint32 global_data_offset = 0;
  613. uint32 i, global_count =
  614. module->import_global_count + module->global_count;
  615. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  616. WASMGlobalInstance *globals, *global;
  617. if (!(globals = runtime_malloc(total_size,
  618. error_buf, error_buf_size))) {
  619. return NULL;
  620. }
  621. /* instantiate globals from import section */
  622. global = globals;
  623. import = module->import_globals;
  624. for (i = 0; i < module->import_global_count; i++, import++) {
  625. WASMGlobalImport *global_import = &import->u.global;
  626. global->type = global_import->type;
  627. global->is_mutable = global_import->is_mutable;
  628. #if WASM_ENABLE_MULTI_MODULE != 0
  629. if (global_import->import_module) {
  630. if (!(global->import_module_inst = get_sub_module_inst(
  631. module_inst, global_import->import_module))) {
  632. set_error_buf(error_buf, error_buf_size, "unknown global");
  633. return NULL;
  634. }
  635. if (!(global->import_global_inst = wasm_lookup_global(
  636. global->import_module_inst, global_import->field_name))) {
  637. set_error_buf(error_buf, error_buf_size, "unknown global");
  638. return NULL;
  639. }
  640. /**
  641. * although, actually don't need initial_value for an imported
  642. * global, we keep it here like a place holder because of
  643. * global-data and
  644. * (global $g2 i32 (global.get $g1))
  645. */
  646. if (!parse_init_expr(
  647. &(global_import->import_global_linked->init_expr),
  648. global->import_module_inst->globals,
  649. global->import_module_inst->global_count,
  650. &(global->initial_value))) {
  651. set_error_buf(error_buf, error_buf_size, "unknown global");
  652. return NULL;
  653. }
  654. }
  655. else
  656. #endif
  657. {
  658. /* native globals share their initial_values in one module */
  659. global->initial_value = global_import->global_data_linked;
  660. }
  661. global->data_offset = global_data_offset;
  662. global_data_offset += wasm_value_type_size(global->type);
  663. global++;
  664. }
  665. /* instantiate globals from global section */
  666. for (i = 0; i < module->global_count; i++) {
  667. bool ret = false;
  668. uint32 global_count =
  669. module->import_global_count + module->global_count;
  670. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  671. global->type = module->globals[i].type;
  672. global->is_mutable = module->globals[i].is_mutable;
  673. global->data_offset = global_data_offset;
  674. global_data_offset += wasm_value_type_size(global->type);
  675. /**
  676. * first init, it might happen that the target global instance
  677. * has not been initialize yet
  678. */
  679. if (init_expr->init_expr_type != INIT_EXPR_TYPE_GET_GLOBAL) {
  680. ret =
  681. parse_init_expr(init_expr, globals, global_count,
  682. &(global->initial_value));
  683. if (!ret) {
  684. set_error_buf(error_buf, error_buf_size, "unknown global");
  685. return NULL;
  686. }
  687. }
  688. global++;
  689. }
  690. bh_assert((uint32)(global - globals) == global_count);
  691. *p_global_data_size = global_data_offset;
  692. (void)module_inst;
  693. return globals;
  694. }
  695. static bool
  696. globals_instantiate_fix(WASMGlobalInstance *globals,
  697. const WASMModule *module,
  698. char *error_buf, uint32 error_buf_size)
  699. {
  700. WASMGlobalInstance *global = globals;
  701. uint32 i;
  702. uint32 global_count = module->import_global_count + module->global_count;
  703. /**
  704. * second init, only target global instances from global
  705. * (ignore import_global)
  706. * to fix skipped init_value in the previous round
  707. * hope two rounds are enough but how about a chain ?
  708. */
  709. for (i = 0; i < module->global_count; i++) {
  710. bool ret = false;
  711. InitializerExpression *init_expr = &module->globals[i].init_expr;
  712. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  713. ret = parse_init_expr(init_expr, globals, global_count,
  714. &global->initial_value);
  715. if (!ret) {
  716. set_error_buf(error_buf, error_buf_size, "unknown global");
  717. return false;
  718. }
  719. }
  720. global++;
  721. }
  722. return true;
  723. }
  724. /**
  725. * Return export function count in module export section.
  726. */
  727. static uint32
  728. get_export_count(const WASMModule *module, uint8 kind)
  729. {
  730. WASMExport *export = module->exports;
  731. uint32 count = 0, i;
  732. for (i = 0; i < module->export_count; i++, export++)
  733. if (export->kind == kind)
  734. count++;
  735. return count;
  736. }
  737. /**
  738. * Destroy export function instances.
  739. */
  740. static void
  741. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  742. {
  743. if (functions)
  744. wasm_runtime_free(functions);
  745. }
  746. /**
  747. * Instantiate export functions in a module.
  748. */
  749. static WASMExportFuncInstance*
  750. export_functions_instantiate(const WASMModule *module,
  751. WASMModuleInstance *module_inst,
  752. uint32 export_func_count,
  753. char *error_buf, uint32 error_buf_size)
  754. {
  755. WASMExportFuncInstance *export_funcs, *export_func;
  756. WASMExport *export = module->exports;
  757. uint32 i;
  758. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  759. if (!(export_func = export_funcs = runtime_malloc
  760. (total_size, error_buf, error_buf_size))) {
  761. return NULL;
  762. }
  763. for (i = 0; i < module->export_count; i++, export++)
  764. if (export->kind == EXPORT_KIND_FUNC) {
  765. export_func->name = export->name;
  766. export_func->function = &module_inst->functions[export->index];
  767. export_func++;
  768. }
  769. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  770. return export_funcs;
  771. }
  772. #if WASM_ENABLE_MULTI_MODULE != 0
  773. static void
  774. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  775. {
  776. if (globals)
  777. wasm_runtime_free(globals);
  778. }
  779. static WASMExportGlobInstance *
  780. export_globals_instantiate(const WASMModule *module,
  781. WASMModuleInstance *module_inst,
  782. uint32 export_glob_count, char *error_buf,
  783. uint32 error_buf_size)
  784. {
  785. WASMExportGlobInstance *export_globals, *export_global;
  786. WASMExport *export = module->exports;
  787. uint32 i;
  788. uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  789. if (!(export_global = export_globals = runtime_malloc
  790. (total_size, error_buf, error_buf_size))) {
  791. return NULL;
  792. }
  793. for (i = 0; i < module->export_count; i++, export++)
  794. if (export->kind == EXPORT_KIND_GLOBAL) {
  795. export_global->name = export->name;
  796. export_global->global = &module_inst->globals[export->index];
  797. export_global++;
  798. }
  799. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  800. return export_globals;
  801. }
  802. #endif
  803. static bool
  804. execute_post_inst_function(WASMModuleInstance *module_inst)
  805. {
  806. WASMFunctionInstance *post_inst_func = NULL;
  807. WASMType *post_inst_func_type;
  808. uint32 i;
  809. for (i = 0; i < module_inst->export_func_count; i++)
  810. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  811. post_inst_func = module_inst->export_functions[i].function;
  812. break;
  813. }
  814. if (!post_inst_func)
  815. /* Not found */
  816. return true;
  817. post_inst_func_type = post_inst_func->u.func->func_type;
  818. if (post_inst_func_type->param_count != 0
  819. || post_inst_func_type->result_count != 0)
  820. /* Not a valid function type, ignore it */
  821. return true;
  822. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  823. 0, NULL);
  824. }
  825. #if WASM_ENABLE_BULK_MEMORY != 0
  826. static bool
  827. execute_memory_init_function(WASMModuleInstance *module_inst)
  828. {
  829. WASMFunctionInstance *memory_init_func = NULL;
  830. WASMType *memory_init_func_type;
  831. uint32 i;
  832. for (i = 0; i < module_inst->export_func_count; i++)
  833. if (!strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) {
  834. memory_init_func = module_inst->export_functions[i].function;
  835. break;
  836. }
  837. if (!memory_init_func)
  838. /* Not found */
  839. return true;
  840. memory_init_func_type = memory_init_func->u.func->func_type;
  841. if (memory_init_func_type->param_count != 0
  842. || memory_init_func_type->result_count != 0)
  843. /* Not a valid function type, ignore it */
  844. return true;
  845. return wasm_create_exec_env_and_call_function(module_inst,
  846. memory_init_func,
  847. 0, NULL);
  848. }
  849. #endif
  850. static bool
  851. execute_start_function(WASMModuleInstance *module_inst)
  852. {
  853. WASMFunctionInstance *func = module_inst->start_function;
  854. if (!func)
  855. return true;
  856. bh_assert(!func->is_import_func && func->param_cell_num == 0
  857. && func->ret_cell_num == 0);
  858. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  859. }
  860. static bool
  861. execute_malloc_function(WASMModuleInstance *module_inst,
  862. WASMFunctionInstance *malloc_func,
  863. uint32 size, uint32 *p_result)
  864. {
  865. uint32 argv[2];
  866. bool ret;
  867. argv[0] = size;
  868. ret = wasm_create_exec_env_and_call_function
  869. (module_inst, malloc_func, 1, argv);
  870. if (ret)
  871. *p_result = argv[0];
  872. return ret;
  873. }
  874. static bool
  875. execute_free_function(WASMModuleInstance *module_inst,
  876. WASMFunctionInstance *free_func,
  877. uint32 offset)
  878. {
  879. uint32 argv[2];
  880. argv[0] = offset;
  881. return wasm_create_exec_env_and_call_function
  882. (module_inst, free_func, 1, argv);
  883. }
  884. #if WASM_ENABLE_MULTI_MODULE != 0
  885. static bool
  886. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  887. uint32 stack_size, uint32 heap_size, char *error_buf,
  888. uint32 error_buf_size)
  889. {
  890. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  891. WASMRegisteredModule *sub_module_list_node =
  892. bh_list_first_elem(module->import_module_list);
  893. while (sub_module_list_node) {
  894. WASMSubModInstNode *sub_module_inst_list_node;
  895. WASMModule *sub_module = (WASMModule*)sub_module_list_node->module;
  896. WASMModuleInstance *sub_module_inst =
  897. wasm_instantiate(sub_module, false, stack_size, heap_size,
  898. error_buf, error_buf_size);
  899. if (!sub_module_inst) {
  900. LOG_DEBUG("instantiate %s failed",
  901. sub_module_list_node->module_name);
  902. return false;
  903. }
  904. sub_module_inst_list_node = runtime_malloc
  905. (sizeof(WASMSubModInstNode), error_buf, error_buf_size);
  906. if (!sub_module_inst_list_node) {
  907. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  908. sizeof(WASMSubModInstNode));
  909. wasm_deinstantiate(sub_module_inst, false);
  910. return false;
  911. }
  912. sub_module_inst_list_node->module_inst = sub_module_inst;
  913. sub_module_inst_list_node->module_name =
  914. sub_module_list_node->module_name;
  915. bh_list_status ret =
  916. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  917. bh_assert(BH_LIST_SUCCESS == ret);
  918. (void)ret;
  919. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  920. }
  921. return true;
  922. }
  923. static void
  924. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  925. {
  926. bh_list *list = module_inst->sub_module_inst_list;
  927. WASMSubModInstNode *node = bh_list_first_elem(list);
  928. while (node) {
  929. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  930. bh_list_remove(list, node);
  931. wasm_deinstantiate(node->module_inst, false);
  932. wasm_runtime_free(node);
  933. node = next_node;
  934. }
  935. }
  936. #endif
  937. static bool
  938. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  939. uint32 error_buf_size)
  940. {
  941. WASMModule *module = module_inst->module;
  942. uint32 i;
  943. for (i = 0; i < module->import_function_count; i++) {
  944. WASMFunctionImport *func =
  945. &((module->import_functions + i)->u.function);
  946. if (!func->func_ptr_linked
  947. #if WASM_ENABLE_MULTI_MODULE != 0
  948. && !func->import_func_linked
  949. #endif
  950. ) {
  951. #if WASM_ENABLE_SPEC_TEST != 0
  952. set_error_buf(error_buf, error_buf_size,
  953. "unknown import or incompatible import type");
  954. return false;
  955. #else
  956. #if WASM_ENABLE_WAMR_COMPILER == 0
  957. LOG_WARNING("warning: fail to link import function (%s, %s)",
  958. func->module_name, func->field_name);
  959. #else
  960. /* do nothing to avoid confused message */
  961. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  962. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  963. }
  964. }
  965. for (i = 0; i < module->import_global_count; i++) {
  966. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  967. if (!global->is_linked) {
  968. #if WASM_ENABLE_SPEC_TEST != 0
  969. set_error_buf(error_buf, error_buf_size,
  970. "unknown import or incompatible import type");
  971. return false;
  972. #else
  973. #if WASM_ENABLE_WAMR_COMPILER == 0
  974. LOG_DEBUG("warning: fail to link import global (%s, %s)",
  975. global->module_name, global->field_name);
  976. #else
  977. /* do nothing to avoid confused message */
  978. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  979. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  980. }
  981. }
  982. return true;
  983. }
  984. /**
  985. * Instantiate module
  986. */
  987. WASMModuleInstance*
  988. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  989. uint32 stack_size, uint32 heap_size,
  990. char *error_buf, uint32 error_buf_size)
  991. {
  992. WASMModuleInstance *module_inst;
  993. WASMGlobalInstance *globals = NULL, *global;
  994. uint32 global_count, global_data_size = 0, i;
  995. uint32 base_offset, length;
  996. uint8 *global_data, *global_data_end;
  997. #if WASM_ENABLE_MULTI_MODULE != 0
  998. bool ret = false;
  999. #endif
  1000. if (!module)
  1001. return NULL;
  1002. /* Check heap size */
  1003. heap_size = align_uint(heap_size, 8);
  1004. if (heap_size > APP_HEAP_SIZE_MAX)
  1005. heap_size = APP_HEAP_SIZE_MAX;
  1006. /* Allocate the memory */
  1007. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  1008. error_buf, error_buf_size))) {
  1009. return NULL;
  1010. }
  1011. module_inst->module = module;
  1012. #if WASM_ENABLE_MULTI_MODULE != 0
  1013. module_inst->sub_module_inst_list =
  1014. &module_inst->sub_module_inst_list_head;
  1015. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1016. error_buf, error_buf_size);
  1017. if (!ret) {
  1018. LOG_DEBUG("build a sub module list failed");
  1019. goto fail;
  1020. }
  1021. #endif
  1022. /* Instantiate global firstly to get the mutable data size */
  1023. global_count = module->import_global_count + module->global_count;
  1024. if (global_count
  1025. && !(globals = globals_instantiate(module, module_inst,
  1026. &global_data_size,
  1027. error_buf, error_buf_size))) {
  1028. goto fail;
  1029. }
  1030. module_inst->global_count = global_count;
  1031. module_inst->globals = globals;
  1032. module_inst->memory_count =
  1033. module->import_memory_count + module->memory_count;
  1034. module_inst->table_count =
  1035. module->import_table_count + module->table_count;
  1036. module_inst->function_count =
  1037. module->import_function_count + module->function_count;
  1038. /* export */
  1039. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1040. #if WASM_ENABLE_MULTI_MODULE != 0
  1041. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1042. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1043. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1044. #endif
  1045. if (global_count > 0) {
  1046. if (!(module_inst->global_data = runtime_malloc
  1047. (global_data_size, error_buf, error_buf_size))) {
  1048. goto fail;
  1049. }
  1050. }
  1051. /* Instantiate memories/tables/functions */
  1052. if ((module_inst->memory_count > 0
  1053. && !(module_inst->memories =
  1054. memories_instantiate(module,
  1055. module_inst,
  1056. heap_size, error_buf, error_buf_size)))
  1057. || (module_inst->table_count > 0
  1058. && !(module_inst->tables =
  1059. tables_instantiate(module,
  1060. module_inst,
  1061. error_buf, error_buf_size)))
  1062. || (module_inst->function_count > 0
  1063. && !(module_inst->functions =
  1064. functions_instantiate(module,
  1065. module_inst,
  1066. error_buf, error_buf_size)))
  1067. || (module_inst->export_func_count > 0
  1068. && !(module_inst->export_functions = export_functions_instantiate(
  1069. module, module_inst, module_inst->export_func_count,
  1070. error_buf, error_buf_size)))
  1071. #if WASM_ENABLE_MULTI_MODULE != 0
  1072. || (module_inst->export_glob_count > 0
  1073. && !(module_inst->export_globals = export_globals_instantiate(
  1074. module, module_inst, module_inst->export_glob_count,
  1075. error_buf, error_buf_size)))
  1076. #endif
  1077. ) {
  1078. goto fail;
  1079. }
  1080. if (global_count > 0) {
  1081. /**
  1082. * since there might be some globals are not instantiate the first
  1083. * instantiate round
  1084. */
  1085. if (!globals_instantiate_fix(globals, module,
  1086. error_buf, error_buf_size)) {
  1087. goto fail;
  1088. }
  1089. /* Initialize the global data */
  1090. global_data = module_inst->global_data;
  1091. global_data_end = global_data + global_data_size;
  1092. global = globals;
  1093. for (i = 0; i < global_count; i++, global++) {
  1094. switch (global->type) {
  1095. case VALUE_TYPE_I32:
  1096. case VALUE_TYPE_F32:
  1097. *(int32*)global_data = global->initial_value.i32;
  1098. global_data += sizeof(int32);
  1099. break;
  1100. case VALUE_TYPE_I64:
  1101. case VALUE_TYPE_F64:
  1102. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1103. &global->initial_value.i64, sizeof(int64));
  1104. global_data += sizeof(int64);
  1105. break;
  1106. default:
  1107. bh_assert(0);
  1108. }
  1109. }
  1110. bh_assert(global_data == global_data_end);
  1111. }
  1112. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1113. goto fail;
  1114. }
  1115. /* Initialize the memory data with data segment section */
  1116. module_inst->default_memory =
  1117. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1118. for (i = 0; i < module->data_seg_count; i++) {
  1119. WASMMemoryInstance *memory = NULL;
  1120. uint8 *memory_data = NULL;
  1121. uint32 memory_size = 0;
  1122. WASMDataSeg *data_seg = module->data_segments[i];
  1123. #if WASM_ENABLE_BULK_MEMORY != 0
  1124. if (data_seg->is_passive)
  1125. continue;
  1126. #endif
  1127. /* has check it in loader */
  1128. memory = module_inst->memories[data_seg->memory_index];
  1129. bh_assert(memory);
  1130. memory_data = memory->memory_data;
  1131. bh_assert(memory_data);
  1132. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1133. bh_assert(data_seg->base_offset.init_expr_type
  1134. == INIT_EXPR_TYPE_I32_CONST
  1135. || data_seg->base_offset.init_expr_type
  1136. == INIT_EXPR_TYPE_GET_GLOBAL);
  1137. if (data_seg->base_offset.init_expr_type
  1138. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1139. if (!globals
  1140. || data_seg->base_offset.u.global_index >= global_count
  1141. || globals[data_seg->base_offset.u.global_index].type
  1142. != VALUE_TYPE_I32) {
  1143. set_error_buf(error_buf, error_buf_size,
  1144. "data segment does not fit");
  1145. goto fail;
  1146. }
  1147. data_seg->base_offset.u.i32 =
  1148. globals[data_seg->base_offset.u.global_index]
  1149. .initial_value.i32;
  1150. }
  1151. /* check offset since length might negative */
  1152. base_offset = (uint32)data_seg->base_offset.u.i32;
  1153. if (base_offset > memory_size) {
  1154. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1155. memory_size);
  1156. set_error_buf(error_buf, error_buf_size,
  1157. "data segment does not fit");
  1158. goto fail;
  1159. }
  1160. /* check offset + length(could be zero) */
  1161. length = data_seg->data_length;
  1162. if (base_offset + length > memory_size) {
  1163. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1164. base_offset, length, memory_size);
  1165. set_error_buf(error_buf, error_buf_size,
  1166. "data segment does not fit");
  1167. goto fail;
  1168. }
  1169. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1170. data_seg->data, length);
  1171. }
  1172. /* Initialize the table data with table segment section */
  1173. module_inst->default_table =
  1174. module_inst->table_count ? module_inst->tables[0] : NULL;
  1175. for (i = 0; i < module->table_seg_count; i++) {
  1176. WASMTableSeg *table_seg = module->table_segments + i;
  1177. /* has check it in loader */
  1178. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1179. bh_assert(table);
  1180. uint32 *table_data = (uint32 *)table->base_addr;
  1181. #if WASM_ENABLE_MULTI_MODULE != 0
  1182. table_data = table->table_inst_linked
  1183. ? (uint32 *)table->table_inst_linked->base_addr
  1184. : table_data;
  1185. #endif
  1186. bh_assert(table_data);
  1187. /* init vec(funcidx) */
  1188. bh_assert(table_seg->base_offset.init_expr_type
  1189. == INIT_EXPR_TYPE_I32_CONST
  1190. || table_seg->base_offset.init_expr_type
  1191. == INIT_EXPR_TYPE_GET_GLOBAL);
  1192. if (table_seg->base_offset.init_expr_type
  1193. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1194. if (!globals
  1195. || table_seg->base_offset.u.global_index >= global_count
  1196. || globals[table_seg->base_offset.u.global_index].type
  1197. != VALUE_TYPE_I32) {
  1198. set_error_buf(error_buf, error_buf_size,
  1199. "elements segment does not fit");
  1200. goto fail;
  1201. }
  1202. table_seg->base_offset.u.i32 =
  1203. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1204. }
  1205. /* check offset since length might negative */
  1206. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1207. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1208. table_seg->base_offset.u.i32, table->cur_size);
  1209. set_error_buf(error_buf, error_buf_size,
  1210. "elements segment does not fit");
  1211. goto fail;
  1212. }
  1213. /* check offset + length(could be zero) */
  1214. length = table_seg->function_count;
  1215. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1216. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1217. table_seg->base_offset.u.i32, length, table->cur_size);
  1218. set_error_buf(error_buf, error_buf_size,
  1219. "elements segment does not fit");
  1220. goto fail;
  1221. }
  1222. /**
  1223. * Check function index in the current module inst for now.
  1224. * will check the linked table inst owner in future.
  1225. * so loader check is enough
  1226. */
  1227. bh_memcpy_s(
  1228. table_data + table_seg->base_offset.u.i32,
  1229. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1230. * sizeof(uint32)),
  1231. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1232. }
  1233. /* module instance type */
  1234. module_inst->module_type = Wasm_Module_Bytecode;
  1235. /* Initialize the thread related data */
  1236. if (stack_size == 0)
  1237. stack_size = DEFAULT_WASM_STACK_SIZE;
  1238. #if WASM_ENABLE_SPEC_TEST != 0
  1239. if (stack_size < 48 *1024)
  1240. stack_size = 48 * 1024;
  1241. #endif
  1242. module_inst->default_wasm_stack_size = stack_size;
  1243. if (module->malloc_function != (uint32)-1) {
  1244. module_inst->malloc_function =
  1245. &module_inst->functions[module->malloc_function];
  1246. }
  1247. if (module->free_function != (uint32)-1) {
  1248. module_inst->free_function =
  1249. &module_inst->functions[module->free_function];
  1250. }
  1251. #if WASM_ENABLE_LIBC_WASI != 0
  1252. /* The sub-instance will get the wasi_ctx from main-instance */
  1253. if (!is_sub_inst) {
  1254. if (heap_size > 0
  1255. && !wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1256. module->wasi_args.dir_list,
  1257. module->wasi_args.dir_count,
  1258. module->wasi_args.map_dir_list,
  1259. module->wasi_args.map_dir_count,
  1260. module->wasi_args.env,
  1261. module->wasi_args.env_count,
  1262. module->wasi_args.argv,
  1263. module->wasi_args.argc,
  1264. error_buf, error_buf_size)) {
  1265. goto fail;
  1266. }
  1267. }
  1268. #endif
  1269. if (module->start_function != (uint32)-1) {
  1270. /* TODO: fix start function can be import function issue */
  1271. if (module->start_function >= module->import_function_count)
  1272. module_inst->start_function =
  1273. &module_inst->functions[module->start_function];
  1274. }
  1275. /* Execute __post_instantiate function */
  1276. if (!execute_post_inst_function(module_inst)
  1277. || !execute_start_function(module_inst)) {
  1278. set_error_buf(error_buf, error_buf_size,
  1279. module_inst->cur_exception);
  1280. goto fail;
  1281. }
  1282. #if WASM_ENABLE_BULK_MEMORY != 0
  1283. #if WASM_ENABLE_LIBC_WASI != 0
  1284. if (!module->is_wasi_module) {
  1285. #endif
  1286. /* Only execute the memory init function for main instance because
  1287. the data segments will be dropped once initialized.
  1288. */
  1289. if (!is_sub_inst) {
  1290. if (!execute_memory_init_function(module_inst)) {
  1291. set_error_buf(error_buf, error_buf_size,
  1292. module_inst->cur_exception);
  1293. goto fail;
  1294. }
  1295. }
  1296. #if WASM_ENABLE_LIBC_WASI != 0
  1297. }
  1298. #endif
  1299. #endif
  1300. #if WASM_ENABLE_MEMORY_TRACING != 0
  1301. wasm_runtime_dump_module_inst_mem_consumption
  1302. ((WASMModuleInstanceCommon *)module_inst);
  1303. #endif
  1304. (void)global_data_end;
  1305. return module_inst;
  1306. fail:
  1307. wasm_deinstantiate(module_inst, false);
  1308. return NULL;
  1309. }
  1310. void
  1311. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1312. {
  1313. if (!module_inst)
  1314. return;
  1315. #if WASM_ENABLE_MULTI_MODULE != 0
  1316. sub_module_deinstantiate(module_inst);
  1317. #endif
  1318. #if WASM_ENABLE_LIBC_WASI != 0
  1319. /* Destroy wasi resource before freeing app heap, since some fields of
  1320. wasi contex are allocated from app heap, and if app heap is freed,
  1321. these fields will be set to NULL, we cannot free their internal data
  1322. which may allocated from global heap. */
  1323. /* Only destroy wasi ctx in the main module instance */
  1324. if (!is_sub_inst)
  1325. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1326. #endif
  1327. if (module_inst->memory_count > 0)
  1328. memories_deinstantiate(
  1329. module_inst,
  1330. module_inst->memories, module_inst->memory_count);
  1331. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1332. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1333. globals_deinstantiate(module_inst->globals);
  1334. export_functions_deinstantiate(module_inst->export_functions);
  1335. #if WASM_ENABLE_MULTI_MODULE != 0
  1336. export_globals_deinstantiate(module_inst->export_globals);
  1337. #endif
  1338. if (module_inst->global_data)
  1339. wasm_runtime_free(module_inst->global_data);
  1340. wasm_runtime_free(module_inst);
  1341. }
  1342. WASMFunctionInstance*
  1343. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1344. const char *name, const char *signature)
  1345. {
  1346. uint32 i;
  1347. for (i = 0; i < module_inst->export_func_count; i++)
  1348. if (!strcmp(module_inst->export_functions[i].name, name))
  1349. return module_inst->export_functions[i].function;
  1350. (void)signature;
  1351. return NULL;
  1352. }
  1353. #if WASM_ENABLE_MULTI_MODULE != 0
  1354. WASMGlobalInstance *
  1355. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1356. {
  1357. uint32 i;
  1358. for (i = 0; i < module_inst->export_glob_count; i++)
  1359. if (!strcmp(module_inst->export_globals[i].name, name))
  1360. return module_inst->export_globals[i].global;
  1361. return NULL;
  1362. }
  1363. WASMMemoryInstance *
  1364. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1365. {
  1366. /**
  1367. * using a strong assumption that one module instance only has
  1368. * one memory instance
  1369. */
  1370. (void)module_inst->export_memories;
  1371. return module_inst->memories[0];
  1372. }
  1373. WASMTableInstance *
  1374. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1375. {
  1376. /**
  1377. * using a strong assumption that one module instance only has
  1378. * one table instance
  1379. */
  1380. (void)module_inst->export_tables;
  1381. return module_inst->tables[0];
  1382. }
  1383. #endif
  1384. static bool
  1385. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1386. {
  1387. #if WASM_ENABLE_LIBC_WASI != 0
  1388. const char *exception = wasm_get_exception(module_inst);
  1389. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1390. /* The "wasi proc exit" exception is thrown by native lib to
  1391. let wasm app exit, which is a normal behavior, we clear
  1392. the exception here. */
  1393. wasm_set_exception(module_inst, NULL);
  1394. return true;
  1395. }
  1396. return false;
  1397. #else
  1398. return false;
  1399. #endif
  1400. }
  1401. bool
  1402. wasm_call_function(WASMExecEnv *exec_env,
  1403. WASMFunctionInstance *function,
  1404. unsigned argc, uint32 argv[])
  1405. {
  1406. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1407. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1408. (void)clear_wasi_proc_exit_exception(module_inst);
  1409. return !wasm_get_exception(module_inst) ? true : false;
  1410. }
  1411. bool
  1412. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1413. WASMFunctionInstance *func,
  1414. unsigned argc, uint32 argv[])
  1415. {
  1416. WASMExecEnv *exec_env;
  1417. bool ret;
  1418. if (!(exec_env = wasm_exec_env_create(
  1419. (WASMModuleInstanceCommon*)module_inst,
  1420. module_inst->default_wasm_stack_size))) {
  1421. wasm_set_exception(module_inst, "allocate memory failed");
  1422. return false;
  1423. }
  1424. /* set thread handle and stack boundary */
  1425. wasm_exec_env_set_thread_info(exec_env);
  1426. ret = wasm_call_function(exec_env, func, argc, argv);
  1427. wasm_exec_env_destroy(exec_env);
  1428. return ret;
  1429. }
  1430. void
  1431. wasm_set_exception(WASMModuleInstance *module_inst,
  1432. const char *exception)
  1433. {
  1434. if (exception)
  1435. snprintf(module_inst->cur_exception,
  1436. sizeof(module_inst->cur_exception),
  1437. "Exception: %s", exception);
  1438. else
  1439. module_inst->cur_exception[0] = '\0';
  1440. }
  1441. const char*
  1442. wasm_get_exception(WASMModuleInstance *module_inst)
  1443. {
  1444. if (module_inst->cur_exception[0] == '\0')
  1445. return NULL;
  1446. else
  1447. return module_inst->cur_exception;
  1448. }
  1449. uint32
  1450. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1451. void **p_native_addr)
  1452. {
  1453. WASMMemoryInstance *memory = module_inst->default_memory;
  1454. uint8 *addr = NULL;
  1455. uint32 offset = 0;
  1456. if (!memory) {
  1457. wasm_set_exception(module_inst, "uninitialized memory");
  1458. return 0;
  1459. }
  1460. if (memory->heap_handle) {
  1461. addr = mem_allocator_malloc(memory->heap_handle, size);
  1462. }
  1463. else if (module_inst->malloc_function
  1464. && module_inst->free_function) {
  1465. if (!execute_malloc_function(module_inst,
  1466. module_inst->malloc_function,
  1467. size, &offset)) {
  1468. return 0;
  1469. }
  1470. /* If we use app's malloc function,
  1471. the default memory may be changed while memory growing */
  1472. memory = module_inst->default_memory;
  1473. addr = offset ? memory->memory_data + offset : NULL;
  1474. }
  1475. if (!addr) {
  1476. wasm_set_exception(module_inst, "out of memory");
  1477. return 0;
  1478. }
  1479. if (p_native_addr)
  1480. *p_native_addr = addr;
  1481. return (uint32)(addr - memory->memory_data);
  1482. }
  1483. void
  1484. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1485. {
  1486. if (ptr) {
  1487. WASMMemoryInstance *memory = module_inst->default_memory;
  1488. uint8* addr;
  1489. if (!memory) {
  1490. return;
  1491. }
  1492. addr = memory->memory_data + ptr;
  1493. if (memory->heap_handle
  1494. && memory->heap_data <= addr
  1495. && addr < memory->heap_data_end) {
  1496. mem_allocator_free(memory->heap_handle, addr);
  1497. }
  1498. else if (module_inst->malloc_function
  1499. && module_inst->free_function
  1500. && memory->memory_data <= addr
  1501. && addr < memory->memory_data_end) {
  1502. execute_free_function(module_inst,
  1503. module_inst->free_function,
  1504. ptr);
  1505. }
  1506. }
  1507. }
  1508. uint32
  1509. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1510. const char *src, uint32 size)
  1511. {
  1512. char *buffer;
  1513. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1514. (void**)&buffer);
  1515. if (buffer_offset != 0) {
  1516. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1517. bh_memcpy_s(buffer, size, src, size);
  1518. }
  1519. return buffer_offset;
  1520. }
  1521. bool
  1522. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1523. uint32 app_offset, uint32 size)
  1524. {
  1525. WASMMemoryInstance *memory = module_inst->default_memory;
  1526. uint32 memory_data_size;
  1527. if (!memory) {
  1528. goto fail;
  1529. }
  1530. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1531. /* integer overflow check */
  1532. if (app_offset + size < app_offset) {
  1533. goto fail;
  1534. }
  1535. if (app_offset + size <= memory_data_size) {
  1536. return true;
  1537. }
  1538. fail:
  1539. wasm_set_exception(module_inst, "out of bounds memory access");
  1540. return false;
  1541. }
  1542. bool
  1543. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1544. void *native_ptr, uint32 size)
  1545. {
  1546. WASMMemoryInstance *memory = module_inst->default_memory;
  1547. uint8 *addr = (uint8 *)native_ptr;
  1548. if (!memory) {
  1549. goto fail;
  1550. }
  1551. /* integer overflow check */
  1552. if (addr + size < addr) {
  1553. goto fail;
  1554. }
  1555. if (memory->memory_data <= addr
  1556. && addr + size <= memory->memory_data_end) {
  1557. return true;
  1558. }
  1559. fail:
  1560. wasm_set_exception(module_inst, "out of bounds memory access");
  1561. return false;
  1562. }
  1563. void *
  1564. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1565. uint32 app_offset)
  1566. {
  1567. WASMMemoryInstance *memory = module_inst->default_memory;
  1568. uint8 *addr;
  1569. if (!memory)
  1570. return NULL;
  1571. addr = memory->memory_data + app_offset;
  1572. if (memory->memory_data <= addr
  1573. && addr < memory->memory_data_end)
  1574. return addr;
  1575. return NULL;
  1576. }
  1577. uint32
  1578. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1579. void *native_ptr)
  1580. {
  1581. WASMMemoryInstance *memory = module_inst->default_memory;
  1582. uint8 *addr = (uint8 *)native_ptr;
  1583. if (!memory)
  1584. return 0;
  1585. if (memory->memory_data <= addr
  1586. && addr < memory->memory_data_end)
  1587. return (uint32)(addr - memory->memory_data);
  1588. return 0;
  1589. }
  1590. bool
  1591. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1592. uint32 app_offset,
  1593. uint32 *p_app_start_offset,
  1594. uint32 *p_app_end_offset)
  1595. {
  1596. WASMMemoryInstance *memory = module_inst->default_memory;
  1597. uint32 memory_data_size;
  1598. if (!memory)
  1599. return false;
  1600. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1601. if (app_offset < memory_data_size) {
  1602. if (p_app_start_offset)
  1603. *p_app_start_offset = 0;
  1604. if (p_app_end_offset)
  1605. *p_app_end_offset = memory_data_size;
  1606. return true;
  1607. }
  1608. return false;
  1609. }
  1610. bool
  1611. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1612. uint8 *native_ptr,
  1613. uint8 **p_native_start_addr,
  1614. uint8 **p_native_end_addr)
  1615. {
  1616. WASMMemoryInstance *memory = module_inst->default_memory;
  1617. uint8 *addr = (uint8 *)native_ptr;
  1618. if (!memory)
  1619. return false;
  1620. if (memory->memory_data <= addr
  1621. && addr < memory->memory_data_end) {
  1622. if (p_native_start_addr)
  1623. *p_native_start_addr = memory->memory_data;
  1624. if (p_native_end_addr)
  1625. *p_native_end_addr = memory->memory_data_end;
  1626. return true;
  1627. }
  1628. return false;
  1629. }
  1630. bool
  1631. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1632. {
  1633. WASMMemoryInstance *memory = module->default_memory;
  1634. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1635. uint32 heap_size, total_size_old, total_page_count;
  1636. uint64 total_size;
  1637. if (!memory)
  1638. return false;
  1639. memory_data = memory->memory_data;
  1640. heap_size = memory->heap_data_end - memory->heap_data;
  1641. total_size_old = memory->memory_data_end - memory_data;
  1642. total_page_count = inc_page_count + memory->cur_page_count;
  1643. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1644. heap_data_old = memory->heap_data;
  1645. if (inc_page_count <= 0)
  1646. /* No need to enlarge memory */
  1647. return true;
  1648. if (total_page_count < memory->cur_page_count /* integer overflow */
  1649. || total_page_count > memory->max_page_count) {
  1650. return false;
  1651. }
  1652. if (total_size >= UINT32_MAX) {
  1653. return false;
  1654. }
  1655. #if WASM_ENABLE_SHARED_MEMORY != 0
  1656. if (memory->is_shared) {
  1657. /* For shared memory, we have reserved the maximum spaces during
  1658. instantiate, only change the cur_page_count here */
  1659. memory->cur_page_count = total_page_count;
  1660. return true;
  1661. }
  1662. #endif
  1663. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1664. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1665. return false;
  1666. }
  1667. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1668. memory_data, total_size_old);
  1669. wasm_runtime_free(memory_data);
  1670. }
  1671. memset(new_memory_data + total_size_old,
  1672. 0, (uint32)total_size - total_size_old);
  1673. if (heap_size > 0) {
  1674. if (mem_allocator_migrate(memory->heap_handle,
  1675. (char *)heap_data_old
  1676. + (new_memory_data - memory_data),
  1677. heap_size) != 0) {
  1678. return false;
  1679. }
  1680. }
  1681. memory->memory_data = new_memory_data;
  1682. memory->cur_page_count = total_page_count;
  1683. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1684. memory->heap_data_end = memory->heap_data + heap_size;
  1685. memory->memory_data_end = memory->memory_data
  1686. + memory->num_bytes_per_page
  1687. * total_page_count;
  1688. return true;
  1689. }
  1690. bool
  1691. wasm_call_indirect(WASMExecEnv *exec_env,
  1692. uint32_t element_indices,
  1693. uint32_t argc, uint32_t argv[])
  1694. {
  1695. WASMModuleInstance *module_inst = NULL;
  1696. WASMTableInstance *table_inst = NULL;
  1697. uint32_t function_indices = 0;
  1698. WASMFunctionInstance *function_inst = NULL;
  1699. module_inst =
  1700. (WASMModuleInstance*)exec_env->module_inst;
  1701. bh_assert(module_inst);
  1702. table_inst = module_inst->default_table;
  1703. if (!table_inst) {
  1704. wasm_set_exception(module_inst, "unknown table");
  1705. goto got_exception;
  1706. }
  1707. if (element_indices >= table_inst->cur_size) {
  1708. wasm_set_exception(module_inst, "undefined element");
  1709. goto got_exception;
  1710. }
  1711. /**
  1712. * please be aware that table_inst->base_addr may point
  1713. * to another module's table
  1714. **/
  1715. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1716. if (function_indices == 0xFFFFFFFF) {
  1717. wasm_set_exception(module_inst, "uninitialized element");
  1718. goto got_exception;
  1719. }
  1720. /**
  1721. * we insist to call functions owned by the module itself
  1722. **/
  1723. if (function_indices >= module_inst->function_count) {
  1724. wasm_set_exception(module_inst, "unknown function");
  1725. goto got_exception;
  1726. }
  1727. function_inst = module_inst->functions + function_indices;
  1728. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1729. (void)clear_wasi_proc_exit_exception(module_inst);
  1730. return !wasm_get_exception(module_inst) ? true : false;
  1731. got_exception:
  1732. return false;
  1733. }
  1734. #if WASM_ENABLE_THREAD_MGR != 0
  1735. bool
  1736. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1737. uint32 start_offset, uint32 size)
  1738. {
  1739. WASMModuleInstance *module_inst =
  1740. (WASMModuleInstance*)exec_env->module_inst;
  1741. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1742. uint32 data_end = module_inst->module->aux_data_end;
  1743. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1744. bool is_stack_before_data =
  1745. stack_bottom < data_end ? true : false;
  1746. /* Check the aux stack space, currently we don't allocate space in heap */
  1747. if ((is_stack_before_data && (size > start_offset))
  1748. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1749. return false;
  1750. if (stack_top_idx != (uint32)-1) {
  1751. /* The aux stack top is a wasm global,
  1752. set the initial value for the global */
  1753. uint8 *global_addr =
  1754. module_inst->global_data +
  1755. module_inst->globals[stack_top_idx].data_offset;
  1756. *(int32*)global_addr = start_offset;
  1757. /* The aux stack boundary is a constant value,
  1758. set the value to exec_env */
  1759. exec_env->aux_stack_boundary = start_offset - size;
  1760. return true;
  1761. }
  1762. return false;
  1763. }
  1764. bool
  1765. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1766. uint32 *start_offset, uint32 *size)
  1767. {
  1768. WASMModuleInstance *module_inst =
  1769. (WASMModuleInstance*)exec_env->module_inst;
  1770. /* The aux stack information is resolved in loader
  1771. and store in module */
  1772. uint32 stack_bottom =
  1773. module_inst->module->aux_stack_bottom;
  1774. uint32 total_aux_stack_size =
  1775. module_inst->module->aux_stack_size;
  1776. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  1777. if (start_offset)
  1778. *start_offset = stack_bottom;
  1779. if (size)
  1780. *size = total_aux_stack_size;
  1781. return true;
  1782. }
  1783. return false;
  1784. }
  1785. #endif
  1786. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1787. void
  1788. wasm_get_module_mem_consumption(const WASMModule *module,
  1789. WASMModuleMemConsumption *mem_conspn)
  1790. {
  1791. uint32 i, size;
  1792. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1793. mem_conspn->module_struct_size = sizeof(WASMModule);
  1794. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  1795. for (i = 0; i < module->type_count; i++) {
  1796. WASMType *type = module->types[i];
  1797. size = offsetof(WASMType, types) +
  1798. sizeof(uint8) * (type->param_count + type->result_count);
  1799. mem_conspn->types_size += size;
  1800. }
  1801. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  1802. mem_conspn->functions_size = sizeof(WASMFunction *)
  1803. * module->function_count;
  1804. for (i = 0; i < module->function_count; i++) {
  1805. WASMFunction *func = module->functions[i];
  1806. WASMType *type = func->func_type;
  1807. size = sizeof(WASMFunction) + func->local_count
  1808. + sizeof(uint16) * (type->param_count + func->local_count);
  1809. #if WASM_ENABLE_FAST_INTERP != 0
  1810. size += func->code_compiled_size
  1811. + sizeof(uint32) * func->const_cell_num;
  1812. #endif
  1813. mem_conspn->functions_size += size;
  1814. }
  1815. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  1816. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  1817. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  1818. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  1819. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  1820. * module->table_seg_count;
  1821. for (i = 0; i < module->table_seg_count; i++) {
  1822. WASMTableSeg *table_seg = &module->table_segments[i];
  1823. mem_conspn->tables_size += sizeof(uint32)
  1824. * table_seg->function_count;
  1825. }
  1826. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  1827. * module->data_seg_count;
  1828. for (i = 0; i < module->data_seg_count; i++) {
  1829. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  1830. }
  1831. if (module->const_str_list) {
  1832. StringNode *node = module->const_str_list, *node_next;
  1833. while (node) {
  1834. node_next = node->next;
  1835. mem_conspn->const_strs_size += sizeof(StringNode)
  1836. + strlen(node->str) + 1;
  1837. node = node_next;
  1838. }
  1839. }
  1840. mem_conspn->total_size += mem_conspn->module_struct_size;
  1841. mem_conspn->total_size += mem_conspn->types_size;
  1842. mem_conspn->total_size += mem_conspn->imports_size;
  1843. mem_conspn->total_size += mem_conspn->functions_size;
  1844. mem_conspn->total_size += mem_conspn->tables_size;
  1845. mem_conspn->total_size += mem_conspn->memories_size;
  1846. mem_conspn->total_size += mem_conspn->globals_size;
  1847. mem_conspn->total_size += mem_conspn->exports_size;
  1848. mem_conspn->total_size += mem_conspn->table_segs_size;
  1849. mem_conspn->total_size += mem_conspn->data_segs_size;
  1850. mem_conspn->total_size += mem_conspn->const_strs_size;
  1851. #if WASM_ENABLE_AOT != 0
  1852. mem_conspn->total_size += mem_conspn->aot_code_size;
  1853. #endif
  1854. }
  1855. void
  1856. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  1857. WASMModuleInstMemConsumption *mem_conspn)
  1858. {
  1859. uint32 i, size;
  1860. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1861. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  1862. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  1863. * module_inst->memory_count;
  1864. for (i = 0; i < module_inst->memory_count; i++) {
  1865. WASMMemoryInstance *memory = module_inst->memories[i];
  1866. size = sizeof(WASMMemoryInstance)
  1867. + memory->num_bytes_per_page * memory->cur_page_count;
  1868. mem_conspn->memories_size += size;
  1869. mem_conspn->app_heap_size += memory->heap_data_end
  1870. - memory->heap_data;
  1871. /* size of app heap structure */
  1872. mem_conspn->memories_size +=
  1873. mem_allocator_get_heap_struct_size();
  1874. }
  1875. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  1876. * module_inst->table_count;
  1877. for (i = 0; i < module_inst->table_count; i++) {
  1878. WASMTableInstance *table = module_inst->tables[i];
  1879. size = offsetof(WASMTableInstance, base_addr)
  1880. + sizeof(uint32) * table->cur_size;
  1881. mem_conspn->tables_size += size;
  1882. }
  1883. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  1884. * module_inst->function_count;
  1885. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  1886. * module_inst->global_count;
  1887. if (module_inst->global_count > 0) {
  1888. WASMGlobalInstance *global =
  1889. &module_inst->globals[module_inst->global_count - 1];
  1890. mem_conspn->globals_size += global->data_offset
  1891. + wasm_value_type_size(global->type);
  1892. }
  1893. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  1894. * module_inst->export_func_count;
  1895. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  1896. mem_conspn->total_size += mem_conspn->memories_size;
  1897. mem_conspn->total_size += mem_conspn->functions_size;
  1898. mem_conspn->total_size += mem_conspn->tables_size;
  1899. mem_conspn->total_size += mem_conspn->globals_size;
  1900. mem_conspn->total_size += mem_conspn->exports_size;
  1901. }
  1902. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  1903. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1904. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1905. void
  1906. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  1907. {
  1908. WASMModuleInstance *module_inst =
  1909. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  1910. WASMInterpFrame *cur_frame =
  1911. wasm_exec_env_get_cur_frame(exec_env);
  1912. WASMFunctionInstance *func_inst;
  1913. const char *func_name = NULL;
  1914. uint32 n;
  1915. os_printf("\n");
  1916. for (n = 0; cur_frame && cur_frame->function; n++) {
  1917. func_inst = cur_frame->function;
  1918. if (func_inst->is_import_func) {
  1919. func_name = func_inst->u.func_import->field_name;
  1920. }
  1921. else {
  1922. func_name = func_inst->u.func->field_name;
  1923. }
  1924. /* function name not exported, print number instead */
  1925. if (func_name == NULL) {
  1926. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  1927. }
  1928. else {
  1929. os_printf("#%02d %s \n", n, func_name);
  1930. }
  1931. cur_frame = cur_frame->prev_frame;
  1932. }
  1933. os_printf("\n");
  1934. }
  1935. #endif /* end of WASM_ENABLE_CUSTOM_NAME_SECTION */