wasm_runtime.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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. WASMFunctionInstance *retain_func,
  864. uint32 size, uint32 *p_result)
  865. {
  866. uint32 argv[2], argc;
  867. bool ret;
  868. argv[0] = size;
  869. argc = 1;
  870. /* if __retain is exported, then this module is compiled by
  871. assemblyscript, the memory should be managed by as's runtime,
  872. in this case we need to call the retain function after malloc
  873. the memory */
  874. if (retain_func) {
  875. /* the malloc functino from assemblyscript is:
  876. function __new(size: usize, id: u32)
  877. id = 0 means this is an ArrayBuffer object */
  878. argv[1] = 0;
  879. argc = 2;
  880. }
  881. ret = wasm_create_exec_env_and_call_function
  882. (module_inst, malloc_func, argc, argv);
  883. if (retain_func && ret) {
  884. ret = wasm_create_exec_env_and_call_function
  885. (module_inst, retain_func, 1, argv);
  886. }
  887. if (ret)
  888. *p_result = argv[0];
  889. return ret;
  890. }
  891. static bool
  892. execute_free_function(WASMModuleInstance *module_inst,
  893. WASMFunctionInstance *free_func,
  894. uint32 offset)
  895. {
  896. uint32 argv[2];
  897. argv[0] = offset;
  898. return wasm_create_exec_env_and_call_function
  899. (module_inst, free_func, 1, argv);
  900. }
  901. #if WASM_ENABLE_MULTI_MODULE != 0
  902. static bool
  903. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  904. uint32 stack_size, uint32 heap_size, char *error_buf,
  905. uint32 error_buf_size)
  906. {
  907. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  908. WASMRegisteredModule *sub_module_list_node =
  909. bh_list_first_elem(module->import_module_list);
  910. while (sub_module_list_node) {
  911. WASMSubModInstNode *sub_module_inst_list_node;
  912. WASMModule *sub_module = (WASMModule*)sub_module_list_node->module;
  913. WASMModuleInstance *sub_module_inst =
  914. wasm_instantiate(sub_module, false, stack_size, heap_size,
  915. error_buf, error_buf_size);
  916. if (!sub_module_inst) {
  917. LOG_DEBUG("instantiate %s failed",
  918. sub_module_list_node->module_name);
  919. return false;
  920. }
  921. sub_module_inst_list_node = runtime_malloc
  922. (sizeof(WASMSubModInstNode), error_buf, error_buf_size);
  923. if (!sub_module_inst_list_node) {
  924. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  925. sizeof(WASMSubModInstNode));
  926. wasm_deinstantiate(sub_module_inst, false);
  927. return false;
  928. }
  929. sub_module_inst_list_node->module_inst = sub_module_inst;
  930. sub_module_inst_list_node->module_name =
  931. sub_module_list_node->module_name;
  932. bh_list_status ret =
  933. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  934. bh_assert(BH_LIST_SUCCESS == ret);
  935. (void)ret;
  936. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  937. }
  938. return true;
  939. }
  940. static void
  941. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  942. {
  943. bh_list *list = module_inst->sub_module_inst_list;
  944. WASMSubModInstNode *node = bh_list_first_elem(list);
  945. while (node) {
  946. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  947. bh_list_remove(list, node);
  948. wasm_deinstantiate(node->module_inst, false);
  949. wasm_runtime_free(node);
  950. node = next_node;
  951. }
  952. }
  953. #endif
  954. static bool
  955. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  956. uint32 error_buf_size)
  957. {
  958. WASMModule *module = module_inst->module;
  959. uint32 i;
  960. for (i = 0; i < module->import_function_count; i++) {
  961. WASMFunctionImport *func =
  962. &((module->import_functions + i)->u.function);
  963. if (!func->func_ptr_linked
  964. #if WASM_ENABLE_MULTI_MODULE != 0
  965. && !func->import_func_linked
  966. #endif
  967. ) {
  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_WARNING("warning: fail to link import function (%s, %s)",
  975. func->module_name, func->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. for (i = 0; i < module->import_global_count; i++) {
  983. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  984. if (!global->is_linked) {
  985. #if WASM_ENABLE_SPEC_TEST != 0
  986. set_error_buf(error_buf, error_buf_size,
  987. "unknown import or incompatible import type");
  988. return false;
  989. #else
  990. #if WASM_ENABLE_WAMR_COMPILER == 0
  991. LOG_DEBUG("warning: fail to link import global (%s, %s)",
  992. global->module_name, global->field_name);
  993. #else
  994. /* do nothing to avoid confused message */
  995. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  996. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  997. }
  998. }
  999. return true;
  1000. }
  1001. /**
  1002. * Instantiate module
  1003. */
  1004. WASMModuleInstance*
  1005. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  1006. uint32 stack_size, uint32 heap_size,
  1007. char *error_buf, uint32 error_buf_size)
  1008. {
  1009. WASMModuleInstance *module_inst;
  1010. WASMGlobalInstance *globals = NULL, *global;
  1011. uint32 global_count, global_data_size = 0, i;
  1012. uint32 base_offset, length;
  1013. uint8 *global_data, *global_data_end;
  1014. #if WASM_ENABLE_MULTI_MODULE != 0
  1015. bool ret = false;
  1016. #endif
  1017. if (!module)
  1018. return NULL;
  1019. /* Check heap size */
  1020. heap_size = align_uint(heap_size, 8);
  1021. if (heap_size > APP_HEAP_SIZE_MAX)
  1022. heap_size = APP_HEAP_SIZE_MAX;
  1023. /* Allocate the memory */
  1024. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  1025. error_buf, error_buf_size))) {
  1026. return NULL;
  1027. }
  1028. module_inst->module = module;
  1029. #if WASM_ENABLE_MULTI_MODULE != 0
  1030. module_inst->sub_module_inst_list =
  1031. &module_inst->sub_module_inst_list_head;
  1032. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1033. error_buf, error_buf_size);
  1034. if (!ret) {
  1035. LOG_DEBUG("build a sub module list failed");
  1036. goto fail;
  1037. }
  1038. #endif
  1039. /* Instantiate global firstly to get the mutable data size */
  1040. global_count = module->import_global_count + module->global_count;
  1041. if (global_count
  1042. && !(globals = globals_instantiate(module, module_inst,
  1043. &global_data_size,
  1044. error_buf, error_buf_size))) {
  1045. goto fail;
  1046. }
  1047. module_inst->global_count = global_count;
  1048. module_inst->globals = globals;
  1049. module_inst->memory_count =
  1050. module->import_memory_count + module->memory_count;
  1051. module_inst->table_count =
  1052. module->import_table_count + module->table_count;
  1053. module_inst->function_count =
  1054. module->import_function_count + module->function_count;
  1055. /* export */
  1056. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1057. #if WASM_ENABLE_MULTI_MODULE != 0
  1058. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1059. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1060. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1061. #endif
  1062. if (global_count > 0) {
  1063. if (!(module_inst->global_data = runtime_malloc
  1064. (global_data_size, error_buf, error_buf_size))) {
  1065. goto fail;
  1066. }
  1067. }
  1068. /* Instantiate memories/tables/functions */
  1069. if ((module_inst->memory_count > 0
  1070. && !(module_inst->memories =
  1071. memories_instantiate(module,
  1072. module_inst,
  1073. heap_size, error_buf, error_buf_size)))
  1074. || (module_inst->table_count > 0
  1075. && !(module_inst->tables =
  1076. tables_instantiate(module,
  1077. module_inst,
  1078. error_buf, error_buf_size)))
  1079. || (module_inst->function_count > 0
  1080. && !(module_inst->functions =
  1081. functions_instantiate(module,
  1082. module_inst,
  1083. error_buf, error_buf_size)))
  1084. || (module_inst->export_func_count > 0
  1085. && !(module_inst->export_functions = export_functions_instantiate(
  1086. module, module_inst, module_inst->export_func_count,
  1087. error_buf, error_buf_size)))
  1088. #if WASM_ENABLE_MULTI_MODULE != 0
  1089. || (module_inst->export_glob_count > 0
  1090. && !(module_inst->export_globals = export_globals_instantiate(
  1091. module, module_inst, module_inst->export_glob_count,
  1092. error_buf, error_buf_size)))
  1093. #endif
  1094. ) {
  1095. goto fail;
  1096. }
  1097. if (global_count > 0) {
  1098. /**
  1099. * since there might be some globals are not instantiate the first
  1100. * instantiate round
  1101. */
  1102. if (!globals_instantiate_fix(globals, module,
  1103. error_buf, error_buf_size)) {
  1104. goto fail;
  1105. }
  1106. /* Initialize the global data */
  1107. global_data = module_inst->global_data;
  1108. global_data_end = global_data + global_data_size;
  1109. global = globals;
  1110. for (i = 0; i < global_count; i++, global++) {
  1111. switch (global->type) {
  1112. case VALUE_TYPE_I32:
  1113. case VALUE_TYPE_F32:
  1114. *(int32*)global_data = global->initial_value.i32;
  1115. global_data += sizeof(int32);
  1116. break;
  1117. case VALUE_TYPE_I64:
  1118. case VALUE_TYPE_F64:
  1119. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1120. &global->initial_value.i64, sizeof(int64));
  1121. global_data += sizeof(int64);
  1122. break;
  1123. default:
  1124. bh_assert(0);
  1125. }
  1126. }
  1127. bh_assert(global_data == global_data_end);
  1128. }
  1129. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1130. goto fail;
  1131. }
  1132. /* Initialize the memory data with data segment section */
  1133. module_inst->default_memory =
  1134. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1135. for (i = 0; i < module->data_seg_count; i++) {
  1136. WASMMemoryInstance *memory = NULL;
  1137. uint8 *memory_data = NULL;
  1138. uint32 memory_size = 0;
  1139. WASMDataSeg *data_seg = module->data_segments[i];
  1140. #if WASM_ENABLE_BULK_MEMORY != 0
  1141. if (data_seg->is_passive)
  1142. continue;
  1143. #endif
  1144. /* has check it in loader */
  1145. memory = module_inst->memories[data_seg->memory_index];
  1146. bh_assert(memory);
  1147. memory_data = memory->memory_data;
  1148. bh_assert(memory_data);
  1149. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1150. bh_assert(data_seg->base_offset.init_expr_type
  1151. == INIT_EXPR_TYPE_I32_CONST
  1152. || data_seg->base_offset.init_expr_type
  1153. == INIT_EXPR_TYPE_GET_GLOBAL);
  1154. if (data_seg->base_offset.init_expr_type
  1155. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1156. if (!globals
  1157. || data_seg->base_offset.u.global_index >= global_count
  1158. || globals[data_seg->base_offset.u.global_index].type
  1159. != VALUE_TYPE_I32) {
  1160. set_error_buf(error_buf, error_buf_size,
  1161. "data segment does not fit");
  1162. goto fail;
  1163. }
  1164. data_seg->base_offset.u.i32 =
  1165. globals[data_seg->base_offset.u.global_index]
  1166. .initial_value.i32;
  1167. }
  1168. /* check offset since length might negative */
  1169. base_offset = (uint32)data_seg->base_offset.u.i32;
  1170. if (base_offset > memory_size) {
  1171. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1172. memory_size);
  1173. set_error_buf(error_buf, error_buf_size,
  1174. "data segment does not fit");
  1175. goto fail;
  1176. }
  1177. /* check offset + length(could be zero) */
  1178. length = data_seg->data_length;
  1179. if (base_offset + length > memory_size) {
  1180. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1181. base_offset, length, memory_size);
  1182. set_error_buf(error_buf, error_buf_size,
  1183. "data segment does not fit");
  1184. goto fail;
  1185. }
  1186. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1187. data_seg->data, length);
  1188. }
  1189. /* Initialize the table data with table segment section */
  1190. module_inst->default_table =
  1191. module_inst->table_count ? module_inst->tables[0] : NULL;
  1192. for (i = 0; i < module->table_seg_count; i++) {
  1193. WASMTableSeg *table_seg = module->table_segments + i;
  1194. /* has check it in loader */
  1195. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1196. bh_assert(table);
  1197. uint32 *table_data = (uint32 *)table->base_addr;
  1198. #if WASM_ENABLE_MULTI_MODULE != 0
  1199. table_data = table->table_inst_linked
  1200. ? (uint32 *)table->table_inst_linked->base_addr
  1201. : table_data;
  1202. #endif
  1203. bh_assert(table_data);
  1204. /* init vec(funcidx) */
  1205. bh_assert(table_seg->base_offset.init_expr_type
  1206. == INIT_EXPR_TYPE_I32_CONST
  1207. || table_seg->base_offset.init_expr_type
  1208. == INIT_EXPR_TYPE_GET_GLOBAL);
  1209. if (table_seg->base_offset.init_expr_type
  1210. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1211. if (!globals
  1212. || table_seg->base_offset.u.global_index >= global_count
  1213. || globals[table_seg->base_offset.u.global_index].type
  1214. != VALUE_TYPE_I32) {
  1215. set_error_buf(error_buf, error_buf_size,
  1216. "elements segment does not fit");
  1217. goto fail;
  1218. }
  1219. table_seg->base_offset.u.i32 =
  1220. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1221. }
  1222. /* check offset since length might negative */
  1223. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1224. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1225. table_seg->base_offset.u.i32, table->cur_size);
  1226. set_error_buf(error_buf, error_buf_size,
  1227. "elements segment does not fit");
  1228. goto fail;
  1229. }
  1230. /* check offset + length(could be zero) */
  1231. length = table_seg->function_count;
  1232. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1233. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1234. table_seg->base_offset.u.i32, length, table->cur_size);
  1235. set_error_buf(error_buf, error_buf_size,
  1236. "elements segment does not fit");
  1237. goto fail;
  1238. }
  1239. /**
  1240. * Check function index in the current module inst for now.
  1241. * will check the linked table inst owner in future.
  1242. * so loader check is enough
  1243. */
  1244. bh_memcpy_s(
  1245. table_data + table_seg->base_offset.u.i32,
  1246. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1247. * sizeof(uint32)),
  1248. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1249. }
  1250. /* module instance type */
  1251. module_inst->module_type = Wasm_Module_Bytecode;
  1252. /* Initialize the thread related data */
  1253. if (stack_size == 0)
  1254. stack_size = DEFAULT_WASM_STACK_SIZE;
  1255. #if WASM_ENABLE_SPEC_TEST != 0
  1256. if (stack_size < 48 *1024)
  1257. stack_size = 48 * 1024;
  1258. #endif
  1259. module_inst->default_wasm_stack_size = stack_size;
  1260. if (module->malloc_function != (uint32)-1) {
  1261. module_inst->malloc_function =
  1262. &module_inst->functions[module->malloc_function];
  1263. }
  1264. if (module->free_function != (uint32)-1) {
  1265. module_inst->free_function =
  1266. &module_inst->functions[module->free_function];
  1267. }
  1268. if (module->retain_function != (uint32)-1) {
  1269. module_inst->retain_function =
  1270. &module_inst->functions[module->retain_function];
  1271. }
  1272. #if WASM_ENABLE_LIBC_WASI != 0
  1273. /* The sub-instance will get the wasi_ctx from main-instance */
  1274. if (!is_sub_inst) {
  1275. if (heap_size > 0
  1276. && !wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1277. module->wasi_args.dir_list,
  1278. module->wasi_args.dir_count,
  1279. module->wasi_args.map_dir_list,
  1280. module->wasi_args.map_dir_count,
  1281. module->wasi_args.env,
  1282. module->wasi_args.env_count,
  1283. module->wasi_args.argv,
  1284. module->wasi_args.argc,
  1285. error_buf, error_buf_size)) {
  1286. goto fail;
  1287. }
  1288. }
  1289. #endif
  1290. if (module->start_function != (uint32)-1) {
  1291. /* TODO: fix start function can be import function issue */
  1292. if (module->start_function >= module->import_function_count)
  1293. module_inst->start_function =
  1294. &module_inst->functions[module->start_function];
  1295. }
  1296. /* Execute __post_instantiate function */
  1297. if (!execute_post_inst_function(module_inst)
  1298. || !execute_start_function(module_inst)) {
  1299. set_error_buf(error_buf, error_buf_size,
  1300. module_inst->cur_exception);
  1301. goto fail;
  1302. }
  1303. #if WASM_ENABLE_BULK_MEMORY != 0
  1304. #if WASM_ENABLE_LIBC_WASI != 0
  1305. if (!module->is_wasi_module) {
  1306. #endif
  1307. /* Only execute the memory init function for main instance because
  1308. the data segments will be dropped once initialized.
  1309. */
  1310. if (!is_sub_inst) {
  1311. if (!execute_memory_init_function(module_inst)) {
  1312. set_error_buf(error_buf, error_buf_size,
  1313. module_inst->cur_exception);
  1314. goto fail;
  1315. }
  1316. }
  1317. #if WASM_ENABLE_LIBC_WASI != 0
  1318. }
  1319. #endif
  1320. #endif
  1321. #if WASM_ENABLE_MEMORY_TRACING != 0
  1322. wasm_runtime_dump_module_inst_mem_consumption
  1323. ((WASMModuleInstanceCommon *)module_inst);
  1324. #endif
  1325. (void)global_data_end;
  1326. return module_inst;
  1327. fail:
  1328. wasm_deinstantiate(module_inst, false);
  1329. return NULL;
  1330. }
  1331. void
  1332. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1333. {
  1334. if (!module_inst)
  1335. return;
  1336. #if WASM_ENABLE_MULTI_MODULE != 0
  1337. sub_module_deinstantiate(module_inst);
  1338. #endif
  1339. #if WASM_ENABLE_LIBC_WASI != 0
  1340. /* Destroy wasi resource before freeing app heap, since some fields of
  1341. wasi contex are allocated from app heap, and if app heap is freed,
  1342. these fields will be set to NULL, we cannot free their internal data
  1343. which may allocated from global heap. */
  1344. /* Only destroy wasi ctx in the main module instance */
  1345. if (!is_sub_inst)
  1346. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1347. #endif
  1348. if (module_inst->memory_count > 0)
  1349. memories_deinstantiate(
  1350. module_inst,
  1351. module_inst->memories, module_inst->memory_count);
  1352. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1353. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1354. globals_deinstantiate(module_inst->globals);
  1355. export_functions_deinstantiate(module_inst->export_functions);
  1356. #if WASM_ENABLE_MULTI_MODULE != 0
  1357. export_globals_deinstantiate(module_inst->export_globals);
  1358. #endif
  1359. if (module_inst->global_data)
  1360. wasm_runtime_free(module_inst->global_data);
  1361. wasm_runtime_free(module_inst);
  1362. }
  1363. WASMFunctionInstance*
  1364. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1365. const char *name, const char *signature)
  1366. {
  1367. uint32 i;
  1368. for (i = 0; i < module_inst->export_func_count; i++)
  1369. if (!strcmp(module_inst->export_functions[i].name, name))
  1370. return module_inst->export_functions[i].function;
  1371. (void)signature;
  1372. return NULL;
  1373. }
  1374. #if WASM_ENABLE_MULTI_MODULE != 0
  1375. WASMGlobalInstance *
  1376. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1377. {
  1378. uint32 i;
  1379. for (i = 0; i < module_inst->export_glob_count; i++)
  1380. if (!strcmp(module_inst->export_globals[i].name, name))
  1381. return module_inst->export_globals[i].global;
  1382. return NULL;
  1383. }
  1384. WASMMemoryInstance *
  1385. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1386. {
  1387. /**
  1388. * using a strong assumption that one module instance only has
  1389. * one memory instance
  1390. */
  1391. (void)module_inst->export_memories;
  1392. return module_inst->memories[0];
  1393. }
  1394. WASMTableInstance *
  1395. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1396. {
  1397. /**
  1398. * using a strong assumption that one module instance only has
  1399. * one table instance
  1400. */
  1401. (void)module_inst->export_tables;
  1402. return module_inst->tables[0];
  1403. }
  1404. #endif
  1405. static bool
  1406. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1407. {
  1408. #if WASM_ENABLE_LIBC_WASI != 0
  1409. const char *exception = wasm_get_exception(module_inst);
  1410. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1411. /* The "wasi proc exit" exception is thrown by native lib to
  1412. let wasm app exit, which is a normal behavior, we clear
  1413. the exception here. */
  1414. wasm_set_exception(module_inst, NULL);
  1415. return true;
  1416. }
  1417. return false;
  1418. #else
  1419. return false;
  1420. #endif
  1421. }
  1422. bool
  1423. wasm_call_function(WASMExecEnv *exec_env,
  1424. WASMFunctionInstance *function,
  1425. unsigned argc, uint32 argv[])
  1426. {
  1427. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1428. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1429. (void)clear_wasi_proc_exit_exception(module_inst);
  1430. return !wasm_get_exception(module_inst) ? true : false;
  1431. }
  1432. bool
  1433. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1434. WASMFunctionInstance *func,
  1435. unsigned argc, uint32 argv[])
  1436. {
  1437. WASMExecEnv *exec_env;
  1438. bool ret;
  1439. if (!(exec_env = wasm_exec_env_create(
  1440. (WASMModuleInstanceCommon*)module_inst,
  1441. module_inst->default_wasm_stack_size))) {
  1442. wasm_set_exception(module_inst, "allocate memory failed");
  1443. return false;
  1444. }
  1445. /* set thread handle and stack boundary */
  1446. wasm_exec_env_set_thread_info(exec_env);
  1447. ret = wasm_call_function(exec_env, func, argc, argv);
  1448. wasm_exec_env_destroy(exec_env);
  1449. return ret;
  1450. }
  1451. void
  1452. wasm_set_exception(WASMModuleInstance *module_inst,
  1453. const char *exception)
  1454. {
  1455. if (exception)
  1456. snprintf(module_inst->cur_exception,
  1457. sizeof(module_inst->cur_exception),
  1458. "Exception: %s", exception);
  1459. else
  1460. module_inst->cur_exception[0] = '\0';
  1461. }
  1462. const char*
  1463. wasm_get_exception(WASMModuleInstance *module_inst)
  1464. {
  1465. if (module_inst->cur_exception[0] == '\0')
  1466. return NULL;
  1467. else
  1468. return module_inst->cur_exception;
  1469. }
  1470. uint32
  1471. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1472. void **p_native_addr)
  1473. {
  1474. WASMMemoryInstance *memory = module_inst->default_memory;
  1475. uint8 *addr = NULL;
  1476. uint32 offset = 0;
  1477. if (!memory) {
  1478. wasm_set_exception(module_inst, "uninitialized memory");
  1479. return 0;
  1480. }
  1481. if (memory->heap_handle) {
  1482. addr = mem_allocator_malloc(memory->heap_handle, size);
  1483. }
  1484. else if (module_inst->malloc_function
  1485. && module_inst->free_function) {
  1486. if (!execute_malloc_function(module_inst,
  1487. module_inst->malloc_function,
  1488. module_inst->retain_function,
  1489. size, &offset)) {
  1490. return 0;
  1491. }
  1492. /* If we use app's malloc function,
  1493. the default memory may be changed while memory growing */
  1494. memory = module_inst->default_memory;
  1495. addr = offset ? memory->memory_data + offset : NULL;
  1496. }
  1497. if (!addr) {
  1498. wasm_set_exception(module_inst, "out of memory");
  1499. return 0;
  1500. }
  1501. if (p_native_addr)
  1502. *p_native_addr = addr;
  1503. return (uint32)(addr - memory->memory_data);
  1504. }
  1505. void
  1506. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1507. {
  1508. if (ptr) {
  1509. WASMMemoryInstance *memory = module_inst->default_memory;
  1510. uint8* addr;
  1511. if (!memory) {
  1512. return;
  1513. }
  1514. addr = memory->memory_data + ptr;
  1515. if (memory->heap_handle
  1516. && memory->heap_data <= addr
  1517. && addr < memory->heap_data_end) {
  1518. mem_allocator_free(memory->heap_handle, addr);
  1519. }
  1520. else if (module_inst->malloc_function
  1521. && module_inst->free_function
  1522. && memory->memory_data <= addr
  1523. && addr < memory->memory_data_end) {
  1524. execute_free_function(module_inst,
  1525. module_inst->free_function,
  1526. ptr);
  1527. }
  1528. }
  1529. }
  1530. uint32
  1531. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1532. const char *src, uint32 size)
  1533. {
  1534. char *buffer;
  1535. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1536. (void**)&buffer);
  1537. if (buffer_offset != 0) {
  1538. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1539. bh_memcpy_s(buffer, size, src, size);
  1540. }
  1541. return buffer_offset;
  1542. }
  1543. bool
  1544. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1545. uint32 app_offset, uint32 size)
  1546. {
  1547. WASMMemoryInstance *memory = module_inst->default_memory;
  1548. uint32 memory_data_size;
  1549. if (!memory) {
  1550. goto fail;
  1551. }
  1552. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1553. /* integer overflow check */
  1554. if (app_offset + size < app_offset) {
  1555. goto fail;
  1556. }
  1557. if (app_offset + size <= memory_data_size) {
  1558. return true;
  1559. }
  1560. fail:
  1561. wasm_set_exception(module_inst, "out of bounds memory access");
  1562. return false;
  1563. }
  1564. bool
  1565. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1566. void *native_ptr, uint32 size)
  1567. {
  1568. WASMMemoryInstance *memory = module_inst->default_memory;
  1569. uint8 *addr = (uint8 *)native_ptr;
  1570. if (!memory) {
  1571. goto fail;
  1572. }
  1573. /* integer overflow check */
  1574. if (addr + size < addr) {
  1575. goto fail;
  1576. }
  1577. if (memory->memory_data <= addr
  1578. && addr + size <= memory->memory_data_end) {
  1579. return true;
  1580. }
  1581. fail:
  1582. wasm_set_exception(module_inst, "out of bounds memory access");
  1583. return false;
  1584. }
  1585. void *
  1586. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1587. uint32 app_offset)
  1588. {
  1589. WASMMemoryInstance *memory = module_inst->default_memory;
  1590. uint8 *addr;
  1591. if (!memory)
  1592. return NULL;
  1593. addr = memory->memory_data + app_offset;
  1594. if (memory->memory_data <= addr
  1595. && addr < memory->memory_data_end)
  1596. return addr;
  1597. return NULL;
  1598. }
  1599. uint32
  1600. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1601. void *native_ptr)
  1602. {
  1603. WASMMemoryInstance *memory = module_inst->default_memory;
  1604. uint8 *addr = (uint8 *)native_ptr;
  1605. if (!memory)
  1606. return 0;
  1607. if (memory->memory_data <= addr
  1608. && addr < memory->memory_data_end)
  1609. return (uint32)(addr - memory->memory_data);
  1610. return 0;
  1611. }
  1612. bool
  1613. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1614. uint32 app_offset,
  1615. uint32 *p_app_start_offset,
  1616. uint32 *p_app_end_offset)
  1617. {
  1618. WASMMemoryInstance *memory = module_inst->default_memory;
  1619. uint32 memory_data_size;
  1620. if (!memory)
  1621. return false;
  1622. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1623. if (app_offset < memory_data_size) {
  1624. if (p_app_start_offset)
  1625. *p_app_start_offset = 0;
  1626. if (p_app_end_offset)
  1627. *p_app_end_offset = memory_data_size;
  1628. return true;
  1629. }
  1630. return false;
  1631. }
  1632. bool
  1633. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1634. uint8 *native_ptr,
  1635. uint8 **p_native_start_addr,
  1636. uint8 **p_native_end_addr)
  1637. {
  1638. WASMMemoryInstance *memory = module_inst->default_memory;
  1639. uint8 *addr = (uint8 *)native_ptr;
  1640. if (!memory)
  1641. return false;
  1642. if (memory->memory_data <= addr
  1643. && addr < memory->memory_data_end) {
  1644. if (p_native_start_addr)
  1645. *p_native_start_addr = memory->memory_data;
  1646. if (p_native_end_addr)
  1647. *p_native_end_addr = memory->memory_data_end;
  1648. return true;
  1649. }
  1650. return false;
  1651. }
  1652. bool
  1653. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1654. {
  1655. WASMMemoryInstance *memory = module->default_memory;
  1656. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1657. uint32 heap_size, total_size_old, total_page_count;
  1658. uint64 total_size;
  1659. if (!memory)
  1660. return false;
  1661. memory_data = memory->memory_data;
  1662. heap_size = memory->heap_data_end - memory->heap_data;
  1663. total_size_old = memory->memory_data_end - memory_data;
  1664. total_page_count = inc_page_count + memory->cur_page_count;
  1665. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1666. heap_data_old = memory->heap_data;
  1667. if (inc_page_count <= 0)
  1668. /* No need to enlarge memory */
  1669. return true;
  1670. if (total_page_count < memory->cur_page_count /* integer overflow */
  1671. || total_page_count > memory->max_page_count) {
  1672. return false;
  1673. }
  1674. if (total_size >= UINT32_MAX) {
  1675. return false;
  1676. }
  1677. #if WASM_ENABLE_SHARED_MEMORY != 0
  1678. if (memory->is_shared) {
  1679. /* For shared memory, we have reserved the maximum spaces during
  1680. instantiate, only change the cur_page_count here */
  1681. memory->cur_page_count = total_page_count;
  1682. return true;
  1683. }
  1684. #endif
  1685. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1686. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1687. return false;
  1688. }
  1689. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1690. memory_data, total_size_old);
  1691. wasm_runtime_free(memory_data);
  1692. }
  1693. memset(new_memory_data + total_size_old,
  1694. 0, (uint32)total_size - total_size_old);
  1695. if (heap_size > 0) {
  1696. if (mem_allocator_migrate(memory->heap_handle,
  1697. (char *)heap_data_old
  1698. + (new_memory_data - memory_data),
  1699. heap_size) != 0) {
  1700. return false;
  1701. }
  1702. }
  1703. memory->memory_data = new_memory_data;
  1704. memory->cur_page_count = total_page_count;
  1705. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1706. memory->heap_data_end = memory->heap_data + heap_size;
  1707. memory->memory_data_end = memory->memory_data
  1708. + memory->num_bytes_per_page
  1709. * total_page_count;
  1710. return true;
  1711. }
  1712. bool
  1713. wasm_call_indirect(WASMExecEnv *exec_env,
  1714. uint32_t element_indices,
  1715. uint32_t argc, uint32_t argv[])
  1716. {
  1717. WASMModuleInstance *module_inst = NULL;
  1718. WASMTableInstance *table_inst = NULL;
  1719. uint32_t function_indices = 0;
  1720. WASMFunctionInstance *function_inst = NULL;
  1721. module_inst =
  1722. (WASMModuleInstance*)exec_env->module_inst;
  1723. bh_assert(module_inst);
  1724. table_inst = module_inst->default_table;
  1725. if (!table_inst) {
  1726. wasm_set_exception(module_inst, "unknown table");
  1727. goto got_exception;
  1728. }
  1729. if (element_indices >= table_inst->cur_size) {
  1730. wasm_set_exception(module_inst, "undefined element");
  1731. goto got_exception;
  1732. }
  1733. /**
  1734. * please be aware that table_inst->base_addr may point
  1735. * to another module's table
  1736. **/
  1737. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1738. if (function_indices == 0xFFFFFFFF) {
  1739. wasm_set_exception(module_inst, "uninitialized element");
  1740. goto got_exception;
  1741. }
  1742. /**
  1743. * we insist to call functions owned by the module itself
  1744. **/
  1745. if (function_indices >= module_inst->function_count) {
  1746. wasm_set_exception(module_inst, "unknown function");
  1747. goto got_exception;
  1748. }
  1749. function_inst = module_inst->functions + function_indices;
  1750. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1751. (void)clear_wasi_proc_exit_exception(module_inst);
  1752. return !wasm_get_exception(module_inst) ? true : false;
  1753. got_exception:
  1754. return false;
  1755. }
  1756. #if WASM_ENABLE_THREAD_MGR != 0
  1757. bool
  1758. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1759. uint32 start_offset, uint32 size)
  1760. {
  1761. WASMModuleInstance *module_inst =
  1762. (WASMModuleInstance*)exec_env->module_inst;
  1763. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1764. uint32 data_end = module_inst->module->aux_data_end;
  1765. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1766. bool is_stack_before_data =
  1767. stack_bottom < data_end ? true : false;
  1768. /* Check the aux stack space, currently we don't allocate space in heap */
  1769. if ((is_stack_before_data && (size > start_offset))
  1770. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1771. return false;
  1772. if (stack_top_idx != (uint32)-1) {
  1773. /* The aux stack top is a wasm global,
  1774. set the initial value for the global */
  1775. uint8 *global_addr =
  1776. module_inst->global_data +
  1777. module_inst->globals[stack_top_idx].data_offset;
  1778. *(int32*)global_addr = start_offset;
  1779. /* The aux stack boundary is a constant value,
  1780. set the value to exec_env */
  1781. exec_env->aux_stack_boundary = start_offset - size;
  1782. return true;
  1783. }
  1784. return false;
  1785. }
  1786. bool
  1787. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1788. uint32 *start_offset, uint32 *size)
  1789. {
  1790. WASMModuleInstance *module_inst =
  1791. (WASMModuleInstance*)exec_env->module_inst;
  1792. /* The aux stack information is resolved in loader
  1793. and store in module */
  1794. uint32 stack_bottom =
  1795. module_inst->module->aux_stack_bottom;
  1796. uint32 total_aux_stack_size =
  1797. module_inst->module->aux_stack_size;
  1798. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  1799. if (start_offset)
  1800. *start_offset = stack_bottom;
  1801. if (size)
  1802. *size = total_aux_stack_size;
  1803. return true;
  1804. }
  1805. return false;
  1806. }
  1807. #endif
  1808. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1809. void
  1810. wasm_get_module_mem_consumption(const WASMModule *module,
  1811. WASMModuleMemConsumption *mem_conspn)
  1812. {
  1813. uint32 i, size;
  1814. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1815. mem_conspn->module_struct_size = sizeof(WASMModule);
  1816. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  1817. for (i = 0; i < module->type_count; i++) {
  1818. WASMType *type = module->types[i];
  1819. size = offsetof(WASMType, types) +
  1820. sizeof(uint8) * (type->param_count + type->result_count);
  1821. mem_conspn->types_size += size;
  1822. }
  1823. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  1824. mem_conspn->functions_size = sizeof(WASMFunction *)
  1825. * module->function_count;
  1826. for (i = 0; i < module->function_count; i++) {
  1827. WASMFunction *func = module->functions[i];
  1828. WASMType *type = func->func_type;
  1829. size = sizeof(WASMFunction) + func->local_count
  1830. + sizeof(uint16) * (type->param_count + func->local_count);
  1831. #if WASM_ENABLE_FAST_INTERP != 0
  1832. size += func->code_compiled_size
  1833. + sizeof(uint32) * func->const_cell_num;
  1834. #endif
  1835. mem_conspn->functions_size += size;
  1836. }
  1837. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  1838. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  1839. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  1840. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  1841. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  1842. * module->table_seg_count;
  1843. for (i = 0; i < module->table_seg_count; i++) {
  1844. WASMTableSeg *table_seg = &module->table_segments[i];
  1845. mem_conspn->tables_size += sizeof(uint32)
  1846. * table_seg->function_count;
  1847. }
  1848. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  1849. * module->data_seg_count;
  1850. for (i = 0; i < module->data_seg_count; i++) {
  1851. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  1852. }
  1853. if (module->const_str_list) {
  1854. StringNode *node = module->const_str_list, *node_next;
  1855. while (node) {
  1856. node_next = node->next;
  1857. mem_conspn->const_strs_size += sizeof(StringNode)
  1858. + strlen(node->str) + 1;
  1859. node = node_next;
  1860. }
  1861. }
  1862. mem_conspn->total_size += mem_conspn->module_struct_size;
  1863. mem_conspn->total_size += mem_conspn->types_size;
  1864. mem_conspn->total_size += mem_conspn->imports_size;
  1865. mem_conspn->total_size += mem_conspn->functions_size;
  1866. mem_conspn->total_size += mem_conspn->tables_size;
  1867. mem_conspn->total_size += mem_conspn->memories_size;
  1868. mem_conspn->total_size += mem_conspn->globals_size;
  1869. mem_conspn->total_size += mem_conspn->exports_size;
  1870. mem_conspn->total_size += mem_conspn->table_segs_size;
  1871. mem_conspn->total_size += mem_conspn->data_segs_size;
  1872. mem_conspn->total_size += mem_conspn->const_strs_size;
  1873. #if WASM_ENABLE_AOT != 0
  1874. mem_conspn->total_size += mem_conspn->aot_code_size;
  1875. #endif
  1876. }
  1877. void
  1878. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  1879. WASMModuleInstMemConsumption *mem_conspn)
  1880. {
  1881. uint32 i, size;
  1882. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1883. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  1884. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  1885. * module_inst->memory_count;
  1886. for (i = 0; i < module_inst->memory_count; i++) {
  1887. WASMMemoryInstance *memory = module_inst->memories[i];
  1888. size = sizeof(WASMMemoryInstance)
  1889. + memory->num_bytes_per_page * memory->cur_page_count;
  1890. mem_conspn->memories_size += size;
  1891. mem_conspn->app_heap_size += memory->heap_data_end
  1892. - memory->heap_data;
  1893. /* size of app heap structure */
  1894. mem_conspn->memories_size +=
  1895. mem_allocator_get_heap_struct_size();
  1896. }
  1897. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  1898. * module_inst->table_count;
  1899. for (i = 0; i < module_inst->table_count; i++) {
  1900. WASMTableInstance *table = module_inst->tables[i];
  1901. size = offsetof(WASMTableInstance, base_addr)
  1902. + sizeof(uint32) * table->cur_size;
  1903. mem_conspn->tables_size += size;
  1904. }
  1905. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  1906. * module_inst->function_count;
  1907. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  1908. * module_inst->global_count;
  1909. if (module_inst->global_count > 0) {
  1910. WASMGlobalInstance *global =
  1911. &module_inst->globals[module_inst->global_count - 1];
  1912. mem_conspn->globals_size += global->data_offset
  1913. + wasm_value_type_size(global->type);
  1914. }
  1915. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  1916. * module_inst->export_func_count;
  1917. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  1918. mem_conspn->total_size += mem_conspn->memories_size;
  1919. mem_conspn->total_size += mem_conspn->functions_size;
  1920. mem_conspn->total_size += mem_conspn->tables_size;
  1921. mem_conspn->total_size += mem_conspn->globals_size;
  1922. mem_conspn->total_size += mem_conspn->exports_size;
  1923. }
  1924. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  1925. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1926. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1927. void
  1928. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  1929. {
  1930. WASMModuleInstance *module_inst =
  1931. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  1932. WASMInterpFrame *cur_frame =
  1933. wasm_exec_env_get_cur_frame(exec_env);
  1934. WASMFunctionInstance *func_inst;
  1935. const char *func_name = NULL;
  1936. uint32 n;
  1937. os_printf("\n");
  1938. for (n = 0; cur_frame && cur_frame->function; n++) {
  1939. func_inst = cur_frame->function;
  1940. if (func_inst->is_import_func) {
  1941. func_name = func_inst->u.func_import->field_name;
  1942. }
  1943. else {
  1944. func_name = func_inst->u.func->field_name;
  1945. }
  1946. /* function name not exported, print number instead */
  1947. if (func_name == NULL) {
  1948. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  1949. }
  1950. else {
  1951. os_printf("#%02d %s \n", n, func_name);
  1952. }
  1953. cur_frame = cur_frame->prev_frame;
  1954. }
  1955. os_printf("\n");
  1956. }
  1957. #endif /* end of WASM_ENABLE_CUSTOM_NAME_SECTION */