wasm_runtime.c 75 KB

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