wasm_runtime.c 41 KB

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