wasm_runtime.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. static void
  13. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  14. {
  15. if (error_buf != NULL)
  16. snprintf(error_buf, error_buf_size, "%s", string);
  17. }
  18. WASMModule*
  19. wasm_load(const uint8 *buf, uint32 size,
  20. char *error_buf, uint32 error_buf_size)
  21. {
  22. return wasm_loader_load(buf, size, error_buf, error_buf_size);
  23. }
  24. WASMModule*
  25. wasm_load_from_sections(WASMSection *section_list,
  26. char *error_buf, uint32_t error_buf_size)
  27. {
  28. return wasm_loader_load_from_sections(section_list,
  29. error_buf, error_buf_size);
  30. }
  31. void
  32. wasm_unload(WASMModule *module)
  33. {
  34. wasm_loader_unload(module);
  35. }
  36. /**
  37. * Destroy memory instances.
  38. */
  39. static void
  40. memories_deinstantiate(WASMMemoryInstance **memories, uint32 count)
  41. {
  42. uint32 i;
  43. if (memories) {
  44. for (i = 0; i < count; i++)
  45. if (memories[i]) {
  46. if (memories[i]->heap_handle)
  47. mem_allocator_destroy(memories[i]->heap_handle);
  48. wasm_runtime_free(memories[i]->heap_data);
  49. wasm_runtime_free(memories[i]);
  50. }
  51. wasm_runtime_free(memories);
  52. }
  53. }
  54. static WASMMemoryInstance*
  55. memory_instantiate(uint32 num_bytes_per_page,
  56. uint32 init_page_count, uint32 max_page_count,
  57. uint32 global_data_size,
  58. uint32 heap_size,
  59. char *error_buf, uint32 error_buf_size)
  60. {
  61. WASMMemoryInstance *memory;
  62. uint64 total_size = offsetof(WASMMemoryInstance, base_addr) +
  63. num_bytes_per_page * (uint64)init_page_count +
  64. global_data_size;
  65. /* Allocate memory space, addr data and global data */
  66. if (total_size >= UINT32_MAX
  67. || !(memory = wasm_runtime_malloc((uint32)total_size))) {
  68. set_error_buf(error_buf, error_buf_size,
  69. "Instantiate memory failed: allocate memory failed.");
  70. return NULL;
  71. }
  72. memset(memory, 0, (uint32)total_size);
  73. memory->num_bytes_per_page = num_bytes_per_page;
  74. memory->cur_page_count = init_page_count;
  75. memory->max_page_count = max_page_count;
  76. memory->memory_data = memory->base_addr;
  77. memory->global_data = memory->memory_data +
  78. num_bytes_per_page * memory->cur_page_count;
  79. memory->global_data_size = global_data_size;
  80. memory->end_addr = memory->global_data + global_data_size;
  81. /* Allocate heap space */
  82. if (!(memory->heap_data = wasm_runtime_malloc(heap_size))) {
  83. set_error_buf(error_buf, error_buf_size,
  84. "Instantiate memory failed: allocate memory failed.");
  85. goto fail1;
  86. }
  87. memory->heap_data_end = memory->heap_data + heap_size;
  88. /* Initialize heap */
  89. if (!(memory->heap_handle = mem_allocator_create
  90. (memory->heap_data, heap_size))) {
  91. goto fail2;
  92. }
  93. #if WASM_ENABLE_MEMORY_GROW != 0
  94. memory->heap_base_offset = DEFAULT_APP_HEAP_BASE_OFFSET;
  95. #else
  96. memory->heap_base_offset = memory->end_addr - memory->memory_data;
  97. #endif
  98. return memory;
  99. fail2:
  100. wasm_runtime_free(memory->heap_data);
  101. fail1:
  102. wasm_runtime_free(memory);
  103. return NULL;
  104. }
  105. /**
  106. * Instantiate memories in a module.
  107. */
  108. static WASMMemoryInstance**
  109. memories_instantiate(const WASMModule *module,
  110. uint32 global_data_size, uint32 heap_size,
  111. char *error_buf, uint32 error_buf_size)
  112. {
  113. WASMImport *import;
  114. uint32 mem_index = 0, i, memory_count =
  115. module->import_memory_count + module->memory_count;
  116. uint64 total_size;
  117. WASMMemoryInstance **memories, *memory;
  118. if (memory_count == 0 && global_data_size > 0)
  119. memory_count = 1;
  120. total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
  121. if (total_size >= UINT32_MAX
  122. || !(memories = wasm_runtime_malloc((uint32)total_size))) {
  123. set_error_buf(error_buf, error_buf_size,
  124. "Instantiate memory failed: "
  125. "allocate memory failed.");
  126. return NULL;
  127. }
  128. memset(memories, 0, (uint32)total_size);
  129. /* instantiate memories from import section */
  130. import = module->import_memories;
  131. for (i = 0; i < module->import_memory_count; i++, import++) {
  132. if (!(memory = memories[mem_index++] =
  133. memory_instantiate(import->u.memory.num_bytes_per_page,
  134. import->u.memory.init_page_count,
  135. import->u.memory. max_page_count,
  136. global_data_size,
  137. heap_size, error_buf, error_buf_size))) {
  138. set_error_buf(error_buf, error_buf_size,
  139. "Instantiate memory failed: "
  140. "allocate memory failed.");
  141. memories_deinstantiate(memories, memory_count);
  142. return NULL;
  143. }
  144. }
  145. /* instantiate memories from memory section */
  146. for (i = 0; i < module->memory_count; i++) {
  147. if (!(memory = memories[mem_index++] =
  148. memory_instantiate(module->memories[i].num_bytes_per_page,
  149. module->memories[i].init_page_count,
  150. module->memories[i].max_page_count,
  151. global_data_size,
  152. heap_size, error_buf, error_buf_size))) {
  153. set_error_buf(error_buf, error_buf_size,
  154. "Instantiate memory failed: "
  155. "allocate memory failed.");
  156. memories_deinstantiate(memories, memory_count);
  157. return NULL;
  158. }
  159. }
  160. if (mem_index == 0) {
  161. /* no import memory and define memory, but has global variables */
  162. if (!(memory = memories[mem_index++] =
  163. memory_instantiate(0, 0, 0, global_data_size,
  164. heap_size, error_buf, error_buf_size))) {
  165. set_error_buf(error_buf, error_buf_size,
  166. "Instantiate memory failed: "
  167. "allocate memory failed.\n");
  168. memories_deinstantiate(memories, memory_count);
  169. return NULL;
  170. }
  171. }
  172. bh_assert(mem_index == memory_count);
  173. return memories;
  174. }
  175. /**
  176. * Destroy table instances.
  177. */
  178. static void
  179. tables_deinstantiate(WASMTableInstance **tables, uint32 count)
  180. {
  181. uint32 i;
  182. if (tables) {
  183. for (i = 0; i < count; i++)
  184. if (tables[i])
  185. wasm_runtime_free(tables[i]);
  186. wasm_runtime_free(tables);
  187. }
  188. }
  189. /**
  190. * Instantiate tables in a module.
  191. */
  192. static WASMTableInstance**
  193. tables_instantiate(const WASMModule *module,
  194. char *error_buf, uint32 error_buf_size)
  195. {
  196. WASMImport *import;
  197. uint32 table_index = 0, i, table_count =
  198. module->import_table_count + module->table_count;
  199. uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  200. WASMTableInstance **tables, *table;
  201. if (total_size >= UINT32_MAX
  202. || !(tables = wasm_runtime_malloc((uint32)total_size))) {
  203. set_error_buf(error_buf, error_buf_size,
  204. "Instantiate table failed: "
  205. "allocate memory failed.");
  206. return NULL;
  207. }
  208. memset(tables, 0, (uint32)total_size);
  209. /* instantiate tables from import section */
  210. import = module->import_tables;
  211. for (i = 0; i < module->import_table_count; i++, import++) {
  212. total_size = offsetof(WASMTableInstance, base_addr) +
  213. sizeof(uint32) * (uint64)import->u.table.init_size;
  214. if (total_size >= UINT32_MAX
  215. || !(table = tables[table_index++] =
  216. wasm_runtime_malloc((uint32)total_size))) {
  217. set_error_buf(error_buf, error_buf_size,
  218. "Instantiate table failed: "
  219. "allocate memory failed.");
  220. tables_deinstantiate(tables, table_count);
  221. return NULL;
  222. }
  223. /* Set all elements to -1 to mark them as uninitialized elements */
  224. memset(table, -1, (uint32)total_size);
  225. table->elem_type = import->u.table.elem_type;
  226. table->cur_size = import->u.table.init_size;
  227. table->max_size = import->u.table.max_size;
  228. }
  229. /* instantiate tables from table section */
  230. for (i = 0; i < module->table_count; i++) {
  231. total_size = offsetof(WASMTableInstance, base_addr) +
  232. sizeof(uint32) * (uint64)module->tables[i].init_size;
  233. if (total_size >= UINT32_MAX
  234. || !(table = tables[table_index++] =
  235. wasm_runtime_malloc((uint32)total_size))) {
  236. set_error_buf(error_buf, error_buf_size,
  237. "Instantiate table failed: "
  238. "allocate memory failed.");
  239. tables_deinstantiate(tables, table_count);
  240. return NULL;
  241. }
  242. /* Set all elements to -1 to mark them as uninitialized elements */
  243. memset(table, -1, (uint32)total_size);
  244. table->elem_type = module->tables[i].elem_type;
  245. table->cur_size = module->tables[i].init_size;
  246. table->max_size = module->tables[i].max_size;
  247. }
  248. bh_assert(table_index == table_count);
  249. return tables;
  250. }
  251. /**
  252. * Destroy function instances.
  253. */
  254. static void
  255. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  256. {
  257. if (functions) {
  258. wasm_runtime_free(functions);
  259. }
  260. }
  261. /**
  262. * Instantiate functions in a module.
  263. */
  264. static WASMFunctionInstance*
  265. functions_instantiate(const WASMModule *module,
  266. char *error_buf, uint32 error_buf_size)
  267. {
  268. WASMImport *import;
  269. uint32 i, function_count =
  270. module->import_function_count + module->function_count;
  271. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  272. WASMFunctionInstance *functions, *function;
  273. if (total_size >= UINT32_MAX
  274. || !(functions = wasm_runtime_malloc((uint32)total_size))) {
  275. set_error_buf(error_buf, error_buf_size,
  276. "Instantiate function failed: "
  277. "allocate memory failed.");
  278. return NULL;
  279. }
  280. memset(functions, 0, (uint32)total_size);
  281. /* instantiate functions from import section */
  282. function = functions;
  283. import = module->import_functions;
  284. for (i = 0; i < module->import_function_count; i++, import++) {
  285. function->is_import_func = true;
  286. function->u.func_import = &import->u.function;
  287. function->param_cell_num =
  288. wasm_type_param_cell_num(import->u.function.func_type);
  289. function->ret_cell_num =
  290. wasm_type_return_cell_num(import->u.function.func_type);
  291. function->local_cell_num = 0;
  292. function->param_count =
  293. (uint16)function->u.func_import->func_type->param_count;
  294. function->local_count = 0;
  295. function->param_types = function->u.func_import->func_type->types;
  296. function->local_types = NULL;
  297. function++;
  298. }
  299. /* instantiate functions from function section */
  300. for (i = 0; i < module->function_count; i++) {
  301. function->is_import_func = false;
  302. function->u.func = module->functions[i];
  303. function->param_cell_num = function->u.func->param_cell_num;
  304. function->ret_cell_num = function->u.func->ret_cell_num;
  305. function->local_cell_num = function->u.func->local_cell_num;
  306. function->param_count = (uint16)function->u.func->func_type->param_count;
  307. function->local_count = (uint16)function->u.func->local_count;
  308. function->param_types = function->u.func->func_type->types;
  309. function->local_types = function->u.func->local_types;
  310. function->local_offsets = function->u.func->local_offsets;
  311. #if WASM_ENABLE_FAST_INTERP != 0
  312. function->const_cell_num = function->u.func->const_cell_num;
  313. #endif
  314. function++;
  315. }
  316. bh_assert((uint32)(function - functions) == function_count);
  317. return functions;
  318. }
  319. /**
  320. * Destroy global instances.
  321. */
  322. static void
  323. globals_deinstantiate(WASMGlobalInstance *globals)
  324. {
  325. if (globals)
  326. wasm_runtime_free(globals);
  327. }
  328. /**
  329. * Instantiate globals in a module.
  330. */
  331. static WASMGlobalInstance*
  332. globals_instantiate(const WASMModule *module,
  333. uint32 *p_global_data_size,
  334. char *error_buf, uint32 error_buf_size)
  335. {
  336. WASMImport *import;
  337. uint32 global_data_offset = 0;
  338. uint32 i, global_count =
  339. module->import_global_count + module->global_count;
  340. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  341. WASMGlobalInstance *globals, *global;
  342. if (total_size >= UINT32_MAX
  343. || !(globals = wasm_runtime_malloc((uint32)total_size))) {
  344. set_error_buf(error_buf, error_buf_size,
  345. "Instantiate global failed: "
  346. "allocate memory failed.");
  347. return NULL;
  348. }
  349. memset(globals, 0, (uint32)total_size);
  350. /* instantiate globals from import section */
  351. global = globals;
  352. import = module->import_globals;
  353. for (i = 0; i < module->import_global_count; i++, import++) {
  354. WASMGlobalImport *global_import = &import->u.global;
  355. global->type = global_import->type;
  356. global->is_mutable = global_import->is_mutable;
  357. global->initial_value = global_import->global_data_linked;
  358. global->data_offset = global_data_offset;
  359. global_data_offset += wasm_value_type_size(global->type);
  360. global++;
  361. }
  362. /* instantiate globals from global section */
  363. for (i = 0; i < module->global_count; i++) {
  364. global->type = module->globals[i].type;
  365. global->is_mutable = module->globals[i].is_mutable;
  366. global->data_offset = global_data_offset;
  367. global_data_offset += wasm_value_type_size(global->type);
  368. global++;
  369. }
  370. bh_assert((uint32)(global - globals) == global_count);
  371. *p_global_data_size = global_data_offset;
  372. return globals;
  373. }
  374. static void
  375. globals_instantiate_fix(WASMGlobalInstance *globals,
  376. const WASMModule *module,
  377. WASMModuleInstance *module_inst)
  378. {
  379. WASMGlobalInstance *global = globals;
  380. WASMImport *import = module->import_globals;
  381. uint32 i;
  382. /* Fix globals from import section */
  383. for (i = 0; i < module->import_global_count; i++, import++, global++) {
  384. if (!strcmp(import->u.names.module_name, "env")) {
  385. if (!strcmp(import->u.names.field_name, "memoryBase")
  386. || !strcmp(import->u.names.field_name, "__memory_base")) {
  387. global->initial_value.addr = 0;
  388. }
  389. else if (!strcmp(import->u.names.field_name, "tableBase")
  390. || !strcmp(import->u.names.field_name, "__table_base")) {
  391. global->initial_value.addr = 0;
  392. }
  393. else if (!strcmp(import->u.names.field_name, "DYNAMICTOP_PTR")) {
  394. global->initial_value.i32 = (int32)
  395. (module_inst->default_memory->num_bytes_per_page
  396. * module_inst->default_memory->cur_page_count);
  397. module_inst->DYNAMICTOP_PTR_offset = global->data_offset;
  398. }
  399. else if (!strcmp(import->u.names.field_name, "STACKTOP")) {
  400. global->initial_value.i32 = 0;
  401. }
  402. else if (!strcmp(import->u.names.field_name, "STACK_MAX")) {
  403. /* Unused in emcc wasm bin actually. */
  404. global->initial_value.i32 = 0;
  405. }
  406. }
  407. }
  408. for (i = 0; i < module->global_count; i++) {
  409. InitializerExpression *init_expr = &module->globals[i].init_expr;
  410. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  411. bh_assert(init_expr->u.global_index < module->import_global_count);
  412. global->initial_value = globals[init_expr->u.global_index].initial_value;
  413. }
  414. else {
  415. bh_memcpy_s(&global->initial_value, sizeof(WASMValue),
  416. &init_expr->u, sizeof(init_expr->u));
  417. }
  418. global++;
  419. }
  420. }
  421. /**
  422. * Return export function count in module export section.
  423. */
  424. static uint32
  425. get_export_function_count(const WASMModule *module)
  426. {
  427. WASMExport *export = module->exports;
  428. uint32 count = 0, i;
  429. for (i = 0; i < module->export_count; i++, export++)
  430. if (export->kind == EXPORT_KIND_FUNC)
  431. count++;
  432. return count;
  433. }
  434. /**
  435. * Destroy export function instances.
  436. */
  437. static void
  438. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  439. {
  440. if (functions)
  441. wasm_runtime_free(functions);
  442. }
  443. /**
  444. * Instantiate export functions in a module.
  445. */
  446. static WASMExportFuncInstance*
  447. export_functions_instantiate(const WASMModule *module,
  448. WASMModuleInstance *module_inst,
  449. uint32 export_func_count,
  450. char *error_buf, uint32 error_buf_size)
  451. {
  452. WASMExportFuncInstance *export_funcs, *export_func;
  453. WASMExport *export = module->exports;
  454. uint32 i;
  455. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  456. if (total_size >= UINT32_MAX
  457. || !(export_func = export_funcs = wasm_runtime_malloc((uint32)total_size))) {
  458. set_error_buf(error_buf, error_buf_size,
  459. "Instantiate export function failed: "
  460. "allocate memory failed.");
  461. return NULL;
  462. }
  463. memset(export_funcs, 0, (uint32)total_size);
  464. for (i = 0; i < module->export_count; i++, export++)
  465. if (export->kind == EXPORT_KIND_FUNC) {
  466. export_func->name = export->name;
  467. export_func->function = &module_inst->functions[export->index];
  468. export_func++;
  469. }
  470. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  471. return export_funcs;
  472. }
  473. static bool
  474. execute_post_inst_function(WASMModuleInstance *module_inst)
  475. {
  476. WASMFunctionInstance *post_inst_func = NULL;
  477. WASMType *post_inst_func_type;
  478. uint32 i;
  479. for (i = 0; i < module_inst->export_func_count; i++)
  480. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  481. post_inst_func = module_inst->export_functions[i].function;
  482. break;
  483. }
  484. if (!post_inst_func)
  485. /* Not found */
  486. return true;
  487. post_inst_func_type = post_inst_func->u.func->func_type;
  488. if (post_inst_func_type->param_count != 0
  489. || post_inst_func_type->result_count != 0)
  490. /* Not a valid function type, ignore it */
  491. return true;
  492. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  493. 0, NULL);
  494. }
  495. static bool
  496. execute_start_function(WASMModuleInstance *module_inst)
  497. {
  498. WASMFunctionInstance *func = module_inst->start_function;
  499. if (!func)
  500. return true;
  501. bh_assert(!func->is_import_func && func->param_cell_num == 0
  502. && func->ret_cell_num == 0);
  503. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  504. }
  505. /**
  506. * Instantiate module
  507. */
  508. WASMModuleInstance*
  509. wasm_instantiate(WASMModule *module,
  510. uint32 stack_size, uint32 heap_size,
  511. char *error_buf, uint32 error_buf_size)
  512. {
  513. WASMModuleInstance *module_inst;
  514. WASMTableSeg *table_seg;
  515. WASMDataSeg *data_seg;
  516. WASMGlobalInstance *globals = NULL, *global;
  517. uint32 global_count, global_data_size = 0, i, j;
  518. uint32 base_offset, length, memory_size;
  519. uint8 *global_data, *global_data_end;
  520. uint8 *memory_data;
  521. uint32 *table_data;
  522. if (!module)
  523. return NULL;
  524. /* Check heap size */
  525. heap_size = align_uint(heap_size, 8);
  526. if (heap_size == 0)
  527. heap_size = APP_HEAP_SIZE_DEFAULT;
  528. if (heap_size < APP_HEAP_SIZE_MIN)
  529. heap_size = APP_HEAP_SIZE_MIN;
  530. if (heap_size > APP_HEAP_SIZE_MAX)
  531. heap_size = APP_HEAP_SIZE_MAX;
  532. /* Instantiate global firstly to get the mutable data size */
  533. global_count = module->import_global_count + module->global_count;
  534. if (global_count &&
  535. !(globals = globals_instantiate(module,
  536. &global_data_size,
  537. error_buf, error_buf_size)))
  538. return NULL;
  539. /* Allocate the memory */
  540. if (!(module_inst = wasm_runtime_malloc((uint32)sizeof(WASMModuleInstance)))) {
  541. set_error_buf(error_buf, error_buf_size,
  542. "Instantiate module failed: allocate memory failed.");
  543. globals_deinstantiate(globals);
  544. return NULL;
  545. }
  546. memset(module_inst, 0, (uint32)sizeof(WASMModuleInstance));
  547. module_inst->global_count = global_count;
  548. module_inst->globals = globals;
  549. module_inst->memory_count =
  550. module->import_memory_count + module->memory_count;
  551. module_inst->table_count =
  552. module->import_table_count + module->table_count;
  553. module_inst->function_count =
  554. module->import_function_count + module->function_count;
  555. module_inst->export_func_count = get_export_function_count(module);
  556. /* Instantiate memories/tables/functions */
  557. if (((module_inst->memory_count > 0 || global_count > 0)
  558. && !(module_inst->memories =
  559. memories_instantiate(module, global_data_size,
  560. heap_size, error_buf, error_buf_size)))
  561. || (module_inst->table_count > 0
  562. && !(module_inst->tables = tables_instantiate(module,
  563. error_buf,
  564. error_buf_size)))
  565. || (module_inst->function_count > 0
  566. && !(module_inst->functions = functions_instantiate(module,
  567. error_buf,
  568. error_buf_size)))
  569. || (module_inst->export_func_count > 0
  570. && !(module_inst->export_functions = export_functions_instantiate(
  571. module, module_inst, module_inst->export_func_count,
  572. error_buf, error_buf_size)))) {
  573. wasm_deinstantiate(module_inst);
  574. return NULL;
  575. }
  576. if (module_inst->memory_count || global_count > 0) {
  577. WASMMemoryInstance *memory;
  578. memory = module_inst->default_memory = module_inst->memories[0];
  579. memory_data = module_inst->default_memory->memory_data;
  580. /* fix import memoryBase */
  581. globals_instantiate_fix(globals, module, module_inst);
  582. /* Initialize the global data */
  583. global_data = memory->global_data;
  584. global_data_end = global_data + global_data_size;
  585. global = globals;
  586. for (i = 0; i < global_count; i++, global++) {
  587. switch (global->type) {
  588. case VALUE_TYPE_I32:
  589. case VALUE_TYPE_F32:
  590. *(int32*)global_data = global->initial_value.i32;
  591. global_data += sizeof(int32);
  592. break;
  593. case VALUE_TYPE_I64:
  594. case VALUE_TYPE_F64:
  595. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  596. &global->initial_value.i64, sizeof(int64));
  597. global_data += sizeof(int64);
  598. break;
  599. default:
  600. bh_assert(0);
  601. }
  602. }
  603. bh_assert(global_data == global_data_end);
  604. /* Initialize the memory data with data segment section */
  605. if (module_inst->default_memory->cur_page_count > 0) {
  606. for (i = 0; i < module->data_seg_count; i++) {
  607. data_seg = module->data_segments[i];
  608. bh_assert(data_seg->memory_index == 0);
  609. bh_assert(data_seg->base_offset.init_expr_type ==
  610. INIT_EXPR_TYPE_I32_CONST
  611. || data_seg->base_offset.init_expr_type ==
  612. INIT_EXPR_TYPE_GET_GLOBAL);
  613. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  614. bh_assert(data_seg->base_offset.u.global_index < global_count
  615. && globals[data_seg->base_offset.u.global_index].type ==
  616. VALUE_TYPE_I32);
  617. data_seg->base_offset.u.i32 =
  618. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  619. }
  620. base_offset = (uint32)data_seg->base_offset.u.i32;
  621. length = data_seg->data_length;
  622. memory_size = module_inst->default_memory->num_bytes_per_page
  623. * module_inst->default_memory->cur_page_count;
  624. if (length > 0
  625. && (base_offset >= memory_size
  626. || base_offset + length > memory_size)) {
  627. set_error_buf(error_buf, error_buf_size,
  628. "Instantiate module failed: data segment out of range.");
  629. wasm_deinstantiate(module_inst);
  630. return NULL;
  631. }
  632. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  633. data_seg->data, length);
  634. }
  635. }
  636. }
  637. if (module_inst->table_count) {
  638. module_inst->default_table = module_inst->tables[0];
  639. /* Initialize the table data with table segment section */
  640. table_data = (uint32*)module_inst->default_table->base_addr;
  641. table_seg = module->table_segments;
  642. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  643. bh_assert(table_seg->table_index == 0);
  644. bh_assert(table_seg->base_offset.init_expr_type ==
  645. INIT_EXPR_TYPE_I32_CONST
  646. || table_seg->base_offset.init_expr_type ==
  647. INIT_EXPR_TYPE_GET_GLOBAL);
  648. if (table_seg->base_offset.init_expr_type ==
  649. INIT_EXPR_TYPE_GET_GLOBAL) {
  650. bh_assert(table_seg->base_offset.u.global_index < global_count
  651. && globals[table_seg->base_offset.u.global_index].type ==
  652. VALUE_TYPE_I32);
  653. table_seg->base_offset.u.i32 =
  654. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  655. }
  656. if ((uint32)table_seg->base_offset.u.i32 <
  657. module_inst->default_table->cur_size) {
  658. length = table_seg->function_count;
  659. if ((uint32)table_seg->base_offset.u.i32 + length >
  660. module_inst->default_table->cur_size)
  661. length = module_inst->default_table->cur_size
  662. - (uint32)table_seg->base_offset.u.i32;
  663. /* Check function index */
  664. for (j = 0; j < length; j++) {
  665. if (table_seg->func_indexes[j] >= module_inst->function_count) {
  666. set_error_buf(error_buf, error_buf_size,
  667. "function index is overflow");
  668. wasm_deinstantiate(module_inst);
  669. return NULL;
  670. }
  671. }
  672. bh_memcpy_s(table_data + table_seg->base_offset.u.i32,
  673. (uint32)((module_inst->default_table->cur_size
  674. - (uint32)table_seg->base_offset.u.i32)
  675. * sizeof(uint32)),
  676. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  677. }
  678. }
  679. }
  680. #if WASM_ENABLE_LIBC_WASI != 0
  681. if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  682. module->wasi_args.dir_list,
  683. module->wasi_args.dir_count,
  684. module->wasi_args.map_dir_list,
  685. module->wasi_args.map_dir_count,
  686. module->wasi_args.env,
  687. module->wasi_args.env_count,
  688. module->wasi_args.argv,
  689. module->wasi_args.argc,
  690. error_buf, error_buf_size)) {
  691. wasm_deinstantiate(module_inst);
  692. return NULL;
  693. }
  694. #endif
  695. if (module->start_function != (uint32)-1) {
  696. bh_assert(module->start_function >= module->import_function_count);
  697. module_inst->start_function =
  698. &module_inst->functions[module->start_function];
  699. }
  700. module_inst->module = module;
  701. /* module instance type */
  702. module_inst->module_type = Wasm_Module_Bytecode;
  703. /* Initialize the thread related data */
  704. if (stack_size == 0)
  705. stack_size = DEFAULT_WASM_STACK_SIZE;
  706. module_inst->default_wasm_stack_size = stack_size;
  707. /* Execute __post_instantiate function */
  708. if (!execute_post_inst_function(module_inst)
  709. || !execute_start_function(module_inst)) {
  710. set_error_buf(error_buf, error_buf_size,
  711. module_inst->cur_exception);
  712. wasm_deinstantiate(module_inst);
  713. return NULL;
  714. }
  715. (void)global_data_end;
  716. return module_inst;
  717. }
  718. void
  719. wasm_deinstantiate(WASMModuleInstance *module_inst)
  720. {
  721. if (!module_inst)
  722. return;
  723. #if WASM_ENABLE_LIBC_WASI != 0
  724. /* Destroy wasi resource before freeing app heap, since some fields of
  725. wasi contex are allocated from app heap, and if app heap is freed,
  726. these fields will be set to NULL, we cannot free their internal data
  727. which may allocated from global heap. */
  728. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  729. #endif
  730. if (module_inst->memory_count > 0)
  731. memories_deinstantiate(module_inst->memories, module_inst->memory_count);
  732. else if (module_inst->memories != NULL && module_inst->global_count > 0)
  733. /* No imported memory and defined memory, the memory is created when
  734. global count > 0. */
  735. memories_deinstantiate(module_inst->memories, 1);
  736. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  737. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  738. globals_deinstantiate(module_inst->globals);
  739. export_functions_deinstantiate(module_inst->export_functions);
  740. wasm_runtime_free(module_inst);
  741. }
  742. WASMFunctionInstance*
  743. wasm_lookup_function(const WASMModuleInstance *module_inst,
  744. const char *name, const char *signature)
  745. {
  746. uint32 i;
  747. for (i = 0; i < module_inst->export_func_count; i++)
  748. if (!strcmp(module_inst->export_functions[i].name, name))
  749. return module_inst->export_functions[i].function;
  750. (void)signature;
  751. return NULL;
  752. }
  753. bool
  754. wasm_call_function(WASMExecEnv *exec_env,
  755. WASMFunctionInstance *function,
  756. unsigned argc, uint32 argv[])
  757. {
  758. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  759. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  760. return !wasm_get_exception(module_inst) ? true : false;
  761. }
  762. bool
  763. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  764. WASMFunctionInstance *func,
  765. unsigned argc, uint32 argv[])
  766. {
  767. WASMExecEnv *exec_env;
  768. bool ret;
  769. if (!(exec_env = wasm_exec_env_create((WASMModuleInstanceCommon*)module_inst,
  770. module_inst->default_wasm_stack_size))) {
  771. wasm_set_exception(module_inst, "allocate memory failed.");
  772. return false;
  773. }
  774. ret = wasm_call_function(exec_env, func, argc, argv);
  775. wasm_exec_env_destroy(exec_env);
  776. return ret;
  777. }
  778. void
  779. wasm_set_exception(WASMModuleInstance *module_inst,
  780. const char *exception)
  781. {
  782. if (exception)
  783. snprintf(module_inst->cur_exception,
  784. sizeof(module_inst->cur_exception),
  785. "Exception: %s", exception);
  786. else
  787. module_inst->cur_exception[0] = '\0';
  788. }
  789. const char*
  790. wasm_get_exception(WASMModuleInstance *module_inst)
  791. {
  792. if (module_inst->cur_exception[0] == '\0')
  793. return NULL;
  794. else
  795. return module_inst->cur_exception;
  796. }
  797. int32
  798. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  799. void **p_native_addr)
  800. {
  801. WASMMemoryInstance *memory = module_inst->default_memory;
  802. uint8 *addr = mem_allocator_malloc(memory->heap_handle, size);
  803. if (p_native_addr)
  804. *p_native_addr = addr;
  805. if (!addr) {
  806. wasm_set_exception(module_inst, "out of memory");
  807. return 0;
  808. }
  809. return memory->heap_base_offset + (int32)(addr - memory->heap_data);
  810. }
  811. void
  812. wasm_module_free(WASMModuleInstance *module_inst, int32 ptr)
  813. {
  814. if (ptr) {
  815. WASMMemoryInstance *memory = module_inst->default_memory;
  816. uint8 *addr = memory->heap_data + (ptr - memory->heap_base_offset);
  817. if (memory->heap_data < addr && addr < memory->heap_data_end)
  818. mem_allocator_free(memory->heap_handle, addr);
  819. }
  820. }
  821. int32
  822. wasm_module_dup_data(WASMModuleInstance *module_inst,
  823. const char *src, uint32 size)
  824. {
  825. char *buffer;
  826. int32 buffer_offset = wasm_module_malloc(module_inst, size,
  827. (void**)&buffer);
  828. if (buffer_offset != 0) {
  829. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  830. bh_memcpy_s(buffer, size, src, size);
  831. }
  832. return buffer_offset;
  833. }
  834. bool
  835. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  836. int32 app_offset, uint32 size)
  837. {
  838. WASMMemoryInstance *memory;
  839. uint8 *addr;
  840. /* integer overflow check */
  841. if(app_offset + (int32)size < app_offset) {
  842. goto fail;
  843. }
  844. memory = module_inst->default_memory;
  845. if (0 <= app_offset
  846. && app_offset < memory->heap_base_offset) {
  847. addr = memory->memory_data + app_offset;
  848. if (!(memory->base_addr <= addr && addr + size <= memory->end_addr))
  849. goto fail;
  850. return true;
  851. }
  852. else if (memory->heap_base_offset < app_offset
  853. && app_offset < memory->heap_base_offset
  854. + (memory->heap_data_end - memory->heap_data)) {
  855. addr = memory->heap_data + (app_offset - memory->heap_base_offset);
  856. if (!(memory->heap_data <= addr && addr + size <= memory->heap_data_end))
  857. goto fail;
  858. return true;
  859. }
  860. fail:
  861. wasm_set_exception(module_inst, "out of bounds memory access");
  862. return false;
  863. }
  864. bool
  865. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  866. void *native_ptr, uint32 size)
  867. {
  868. uint8 *addr = native_ptr;
  869. WASMMemoryInstance *memory = module_inst->default_memory;
  870. if (addr + size < addr) {
  871. goto fail;
  872. }
  873. if ((memory->base_addr <= addr && addr + size <= memory->end_addr)
  874. || (memory->heap_data <= addr && addr + size <= memory->heap_data_end)
  875. )
  876. return true;
  877. fail:
  878. wasm_set_exception(module_inst, "out of bounds memory access");
  879. return false;
  880. }
  881. void *
  882. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  883. int32 app_offset)
  884. {
  885. WASMMemoryInstance *memory = module_inst->default_memory;
  886. if (0 <= app_offset && app_offset < memory->heap_base_offset)
  887. return memory->memory_data + app_offset;
  888. else if (memory->heap_base_offset < app_offset
  889. && app_offset < memory->heap_base_offset
  890. + (memory->heap_data_end - memory->heap_data))
  891. return memory->heap_data + (app_offset - memory->heap_base_offset);
  892. else
  893. return NULL;
  894. }
  895. int32
  896. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  897. void *native_ptr)
  898. {
  899. WASMMemoryInstance *memory = module_inst->default_memory;
  900. if (memory->base_addr <= (uint8*)native_ptr
  901. && (uint8*)native_ptr < memory->end_addr)
  902. return (int32)((uint8*)native_ptr - memory->memory_data);
  903. else if (memory->heap_data <= (uint8*)native_ptr
  904. && (uint8*)native_ptr < memory->heap_data_end)
  905. return memory->heap_base_offset
  906. + (int32)((uint8*)native_ptr - memory->heap_data);
  907. else
  908. return 0;
  909. }
  910. bool
  911. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  912. int32 app_offset,
  913. int32 *p_app_start_offset,
  914. int32 *p_app_end_offset)
  915. {
  916. int32 app_start_offset, app_end_offset;
  917. WASMMemoryInstance *memory = module_inst->default_memory;
  918. if (0 <= app_offset && app_offset < memory->heap_base_offset) {
  919. app_start_offset = 0;
  920. app_end_offset = (int32)(memory->num_bytes_per_page * memory->cur_page_count);
  921. }
  922. else if (memory->heap_base_offset < app_offset
  923. && app_offset < memory->heap_base_offset
  924. + (memory->heap_data_end - memory->heap_data)) {
  925. app_start_offset = memory->heap_base_offset;
  926. app_end_offset = memory->heap_base_offset
  927. + (int32)(memory->heap_data_end - memory->heap_data);
  928. }
  929. else
  930. return false;
  931. if (p_app_start_offset)
  932. *p_app_start_offset = app_start_offset;
  933. if (p_app_end_offset)
  934. *p_app_end_offset = app_end_offset;
  935. return true;
  936. }
  937. bool
  938. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  939. uint8 *native_ptr,
  940. uint8 **p_native_start_addr,
  941. uint8 **p_native_end_addr)
  942. {
  943. uint8 *native_start_addr, *native_end_addr;
  944. WASMMemoryInstance *memory = module_inst->default_memory;
  945. if (memory->base_addr <= (uint8*)native_ptr
  946. && (uint8*)native_ptr < memory->end_addr) {
  947. native_start_addr = memory->memory_data;
  948. native_end_addr = memory->memory_data
  949. + memory->num_bytes_per_page * memory->cur_page_count;
  950. }
  951. else if (memory->heap_data <= (uint8*)native_ptr
  952. && (uint8*)native_ptr < memory->heap_data_end) {
  953. native_start_addr = memory->heap_data;
  954. native_end_addr = memory->heap_data_end;
  955. }
  956. else
  957. return false;
  958. if (p_native_start_addr)
  959. *p_native_start_addr = native_start_addr;
  960. if (p_native_end_addr)
  961. *p_native_end_addr = native_end_addr;
  962. return true;
  963. }
  964. bool
  965. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  966. {
  967. #if WASM_ENABLE_MEMORY_GROW != 0
  968. WASMMemoryInstance *memory = module->default_memory, *new_memory;
  969. uint32 old_page_count = memory->cur_page_count, total_size_old;
  970. uint32 total_page_count = inc_page_count + memory->cur_page_count;
  971. uint64 total_size = offsetof(WASMMemoryInstance, base_addr) +
  972. memory->num_bytes_per_page * (uint64)total_page_count +
  973. memory->global_data_size;
  974. uint8 *global_data_old;
  975. if (inc_page_count <= 0)
  976. /* No need to enlarge memory */
  977. return true;
  978. if (total_page_count < memory->cur_page_count /* integer overflow */
  979. || total_page_count > memory->max_page_count) {
  980. wasm_set_exception(module, "fail to enlarge memory.");
  981. return false;
  982. }
  983. if (total_size >= UINT32_MAX) {
  984. wasm_set_exception(module, "fail to enlarge memory.");
  985. return false;
  986. }
  987. if (!(new_memory = wasm_runtime_realloc(memory, (uint32)total_size))) {
  988. if (!(new_memory = wasm_runtime_malloc((uint32)total_size))) {
  989. wasm_set_exception(module, "fail to enlarge memory.");
  990. return false;
  991. }
  992. total_size_old = memory->end_addr - (uint8*)memory;
  993. bh_memcpy_s((uint8*)new_memory, (uint32)total_size,
  994. (uint8*)memory, total_size_old);
  995. memset((uint8*)new_memory + total_size_old,
  996. 0, (uint32)total_size - total_size_old);
  997. wasm_runtime_free(memory);
  998. }
  999. new_memory->cur_page_count = total_page_count;
  1000. new_memory->memory_data = new_memory->base_addr;
  1001. new_memory->global_data = new_memory->memory_data +
  1002. new_memory->num_bytes_per_page * total_page_count;
  1003. new_memory->end_addr = new_memory->global_data + new_memory->global_data_size;
  1004. global_data_old = new_memory->memory_data +
  1005. new_memory->num_bytes_per_page * old_page_count;
  1006. /* Copy global data */
  1007. bh_memcpy_s(new_memory->global_data, new_memory->global_data_size,
  1008. global_data_old, new_memory->global_data_size);
  1009. memset(global_data_old, 0, new_memory->global_data_size);
  1010. module->memories[0] = module->default_memory = new_memory;
  1011. return true;
  1012. #else /* else of WASM_ENABLE_MEMORY_GROW */
  1013. wasm_set_exception(module, "unsupported operation: enlarge memory.");
  1014. return false;
  1015. #endif /* end of WASM_ENABLE_MEMORY_GROW */
  1016. }