wasm_runtime.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  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. uint32 max_size_fixed = 0;
  435. #if WASM_ENABLE_MULTI_MODULE != 0
  436. WASMTableInstance *table_inst_linked = NULL;
  437. WASMModuleInstance *module_inst_linked = NULL;
  438. if (import->u.table.import_module) {
  439. if (!(module_inst_linked =
  440. get_sub_module_inst(module_inst, import->u.table.import_module))) {
  441. set_error_buf(error_buf, error_buf_size, "unknown table");
  442. tables_deinstantiate(tables, table_count);
  443. return NULL;
  444. }
  445. if (!(table_inst_linked = wasm_lookup_table(module_inst_linked,
  446. import->u.table.field_name))) {
  447. set_error_buf(error_buf, error_buf_size, "unknown table");
  448. tables_deinstantiate(tables, table_count);
  449. return NULL;
  450. }
  451. total_size = offsetof(WASMTableInstance, base_addr);
  452. }
  453. else
  454. #endif
  455. {
  456. /* in order to save memory, alloc resource as few as possible */
  457. max_size_fixed = import->u.table.possible_grow
  458. ? import->u.table.max_size
  459. : import->u.table.init_size;
  460. /* it is a built-in table, every module has its own */
  461. total_size = offsetof(WASMTableInstance, base_addr);
  462. total_size += (uint64)max_size_fixed * sizeof(uint32);
  463. }
  464. if (!(table = tables[table_index++] = runtime_malloc
  465. (total_size, error_buf, error_buf_size))) {
  466. tables_deinstantiate(tables, table_count);
  467. return NULL;
  468. }
  469. /* Set all elements to -1 to mark them as uninitialized elements */
  470. memset(table, -1, (uint32)total_size);
  471. #if WASM_ENABLE_MULTI_MODULE != 0
  472. table->table_inst_linked = table_inst_linked;
  473. if (table_inst_linked != NULL) {
  474. table->elem_type = table_inst_linked->elem_type;
  475. table->cur_size = table_inst_linked->cur_size;
  476. table->max_size = table_inst_linked->max_size;
  477. }
  478. else
  479. #endif
  480. {
  481. table->elem_type = import->u.table.elem_type;
  482. table->cur_size = import->u.table.init_size;
  483. table->max_size = max_size_fixed;
  484. }
  485. }
  486. /* instantiate tables from table section */
  487. for (i = 0; i < module->table_count; i++) {
  488. uint32 max_size_fixed = 0;
  489. total_size = offsetof(WASMTableInstance, base_addr);
  490. #if WASM_ENABLE_MULTI_MODULE != 0
  491. /* in case, a module which imports this table will grow it */
  492. max_size_fixed = module->tables[i].max_size;
  493. #else
  494. max_size_fixed =
  495. module->tables[i].possible_grow
  496. ? module->tables[i].max_size
  497. : module->tables[i].init_size;
  498. #endif
  499. total_size += sizeof(uint32) * (uint64)max_size_fixed;
  500. if (!(table = tables[table_index++] = runtime_malloc
  501. (total_size, error_buf, error_buf_size))) {
  502. tables_deinstantiate(tables, table_count);
  503. return NULL;
  504. }
  505. /* Set all elements to -1 to mark them as uninitialized elements */
  506. memset(table, -1, (uint32)total_size);
  507. table->elem_type = module->tables[i].elem_type;
  508. table->cur_size = module->tables[i].init_size;
  509. table->max_size = max_size_fixed;
  510. #if WASM_ENABLE_MULTI_MODULE != 0
  511. table->table_inst_linked = NULL;
  512. #endif
  513. }
  514. bh_assert(table_index == table_count);
  515. (void)module_inst;
  516. return tables;
  517. }
  518. /**
  519. * Destroy function instances.
  520. */
  521. static void
  522. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  523. {
  524. if (functions) {
  525. wasm_runtime_free(functions);
  526. }
  527. }
  528. /**
  529. * Instantiate functions in a module.
  530. */
  531. static WASMFunctionInstance *
  532. functions_instantiate(const WASMModule *module,
  533. WASMModuleInstance *module_inst,
  534. char *error_buf, uint32 error_buf_size)
  535. {
  536. WASMImport *import;
  537. uint32 i, function_count =
  538. module->import_function_count + module->function_count;
  539. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  540. WASMFunctionInstance *functions, *function;
  541. if (!(functions = runtime_malloc(total_size,
  542. error_buf, error_buf_size))) {
  543. return NULL;
  544. }
  545. /* instantiate functions from import section */
  546. function = functions;
  547. import = module->import_functions;
  548. for (i = 0; i < module->import_function_count; i++, import++) {
  549. function->is_import_func = true;
  550. #if WASM_ENABLE_MULTI_MODULE != 0
  551. if (import->u.function.import_module) {
  552. function->import_module_inst =
  553. get_sub_module_inst(module_inst,
  554. import->u.function.import_module);
  555. if (function->import_module_inst) {
  556. function->import_func_inst =
  557. wasm_lookup_function(function->import_module_inst,
  558. import->u.function.field_name, NULL);
  559. }
  560. }
  561. #endif /* WASM_ENABLE_MULTI_MODULE */
  562. function->u.func_import = &import->u.function;
  563. function->param_cell_num =
  564. import->u.function.func_type->param_cell_num;
  565. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  566. function->param_count =
  567. (uint16)function->u.func_import->func_type->param_count;
  568. function->param_types = function->u.func_import->func_type->types;
  569. function->local_cell_num = 0;
  570. function->local_count = 0;
  571. function->local_types = NULL;
  572. function++;
  573. }
  574. /* instantiate functions from function section */
  575. for (i = 0; i < module->function_count; i++) {
  576. function->is_import_func = false;
  577. function->u.func = module->functions[i];
  578. function->param_cell_num = function->u.func->param_cell_num;
  579. function->ret_cell_num = function->u.func->ret_cell_num;
  580. function->local_cell_num = function->u.func->local_cell_num;
  581. function->param_count = (uint16)function->u.func->func_type->param_count;
  582. function->local_count = (uint16)function->u.func->local_count;
  583. function->param_types = function->u.func->func_type->types;
  584. function->local_types = function->u.func->local_types;
  585. function->local_offsets = function->u.func->local_offsets;
  586. #if WASM_ENABLE_FAST_INTERP != 0
  587. function->const_cell_num = function->u.func->const_cell_num;
  588. #endif
  589. function++;
  590. }
  591. bh_assert((uint32)(function - functions) == function_count);
  592. (void)module_inst;
  593. return functions;
  594. }
  595. /**
  596. * Destroy global instances.
  597. */
  598. static void
  599. globals_deinstantiate(WASMGlobalInstance *globals)
  600. {
  601. if (globals)
  602. wasm_runtime_free(globals);
  603. }
  604. static bool
  605. check_global_init_expr(const WASMModule *module, uint32 global_index,
  606. char *error_buf, uint32 error_buf_size)
  607. {
  608. if (global_index >= module->import_global_count + module->global_count) {
  609. set_error_buf_v(error_buf, error_buf_size,
  610. "unknown global %d", global_index);
  611. return false;
  612. }
  613. /**
  614. * Currently, constant expressions occurring as initializers of
  615. * globals are further constrained in that contained global.get
  616. * instructions are only allowed to refer to imported globals.
  617. *
  618. * And initializer expression cannot reference a mutable global.
  619. */
  620. if (global_index >= module->import_global_count
  621. || (module->import_globals + global_index)->u.global.is_mutable) {
  622. set_error_buf(error_buf, error_buf_size,
  623. "constant expression required");
  624. return false;
  625. }
  626. return true;
  627. }
  628. /**
  629. * Instantiate globals in a module.
  630. */
  631. static WASMGlobalInstance *
  632. globals_instantiate(const WASMModule *module,
  633. WASMModuleInstance *module_inst,
  634. uint32 *p_global_data_size, char *error_buf,
  635. uint32 error_buf_size)
  636. {
  637. WASMImport *import;
  638. uint32 global_data_offset = 0;
  639. uint32 i, global_count =
  640. module->import_global_count + module->global_count;
  641. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  642. WASMGlobalInstance *globals, *global;
  643. if (!(globals = runtime_malloc(total_size,
  644. error_buf, error_buf_size))) {
  645. return NULL;
  646. }
  647. /* instantiate globals from import section */
  648. global = globals;
  649. import = module->import_globals;
  650. for (i = 0; i < module->import_global_count; i++, import++) {
  651. WASMGlobalImport *global_import = &import->u.global;
  652. global->type = global_import->type;
  653. global->is_mutable = global_import->is_mutable;
  654. #if WASM_ENABLE_MULTI_MODULE != 0
  655. if (global_import->import_module) {
  656. if (!(global->import_module_inst = get_sub_module_inst(
  657. module_inst, global_import->import_module))) {
  658. set_error_buf(error_buf, error_buf_size, "unknown global");
  659. return NULL;
  660. }
  661. if (!(global->import_global_inst = wasm_lookup_global(
  662. global->import_module_inst, global_import->field_name))) {
  663. set_error_buf(error_buf, error_buf_size, "unknown global");
  664. return NULL;
  665. }
  666. /* The linked global instance has been initialized, we
  667. just need to copy the value. */
  668. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  669. &(global_import->import_global_linked->init_expr),
  670. sizeof(WASMValue));
  671. }
  672. else
  673. #endif
  674. {
  675. /* native globals share their initial_values in one module */
  676. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  677. &(global_import->global_data_linked),
  678. sizeof(WASMValue));
  679. }
  680. global->data_offset = global_data_offset;
  681. global_data_offset += wasm_value_type_size(global->type);
  682. global++;
  683. }
  684. /* instantiate globals from global section */
  685. for (i = 0; i < module->global_count; i++) {
  686. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  687. global->type = module->globals[i].type;
  688. global->is_mutable = module->globals[i].is_mutable;
  689. global->data_offset = global_data_offset;
  690. global_data_offset += wasm_value_type_size(global->type);
  691. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  692. if (!check_global_init_expr(module, init_expr->u.global_index,
  693. error_buf, error_buf_size)) {
  694. return NULL;
  695. }
  696. bh_memcpy_s(
  697. &(global->initial_value), sizeof(WASMValue),
  698. &(globals[init_expr->u.global_index].initial_value),
  699. sizeof(globals[init_expr->u.global_index].initial_value));
  700. }
  701. #if WASM_ENABLE_REF_TYPES != 0
  702. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  703. global->initial_value.u32 = (uint32)NULL_REF;
  704. }
  705. #endif
  706. else {
  707. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  708. &(init_expr->u), sizeof(init_expr->u));
  709. }
  710. global++;
  711. }
  712. bh_assert((uint32)(global - globals) == global_count);
  713. *p_global_data_size = global_data_offset;
  714. (void)module_inst;
  715. return globals;
  716. }
  717. /**
  718. * Return export function count in module export section.
  719. */
  720. static uint32
  721. get_export_count(const WASMModule *module, uint8 kind)
  722. {
  723. WASMExport *export = module->exports;
  724. uint32 count = 0, i;
  725. for (i = 0; i < module->export_count; i++, export++)
  726. if (export->kind == kind)
  727. count++;
  728. return count;
  729. }
  730. /**
  731. * Destroy export function instances.
  732. */
  733. static void
  734. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  735. {
  736. if (functions)
  737. wasm_runtime_free(functions);
  738. }
  739. /**
  740. * Instantiate export functions in a module.
  741. */
  742. static WASMExportFuncInstance*
  743. export_functions_instantiate(const WASMModule *module,
  744. WASMModuleInstance *module_inst,
  745. uint32 export_func_count,
  746. char *error_buf, uint32 error_buf_size)
  747. {
  748. WASMExportFuncInstance *export_funcs, *export_func;
  749. WASMExport *export = module->exports;
  750. uint32 i;
  751. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  752. if (!(export_func = export_funcs = runtime_malloc
  753. (total_size, error_buf, error_buf_size))) {
  754. return NULL;
  755. }
  756. for (i = 0; i < module->export_count; i++, export++)
  757. if (export->kind == EXPORT_KIND_FUNC) {
  758. export_func->name = export->name;
  759. export_func->function = &module_inst->functions[export->index];
  760. export_func++;
  761. }
  762. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  763. return export_funcs;
  764. }
  765. #if WASM_ENABLE_MULTI_MODULE != 0
  766. static void
  767. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  768. {
  769. if (globals)
  770. wasm_runtime_free(globals);
  771. }
  772. static WASMExportGlobInstance *
  773. export_globals_instantiate(const WASMModule *module,
  774. WASMModuleInstance *module_inst,
  775. uint32 export_glob_count, char *error_buf,
  776. uint32 error_buf_size)
  777. {
  778. WASMExportGlobInstance *export_globals, *export_global;
  779. WASMExport *export = module->exports;
  780. uint32 i;
  781. uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  782. if (!(export_global = export_globals = runtime_malloc
  783. (total_size, error_buf, error_buf_size))) {
  784. return NULL;
  785. }
  786. for (i = 0; i < module->export_count; i++, export++)
  787. if (export->kind == EXPORT_KIND_GLOBAL) {
  788. export_global->name = export->name;
  789. export_global->global = &module_inst->globals[export->index];
  790. export_global++;
  791. }
  792. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  793. return export_globals;
  794. }
  795. #endif
  796. static bool
  797. execute_post_inst_function(WASMModuleInstance *module_inst)
  798. {
  799. WASMFunctionInstance *post_inst_func = NULL;
  800. WASMType *post_inst_func_type;
  801. uint32 i;
  802. for (i = 0; i < module_inst->export_func_count; i++)
  803. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  804. post_inst_func = module_inst->export_functions[i].function;
  805. break;
  806. }
  807. if (!post_inst_func)
  808. /* Not found */
  809. return true;
  810. post_inst_func_type = post_inst_func->u.func->func_type;
  811. if (post_inst_func_type->param_count != 0
  812. || post_inst_func_type->result_count != 0)
  813. /* Not a valid function type, ignore it */
  814. return true;
  815. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  816. 0, NULL);
  817. }
  818. #if WASM_ENABLE_BULK_MEMORY != 0
  819. static bool
  820. execute_memory_init_function(WASMModuleInstance *module_inst)
  821. {
  822. WASMFunctionInstance *memory_init_func = NULL;
  823. WASMType *memory_init_func_type;
  824. uint32 i;
  825. for (i = 0; i < module_inst->export_func_count; i++)
  826. if (!strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) {
  827. memory_init_func = module_inst->export_functions[i].function;
  828. break;
  829. }
  830. if (!memory_init_func)
  831. /* Not found */
  832. return true;
  833. memory_init_func_type = memory_init_func->u.func->func_type;
  834. if (memory_init_func_type->param_count != 0
  835. || memory_init_func_type->result_count != 0)
  836. /* Not a valid function type, ignore it */
  837. return true;
  838. return wasm_create_exec_env_and_call_function(module_inst,
  839. memory_init_func,
  840. 0, NULL);
  841. }
  842. #endif
  843. static bool
  844. execute_start_function(WASMModuleInstance *module_inst)
  845. {
  846. WASMFunctionInstance *func = module_inst->start_function;
  847. if (!func)
  848. return true;
  849. bh_assert(!func->is_import_func && func->param_cell_num == 0
  850. && func->ret_cell_num == 0);
  851. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  852. }
  853. static bool
  854. execute_malloc_function(WASMModuleInstance *module_inst,
  855. WASMFunctionInstance *malloc_func,
  856. WASMFunctionInstance *retain_func,
  857. uint32 size, uint32 *p_result)
  858. {
  859. uint32 argv[2], argc;
  860. bool ret;
  861. argv[0] = size;
  862. argc = 1;
  863. /* if __retain is exported, then this module is compiled by
  864. assemblyscript, the memory should be managed by as's runtime,
  865. in this case we need to call the retain function after malloc
  866. the memory */
  867. if (retain_func) {
  868. /* the malloc functino from assemblyscript is:
  869. function __new(size: usize, id: u32)
  870. id = 0 means this is an ArrayBuffer object */
  871. argv[1] = 0;
  872. argc = 2;
  873. }
  874. ret = wasm_create_exec_env_and_call_function
  875. (module_inst, malloc_func, argc, argv);
  876. if (retain_func && ret) {
  877. ret = wasm_create_exec_env_and_call_function
  878. (module_inst, retain_func, 1, argv);
  879. }
  880. if (ret)
  881. *p_result = argv[0];
  882. return ret;
  883. }
  884. static bool
  885. execute_free_function(WASMModuleInstance *module_inst,
  886. WASMFunctionInstance *free_func,
  887. uint32 offset)
  888. {
  889. uint32 argv[2];
  890. argv[0] = offset;
  891. return wasm_create_exec_env_and_call_function
  892. (module_inst, free_func, 1, argv);
  893. }
  894. #if WASM_ENABLE_MULTI_MODULE != 0
  895. static bool
  896. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  897. uint32 stack_size, uint32 heap_size, char *error_buf,
  898. uint32 error_buf_size)
  899. {
  900. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  901. WASMRegisteredModule *sub_module_list_node =
  902. bh_list_first_elem(module->import_module_list);
  903. while (sub_module_list_node) {
  904. WASMSubModInstNode *sub_module_inst_list_node;
  905. WASMModule *sub_module = (WASMModule*)sub_module_list_node->module;
  906. WASMModuleInstance *sub_module_inst =
  907. wasm_instantiate(sub_module, false, stack_size, heap_size,
  908. error_buf, error_buf_size);
  909. if (!sub_module_inst) {
  910. LOG_DEBUG("instantiate %s failed",
  911. sub_module_list_node->module_name);
  912. return false;
  913. }
  914. sub_module_inst_list_node = runtime_malloc
  915. (sizeof(WASMSubModInstNode), error_buf, error_buf_size);
  916. if (!sub_module_inst_list_node) {
  917. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  918. sizeof(WASMSubModInstNode));
  919. wasm_deinstantiate(sub_module_inst, false);
  920. return false;
  921. }
  922. sub_module_inst_list_node->module_inst = sub_module_inst;
  923. sub_module_inst_list_node->module_name =
  924. sub_module_list_node->module_name;
  925. bh_list_status ret =
  926. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  927. bh_assert(BH_LIST_SUCCESS == ret);
  928. (void)ret;
  929. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  930. }
  931. return true;
  932. }
  933. static void
  934. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  935. {
  936. bh_list *list = module_inst->sub_module_inst_list;
  937. WASMSubModInstNode *node = bh_list_first_elem(list);
  938. while (node) {
  939. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  940. bh_list_remove(list, node);
  941. wasm_deinstantiate(node->module_inst, false);
  942. wasm_runtime_free(node);
  943. node = next_node;
  944. }
  945. }
  946. #endif
  947. static bool
  948. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  949. uint32 error_buf_size)
  950. {
  951. WASMModule *module = module_inst->module;
  952. uint32 i;
  953. for (i = 0; i < module->import_function_count; i++) {
  954. WASMFunctionImport *func =
  955. &((module->import_functions + i)->u.function);
  956. if (!func->func_ptr_linked
  957. #if WASM_ENABLE_MULTI_MODULE != 0
  958. && !func->import_func_linked
  959. #endif
  960. ) {
  961. #if WASM_ENABLE_SPEC_TEST != 0
  962. set_error_buf(error_buf, error_buf_size,
  963. "unknown import or incompatible import type");
  964. return false;
  965. #else
  966. #if WASM_ENABLE_WAMR_COMPILER == 0
  967. LOG_WARNING("warning: failed to link import function (%s, %s)",
  968. func->module_name, func->field_name);
  969. #else
  970. /* do nothing to avoid confused message */
  971. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  972. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  973. }
  974. }
  975. for (i = 0; i < module->import_global_count; i++) {
  976. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  977. if (!global->is_linked) {
  978. #if WASM_ENABLE_SPEC_TEST != 0
  979. set_error_buf(error_buf, error_buf_size,
  980. "unknown import or incompatible import type");
  981. return false;
  982. #else
  983. #if WASM_ENABLE_WAMR_COMPILER == 0
  984. LOG_DEBUG("warning: failed to link import global (%s, %s)",
  985. global->module_name, global->field_name);
  986. #else
  987. /* do nothing to avoid confused message */
  988. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  989. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  990. }
  991. }
  992. return true;
  993. }
  994. /**
  995. * Instantiate module
  996. */
  997. WASMModuleInstance*
  998. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  999. uint32 stack_size, uint32 heap_size,
  1000. char *error_buf, uint32 error_buf_size)
  1001. {
  1002. WASMModuleInstance *module_inst;
  1003. WASMGlobalInstance *globals = NULL, *global;
  1004. uint32 global_count, global_data_size = 0, i;
  1005. uint32 base_offset, length;
  1006. uint8 *global_data, *global_data_end;
  1007. #if WASM_ENABLE_MULTI_MODULE != 0
  1008. bool ret = false;
  1009. #endif
  1010. if (!module)
  1011. return NULL;
  1012. /* Check heap size */
  1013. heap_size = align_uint(heap_size, 8);
  1014. if (heap_size > APP_HEAP_SIZE_MAX)
  1015. heap_size = APP_HEAP_SIZE_MAX;
  1016. /* Allocate the memory */
  1017. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  1018. error_buf, error_buf_size))) {
  1019. return NULL;
  1020. }
  1021. module_inst->module = module;
  1022. #if WASM_ENABLE_MULTI_MODULE != 0
  1023. module_inst->sub_module_inst_list =
  1024. &module_inst->sub_module_inst_list_head;
  1025. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1026. error_buf, error_buf_size);
  1027. if (!ret) {
  1028. LOG_DEBUG("build a sub module list failed");
  1029. goto fail;
  1030. }
  1031. #endif
  1032. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1033. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1034. error_buf, error_buf_size))) {
  1035. goto fail;
  1036. }
  1037. #endif
  1038. /* Instantiate global firstly to get the mutable data size */
  1039. global_count = module->import_global_count + module->global_count;
  1040. if (global_count
  1041. && !(globals = globals_instantiate(module, module_inst,
  1042. &global_data_size,
  1043. error_buf, error_buf_size))) {
  1044. goto fail;
  1045. }
  1046. module_inst->global_count = global_count;
  1047. module_inst->globals = globals;
  1048. module_inst->memory_count =
  1049. module->import_memory_count + module->memory_count;
  1050. module_inst->table_count =
  1051. module->import_table_count + module->table_count;
  1052. module_inst->function_count =
  1053. module->import_function_count + module->function_count;
  1054. /* export */
  1055. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1056. #if WASM_ENABLE_MULTI_MODULE != 0
  1057. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1058. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1059. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1060. #endif
  1061. if (global_count > 0) {
  1062. if (!(module_inst->global_data = runtime_malloc
  1063. (global_data_size, error_buf, error_buf_size))) {
  1064. goto fail;
  1065. }
  1066. }
  1067. /* Instantiate memories/tables/functions */
  1068. if ((module_inst->memory_count > 0
  1069. && !(module_inst->memories =
  1070. memories_instantiate(module,
  1071. module_inst,
  1072. heap_size, error_buf, error_buf_size)))
  1073. || (module_inst->table_count > 0
  1074. && !(module_inst->tables =
  1075. tables_instantiate(module,
  1076. module_inst,
  1077. error_buf, error_buf_size)))
  1078. || (module_inst->function_count > 0
  1079. && !(module_inst->functions =
  1080. functions_instantiate(module,
  1081. module_inst,
  1082. error_buf, error_buf_size)))
  1083. || (module_inst->export_func_count > 0
  1084. && !(module_inst->export_functions = export_functions_instantiate(
  1085. module, module_inst, module_inst->export_func_count,
  1086. error_buf, error_buf_size)))
  1087. #if WASM_ENABLE_MULTI_MODULE != 0
  1088. || (module_inst->export_glob_count > 0
  1089. && !(module_inst->export_globals = export_globals_instantiate(
  1090. module, module_inst, module_inst->export_glob_count,
  1091. error_buf, error_buf_size)))
  1092. #endif
  1093. ) {
  1094. goto fail;
  1095. }
  1096. if (global_count > 0) {
  1097. /* Initialize the global data */
  1098. global_data = module_inst->global_data;
  1099. global_data_end = global_data + global_data_size;
  1100. global = globals;
  1101. for (i = 0; i < global_count; i++, global++) {
  1102. switch (global->type) {
  1103. case VALUE_TYPE_I32:
  1104. case VALUE_TYPE_F32:
  1105. #if WASM_ENABLE_REF_TYPES != 0
  1106. case VALUE_TYPE_FUNCREF:
  1107. case VALUE_TYPE_EXTERNREF:
  1108. #endif
  1109. *(int32*)global_data = global->initial_value.i32;
  1110. global_data += sizeof(int32);
  1111. break;
  1112. case VALUE_TYPE_I64:
  1113. case VALUE_TYPE_F64:
  1114. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1115. &global->initial_value.i64, sizeof(int64));
  1116. global_data += sizeof(int64);
  1117. break;
  1118. default:
  1119. bh_assert(0);
  1120. }
  1121. }
  1122. bh_assert(global_data == global_data_end);
  1123. }
  1124. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1125. goto fail;
  1126. }
  1127. /* Initialize the memory data with data segment section */
  1128. module_inst->default_memory =
  1129. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1130. for (i = 0; i < module->data_seg_count; i++) {
  1131. WASMMemoryInstance *memory = NULL;
  1132. uint8 *memory_data = NULL;
  1133. uint32 memory_size = 0;
  1134. WASMDataSeg *data_seg = module->data_segments[i];
  1135. #if WASM_ENABLE_BULK_MEMORY != 0
  1136. if (data_seg->is_passive)
  1137. continue;
  1138. #endif
  1139. /* has check it in loader */
  1140. memory = module_inst->memories[data_seg->memory_index];
  1141. bh_assert(memory);
  1142. memory_data = memory->memory_data;
  1143. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1144. bh_assert(memory_data || memory_size == 0);
  1145. bh_assert(data_seg->base_offset.init_expr_type
  1146. == INIT_EXPR_TYPE_I32_CONST
  1147. || data_seg->base_offset.init_expr_type
  1148. == INIT_EXPR_TYPE_GET_GLOBAL);
  1149. if (data_seg->base_offset.init_expr_type
  1150. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1151. if (!check_global_init_expr(module,
  1152. data_seg->base_offset.u.global_index,
  1153. error_buf, error_buf_size)) {
  1154. goto fail;
  1155. }
  1156. if (!globals
  1157. || globals[data_seg->base_offset.u.global_index].type
  1158. != VALUE_TYPE_I32) {
  1159. set_error_buf(error_buf, error_buf_size,
  1160. "data segment does not fit");
  1161. goto fail;
  1162. }
  1163. data_seg->base_offset.u.i32 =
  1164. globals[data_seg->base_offset.u.global_index]
  1165. .initial_value.i32;
  1166. }
  1167. /* check offset */
  1168. base_offset = (uint32)data_seg->base_offset.u.i32;
  1169. if (base_offset > memory_size) {
  1170. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1171. memory_size);
  1172. #if WASM_ENABLE_REF_TYPES != 0
  1173. set_error_buf(error_buf, error_buf_size,
  1174. "out of bounds memory access");
  1175. #else
  1176. set_error_buf(error_buf, error_buf_size,
  1177. "data segment does not fit");
  1178. #endif
  1179. goto fail;
  1180. }
  1181. /* check offset + length(could be zero) */
  1182. length = data_seg->data_length;
  1183. if (base_offset + length > memory_size) {
  1184. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1185. base_offset, length, memory_size);
  1186. #if WASM_ENABLE_REF_TYPES != 0
  1187. set_error_buf(error_buf, error_buf_size,
  1188. "out of bounds memory access");
  1189. #else
  1190. set_error_buf(error_buf, error_buf_size,
  1191. "data segment does not fit");
  1192. #endif
  1193. goto fail;
  1194. }
  1195. if (memory_data) {
  1196. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1197. data_seg->data, length);
  1198. }
  1199. }
  1200. /* Initialize the table data with table segment section */
  1201. module_inst->default_table =
  1202. module_inst->table_count ? module_inst->tables[0] : NULL;
  1203. /* in case there is no table */
  1204. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1205. i++) {
  1206. WASMTableSeg *table_seg = module->table_segments + i;
  1207. /* has check it in loader */
  1208. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1209. bh_assert(table);
  1210. #if WASM_ENABLE_REF_TYPES != 0
  1211. if (table->elem_type != VALUE_TYPE_FUNCREF
  1212. && table->elem_type != VALUE_TYPE_EXTERNREF) {
  1213. set_error_buf(error_buf, error_buf_size,
  1214. "elements segment does not fit");
  1215. goto fail;
  1216. }
  1217. #endif
  1218. uint32 *table_data = (uint32 *)table->base_addr;
  1219. #if WASM_ENABLE_MULTI_MODULE != 0
  1220. table_data = table->table_inst_linked
  1221. ? (uint32 *)table->table_inst_linked->base_addr
  1222. : table_data;
  1223. #endif
  1224. bh_assert(table_data);
  1225. #if WASM_ENABLE_REF_TYPES != 0
  1226. if (!wasm_elem_is_active(table_seg->mode))
  1227. continue;
  1228. #endif
  1229. /* init vec(funcidx) or vec(expr) */
  1230. bh_assert(
  1231. table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  1232. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  1233. #if WASM_ENABLE_REF_TYPES != 0
  1234. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  1235. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST
  1236. #endif
  1237. );
  1238. if (table_seg->base_offset.init_expr_type
  1239. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1240. if (!check_global_init_expr(module,
  1241. table_seg->base_offset.u.global_index,
  1242. error_buf, error_buf_size)) {
  1243. goto fail;
  1244. }
  1245. if (!globals
  1246. || globals[table_seg->base_offset.u.global_index].type
  1247. != VALUE_TYPE_I32) {
  1248. set_error_buf(error_buf, error_buf_size,
  1249. "elements segment does not fit");
  1250. goto fail;
  1251. }
  1252. table_seg->base_offset.u.i32 =
  1253. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1254. }
  1255. /* check offset since length might negative */
  1256. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1257. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1258. table_seg->base_offset.u.i32, table->cur_size);
  1259. #if WASM_ENABLE_REF_TYPES != 0
  1260. set_error_buf(error_buf, error_buf_size,
  1261. "out of bounds table access");
  1262. #else
  1263. set_error_buf(error_buf, error_buf_size,
  1264. "elements segment does not fit");
  1265. #endif
  1266. goto fail;
  1267. }
  1268. /* check offset + length(could be zero) */
  1269. length = table_seg->function_count;
  1270. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1271. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1272. table_seg->base_offset.u.i32, length, table->cur_size);
  1273. #if WASM_ENABLE_REF_TYPES != 0
  1274. set_error_buf(error_buf, error_buf_size,
  1275. "out of bounds table access");
  1276. #else
  1277. set_error_buf(error_buf, error_buf_size,
  1278. "elements segment does not fit");
  1279. #endif
  1280. goto fail;
  1281. }
  1282. /**
  1283. * Check function index in the current module inst for now.
  1284. * will check the linked table inst owner in future.
  1285. * so loader check is enough
  1286. */
  1287. bh_memcpy_s(
  1288. table_data + table_seg->base_offset.u.i32,
  1289. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1290. * sizeof(uint32)),
  1291. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1292. }
  1293. /* module instance type */
  1294. module_inst->module_type = Wasm_Module_Bytecode;
  1295. /* Initialize the thread related data */
  1296. if (stack_size == 0)
  1297. stack_size = DEFAULT_WASM_STACK_SIZE;
  1298. #if WASM_ENABLE_SPEC_TEST != 0
  1299. if (stack_size < 48 *1024)
  1300. stack_size = 48 * 1024;
  1301. #endif
  1302. module_inst->default_wasm_stack_size = stack_size;
  1303. if (module->malloc_function != (uint32)-1) {
  1304. module_inst->malloc_function =
  1305. &module_inst->functions[module->malloc_function];
  1306. }
  1307. if (module->free_function != (uint32)-1) {
  1308. module_inst->free_function =
  1309. &module_inst->functions[module->free_function];
  1310. }
  1311. if (module->retain_function != (uint32)-1) {
  1312. module_inst->retain_function =
  1313. &module_inst->functions[module->retain_function];
  1314. }
  1315. #if WASM_ENABLE_LIBC_WASI != 0
  1316. /* The sub-instance will get the wasi_ctx from main-instance */
  1317. if (!is_sub_inst) {
  1318. if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1319. module->wasi_args.dir_list,
  1320. module->wasi_args.dir_count,
  1321. module->wasi_args.map_dir_list,
  1322. module->wasi_args.map_dir_count,
  1323. module->wasi_args.env,
  1324. module->wasi_args.env_count,
  1325. module->wasi_args.argv,
  1326. module->wasi_args.argc,
  1327. module->wasi_args.stdio[0],
  1328. module->wasi_args.stdio[1],
  1329. module->wasi_args.stdio[2],
  1330. error_buf, error_buf_size)) {
  1331. goto fail;
  1332. }
  1333. }
  1334. #endif
  1335. if (module->start_function != (uint32)-1) {
  1336. /* TODO: fix start function can be import function issue */
  1337. if (module->start_function >= module->import_function_count)
  1338. module_inst->start_function =
  1339. &module_inst->functions[module->start_function];
  1340. }
  1341. /* Execute __post_instantiate function */
  1342. if (!execute_post_inst_function(module_inst)
  1343. || !execute_start_function(module_inst)) {
  1344. set_error_buf(error_buf, error_buf_size,
  1345. module_inst->cur_exception);
  1346. goto fail;
  1347. }
  1348. #if WASM_ENABLE_BULK_MEMORY != 0
  1349. #if WASM_ENABLE_LIBC_WASI != 0
  1350. if (!module->is_wasi_module) {
  1351. #endif
  1352. /* Only execute the memory init function for main instance because
  1353. the data segments will be dropped once initialized.
  1354. */
  1355. if (!is_sub_inst) {
  1356. if (!execute_memory_init_function(module_inst)) {
  1357. set_error_buf(error_buf, error_buf_size,
  1358. module_inst->cur_exception);
  1359. goto fail;
  1360. }
  1361. }
  1362. #if WASM_ENABLE_LIBC_WASI != 0
  1363. }
  1364. #endif
  1365. #endif
  1366. #if WASM_ENABLE_MEMORY_TRACING != 0
  1367. wasm_runtime_dump_module_inst_mem_consumption
  1368. ((WASMModuleInstanceCommon *)module_inst);
  1369. #endif
  1370. (void)global_data_end;
  1371. return module_inst;
  1372. fail:
  1373. wasm_deinstantiate(module_inst, false);
  1374. return NULL;
  1375. }
  1376. void
  1377. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1378. {
  1379. if (!module_inst)
  1380. return;
  1381. #if WASM_ENABLE_MULTI_MODULE != 0
  1382. sub_module_deinstantiate(module_inst);
  1383. #endif
  1384. #if WASM_ENABLE_LIBC_WASI != 0
  1385. /* Destroy wasi resource before freeing app heap, since some fields of
  1386. wasi contex are allocated from app heap, and if app heap is freed,
  1387. these fields will be set to NULL, we cannot free their internal data
  1388. which may allocated from global heap. */
  1389. /* Only destroy wasi ctx in the main module instance */
  1390. if (!is_sub_inst)
  1391. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1392. #endif
  1393. if (module_inst->memory_count > 0)
  1394. memories_deinstantiate(
  1395. module_inst,
  1396. module_inst->memories, module_inst->memory_count);
  1397. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1398. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1399. globals_deinstantiate(module_inst->globals);
  1400. export_functions_deinstantiate(module_inst->export_functions);
  1401. #if WASM_ENABLE_MULTI_MODULE != 0
  1402. export_globals_deinstantiate(module_inst->export_globals);
  1403. #endif
  1404. if (module_inst->global_data)
  1405. wasm_runtime_free(module_inst->global_data);
  1406. #if WASM_ENABLE_REF_TYPES != 0
  1407. wasm_externref_cleanup((WASMModuleInstanceCommon*)module_inst);
  1408. #endif
  1409. if (module_inst->exec_env_singleton)
  1410. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1411. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1412. if (module_inst->frames) {
  1413. bh_vector_destroy(module_inst->frames);
  1414. wasm_runtime_free(module_inst->frames);
  1415. module_inst->frames = NULL;
  1416. }
  1417. #endif
  1418. wasm_runtime_free(module_inst);
  1419. }
  1420. WASMFunctionInstance*
  1421. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1422. const char *name, const char *signature)
  1423. {
  1424. uint32 i;
  1425. for (i = 0; i < module_inst->export_func_count; i++)
  1426. if (!strcmp(module_inst->export_functions[i].name, name))
  1427. return module_inst->export_functions[i].function;
  1428. (void)signature;
  1429. return NULL;
  1430. }
  1431. #if WASM_ENABLE_MULTI_MODULE != 0
  1432. WASMGlobalInstance *
  1433. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1434. {
  1435. uint32 i;
  1436. for (i = 0; i < module_inst->export_glob_count; i++)
  1437. if (!strcmp(module_inst->export_globals[i].name, name))
  1438. return module_inst->export_globals[i].global;
  1439. return NULL;
  1440. }
  1441. WASMMemoryInstance *
  1442. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1443. {
  1444. /**
  1445. * using a strong assumption that one module instance only has
  1446. * one memory instance
  1447. */
  1448. (void)module_inst->export_memories;
  1449. return module_inst->memories[0];
  1450. }
  1451. WASMTableInstance *
  1452. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1453. {
  1454. /**
  1455. * using a strong assumption that one module instance only has
  1456. * one table instance
  1457. */
  1458. (void)module_inst->export_tables;
  1459. return module_inst->tables[0];
  1460. }
  1461. #endif
  1462. static bool
  1463. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1464. {
  1465. #if WASM_ENABLE_LIBC_WASI != 0
  1466. const char *exception = wasm_get_exception(module_inst);
  1467. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1468. /* The "wasi proc exit" exception is thrown by native lib to
  1469. let wasm app exit, which is a normal behavior, we clear
  1470. the exception here. */
  1471. wasm_set_exception(module_inst, NULL);
  1472. return true;
  1473. }
  1474. return false;
  1475. #else
  1476. return false;
  1477. #endif
  1478. }
  1479. bool
  1480. wasm_call_function(WASMExecEnv *exec_env,
  1481. WASMFunctionInstance *function,
  1482. unsigned argc, uint32 argv[])
  1483. {
  1484. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1485. /* set thread handle and stack boundary */
  1486. wasm_exec_env_set_thread_info(exec_env);
  1487. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1488. (void)clear_wasi_proc_exit_exception(module_inst);
  1489. return !wasm_get_exception(module_inst) ? true : false;
  1490. }
  1491. bool
  1492. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1493. WASMFunctionInstance *func,
  1494. unsigned argc, uint32 argv[])
  1495. {
  1496. WASMExecEnv *exec_env;
  1497. bool ret;
  1498. #if WASM_ENABLE_THREAD_MGR != 0
  1499. WASMExecEnv *existing_exec_env = NULL;
  1500. if (!(existing_exec_env = exec_env =
  1501. wasm_clusters_search_exec_env(
  1502. (WASMModuleInstanceCommon*)module_inst))) {
  1503. #endif
  1504. if (!(exec_env = wasm_exec_env_create(
  1505. (WASMModuleInstanceCommon*)module_inst,
  1506. module_inst->default_wasm_stack_size))) {
  1507. wasm_set_exception(module_inst, "allocate memory failed");
  1508. return false;
  1509. }
  1510. #if WASM_ENABLE_THREAD_MGR != 0
  1511. }
  1512. #endif
  1513. #if WASM_ENABLE_REF_TYPES != 0
  1514. wasm_runtime_prepare_call_function(exec_env, func);
  1515. #endif
  1516. ret = wasm_call_function(exec_env, func, argc, argv);
  1517. #if WASM_ENABLE_REF_TYPES != 0
  1518. wasm_runtime_finalize_call_function(exec_env, func, ret, argv);
  1519. #endif
  1520. #if WASM_ENABLE_THREAD_MGR != 0
  1521. /* don't destroy the exec_env if it's searched from the cluster */
  1522. if (!existing_exec_env)
  1523. #endif
  1524. wasm_exec_env_destroy(exec_env);
  1525. return ret;
  1526. }
  1527. bool
  1528. wasm_create_exec_env_singleton(WASMModuleInstance *module_inst)
  1529. {
  1530. WASMExecEnv *exec_env =
  1531. wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  1532. module_inst->default_wasm_stack_size);
  1533. if (exec_env)
  1534. module_inst->exec_env_singleton = exec_env;
  1535. return exec_env ? true : false;
  1536. }
  1537. void
  1538. wasm_set_exception(WASMModuleInstance *module_inst,
  1539. const char *exception)
  1540. {
  1541. if (exception)
  1542. snprintf(module_inst->cur_exception,
  1543. sizeof(module_inst->cur_exception),
  1544. "Exception: %s", exception);
  1545. else
  1546. module_inst->cur_exception[0] = '\0';
  1547. }
  1548. const char*
  1549. wasm_get_exception(WASMModuleInstance *module_inst)
  1550. {
  1551. if (module_inst->cur_exception[0] == '\0')
  1552. return NULL;
  1553. else
  1554. return module_inst->cur_exception;
  1555. }
  1556. #if WASM_ENABLE_PERF_PROFILING != 0
  1557. void
  1558. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  1559. {
  1560. WASMExportFuncInstance *export_func;
  1561. WASMFunctionInstance *func_inst;
  1562. char *func_name;
  1563. uint32 i, j;
  1564. os_printf("Performance profiler data:\n");
  1565. for (i = 0; i < module_inst->function_count; i++) {
  1566. func_inst = module_inst->functions + i;
  1567. if (func_inst->is_import_func) {
  1568. func_name = func_inst->u.func_import->field_name;
  1569. }
  1570. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1571. else if (func_inst->u.func->field_name) {
  1572. func_name = func_inst->u.func->field_name;
  1573. }
  1574. #endif
  1575. else {
  1576. func_name = NULL;
  1577. for (j = 0; j < module_inst->export_func_count; j++) {
  1578. export_func = module_inst->export_functions + j;
  1579. if (export_func->function == func_inst) {
  1580. func_name = export_func->name;
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. if (func_name)
  1586. os_printf(" func %s, execution time: %.3f ms, execution count: %d times\n",
  1587. func_name, module_inst->functions[i].total_exec_time / 1000.0f,
  1588. module_inst->functions[i].total_exec_cnt);
  1589. else
  1590. os_printf(" func %d, execution time: %.3f ms, execution count: %d times\n",
  1591. i, module_inst->functions[i].total_exec_time / 1000.0f,
  1592. module_inst->functions[i].total_exec_cnt);
  1593. }
  1594. }
  1595. #endif
  1596. uint32
  1597. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1598. void **p_native_addr)
  1599. {
  1600. WASMMemoryInstance *memory = module_inst->default_memory;
  1601. uint8 *addr = NULL;
  1602. uint32 offset = 0;
  1603. if (!memory) {
  1604. wasm_set_exception(module_inst, "uninitialized memory");
  1605. return 0;
  1606. }
  1607. if (memory->heap_handle) {
  1608. addr = mem_allocator_malloc(memory->heap_handle, size);
  1609. }
  1610. else if (module_inst->malloc_function
  1611. && module_inst->free_function) {
  1612. if (!execute_malloc_function(module_inst,
  1613. module_inst->malloc_function,
  1614. module_inst->retain_function,
  1615. size, &offset)) {
  1616. return 0;
  1617. }
  1618. /* If we use app's malloc function,
  1619. the default memory may be changed while memory growing */
  1620. memory = module_inst->default_memory;
  1621. addr = offset ? memory->memory_data + offset : NULL;
  1622. }
  1623. if (!addr) {
  1624. if (memory->heap_handle
  1625. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1626. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  1627. "is compiled by wasi-sdk-12.0 or larger version, "
  1628. "please add -Wl,--export=malloc -Wl,--export=free "
  1629. " to export malloc and free functions.");
  1630. wasm_set_exception(module_inst, "app heap corrupted");
  1631. }
  1632. else {
  1633. wasm_set_exception(module_inst, "out of memory");
  1634. }
  1635. return 0;
  1636. }
  1637. if (p_native_addr)
  1638. *p_native_addr = addr;
  1639. return (uint32)(addr - memory->memory_data);
  1640. }
  1641. uint32
  1642. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  1643. void **p_native_addr)
  1644. {
  1645. WASMMemoryInstance *memory = module_inst->default_memory;
  1646. uint8 *addr = NULL;
  1647. if (!memory) {
  1648. wasm_set_exception(module_inst, "uninitialized memory");
  1649. return 0;
  1650. }
  1651. if (memory->heap_handle) {
  1652. addr = mem_allocator_realloc(memory->heap_handle,
  1653. memory->memory_data + ptr, size);
  1654. }
  1655. /* Only support realloc in WAMR's app heap */
  1656. if (!addr) {
  1657. if (memory->heap_handle
  1658. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1659. wasm_set_exception(module_inst, "app heap corrupted");
  1660. }
  1661. else {
  1662. wasm_set_exception(module_inst, "out of memory");
  1663. }
  1664. return 0;
  1665. }
  1666. if (p_native_addr)
  1667. *p_native_addr = addr;
  1668. return (uint32)(addr - memory->memory_data);
  1669. }
  1670. void
  1671. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1672. {
  1673. if (ptr) {
  1674. WASMMemoryInstance *memory = module_inst->default_memory;
  1675. uint8* addr;
  1676. if (!memory) {
  1677. return;
  1678. }
  1679. addr = memory->memory_data + ptr;
  1680. if (memory->heap_handle
  1681. && memory->heap_data <= addr
  1682. && addr < memory->heap_data_end) {
  1683. mem_allocator_free(memory->heap_handle, addr);
  1684. }
  1685. else if (module_inst->malloc_function
  1686. && module_inst->free_function
  1687. && memory->memory_data <= addr
  1688. && addr < memory->memory_data_end) {
  1689. execute_free_function(module_inst,
  1690. module_inst->free_function,
  1691. ptr);
  1692. }
  1693. }
  1694. }
  1695. uint32
  1696. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1697. const char *src, uint32 size)
  1698. {
  1699. char *buffer;
  1700. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1701. (void**)&buffer);
  1702. if (buffer_offset != 0) {
  1703. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1704. bh_memcpy_s(buffer, size, src, size);
  1705. }
  1706. return buffer_offset;
  1707. }
  1708. bool
  1709. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1710. uint32 app_offset, uint32 size)
  1711. {
  1712. WASMMemoryInstance *memory = module_inst->default_memory;
  1713. uint32 memory_data_size;
  1714. if (!memory) {
  1715. goto fail;
  1716. }
  1717. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1718. /* integer overflow check */
  1719. if (app_offset > UINT32_MAX - size) {
  1720. goto fail;
  1721. }
  1722. if (app_offset + size <= memory_data_size) {
  1723. return true;
  1724. }
  1725. fail:
  1726. wasm_set_exception(module_inst, "out of bounds memory access");
  1727. return false;
  1728. }
  1729. bool
  1730. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1731. void *native_ptr, uint32 size)
  1732. {
  1733. WASMMemoryInstance *memory = module_inst->default_memory;
  1734. uint8 *addr = (uint8 *)native_ptr;
  1735. if (!memory) {
  1736. goto fail;
  1737. }
  1738. /* integer overflow check */
  1739. if ((uintptr_t)addr > UINTPTR_MAX - size) {
  1740. goto fail;
  1741. }
  1742. if (memory->memory_data <= addr
  1743. && addr + size <= memory->memory_data_end) {
  1744. return true;
  1745. }
  1746. fail:
  1747. wasm_set_exception(module_inst, "out of bounds memory access");
  1748. return false;
  1749. }
  1750. void *
  1751. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1752. uint32 app_offset)
  1753. {
  1754. WASMMemoryInstance *memory = module_inst->default_memory;
  1755. uint8 *addr;
  1756. if (!memory)
  1757. return NULL;
  1758. addr = memory->memory_data + app_offset;
  1759. if (memory->memory_data <= addr
  1760. && addr < memory->memory_data_end)
  1761. return addr;
  1762. return NULL;
  1763. }
  1764. uint32
  1765. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1766. void *native_ptr)
  1767. {
  1768. WASMMemoryInstance *memory = module_inst->default_memory;
  1769. uint8 *addr = (uint8 *)native_ptr;
  1770. if (!memory)
  1771. return 0;
  1772. if (memory->memory_data <= addr
  1773. && addr < memory->memory_data_end)
  1774. return (uint32)(addr - memory->memory_data);
  1775. return 0;
  1776. }
  1777. bool
  1778. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1779. uint32 app_offset,
  1780. uint32 *p_app_start_offset,
  1781. uint32 *p_app_end_offset)
  1782. {
  1783. WASMMemoryInstance *memory = module_inst->default_memory;
  1784. uint32 memory_data_size;
  1785. if (!memory)
  1786. return false;
  1787. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1788. if (app_offset < memory_data_size) {
  1789. if (p_app_start_offset)
  1790. *p_app_start_offset = 0;
  1791. if (p_app_end_offset)
  1792. *p_app_end_offset = memory_data_size;
  1793. return true;
  1794. }
  1795. return false;
  1796. }
  1797. bool
  1798. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1799. uint8 *native_ptr,
  1800. uint8 **p_native_start_addr,
  1801. uint8 **p_native_end_addr)
  1802. {
  1803. WASMMemoryInstance *memory = module_inst->default_memory;
  1804. uint8 *addr = (uint8 *)native_ptr;
  1805. if (!memory)
  1806. return false;
  1807. if (memory->memory_data <= addr
  1808. && addr < memory->memory_data_end) {
  1809. if (p_native_start_addr)
  1810. *p_native_start_addr = memory->memory_data;
  1811. if (p_native_end_addr)
  1812. *p_native_end_addr = memory->memory_data_end;
  1813. return true;
  1814. }
  1815. return false;
  1816. }
  1817. bool
  1818. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1819. {
  1820. WASMMemoryInstance *memory = module->default_memory;
  1821. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1822. uint32 heap_size, total_size_old, total_page_count;
  1823. uint64 total_size;
  1824. if (!memory)
  1825. return false;
  1826. memory_data = memory->memory_data;
  1827. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1828. total_size_old = (uint32)(memory->memory_data_end - memory_data);
  1829. total_page_count = inc_page_count + memory->cur_page_count;
  1830. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1831. heap_data_old = memory->heap_data;
  1832. if (inc_page_count <= 0)
  1833. /* No need to enlarge memory */
  1834. return true;
  1835. if (total_page_count < memory->cur_page_count /* integer overflow */
  1836. || total_page_count > memory->max_page_count) {
  1837. return false;
  1838. }
  1839. if (total_size >= UINT32_MAX) {
  1840. return false;
  1841. }
  1842. #if WASM_ENABLE_SHARED_MEMORY != 0
  1843. if (memory->is_shared) {
  1844. /* For shared memory, we have reserved the maximum spaces during
  1845. instantiate, only change the cur_page_count here */
  1846. memory->cur_page_count = total_page_count;
  1847. return true;
  1848. }
  1849. #endif
  1850. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1851. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1852. return false;
  1853. }
  1854. if (memory_data) {
  1855. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1856. memory_data, total_size_old);
  1857. wasm_runtime_free(memory_data);
  1858. }
  1859. }
  1860. memset(new_memory_data + total_size_old,
  1861. 0, (uint32)total_size - total_size_old);
  1862. if (heap_size > 0) {
  1863. if (mem_allocator_migrate(memory->heap_handle,
  1864. (char *)heap_data_old
  1865. + (new_memory_data - memory_data),
  1866. heap_size) != 0) {
  1867. return false;
  1868. }
  1869. }
  1870. memory->memory_data = new_memory_data;
  1871. memory->cur_page_count = total_page_count;
  1872. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1873. memory->heap_data_end = memory->heap_data + heap_size;
  1874. memory->memory_data_end = memory->memory_data
  1875. + memory->num_bytes_per_page
  1876. * total_page_count;
  1877. return true;
  1878. }
  1879. #if WASM_ENABLE_REF_TYPES != 0
  1880. bool
  1881. wasm_enlarge_table(WASMModuleInstance *module_inst,
  1882. uint32 table_idx, uint32 inc_entries, uint32 init_val)
  1883. {
  1884. uint32 entry_count, *new_table_data_start, i;
  1885. WASMTableInstance *table_inst;
  1886. if (!inc_entries) {
  1887. return true;
  1888. }
  1889. bh_assert(table_idx < module_inst->table_count);
  1890. table_inst = wasm_get_table_inst(module_inst, table_idx);
  1891. if (!table_inst) {
  1892. return false;
  1893. }
  1894. if (inc_entries > UINT32_MAX - table_inst->cur_size) {
  1895. return false;
  1896. }
  1897. entry_count = table_inst->cur_size + inc_entries;
  1898. if (entry_count > table_inst->max_size) {
  1899. return false;
  1900. }
  1901. /* fill in */
  1902. new_table_data_start =
  1903. (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr))
  1904. + table_inst->cur_size;
  1905. for (i = 0; i < inc_entries; ++i) {
  1906. new_table_data_start[i] = init_val;
  1907. }
  1908. table_inst->cur_size = entry_count;
  1909. return true;
  1910. }
  1911. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1912. bool
  1913. wasm_call_indirect(WASMExecEnv *exec_env,
  1914. uint32_t tbl_idx,
  1915. uint32_t element_indices,
  1916. uint32_t argc, uint32_t argv[])
  1917. {
  1918. WASMModuleInstance *module_inst = NULL;
  1919. WASMTableInstance *table_inst = NULL;
  1920. uint32_t function_indices = 0;
  1921. WASMFunctionInstance *function_inst = NULL;
  1922. module_inst =
  1923. (WASMModuleInstance*)exec_env->module_inst;
  1924. bh_assert(module_inst);
  1925. table_inst = module_inst->tables[tbl_idx];
  1926. if (!table_inst) {
  1927. wasm_set_exception(module_inst, "unknown table");
  1928. goto got_exception;
  1929. }
  1930. if (element_indices >= table_inst->cur_size) {
  1931. wasm_set_exception(module_inst, "undefined element");
  1932. goto got_exception;
  1933. }
  1934. /**
  1935. * please be aware that table_inst->base_addr may point
  1936. * to another module's table
  1937. **/
  1938. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1939. if (function_indices == NULL_REF) {
  1940. wasm_set_exception(module_inst, "uninitialized element");
  1941. goto got_exception;
  1942. }
  1943. /**
  1944. * we insist to call functions owned by the module itself
  1945. **/
  1946. if (function_indices >= module_inst->function_count) {
  1947. wasm_set_exception(module_inst, "unknown function");
  1948. goto got_exception;
  1949. }
  1950. function_inst = module_inst->functions + function_indices;
  1951. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1952. (void)clear_wasi_proc_exit_exception(module_inst);
  1953. return !wasm_get_exception(module_inst) ? true : false;
  1954. got_exception:
  1955. return false;
  1956. }
  1957. #if WASM_ENABLE_THREAD_MGR != 0
  1958. bool
  1959. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1960. uint32 start_offset, uint32 size)
  1961. {
  1962. WASMModuleInstance *module_inst =
  1963. (WASMModuleInstance*)exec_env->module_inst;
  1964. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1965. uint32 data_end = module_inst->module->aux_data_end;
  1966. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1967. bool is_stack_before_data =
  1968. stack_bottom < data_end ? true : false;
  1969. /* Check the aux stack space, currently we don't allocate space in heap */
  1970. if ((is_stack_before_data && (size > start_offset))
  1971. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1972. return false;
  1973. if (stack_top_idx != (uint32)-1) {
  1974. /* The aux stack top is a wasm global,
  1975. set the initial value for the global */
  1976. uint8 *global_addr =
  1977. module_inst->global_data +
  1978. module_inst->globals[stack_top_idx].data_offset;
  1979. *(int32*)global_addr = start_offset;
  1980. /* The aux stack boundary is a constant value,
  1981. set the value to exec_env */
  1982. exec_env->aux_stack_boundary.boundary = start_offset - size;
  1983. exec_env->aux_stack_bottom.bottom = start_offset;
  1984. return true;
  1985. }
  1986. return false;
  1987. }
  1988. bool
  1989. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1990. uint32 *start_offset, uint32 *size)
  1991. {
  1992. WASMModuleInstance *module_inst =
  1993. (WASMModuleInstance*)exec_env->module_inst;
  1994. /* The aux stack information is resolved in loader
  1995. and store in module */
  1996. uint32 stack_bottom =
  1997. module_inst->module->aux_stack_bottom;
  1998. uint32 total_aux_stack_size =
  1999. module_inst->module->aux_stack_size;
  2000. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2001. if (start_offset)
  2002. *start_offset = stack_bottom;
  2003. if (size)
  2004. *size = total_aux_stack_size;
  2005. return true;
  2006. }
  2007. return false;
  2008. }
  2009. #endif
  2010. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2011. void
  2012. wasm_get_module_mem_consumption(const WASMModule *module,
  2013. WASMModuleMemConsumption *mem_conspn)
  2014. {
  2015. uint32 i, size;
  2016. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2017. mem_conspn->module_struct_size = sizeof(WASMModule);
  2018. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2019. for (i = 0; i < module->type_count; i++) {
  2020. WASMType *type = module->types[i];
  2021. size = offsetof(WASMType, types) +
  2022. sizeof(uint8) * (type->param_count + type->result_count);
  2023. mem_conspn->types_size += size;
  2024. }
  2025. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2026. mem_conspn->functions_size = sizeof(WASMFunction *)
  2027. * module->function_count;
  2028. for (i = 0; i < module->function_count; i++) {
  2029. WASMFunction *func = module->functions[i];
  2030. WASMType *type = func->func_type;
  2031. size = sizeof(WASMFunction) + func->local_count
  2032. + sizeof(uint16) * (type->param_count + func->local_count);
  2033. #if WASM_ENABLE_FAST_INTERP != 0
  2034. size += func->code_compiled_size
  2035. + sizeof(uint32) * func->const_cell_num;
  2036. #endif
  2037. mem_conspn->functions_size += size;
  2038. }
  2039. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2040. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2041. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2042. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2043. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  2044. * module->table_seg_count;
  2045. for (i = 0; i < module->table_seg_count; i++) {
  2046. WASMTableSeg *table_seg = &module->table_segments[i];
  2047. mem_conspn->tables_size += sizeof(uint32)
  2048. * table_seg->function_count;
  2049. }
  2050. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  2051. * module->data_seg_count;
  2052. for (i = 0; i < module->data_seg_count; i++) {
  2053. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2054. }
  2055. if (module->const_str_list) {
  2056. StringNode *node = module->const_str_list, *node_next;
  2057. while (node) {
  2058. node_next = node->next;
  2059. mem_conspn->const_strs_size += sizeof(StringNode)
  2060. + strlen(node->str) + 1;
  2061. node = node_next;
  2062. }
  2063. }
  2064. mem_conspn->total_size += mem_conspn->module_struct_size;
  2065. mem_conspn->total_size += mem_conspn->types_size;
  2066. mem_conspn->total_size += mem_conspn->imports_size;
  2067. mem_conspn->total_size += mem_conspn->functions_size;
  2068. mem_conspn->total_size += mem_conspn->tables_size;
  2069. mem_conspn->total_size += mem_conspn->memories_size;
  2070. mem_conspn->total_size += mem_conspn->globals_size;
  2071. mem_conspn->total_size += mem_conspn->exports_size;
  2072. mem_conspn->total_size += mem_conspn->table_segs_size;
  2073. mem_conspn->total_size += mem_conspn->data_segs_size;
  2074. mem_conspn->total_size += mem_conspn->const_strs_size;
  2075. #if WASM_ENABLE_AOT != 0
  2076. mem_conspn->total_size += mem_conspn->aot_code_size;
  2077. #endif
  2078. }
  2079. void
  2080. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2081. WASMModuleInstMemConsumption *mem_conspn)
  2082. {
  2083. uint32 i, size;
  2084. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2085. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  2086. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  2087. * module_inst->memory_count;
  2088. for (i = 0; i < module_inst->memory_count; i++) {
  2089. WASMMemoryInstance *memory = module_inst->memories[i];
  2090. size = sizeof(WASMMemoryInstance)
  2091. + memory->num_bytes_per_page * memory->cur_page_count;
  2092. mem_conspn->memories_size += size;
  2093. mem_conspn->app_heap_size += memory->heap_data_end
  2094. - memory->heap_data;
  2095. /* size of app heap structure */
  2096. mem_conspn->memories_size +=
  2097. mem_allocator_get_heap_struct_size();
  2098. }
  2099. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  2100. * module_inst->table_count;
  2101. for (i = 0; i < module_inst->table_count; i++) {
  2102. WASMTableInstance *table = module_inst->tables[i];
  2103. #if WASM_ENABLE_MULTI_MODULE != 0
  2104. if (table->table_inst_linked) {
  2105. size = offsetof(WASMTableInstance, base_addr);
  2106. }
  2107. else
  2108. #endif
  2109. {
  2110. size = offsetof(WASMTableInstance, base_addr)
  2111. + sizeof(uint32) * table->cur_size;
  2112. }
  2113. mem_conspn->tables_size += size;
  2114. }
  2115. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  2116. * module_inst->function_count;
  2117. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  2118. * module_inst->global_count;
  2119. if (module_inst->global_count > 0) {
  2120. WASMGlobalInstance *global =
  2121. &module_inst->globals[module_inst->global_count - 1];
  2122. mem_conspn->globals_size += global->data_offset
  2123. + wasm_value_type_size(global->type);
  2124. }
  2125. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  2126. * module_inst->export_func_count;
  2127. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2128. mem_conspn->total_size += mem_conspn->memories_size;
  2129. mem_conspn->total_size += mem_conspn->functions_size;
  2130. mem_conspn->total_size += mem_conspn->tables_size;
  2131. mem_conspn->total_size += mem_conspn->globals_size;
  2132. mem_conspn->total_size += mem_conspn->exports_size;
  2133. }
  2134. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  2135. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2136. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2137. void
  2138. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  2139. {
  2140. WASMModuleInstance *module_inst =
  2141. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2142. WASMInterpFrame *first_frame,
  2143. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2144. uint32 n = 0;
  2145. /* count frames includes a function */
  2146. first_frame = cur_frame;
  2147. while (cur_frame) {
  2148. if (cur_frame->function) {
  2149. n++;
  2150. }
  2151. cur_frame = cur_frame->prev_frame;
  2152. }
  2153. /* release previous stack frames and create new ones */
  2154. if (!bh_vector_destroy(module_inst->frames)
  2155. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame))) {
  2156. return;
  2157. }
  2158. cur_frame = first_frame;
  2159. n = 0;
  2160. os_printf("\n");
  2161. while (cur_frame) {
  2162. WASMCApiFrame frame = { 0 };
  2163. WASMFunctionInstance *func_inst = cur_frame->function;
  2164. const char *func_name = NULL;
  2165. const uint8 *func_code_base = NULL;
  2166. if (!func_inst) {
  2167. cur_frame = cur_frame->prev_frame;
  2168. continue;
  2169. }
  2170. /* place holder, will overwrite it in wasm_c_api */
  2171. frame.instance = module_inst;
  2172. frame.module_offset = 0;
  2173. frame.func_index = func_inst - module_inst->functions;
  2174. func_code_base = wasm_get_func_code(func_inst);
  2175. if (!cur_frame->ip || !func_code_base) {
  2176. frame.func_offset = 0;
  2177. }
  2178. else {
  2179. frame.func_offset = cur_frame->ip - func_code_base;
  2180. }
  2181. /* look for the function name */
  2182. if (func_inst->is_import_func) {
  2183. func_name = func_inst->u.func_import->field_name;
  2184. }
  2185. else {
  2186. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2187. func_name = func_inst->u.func->field_name;
  2188. #endif
  2189. /* if custom name section is not generated,
  2190. search symbols from export table */
  2191. if (!func_name) {
  2192. uint32 i;
  2193. for (i = 0; i < module_inst->export_func_count; i++) {
  2194. WASMExportFuncInstance *export_func =
  2195. module_inst->export_functions + i;
  2196. if (export_func->function == func_inst) {
  2197. func_name = export_func->name;
  2198. break;
  2199. }
  2200. }
  2201. }
  2202. }
  2203. /* function name not exported, print number instead */
  2204. if (func_name == NULL) {
  2205. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  2206. }
  2207. else {
  2208. os_printf("#%02d %s \n", n, func_name);
  2209. }
  2210. /* keep print */
  2211. bh_vector_append(module_inst->frames, &frame);
  2212. cur_frame = cur_frame->prev_frame;
  2213. n++;
  2214. }
  2215. os_printf("\n");
  2216. }
  2217. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */