wasm_runtime.c 61 KB

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