wasm_runtime.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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. #if WASM_ENABLE_THREAD_MGR != 0
  16. #include "../libraries/thread-mgr/thread_manager.h"
  17. #endif
  18. static void
  19. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  20. {
  21. if (error_buf != NULL) {
  22. snprintf(error_buf, error_buf_size,
  23. "WASM module instantiate failed: %s", string);
  24. }
  25. }
  26. static void
  27. set_error_buf_v(char *error_buf, uint32 error_buf_size,
  28. const char *format, ...)
  29. {
  30. va_list args;
  31. char buf[128];
  32. if (error_buf != NULL) {
  33. va_start(args, format);
  34. vsnprintf(buf, sizeof(buf), format, args);
  35. va_end(args);
  36. snprintf(error_buf, error_buf_size,
  37. "WASM module instantiate failed: %s", buf);
  38. }
  39. }
  40. WASMModule*
  41. wasm_load(const uint8 *buf, uint32 size,
  42. char *error_buf, uint32 error_buf_size)
  43. {
  44. return wasm_loader_load(buf, size, error_buf, error_buf_size);
  45. }
  46. WASMModule*
  47. wasm_load_from_sections(WASMSection *section_list,
  48. char *error_buf, uint32_t error_buf_size)
  49. {
  50. return wasm_loader_load_from_sections(section_list,
  51. error_buf, error_buf_size);
  52. }
  53. void
  54. wasm_unload(WASMModule *module)
  55. {
  56. wasm_loader_unload(module);
  57. }
  58. static void *
  59. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  60. {
  61. void *mem;
  62. if (size >= UINT32_MAX
  63. || !(mem = wasm_runtime_malloc((uint32)size))) {
  64. set_error_buf(error_buf, error_buf_size,
  65. "allocate memory failed");
  66. return NULL;
  67. }
  68. memset(mem, 0, (uint32)size);
  69. return mem;
  70. }
  71. #if WASM_ENABLE_MULTI_MODULE != 0
  72. static WASMModuleInstance *
  73. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  74. const WASMModule *sub_module)
  75. {
  76. bh_list *sub_module_inst_list = parent_module_inst->sub_module_inst_list;
  77. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  78. while (node && sub_module != node->module_inst->module) {
  79. node = bh_list_elem_next(node);
  80. }
  81. return node ? node->module_inst : NULL;
  82. }
  83. #endif
  84. /**
  85. * Destroy memory instances.
  86. */
  87. static void
  88. memories_deinstantiate(WASMModuleInstance *module_inst,
  89. WASMMemoryInstance **memories,
  90. uint32 count)
  91. {
  92. uint32 i;
  93. if (memories) {
  94. for (i = 0; i < count; i++) {
  95. if (memories[i]) {
  96. #if WASM_ENABLE_MULTI_MODULE != 0
  97. if (memories[i]->owner != module_inst)
  98. continue;
  99. #endif
  100. #if WASM_ENABLE_SHARED_MEMORY != 0
  101. if (memories[i]->is_shared) {
  102. int32 ref_count =
  103. shared_memory_dec_reference(
  104. (WASMModuleCommon *)module_inst->module);
  105. bh_assert(ref_count >= 0);
  106. /* if the reference count is not zero,
  107. don't free the memory */
  108. if (ref_count > 0)
  109. continue;
  110. }
  111. os_mutex_destroy(&memories[i]->mem_lock);
  112. #endif
  113. if (memories[i]->heap_handle) {
  114. mem_allocator_destroy(memories[i]->heap_handle);
  115. wasm_runtime_free(memories[i]->heap_handle);
  116. memories[i]->heap_handle = NULL;
  117. }
  118. if (memories[i]->memory_data)
  119. wasm_runtime_free(memories[i]->memory_data);
  120. wasm_runtime_free(memories[i]);
  121. }
  122. }
  123. wasm_runtime_free(memories);
  124. }
  125. (void)module_inst;
  126. }
  127. static WASMMemoryInstance*
  128. memory_instantiate(WASMModuleInstance *module_inst,
  129. uint32 num_bytes_per_page,
  130. uint32 init_page_count, uint32 max_page_count,
  131. uint32 heap_size, uint32 flags,
  132. char *error_buf, uint32 error_buf_size)
  133. {
  134. WASMModule *module = module_inst->module;
  135. WASMMemoryInstance *memory;
  136. uint64 memory_data_size;
  137. uint32 heap_offset = num_bytes_per_page * init_page_count;
  138. uint32 inc_page_count, aux_heap_base, global_idx;
  139. uint32 bytes_of_last_page, bytes_to_page_end;
  140. uint8 *global_addr;
  141. #if WASM_ENABLE_SHARED_MEMORY != 0
  142. bool is_shared_memory = flags & 0x02 ? true : false;
  143. /* shared memory */
  144. if (is_shared_memory) {
  145. WASMSharedMemNode *node =
  146. wasm_module_get_shared_memory(
  147. (WASMModuleCommon *)module_inst->module);
  148. /* If the memory of this module has been instantiated,
  149. return the memory instance directly */
  150. if (node) {
  151. uint32 ref_count;
  152. ref_count = shared_memory_inc_reference(
  153. (WASMModuleCommon *)module_inst->module);
  154. bh_assert(ref_count > 0);
  155. memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node);
  156. bh_assert(memory);
  157. (void)ref_count;
  158. return memory;
  159. }
  160. }
  161. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  162. if (heap_size > 0
  163. && module_inst->module->malloc_function != (uint32)-1
  164. && module_inst->module->free_function != (uint32)-1) {
  165. /* Disable app heap, use malloc/free function exported
  166. by wasm app to allocate/free memory instead */
  167. heap_size = 0;
  168. }
  169. if (init_page_count == max_page_count && init_page_count == 1) {
  170. /* If only one page and at most one page, we just append
  171. the app heap to the end of linear memory, enlarge the
  172. num_bytes_per_page, and don't change the page count*/
  173. heap_offset = num_bytes_per_page;
  174. num_bytes_per_page += heap_size;
  175. if (num_bytes_per_page < heap_size) {
  176. set_error_buf(error_buf, error_buf_size,
  177. "memory size must be at most 65536 pages (4GiB)");
  178. return NULL;
  179. }
  180. }
  181. else if (heap_size > 0) {
  182. if (module->aux_heap_base_global_index != (uint32)-1
  183. && module->aux_heap_base < num_bytes_per_page
  184. * init_page_count) {
  185. /* Insert app heap before __heap_base */
  186. aux_heap_base = module->aux_heap_base;
  187. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  188. if (bytes_of_last_page == 0)
  189. bytes_of_last_page = num_bytes_per_page;
  190. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  191. inc_page_count = (heap_size - bytes_to_page_end
  192. + num_bytes_per_page - 1) / num_bytes_per_page;
  193. heap_offset = aux_heap_base;
  194. aux_heap_base += heap_size;
  195. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  196. if (bytes_of_last_page == 0)
  197. bytes_of_last_page = num_bytes_per_page;
  198. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  199. if (bytes_to_page_end < 1 * BH_KB) {
  200. aux_heap_base += 1 * BH_KB;
  201. inc_page_count++;
  202. }
  203. /* Adjust __heap_base global value */
  204. global_idx = module->aux_heap_base_global_index;
  205. global_addr = module_inst->global_data +
  206. module_inst->globals[global_idx].data_offset;
  207. *(uint32 *)global_addr = aux_heap_base;
  208. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  209. }
  210. else {
  211. /* Insert app heap before new page */
  212. inc_page_count = (heap_size + num_bytes_per_page - 1)
  213. / num_bytes_per_page;
  214. heap_offset = num_bytes_per_page * init_page_count;
  215. heap_size = num_bytes_per_page * inc_page_count;
  216. if (heap_size > 0)
  217. heap_size -= 1 * BH_KB;
  218. }
  219. init_page_count += inc_page_count;
  220. max_page_count += inc_page_count;
  221. if (init_page_count > 65536) {
  222. set_error_buf(error_buf, error_buf_size,
  223. "memory size must be at most 65536 pages (4GiB)");
  224. return NULL;
  225. }
  226. if (max_page_count > 65536)
  227. max_page_count = 65536;
  228. }
  229. LOG_VERBOSE("Memory instantiate:");
  230. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  231. num_bytes_per_page, init_page_count, max_page_count);
  232. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  233. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  234. #if WASM_ENABLE_SHARED_MEMORY != 0
  235. if (is_shared_memory) {
  236. /* Allocate max page for shared memory */
  237. memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  238. }
  239. #endif
  240. /* Allocate memory space, addr data and global data */
  241. if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance),
  242. error_buf, error_buf_size))) {
  243. return NULL;
  244. }
  245. if (memory_data_size > 0
  246. && !(memory->memory_data =
  247. runtime_malloc(memory_data_size,
  248. error_buf, error_buf_size))) {
  249. goto fail1;
  250. }
  251. memory->module_type = Wasm_Module_Bytecode;
  252. memory->num_bytes_per_page = num_bytes_per_page;
  253. memory->cur_page_count = init_page_count;
  254. memory->max_page_count = max_page_count;
  255. memory->heap_data = memory->memory_data + heap_offset;
  256. memory->heap_data_end = memory->heap_data + heap_size;
  257. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  258. /* Initialize heap */
  259. if (heap_size > 0) {
  260. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  261. if (!(memory->heap_handle = runtime_malloc((uint64)heap_struct_size,
  262. error_buf, error_buf_size))) {
  263. goto fail2;
  264. }
  265. if (!mem_allocator_create_with_struct_and_pool
  266. (memory->heap_handle, heap_struct_size,
  267. memory->heap_data, heap_size)) {
  268. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  269. goto fail3;
  270. }
  271. }
  272. #if WASM_ENABLE_SHARED_MEMORY != 0
  273. if (0 != os_mutex_init(&memory->mem_lock)) {
  274. set_error_buf(error_buf, error_buf_size, "init mutex failed");
  275. goto fail4;
  276. }
  277. if (is_shared_memory) {
  278. memory->is_shared = true;
  279. if (!shared_memory_set_memory_inst(
  280. (WASMModuleCommon *)module_inst->module,
  281. (WASMMemoryInstanceCommon *)memory)) {
  282. set_error_buf(error_buf, error_buf_size,
  283. "allocate memory failed");
  284. goto fail5;
  285. }
  286. }
  287. #endif
  288. LOG_VERBOSE("Memory instantiate success.");
  289. return memory;
  290. #if WASM_ENABLE_SHARED_MEMORY != 0
  291. fail5:
  292. os_mutex_destroy(&memory->mem_lock);
  293. fail4:
  294. if (heap_size > 0)
  295. mem_allocator_destroy(memory->heap_handle);
  296. #endif
  297. fail3:
  298. if (heap_size > 0)
  299. wasm_runtime_free(memory->heap_handle);
  300. fail2:
  301. if (memory->memory_data)
  302. wasm_runtime_free(memory->memory_data);
  303. fail1:
  304. wasm_runtime_free(memory);
  305. return NULL;
  306. }
  307. /**
  308. * Instantiate memories in a module.
  309. */
  310. static WASMMemoryInstance **
  311. memories_instantiate(const WASMModule *module,
  312. WASMModuleInstance *module_inst,
  313. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  314. {
  315. WASMImport *import;
  316. uint32 mem_index = 0, i, memory_count =
  317. module->import_memory_count + module->memory_count;
  318. uint64 total_size;
  319. WASMMemoryInstance **memories, *memory;
  320. total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
  321. if (!(memories = runtime_malloc(total_size,
  322. error_buf, error_buf_size))) {
  323. return NULL;
  324. }
  325. /* instantiate memories from import section */
  326. import = module->import_memories;
  327. for (i = 0; i < module->import_memory_count; i++, import++) {
  328. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  329. uint32 init_page_count = import->u.memory.init_page_count;
  330. uint32 max_page_count = import->u.memory.max_page_count;
  331. uint32 flags = import->u.memory.flags;
  332. uint32 actual_heap_size = heap_size;
  333. #if WASM_ENABLE_MULTI_MODULE != 0
  334. if (import->u.memory.import_module != NULL) {
  335. WASMModuleInstance *module_inst_linked;
  336. if (!(module_inst_linked = get_sub_module_inst(
  337. module_inst, import->u.memory.import_module))) {
  338. set_error_buf(error_buf, error_buf_size, "unknown memory");
  339. memories_deinstantiate(module_inst, memories, memory_count);
  340. return NULL;
  341. }
  342. if (!(memory = memories[mem_index++] = wasm_lookup_memory(
  343. module_inst_linked, import->u.memory.field_name))) {
  344. set_error_buf(error_buf, error_buf_size, "unknown memory");
  345. memories_deinstantiate(module_inst, memories, memory_count);
  346. return NULL;
  347. }
  348. }
  349. else
  350. #endif
  351. {
  352. if (!(memory = memories[mem_index++] = memory_instantiate(
  353. module_inst, num_bytes_per_page, init_page_count,
  354. max_page_count, actual_heap_size, flags,
  355. error_buf, error_buf_size))) {
  356. memories_deinstantiate(module_inst, memories, memory_count);
  357. return NULL;
  358. }
  359. #if WASM_ENABLE_MULTI_MODULE != 0
  360. /* The module of the import memory is a builtin module, and
  361. the memory is created by current module, set its owner
  362. to current module, so the memory can be destroyed in
  363. memories_deinstantiate. */
  364. memory->owner = module_inst;
  365. #endif
  366. }
  367. }
  368. /* instantiate memories from memory section */
  369. for (i = 0; i < module->memory_count; i++) {
  370. if (!(memory = memories[mem_index++] =
  371. memory_instantiate(module_inst,
  372. module->memories[i].num_bytes_per_page,
  373. module->memories[i].init_page_count,
  374. module->memories[i].max_page_count,
  375. heap_size, module->memories[i].flags,
  376. error_buf, error_buf_size))) {
  377. memories_deinstantiate(module_inst, memories, memory_count);
  378. return NULL;
  379. }
  380. #if WASM_ENABLE_MULTI_MODULE != 0
  381. memory->owner = module_inst;
  382. #endif
  383. }
  384. if (mem_index == 0) {
  385. /**
  386. * no import memory and define memory, but still need heap
  387. * for wasm code
  388. */
  389. if (!(memory = memories[mem_index++] =
  390. memory_instantiate(module_inst, 0, 0, 0, heap_size, 0,
  391. error_buf, error_buf_size))) {
  392. memories_deinstantiate(module_inst, memories, memory_count);
  393. return NULL;
  394. }
  395. }
  396. bh_assert(mem_index == memory_count);
  397. (void)module_inst;
  398. return memories;
  399. }
  400. /**
  401. * Destroy table instances.
  402. */
  403. static void
  404. tables_deinstantiate(WASMTableInstance **tables, uint32 count)
  405. {
  406. uint32 i;
  407. if (tables) {
  408. for (i = 0; i < count; i++)
  409. if (tables[i])
  410. wasm_runtime_free(tables[i]);
  411. wasm_runtime_free(tables);
  412. }
  413. }
  414. /**
  415. * Instantiate tables in a module.
  416. */
  417. static WASMTableInstance **
  418. tables_instantiate(const WASMModule *module,
  419. WASMModuleInstance *module_inst,
  420. char *error_buf, uint32 error_buf_size)
  421. {
  422. WASMImport *import;
  423. uint32 table_index = 0, i, table_count =
  424. module->import_table_count + module->table_count;
  425. uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  426. WASMTableInstance **tables, *table;
  427. if (!(tables = runtime_malloc(total_size,
  428. error_buf, error_buf_size))) {
  429. return NULL;
  430. }
  431. /* instantiate tables from import section */
  432. import = module->import_tables;
  433. for (i = 0; i < module->import_table_count; i++, import++) {
  434. #if WASM_ENABLE_MULTI_MODULE != 0
  435. WASMTableInstance *table_inst_linked = NULL;
  436. WASMModuleInstance *module_inst_linked = NULL;
  437. if (import->u.table.import_module) {
  438. if (!(module_inst_linked =
  439. get_sub_module_inst(module_inst, import->u.table.import_module))) {
  440. set_error_buf(error_buf, error_buf_size, "unknown table");
  441. tables_deinstantiate(tables, table_count);
  442. return NULL;
  443. }
  444. if (!(table_inst_linked = wasm_lookup_table(module_inst_linked,
  445. import->u.table.field_name))) {
  446. set_error_buf(error_buf, error_buf_size, "unknown table");
  447. tables_deinstantiate(tables, table_count);
  448. return NULL;
  449. }
  450. total_size = offsetof(WASMTableInstance, base_addr);
  451. }
  452. else
  453. #endif
  454. {
  455. /* it is a built-in table */
  456. total_size = offsetof(WASMTableInstance, base_addr)
  457. + sizeof(uint32) * (uint64)import->u.table.init_size;
  458. }
  459. if (!(table = tables[table_index++] = runtime_malloc
  460. (total_size, error_buf, error_buf_size))) {
  461. tables_deinstantiate(tables, table_count);
  462. return NULL;
  463. }
  464. /* Set all elements to -1 to mark them as uninitialized elements */
  465. memset(table, -1, (uint32)total_size);
  466. #if WASM_ENABLE_MULTI_MODULE != 0
  467. table->table_inst_linked = table_inst_linked;
  468. if (table_inst_linked != NULL) {
  469. table->elem_type = table_inst_linked->elem_type;
  470. table->cur_size = table_inst_linked->cur_size;
  471. table->max_size = table_inst_linked->max_size;
  472. }
  473. else
  474. #endif
  475. {
  476. table->elem_type = import->u.table.elem_type;
  477. table->cur_size = import->u.table.init_size;
  478. table->max_size = import->u.table.max_size;
  479. }
  480. }
  481. /* instantiate tables from table section */
  482. for (i = 0; i < module->table_count; i++) {
  483. total_size = offsetof(WASMTableInstance, base_addr) +
  484. sizeof(uint32) * (uint64)module->tables[i].init_size;
  485. if (!(table = tables[table_index++] = runtime_malloc
  486. (total_size, error_buf, error_buf_size))) {
  487. tables_deinstantiate(tables, table_count);
  488. return NULL;
  489. }
  490. /* Set all elements to -1 to mark them as uninitialized elements */
  491. memset(table, -1, (uint32)total_size);
  492. table->elem_type = module->tables[i].elem_type;
  493. table->cur_size = module->tables[i].init_size;
  494. table->max_size = module->tables[i].max_size;
  495. #if WASM_ENABLE_MULTI_MODULE != 0
  496. table->table_inst_linked = NULL;
  497. #endif
  498. }
  499. bh_assert(table_index == table_count);
  500. (void)module_inst;
  501. return tables;
  502. }
  503. /**
  504. * Destroy function instances.
  505. */
  506. static void
  507. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  508. {
  509. if (functions) {
  510. wasm_runtime_free(functions);
  511. }
  512. }
  513. /**
  514. * Instantiate functions in a module.
  515. */
  516. static WASMFunctionInstance *
  517. functions_instantiate(const WASMModule *module,
  518. WASMModuleInstance *module_inst,
  519. char *error_buf, uint32 error_buf_size)
  520. {
  521. WASMImport *import;
  522. uint32 i, function_count =
  523. module->import_function_count + module->function_count;
  524. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  525. WASMFunctionInstance *functions, *function;
  526. if (!(functions = runtime_malloc(total_size,
  527. error_buf, error_buf_size))) {
  528. return NULL;
  529. }
  530. /* instantiate functions from import section */
  531. function = functions;
  532. import = module->import_functions;
  533. for (i = 0; i < module->import_function_count; i++, import++) {
  534. function->is_import_func = true;
  535. #if WASM_ENABLE_MULTI_MODULE != 0
  536. if (import->u.function.import_module) {
  537. function->import_module_inst =
  538. get_sub_module_inst(module_inst,
  539. import->u.function.import_module);
  540. if (function->import_module_inst) {
  541. function->import_func_inst =
  542. wasm_lookup_function(function->import_module_inst,
  543. import->u.function.field_name, NULL);
  544. }
  545. }
  546. #endif /* WASM_ENABLE_MULTI_MODULE */
  547. function->u.func_import = &import->u.function;
  548. function->param_cell_num =
  549. import->u.function.func_type->param_cell_num;
  550. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  551. function->param_count =
  552. (uint16)function->u.func_import->func_type->param_count;
  553. function->param_types = function->u.func_import->func_type->types;
  554. function->local_cell_num = 0;
  555. function->local_count = 0;
  556. function->local_types = NULL;
  557. function++;
  558. }
  559. /* instantiate functions from function section */
  560. for (i = 0; i < module->function_count; i++) {
  561. function->is_import_func = false;
  562. function->u.func = module->functions[i];
  563. function->param_cell_num = function->u.func->param_cell_num;
  564. function->ret_cell_num = function->u.func->ret_cell_num;
  565. function->local_cell_num = function->u.func->local_cell_num;
  566. function->param_count = (uint16)function->u.func->func_type->param_count;
  567. function->local_count = (uint16)function->u.func->local_count;
  568. function->param_types = function->u.func->func_type->types;
  569. function->local_types = function->u.func->local_types;
  570. function->local_offsets = function->u.func->local_offsets;
  571. #if WASM_ENABLE_FAST_INTERP != 0
  572. function->const_cell_num = function->u.func->const_cell_num;
  573. #endif
  574. function++;
  575. }
  576. bh_assert((uint32)(function - functions) == function_count);
  577. (void)module_inst;
  578. return functions;
  579. }
  580. /**
  581. * Destroy global instances.
  582. */
  583. static void
  584. globals_deinstantiate(WASMGlobalInstance *globals)
  585. {
  586. if (globals)
  587. wasm_runtime_free(globals);
  588. }
  589. static bool
  590. check_global_init_expr(const WASMModule *module, uint32 global_index,
  591. char *error_buf, uint32 error_buf_size)
  592. {
  593. if (global_index >= module->import_global_count + module->global_count) {
  594. set_error_buf_v(error_buf, error_buf_size,
  595. "unknown global %d", global_index);
  596. return false;
  597. }
  598. /**
  599. * Currently, constant expressions occurring as initializers of
  600. * globals are further constrained in that contained global.get
  601. * instructions are only allowed to refer to imported globals.
  602. *
  603. * And initializer expression cannot reference a mutable global.
  604. */
  605. if (global_index >= module->import_global_count
  606. || (module->import_globals + global_index)->u.global.is_mutable) {
  607. set_error_buf(error_buf, error_buf_size,
  608. "constant expression required");
  609. return false;
  610. }
  611. return true;
  612. }
  613. /**
  614. * Instantiate globals in a module.
  615. */
  616. static WASMGlobalInstance *
  617. globals_instantiate(const WASMModule *module,
  618. WASMModuleInstance *module_inst,
  619. uint32 *p_global_data_size, char *error_buf,
  620. uint32 error_buf_size)
  621. {
  622. WASMImport *import;
  623. uint32 global_data_offset = 0;
  624. uint32 i, global_count =
  625. module->import_global_count + module->global_count;
  626. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  627. WASMGlobalInstance *globals, *global;
  628. if (!(globals = runtime_malloc(total_size,
  629. error_buf, error_buf_size))) {
  630. return NULL;
  631. }
  632. /* instantiate globals from import section */
  633. global = globals;
  634. import = module->import_globals;
  635. for (i = 0; i < module->import_global_count; i++, import++) {
  636. WASMGlobalImport *global_import = &import->u.global;
  637. global->type = global_import->type;
  638. global->is_mutable = global_import->is_mutable;
  639. #if WASM_ENABLE_MULTI_MODULE != 0
  640. if (global_import->import_module) {
  641. if (!(global->import_module_inst = get_sub_module_inst(
  642. module_inst, global_import->import_module))) {
  643. set_error_buf(error_buf, error_buf_size, "unknown global");
  644. return NULL;
  645. }
  646. if (!(global->import_global_inst = wasm_lookup_global(
  647. global->import_module_inst, global_import->field_name))) {
  648. set_error_buf(error_buf, error_buf_size, "unknown global");
  649. return NULL;
  650. }
  651. /* The linked global instance has been initialized, we
  652. just need to copy the value. */
  653. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  654. &(global_import->import_global_linked->init_expr),
  655. sizeof(WASMValue));
  656. }
  657. else
  658. #endif
  659. {
  660. /* native globals share their initial_values in one module */
  661. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  662. &(global_import->global_data_linked),
  663. sizeof(WASMValue));
  664. }
  665. global->data_offset = global_data_offset;
  666. global_data_offset += wasm_value_type_size(global->type);
  667. global++;
  668. }
  669. /* instantiate globals from global section */
  670. for (i = 0; i < module->global_count; i++) {
  671. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  672. global->type = module->globals[i].type;
  673. global->is_mutable = module->globals[i].is_mutable;
  674. global->data_offset = global_data_offset;
  675. global_data_offset += wasm_value_type_size(global->type);
  676. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  677. if (!check_global_init_expr(module, init_expr->u.global_index,
  678. error_buf, error_buf_size)) {
  679. return NULL;
  680. }
  681. bh_memcpy_s(
  682. &(global->initial_value), sizeof(WASMValue),
  683. &(globals[init_expr->u.global_index].initial_value),
  684. sizeof(globals[init_expr->u.global_index].initial_value));
  685. }
  686. else {
  687. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  688. &(init_expr->u), sizeof(init_expr->u));
  689. }
  690. global++;
  691. }
  692. bh_assert((uint32)(global - globals) == global_count);
  693. *p_global_data_size = global_data_offset;
  694. (void)module_inst;
  695. return globals;
  696. }
  697. /**
  698. * Return export function count in module export section.
  699. */
  700. static uint32
  701. get_export_count(const WASMModule *module, uint8 kind)
  702. {
  703. WASMExport *export = module->exports;
  704. uint32 count = 0, i;
  705. for (i = 0; i < module->export_count; i++, export++)
  706. if (export->kind == kind)
  707. count++;
  708. return count;
  709. }
  710. /**
  711. * Destroy export function instances.
  712. */
  713. static void
  714. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  715. {
  716. if (functions)
  717. wasm_runtime_free(functions);
  718. }
  719. /**
  720. * Instantiate export functions in a module.
  721. */
  722. static WASMExportFuncInstance*
  723. export_functions_instantiate(const WASMModule *module,
  724. WASMModuleInstance *module_inst,
  725. uint32 export_func_count,
  726. char *error_buf, uint32 error_buf_size)
  727. {
  728. WASMExportFuncInstance *export_funcs, *export_func;
  729. WASMExport *export = module->exports;
  730. uint32 i;
  731. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  732. if (!(export_func = export_funcs = runtime_malloc
  733. (total_size, error_buf, error_buf_size))) {
  734. return NULL;
  735. }
  736. for (i = 0; i < module->export_count; i++, export++)
  737. if (export->kind == EXPORT_KIND_FUNC) {
  738. export_func->name = export->name;
  739. export_func->function = &module_inst->functions[export->index];
  740. export_func++;
  741. }
  742. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  743. return export_funcs;
  744. }
  745. #if WASM_ENABLE_MULTI_MODULE != 0
  746. static void
  747. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  748. {
  749. if (globals)
  750. wasm_runtime_free(globals);
  751. }
  752. static WASMExportGlobInstance *
  753. export_globals_instantiate(const WASMModule *module,
  754. WASMModuleInstance *module_inst,
  755. uint32 export_glob_count, char *error_buf,
  756. uint32 error_buf_size)
  757. {
  758. WASMExportGlobInstance *export_globals, *export_global;
  759. WASMExport *export = module->exports;
  760. uint32 i;
  761. uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  762. if (!(export_global = export_globals = runtime_malloc
  763. (total_size, error_buf, error_buf_size))) {
  764. return NULL;
  765. }
  766. for (i = 0; i < module->export_count; i++, export++)
  767. if (export->kind == EXPORT_KIND_GLOBAL) {
  768. export_global->name = export->name;
  769. export_global->global = &module_inst->globals[export->index];
  770. export_global++;
  771. }
  772. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  773. return export_globals;
  774. }
  775. #endif
  776. static bool
  777. execute_post_inst_function(WASMModuleInstance *module_inst)
  778. {
  779. WASMFunctionInstance *post_inst_func = NULL;
  780. WASMType *post_inst_func_type;
  781. uint32 i;
  782. for (i = 0; i < module_inst->export_func_count; i++)
  783. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  784. post_inst_func = module_inst->export_functions[i].function;
  785. break;
  786. }
  787. if (!post_inst_func)
  788. /* Not found */
  789. return true;
  790. post_inst_func_type = post_inst_func->u.func->func_type;
  791. if (post_inst_func_type->param_count != 0
  792. || post_inst_func_type->result_count != 0)
  793. /* Not a valid function type, ignore it */
  794. return true;
  795. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  796. 0, NULL);
  797. }
  798. #if WASM_ENABLE_BULK_MEMORY != 0
  799. static bool
  800. execute_memory_init_function(WASMModuleInstance *module_inst)
  801. {
  802. WASMFunctionInstance *memory_init_func = NULL;
  803. WASMType *memory_init_func_type;
  804. uint32 i;
  805. for (i = 0; i < module_inst->export_func_count; i++)
  806. if (!strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) {
  807. memory_init_func = module_inst->export_functions[i].function;
  808. break;
  809. }
  810. if (!memory_init_func)
  811. /* Not found */
  812. return true;
  813. memory_init_func_type = memory_init_func->u.func->func_type;
  814. if (memory_init_func_type->param_count != 0
  815. || memory_init_func_type->result_count != 0)
  816. /* Not a valid function type, ignore it */
  817. return true;
  818. return wasm_create_exec_env_and_call_function(module_inst,
  819. memory_init_func,
  820. 0, NULL);
  821. }
  822. #endif
  823. static bool
  824. execute_start_function(WASMModuleInstance *module_inst)
  825. {
  826. WASMFunctionInstance *func = module_inst->start_function;
  827. if (!func)
  828. return true;
  829. bh_assert(!func->is_import_func && func->param_cell_num == 0
  830. && func->ret_cell_num == 0);
  831. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  832. }
  833. static bool
  834. execute_malloc_function(WASMModuleInstance *module_inst,
  835. WASMFunctionInstance *malloc_func,
  836. WASMFunctionInstance *retain_func,
  837. uint32 size, uint32 *p_result)
  838. {
  839. uint32 argv[2], argc;
  840. bool ret;
  841. argv[0] = size;
  842. argc = 1;
  843. /* if __retain is exported, then this module is compiled by
  844. assemblyscript, the memory should be managed by as's runtime,
  845. in this case we need to call the retain function after malloc
  846. the memory */
  847. if (retain_func) {
  848. /* the malloc functino from assemblyscript is:
  849. function __new(size: usize, id: u32)
  850. id = 0 means this is an ArrayBuffer object */
  851. argv[1] = 0;
  852. argc = 2;
  853. }
  854. ret = wasm_create_exec_env_and_call_function
  855. (module_inst, malloc_func, argc, argv);
  856. if (retain_func && ret) {
  857. ret = wasm_create_exec_env_and_call_function
  858. (module_inst, retain_func, 1, argv);
  859. }
  860. if (ret)
  861. *p_result = argv[0];
  862. return ret;
  863. }
  864. static bool
  865. execute_free_function(WASMModuleInstance *module_inst,
  866. WASMFunctionInstance *free_func,
  867. uint32 offset)
  868. {
  869. uint32 argv[2];
  870. argv[0] = offset;
  871. return wasm_create_exec_env_and_call_function
  872. (module_inst, free_func, 1, argv);
  873. }
  874. #if WASM_ENABLE_MULTI_MODULE != 0
  875. static bool
  876. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  877. uint32 stack_size, uint32 heap_size, char *error_buf,
  878. uint32 error_buf_size)
  879. {
  880. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  881. WASMRegisteredModule *sub_module_list_node =
  882. bh_list_first_elem(module->import_module_list);
  883. while (sub_module_list_node) {
  884. WASMSubModInstNode *sub_module_inst_list_node;
  885. WASMModule *sub_module = (WASMModule*)sub_module_list_node->module;
  886. WASMModuleInstance *sub_module_inst =
  887. wasm_instantiate(sub_module, false, stack_size, heap_size,
  888. error_buf, error_buf_size);
  889. if (!sub_module_inst) {
  890. LOG_DEBUG("instantiate %s failed",
  891. sub_module_list_node->module_name);
  892. return false;
  893. }
  894. sub_module_inst_list_node = runtime_malloc
  895. (sizeof(WASMSubModInstNode), error_buf, error_buf_size);
  896. if (!sub_module_inst_list_node) {
  897. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  898. sizeof(WASMSubModInstNode));
  899. wasm_deinstantiate(sub_module_inst, false);
  900. return false;
  901. }
  902. sub_module_inst_list_node->module_inst = sub_module_inst;
  903. sub_module_inst_list_node->module_name =
  904. sub_module_list_node->module_name;
  905. bh_list_status ret =
  906. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  907. bh_assert(BH_LIST_SUCCESS == ret);
  908. (void)ret;
  909. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  910. }
  911. return true;
  912. }
  913. static void
  914. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  915. {
  916. bh_list *list = module_inst->sub_module_inst_list;
  917. WASMSubModInstNode *node = bh_list_first_elem(list);
  918. while (node) {
  919. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  920. bh_list_remove(list, node);
  921. wasm_deinstantiate(node->module_inst, false);
  922. wasm_runtime_free(node);
  923. node = next_node;
  924. }
  925. }
  926. #endif
  927. static bool
  928. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  929. uint32 error_buf_size)
  930. {
  931. WASMModule *module = module_inst->module;
  932. uint32 i;
  933. for (i = 0; i < module->import_function_count; i++) {
  934. WASMFunctionImport *func =
  935. &((module->import_functions + i)->u.function);
  936. if (!func->func_ptr_linked
  937. #if WASM_ENABLE_MULTI_MODULE != 0
  938. && !func->import_func_linked
  939. #endif
  940. ) {
  941. #if WASM_ENABLE_SPEC_TEST != 0
  942. set_error_buf(error_buf, error_buf_size,
  943. "unknown import or incompatible import type");
  944. return false;
  945. #else
  946. #if WASM_ENABLE_WAMR_COMPILER == 0
  947. LOG_WARNING("warning: failed to link import function (%s, %s)",
  948. func->module_name, func->field_name);
  949. #else
  950. /* do nothing to avoid confused message */
  951. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  952. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  953. }
  954. }
  955. for (i = 0; i < module->import_global_count; i++) {
  956. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  957. if (!global->is_linked) {
  958. #if WASM_ENABLE_SPEC_TEST != 0
  959. set_error_buf(error_buf, error_buf_size,
  960. "unknown import or incompatible import type");
  961. return false;
  962. #else
  963. #if WASM_ENABLE_WAMR_COMPILER == 0
  964. LOG_DEBUG("warning: failed to link import global (%s, %s)",
  965. global->module_name, global->field_name);
  966. #else
  967. /* do nothing to avoid confused message */
  968. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  969. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  970. }
  971. }
  972. return true;
  973. }
  974. /**
  975. * Instantiate module
  976. */
  977. WASMModuleInstance*
  978. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  979. uint32 stack_size, uint32 heap_size,
  980. char *error_buf, uint32 error_buf_size)
  981. {
  982. WASMModuleInstance *module_inst;
  983. WASMGlobalInstance *globals = NULL, *global;
  984. uint32 global_count, global_data_size = 0, i;
  985. uint32 base_offset, length;
  986. uint8 *global_data, *global_data_end;
  987. #if WASM_ENABLE_MULTI_MODULE != 0
  988. bool ret = false;
  989. #endif
  990. if (!module)
  991. return NULL;
  992. /* Check heap size */
  993. heap_size = align_uint(heap_size, 8);
  994. if (heap_size > APP_HEAP_SIZE_MAX)
  995. heap_size = APP_HEAP_SIZE_MAX;
  996. /* Allocate the memory */
  997. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  998. error_buf, error_buf_size))) {
  999. return NULL;
  1000. }
  1001. module_inst->module = module;
  1002. #if WASM_ENABLE_MULTI_MODULE != 0
  1003. module_inst->sub_module_inst_list =
  1004. &module_inst->sub_module_inst_list_head;
  1005. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1006. error_buf, error_buf_size);
  1007. if (!ret) {
  1008. LOG_DEBUG("build a sub module list failed");
  1009. goto fail;
  1010. }
  1011. #endif
  1012. /* Instantiate global firstly to get the mutable data size */
  1013. global_count = module->import_global_count + module->global_count;
  1014. if (global_count
  1015. && !(globals = globals_instantiate(module, module_inst,
  1016. &global_data_size,
  1017. error_buf, error_buf_size))) {
  1018. goto fail;
  1019. }
  1020. module_inst->global_count = global_count;
  1021. module_inst->globals = globals;
  1022. module_inst->memory_count =
  1023. module->import_memory_count + module->memory_count;
  1024. module_inst->table_count =
  1025. module->import_table_count + module->table_count;
  1026. module_inst->function_count =
  1027. module->import_function_count + module->function_count;
  1028. /* export */
  1029. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1030. #if WASM_ENABLE_MULTI_MODULE != 0
  1031. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1032. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1033. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1034. #endif
  1035. if (global_count > 0) {
  1036. if (!(module_inst->global_data = runtime_malloc
  1037. (global_data_size, error_buf, error_buf_size))) {
  1038. goto fail;
  1039. }
  1040. }
  1041. /* Instantiate memories/tables/functions */
  1042. if ((module_inst->memory_count > 0
  1043. && !(module_inst->memories =
  1044. memories_instantiate(module,
  1045. module_inst,
  1046. heap_size, error_buf, error_buf_size)))
  1047. || (module_inst->table_count > 0
  1048. && !(module_inst->tables =
  1049. tables_instantiate(module,
  1050. module_inst,
  1051. error_buf, error_buf_size)))
  1052. || (module_inst->function_count > 0
  1053. && !(module_inst->functions =
  1054. functions_instantiate(module,
  1055. module_inst,
  1056. error_buf, error_buf_size)))
  1057. || (module_inst->export_func_count > 0
  1058. && !(module_inst->export_functions = export_functions_instantiate(
  1059. module, module_inst, module_inst->export_func_count,
  1060. error_buf, error_buf_size)))
  1061. #if WASM_ENABLE_MULTI_MODULE != 0
  1062. || (module_inst->export_glob_count > 0
  1063. && !(module_inst->export_globals = export_globals_instantiate(
  1064. module, module_inst, module_inst->export_glob_count,
  1065. error_buf, error_buf_size)))
  1066. #endif
  1067. ) {
  1068. goto fail;
  1069. }
  1070. if (global_count > 0) {
  1071. /* Initialize the global data */
  1072. global_data = module_inst->global_data;
  1073. global_data_end = global_data + global_data_size;
  1074. global = globals;
  1075. for (i = 0; i < global_count; i++, global++) {
  1076. switch (global->type) {
  1077. case VALUE_TYPE_I32:
  1078. case VALUE_TYPE_F32:
  1079. *(int32*)global_data = global->initial_value.i32;
  1080. global_data += sizeof(int32);
  1081. break;
  1082. case VALUE_TYPE_I64:
  1083. case VALUE_TYPE_F64:
  1084. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1085. &global->initial_value.i64, sizeof(int64));
  1086. global_data += sizeof(int64);
  1087. break;
  1088. default:
  1089. bh_assert(0);
  1090. }
  1091. }
  1092. bh_assert(global_data == global_data_end);
  1093. }
  1094. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1095. goto fail;
  1096. }
  1097. /* Initialize the memory data with data segment section */
  1098. module_inst->default_memory =
  1099. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1100. for (i = 0; i < module->data_seg_count; i++) {
  1101. WASMMemoryInstance *memory = NULL;
  1102. uint8 *memory_data = NULL;
  1103. uint32 memory_size = 0;
  1104. WASMDataSeg *data_seg = module->data_segments[i];
  1105. #if WASM_ENABLE_BULK_MEMORY != 0
  1106. if (data_seg->is_passive)
  1107. continue;
  1108. #endif
  1109. /* has check it in loader */
  1110. memory = module_inst->memories[data_seg->memory_index];
  1111. bh_assert(memory);
  1112. memory_data = memory->memory_data;
  1113. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1114. bh_assert(memory_data || memory_size == 0);
  1115. bh_assert(data_seg->base_offset.init_expr_type
  1116. == INIT_EXPR_TYPE_I32_CONST
  1117. || data_seg->base_offset.init_expr_type
  1118. == INIT_EXPR_TYPE_GET_GLOBAL);
  1119. if (data_seg->base_offset.init_expr_type
  1120. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1121. if (!check_global_init_expr(module,
  1122. data_seg->base_offset.u.global_index,
  1123. error_buf, error_buf_size)) {
  1124. goto fail;
  1125. }
  1126. if (!globals
  1127. || globals[data_seg->base_offset.u.global_index].type
  1128. != VALUE_TYPE_I32) {
  1129. set_error_buf(error_buf, error_buf_size,
  1130. "data segment does not fit");
  1131. goto fail;
  1132. }
  1133. data_seg->base_offset.u.i32 =
  1134. globals[data_seg->base_offset.u.global_index]
  1135. .initial_value.i32;
  1136. }
  1137. /* check offset since length might negative */
  1138. base_offset = (uint32)data_seg->base_offset.u.i32;
  1139. if (base_offset > memory_size) {
  1140. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1141. memory_size);
  1142. set_error_buf(error_buf, error_buf_size,
  1143. "data segment does not fit");
  1144. goto fail;
  1145. }
  1146. /* check offset + length(could be zero) */
  1147. length = data_seg->data_length;
  1148. if (base_offset + length > memory_size) {
  1149. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1150. base_offset, length, memory_size);
  1151. set_error_buf(error_buf, error_buf_size,
  1152. "data segment does not fit");
  1153. goto fail;
  1154. }
  1155. if (memory_data) {
  1156. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1157. data_seg->data, length);
  1158. }
  1159. }
  1160. /* Initialize the table data with table segment section */
  1161. module_inst->default_table =
  1162. module_inst->table_count ? module_inst->tables[0] : NULL;
  1163. for (i = 0; i < module->table_seg_count; i++) {
  1164. WASMTableSeg *table_seg = module->table_segments + i;
  1165. /* has check it in loader */
  1166. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1167. bh_assert(table);
  1168. uint32 *table_data = (uint32 *)table->base_addr;
  1169. #if WASM_ENABLE_MULTI_MODULE != 0
  1170. table_data = table->table_inst_linked
  1171. ? (uint32 *)table->table_inst_linked->base_addr
  1172. : table_data;
  1173. #endif
  1174. bh_assert(table_data);
  1175. /* init vec(funcidx) */
  1176. bh_assert(table_seg->base_offset.init_expr_type
  1177. == INIT_EXPR_TYPE_I32_CONST
  1178. || table_seg->base_offset.init_expr_type
  1179. == INIT_EXPR_TYPE_GET_GLOBAL);
  1180. if (table_seg->base_offset.init_expr_type
  1181. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1182. if (!check_global_init_expr(module,
  1183. table_seg->base_offset.u.global_index,
  1184. error_buf, error_buf_size)) {
  1185. goto fail;
  1186. }
  1187. if (!globals
  1188. || globals[table_seg->base_offset.u.global_index].type
  1189. != VALUE_TYPE_I32) {
  1190. set_error_buf(error_buf, error_buf_size,
  1191. "elements segment does not fit");
  1192. goto fail;
  1193. }
  1194. table_seg->base_offset.u.i32 =
  1195. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1196. }
  1197. /* check offset since length might negative */
  1198. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1199. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1200. table_seg->base_offset.u.i32, table->cur_size);
  1201. set_error_buf(error_buf, error_buf_size,
  1202. "elements segment does not fit");
  1203. goto fail;
  1204. }
  1205. /* check offset + length(could be zero) */
  1206. length = table_seg->function_count;
  1207. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1208. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1209. table_seg->base_offset.u.i32, length, table->cur_size);
  1210. set_error_buf(error_buf, error_buf_size,
  1211. "elements segment does not fit");
  1212. goto fail;
  1213. }
  1214. /**
  1215. * Check function index in the current module inst for now.
  1216. * will check the linked table inst owner in future.
  1217. * so loader check is enough
  1218. */
  1219. bh_memcpy_s(
  1220. table_data + table_seg->base_offset.u.i32,
  1221. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1222. * sizeof(uint32)),
  1223. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1224. }
  1225. /* module instance type */
  1226. module_inst->module_type = Wasm_Module_Bytecode;
  1227. /* Initialize the thread related data */
  1228. if (stack_size == 0)
  1229. stack_size = DEFAULT_WASM_STACK_SIZE;
  1230. #if WASM_ENABLE_SPEC_TEST != 0
  1231. if (stack_size < 48 *1024)
  1232. stack_size = 48 * 1024;
  1233. #endif
  1234. module_inst->default_wasm_stack_size = stack_size;
  1235. if (module->malloc_function != (uint32)-1) {
  1236. module_inst->malloc_function =
  1237. &module_inst->functions[module->malloc_function];
  1238. }
  1239. if (module->free_function != (uint32)-1) {
  1240. module_inst->free_function =
  1241. &module_inst->functions[module->free_function];
  1242. }
  1243. if (module->retain_function != (uint32)-1) {
  1244. module_inst->retain_function =
  1245. &module_inst->functions[module->retain_function];
  1246. }
  1247. #if WASM_ENABLE_LIBC_WASI != 0
  1248. /* The sub-instance will get the wasi_ctx from main-instance */
  1249. if (!is_sub_inst) {
  1250. if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1251. module->wasi_args.dir_list,
  1252. module->wasi_args.dir_count,
  1253. module->wasi_args.map_dir_list,
  1254. module->wasi_args.map_dir_count,
  1255. module->wasi_args.env,
  1256. module->wasi_args.env_count,
  1257. module->wasi_args.argv,
  1258. module->wasi_args.argc,
  1259. error_buf, error_buf_size)) {
  1260. goto fail;
  1261. }
  1262. }
  1263. #endif
  1264. if (module->start_function != (uint32)-1) {
  1265. /* TODO: fix start function can be import function issue */
  1266. if (module->start_function >= module->import_function_count)
  1267. module_inst->start_function =
  1268. &module_inst->functions[module->start_function];
  1269. }
  1270. /* Execute __post_instantiate function */
  1271. if (!execute_post_inst_function(module_inst)
  1272. || !execute_start_function(module_inst)) {
  1273. set_error_buf(error_buf, error_buf_size,
  1274. module_inst->cur_exception);
  1275. goto fail;
  1276. }
  1277. #if WASM_ENABLE_BULK_MEMORY != 0
  1278. #if WASM_ENABLE_LIBC_WASI != 0
  1279. if (!module->is_wasi_module) {
  1280. #endif
  1281. /* Only execute the memory init function for main instance because
  1282. the data segments will be dropped once initialized.
  1283. */
  1284. if (!is_sub_inst) {
  1285. if (!execute_memory_init_function(module_inst)) {
  1286. set_error_buf(error_buf, error_buf_size,
  1287. module_inst->cur_exception);
  1288. goto fail;
  1289. }
  1290. }
  1291. #if WASM_ENABLE_LIBC_WASI != 0
  1292. }
  1293. #endif
  1294. #endif
  1295. #if WASM_ENABLE_MEMORY_TRACING != 0
  1296. wasm_runtime_dump_module_inst_mem_consumption
  1297. ((WASMModuleInstanceCommon *)module_inst);
  1298. #endif
  1299. (void)global_data_end;
  1300. return module_inst;
  1301. fail:
  1302. wasm_deinstantiate(module_inst, false);
  1303. return NULL;
  1304. }
  1305. void
  1306. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1307. {
  1308. if (!module_inst)
  1309. return;
  1310. #if WASM_ENABLE_MULTI_MODULE != 0
  1311. sub_module_deinstantiate(module_inst);
  1312. #endif
  1313. #if WASM_ENABLE_LIBC_WASI != 0
  1314. /* Destroy wasi resource before freeing app heap, since some fields of
  1315. wasi contex are allocated from app heap, and if app heap is freed,
  1316. these fields will be set to NULL, we cannot free their internal data
  1317. which may allocated from global heap. */
  1318. /* Only destroy wasi ctx in the main module instance */
  1319. if (!is_sub_inst)
  1320. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1321. #endif
  1322. if (module_inst->memory_count > 0)
  1323. memories_deinstantiate(
  1324. module_inst,
  1325. module_inst->memories, module_inst->memory_count);
  1326. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1327. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1328. globals_deinstantiate(module_inst->globals);
  1329. export_functions_deinstantiate(module_inst->export_functions);
  1330. #if WASM_ENABLE_MULTI_MODULE != 0
  1331. export_globals_deinstantiate(module_inst->export_globals);
  1332. #endif
  1333. if (module_inst->global_data)
  1334. wasm_runtime_free(module_inst->global_data);
  1335. wasm_runtime_free(module_inst);
  1336. }
  1337. WASMFunctionInstance*
  1338. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1339. const char *name, const char *signature)
  1340. {
  1341. uint32 i;
  1342. for (i = 0; i < module_inst->export_func_count; i++)
  1343. if (!strcmp(module_inst->export_functions[i].name, name))
  1344. return module_inst->export_functions[i].function;
  1345. (void)signature;
  1346. return NULL;
  1347. }
  1348. #if WASM_ENABLE_MULTI_MODULE != 0
  1349. WASMGlobalInstance *
  1350. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1351. {
  1352. uint32 i;
  1353. for (i = 0; i < module_inst->export_glob_count; i++)
  1354. if (!strcmp(module_inst->export_globals[i].name, name))
  1355. return module_inst->export_globals[i].global;
  1356. return NULL;
  1357. }
  1358. WASMMemoryInstance *
  1359. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1360. {
  1361. /**
  1362. * using a strong assumption that one module instance only has
  1363. * one memory instance
  1364. */
  1365. (void)module_inst->export_memories;
  1366. return module_inst->memories[0];
  1367. }
  1368. WASMTableInstance *
  1369. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1370. {
  1371. /**
  1372. * using a strong assumption that one module instance only has
  1373. * one table instance
  1374. */
  1375. (void)module_inst->export_tables;
  1376. return module_inst->tables[0];
  1377. }
  1378. #endif
  1379. static bool
  1380. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1381. {
  1382. #if WASM_ENABLE_LIBC_WASI != 0
  1383. const char *exception = wasm_get_exception(module_inst);
  1384. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1385. /* The "wasi proc exit" exception is thrown by native lib to
  1386. let wasm app exit, which is a normal behavior, we clear
  1387. the exception here. */
  1388. wasm_set_exception(module_inst, NULL);
  1389. return true;
  1390. }
  1391. return false;
  1392. #else
  1393. return false;
  1394. #endif
  1395. }
  1396. bool
  1397. wasm_call_function(WASMExecEnv *exec_env,
  1398. WASMFunctionInstance *function,
  1399. unsigned argc, uint32 argv[])
  1400. {
  1401. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1402. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1403. (void)clear_wasi_proc_exit_exception(module_inst);
  1404. return !wasm_get_exception(module_inst) ? true : false;
  1405. }
  1406. bool
  1407. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1408. WASMFunctionInstance *func,
  1409. unsigned argc, uint32 argv[])
  1410. {
  1411. WASMExecEnv *exec_env;
  1412. bool ret;
  1413. #if WASM_ENABLE_THREAD_MGR != 0
  1414. WASMExecEnv *existing_exec_env = NULL;
  1415. if (!(existing_exec_env = exec_env =
  1416. wasm_clusters_search_exec_env(
  1417. (WASMModuleInstanceCommon*)module_inst))) {
  1418. #endif
  1419. if (!(exec_env = wasm_exec_env_create(
  1420. (WASMModuleInstanceCommon*)module_inst,
  1421. module_inst->default_wasm_stack_size))) {
  1422. wasm_set_exception(module_inst, "allocate memory failed");
  1423. return false;
  1424. }
  1425. /* set thread handle and stack boundary */
  1426. wasm_exec_env_set_thread_info(exec_env);
  1427. #if WASM_ENABLE_THREAD_MGR != 0
  1428. }
  1429. #endif
  1430. ret = wasm_call_function(exec_env, func, argc, argv);
  1431. #if WASM_ENABLE_THREAD_MGR != 0
  1432. /* don't destroy the exec_env if it's searched from the cluster */
  1433. if (!existing_exec_env)
  1434. #endif
  1435. wasm_exec_env_destroy(exec_env);
  1436. return ret;
  1437. }
  1438. void
  1439. wasm_set_exception(WASMModuleInstance *module_inst,
  1440. const char *exception)
  1441. {
  1442. if (exception)
  1443. snprintf(module_inst->cur_exception,
  1444. sizeof(module_inst->cur_exception),
  1445. "Exception: %s", exception);
  1446. else
  1447. module_inst->cur_exception[0] = '\0';
  1448. }
  1449. const char*
  1450. wasm_get_exception(WASMModuleInstance *module_inst)
  1451. {
  1452. if (module_inst->cur_exception[0] == '\0')
  1453. return NULL;
  1454. else
  1455. return module_inst->cur_exception;
  1456. }
  1457. #if WASM_ENABLE_PERF_PROFILING != 0
  1458. void
  1459. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  1460. {
  1461. WASMExportFuncInstance *export_func;
  1462. WASMFunctionInstance *func_inst;
  1463. char *func_name;
  1464. uint32 i, j;
  1465. os_printf("Performance profiler data:\n");
  1466. for (i = 0; i < module_inst->function_count; i++) {
  1467. func_inst = module_inst->functions + i;
  1468. if (func_inst->is_import_func) {
  1469. func_name = func_inst->u.func_import->field_name;
  1470. }
  1471. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1472. else if (func_inst->u.func->field_name) {
  1473. func_name = func_inst->u.func->field_name;
  1474. }
  1475. #endif
  1476. else {
  1477. func_name = NULL;
  1478. for (j = 0; j < module_inst->export_func_count; j++) {
  1479. export_func = module_inst->export_functions + j;
  1480. if (export_func->function == func_inst) {
  1481. func_name = export_func->name;
  1482. break;
  1483. }
  1484. }
  1485. }
  1486. if (func_name)
  1487. os_printf(" func %s, execution time: %.3f ms, execution count: %d times\n",
  1488. func_name, module_inst->functions[i].total_exec_time / 1000.0f,
  1489. module_inst->functions[i].total_exec_cnt);
  1490. else
  1491. os_printf(" func %d, execution time: %.3f ms, execution count: %d times\n",
  1492. i, module_inst->functions[i].total_exec_time / 1000.0f,
  1493. module_inst->functions[i].total_exec_cnt);
  1494. }
  1495. }
  1496. #endif
  1497. uint32
  1498. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1499. void **p_native_addr)
  1500. {
  1501. WASMMemoryInstance *memory = module_inst->default_memory;
  1502. uint8 *addr = NULL;
  1503. uint32 offset = 0;
  1504. if (!memory) {
  1505. wasm_set_exception(module_inst, "uninitialized memory");
  1506. return 0;
  1507. }
  1508. if (memory->heap_handle) {
  1509. addr = mem_allocator_malloc(memory->heap_handle, size);
  1510. }
  1511. else if (module_inst->malloc_function
  1512. && module_inst->free_function) {
  1513. if (!execute_malloc_function(module_inst,
  1514. module_inst->malloc_function,
  1515. module_inst->retain_function,
  1516. size, &offset)) {
  1517. return 0;
  1518. }
  1519. /* If we use app's malloc function,
  1520. the default memory may be changed while memory growing */
  1521. memory = module_inst->default_memory;
  1522. addr = offset ? memory->memory_data + offset : NULL;
  1523. }
  1524. if (!addr) {
  1525. if (memory->heap_handle
  1526. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1527. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  1528. "is compiled by wasi-sdk-12.0 or larger version, "
  1529. "please add -Wl,--export=malloc -Wl,--export=free "
  1530. " to export malloc and free functions.");
  1531. wasm_set_exception(module_inst, "app heap corrupted");
  1532. }
  1533. else {
  1534. wasm_set_exception(module_inst, "out of memory");
  1535. }
  1536. return 0;
  1537. }
  1538. if (p_native_addr)
  1539. *p_native_addr = addr;
  1540. return (uint32)(addr - memory->memory_data);
  1541. }
  1542. void
  1543. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1544. {
  1545. if (ptr) {
  1546. WASMMemoryInstance *memory = module_inst->default_memory;
  1547. uint8* addr;
  1548. if (!memory) {
  1549. return;
  1550. }
  1551. addr = memory->memory_data + ptr;
  1552. if (memory->heap_handle
  1553. && memory->heap_data <= addr
  1554. && addr < memory->heap_data_end) {
  1555. mem_allocator_free(memory->heap_handle, addr);
  1556. }
  1557. else if (module_inst->malloc_function
  1558. && module_inst->free_function
  1559. && memory->memory_data <= addr
  1560. && addr < memory->memory_data_end) {
  1561. execute_free_function(module_inst,
  1562. module_inst->free_function,
  1563. ptr);
  1564. }
  1565. }
  1566. }
  1567. uint32
  1568. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1569. const char *src, uint32 size)
  1570. {
  1571. char *buffer;
  1572. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1573. (void**)&buffer);
  1574. if (buffer_offset != 0) {
  1575. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1576. bh_memcpy_s(buffer, size, src, size);
  1577. }
  1578. return buffer_offset;
  1579. }
  1580. bool
  1581. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1582. uint32 app_offset, uint32 size)
  1583. {
  1584. WASMMemoryInstance *memory = module_inst->default_memory;
  1585. uint32 memory_data_size;
  1586. if (!memory) {
  1587. goto fail;
  1588. }
  1589. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1590. /* integer overflow check */
  1591. if (app_offset + size < app_offset) {
  1592. goto fail;
  1593. }
  1594. if (app_offset + size <= memory_data_size) {
  1595. return true;
  1596. }
  1597. fail:
  1598. wasm_set_exception(module_inst, "out of bounds memory access");
  1599. return false;
  1600. }
  1601. bool
  1602. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1603. void *native_ptr, uint32 size)
  1604. {
  1605. WASMMemoryInstance *memory = module_inst->default_memory;
  1606. uint8 *addr = (uint8 *)native_ptr;
  1607. if (!memory) {
  1608. goto fail;
  1609. }
  1610. /* integer overflow check */
  1611. if (addr + size < addr) {
  1612. goto fail;
  1613. }
  1614. if (memory->memory_data <= addr
  1615. && addr + size <= memory->memory_data_end) {
  1616. return true;
  1617. }
  1618. fail:
  1619. wasm_set_exception(module_inst, "out of bounds memory access");
  1620. return false;
  1621. }
  1622. void *
  1623. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1624. uint32 app_offset)
  1625. {
  1626. WASMMemoryInstance *memory = module_inst->default_memory;
  1627. uint8 *addr;
  1628. if (!memory)
  1629. return NULL;
  1630. addr = memory->memory_data + app_offset;
  1631. if (memory->memory_data <= addr
  1632. && addr < memory->memory_data_end)
  1633. return addr;
  1634. return NULL;
  1635. }
  1636. uint32
  1637. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1638. void *native_ptr)
  1639. {
  1640. WASMMemoryInstance *memory = module_inst->default_memory;
  1641. uint8 *addr = (uint8 *)native_ptr;
  1642. if (!memory)
  1643. return 0;
  1644. if (memory->memory_data <= addr
  1645. && addr < memory->memory_data_end)
  1646. return (uint32)(addr - memory->memory_data);
  1647. return 0;
  1648. }
  1649. bool
  1650. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1651. uint32 app_offset,
  1652. uint32 *p_app_start_offset,
  1653. uint32 *p_app_end_offset)
  1654. {
  1655. WASMMemoryInstance *memory = module_inst->default_memory;
  1656. uint32 memory_data_size;
  1657. if (!memory)
  1658. return false;
  1659. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1660. if (app_offset < memory_data_size) {
  1661. if (p_app_start_offset)
  1662. *p_app_start_offset = 0;
  1663. if (p_app_end_offset)
  1664. *p_app_end_offset = memory_data_size;
  1665. return true;
  1666. }
  1667. return false;
  1668. }
  1669. bool
  1670. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1671. uint8 *native_ptr,
  1672. uint8 **p_native_start_addr,
  1673. uint8 **p_native_end_addr)
  1674. {
  1675. WASMMemoryInstance *memory = module_inst->default_memory;
  1676. uint8 *addr = (uint8 *)native_ptr;
  1677. if (!memory)
  1678. return false;
  1679. if (memory->memory_data <= addr
  1680. && addr < memory->memory_data_end) {
  1681. if (p_native_start_addr)
  1682. *p_native_start_addr = memory->memory_data;
  1683. if (p_native_end_addr)
  1684. *p_native_end_addr = memory->memory_data_end;
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. bool
  1690. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1691. {
  1692. WASMMemoryInstance *memory = module->default_memory;
  1693. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1694. uint32 heap_size, total_size_old, total_page_count;
  1695. uint64 total_size;
  1696. if (!memory)
  1697. return false;
  1698. memory_data = memory->memory_data;
  1699. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1700. total_size_old = (uint32)(memory->memory_data_end - memory_data);
  1701. total_page_count = inc_page_count + memory->cur_page_count;
  1702. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1703. heap_data_old = memory->heap_data;
  1704. if (inc_page_count <= 0)
  1705. /* No need to enlarge memory */
  1706. return true;
  1707. if (total_page_count < memory->cur_page_count /* integer overflow */
  1708. || total_page_count > memory->max_page_count) {
  1709. return false;
  1710. }
  1711. if (total_size >= UINT32_MAX) {
  1712. return false;
  1713. }
  1714. #if WASM_ENABLE_SHARED_MEMORY != 0
  1715. if (memory->is_shared) {
  1716. /* For shared memory, we have reserved the maximum spaces during
  1717. instantiate, only change the cur_page_count here */
  1718. memory->cur_page_count = total_page_count;
  1719. return true;
  1720. }
  1721. #endif
  1722. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1723. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1724. return false;
  1725. }
  1726. if (memory_data) {
  1727. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1728. memory_data, total_size_old);
  1729. wasm_runtime_free(memory_data);
  1730. }
  1731. }
  1732. memset(new_memory_data + total_size_old,
  1733. 0, (uint32)total_size - total_size_old);
  1734. if (heap_size > 0) {
  1735. if (mem_allocator_migrate(memory->heap_handle,
  1736. (char *)heap_data_old
  1737. + (new_memory_data - memory_data),
  1738. heap_size) != 0) {
  1739. return false;
  1740. }
  1741. }
  1742. memory->memory_data = new_memory_data;
  1743. memory->cur_page_count = total_page_count;
  1744. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1745. memory->heap_data_end = memory->heap_data + heap_size;
  1746. memory->memory_data_end = memory->memory_data
  1747. + memory->num_bytes_per_page
  1748. * total_page_count;
  1749. return true;
  1750. }
  1751. bool
  1752. wasm_call_indirect(WASMExecEnv *exec_env,
  1753. uint32_t element_indices,
  1754. uint32_t argc, uint32_t argv[])
  1755. {
  1756. WASMModuleInstance *module_inst = NULL;
  1757. WASMTableInstance *table_inst = NULL;
  1758. uint32_t function_indices = 0;
  1759. WASMFunctionInstance *function_inst = NULL;
  1760. module_inst =
  1761. (WASMModuleInstance*)exec_env->module_inst;
  1762. bh_assert(module_inst);
  1763. table_inst = module_inst->default_table;
  1764. if (!table_inst) {
  1765. wasm_set_exception(module_inst, "unknown table");
  1766. goto got_exception;
  1767. }
  1768. if (element_indices >= table_inst->cur_size) {
  1769. wasm_set_exception(module_inst, "undefined element");
  1770. goto got_exception;
  1771. }
  1772. /**
  1773. * please be aware that table_inst->base_addr may point
  1774. * to another module's table
  1775. **/
  1776. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1777. if (function_indices == 0xFFFFFFFF) {
  1778. wasm_set_exception(module_inst, "uninitialized element");
  1779. goto got_exception;
  1780. }
  1781. /**
  1782. * we insist to call functions owned by the module itself
  1783. **/
  1784. if (function_indices >= module_inst->function_count) {
  1785. wasm_set_exception(module_inst, "unknown function");
  1786. goto got_exception;
  1787. }
  1788. function_inst = module_inst->functions + function_indices;
  1789. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1790. (void)clear_wasi_proc_exit_exception(module_inst);
  1791. return !wasm_get_exception(module_inst) ? true : false;
  1792. got_exception:
  1793. return false;
  1794. }
  1795. #if WASM_ENABLE_THREAD_MGR != 0
  1796. bool
  1797. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1798. uint32 start_offset, uint32 size)
  1799. {
  1800. WASMModuleInstance *module_inst =
  1801. (WASMModuleInstance*)exec_env->module_inst;
  1802. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1803. uint32 data_end = module_inst->module->aux_data_end;
  1804. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1805. bool is_stack_before_data =
  1806. stack_bottom < data_end ? true : false;
  1807. /* Check the aux stack space, currently we don't allocate space in heap */
  1808. if ((is_stack_before_data && (size > start_offset))
  1809. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1810. return false;
  1811. if (stack_top_idx != (uint32)-1) {
  1812. /* The aux stack top is a wasm global,
  1813. set the initial value for the global */
  1814. uint8 *global_addr =
  1815. module_inst->global_data +
  1816. module_inst->globals[stack_top_idx].data_offset;
  1817. *(int32*)global_addr = start_offset;
  1818. /* The aux stack boundary is a constant value,
  1819. set the value to exec_env */
  1820. exec_env->aux_stack_boundary = start_offset - size;
  1821. return true;
  1822. }
  1823. return false;
  1824. }
  1825. bool
  1826. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1827. uint32 *start_offset, uint32 *size)
  1828. {
  1829. WASMModuleInstance *module_inst =
  1830. (WASMModuleInstance*)exec_env->module_inst;
  1831. /* The aux stack information is resolved in loader
  1832. and store in module */
  1833. uint32 stack_bottom =
  1834. module_inst->module->aux_stack_bottom;
  1835. uint32 total_aux_stack_size =
  1836. module_inst->module->aux_stack_size;
  1837. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  1838. if (start_offset)
  1839. *start_offset = stack_bottom;
  1840. if (size)
  1841. *size = total_aux_stack_size;
  1842. return true;
  1843. }
  1844. return false;
  1845. }
  1846. #endif
  1847. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1848. void
  1849. wasm_get_module_mem_consumption(const WASMModule *module,
  1850. WASMModuleMemConsumption *mem_conspn)
  1851. {
  1852. uint32 i, size;
  1853. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1854. mem_conspn->module_struct_size = sizeof(WASMModule);
  1855. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  1856. for (i = 0; i < module->type_count; i++) {
  1857. WASMType *type = module->types[i];
  1858. size = offsetof(WASMType, types) +
  1859. sizeof(uint8) * (type->param_count + type->result_count);
  1860. mem_conspn->types_size += size;
  1861. }
  1862. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  1863. mem_conspn->functions_size = sizeof(WASMFunction *)
  1864. * module->function_count;
  1865. for (i = 0; i < module->function_count; i++) {
  1866. WASMFunction *func = module->functions[i];
  1867. WASMType *type = func->func_type;
  1868. size = sizeof(WASMFunction) + func->local_count
  1869. + sizeof(uint16) * (type->param_count + func->local_count);
  1870. #if WASM_ENABLE_FAST_INTERP != 0
  1871. size += func->code_compiled_size
  1872. + sizeof(uint32) * func->const_cell_num;
  1873. #endif
  1874. mem_conspn->functions_size += size;
  1875. }
  1876. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  1877. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  1878. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  1879. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  1880. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  1881. * module->table_seg_count;
  1882. for (i = 0; i < module->table_seg_count; i++) {
  1883. WASMTableSeg *table_seg = &module->table_segments[i];
  1884. mem_conspn->tables_size += sizeof(uint32)
  1885. * table_seg->function_count;
  1886. }
  1887. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  1888. * module->data_seg_count;
  1889. for (i = 0; i < module->data_seg_count; i++) {
  1890. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  1891. }
  1892. if (module->const_str_list) {
  1893. StringNode *node = module->const_str_list, *node_next;
  1894. while (node) {
  1895. node_next = node->next;
  1896. mem_conspn->const_strs_size += sizeof(StringNode)
  1897. + strlen(node->str) + 1;
  1898. node = node_next;
  1899. }
  1900. }
  1901. mem_conspn->total_size += mem_conspn->module_struct_size;
  1902. mem_conspn->total_size += mem_conspn->types_size;
  1903. mem_conspn->total_size += mem_conspn->imports_size;
  1904. mem_conspn->total_size += mem_conspn->functions_size;
  1905. mem_conspn->total_size += mem_conspn->tables_size;
  1906. mem_conspn->total_size += mem_conspn->memories_size;
  1907. mem_conspn->total_size += mem_conspn->globals_size;
  1908. mem_conspn->total_size += mem_conspn->exports_size;
  1909. mem_conspn->total_size += mem_conspn->table_segs_size;
  1910. mem_conspn->total_size += mem_conspn->data_segs_size;
  1911. mem_conspn->total_size += mem_conspn->const_strs_size;
  1912. #if WASM_ENABLE_AOT != 0
  1913. mem_conspn->total_size += mem_conspn->aot_code_size;
  1914. #endif
  1915. }
  1916. void
  1917. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  1918. WASMModuleInstMemConsumption *mem_conspn)
  1919. {
  1920. uint32 i, size;
  1921. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1922. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  1923. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  1924. * module_inst->memory_count;
  1925. for (i = 0; i < module_inst->memory_count; i++) {
  1926. WASMMemoryInstance *memory = module_inst->memories[i];
  1927. size = sizeof(WASMMemoryInstance)
  1928. + memory->num_bytes_per_page * memory->cur_page_count;
  1929. mem_conspn->memories_size += size;
  1930. mem_conspn->app_heap_size += memory->heap_data_end
  1931. - memory->heap_data;
  1932. /* size of app heap structure */
  1933. mem_conspn->memories_size +=
  1934. mem_allocator_get_heap_struct_size();
  1935. }
  1936. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  1937. * module_inst->table_count;
  1938. for (i = 0; i < module_inst->table_count; i++) {
  1939. WASMTableInstance *table = module_inst->tables[i];
  1940. size = offsetof(WASMTableInstance, base_addr)
  1941. + sizeof(uint32) * table->cur_size;
  1942. mem_conspn->tables_size += size;
  1943. }
  1944. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  1945. * module_inst->function_count;
  1946. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  1947. * module_inst->global_count;
  1948. if (module_inst->global_count > 0) {
  1949. WASMGlobalInstance *global =
  1950. &module_inst->globals[module_inst->global_count - 1];
  1951. mem_conspn->globals_size += global->data_offset
  1952. + wasm_value_type_size(global->type);
  1953. }
  1954. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  1955. * module_inst->export_func_count;
  1956. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  1957. mem_conspn->total_size += mem_conspn->memories_size;
  1958. mem_conspn->total_size += mem_conspn->functions_size;
  1959. mem_conspn->total_size += mem_conspn->tables_size;
  1960. mem_conspn->total_size += mem_conspn->globals_size;
  1961. mem_conspn->total_size += mem_conspn->exports_size;
  1962. }
  1963. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  1964. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1965. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1966. void
  1967. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  1968. {
  1969. WASMModuleInstance *module_inst =
  1970. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  1971. WASMInterpFrame *cur_frame =
  1972. wasm_exec_env_get_cur_frame(exec_env);
  1973. WASMFunctionInstance *func_inst;
  1974. WASMExportFuncInstance *export_func;
  1975. const char *func_name = NULL;
  1976. uint32 n, i;
  1977. os_printf("\n");
  1978. for (n = 0; cur_frame && cur_frame->function; n++) {
  1979. func_name = NULL;
  1980. func_inst = cur_frame->function;
  1981. if (func_inst->is_import_func) {
  1982. func_name = func_inst->u.func_import->field_name;
  1983. }
  1984. else {
  1985. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1986. func_name = func_inst->u.func->field_name;
  1987. #endif
  1988. /* if custom name section is not generated,
  1989. search symbols from export table */
  1990. if (!func_name) {
  1991. for (i = 0; i < module_inst->export_func_count; i++) {
  1992. export_func = module_inst->export_functions + i;
  1993. if (export_func->function == func_inst) {
  1994. func_name = export_func->name;
  1995. break;
  1996. }
  1997. }
  1998. }
  1999. }
  2000. /* function name not exported, print number instead */
  2001. if (func_name == NULL) {
  2002. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  2003. }
  2004. else {
  2005. os_printf("#%02d %s \n", n, func_name);
  2006. }
  2007. cur_frame = cur_frame->prev_frame;
  2008. }
  2009. os_printf("\n");
  2010. }
  2011. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */