wasm_runtime.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  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. function->import_module_inst =
  497. get_sub_module_inst(module_inst,
  498. import->u.function.import_module);
  499. if (function->import_module_inst) {
  500. function->import_func_inst =
  501. wasm_lookup_function(function->import_module_inst,
  502. import->u.function.field_name, NULL);
  503. }
  504. }
  505. #endif /* WASM_ENABLE_MULTI_MODULE */
  506. function->u.func_import = &import->u.function;
  507. function->param_cell_num =
  508. import->u.function.func_type->param_cell_num;
  509. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  510. function->param_count =
  511. (uint16)function->u.func_import->func_type->param_count;
  512. function->param_types = function->u.func_import->func_type->types;
  513. function->local_cell_num = 0;
  514. function->local_count = 0;
  515. function->local_types = NULL;
  516. function++;
  517. }
  518. /* instantiate functions from function section */
  519. for (i = 0; i < module->function_count; i++) {
  520. function->is_import_func = false;
  521. function->u.func = module->functions[i];
  522. function->param_cell_num = function->u.func->param_cell_num;
  523. function->ret_cell_num = function->u.func->ret_cell_num;
  524. function->local_cell_num = function->u.func->local_cell_num;
  525. function->param_count = (uint16)function->u.func->func_type->param_count;
  526. function->local_count = (uint16)function->u.func->local_count;
  527. function->param_types = function->u.func->func_type->types;
  528. function->local_types = function->u.func->local_types;
  529. function->local_offsets = function->u.func->local_offsets;
  530. #if WASM_ENABLE_FAST_INTERP != 0
  531. function->const_cell_num = function->u.func->const_cell_num;
  532. #endif
  533. function++;
  534. }
  535. bh_assert((uint32)(function - functions) == function_count);
  536. (void)module_inst;
  537. return functions;
  538. }
  539. /**
  540. * Destroy global instances.
  541. */
  542. static void
  543. globals_deinstantiate(WASMGlobalInstance *globals)
  544. {
  545. if (globals)
  546. wasm_runtime_free(globals);
  547. }
  548. /**
  549. * init_expr->u ==> init_val
  550. */
  551. static bool
  552. parse_init_expr(const InitializerExpression *init_expr,
  553. const WASMGlobalInstance *global_inst_array,
  554. uint32 boundary, WASMValue *init_val)
  555. {
  556. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  557. uint32 target_global_index = init_expr->u.global_index;
  558. /**
  559. * a global gets the init value of another global
  560. */
  561. if (target_global_index >= boundary) {
  562. LOG_DEBUG("unknown target global, %d", target_global_index);
  563. return false;
  564. }
  565. /**
  566. * it will work if using WASMGlobalImport and WASMGlobal in
  567. * WASMModule, but will have to face complicated cases
  568. *
  569. * but we still have no sure the target global has been
  570. * initialized before
  571. */
  572. WASMValue target_value =
  573. global_inst_array[target_global_index].initial_value;
  574. bh_memcpy_s(init_val, sizeof(WASMValue), &target_value,
  575. sizeof(target_value));
  576. }
  577. else {
  578. bh_memcpy_s(init_val, sizeof(WASMValue), &init_expr->u,
  579. sizeof(init_expr->u));
  580. }
  581. return true;
  582. }
  583. /**
  584. * Instantiate globals in a module.
  585. */
  586. static WASMGlobalInstance *
  587. globals_instantiate(const WASMModule *module,
  588. WASMModuleInstance *module_inst,
  589. uint32 *p_global_data_size, char *error_buf,
  590. uint32 error_buf_size)
  591. {
  592. WASMImport *import;
  593. uint32 global_data_offset = 0;
  594. uint32 i, global_count =
  595. module->import_global_count + module->global_count;
  596. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  597. WASMGlobalInstance *globals, *global;
  598. if (!(globals = runtime_malloc(total_size,
  599. error_buf, error_buf_size))) {
  600. return NULL;
  601. }
  602. /* instantiate globals from import section */
  603. global = globals;
  604. import = module->import_globals;
  605. for (i = 0; i < module->import_global_count; i++, import++) {
  606. WASMGlobalImport *global_import = &import->u.global;
  607. global->type = global_import->type;
  608. global->is_mutable = global_import->is_mutable;
  609. #if WASM_ENABLE_MULTI_MODULE != 0
  610. if (global_import->import_module) {
  611. WASMModuleInstance *sub_module_inst = get_sub_module_inst(
  612. module_inst, global_import->import_module);
  613. bh_assert(sub_module_inst);
  614. WASMGlobalInstance *global_inst_linked =
  615. wasm_lookup_global(sub_module_inst, global_import->field_name);
  616. bh_assert(global_inst_linked);
  617. global->import_global_inst = global_inst_linked;
  618. global->import_module_inst = sub_module_inst;
  619. /**
  620. * although, actually don't need initial_value for an imported
  621. * global, we keep it here like a place holder because of
  622. * global-data and
  623. * (global $g2 i32 (global.get $g1))
  624. */
  625. WASMGlobal *linked_global = global_import->import_global_linked;
  626. InitializerExpression *linked_init_expr =
  627. &(linked_global->init_expr);
  628. bool ret = parse_init_expr(
  629. linked_init_expr,
  630. sub_module_inst->globals,
  631. sub_module_inst->global_count, &(global->initial_value));
  632. if (!ret) {
  633. set_error_buf(error_buf, error_buf_size, "unknown global");
  634. return NULL;
  635. }
  636. }
  637. else
  638. #endif
  639. {
  640. /* native globals share their initial_values in one module */
  641. global->initial_value = global_import->global_data_linked;
  642. }
  643. global->data_offset = global_data_offset;
  644. global_data_offset += wasm_value_type_size(global->type);
  645. global++;
  646. }
  647. /* instantiate globals from global section */
  648. for (i = 0; i < module->global_count; i++) {
  649. bool ret = false;
  650. uint32 global_count =
  651. module->import_global_count + module->global_count;
  652. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  653. global->type = module->globals[i].type;
  654. global->is_mutable = module->globals[i].is_mutable;
  655. global->data_offset = global_data_offset;
  656. global_data_offset += wasm_value_type_size(global->type);
  657. /**
  658. * first init, it might happen that the target global instance
  659. * has not been initialize yet
  660. */
  661. if (init_expr->init_expr_type != INIT_EXPR_TYPE_GET_GLOBAL) {
  662. ret =
  663. parse_init_expr(init_expr, globals, global_count,
  664. &(global->initial_value));
  665. if (!ret) {
  666. set_error_buf(error_buf, error_buf_size, "unknown global");
  667. return NULL;
  668. }
  669. }
  670. global++;
  671. }
  672. bh_assert((uint32)(global - globals) == global_count);
  673. *p_global_data_size = global_data_offset;
  674. (void)module_inst;
  675. return globals;
  676. }
  677. static bool
  678. globals_instantiate_fix(WASMGlobalInstance *globals,
  679. const WASMModule *module,
  680. char *error_buf, uint32 error_buf_size)
  681. {
  682. WASMGlobalInstance *global = globals;
  683. uint32 i;
  684. uint32 global_count = module->import_global_count + module->global_count;
  685. /**
  686. * second init, only target global instances from global
  687. * (ignore import_global)
  688. * to fix skipped init_value in the previous round
  689. * hope two rounds are enough but how about a chain ?
  690. */
  691. for (i = 0; i < module->global_count; i++) {
  692. bool ret = false;
  693. InitializerExpression *init_expr = &module->globals[i].init_expr;
  694. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  695. ret = parse_init_expr(init_expr, globals, global_count,
  696. &global->initial_value);
  697. if (!ret) {
  698. set_error_buf(error_buf, error_buf_size, "unknown global");
  699. return false;
  700. }
  701. }
  702. global++;
  703. }
  704. return true;
  705. }
  706. /**
  707. * Return export function count in module export section.
  708. */
  709. static uint32
  710. get_export_count(const WASMModule *module, uint8 kind)
  711. {
  712. WASMExport *export = module->exports;
  713. uint32 count = 0, i;
  714. for (i = 0; i < module->export_count; i++, export++)
  715. if (export->kind == kind)
  716. count++;
  717. return count;
  718. }
  719. /**
  720. * Destroy export function instances.
  721. */
  722. static void
  723. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  724. {
  725. if (functions)
  726. wasm_runtime_free(functions);
  727. }
  728. /**
  729. * Instantiate export functions in a module.
  730. */
  731. static WASMExportFuncInstance*
  732. export_functions_instantiate(const WASMModule *module,
  733. WASMModuleInstance *module_inst,
  734. uint32 export_func_count,
  735. char *error_buf, uint32 error_buf_size)
  736. {
  737. WASMExportFuncInstance *export_funcs, *export_func;
  738. WASMExport *export = module->exports;
  739. uint32 i;
  740. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  741. if (!(export_func = export_funcs = runtime_malloc
  742. (total_size, error_buf, error_buf_size))) {
  743. return NULL;
  744. }
  745. for (i = 0; i < module->export_count; i++, export++)
  746. if (export->kind == EXPORT_KIND_FUNC) {
  747. export_func->name = export->name;
  748. export_func->function = &module_inst->functions[export->index];
  749. export_func++;
  750. }
  751. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  752. return export_funcs;
  753. }
  754. #if WASM_ENABLE_MULTI_MODULE != 0
  755. static void
  756. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  757. {
  758. if (globals)
  759. wasm_runtime_free(globals);
  760. }
  761. static WASMExportGlobInstance *
  762. export_globals_instantiate(const WASMModule *module,
  763. WASMModuleInstance *module_inst,
  764. uint32 export_glob_count, char *error_buf,
  765. uint32 error_buf_size)
  766. {
  767. WASMExportGlobInstance *export_globals, *export_global;
  768. WASMExport *export = module->exports;
  769. uint32 i;
  770. uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  771. if (!(export_global = export_globals = runtime_malloc
  772. (total_size, error_buf, error_buf_size))) {
  773. return NULL;
  774. }
  775. for (i = 0; i < module->export_count; i++, export++)
  776. if (export->kind == EXPORT_KIND_GLOBAL) {
  777. export_global->name = export->name;
  778. export_global->global = &module_inst->globals[export->index];
  779. export_global++;
  780. }
  781. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  782. return export_globals;
  783. }
  784. #endif
  785. static bool
  786. execute_post_inst_function(WASMModuleInstance *module_inst)
  787. {
  788. WASMFunctionInstance *post_inst_func = NULL;
  789. WASMType *post_inst_func_type;
  790. uint32 i;
  791. for (i = 0; i < module_inst->export_func_count; i++)
  792. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  793. post_inst_func = module_inst->export_functions[i].function;
  794. break;
  795. }
  796. if (!post_inst_func)
  797. /* Not found */
  798. return true;
  799. post_inst_func_type = post_inst_func->u.func->func_type;
  800. if (post_inst_func_type->param_count != 0
  801. || post_inst_func_type->result_count != 0)
  802. /* Not a valid function type, ignore it */
  803. return true;
  804. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  805. 0, NULL);
  806. }
  807. #if WASM_ENABLE_BULK_MEMORY != 0
  808. static bool
  809. execute_memory_init_function(WASMModuleInstance *module_inst)
  810. {
  811. WASMFunctionInstance *memory_init_func = NULL;
  812. WASMType *memory_init_func_type;
  813. uint32 i;
  814. for (i = 0; i < module_inst->export_func_count; i++)
  815. if (!strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) {
  816. memory_init_func = module_inst->export_functions[i].function;
  817. break;
  818. }
  819. if (!memory_init_func)
  820. /* Not found */
  821. return true;
  822. memory_init_func_type = memory_init_func->u.func->func_type;
  823. if (memory_init_func_type->param_count != 0
  824. || memory_init_func_type->result_count != 0)
  825. /* Not a valid function type, ignore it */
  826. return true;
  827. return wasm_create_exec_env_and_call_function(module_inst,
  828. memory_init_func,
  829. 0, NULL);
  830. }
  831. #endif
  832. static bool
  833. execute_start_function(WASMModuleInstance *module_inst)
  834. {
  835. WASMFunctionInstance *func = module_inst->start_function;
  836. if (!func)
  837. return true;
  838. bh_assert(!func->is_import_func && func->param_cell_num == 0
  839. && func->ret_cell_num == 0);
  840. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  841. }
  842. static bool
  843. execute_malloc_function(WASMModuleInstance *module_inst,
  844. WASMFunctionInstance *malloc_func,
  845. uint32 size, uint32 *p_result)
  846. {
  847. uint32 argv[2];
  848. bool ret;
  849. argv[0] = size;
  850. ret = wasm_create_exec_env_and_call_function
  851. (module_inst, malloc_func, 1, argv);
  852. if (ret)
  853. *p_result = argv[0];
  854. return ret;
  855. }
  856. static bool
  857. execute_free_function(WASMModuleInstance *module_inst,
  858. WASMFunctionInstance *free_func,
  859. uint32 offset)
  860. {
  861. uint32 argv[2];
  862. argv[0] = offset;
  863. return wasm_create_exec_env_and_call_function
  864. (module_inst, free_func, 1, argv);
  865. }
  866. #if WASM_ENABLE_MULTI_MODULE != 0
  867. static bool
  868. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  869. uint32 stack_size, uint32 heap_size, char *error_buf,
  870. uint32 error_buf_size)
  871. {
  872. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  873. WASMRegisteredModule *sub_module_list_node =
  874. bh_list_first_elem(module->import_module_list);
  875. while (sub_module_list_node) {
  876. WASMSubModInstNode *sub_module_inst_list_node;
  877. WASMModule *sub_module = (WASMModule*)sub_module_list_node->module;
  878. WASMModuleInstance *sub_module_inst =
  879. wasm_instantiate(sub_module, false, stack_size, heap_size,
  880. error_buf, error_buf_size);
  881. if (!sub_module_inst) {
  882. LOG_DEBUG("instantiate %s failed",
  883. sub_module_list_node->module_name);
  884. return false;
  885. }
  886. sub_module_inst_list_node = runtime_malloc
  887. (sizeof(WASMSubModInstNode), error_buf, error_buf_size);
  888. if (!sub_module_inst_list_node) {
  889. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  890. sizeof(WASMSubModInstNode));
  891. wasm_deinstantiate(sub_module_inst, false);
  892. return false;
  893. }
  894. sub_module_inst_list_node->module_inst = sub_module_inst;
  895. sub_module_inst_list_node->module_name =
  896. sub_module_list_node->module_name;
  897. bh_list_status ret =
  898. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  899. bh_assert(BH_LIST_SUCCESS == ret);
  900. (void)ret;
  901. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  902. }
  903. return true;
  904. }
  905. static void
  906. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  907. {
  908. bh_list *list = module_inst->sub_module_inst_list;
  909. WASMSubModInstNode *node = bh_list_first_elem(list);
  910. while (node) {
  911. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  912. bh_list_remove(list, node);
  913. wasm_deinstantiate(node->module_inst, false);
  914. node = next_node;
  915. }
  916. }
  917. #endif
  918. static bool
  919. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  920. uint32 error_buf_size)
  921. {
  922. WASMModule *module = module_inst->module;
  923. uint32 i;
  924. for (i = 0; i < module->import_function_count; i++) {
  925. WASMFunctionImport *func =
  926. &((module->import_functions + i)->u.function);
  927. if (!func->func_ptr_linked
  928. #if WASM_ENABLE_MULTI_MODULE != 0
  929. && !func->import_func_linked
  930. #endif
  931. ) {
  932. #if WASM_ENABLE_SPEC_TEST != 0
  933. set_error_buf(error_buf, error_buf_size,
  934. "unknown import or incompatible import type");
  935. return false;
  936. #else
  937. #if WASM_ENABLE_WAMR_COMPILER == 0
  938. LOG_WARNING("warning: fail to link import function (%s, %s)",
  939. func->module_name, func->field_name);
  940. #else
  941. /* do nothing to avoid confused message */
  942. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  943. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  944. }
  945. }
  946. for (i = 0; i < module->import_global_count; i++) {
  947. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  948. if (!global->is_linked) {
  949. #if WASM_ENABLE_SPEC_TEST != 0
  950. set_error_buf(error_buf, error_buf_size,
  951. "unknown import or incompatible import type");
  952. return false;
  953. #else
  954. #if WASM_ENABLE_WAMR_COMPILER == 0
  955. LOG_DEBUG("warning: fail to link import global (%s, %s)",
  956. global->module_name, global->field_name);
  957. #else
  958. /* do nothing to avoid confused message */
  959. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  960. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  961. }
  962. }
  963. return true;
  964. }
  965. /**
  966. * Instantiate module
  967. */
  968. WASMModuleInstance*
  969. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  970. uint32 stack_size, uint32 heap_size,
  971. char *error_buf, uint32 error_buf_size)
  972. {
  973. WASMModuleInstance *module_inst;
  974. WASMGlobalInstance *globals = NULL, *global;
  975. uint32 global_count, global_data_size = 0, i;
  976. uint32 base_offset, length;
  977. uint8 *global_data, *global_data_end;
  978. #if WASM_ENABLE_MULTI_MODULE != 0
  979. bool ret = false;
  980. #endif
  981. if (!module)
  982. return NULL;
  983. /* Check heap size */
  984. heap_size = align_uint(heap_size, 8);
  985. if (heap_size > APP_HEAP_SIZE_MAX)
  986. heap_size = APP_HEAP_SIZE_MAX;
  987. /* Allocate the memory */
  988. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  989. error_buf, error_buf_size))) {
  990. return NULL;
  991. }
  992. module_inst->module = module;
  993. #if WASM_ENABLE_MULTI_MODULE != 0
  994. module_inst->sub_module_inst_list =
  995. &module_inst->sub_module_inst_list_head;
  996. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  997. error_buf, error_buf_size);
  998. if (!ret) {
  999. LOG_DEBUG("build a sub module list failed");
  1000. wasm_deinstantiate(module_inst, false);
  1001. return NULL;
  1002. }
  1003. #endif
  1004. /* Instantiate global firstly to get the mutable data size */
  1005. global_count = module->import_global_count + module->global_count;
  1006. if (global_count
  1007. && !(globals = globals_instantiate(module, module_inst,
  1008. &global_data_size,
  1009. error_buf, error_buf_size))) {
  1010. wasm_deinstantiate(module_inst, false);
  1011. return NULL;
  1012. }
  1013. module_inst->global_count = global_count;
  1014. module_inst->globals = globals;
  1015. module_inst->memory_count =
  1016. module->import_memory_count + module->memory_count;
  1017. module_inst->table_count =
  1018. module->import_table_count + module->table_count;
  1019. module_inst->function_count =
  1020. module->import_function_count + module->function_count;
  1021. /* export */
  1022. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1023. #if WASM_ENABLE_MULTI_MODULE != 0
  1024. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1025. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1026. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1027. #endif
  1028. if (global_count > 0) {
  1029. if (!(module_inst->global_data = runtime_malloc
  1030. (global_data_size, error_buf, error_buf_size))) {
  1031. wasm_deinstantiate(module_inst, false);
  1032. return NULL;
  1033. }
  1034. }
  1035. /* Instantiate memories/tables/functions */
  1036. if ((module_inst->memory_count > 0
  1037. && !(module_inst->memories =
  1038. memories_instantiate(module,
  1039. module_inst,
  1040. heap_size, error_buf, error_buf_size)))
  1041. || (module_inst->table_count > 0
  1042. && !(module_inst->tables =
  1043. tables_instantiate(module,
  1044. module_inst,
  1045. error_buf, error_buf_size)))
  1046. || (module_inst->function_count > 0
  1047. && !(module_inst->functions =
  1048. functions_instantiate(module,
  1049. module_inst,
  1050. error_buf, error_buf_size)))
  1051. || (module_inst->export_func_count > 0
  1052. && !(module_inst->export_functions = export_functions_instantiate(
  1053. module, module_inst, module_inst->export_func_count,
  1054. error_buf, error_buf_size)))
  1055. #if WASM_ENABLE_MULTI_MODULE != 0
  1056. || (module_inst->export_glob_count > 0
  1057. && !(module_inst->export_globals = export_globals_instantiate(
  1058. module, module_inst, module_inst->export_glob_count,
  1059. error_buf, error_buf_size)))
  1060. #endif
  1061. ) {
  1062. wasm_deinstantiate(module_inst, false);
  1063. return NULL;
  1064. }
  1065. if (global_count > 0) {
  1066. /**
  1067. * since there might be some globals are not instantiate the first
  1068. * instantiate round
  1069. */
  1070. if (!globals_instantiate_fix(globals, module,
  1071. error_buf, error_buf_size)) {
  1072. wasm_deinstantiate(module_inst, false);
  1073. return NULL;
  1074. }
  1075. /* Initialize the global data */
  1076. global_data = module_inst->global_data;
  1077. global_data_end = global_data + global_data_size;
  1078. global = globals;
  1079. for (i = 0; i < global_count; i++, global++) {
  1080. switch (global->type) {
  1081. case VALUE_TYPE_I32:
  1082. case VALUE_TYPE_F32:
  1083. *(int32*)global_data = global->initial_value.i32;
  1084. global_data += sizeof(int32);
  1085. break;
  1086. case VALUE_TYPE_I64:
  1087. case VALUE_TYPE_F64:
  1088. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1089. &global->initial_value.i64, sizeof(int64));
  1090. global_data += sizeof(int64);
  1091. break;
  1092. default:
  1093. bh_assert(0);
  1094. }
  1095. }
  1096. bh_assert(global_data == global_data_end);
  1097. }
  1098. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1099. wasm_deinstantiate(module_inst, false);
  1100. return NULL;
  1101. }
  1102. /* Initialize the memory data with data segment section */
  1103. module_inst->default_memory =
  1104. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1105. for (i = 0; i < module->data_seg_count; i++) {
  1106. WASMMemoryInstance *memory = NULL;
  1107. uint8 *memory_data = NULL;
  1108. uint32 memory_size = 0;
  1109. WASMDataSeg *data_seg = module->data_segments[i];
  1110. #if WASM_ENABLE_BULK_MEMORY != 0
  1111. if (data_seg->is_passive)
  1112. continue;
  1113. #endif
  1114. /* has check it in loader */
  1115. memory = module_inst->memories[data_seg->memory_index];
  1116. bh_assert(memory);
  1117. memory_data = memory->memory_data;
  1118. bh_assert(memory_data);
  1119. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1120. bh_assert(data_seg->base_offset.init_expr_type
  1121. == INIT_EXPR_TYPE_I32_CONST
  1122. || data_seg->base_offset.init_expr_type
  1123. == INIT_EXPR_TYPE_GET_GLOBAL);
  1124. if (data_seg->base_offset.init_expr_type
  1125. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1126. bh_assert(data_seg->base_offset.u.global_index < global_count
  1127. && globals[data_seg->base_offset.u.global_index].type
  1128. == VALUE_TYPE_I32);
  1129. data_seg->base_offset.u.i32 =
  1130. globals[data_seg->base_offset.u.global_index]
  1131. .initial_value.i32;
  1132. }
  1133. /* check offset since length might negative */
  1134. base_offset = (uint32)data_seg->base_offset.u.i32;
  1135. if (base_offset > memory_size) {
  1136. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1137. memory_size);
  1138. set_error_buf(error_buf, error_buf_size,
  1139. "data segment does not fit");
  1140. wasm_deinstantiate(module_inst, false);
  1141. return NULL;
  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. wasm_deinstantiate(module_inst, false);
  1151. return NULL;
  1152. }
  1153. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1154. data_seg->data, length);
  1155. }
  1156. /* Initialize the table data with table segment section */
  1157. module_inst->default_table =
  1158. module_inst->table_count ? module_inst->tables[0] : NULL;
  1159. for (i = 0; i < module->table_seg_count; i++) {
  1160. WASMTableSeg *table_seg = module->table_segments + i;
  1161. /* has check it in loader */
  1162. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1163. bh_assert(table);
  1164. uint32 *table_data = (uint32 *)table->base_addr;
  1165. #if WASM_ENABLE_MULTI_MODULE != 0
  1166. table_data = table->table_inst_linked
  1167. ? (uint32 *)table->table_inst_linked->base_addr
  1168. : table_data;
  1169. #endif
  1170. bh_assert(table_data);
  1171. /* init vec(funcidx) */
  1172. bh_assert(table_seg->base_offset.init_expr_type
  1173. == INIT_EXPR_TYPE_I32_CONST
  1174. || table_seg->base_offset.init_expr_type
  1175. == INIT_EXPR_TYPE_GET_GLOBAL);
  1176. if (table_seg->base_offset.init_expr_type
  1177. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1178. bh_assert(table_seg->base_offset.u.global_index < global_count
  1179. && globals[table_seg->base_offset.u.global_index].type
  1180. == VALUE_TYPE_I32);
  1181. table_seg->base_offset.u.i32 =
  1182. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1183. }
  1184. /* check offset since length might negative */
  1185. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1186. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1187. table_seg->base_offset.u.i32, table->cur_size);
  1188. set_error_buf(error_buf, error_buf_size,
  1189. "elements segment does not fit");
  1190. wasm_deinstantiate(module_inst, false);
  1191. return NULL;
  1192. }
  1193. /* check offset + length(could be zero) */
  1194. length = table_seg->function_count;
  1195. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1196. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1197. table_seg->base_offset.u.i32, length, table->cur_size);
  1198. set_error_buf(error_buf, error_buf_size,
  1199. "elements segment does not fit");
  1200. wasm_deinstantiate(module_inst, false);
  1201. return NULL;
  1202. }
  1203. /**
  1204. * Check function index in the current module inst for now.
  1205. * will check the linked table inst owner in future.
  1206. * so loader check is enough
  1207. */
  1208. bh_memcpy_s(
  1209. table_data + table_seg->base_offset.u.i32,
  1210. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1211. * sizeof(uint32)),
  1212. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1213. }
  1214. /* module instance type */
  1215. module_inst->module_type = Wasm_Module_Bytecode;
  1216. /* Initialize the thread related data */
  1217. if (stack_size == 0)
  1218. stack_size = DEFAULT_WASM_STACK_SIZE;
  1219. #if WASM_ENABLE_SPEC_TEST != 0
  1220. if (stack_size < 48 *1024)
  1221. stack_size = 48 * 1024;
  1222. #endif
  1223. module_inst->default_wasm_stack_size = stack_size;
  1224. if (module->malloc_function != (uint32)-1) {
  1225. module_inst->malloc_function =
  1226. &module_inst->functions[module->malloc_function];
  1227. }
  1228. if (module->free_function != (uint32)-1) {
  1229. module_inst->free_function =
  1230. &module_inst->functions[module->free_function];
  1231. }
  1232. #if WASM_ENABLE_LIBC_WASI != 0
  1233. /* The sub-instance will get the wasi_ctx from main-instance */
  1234. if (!is_sub_inst) {
  1235. if (heap_size > 0
  1236. && !wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1237. module->wasi_args.dir_list,
  1238. module->wasi_args.dir_count,
  1239. module->wasi_args.map_dir_list,
  1240. module->wasi_args.map_dir_count,
  1241. module->wasi_args.env,
  1242. module->wasi_args.env_count,
  1243. module->wasi_args.argv,
  1244. module->wasi_args.argc,
  1245. error_buf, error_buf_size)) {
  1246. wasm_deinstantiate(module_inst, false);
  1247. return NULL;
  1248. }
  1249. }
  1250. #endif
  1251. if (module->start_function != (uint32)-1) {
  1252. /* TODO: fix start function can be import function issue */
  1253. if (module->start_function >= module->import_function_count)
  1254. module_inst->start_function =
  1255. &module_inst->functions[module->start_function];
  1256. }
  1257. /* Execute __post_instantiate function */
  1258. if (!execute_post_inst_function(module_inst)
  1259. || !execute_start_function(module_inst)) {
  1260. set_error_buf(error_buf, error_buf_size,
  1261. module_inst->cur_exception);
  1262. wasm_deinstantiate(module_inst, false);
  1263. return NULL;
  1264. }
  1265. #if WASM_ENABLE_BULK_MEMORY != 0
  1266. #if WASM_ENABLE_LIBC_WASI != 0
  1267. if (!module->is_wasi_module) {
  1268. #endif
  1269. /* Only execute the memory init function for main instance because
  1270. the data segments will be dropped once initialized.
  1271. */
  1272. if (!is_sub_inst) {
  1273. if (!execute_memory_init_function(module_inst)) {
  1274. set_error_buf(error_buf, error_buf_size,
  1275. module_inst->cur_exception);
  1276. wasm_deinstantiate(module_inst, false);
  1277. return NULL;
  1278. }
  1279. }
  1280. #if WASM_ENABLE_LIBC_WASI != 0
  1281. }
  1282. #endif
  1283. #endif
  1284. #if WASM_ENABLE_MEMORY_TRACING != 0
  1285. wasm_runtime_dump_module_inst_mem_consumption
  1286. ((WASMModuleInstanceCommon *)module_inst);
  1287. #endif
  1288. (void)global_data_end;
  1289. return module_inst;
  1290. }
  1291. void
  1292. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1293. {
  1294. if (!module_inst)
  1295. return;
  1296. #if WASM_ENABLE_MULTI_MODULE != 0
  1297. sub_module_deinstantiate(module_inst);
  1298. #endif
  1299. #if WASM_ENABLE_LIBC_WASI != 0
  1300. /* Destroy wasi resource before freeing app heap, since some fields of
  1301. wasi contex are allocated from app heap, and if app heap is freed,
  1302. these fields will be set to NULL, we cannot free their internal data
  1303. which may allocated from global heap. */
  1304. /* Only destroy wasi ctx in the main module instance */
  1305. if (!is_sub_inst)
  1306. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1307. #endif
  1308. if (module_inst->memory_count > 0)
  1309. memories_deinstantiate(
  1310. module_inst,
  1311. module_inst->memories, module_inst->memory_count);
  1312. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1313. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1314. globals_deinstantiate(module_inst->globals);
  1315. export_functions_deinstantiate(module_inst->export_functions);
  1316. #if WASM_ENABLE_MULTI_MODULE != 0
  1317. export_globals_deinstantiate(module_inst->export_globals);
  1318. #endif
  1319. if (module_inst->global_data)
  1320. wasm_runtime_free(module_inst->global_data);
  1321. wasm_runtime_free(module_inst);
  1322. }
  1323. WASMFunctionInstance*
  1324. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1325. const char *name, const char *signature)
  1326. {
  1327. uint32 i;
  1328. for (i = 0; i < module_inst->export_func_count; i++)
  1329. if (!strcmp(module_inst->export_functions[i].name, name))
  1330. return module_inst->export_functions[i].function;
  1331. (void)signature;
  1332. return NULL;
  1333. }
  1334. #if WASM_ENABLE_MULTI_MODULE != 0
  1335. WASMGlobalInstance *
  1336. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1337. {
  1338. uint32 i;
  1339. for (i = 0; i < module_inst->export_glob_count; i++)
  1340. if (!strcmp(module_inst->export_globals[i].name, name))
  1341. return module_inst->export_globals[i].global;
  1342. return NULL;
  1343. }
  1344. WASMMemoryInstance *
  1345. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1346. {
  1347. /**
  1348. * using a strong assumption that one module instance only has
  1349. * one memory instance
  1350. */
  1351. (void)module_inst->export_memories;
  1352. return module_inst->memories[0];
  1353. }
  1354. WASMTableInstance *
  1355. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1356. {
  1357. /**
  1358. * using a strong assumption that one module instance only has
  1359. * one table instance
  1360. */
  1361. (void)module_inst->export_tables;
  1362. return module_inst->tables[0];
  1363. }
  1364. #endif
  1365. bool
  1366. wasm_call_function(WASMExecEnv *exec_env,
  1367. WASMFunctionInstance *function,
  1368. unsigned argc, uint32 argv[])
  1369. {
  1370. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1371. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1372. return !wasm_get_exception(module_inst) ? true : false;
  1373. }
  1374. bool
  1375. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1376. WASMFunctionInstance *func,
  1377. unsigned argc, uint32 argv[])
  1378. {
  1379. WASMExecEnv *exec_env;
  1380. bool ret;
  1381. if (!(exec_env = wasm_exec_env_create(
  1382. (WASMModuleInstanceCommon*)module_inst,
  1383. module_inst->default_wasm_stack_size))) {
  1384. wasm_set_exception(module_inst, "allocate memory failed");
  1385. return false;
  1386. }
  1387. /* set thread handle and stack boundary */
  1388. wasm_exec_env_set_thread_info(exec_env);
  1389. ret = wasm_call_function(exec_env, func, argc, argv);
  1390. wasm_exec_env_destroy(exec_env);
  1391. return ret;
  1392. }
  1393. void
  1394. wasm_set_exception(WASMModuleInstance *module_inst,
  1395. const char *exception)
  1396. {
  1397. if (exception)
  1398. snprintf(module_inst->cur_exception,
  1399. sizeof(module_inst->cur_exception),
  1400. "Exception: %s", exception);
  1401. else
  1402. module_inst->cur_exception[0] = '\0';
  1403. }
  1404. const char*
  1405. wasm_get_exception(WASMModuleInstance *module_inst)
  1406. {
  1407. if (module_inst->cur_exception[0] == '\0')
  1408. return NULL;
  1409. else
  1410. return module_inst->cur_exception;
  1411. }
  1412. uint32
  1413. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1414. void **p_native_addr)
  1415. {
  1416. WASMMemoryInstance *memory = module_inst->default_memory;
  1417. uint8 *addr = NULL;
  1418. uint32 offset = 0;
  1419. if (!memory) {
  1420. wasm_set_exception(module_inst, "uninitialized memory");
  1421. return 0;
  1422. }
  1423. if (memory->heap_handle) {
  1424. addr = mem_allocator_malloc(memory->heap_handle, size);
  1425. }
  1426. else if (module_inst->malloc_function
  1427. && module_inst->free_function) {
  1428. if (!execute_malloc_function(module_inst,
  1429. module_inst->malloc_function,
  1430. size, &offset)) {
  1431. return 0;
  1432. }
  1433. /* If we use app's malloc function,
  1434. the default memory may be changed while memory growing */
  1435. memory = module_inst->default_memory;
  1436. addr = offset ? memory->memory_data + offset : NULL;
  1437. }
  1438. if (!addr) {
  1439. wasm_set_exception(module_inst, "out of memory");
  1440. return 0;
  1441. }
  1442. if (p_native_addr)
  1443. *p_native_addr = addr;
  1444. return (uint32)(addr - memory->memory_data);
  1445. }
  1446. void
  1447. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1448. {
  1449. if (ptr) {
  1450. WASMMemoryInstance *memory = module_inst->default_memory;
  1451. uint8* addr;
  1452. if (!memory) {
  1453. return;
  1454. }
  1455. addr = memory->memory_data + ptr;
  1456. if (memory->heap_handle
  1457. && memory->heap_data <= addr
  1458. && addr < memory->heap_data_end) {
  1459. mem_allocator_free(memory->heap_handle, addr);
  1460. }
  1461. else if (module_inst->malloc_function
  1462. && module_inst->free_function
  1463. && memory->memory_data <= addr
  1464. && addr < memory->memory_data_end) {
  1465. execute_free_function(module_inst,
  1466. module_inst->free_function,
  1467. ptr);
  1468. }
  1469. }
  1470. }
  1471. uint32
  1472. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1473. const char *src, uint32 size)
  1474. {
  1475. char *buffer;
  1476. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1477. (void**)&buffer);
  1478. if (buffer_offset != 0) {
  1479. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1480. bh_memcpy_s(buffer, size, src, size);
  1481. }
  1482. return buffer_offset;
  1483. }
  1484. bool
  1485. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1486. uint32 app_offset, uint32 size)
  1487. {
  1488. WASMMemoryInstance *memory = module_inst->default_memory;
  1489. uint32 memory_data_size =
  1490. memory->num_bytes_per_page * memory->cur_page_count;
  1491. /* integer overflow check */
  1492. if (app_offset + size < app_offset) {
  1493. goto fail;
  1494. }
  1495. if (app_offset + size <= memory_data_size) {
  1496. return true;
  1497. }
  1498. fail:
  1499. wasm_set_exception(module_inst, "out of bounds memory access");
  1500. return false;
  1501. }
  1502. bool
  1503. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1504. void *native_ptr, uint32 size)
  1505. {
  1506. uint8 *addr = (uint8 *)native_ptr;
  1507. WASMMemoryInstance *memory = module_inst->default_memory;
  1508. /* integer overflow check */
  1509. if (addr + size < addr) {
  1510. goto fail;
  1511. }
  1512. if (memory->memory_data <= addr
  1513. && addr + size <= memory->memory_data_end) {
  1514. return true;
  1515. }
  1516. fail:
  1517. wasm_set_exception(module_inst, "out of bounds memory access");
  1518. return false;
  1519. }
  1520. void *
  1521. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1522. uint32 app_offset)
  1523. {
  1524. WASMMemoryInstance *memory = module_inst->default_memory;
  1525. uint8 *addr = memory->memory_data + app_offset;
  1526. if (memory->memory_data <= addr
  1527. && addr < memory->memory_data_end)
  1528. return addr;
  1529. return NULL;
  1530. }
  1531. uint32
  1532. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1533. void *native_ptr)
  1534. {
  1535. WASMMemoryInstance *memory = module_inst->default_memory;
  1536. uint8 *addr = (uint8 *)native_ptr;
  1537. if (memory->memory_data <= addr
  1538. && addr < memory->memory_data_end)
  1539. return (uint32)(addr - memory->memory_data);
  1540. return 0;
  1541. }
  1542. bool
  1543. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1544. uint32 app_offset,
  1545. uint32 *p_app_start_offset,
  1546. uint32 *p_app_end_offset)
  1547. {
  1548. WASMMemoryInstance *memory = module_inst->default_memory;
  1549. uint32 memory_data_size =
  1550. memory->num_bytes_per_page * memory->cur_page_count;
  1551. if (app_offset < memory_data_size) {
  1552. if (p_app_start_offset)
  1553. *p_app_start_offset = 0;
  1554. if (p_app_end_offset)
  1555. *p_app_end_offset = memory_data_size;
  1556. return true;
  1557. }
  1558. return false;
  1559. }
  1560. bool
  1561. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1562. uint8 *native_ptr,
  1563. uint8 **p_native_start_addr,
  1564. uint8 **p_native_end_addr)
  1565. {
  1566. WASMMemoryInstance *memory = module_inst->default_memory;
  1567. uint8 *addr = (uint8 *)native_ptr;
  1568. if (memory->memory_data <= addr
  1569. && addr < memory->memory_data_end) {
  1570. if (p_native_start_addr)
  1571. *p_native_start_addr = memory->memory_data;
  1572. if (p_native_end_addr)
  1573. *p_native_end_addr = memory->memory_data_end;
  1574. return true;
  1575. }
  1576. return false;
  1577. }
  1578. bool
  1579. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1580. {
  1581. WASMMemoryInstance *memory = module->default_memory;
  1582. uint8 *new_memory_data, *memory_data = memory->memory_data;
  1583. uint32 heap_size = memory->heap_data_end - memory->heap_data;
  1584. uint32 total_size_old = memory->memory_data_end - memory_data;
  1585. uint32 total_page_count = inc_page_count + memory->cur_page_count;
  1586. uint64 total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1587. void *heap_handle_old = memory->heap_handle;
  1588. uint8 *heap_data_old = memory->heap_data;
  1589. if (inc_page_count <= 0)
  1590. /* No need to enlarge memory */
  1591. return true;
  1592. if (total_page_count < memory->cur_page_count /* integer overflow */
  1593. || total_page_count > memory->max_page_count) {
  1594. return false;
  1595. }
  1596. if (total_size >= UINT32_MAX) {
  1597. return false;
  1598. }
  1599. #if WASM_ENABLE_SHARED_MEMORY != 0
  1600. if (memory->is_shared) {
  1601. /* For shared memory, we have reserved the maximum spaces during
  1602. instantiate, only change the cur_page_count here */
  1603. memory->cur_page_count = total_page_count;
  1604. return true;
  1605. }
  1606. #endif
  1607. if (heap_size > 0) {
  1608. /* Destroy heap's lock firstly, if its memory is re-allocated,
  1609. we cannot access its lock again. */
  1610. mem_allocator_destroy_lock(memory->heap_handle);
  1611. }
  1612. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1613. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1614. if (heap_size > 0) {
  1615. /* Restore heap's lock if memory re-alloc failed */
  1616. mem_allocator_reinit_lock(memory->heap_handle);
  1617. }
  1618. return false;
  1619. }
  1620. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1621. memory_data, total_size_old);
  1622. wasm_runtime_free(memory_data);
  1623. }
  1624. memset(new_memory_data + total_size_old,
  1625. 0, (uint32)total_size - total_size_old);
  1626. if (heap_size > 0) {
  1627. memory->heap_handle = (uint8 *)heap_handle_old +
  1628. (new_memory_data - memory_data);
  1629. if (mem_allocator_migrate(memory->heap_handle,
  1630. heap_handle_old) != 0) {
  1631. return false;
  1632. }
  1633. }
  1634. memory->memory_data = new_memory_data;
  1635. memory->cur_page_count = total_page_count;
  1636. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1637. memory->heap_data_end = memory->heap_data + heap_size;
  1638. memory->memory_data_end = memory->memory_data
  1639. + memory->num_bytes_per_page
  1640. * total_page_count;
  1641. return true;
  1642. }
  1643. bool
  1644. wasm_call_indirect(WASMExecEnv *exec_env,
  1645. uint32_t element_indices,
  1646. uint32_t argc, uint32_t argv[])
  1647. {
  1648. WASMModuleInstance *module_inst = NULL;
  1649. WASMTableInstance *table_inst = NULL;
  1650. uint32_t function_indices = 0;
  1651. WASMFunctionInstance *function_inst = NULL;
  1652. module_inst =
  1653. (WASMModuleInstance*)exec_env->module_inst;
  1654. bh_assert(module_inst);
  1655. table_inst = module_inst->default_table;
  1656. if (!table_inst) {
  1657. wasm_set_exception(module_inst, "unknown table");
  1658. goto got_exception;
  1659. }
  1660. if (element_indices >= table_inst->cur_size) {
  1661. wasm_set_exception(module_inst, "undefined element");
  1662. goto got_exception;
  1663. }
  1664. /**
  1665. * please be aware that table_inst->base_addr may point
  1666. * to another module's table
  1667. **/
  1668. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1669. if (function_indices == 0xFFFFFFFF) {
  1670. wasm_set_exception(module_inst, "uninitialized element");
  1671. goto got_exception;
  1672. }
  1673. /**
  1674. * we insist to call functions owned by the module itself
  1675. **/
  1676. if (function_indices >= module_inst->function_count) {
  1677. wasm_set_exception(module_inst, "unknown function");
  1678. goto got_exception;
  1679. }
  1680. function_inst = module_inst->functions + function_indices;
  1681. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1682. return !wasm_get_exception(module_inst) ? true : false;
  1683. got_exception:
  1684. return false;
  1685. }
  1686. #if WASM_ENABLE_THREAD_MGR != 0
  1687. bool
  1688. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1689. uint32 start_offset, uint32 size)
  1690. {
  1691. WASMModuleInstance *module_inst =
  1692. (WASMModuleInstance*)exec_env->module_inst;
  1693. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1694. uint32 data_end = module_inst->module->aux_data_end;
  1695. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1696. bool is_stack_before_data =
  1697. stack_bottom < data_end ? true : false;
  1698. /* Check the aux stack space, currently we don't allocate space in heap */
  1699. if ((is_stack_before_data && (size > start_offset))
  1700. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1701. return false;
  1702. if (stack_top_idx != (uint32)-1) {
  1703. /* The aux stack top is a wasm global,
  1704. set the initial value for the global */
  1705. uint8 *global_addr =
  1706. module_inst->global_data +
  1707. module_inst->globals[stack_top_idx].data_offset;
  1708. *(int32*)global_addr = start_offset;
  1709. /* The aux stack boundary is a constant value,
  1710. set the value to exec_env */
  1711. exec_env->aux_stack_boundary = start_offset - size;
  1712. return true;
  1713. }
  1714. return false;
  1715. }
  1716. bool
  1717. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1718. uint32 *start_offset, uint32 *size)
  1719. {
  1720. WASMModuleInstance *module_inst =
  1721. (WASMModuleInstance*)exec_env->module_inst;
  1722. /* The aux stack information is resolved in loader
  1723. and store in module */
  1724. uint32 stack_bottom =
  1725. module_inst->module->aux_stack_bottom;
  1726. uint32 total_aux_stack_size =
  1727. module_inst->module->aux_stack_size;
  1728. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  1729. if (start_offset)
  1730. *start_offset = stack_bottom;
  1731. if (size)
  1732. *size = total_aux_stack_size;
  1733. return true;
  1734. }
  1735. return false;
  1736. }
  1737. #endif
  1738. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1739. void
  1740. wasm_get_module_mem_consumption(const WASMModule *module,
  1741. WASMModuleMemConsumption *mem_conspn)
  1742. {
  1743. uint32 i, size;
  1744. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1745. mem_conspn->module_struct_size = sizeof(WASMModule);
  1746. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  1747. for (i = 0; i < module->type_count; i++) {
  1748. WASMType *type = module->types[i];
  1749. size = offsetof(WASMType, types) +
  1750. sizeof(uint8) * (type->param_count + type->result_count);
  1751. mem_conspn->types_size += size;
  1752. }
  1753. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  1754. mem_conspn->functions_size = sizeof(WASMFunction *)
  1755. * module->function_count;
  1756. for (i = 0; i < module->function_count; i++) {
  1757. WASMFunction *func = module->functions[i];
  1758. WASMType *type = func->func_type;
  1759. size = sizeof(WASMFunction) + func->local_count
  1760. + sizeof(uint16) * (type->param_count + func->local_count);
  1761. #if WASM_ENABLE_FAST_INTERP != 0
  1762. size += func->code_compiled_size
  1763. + sizeof(uint32) * func->const_cell_num;
  1764. #endif
  1765. mem_conspn->functions_size += size;
  1766. }
  1767. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  1768. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  1769. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  1770. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  1771. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  1772. * module->table_seg_count;
  1773. for (i = 0; i < module->table_seg_count; i++) {
  1774. WASMTableSeg *table_seg = &module->table_segments[i];
  1775. mem_conspn->tables_size += sizeof(uint32)
  1776. * table_seg->function_count;
  1777. }
  1778. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  1779. * module->data_seg_count;
  1780. for (i = 0; i < module->data_seg_count; i++) {
  1781. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  1782. }
  1783. if (module->const_str_list) {
  1784. StringNode *node = module->const_str_list, *node_next;
  1785. while (node) {
  1786. node_next = node->next;
  1787. mem_conspn->const_strs_size += sizeof(StringNode)
  1788. + strlen(node->str) + 1;
  1789. node = node_next;
  1790. }
  1791. }
  1792. mem_conspn->total_size += mem_conspn->module_struct_size;
  1793. mem_conspn->total_size += mem_conspn->types_size;
  1794. mem_conspn->total_size += mem_conspn->imports_size;
  1795. mem_conspn->total_size += mem_conspn->functions_size;
  1796. mem_conspn->total_size += mem_conspn->tables_size;
  1797. mem_conspn->total_size += mem_conspn->memories_size;
  1798. mem_conspn->total_size += mem_conspn->globals_size;
  1799. mem_conspn->total_size += mem_conspn->exports_size;
  1800. mem_conspn->total_size += mem_conspn->table_segs_size;
  1801. mem_conspn->total_size += mem_conspn->data_segs_size;
  1802. mem_conspn->total_size += mem_conspn->const_strs_size;
  1803. #if WASM_ENABLE_AOT != 0
  1804. mem_conspn->total_size += mem_conspn->aot_code_size;
  1805. #endif
  1806. }
  1807. void
  1808. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  1809. WASMModuleInstMemConsumption *mem_conspn)
  1810. {
  1811. uint32 i, size;
  1812. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1813. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  1814. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  1815. * module_inst->memory_count;
  1816. for (i = 0; i < module_inst->memory_count; i++) {
  1817. WASMMemoryInstance *memory = module_inst->memories[i];
  1818. size = sizeof(WASMMemoryInstance)
  1819. + memory->num_bytes_per_page * memory->cur_page_count;
  1820. mem_conspn->memories_size += size;
  1821. mem_conspn->app_heap_size += memory->heap_data_end
  1822. - memory->heap_data;
  1823. }
  1824. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  1825. * module_inst->table_count;
  1826. for (i = 0; i < module_inst->table_count; i++) {
  1827. WASMTableInstance *table = module_inst->tables[i];
  1828. size = offsetof(WASMTableInstance, base_addr)
  1829. + sizeof(uint32) * table->cur_size;
  1830. mem_conspn->tables_size += size;
  1831. }
  1832. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  1833. * module_inst->function_count;
  1834. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  1835. * module_inst->global_count;
  1836. if (module_inst->global_count > 0) {
  1837. WASMGlobalInstance *global =
  1838. &module_inst->globals[module_inst->global_count - 1];
  1839. mem_conspn->globals_size += global->data_offset
  1840. + wasm_value_type_size(global->type);
  1841. }
  1842. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  1843. * module_inst->export_func_count;
  1844. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  1845. mem_conspn->total_size += mem_conspn->memories_size;
  1846. mem_conspn->total_size += mem_conspn->functions_size;
  1847. mem_conspn->total_size += mem_conspn->tables_size;
  1848. mem_conspn->total_size += mem_conspn->globals_size;
  1849. mem_conspn->total_size += mem_conspn->exports_size;
  1850. }
  1851. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  1852. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1853. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1854. void
  1855. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  1856. {
  1857. WASMModuleInstance *module_inst =
  1858. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  1859. WASMInterpFrame *cur_frame =
  1860. wasm_exec_env_get_cur_frame(exec_env);
  1861. WASMFunctionInstance *func_inst;
  1862. const char *func_name = NULL;
  1863. uint32 n;
  1864. os_printf("\n");
  1865. for (n = 0; cur_frame && cur_frame->function; n++) {
  1866. func_inst = cur_frame->function;
  1867. if (func_inst->is_import_func) {
  1868. func_name = func_inst->u.func_import->field_name;
  1869. }
  1870. else {
  1871. func_name = func_inst->u.func->field_name;
  1872. }
  1873. /* function name not exported, print number instead */
  1874. if (func_name == NULL) {
  1875. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  1876. }
  1877. else {
  1878. os_printf("#%02d %s \n", n, func_name);
  1879. }
  1880. cur_frame = cur_frame->prev_frame;
  1881. }
  1882. os_printf("\n");
  1883. }
  1884. #endif /* end of WASM_ENABLE_CUSTOM_NAME_SECTION */