wasm_runtime.c 64 KB

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