wasm_runtime.c 71 KB

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