wasm_runtime.c 84 KB

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