wasm_runtime.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574
  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(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_WAMR_COMPILER == 0
  968. LOG_WARNING("warning: failed to link import function (%s, %s)",
  969. func->module_name, func->field_name);
  970. #else
  971. /* do nothing to avoid confused message */
  972. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  973. }
  974. }
  975. for (i = 0; i < module->import_global_count; i++) {
  976. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  977. if (!global->is_linked) {
  978. #if WASM_ENABLE_SPEC_TEST != 0
  979. set_error_buf(error_buf, error_buf_size,
  980. "unknown import or incompatible import type");
  981. return false;
  982. #else
  983. #if WASM_ENABLE_WAMR_COMPILER == 0
  984. LOG_DEBUG("warning: failed to link import global (%s, %s)",
  985. global->module_name, global->field_name);
  986. #else
  987. /* do nothing to avoid confused message */
  988. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  989. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  990. }
  991. }
  992. return true;
  993. }
  994. /**
  995. * Instantiate module
  996. */
  997. WASMModuleInstance *
  998. wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
  999. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  1000. {
  1001. WASMModuleInstance *module_inst;
  1002. WASMGlobalInstance *globals = NULL, *global;
  1003. uint32 global_count, global_data_size = 0, i;
  1004. uint32 base_offset, length;
  1005. uint8 *global_data, *global_data_end;
  1006. #if WASM_ENABLE_MULTI_MODULE != 0
  1007. bool ret = false;
  1008. #endif
  1009. if (!module)
  1010. return NULL;
  1011. #if WASM_ENABLE_DEBUG_INTERP != 0
  1012. if (!is_sub_inst) {
  1013. os_mutex_lock(&module->ref_count_lock);
  1014. if (module->ref_count != 0) {
  1015. LOG_WARNING(
  1016. "warning: multiple instances referencing the same module may "
  1017. "cause unexpected behaviour during debugging");
  1018. }
  1019. module->ref_count++;
  1020. os_mutex_unlock(&module->ref_count_lock);
  1021. }
  1022. #endif
  1023. /* Check heap size */
  1024. heap_size = align_uint(heap_size, 8);
  1025. if (heap_size > APP_HEAP_SIZE_MAX)
  1026. heap_size = APP_HEAP_SIZE_MAX;
  1027. /* Allocate the memory */
  1028. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance), error_buf,
  1029. error_buf_size))) {
  1030. #if WASM_ENABLE_DEBUG_INTERP != 0
  1031. if (!is_sub_inst) {
  1032. os_mutex_lock(&module->ref_count_lock);
  1033. module->ref_count--;
  1034. os_mutex_unlock(&module->ref_count_lock);
  1035. }
  1036. #endif
  1037. return NULL;
  1038. }
  1039. module_inst->module = module;
  1040. #if WASM_ENABLE_MULTI_MODULE != 0
  1041. module_inst->sub_module_inst_list = &module_inst->sub_module_inst_list_head;
  1042. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1043. error_buf, error_buf_size);
  1044. if (!ret) {
  1045. LOG_DEBUG("build a sub module list failed");
  1046. goto fail;
  1047. }
  1048. #endif
  1049. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1050. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1051. error_buf, error_buf_size))) {
  1052. goto fail;
  1053. }
  1054. #endif
  1055. /* Instantiate global firstly to get the mutable data size */
  1056. global_count = module->import_global_count + module->global_count;
  1057. if (global_count
  1058. && !(globals =
  1059. globals_instantiate(module, module_inst, &global_data_size,
  1060. error_buf, error_buf_size))) {
  1061. goto fail;
  1062. }
  1063. module_inst->global_count = global_count;
  1064. module_inst->globals = globals;
  1065. module_inst->memory_count =
  1066. module->import_memory_count + module->memory_count;
  1067. module_inst->table_count = module->import_table_count + module->table_count;
  1068. module_inst->function_count =
  1069. module->import_function_count + module->function_count;
  1070. /* export */
  1071. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1072. #if WASM_ENABLE_MULTI_MODULE != 0
  1073. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1074. module_inst->export_mem_count =
  1075. get_export_count(module, EXPORT_KIND_MEMORY);
  1076. module_inst->export_glob_count =
  1077. get_export_count(module, EXPORT_KIND_GLOBAL);
  1078. #endif
  1079. if (global_count > 0) {
  1080. if (!(module_inst->global_data = runtime_malloc(
  1081. global_data_size, error_buf, error_buf_size))) {
  1082. goto fail;
  1083. }
  1084. }
  1085. /* Instantiate memories/tables/functions */
  1086. if ((module_inst->memory_count > 0
  1087. && !(module_inst->memories = memories_instantiate(
  1088. module, module_inst, heap_size, error_buf, error_buf_size)))
  1089. || (module_inst->table_count > 0
  1090. && !(module_inst->tables = tables_instantiate(
  1091. module, module_inst, error_buf, error_buf_size)))
  1092. || (module_inst->function_count > 0
  1093. && !(module_inst->functions = functions_instantiate(
  1094. module, module_inst, error_buf, error_buf_size)))
  1095. || (module_inst->export_func_count > 0
  1096. && !(module_inst->export_functions = export_functions_instantiate(
  1097. module, module_inst, module_inst->export_func_count,
  1098. error_buf, error_buf_size)))
  1099. #if WASM_ENABLE_MULTI_MODULE != 0
  1100. || (module_inst->export_glob_count > 0
  1101. && !(module_inst->export_globals = export_globals_instantiate(
  1102. module, module_inst, module_inst->export_glob_count,
  1103. error_buf, error_buf_size)))
  1104. #endif
  1105. ) {
  1106. goto fail;
  1107. }
  1108. if (global_count > 0) {
  1109. /* Initialize the global data */
  1110. global_data = module_inst->global_data;
  1111. global_data_end = global_data + global_data_size;
  1112. global = globals;
  1113. for (i = 0; i < global_count; i++, global++) {
  1114. switch (global->type) {
  1115. case VALUE_TYPE_I32:
  1116. case VALUE_TYPE_F32:
  1117. #if WASM_ENABLE_REF_TYPES != 0
  1118. case VALUE_TYPE_FUNCREF:
  1119. case VALUE_TYPE_EXTERNREF:
  1120. #endif
  1121. *(int32 *)global_data = global->initial_value.i32;
  1122. global_data += sizeof(int32);
  1123. break;
  1124. case VALUE_TYPE_I64:
  1125. case VALUE_TYPE_F64:
  1126. bh_memcpy_s(global_data,
  1127. (uint32)(global_data_end - global_data),
  1128. &global->initial_value.i64, sizeof(int64));
  1129. global_data += sizeof(int64);
  1130. break;
  1131. default:
  1132. bh_assert(0);
  1133. }
  1134. }
  1135. bh_assert(global_data == global_data_end);
  1136. }
  1137. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1138. goto fail;
  1139. }
  1140. /* Initialize the memory data with data segment section */
  1141. module_inst->default_memory =
  1142. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1143. for (i = 0; i < module->data_seg_count; i++) {
  1144. WASMMemoryInstance *memory = NULL;
  1145. uint8 *memory_data = NULL;
  1146. uint32 memory_size = 0;
  1147. WASMDataSeg *data_seg = module->data_segments[i];
  1148. #if WASM_ENABLE_BULK_MEMORY != 0
  1149. if (data_seg->is_passive)
  1150. continue;
  1151. #endif
  1152. /* has check it in loader */
  1153. memory = module_inst->memories[data_seg->memory_index];
  1154. bh_assert(memory);
  1155. memory_data = memory->memory_data;
  1156. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1157. bh_assert(memory_data || memory_size == 0);
  1158. bh_assert(data_seg->base_offset.init_expr_type
  1159. == INIT_EXPR_TYPE_I32_CONST
  1160. || data_seg->base_offset.init_expr_type
  1161. == INIT_EXPR_TYPE_GET_GLOBAL);
  1162. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1163. if (!check_global_init_expr(module,
  1164. data_seg->base_offset.u.global_index,
  1165. error_buf, error_buf_size)) {
  1166. goto fail;
  1167. }
  1168. if (!globals
  1169. || globals[data_seg->base_offset.u.global_index].type
  1170. != VALUE_TYPE_I32) {
  1171. set_error_buf(error_buf, error_buf_size,
  1172. "data segment does not fit");
  1173. goto fail;
  1174. }
  1175. base_offset =
  1176. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  1177. }
  1178. else {
  1179. base_offset = (uint32)data_seg->base_offset.u.i32;
  1180. }
  1181. /* check offset */
  1182. if (base_offset > memory_size) {
  1183. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1184. memory_size);
  1185. #if WASM_ENABLE_REF_TYPES != 0
  1186. set_error_buf(error_buf, error_buf_size,
  1187. "out of bounds memory access");
  1188. #else
  1189. set_error_buf(error_buf, error_buf_size,
  1190. "data segment does not fit");
  1191. #endif
  1192. goto fail;
  1193. }
  1194. /* check offset + length(could be zero) */
  1195. length = data_seg->data_length;
  1196. if (base_offset + length > memory_size) {
  1197. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1198. base_offset, length, memory_size);
  1199. #if WASM_ENABLE_REF_TYPES != 0
  1200. set_error_buf(error_buf, error_buf_size,
  1201. "out of bounds memory access");
  1202. #else
  1203. set_error_buf(error_buf, error_buf_size,
  1204. "data segment does not fit");
  1205. #endif
  1206. goto fail;
  1207. }
  1208. if (memory_data) {
  1209. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1210. data_seg->data, length);
  1211. }
  1212. }
  1213. /* Initialize the table data with table segment section */
  1214. module_inst->default_table =
  1215. module_inst->table_count ? module_inst->tables[0] : NULL;
  1216. /* in case there is no table */
  1217. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1218. i++) {
  1219. WASMTableSeg *table_seg = module->table_segments + i;
  1220. /* has check it in loader */
  1221. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1222. bh_assert(table);
  1223. #if WASM_ENABLE_REF_TYPES != 0
  1224. if (table->elem_type != VALUE_TYPE_FUNCREF
  1225. && table->elem_type != VALUE_TYPE_EXTERNREF) {
  1226. set_error_buf(error_buf, error_buf_size,
  1227. "elements segment does not fit");
  1228. goto fail;
  1229. }
  1230. #endif
  1231. uint32 *table_data = (uint32 *)table->base_addr;
  1232. #if WASM_ENABLE_MULTI_MODULE != 0
  1233. table_data = table->table_inst_linked
  1234. ? (uint32 *)table->table_inst_linked->base_addr
  1235. : table_data;
  1236. #endif
  1237. bh_assert(table_data);
  1238. #if WASM_ENABLE_REF_TYPES != 0
  1239. if (!wasm_elem_is_active(table_seg->mode))
  1240. continue;
  1241. #endif
  1242. /* init vec(funcidx) or vec(expr) */
  1243. bh_assert(
  1244. table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  1245. || table_seg->base_offset.init_expr_type
  1246. == INIT_EXPR_TYPE_GET_GLOBAL
  1247. #if WASM_ENABLE_REF_TYPES != 0
  1248. || table_seg->base_offset.init_expr_type
  1249. == INIT_EXPR_TYPE_FUNCREF_CONST
  1250. || table_seg->base_offset.init_expr_type
  1251. == INIT_EXPR_TYPE_REFNULL_CONST
  1252. #endif
  1253. );
  1254. if (table_seg->base_offset.init_expr_type
  1255. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1256. if (!check_global_init_expr(module,
  1257. table_seg->base_offset.u.global_index,
  1258. error_buf, error_buf_size)) {
  1259. goto fail;
  1260. }
  1261. if (!globals
  1262. || globals[table_seg->base_offset.u.global_index].type
  1263. != VALUE_TYPE_I32) {
  1264. set_error_buf(error_buf, error_buf_size,
  1265. "elements segment does not fit");
  1266. goto fail;
  1267. }
  1268. table_seg->base_offset.u.i32 =
  1269. globals[table_seg->base_offset.u.global_index]
  1270. .initial_value.i32;
  1271. }
  1272. /* check offset since length might negative */
  1273. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1274. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1275. table_seg->base_offset.u.i32, table->cur_size);
  1276. #if WASM_ENABLE_REF_TYPES != 0
  1277. set_error_buf(error_buf, error_buf_size,
  1278. "out of bounds table access");
  1279. #else
  1280. set_error_buf(error_buf, error_buf_size,
  1281. "elements segment does not fit");
  1282. #endif
  1283. goto fail;
  1284. }
  1285. /* check offset + length(could be zero) */
  1286. length = table_seg->function_count;
  1287. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1288. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1289. table_seg->base_offset.u.i32, length, table->cur_size);
  1290. #if WASM_ENABLE_REF_TYPES != 0
  1291. set_error_buf(error_buf, error_buf_size,
  1292. "out of bounds table access");
  1293. #else
  1294. set_error_buf(error_buf, error_buf_size,
  1295. "elements segment does not fit");
  1296. #endif
  1297. goto fail;
  1298. }
  1299. /**
  1300. * Check function index in the current module inst for now.
  1301. * will check the linked table inst owner in future.
  1302. * so loader check is enough
  1303. */
  1304. bh_memcpy_s(
  1305. table_data + table_seg->base_offset.u.i32,
  1306. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1307. * sizeof(uint32)),
  1308. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1309. }
  1310. /* module instance type */
  1311. module_inst->module_type = Wasm_Module_Bytecode;
  1312. /* Initialize the thread related data */
  1313. if (stack_size == 0)
  1314. stack_size = DEFAULT_WASM_STACK_SIZE;
  1315. #if WASM_ENABLE_SPEC_TEST != 0
  1316. if (stack_size < 48 * 1024)
  1317. stack_size = 48 * 1024;
  1318. #endif
  1319. module_inst->default_wasm_stack_size = stack_size;
  1320. if (module->malloc_function != (uint32)-1) {
  1321. module_inst->malloc_function =
  1322. &module_inst->functions[module->malloc_function];
  1323. }
  1324. if (module->free_function != (uint32)-1) {
  1325. module_inst->free_function =
  1326. &module_inst->functions[module->free_function];
  1327. }
  1328. if (module->retain_function != (uint32)-1) {
  1329. module_inst->retain_function =
  1330. &module_inst->functions[module->retain_function];
  1331. }
  1332. #if WASM_ENABLE_LIBC_WASI != 0
  1333. /* The sub-instance will get the wasi_ctx from main-instance */
  1334. if (!is_sub_inst) {
  1335. if (!wasm_runtime_init_wasi(
  1336. (WASMModuleInstanceCommon *)module_inst,
  1337. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1338. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1339. module->wasi_args.env, module->wasi_args.env_count,
  1340. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  1341. module->wasi_args.argv, module->wasi_args.argc,
  1342. module->wasi_args.stdio[0], module->wasi_args.stdio[1],
  1343. module->wasi_args.stdio[2], error_buf, error_buf_size)) {
  1344. goto fail;
  1345. }
  1346. }
  1347. #endif
  1348. if (module->start_function != (uint32)-1) {
  1349. /* TODO: fix start function can be import function issue */
  1350. if (module->start_function >= module->import_function_count)
  1351. module_inst->start_function =
  1352. &module_inst->functions[module->start_function];
  1353. }
  1354. /* Execute __post_instantiate function */
  1355. if (!execute_post_inst_function(module_inst)
  1356. || !execute_start_function(module_inst)) {
  1357. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1358. goto fail;
  1359. }
  1360. #if WASM_ENABLE_BULK_MEMORY != 0
  1361. #if WASM_ENABLE_LIBC_WASI != 0
  1362. if (!module->import_wasi_api) {
  1363. #endif
  1364. /* Only execute the memory init function for main instance because
  1365. the data segments will be dropped once initialized.
  1366. */
  1367. if (!is_sub_inst) {
  1368. if (!execute_memory_init_function(module_inst)) {
  1369. set_error_buf(error_buf, error_buf_size,
  1370. module_inst->cur_exception);
  1371. goto fail;
  1372. }
  1373. }
  1374. #if WASM_ENABLE_LIBC_WASI != 0
  1375. }
  1376. #endif
  1377. #endif
  1378. #if WASM_ENABLE_MEMORY_TRACING != 0
  1379. wasm_runtime_dump_module_inst_mem_consumption(
  1380. (WASMModuleInstanceCommon *)module_inst);
  1381. #endif
  1382. (void)global_data_end;
  1383. return module_inst;
  1384. fail:
  1385. wasm_deinstantiate(module_inst, false);
  1386. return NULL;
  1387. }
  1388. void
  1389. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1390. {
  1391. if (!module_inst)
  1392. return;
  1393. #if WASM_ENABLE_MULTI_MODULE != 0
  1394. sub_module_deinstantiate(module_inst);
  1395. #endif
  1396. #if WASM_ENABLE_LIBC_WASI != 0
  1397. /* Destroy wasi resource before freeing app heap, since some fields of
  1398. wasi contex are allocated from app heap, and if app heap is freed,
  1399. these fields will be set to NULL, we cannot free their internal data
  1400. which may allocated from global heap. */
  1401. /* Only destroy wasi ctx in the main module instance */
  1402. if (!is_sub_inst)
  1403. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst);
  1404. #endif
  1405. if (module_inst->memory_count > 0)
  1406. memories_deinstantiate(module_inst, module_inst->memories,
  1407. module_inst->memory_count);
  1408. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1409. functions_deinstantiate(module_inst->functions,
  1410. module_inst->function_count);
  1411. globals_deinstantiate(module_inst->globals);
  1412. export_functions_deinstantiate(module_inst->export_functions);
  1413. #if WASM_ENABLE_MULTI_MODULE != 0
  1414. export_globals_deinstantiate(module_inst->export_globals);
  1415. #endif
  1416. if (module_inst->global_data)
  1417. wasm_runtime_free(module_inst->global_data);
  1418. #if WASM_ENABLE_REF_TYPES != 0
  1419. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  1420. #endif
  1421. if (module_inst->exec_env_singleton)
  1422. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1423. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1424. if (module_inst->frames) {
  1425. bh_vector_destroy(module_inst->frames);
  1426. wasm_runtime_free(module_inst->frames);
  1427. module_inst->frames = NULL;
  1428. }
  1429. #endif
  1430. #if WASM_ENABLE_DEBUG_INTERP != 0
  1431. if (!is_sub_inst) {
  1432. os_mutex_lock(&module_inst->module->ref_count_lock);
  1433. module_inst->module->ref_count--;
  1434. os_mutex_unlock(&module_inst->module->ref_count_lock);
  1435. }
  1436. #endif
  1437. wasm_runtime_free(module_inst);
  1438. }
  1439. WASMFunctionInstance *
  1440. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  1441. const char *signature)
  1442. {
  1443. uint32 i;
  1444. for (i = 0; i < module_inst->export_func_count; i++)
  1445. if (!strcmp(module_inst->export_functions[i].name, name))
  1446. return module_inst->export_functions[i].function;
  1447. (void)signature;
  1448. return NULL;
  1449. }
  1450. #if WASM_ENABLE_MULTI_MODULE != 0
  1451. WASMGlobalInstance *
  1452. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1453. {
  1454. uint32 i;
  1455. for (i = 0; i < module_inst->export_glob_count; i++)
  1456. if (!strcmp(module_inst->export_globals[i].name, name))
  1457. return module_inst->export_globals[i].global;
  1458. return NULL;
  1459. }
  1460. WASMMemoryInstance *
  1461. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1462. {
  1463. /**
  1464. * using a strong assumption that one module instance only has
  1465. * one memory instance
  1466. */
  1467. (void)module_inst->export_memories;
  1468. return module_inst->memories[0];
  1469. }
  1470. WASMTableInstance *
  1471. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1472. {
  1473. /**
  1474. * using a strong assumption that one module instance only has
  1475. * one table instance
  1476. */
  1477. (void)module_inst->export_tables;
  1478. return module_inst->tables[0];
  1479. }
  1480. #endif
  1481. static bool
  1482. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1483. {
  1484. #if WASM_ENABLE_LIBC_WASI != 0
  1485. const char *exception = wasm_get_exception(module_inst);
  1486. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1487. /* The "wasi proc exit" exception is thrown by native lib to
  1488. let wasm app exit, which is a normal behavior, we clear
  1489. the exception here. */
  1490. wasm_set_exception(module_inst, NULL);
  1491. return true;
  1492. }
  1493. return false;
  1494. #else
  1495. return false;
  1496. #endif
  1497. }
  1498. bool
  1499. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  1500. unsigned argc, uint32 argv[])
  1501. {
  1502. WASMModuleInstance *module_inst =
  1503. (WASMModuleInstance *)exec_env->module_inst;
  1504. /* set thread handle and stack boundary */
  1505. wasm_exec_env_set_thread_info(exec_env);
  1506. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1507. (void)clear_wasi_proc_exit_exception(module_inst);
  1508. return !wasm_get_exception(module_inst) ? true : false;
  1509. }
  1510. bool
  1511. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1512. WASMFunctionInstance *func,
  1513. unsigned argc, uint32 argv[],
  1514. bool enable_debug)
  1515. {
  1516. WASMExecEnv *exec_env;
  1517. bool ret;
  1518. #if WASM_ENABLE_THREAD_MGR != 0
  1519. WASMExecEnv *existing_exec_env = NULL;
  1520. if (!(existing_exec_env = exec_env = wasm_clusters_search_exec_env(
  1521. (WASMModuleInstanceCommon *)module_inst))) {
  1522. #endif
  1523. if (!(exec_env =
  1524. wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  1525. module_inst->default_wasm_stack_size))) {
  1526. wasm_set_exception(module_inst, "allocate memory failed");
  1527. return false;
  1528. }
  1529. #if WASM_ENABLE_THREAD_MGR != 0
  1530. if (enable_debug) {
  1531. #if WASM_ENABLE_DEBUG_INTERP != 0
  1532. wasm_runtime_start_debug_instance(exec_env);
  1533. #endif
  1534. }
  1535. }
  1536. #endif
  1537. ret = wasm_call_function(exec_env, func, argc, argv);
  1538. #if WASM_ENABLE_THREAD_MGR != 0
  1539. /* don't destroy the exec_env if it's searched from the cluster */
  1540. if (!existing_exec_env)
  1541. #endif
  1542. wasm_exec_env_destroy(exec_env);
  1543. return ret;
  1544. }
  1545. bool
  1546. wasm_create_exec_env_singleton(WASMModuleInstance *module_inst)
  1547. {
  1548. WASMExecEnv *exec_env = NULL;
  1549. if (module_inst->exec_env_singleton) {
  1550. return true;
  1551. }
  1552. exec_env = wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  1553. module_inst->default_wasm_stack_size);
  1554. if (exec_env)
  1555. module_inst->exec_env_singleton = exec_env;
  1556. return exec_env ? true : false;
  1557. }
  1558. void
  1559. wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
  1560. {
  1561. if (exception)
  1562. snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
  1563. "Exception: %s", exception);
  1564. else
  1565. module_inst->cur_exception[0] = '\0';
  1566. }
  1567. const char *
  1568. wasm_get_exception(WASMModuleInstance *module_inst)
  1569. {
  1570. if (module_inst->cur_exception[0] == '\0')
  1571. return NULL;
  1572. else
  1573. return module_inst->cur_exception;
  1574. }
  1575. #if WASM_ENABLE_PERF_PROFILING != 0
  1576. void
  1577. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  1578. {
  1579. WASMExportFuncInstance *export_func;
  1580. WASMFunctionInstance *func_inst;
  1581. char *func_name;
  1582. uint32 i, j;
  1583. os_printf("Performance profiler data:\n");
  1584. for (i = 0; i < module_inst->function_count; i++) {
  1585. func_inst = module_inst->functions + i;
  1586. if (func_inst->is_import_func) {
  1587. func_name = func_inst->u.func_import->field_name;
  1588. }
  1589. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1590. else if (func_inst->u.func->field_name) {
  1591. func_name = func_inst->u.func->field_name;
  1592. }
  1593. #endif
  1594. else {
  1595. func_name = NULL;
  1596. for (j = 0; j < module_inst->export_func_count; j++) {
  1597. export_func = module_inst->export_functions + j;
  1598. if (export_func->function == func_inst) {
  1599. func_name = export_func->name;
  1600. break;
  1601. }
  1602. }
  1603. }
  1604. if (func_name)
  1605. os_printf(" func %s, execution time: %.3f ms, execution count: %d "
  1606. "times\n",
  1607. func_name,
  1608. module_inst->functions[i].total_exec_time / 1000.0f,
  1609. module_inst->functions[i].total_exec_cnt);
  1610. else
  1611. os_printf(" func %d, execution time: %.3f ms, execution count: %d "
  1612. "times\n",
  1613. i, module_inst->functions[i].total_exec_time / 1000.0f,
  1614. module_inst->functions[i].total_exec_cnt);
  1615. }
  1616. }
  1617. #endif
  1618. uint32
  1619. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1620. void **p_native_addr)
  1621. {
  1622. WASMMemoryInstance *memory = module_inst->default_memory;
  1623. uint8 *addr = NULL;
  1624. uint32 offset = 0;
  1625. if (!memory) {
  1626. wasm_set_exception(module_inst, "uninitialized memory");
  1627. return 0;
  1628. }
  1629. if (memory->heap_handle) {
  1630. addr = mem_allocator_malloc(memory->heap_handle, size);
  1631. }
  1632. else if (module_inst->malloc_function && module_inst->free_function) {
  1633. #if WASM_ENABLE_DEBUG_INTERP != 0
  1634. /* TODO: obviously, we can not create debug instance for
  1635. * module malloc here, so, just disable the engine here,
  1636. * it is strange, but we now are lack of ways to indicate
  1637. * which calls should not be debugged. And we have other
  1638. * execute_xxx_function may need to be taken care of
  1639. */
  1640. bool active = wasm_debug_get_engine_active();
  1641. wasm_debug_set_engine_active(false);
  1642. #endif
  1643. if (!execute_malloc_function(module_inst, module_inst->malloc_function,
  1644. module_inst->retain_function, size,
  1645. &offset)) {
  1646. #if WASM_ENABLE_DEBUG_INTERP != 0
  1647. wasm_debug_set_engine_active(active);
  1648. #endif
  1649. return 0;
  1650. }
  1651. #if WASM_ENABLE_DEBUG_INTERP != 0
  1652. wasm_debug_set_engine_active(active);
  1653. #endif
  1654. /* If we use app's malloc function,
  1655. the default memory may be changed while memory growing */
  1656. memory = module_inst->default_memory;
  1657. addr = offset ? memory->memory_data + offset : NULL;
  1658. }
  1659. if (!addr) {
  1660. if (memory->heap_handle
  1661. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1662. wasm_runtime_show_app_heap_corrupted_prompt();
  1663. wasm_set_exception(module_inst, "app heap corrupted");
  1664. }
  1665. else {
  1666. LOG_WARNING("warning: allocate %u bytes memory failed", size);
  1667. }
  1668. return 0;
  1669. }
  1670. if (p_native_addr)
  1671. *p_native_addr = addr;
  1672. return (uint32)(addr - memory->memory_data);
  1673. }
  1674. uint32
  1675. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  1676. void **p_native_addr)
  1677. {
  1678. WASMMemoryInstance *memory = module_inst->default_memory;
  1679. uint8 *addr = NULL;
  1680. if (!memory) {
  1681. wasm_set_exception(module_inst, "uninitialized memory");
  1682. return 0;
  1683. }
  1684. if (memory->heap_handle) {
  1685. addr = mem_allocator_realloc(memory->heap_handle,
  1686. memory->memory_data + ptr, size);
  1687. }
  1688. /* Only support realloc in WAMR's app heap */
  1689. if (!addr) {
  1690. if (memory->heap_handle
  1691. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1692. wasm_set_exception(module_inst, "app heap corrupted");
  1693. }
  1694. else {
  1695. wasm_set_exception(module_inst, "out of memory");
  1696. }
  1697. return 0;
  1698. }
  1699. if (p_native_addr)
  1700. *p_native_addr = addr;
  1701. return (uint32)(addr - memory->memory_data);
  1702. }
  1703. void
  1704. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1705. {
  1706. if (ptr) {
  1707. WASMMemoryInstance *memory = module_inst->default_memory;
  1708. uint8 *addr;
  1709. if (!memory) {
  1710. return;
  1711. }
  1712. addr = memory->memory_data + ptr;
  1713. if (memory->heap_handle && memory->heap_data <= addr
  1714. && addr < memory->heap_data_end) {
  1715. mem_allocator_free(memory->heap_handle, addr);
  1716. }
  1717. else if (module_inst->malloc_function && module_inst->free_function
  1718. && memory->memory_data <= addr
  1719. && addr < memory->memory_data_end) {
  1720. #if WASM_ENABLE_DEBUG_INTERP != 0
  1721. /*TODO: obviously, we can not create debug instance for module
  1722. malloc here, so, just disable the engine here, it is strange. the
  1723. wasm's call should be marshed to its own thread */
  1724. bool active = wasm_debug_get_engine_active();
  1725. wasm_debug_set_engine_active(false);
  1726. #endif
  1727. execute_free_function(module_inst, module_inst->free_function, ptr);
  1728. #if WASM_ENABLE_DEBUG_INTERP != 0
  1729. wasm_debug_set_engine_active(active);
  1730. #endif
  1731. }
  1732. }
  1733. }
  1734. uint32
  1735. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  1736. uint32 size)
  1737. {
  1738. char *buffer;
  1739. uint32 buffer_offset =
  1740. wasm_module_malloc(module_inst, size, (void **)&buffer);
  1741. if (buffer_offset != 0) {
  1742. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1743. bh_memcpy_s(buffer, size, src, size);
  1744. }
  1745. return buffer_offset;
  1746. }
  1747. bool
  1748. wasm_validate_app_addr(WASMModuleInstance *module_inst, uint32 app_offset,
  1749. uint32 size)
  1750. {
  1751. WASMMemoryInstance *memory = module_inst->default_memory;
  1752. uint32 memory_data_size;
  1753. if (!memory) {
  1754. goto fail;
  1755. }
  1756. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1757. /* integer overflow check */
  1758. if (app_offset > UINT32_MAX - size) {
  1759. goto fail;
  1760. }
  1761. if (app_offset + size <= memory_data_size) {
  1762. return true;
  1763. }
  1764. fail:
  1765. wasm_set_exception(module_inst, "out of bounds memory access");
  1766. return false;
  1767. }
  1768. bool
  1769. wasm_validate_native_addr(WASMModuleInstance *module_inst, void *native_ptr,
  1770. uint32 size)
  1771. {
  1772. WASMMemoryInstance *memory = module_inst->default_memory;
  1773. uint8 *addr = (uint8 *)native_ptr;
  1774. if (!memory) {
  1775. goto fail;
  1776. }
  1777. /* integer overflow check */
  1778. if ((uintptr_t)addr > UINTPTR_MAX - size) {
  1779. goto fail;
  1780. }
  1781. if (memory->memory_data <= addr && addr + size <= memory->memory_data_end) {
  1782. return true;
  1783. }
  1784. fail:
  1785. wasm_set_exception(module_inst, "out of bounds memory access");
  1786. return false;
  1787. }
  1788. void *
  1789. wasm_addr_app_to_native(WASMModuleInstance *module_inst, uint32 app_offset)
  1790. {
  1791. WASMMemoryInstance *memory = module_inst->default_memory;
  1792. uint8 *addr;
  1793. if (!memory)
  1794. return NULL;
  1795. addr = memory->memory_data + app_offset;
  1796. if (memory->memory_data <= addr && addr < memory->memory_data_end)
  1797. return addr;
  1798. return NULL;
  1799. }
  1800. uint32
  1801. wasm_addr_native_to_app(WASMModuleInstance *module_inst, void *native_ptr)
  1802. {
  1803. WASMMemoryInstance *memory = module_inst->default_memory;
  1804. uint8 *addr = (uint8 *)native_ptr;
  1805. if (!memory)
  1806. return 0;
  1807. if (memory->memory_data <= addr && addr < memory->memory_data_end)
  1808. return (uint32)(addr - memory->memory_data);
  1809. return 0;
  1810. }
  1811. bool
  1812. wasm_get_app_addr_range(WASMModuleInstance *module_inst, uint32 app_offset,
  1813. uint32 *p_app_start_offset, uint32 *p_app_end_offset)
  1814. {
  1815. WASMMemoryInstance *memory = module_inst->default_memory;
  1816. uint32 memory_data_size;
  1817. if (!memory)
  1818. return false;
  1819. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1820. if (app_offset < memory_data_size) {
  1821. if (p_app_start_offset)
  1822. *p_app_start_offset = 0;
  1823. if (p_app_end_offset)
  1824. *p_app_end_offset = memory_data_size;
  1825. return true;
  1826. }
  1827. return false;
  1828. }
  1829. bool
  1830. wasm_get_native_addr_range(WASMModuleInstance *module_inst, uint8 *native_ptr,
  1831. uint8 **p_native_start_addr,
  1832. uint8 **p_native_end_addr)
  1833. {
  1834. WASMMemoryInstance *memory = module_inst->default_memory;
  1835. uint8 *addr = (uint8 *)native_ptr;
  1836. if (!memory)
  1837. return false;
  1838. if (memory->memory_data <= addr && addr < memory->memory_data_end) {
  1839. if (p_native_start_addr)
  1840. *p_native_start_addr = memory->memory_data;
  1841. if (p_native_end_addr)
  1842. *p_native_end_addr = memory->memory_data_end;
  1843. return true;
  1844. }
  1845. return false;
  1846. }
  1847. bool
  1848. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1849. {
  1850. WASMMemoryInstance *memory = module->default_memory;
  1851. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1852. uint32 heap_size, total_size_old, total_page_count;
  1853. uint64 total_size;
  1854. bool ret = true;
  1855. if (!memory)
  1856. return false;
  1857. memory_data = memory->memory_data;
  1858. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1859. total_size_old = (uint32)(memory->memory_data_end - memory_data);
  1860. total_page_count = inc_page_count + memory->cur_page_count;
  1861. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1862. heap_data_old = memory->heap_data;
  1863. if (inc_page_count <= 0)
  1864. /* No need to enlarge memory */
  1865. return true;
  1866. if (total_page_count < memory->cur_page_count /* integer overflow */
  1867. || total_page_count > memory->max_page_count) {
  1868. return false;
  1869. }
  1870. if (total_size >= UINT32_MAX) {
  1871. return false;
  1872. }
  1873. #if WASM_ENABLE_SHARED_MEMORY != 0
  1874. if (memory->is_shared) {
  1875. /* For shared memory, we have reserved the maximum spaces during
  1876. instantiate, only change the cur_page_count here */
  1877. memory->cur_page_count = total_page_count;
  1878. return true;
  1879. }
  1880. #endif
  1881. if (heap_size > 0) {
  1882. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1883. wasm_runtime_show_app_heap_corrupted_prompt();
  1884. return false;
  1885. }
  1886. }
  1887. if (!(new_memory_data =
  1888. wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1889. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1890. return false;
  1891. }
  1892. if (memory_data) {
  1893. bh_memcpy_s(new_memory_data, (uint32)total_size, memory_data,
  1894. total_size_old);
  1895. wasm_runtime_free(memory_data);
  1896. }
  1897. }
  1898. memset(new_memory_data + total_size_old, 0,
  1899. (uint32)total_size - total_size_old);
  1900. if (heap_size > 0) {
  1901. if (mem_allocator_migrate(memory->heap_handle,
  1902. (char *)heap_data_old
  1903. + (new_memory_data - memory_data),
  1904. heap_size)
  1905. != 0) {
  1906. /* Don't return here as memory->memory_data is obsolete and
  1907. must be updated to be correctly used later. */
  1908. ret = false;
  1909. }
  1910. }
  1911. memory->memory_data = new_memory_data;
  1912. memory->cur_page_count = total_page_count;
  1913. memory->heap_data = new_memory_data + (heap_data_old - memory_data);
  1914. memory->heap_data_end = memory->heap_data + heap_size;
  1915. memory->memory_data_end =
  1916. memory->memory_data + memory->num_bytes_per_page * total_page_count;
  1917. return ret;
  1918. }
  1919. #if WASM_ENABLE_REF_TYPES != 0
  1920. bool
  1921. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  1922. uint32 inc_entries, uint32 init_val)
  1923. {
  1924. uint32 entry_count, *new_table_data_start, i;
  1925. WASMTableInstance *table_inst;
  1926. if (!inc_entries) {
  1927. return true;
  1928. }
  1929. bh_assert(table_idx < module_inst->table_count);
  1930. table_inst = wasm_get_table_inst(module_inst, table_idx);
  1931. if (!table_inst) {
  1932. return false;
  1933. }
  1934. if (inc_entries > UINT32_MAX - table_inst->cur_size) {
  1935. return false;
  1936. }
  1937. entry_count = table_inst->cur_size + inc_entries;
  1938. if (entry_count > table_inst->max_size) {
  1939. return false;
  1940. }
  1941. /* fill in */
  1942. new_table_data_start =
  1943. (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr))
  1944. + table_inst->cur_size;
  1945. for (i = 0; i < inc_entries; ++i) {
  1946. new_table_data_start[i] = init_val;
  1947. }
  1948. table_inst->cur_size = entry_count;
  1949. return true;
  1950. }
  1951. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1952. bool
  1953. wasm_call_indirect(WASMExecEnv *exec_env, uint32_t tbl_idx,
  1954. uint32_t element_indices, uint32_t argc, uint32_t argv[])
  1955. {
  1956. WASMModuleInstance *module_inst = NULL;
  1957. WASMTableInstance *table_inst = NULL;
  1958. uint32_t function_indices = 0;
  1959. WASMFunctionInstance *function_inst = NULL;
  1960. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  1961. bh_assert(module_inst);
  1962. table_inst = module_inst->tables[tbl_idx];
  1963. if (!table_inst) {
  1964. wasm_set_exception(module_inst, "unknown table");
  1965. goto got_exception;
  1966. }
  1967. if (element_indices >= table_inst->cur_size) {
  1968. wasm_set_exception(module_inst, "undefined element");
  1969. goto got_exception;
  1970. }
  1971. /**
  1972. * please be aware that table_inst->base_addr may point
  1973. * to another module's table
  1974. **/
  1975. function_indices = ((uint32_t *)table_inst->base_addr)[element_indices];
  1976. if (function_indices == NULL_REF) {
  1977. wasm_set_exception(module_inst, "uninitialized element");
  1978. goto got_exception;
  1979. }
  1980. /**
  1981. * we insist to call functions owned by the module itself
  1982. **/
  1983. if (function_indices >= module_inst->function_count) {
  1984. wasm_set_exception(module_inst, "unknown function");
  1985. goto got_exception;
  1986. }
  1987. function_inst = module_inst->functions + function_indices;
  1988. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1989. (void)clear_wasi_proc_exit_exception(module_inst);
  1990. return !wasm_get_exception(module_inst) ? true : false;
  1991. got_exception:
  1992. return false;
  1993. }
  1994. #if WASM_ENABLE_THREAD_MGR != 0
  1995. bool
  1996. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  1997. {
  1998. WASMModuleInstance *module_inst =
  1999. (WASMModuleInstance *)exec_env->module_inst;
  2000. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2001. uint32 data_end = module_inst->module->aux_data_end;
  2002. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2003. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2004. /* Check the aux stack space, currently we don't allocate space in heap */
  2005. if ((is_stack_before_data && (size > start_offset))
  2006. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2007. return false;
  2008. if (stack_top_idx != (uint32)-1) {
  2009. /* The aux stack top is a wasm global,
  2010. set the initial value for the global */
  2011. uint8 *global_addr = module_inst->global_data
  2012. + module_inst->globals[stack_top_idx].data_offset;
  2013. *(int32 *)global_addr = start_offset;
  2014. /* The aux stack boundary is a constant value,
  2015. set the value to exec_env */
  2016. exec_env->aux_stack_boundary.boundary = start_offset - size;
  2017. exec_env->aux_stack_bottom.bottom = start_offset;
  2018. return true;
  2019. }
  2020. return false;
  2021. }
  2022. bool
  2023. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  2024. {
  2025. WASMModuleInstance *module_inst =
  2026. (WASMModuleInstance *)exec_env->module_inst;
  2027. /* The aux stack information is resolved in loader
  2028. and store in module */
  2029. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2030. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  2031. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2032. if (start_offset)
  2033. *start_offset = stack_bottom;
  2034. if (size)
  2035. *size = total_aux_stack_size;
  2036. return true;
  2037. }
  2038. return false;
  2039. }
  2040. #endif
  2041. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2042. void
  2043. wasm_get_module_mem_consumption(const WASMModule *module,
  2044. WASMModuleMemConsumption *mem_conspn)
  2045. {
  2046. uint32 i, size;
  2047. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2048. mem_conspn->module_struct_size = sizeof(WASMModule);
  2049. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2050. for (i = 0; i < module->type_count; i++) {
  2051. WASMType *type = module->types[i];
  2052. size = offsetof(WASMType, types)
  2053. + sizeof(uint8) * (type->param_count + type->result_count);
  2054. mem_conspn->types_size += size;
  2055. }
  2056. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2057. mem_conspn->functions_size =
  2058. sizeof(WASMFunction *) * module->function_count;
  2059. for (i = 0; i < module->function_count; i++) {
  2060. WASMFunction *func = module->functions[i];
  2061. WASMType *type = func->func_type;
  2062. size = sizeof(WASMFunction) + func->local_count
  2063. + sizeof(uint16) * (type->param_count + func->local_count);
  2064. #if WASM_ENABLE_FAST_INTERP != 0
  2065. size +=
  2066. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  2067. #endif
  2068. mem_conspn->functions_size += size;
  2069. }
  2070. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2071. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2072. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2073. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2074. mem_conspn->table_segs_size =
  2075. sizeof(WASMTableSeg) * module->table_seg_count;
  2076. for (i = 0; i < module->table_seg_count; i++) {
  2077. WASMTableSeg *table_seg = &module->table_segments[i];
  2078. mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count;
  2079. }
  2080. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  2081. for (i = 0; i < module->data_seg_count; i++) {
  2082. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2083. }
  2084. if (module->const_str_list) {
  2085. StringNode *node = module->const_str_list, *node_next;
  2086. while (node) {
  2087. node_next = node->next;
  2088. mem_conspn->const_strs_size +=
  2089. sizeof(StringNode) + strlen(node->str) + 1;
  2090. node = node_next;
  2091. }
  2092. }
  2093. mem_conspn->total_size += mem_conspn->module_struct_size;
  2094. mem_conspn->total_size += mem_conspn->types_size;
  2095. mem_conspn->total_size += mem_conspn->imports_size;
  2096. mem_conspn->total_size += mem_conspn->functions_size;
  2097. mem_conspn->total_size += mem_conspn->tables_size;
  2098. mem_conspn->total_size += mem_conspn->memories_size;
  2099. mem_conspn->total_size += mem_conspn->globals_size;
  2100. mem_conspn->total_size += mem_conspn->exports_size;
  2101. mem_conspn->total_size += mem_conspn->table_segs_size;
  2102. mem_conspn->total_size += mem_conspn->data_segs_size;
  2103. mem_conspn->total_size += mem_conspn->const_strs_size;
  2104. #if WASM_ENABLE_AOT != 0
  2105. mem_conspn->total_size += mem_conspn->aot_code_size;
  2106. #endif
  2107. }
  2108. void
  2109. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2110. WASMModuleInstMemConsumption *mem_conspn)
  2111. {
  2112. uint32 i, size;
  2113. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2114. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  2115. mem_conspn->memories_size =
  2116. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  2117. for (i = 0; i < module_inst->memory_count; i++) {
  2118. WASMMemoryInstance *memory = module_inst->memories[i];
  2119. size = sizeof(WASMMemoryInstance)
  2120. + memory->num_bytes_per_page * memory->cur_page_count;
  2121. mem_conspn->memories_size += size;
  2122. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  2123. /* size of app heap structure */
  2124. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  2125. }
  2126. mem_conspn->tables_size =
  2127. sizeof(WASMTableInstance *) * module_inst->table_count;
  2128. for (i = 0; i < module_inst->table_count; i++) {
  2129. WASMTableInstance *table = module_inst->tables[i];
  2130. #if WASM_ENABLE_MULTI_MODULE != 0
  2131. if (table->table_inst_linked) {
  2132. size = offsetof(WASMTableInstance, base_addr);
  2133. }
  2134. else
  2135. #endif
  2136. {
  2137. size = offsetof(WASMTableInstance, base_addr)
  2138. + sizeof(uint32) * table->cur_size;
  2139. }
  2140. mem_conspn->tables_size += size;
  2141. }
  2142. mem_conspn->functions_size =
  2143. sizeof(WASMFunctionInstance) * module_inst->function_count;
  2144. mem_conspn->globals_size =
  2145. sizeof(WASMGlobalInstance) * module_inst->global_count;
  2146. if (module_inst->global_count > 0) {
  2147. WASMGlobalInstance *global =
  2148. &module_inst->globals[module_inst->global_count - 1];
  2149. mem_conspn->globals_size +=
  2150. global->data_offset + wasm_value_type_size(global->type);
  2151. }
  2152. mem_conspn->exports_size =
  2153. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  2154. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2155. mem_conspn->total_size += mem_conspn->memories_size;
  2156. mem_conspn->total_size += mem_conspn->functions_size;
  2157. mem_conspn->total_size += mem_conspn->tables_size;
  2158. mem_conspn->total_size += mem_conspn->globals_size;
  2159. mem_conspn->total_size += mem_conspn->exports_size;
  2160. }
  2161. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  2162. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2163. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2164. void
  2165. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  2166. {
  2167. WASMModuleInstance *module_inst =
  2168. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2169. WASMInterpFrame *first_frame,
  2170. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2171. uint32 n = 0;
  2172. /* count frames includes a function */
  2173. first_frame = cur_frame;
  2174. while (cur_frame) {
  2175. if (cur_frame->function) {
  2176. n++;
  2177. }
  2178. cur_frame = cur_frame->prev_frame;
  2179. }
  2180. /* release previous stack frames and create new ones */
  2181. if (!bh_vector_destroy(module_inst->frames)
  2182. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame),
  2183. false)) {
  2184. return;
  2185. }
  2186. cur_frame = first_frame;
  2187. n = 0;
  2188. os_printf("\n");
  2189. while (cur_frame) {
  2190. WASMCApiFrame frame = { 0 };
  2191. WASMFunctionInstance *func_inst = cur_frame->function;
  2192. const char *func_name = NULL;
  2193. const uint8 *func_code_base = NULL;
  2194. if (!func_inst) {
  2195. cur_frame = cur_frame->prev_frame;
  2196. continue;
  2197. }
  2198. /* place holder, will overwrite it in wasm_c_api */
  2199. frame.instance = module_inst;
  2200. frame.module_offset = 0;
  2201. frame.func_index = (uint32)(func_inst - module_inst->functions);
  2202. func_code_base = wasm_get_func_code(func_inst);
  2203. if (!cur_frame->ip || !func_code_base) {
  2204. frame.func_offset = 0;
  2205. }
  2206. else {
  2207. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  2208. }
  2209. /* look for the function name */
  2210. if (func_inst->is_import_func) {
  2211. func_name = func_inst->u.func_import->field_name;
  2212. }
  2213. else {
  2214. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2215. func_name = func_inst->u.func->field_name;
  2216. #endif
  2217. /* if custom name section is not generated,
  2218. search symbols from export table */
  2219. if (!func_name) {
  2220. uint32 i;
  2221. for (i = 0; i < module_inst->export_func_count; i++) {
  2222. WASMExportFuncInstance *export_func =
  2223. module_inst->export_functions + i;
  2224. if (export_func->function == func_inst) {
  2225. func_name = export_func->name;
  2226. break;
  2227. }
  2228. }
  2229. }
  2230. }
  2231. /* function name not exported, print number instead */
  2232. if (func_name == NULL) {
  2233. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  2234. }
  2235. else {
  2236. os_printf("#%02d %s \n", n, func_name);
  2237. }
  2238. /* keep print */
  2239. bh_vector_append(module_inst->frames, &frame);
  2240. cur_frame = cur_frame->prev_frame;
  2241. n++;
  2242. }
  2243. os_printf("\n");
  2244. }
  2245. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */