wasm_runtime.c 60 KB

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