wasm_runtime.c 82 KB

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