wasm_runtime.c 101 KB

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