wasm_runtime.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521
  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. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../aot/aot_runtime.h"
  26. #endif
  27. static void
  28. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  29. {
  30. if (error_buf != NULL) {
  31. snprintf(error_buf, error_buf_size,
  32. "WASM module instantiate failed: %s", string);
  33. }
  34. }
  35. static void
  36. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  37. {
  38. va_list args;
  39. char buf[128];
  40. if (error_buf != NULL) {
  41. va_start(args, format);
  42. vsnprintf(buf, sizeof(buf), format, args);
  43. va_end(args);
  44. snprintf(error_buf, error_buf_size,
  45. "WASM module instantiate failed: %s", buf);
  46. }
  47. }
  48. WASMModule *
  49. wasm_load(uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  50. {
  51. return wasm_loader_load(buf, size,
  52. #if WASM_ENABLE_MULTI_MODULE != 0
  53. true,
  54. #endif
  55. error_buf, error_buf_size);
  56. }
  57. WASMModule *
  58. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  59. uint32 error_buf_size)
  60. {
  61. return wasm_loader_load_from_sections(section_list, error_buf,
  62. error_buf_size);
  63. }
  64. void
  65. wasm_unload(WASMModule *module)
  66. {
  67. wasm_loader_unload(module);
  68. }
  69. static void *
  70. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  71. {
  72. void *mem;
  73. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  74. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  75. return NULL;
  76. }
  77. memset(mem, 0, (uint32)size);
  78. return mem;
  79. }
  80. #if WASM_ENABLE_MULTI_MODULE != 0
  81. static WASMModuleInstance *
  82. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  83. const WASMModule *sub_module)
  84. {
  85. bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
  86. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  87. while (node && sub_module != node->module_inst->module) {
  88. node = bh_list_elem_next(node);
  89. }
  90. return node ? node->module_inst : NULL;
  91. }
  92. #endif
  93. /**
  94. * Destroy memory instances.
  95. */
  96. static void
  97. memories_deinstantiate(WASMModuleInstance *module_inst,
  98. WASMMemoryInstance **memories, uint32 count)
  99. {
  100. uint32 i;
  101. if (memories) {
  102. for (i = 0; i < count; i++) {
  103. if (memories[i]) {
  104. #if WASM_ENABLE_MULTI_MODULE != 0
  105. WASMModule *module = module_inst->module;
  106. if (i < module->import_memory_count
  107. && module->import_memories[i].u.memory.import_module) {
  108. continue;
  109. }
  110. #endif
  111. #if WASM_ENABLE_SHARED_MEMORY != 0
  112. if (memories[i]->is_shared) {
  113. int32 ref_count = shared_memory_dec_reference(
  114. (WASMModuleCommon *)module_inst->module);
  115. bh_assert(ref_count >= 0);
  116. /* if the reference count is not zero,
  117. don't free the memory */
  118. if (ref_count > 0)
  119. continue;
  120. }
  121. #endif
  122. if (memories[i]->heap_handle) {
  123. mem_allocator_destroy(memories[i]->heap_handle);
  124. wasm_runtime_free(memories[i]->heap_handle);
  125. memories[i]->heap_handle = NULL;
  126. }
  127. if (memories[i]->memory_data) {
  128. #ifndef OS_ENABLE_HW_BOUND_CHECK
  129. wasm_runtime_free(memories[i]->memory_data);
  130. #else
  131. #ifdef BH_PLATFORM_WINDOWS
  132. os_mem_decommit(memories[i]->memory_data,
  133. memories[i]->num_bytes_per_page
  134. * memories[i]->cur_page_count);
  135. #endif
  136. os_munmap((uint8 *)memories[i]->memory_data,
  137. 8 * (uint64)BH_GB);
  138. #endif
  139. }
  140. }
  141. }
  142. wasm_runtime_free(memories);
  143. }
  144. (void)module_inst;
  145. }
  146. static WASMMemoryInstance *
  147. memory_instantiate(WASMModuleInstance *module_inst, WASMMemoryInstance *memory,
  148. uint32 num_bytes_per_page, uint32 init_page_count,
  149. uint32 max_page_count, uint32 heap_size, uint32 flags,
  150. char *error_buf, uint32 error_buf_size)
  151. {
  152. WASMModule *module = module_inst->module;
  153. uint64 memory_data_size;
  154. uint32 heap_offset = num_bytes_per_page * init_page_count;
  155. uint32 inc_page_count, aux_heap_base, global_idx;
  156. uint32 bytes_of_last_page, bytes_to_page_end;
  157. uint8 *global_addr;
  158. #ifdef OS_ENABLE_HW_BOUND_CHECK
  159. uint8 *mapped_mem;
  160. uint64 map_size = 8 * (uint64)BH_GB;
  161. uint64 page_size = os_getpagesize();
  162. #endif
  163. #if WASM_ENABLE_SHARED_MEMORY != 0
  164. bool is_shared_memory = flags & 0x02 ? true : false;
  165. /* shared memory */
  166. if (is_shared_memory) {
  167. WASMSharedMemNode *node = wasm_module_get_shared_memory(
  168. (WASMModuleCommon *)module_inst->module);
  169. /* If the memory of this module has been instantiated,
  170. return the memory instance directly */
  171. if (node) {
  172. uint32 ref_count;
  173. ref_count = shared_memory_inc_reference(
  174. (WASMModuleCommon *)module_inst->module);
  175. bh_assert(ref_count > 0);
  176. memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node);
  177. bh_assert(memory);
  178. (void)ref_count;
  179. return memory;
  180. }
  181. }
  182. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  183. if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
  184. && module_inst->module->free_function != (uint32)-1) {
  185. /* Disable app heap, use malloc/free function exported
  186. by wasm app to allocate/free memory instead */
  187. heap_size = 0;
  188. }
  189. if (init_page_count == max_page_count && init_page_count == 1) {
  190. /* If only one page and at most one page, we just append
  191. the app heap to the end of linear memory, enlarge the
  192. num_bytes_per_page, and don't change the page count */
  193. heap_offset = num_bytes_per_page;
  194. num_bytes_per_page += heap_size;
  195. if (num_bytes_per_page < heap_size) {
  196. set_error_buf(error_buf, error_buf_size,
  197. "failed to insert app heap into linear memory, "
  198. "try using `--heap_size=0` option");
  199. return NULL;
  200. }
  201. }
  202. else if (heap_size > 0) {
  203. if (init_page_count == max_page_count && init_page_count == 0) {
  204. /* If the memory data size is always 0, we resize it to
  205. one page for app heap */
  206. num_bytes_per_page = heap_size;
  207. heap_offset = 0;
  208. inc_page_count = 1;
  209. }
  210. else if (module->aux_heap_base_global_index != (uint32)-1
  211. && module->aux_heap_base
  212. < num_bytes_per_page * init_page_count) {
  213. /* Insert app heap before __heap_base */
  214. aux_heap_base = module->aux_heap_base;
  215. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  216. if (bytes_of_last_page == 0)
  217. bytes_of_last_page = num_bytes_per_page;
  218. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  219. inc_page_count =
  220. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  221. / num_bytes_per_page;
  222. heap_offset = aux_heap_base;
  223. aux_heap_base += heap_size;
  224. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  225. if (bytes_of_last_page == 0)
  226. bytes_of_last_page = num_bytes_per_page;
  227. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  228. if (bytes_to_page_end < 1 * BH_KB) {
  229. aux_heap_base += 1 * BH_KB;
  230. inc_page_count++;
  231. }
  232. /* Adjust __heap_base global value */
  233. global_idx = module->aux_heap_base_global_index;
  234. bh_assert(module_inst->e->globals
  235. && global_idx < module_inst->e->global_count);
  236. global_addr = module_inst->global_data
  237. + module_inst->e->globals[global_idx].data_offset;
  238. *(uint32 *)global_addr = aux_heap_base;
  239. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  240. }
  241. else {
  242. /* Insert app heap before new page */
  243. inc_page_count =
  244. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  245. heap_offset = num_bytes_per_page * init_page_count;
  246. heap_size = num_bytes_per_page * inc_page_count;
  247. if (heap_size > 0)
  248. heap_size -= 1 * BH_KB;
  249. }
  250. init_page_count += inc_page_count;
  251. max_page_count += inc_page_count;
  252. if (init_page_count > DEFAULT_MAX_PAGES) {
  253. set_error_buf(error_buf, error_buf_size,
  254. "failed to insert app heap into linear memory, "
  255. "try using `--heap_size=0` option");
  256. return NULL;
  257. }
  258. else if (init_page_count == DEFAULT_MAX_PAGES) {
  259. num_bytes_per_page = UINT32_MAX;
  260. init_page_count = max_page_count = 1;
  261. }
  262. if (max_page_count > DEFAULT_MAX_PAGES)
  263. max_page_count = DEFAULT_MAX_PAGES;
  264. }
  265. LOG_VERBOSE("Memory instantiate:");
  266. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  267. num_bytes_per_page, init_page_count, max_page_count);
  268. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  269. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  270. #if WASM_ENABLE_SHARED_MEMORY != 0
  271. if (is_shared_memory) {
  272. /* Allocate max page for shared memory */
  273. memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  274. }
  275. #endif
  276. bh_assert(memory_data_size <= 4 * (uint64)BH_GB);
  277. bh_assert(memory != NULL);
  278. #ifndef OS_ENABLE_HW_BOUND_CHECK
  279. if (memory_data_size > 0
  280. && !(memory->memory_data =
  281. runtime_malloc(memory_data_size, error_buf, error_buf_size))) {
  282. goto fail1;
  283. }
  284. #else
  285. memory_data_size = (memory_data_size + page_size - 1) & ~(page_size - 1);
  286. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  287. * ea = i + memarg.offset
  288. * both i and memarg.offset are u32 in range 0 to 4G
  289. * so the range of ea is 0 to 8G
  290. */
  291. if (!(memory->memory_data = mapped_mem =
  292. os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) {
  293. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  294. goto fail1;
  295. }
  296. #ifdef BH_PLATFORM_WINDOWS
  297. if (!os_mem_commit(mapped_mem, memory_data_size,
  298. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  299. set_error_buf(error_buf, error_buf_size, "commit memory failed");
  300. os_munmap(mapped_mem, map_size);
  301. goto fail1;
  302. }
  303. #endif
  304. if (os_mprotect(mapped_mem, memory_data_size,
  305. MMAP_PROT_READ | MMAP_PROT_WRITE)
  306. != 0) {
  307. set_error_buf(error_buf, error_buf_size, "mprotect memory failed");
  308. goto fail2;
  309. }
  310. /* Newly allocated pages are filled with zero by the OS, we don't fill it
  311. * again here */
  312. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  313. if (memory_data_size > UINT32_MAX)
  314. memory_data_size = (uint32)memory_data_size;
  315. memory->module_type = Wasm_Module_Bytecode;
  316. memory->num_bytes_per_page = num_bytes_per_page;
  317. memory->cur_page_count = init_page_count;
  318. memory->max_page_count = max_page_count;
  319. memory->memory_data_size = (uint32)memory_data_size;
  320. memory->heap_data = memory->memory_data + heap_offset;
  321. memory->heap_data_end = memory->heap_data + heap_size;
  322. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  323. /* Initialize heap */
  324. if (heap_size > 0) {
  325. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  326. if (!(memory->heap_handle = runtime_malloc(
  327. (uint64)heap_struct_size, error_buf, error_buf_size))) {
  328. goto fail2;
  329. }
  330. if (!mem_allocator_create_with_struct_and_pool(
  331. memory->heap_handle, heap_struct_size, memory->heap_data,
  332. heap_size)) {
  333. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  334. goto fail3;
  335. }
  336. }
  337. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  338. if (memory_data_size > 0) {
  339. #if UINTPTR_MAX == UINT64_MAX
  340. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  341. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  342. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  343. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  344. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  345. #else
  346. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  347. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  348. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  349. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  350. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  351. #endif
  352. }
  353. #endif
  354. #if WASM_ENABLE_SHARED_MEMORY != 0
  355. if (is_shared_memory) {
  356. memory->is_shared = true;
  357. if (!shared_memory_set_memory_inst(
  358. (WASMModuleCommon *)module_inst->module,
  359. (WASMMemoryInstanceCommon *)memory)) {
  360. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  361. goto fail4;
  362. }
  363. }
  364. #endif
  365. LOG_VERBOSE("Memory instantiate success.");
  366. return memory;
  367. #if WASM_ENABLE_SHARED_MEMORY != 0
  368. fail4:
  369. if (heap_size > 0)
  370. mem_allocator_destroy(memory->heap_handle);
  371. #endif
  372. fail3:
  373. if (heap_size > 0)
  374. wasm_runtime_free(memory->heap_handle);
  375. fail2:
  376. #ifndef OS_ENABLE_HW_BOUND_CHECK
  377. if (memory->memory_data)
  378. wasm_runtime_free(memory->memory_data);
  379. #else
  380. #ifdef BH_PLATFORM_WINDOWS
  381. os_mem_decommit(mapped_mem, memory_data_size);
  382. #endif
  383. os_munmap(mapped_mem, map_size);
  384. #endif
  385. fail1:
  386. return NULL;
  387. }
  388. /**
  389. * Instantiate memories in a module.
  390. */
  391. static WASMMemoryInstance **
  392. memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  393. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  394. {
  395. WASMImport *import;
  396. uint32 mem_index = 0, i,
  397. memory_count = module->import_memory_count + module->memory_count;
  398. uint64 total_size;
  399. WASMMemoryInstance **memories, *memory;
  400. total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
  401. if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
  402. return NULL;
  403. }
  404. memory = module_inst->global_table_data.memory_instances;
  405. /* instantiate memories from import section */
  406. import = module->import_memories;
  407. for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
  408. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  409. uint32 init_page_count = import->u.memory.init_page_count;
  410. uint32 max_page_count = import->u.memory.max_page_count;
  411. uint32 flags = import->u.memory.flags;
  412. uint32 actual_heap_size = heap_size;
  413. #if WASM_ENABLE_MULTI_MODULE != 0
  414. if (import->u.memory.import_module != NULL) {
  415. WASMModuleInstance *module_inst_linked;
  416. if (!(module_inst_linked = get_sub_module_inst(
  417. module_inst, import->u.memory.import_module))) {
  418. set_error_buf(error_buf, error_buf_size, "unknown memory");
  419. memories_deinstantiate(module_inst, memories, memory_count);
  420. return NULL;
  421. }
  422. if (!(memories[mem_index++] = wasm_lookup_memory(
  423. module_inst_linked, import->u.memory.field_name))) {
  424. set_error_buf(error_buf, error_buf_size, "unknown memory");
  425. memories_deinstantiate(module_inst, memories, memory_count);
  426. return NULL;
  427. }
  428. }
  429. else
  430. #endif
  431. {
  432. if (!(memories[mem_index++] = memory_instantiate(
  433. module_inst, memory, num_bytes_per_page, init_page_count,
  434. max_page_count, actual_heap_size, flags, error_buf,
  435. error_buf_size))) {
  436. memories_deinstantiate(module_inst, memories, memory_count);
  437. return NULL;
  438. }
  439. }
  440. }
  441. /* instantiate memories from memory section */
  442. for (i = 0; i < module->memory_count; i++, memory++) {
  443. if (!(memories[mem_index++] = memory_instantiate(
  444. module_inst, memory, module->memories[i].num_bytes_per_page,
  445. module->memories[i].init_page_count,
  446. module->memories[i].max_page_count, heap_size,
  447. module->memories[i].flags, error_buf, error_buf_size))) {
  448. memories_deinstantiate(module_inst, memories, memory_count);
  449. return NULL;
  450. }
  451. }
  452. bh_assert(mem_index == memory_count);
  453. (void)module_inst;
  454. return memories;
  455. }
  456. /**
  457. * Destroy table instances.
  458. */
  459. static void
  460. tables_deinstantiate(WASMModuleInstance *module_inst)
  461. {
  462. if (module_inst->tables) {
  463. wasm_runtime_free(module_inst->tables);
  464. }
  465. #if WASM_ENABLE_MULTI_MODULE != 0
  466. if (module_inst->e->table_insts_linked) {
  467. wasm_runtime_free(module_inst->e->table_insts_linked);
  468. }
  469. #endif
  470. }
  471. /**
  472. * Instantiate tables in a module.
  473. */
  474. static WASMTableInstance **
  475. tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  476. WASMTableInstance *first_table, char *error_buf,
  477. uint32 error_buf_size)
  478. {
  479. WASMImport *import;
  480. uint32 table_index = 0, i;
  481. uint32 table_count = module->import_table_count + module->table_count;
  482. uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
  483. WASMTableInstance **tables, *table = first_table;
  484. #if WASM_ENABLE_MULTI_MODULE != 0
  485. uint64 total_size_of_tables_linked =
  486. (uint64)sizeof(WASMTableInstance *) * module->import_table_count;
  487. WASMTableInstance **table_linked = NULL;
  488. #endif
  489. if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
  490. return NULL;
  491. }
  492. #if WASM_ENABLE_MULTI_MODULE != 0
  493. if (module->import_table_count > 0
  494. && !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
  495. total_size_of_tables_linked, error_buf, error_buf_size))) {
  496. goto fail;
  497. }
  498. #endif
  499. /* instantiate tables from import section */
  500. import = module->import_tables;
  501. for (i = 0; i < module->import_table_count; i++, import++) {
  502. uint32 max_size_fixed = 0;
  503. #if WASM_ENABLE_MULTI_MODULE != 0
  504. WASMTableInstance *table_inst_linked = NULL;
  505. WASMModuleInstance *module_inst_linked = NULL;
  506. if (import->u.table.import_module) {
  507. if (!(module_inst_linked = get_sub_module_inst(
  508. module_inst, import->u.table.import_module))) {
  509. set_error_buf(error_buf, error_buf_size, "unknown table");
  510. goto fail;
  511. }
  512. if (!(table_inst_linked = wasm_lookup_table(
  513. module_inst_linked, import->u.table.field_name))) {
  514. set_error_buf(error_buf, error_buf_size, "unknown table");
  515. goto fail;
  516. }
  517. total_size = offsetof(WASMTableInstance, elems);
  518. }
  519. else
  520. #endif
  521. {
  522. /* in order to save memory, alloc resource as few as possible */
  523. max_size_fixed = import->u.table.possible_grow
  524. ? import->u.table.max_size
  525. : import->u.table.init_size;
  526. /* it is a built-in table, every module has its own */
  527. total_size = offsetof(WASMTableInstance, elems);
  528. total_size += (uint64)max_size_fixed * sizeof(uint32);
  529. }
  530. tables[table_index++] = table;
  531. /* Set all elements to -1 to mark them as uninitialized elements */
  532. memset(table, -1, (uint32)total_size);
  533. #if WASM_ENABLE_MULTI_MODULE != 0
  534. *table_linked = table_inst_linked;
  535. if (table_inst_linked != NULL) {
  536. table->cur_size = table_inst_linked->cur_size;
  537. table->max_size = table_inst_linked->max_size;
  538. }
  539. else
  540. #endif
  541. {
  542. table->cur_size = import->u.table.init_size;
  543. table->max_size = max_size_fixed;
  544. }
  545. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  546. #if WASM_ENABLE_MULTI_MODULE != 0
  547. table_linked++;
  548. #endif
  549. }
  550. /* instantiate tables from table section */
  551. for (i = 0; i < module->table_count; i++) {
  552. uint32 max_size_fixed = 0;
  553. total_size = offsetof(WASMTableInstance, elems);
  554. #if WASM_ENABLE_MULTI_MODULE != 0
  555. /* in case, a module which imports this table will grow it */
  556. max_size_fixed = module->tables[i].max_size;
  557. #else
  558. max_size_fixed = module->tables[i].possible_grow
  559. ? module->tables[i].max_size
  560. : module->tables[i].init_size;
  561. #endif
  562. total_size += sizeof(uint32) * (uint64)max_size_fixed;
  563. tables[table_index++] = table;
  564. /* Set all elements to -1 to mark them as uninitialized elements */
  565. memset(table, -1, (uint32)total_size);
  566. table->cur_size = module->tables[i].init_size;
  567. table->max_size = max_size_fixed;
  568. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  569. }
  570. bh_assert(table_index == table_count);
  571. (void)module_inst;
  572. return tables;
  573. #if WASM_ENABLE_MULTI_MODULE != 0
  574. fail:
  575. wasm_runtime_free(tables);
  576. return NULL;
  577. #endif
  578. }
  579. /**
  580. * Destroy function instances.
  581. */
  582. static void
  583. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  584. {
  585. if (functions) {
  586. wasm_runtime_free(functions);
  587. }
  588. }
  589. /**
  590. * Instantiate functions in a module.
  591. */
  592. static WASMFunctionInstance *
  593. functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  594. char *error_buf, uint32 error_buf_size)
  595. {
  596. WASMImport *import;
  597. uint32 i,
  598. function_count = module->import_function_count + module->function_count;
  599. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  600. WASMFunctionInstance *functions, *function;
  601. if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
  602. return NULL;
  603. }
  604. total_size = sizeof(void *) * (uint64)module->import_function_count;
  605. if (total_size > 0
  606. && !(module_inst->import_func_ptrs =
  607. runtime_malloc(total_size, error_buf, error_buf_size))) {
  608. wasm_runtime_free(functions);
  609. return NULL;
  610. }
  611. /* instantiate functions from import section */
  612. function = functions;
  613. import = module->import_functions;
  614. for (i = 0; i < module->import_function_count; i++, import++) {
  615. function->is_import_func = true;
  616. #if WASM_ENABLE_MULTI_MODULE != 0
  617. if (import->u.function.import_module) {
  618. function->import_module_inst = get_sub_module_inst(
  619. module_inst, import->u.function.import_module);
  620. if (function->import_module_inst) {
  621. function->import_func_inst =
  622. wasm_lookup_function(function->import_module_inst,
  623. import->u.function.field_name, NULL);
  624. }
  625. }
  626. #endif /* WASM_ENABLE_MULTI_MODULE */
  627. function->u.func_import = &import->u.function;
  628. function->param_cell_num = import->u.function.func_type->param_cell_num;
  629. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  630. function->param_count =
  631. (uint16)function->u.func_import->func_type->param_count;
  632. function->param_types = function->u.func_import->func_type->types;
  633. function->local_cell_num = 0;
  634. function->local_count = 0;
  635. function->local_types = NULL;
  636. /* Copy the function pointer to current instance */
  637. module_inst->import_func_ptrs[i] =
  638. function->u.func_import->func_ptr_linked;
  639. function++;
  640. }
  641. /* instantiate functions from function section */
  642. for (i = 0; i < module->function_count; i++) {
  643. function->is_import_func = false;
  644. function->u.func = module->functions[i];
  645. function->param_cell_num = function->u.func->param_cell_num;
  646. function->ret_cell_num = function->u.func->ret_cell_num;
  647. function->local_cell_num = function->u.func->local_cell_num;
  648. function->param_count =
  649. (uint16)function->u.func->func_type->param_count;
  650. function->local_count = (uint16)function->u.func->local_count;
  651. function->param_types = function->u.func->func_type->types;
  652. function->local_types = function->u.func->local_types;
  653. function->local_offsets = function->u.func->local_offsets;
  654. #if WASM_ENABLE_FAST_INTERP != 0
  655. function->const_cell_num = function->u.func->const_cell_num;
  656. #endif
  657. function++;
  658. }
  659. bh_assert((uint32)(function - functions) == function_count);
  660. #if WASM_ENABLE_FAST_JIT != 0
  661. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  662. #endif
  663. return functions;
  664. }
  665. /**
  666. * Destroy global instances.
  667. */
  668. static void
  669. globals_deinstantiate(WASMGlobalInstance *globals)
  670. {
  671. if (globals)
  672. wasm_runtime_free(globals);
  673. }
  674. static bool
  675. check_global_init_expr(const WASMModule *module, uint32 global_index,
  676. char *error_buf, uint32 error_buf_size)
  677. {
  678. if (global_index >= module->import_global_count + module->global_count) {
  679. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  680. global_index);
  681. return false;
  682. }
  683. /**
  684. * Currently, constant expressions occurring as initializers of
  685. * globals are further constrained in that contained global.get
  686. * instructions are only allowed to refer to imported globals.
  687. *
  688. * And initializer expression cannot reference a mutable global.
  689. */
  690. if (global_index >= module->import_global_count
  691. || (module->import_globals + global_index)->u.global.is_mutable) {
  692. set_error_buf(error_buf, error_buf_size,
  693. "constant expression required");
  694. return false;
  695. }
  696. return true;
  697. }
  698. /**
  699. * Instantiate globals in a module.
  700. */
  701. static WASMGlobalInstance *
  702. globals_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  703. char *error_buf, uint32 error_buf_size)
  704. {
  705. WASMImport *import;
  706. uint32 global_data_offset = 0;
  707. uint32 i, global_count = module->import_global_count + module->global_count;
  708. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  709. WASMGlobalInstance *globals, *global;
  710. if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
  711. return NULL;
  712. }
  713. /* instantiate globals from import section */
  714. global = globals;
  715. import = module->import_globals;
  716. for (i = 0; i < module->import_global_count; i++, import++) {
  717. WASMGlobalImport *global_import = &import->u.global;
  718. global->type = global_import->type;
  719. global->is_mutable = global_import->is_mutable;
  720. #if WASM_ENABLE_MULTI_MODULE != 0
  721. if (global_import->import_module) {
  722. if (!(global->import_module_inst = get_sub_module_inst(
  723. module_inst, global_import->import_module))) {
  724. set_error_buf(error_buf, error_buf_size, "unknown global");
  725. goto fail;
  726. }
  727. if (!(global->import_global_inst = wasm_lookup_global(
  728. global->import_module_inst, global_import->field_name))) {
  729. set_error_buf(error_buf, error_buf_size, "unknown global");
  730. goto fail;
  731. }
  732. /* The linked global instance has been initialized, we
  733. just need to copy the value. */
  734. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  735. &(global_import->import_global_linked->init_expr),
  736. sizeof(WASMValue));
  737. }
  738. else
  739. #endif
  740. {
  741. /* native globals share their initial_values in one module */
  742. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  743. &(global_import->global_data_linked),
  744. sizeof(WASMValue));
  745. }
  746. #if WASM_ENABLE_FAST_JIT != 0
  747. bh_assert(global_data_offset == global_import->data_offset);
  748. #endif
  749. global->data_offset = global_data_offset;
  750. global_data_offset += wasm_value_type_size(global->type);
  751. global++;
  752. }
  753. /* instantiate globals from global section */
  754. for (i = 0; i < module->global_count; i++) {
  755. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  756. global->type = module->globals[i].type;
  757. global->is_mutable = module->globals[i].is_mutable;
  758. #if WASM_ENABLE_FAST_JIT != 0
  759. bh_assert(global_data_offset == module->globals[i].data_offset);
  760. #endif
  761. global->data_offset = global_data_offset;
  762. global_data_offset += wasm_value_type_size(global->type);
  763. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  764. if (!check_global_init_expr(module, init_expr->u.global_index,
  765. error_buf, error_buf_size)) {
  766. goto fail;
  767. }
  768. bh_memcpy_s(
  769. &(global->initial_value), sizeof(WASMValue),
  770. &(globals[init_expr->u.global_index].initial_value),
  771. sizeof(globals[init_expr->u.global_index].initial_value));
  772. }
  773. #if WASM_ENABLE_REF_TYPES != 0
  774. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  775. global->initial_value.u32 = (uint32)NULL_REF;
  776. }
  777. #endif
  778. else {
  779. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  780. &(init_expr->u), sizeof(init_expr->u));
  781. }
  782. global++;
  783. }
  784. bh_assert((uint32)(global - globals) == global_count);
  785. bh_assert(global_data_offset == module->global_data_size);
  786. (void)module_inst;
  787. return globals;
  788. fail:
  789. wasm_runtime_free(globals);
  790. return NULL;
  791. }
  792. /**
  793. * Return export function count in module export section.
  794. */
  795. static uint32
  796. get_export_count(const WASMModule *module, uint8 kind)
  797. {
  798. WASMExport *export = module->exports;
  799. uint32 count = 0, i;
  800. for (i = 0; i < module->export_count; i++, export ++)
  801. if (export->kind == kind)
  802. count++;
  803. return count;
  804. }
  805. /**
  806. * Destroy export function instances.
  807. */
  808. static void
  809. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  810. {
  811. if (functions)
  812. wasm_runtime_free(functions);
  813. }
  814. /**
  815. * Instantiate export functions in a module.
  816. */
  817. static WASMExportFuncInstance *
  818. export_functions_instantiate(const WASMModule *module,
  819. WASMModuleInstance *module_inst,
  820. uint32 export_func_count, char *error_buf,
  821. uint32 error_buf_size)
  822. {
  823. WASMExportFuncInstance *export_funcs, *export_func;
  824. WASMExport *export = module->exports;
  825. uint32 i;
  826. uint64 total_size =
  827. sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  828. if (!(export_func = export_funcs =
  829. runtime_malloc(total_size, error_buf, error_buf_size))) {
  830. return NULL;
  831. }
  832. for (i = 0; i < module->export_count; i++, export ++)
  833. if (export->kind == EXPORT_KIND_FUNC) {
  834. export_func->name = export->name;
  835. export_func->function = &module_inst->e->functions[export->index];
  836. export_func++;
  837. }
  838. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  839. return export_funcs;
  840. }
  841. #if WASM_ENABLE_MULTI_MODULE != 0
  842. static void
  843. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  844. {
  845. if (globals)
  846. wasm_runtime_free(globals);
  847. }
  848. static WASMExportGlobInstance *
  849. export_globals_instantiate(const WASMModule *module,
  850. WASMModuleInstance *module_inst,
  851. uint32 export_glob_count, char *error_buf,
  852. uint32 error_buf_size)
  853. {
  854. WASMExportGlobInstance *export_globals, *export_global;
  855. WASMExport *export = module->exports;
  856. uint32 i;
  857. uint64 total_size =
  858. sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  859. if (!(export_global = export_globals =
  860. runtime_malloc(total_size, error_buf, error_buf_size))) {
  861. return NULL;
  862. }
  863. for (i = 0; i < module->export_count; i++, export ++)
  864. if (export->kind == EXPORT_KIND_GLOBAL) {
  865. export_global->name = export->name;
  866. export_global->global = &module_inst->e->globals[export->index];
  867. export_global++;
  868. }
  869. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  870. return export_globals;
  871. }
  872. #endif
  873. static WASMFunctionInstance *
  874. lookup_post_instantiate_func(WASMModuleInstance *module_inst,
  875. const char *func_name)
  876. {
  877. WASMFunctionInstance *func;
  878. WASMType *func_type;
  879. if (!(func = wasm_lookup_function(module_inst, func_name, NULL)))
  880. /* Not found */
  881. return NULL;
  882. func_type = func->u.func->func_type;
  883. if (!(func_type->param_count == 0 && func_type->result_count == 0))
  884. /* Not a valid function type, ignore it */
  885. return NULL;
  886. return func;
  887. }
  888. static bool
  889. execute_post_instantiate_functions(WASMModuleInstance *module_inst,
  890. bool is_sub_inst, WASMExecEnv *exec_env_main)
  891. {
  892. WASMFunctionInstance *start_func = module_inst->e->start_function;
  893. WASMFunctionInstance *initialize_func = NULL;
  894. WASMFunctionInstance *post_inst_func = NULL;
  895. WASMFunctionInstance *call_ctors_func = NULL;
  896. #if WASM_ENABLE_LIBC_WASI != 0
  897. WASMModule *module = module_inst->module;
  898. #endif
  899. WASMModuleInstanceCommon *module_inst_main = NULL;
  900. #ifdef OS_ENABLE_HW_BOUND_CHECK
  901. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  902. #endif
  903. WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
  904. bool ret = false;
  905. #if WASM_ENABLE_LIBC_WASI != 0
  906. /*
  907. * WASI reactor instances may assume that _initialize will be called by
  908. * the environment at most once, and that none of their other exports
  909. * are accessed before that call.
  910. */
  911. if (!is_sub_inst && module->import_wasi_api) {
  912. initialize_func =
  913. lookup_post_instantiate_func(module_inst, "_initialize");
  914. }
  915. #endif
  916. /* Execute possible "__post_instantiate" function if wasm app is
  917. compiled by emsdk's early version */
  918. if (!is_sub_inst) {
  919. post_inst_func =
  920. lookup_post_instantiate_func(module_inst, "__post_instantiate");
  921. }
  922. #if WASM_ENABLE_BULK_MEMORY != 0
  923. /* Only execute the memory init function for main instance since
  924. the data segments will be dropped once initialized */
  925. if (!is_sub_inst
  926. #if WASM_ENABLE_LIBC_WASI != 0
  927. && !module->import_wasi_api
  928. #endif
  929. ) {
  930. call_ctors_func =
  931. lookup_post_instantiate_func(module_inst, "__wasm_call_ctors");
  932. }
  933. #endif
  934. if (!start_func && !initialize_func && !post_inst_func
  935. && !call_ctors_func) {
  936. /* No post instantiation functions to call */
  937. return true;
  938. }
  939. if (is_sub_inst) {
  940. bh_assert(exec_env_main);
  941. #ifdef OS_ENABLE_HW_BOUND_CHECK
  942. bh_assert(exec_env_tls == exec_env_main);
  943. (void)exec_env_tls;
  944. #endif
  945. exec_env = exec_env_main;
  946. /* Temporarily replace parent exec_env's module inst to current
  947. module inst to avoid checking failure when calling the
  948. wasm functions, and ensure that the exec_env's module inst
  949. is the correct one. */
  950. module_inst_main = exec_env_main->module_inst;
  951. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  952. }
  953. else {
  954. /* Try using the existing exec_env */
  955. #ifdef OS_ENABLE_HW_BOUND_CHECK
  956. exec_env = exec_env_tls;
  957. #endif
  958. #if WASM_ENABLE_THREAD_MGR != 0
  959. if (!exec_env)
  960. exec_env = wasm_clusters_search_exec_env(
  961. (WASMModuleInstanceCommon *)module_inst);
  962. #endif
  963. if (!exec_env) {
  964. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  965. (WASMModuleInstanceCommon *)module_inst,
  966. module_inst->default_wasm_stack_size))) {
  967. wasm_set_exception(module_inst, "allocate memory failed");
  968. return false;
  969. }
  970. }
  971. else {
  972. /* Temporarily replace exec_env's module inst with current
  973. module inst to ensure that the exec_env's module inst
  974. is the correct one. */
  975. module_inst_main = exec_env->module_inst;
  976. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  977. }
  978. }
  979. /* Execute start function for both main insance and sub instance */
  980. if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
  981. goto fail;
  982. }
  983. if (initialize_func
  984. && !wasm_call_function(exec_env, initialize_func, 0, NULL)) {
  985. goto fail;
  986. }
  987. if (post_inst_func
  988. && !wasm_call_function(exec_env, post_inst_func, 0, NULL)) {
  989. goto fail;
  990. }
  991. if (call_ctors_func
  992. && !wasm_call_function(exec_env, call_ctors_func, 0, NULL)) {
  993. goto fail;
  994. }
  995. ret = true;
  996. fail:
  997. if (is_sub_inst) {
  998. /* Restore the parent exec_env's module inst */
  999. exec_env_main->module_inst = module_inst_main;
  1000. }
  1001. else {
  1002. if (module_inst_main)
  1003. /* Restore the existing exec_env's module inst */
  1004. exec_env->module_inst = module_inst_main;
  1005. if (exec_env_created)
  1006. wasm_exec_env_destroy(exec_env_created);
  1007. }
  1008. return ret;
  1009. }
  1010. static bool
  1011. execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1012. WASMFunctionInstance *malloc_func,
  1013. WASMFunctionInstance *retain_func, uint32 size,
  1014. uint32 *p_result)
  1015. {
  1016. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1017. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1018. #endif
  1019. WASMExecEnv *exec_env_created = NULL;
  1020. WASMModuleInstanceCommon *module_inst_old = NULL;
  1021. uint32 argv[2], argc;
  1022. bool ret;
  1023. argv[0] = size;
  1024. argc = 1;
  1025. /* if __retain is exported, then this module is compiled by
  1026. assemblyscript, the memory should be managed by as's runtime,
  1027. in this case we need to call the retain function after malloc
  1028. the memory */
  1029. if (retain_func) {
  1030. /* the malloc functino from assemblyscript is:
  1031. function __new(size: usize, id: u32)
  1032. id = 0 means this is an ArrayBuffer object */
  1033. argv[1] = 0;
  1034. argc = 2;
  1035. }
  1036. if (exec_env) {
  1037. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1038. if (exec_env_tls) {
  1039. bh_assert(exec_env_tls == exec_env);
  1040. }
  1041. #endif
  1042. bh_assert(exec_env->module_inst
  1043. == (WASMModuleInstanceCommon *)module_inst);
  1044. }
  1045. else {
  1046. /* Try using the existing exec_env */
  1047. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1048. exec_env = exec_env_tls;
  1049. #endif
  1050. #if WASM_ENABLE_THREAD_MGR != 0
  1051. if (!exec_env)
  1052. exec_env = wasm_clusters_search_exec_env(
  1053. (WASMModuleInstanceCommon *)module_inst);
  1054. #endif
  1055. if (!exec_env) {
  1056. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1057. (WASMModuleInstanceCommon *)module_inst,
  1058. module_inst->default_wasm_stack_size))) {
  1059. wasm_set_exception(module_inst, "allocate memory failed");
  1060. return false;
  1061. }
  1062. }
  1063. else {
  1064. /* Temporarily replace exec_env's module inst with current
  1065. module inst to ensure that the exec_env's module inst
  1066. is the correct one. */
  1067. module_inst_old = exec_env->module_inst;
  1068. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  1069. }
  1070. }
  1071. ret = wasm_call_function(exec_env, malloc_func, argc, argv);
  1072. if (retain_func && ret)
  1073. ret = wasm_call_function(exec_env, retain_func, 1, argv);
  1074. if (module_inst_old)
  1075. /* Restore the existing exec_env's module inst */
  1076. exec_env->module_inst = module_inst_old;
  1077. if (exec_env_created)
  1078. wasm_exec_env_destroy(exec_env_created);
  1079. if (ret)
  1080. *p_result = argv[0];
  1081. return ret;
  1082. }
  1083. static bool
  1084. execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1085. WASMFunctionInstance *free_func, uint32 offset)
  1086. {
  1087. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1088. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1089. #endif
  1090. WASMExecEnv *exec_env_created = NULL;
  1091. WASMModuleInstanceCommon *module_inst_old = NULL;
  1092. uint32 argv[2];
  1093. bool ret;
  1094. argv[0] = offset;
  1095. if (exec_env) {
  1096. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1097. if (exec_env_tls) {
  1098. bh_assert(exec_env_tls == exec_env);
  1099. }
  1100. #endif
  1101. bh_assert(exec_env->module_inst
  1102. == (WASMModuleInstanceCommon *)module_inst);
  1103. }
  1104. else {
  1105. /* Try using the existing exec_env */
  1106. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1107. exec_env = exec_env_tls;
  1108. #endif
  1109. #if WASM_ENABLE_THREAD_MGR != 0
  1110. if (!exec_env)
  1111. exec_env = wasm_clusters_search_exec_env(
  1112. (WASMModuleInstanceCommon *)module_inst);
  1113. #endif
  1114. if (!exec_env) {
  1115. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1116. (WASMModuleInstanceCommon *)module_inst,
  1117. module_inst->default_wasm_stack_size))) {
  1118. wasm_set_exception(module_inst, "allocate memory failed");
  1119. return false;
  1120. }
  1121. }
  1122. else {
  1123. /* Temporarily replace exec_env's module inst with current
  1124. module inst to ensure that the exec_env's module inst
  1125. is the correct one. */
  1126. module_inst_old = exec_env->module_inst;
  1127. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  1128. }
  1129. }
  1130. ret = wasm_call_function(exec_env, free_func, 1, argv);
  1131. if (module_inst_old)
  1132. /* Restore the existing exec_env's module inst */
  1133. exec_env->module_inst = module_inst_old;
  1134. if (exec_env_created)
  1135. wasm_exec_env_destroy(exec_env_created);
  1136. return ret;
  1137. }
  1138. #if WASM_ENABLE_MULTI_MODULE != 0
  1139. static bool
  1140. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  1141. uint32 stack_size, uint32 heap_size, char *error_buf,
  1142. uint32 error_buf_size)
  1143. {
  1144. bh_list *sub_module_inst_list = module_inst->e->sub_module_inst_list;
  1145. WASMRegisteredModule *sub_module_list_node =
  1146. bh_list_first_elem(module->import_module_list);
  1147. while (sub_module_list_node) {
  1148. WASMSubModInstNode *sub_module_inst_list_node = NULL;
  1149. WASMModule *sub_module = (WASMModule *)sub_module_list_node->module;
  1150. WASMModuleInstance *sub_module_inst = NULL;
  1151. sub_module_inst =
  1152. wasm_instantiate(sub_module, false, NULL, stack_size, heap_size,
  1153. error_buf, error_buf_size);
  1154. if (!sub_module_inst) {
  1155. LOG_DEBUG("instantiate %s failed",
  1156. sub_module_list_node->module_name);
  1157. goto failed;
  1158. }
  1159. sub_module_inst_list_node = runtime_malloc(sizeof(WASMSubModInstNode),
  1160. error_buf, error_buf_size);
  1161. if (!sub_module_inst_list_node) {
  1162. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  1163. sizeof(WASMSubModInstNode));
  1164. goto failed;
  1165. }
  1166. sub_module_inst_list_node->module_inst = sub_module_inst;
  1167. sub_module_inst_list_node->module_name =
  1168. sub_module_list_node->module_name;
  1169. bh_list_status ret =
  1170. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  1171. bh_assert(BH_LIST_SUCCESS == ret);
  1172. (void)ret;
  1173. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  1174. continue;
  1175. failed:
  1176. if (sub_module_inst_list_node) {
  1177. bh_list_remove(sub_module_inst_list, sub_module_inst_list_node);
  1178. wasm_runtime_free(sub_module_inst_list_node);
  1179. }
  1180. if (sub_module_inst)
  1181. wasm_deinstantiate(sub_module_inst, false);
  1182. return false;
  1183. }
  1184. return true;
  1185. }
  1186. static void
  1187. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  1188. {
  1189. bh_list *list = module_inst->e->sub_module_inst_list;
  1190. WASMSubModInstNode *node = bh_list_first_elem(list);
  1191. while (node) {
  1192. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  1193. bh_list_remove(list, node);
  1194. wasm_deinstantiate(node->module_inst, false);
  1195. wasm_runtime_free(node);
  1196. node = next_node;
  1197. }
  1198. }
  1199. #endif
  1200. static bool
  1201. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  1202. uint32 error_buf_size)
  1203. {
  1204. WASMModule *module = module_inst->module;
  1205. uint32 i;
  1206. for (i = 0; i < module->import_function_count; i++) {
  1207. WASMFunctionImport *func =
  1208. &((module->import_functions + i)->u.function);
  1209. if (!func->func_ptr_linked
  1210. #if WASM_ENABLE_MULTI_MODULE != 0
  1211. && !func->import_func_linked
  1212. #endif
  1213. ) {
  1214. #if WASM_ENABLE_WAMR_COMPILER == 0
  1215. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1216. func->module_name, func->field_name);
  1217. /* will throw exception only if calling */
  1218. #else
  1219. /* do nothing to avoid confused message */
  1220. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1221. }
  1222. }
  1223. for (i = 0; i < module->import_global_count; i++) {
  1224. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  1225. if (!global->is_linked) {
  1226. #if WASM_ENABLE_SPEC_TEST != 0
  1227. set_error_buf(error_buf, error_buf_size,
  1228. "unknown import or incompatible import type");
  1229. return false;
  1230. #else
  1231. #if WASM_ENABLE_WAMR_COMPILER == 0
  1232. set_error_buf_v(error_buf, error_buf_size,
  1233. "failed to link import global (%s, %s)",
  1234. global->module_name, global->field_name);
  1235. return false;
  1236. #else
  1237. /* do nothing to avoid confused message */
  1238. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1239. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1240. }
  1241. }
  1242. return true;
  1243. }
  1244. #if WASM_ENABLE_JIT != 0
  1245. static bool
  1246. init_func_ptrs(WASMModuleInstance *module_inst, WASMModule *module,
  1247. char *error_buf, uint32 error_buf_size)
  1248. {
  1249. uint32 i;
  1250. void **func_ptrs;
  1251. uint64 total_size = (uint64)sizeof(void *) * module_inst->e->function_count;
  1252. /* Allocate memory */
  1253. if (!(func_ptrs = module_inst->func_ptrs =
  1254. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1255. return false;
  1256. }
  1257. /* Set import function pointers */
  1258. for (i = 0; i < module->import_function_count; i++, func_ptrs++) {
  1259. WASMFunctionImport *import_func =
  1260. &module->import_functions[i].u.function;
  1261. /* TODO: handle multi module */
  1262. *func_ptrs = import_func->func_ptr_linked;
  1263. }
  1264. /* The defined function pointers will be set in
  1265. wasm_runtime_set_running_mode, no need to set them here */
  1266. return true;
  1267. }
  1268. #endif /* end of WASM_ENABLE_JIT != 0 */
  1269. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1270. static uint32
  1271. get_smallest_type_idx(WASMModule *module, WASMType *func_type)
  1272. {
  1273. uint32 i;
  1274. for (i = 0; i < module->type_count; i++) {
  1275. if (func_type == module->types[i])
  1276. return i;
  1277. }
  1278. bh_assert(0);
  1279. return -1;
  1280. }
  1281. static bool
  1282. init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
  1283. uint32 error_buf_size)
  1284. {
  1285. uint32 i;
  1286. uint64 total_size = (uint64)sizeof(uint32) * module_inst->e->function_count;
  1287. /* Allocate memory */
  1288. if (!(module_inst->func_type_indexes =
  1289. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1290. return false;
  1291. }
  1292. for (i = 0; i < module_inst->e->function_count; i++) {
  1293. WASMFunctionInstance *func_inst = module_inst->e->functions + i;
  1294. WASMType *func_type = func_inst->is_import_func
  1295. ? func_inst->u.func_import->func_type
  1296. : func_inst->u.func->func_type;
  1297. module_inst->func_type_indexes[i] =
  1298. get_smallest_type_idx(module_inst->module, func_type);
  1299. }
  1300. return true;
  1301. }
  1302. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1303. static bool
  1304. set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode,
  1305. bool first_time_set)
  1306. {
  1307. WASMModule *module = module_inst->module;
  1308. if (running_mode == Mode_Default) {
  1309. #if WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT == 0
  1310. running_mode = Mode_Interp;
  1311. #elif WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT == 0
  1312. running_mode = Mode_Fast_JIT;
  1313. #elif WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT != 0
  1314. running_mode = Mode_LLVM_JIT;
  1315. #else /* WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 */
  1316. #if WASM_ENABLE_LAZY_JIT == 0
  1317. running_mode = Mode_LLVM_JIT;
  1318. #else
  1319. running_mode = Mode_Multi_Tier_JIT;
  1320. #endif
  1321. #endif
  1322. }
  1323. if (!wasm_runtime_is_running_mode_supported(running_mode))
  1324. return false;
  1325. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1326. && WASM_ENABLE_LAZY_JIT != 0) /* No possible multi-tier JIT */
  1327. module_inst->e->running_mode = running_mode;
  1328. if (running_mode == Mode_Interp) {
  1329. /* Do nothing for Mode_Interp */
  1330. }
  1331. else if (running_mode == Mode_Fast_JIT) {
  1332. /* Do nothing for Mode_Fast_JIT since
  1333. module_inst->fast_jit_func_ptrs is same as
  1334. module->fast_jit_func_ptrs */
  1335. }
  1336. #if WASM_ENABLE_JIT != 0
  1337. else if (running_mode == Mode_LLVM_JIT) {
  1338. /* Set defined function pointers */
  1339. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1340. sizeof(void *) * module->function_count, module->func_ptrs,
  1341. sizeof(void *) * module->function_count);
  1342. }
  1343. #endif
  1344. else {
  1345. bh_assert(0);
  1346. }
  1347. #else /* Possible multi-tier JIT */
  1348. os_mutex_lock(&module->instance_list_lock);
  1349. module_inst->e->running_mode = running_mode;
  1350. if (running_mode == Mode_Interp) {
  1351. /* Do nothing for Mode_Interp */
  1352. }
  1353. #if WASM_ENABLE_FAST_JIT != 0
  1354. else if (running_mode == Mode_Fast_JIT) {
  1355. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  1356. uint32 i;
  1357. /* Allocate memory for fast_jit_func_ptrs if needed */
  1358. if (!module_inst->fast_jit_func_ptrs
  1359. || module_inst->fast_jit_func_ptrs == module->fast_jit_func_ptrs) {
  1360. uint64 total_size = (uint64)sizeof(void *) * module->function_count;
  1361. if (!(module_inst->fast_jit_func_ptrs =
  1362. runtime_malloc(total_size, NULL, 0))) {
  1363. os_mutex_unlock(&module->instance_list_lock);
  1364. return false;
  1365. }
  1366. }
  1367. for (i = 0; i < module->function_count; i++) {
  1368. if (module->functions[i]->fast_jit_jitted_code) {
  1369. /* current fast jit function has been compiled */
  1370. module_inst->fast_jit_func_ptrs[i] =
  1371. module->functions[i]->fast_jit_jitted_code;
  1372. }
  1373. else {
  1374. module_inst->fast_jit_func_ptrs[i] =
  1375. jit_globals->compile_fast_jit_and_then_call;
  1376. }
  1377. }
  1378. }
  1379. #endif
  1380. #if WASM_ENABLE_JIT != 0
  1381. else if (running_mode == Mode_LLVM_JIT) {
  1382. void **llvm_jit_func_ptrs;
  1383. uint32 i;
  1384. /* Notify backend threads to start llvm jit compilation */
  1385. module->enable_llvm_jit_compilation = true;
  1386. /* Wait until llvm jit finishes initialization */
  1387. os_mutex_lock(&module->tierup_wait_lock);
  1388. while (!module->llvm_jit_inited) {
  1389. os_cond_reltimedwait(&module->tierup_wait_cond,
  1390. &module->tierup_wait_lock, 10000);
  1391. if (module->orcjit_stop_compiling) {
  1392. /* init_llvm_jit_functions_stage2 failed */
  1393. os_mutex_unlock(&module->tierup_wait_lock);
  1394. os_mutex_unlock(&module->instance_list_lock);
  1395. return false;
  1396. }
  1397. }
  1398. os_mutex_unlock(&module->tierup_wait_lock);
  1399. llvm_jit_func_ptrs =
  1400. module_inst->func_ptrs + module->import_function_count;
  1401. for (i = 0; i < module->function_count; i++) {
  1402. llvm_jit_func_ptrs[i] = module->functions[i]->llvm_jit_func_ptr;
  1403. }
  1404. }
  1405. #endif
  1406. else if (running_mode == Mode_Multi_Tier_JIT) {
  1407. /* Notify backend threads to start llvm jit compilation */
  1408. module->enable_llvm_jit_compilation = true;
  1409. /* Free fast_jit_func_ptrs if it is allocated before */
  1410. if (module_inst->fast_jit_func_ptrs
  1411. && module_inst->fast_jit_func_ptrs != module->fast_jit_func_ptrs) {
  1412. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1413. }
  1414. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  1415. /* Copy all llvm jit func ptrs from the module */
  1416. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1417. sizeof(void *) * module->function_count, module->func_ptrs,
  1418. sizeof(void *) * module->function_count);
  1419. }
  1420. else {
  1421. bh_assert(0);
  1422. }
  1423. /* Add module instance into module's instance list if not added */
  1424. if (first_time_set) {
  1425. bool found = false;
  1426. WASMModuleInstance *node = module->instance_list;
  1427. while (node) {
  1428. if (node == module_inst) {
  1429. found = true;
  1430. break;
  1431. }
  1432. node = node->e->next;
  1433. }
  1434. if (!found) {
  1435. module_inst->e->next = module->instance_list;
  1436. module->instance_list = module_inst;
  1437. }
  1438. }
  1439. os_mutex_unlock(&module->instance_list_lock);
  1440. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1441. && WASM_ENABLE_LAZY_JIT != 0) */
  1442. (void)module;
  1443. return true;
  1444. }
  1445. bool
  1446. wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
  1447. {
  1448. return set_running_mode(module_inst, running_mode, false);
  1449. }
  1450. /**
  1451. * Instantiate module
  1452. */
  1453. WASMModuleInstance *
  1454. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  1455. WASMExecEnv *exec_env_main, uint32 stack_size,
  1456. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  1457. {
  1458. WASMModuleInstance *module_inst;
  1459. WASMGlobalInstance *globals = NULL, *global;
  1460. WASMTableInstance *first_table;
  1461. uint32 global_count, i;
  1462. uint32 base_offset, length, extra_info_offset;
  1463. uint32 module_inst_struct_size =
  1464. offsetof(WASMModuleInstance, global_table_data.bytes);
  1465. uint64 module_inst_mem_inst_size;
  1466. uint64 total_size, table_size = 0;
  1467. uint8 *global_data, *global_data_end;
  1468. #if WASM_ENABLE_MULTI_MODULE != 0
  1469. bool ret = false;
  1470. #endif
  1471. if (!module)
  1472. return NULL;
  1473. /* Check the heap size */
  1474. heap_size = align_uint(heap_size, 8);
  1475. if (heap_size > APP_HEAP_SIZE_MAX)
  1476. heap_size = APP_HEAP_SIZE_MAX;
  1477. module_inst_mem_inst_size =
  1478. (uint64)sizeof(WASMMemoryInstance)
  1479. * (module->import_memory_count + module->memory_count);
  1480. #if WASM_ENABLE_JIT != 0
  1481. /* If the module dosen't have memory, reserve one mem_info space
  1482. with empty content to align with llvm jit compiler */
  1483. if (module_inst_mem_inst_size == 0)
  1484. module_inst_mem_inst_size = (uint64)sizeof(WASMMemoryInstance);
  1485. #endif
  1486. /* Size of module inst, memory instances and global data */
  1487. total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
  1488. + module->global_data_size;
  1489. /* Calculate the size of table data */
  1490. for (i = 0; i < module->import_table_count; i++) {
  1491. WASMTableImport *import_table = &module->import_tables[i].u.table;
  1492. table_size += offsetof(WASMTableInstance, elems);
  1493. #if WASM_ENABLE_MULTI_MODULE != 0
  1494. table_size += (uint64)sizeof(uint32) * import_table->max_size;
  1495. #else
  1496. table_size += (uint64)sizeof(uint32)
  1497. * (import_table->possible_grow ? import_table->max_size
  1498. : import_table->init_size);
  1499. #endif
  1500. }
  1501. for (i = 0; i < module->table_count; i++) {
  1502. WASMTable *table = module->tables + i;
  1503. table_size += offsetof(WASMTableInstance, elems);
  1504. #if WASM_ENABLE_MULTI_MODULE != 0
  1505. table_size += (uint64)sizeof(uint32) * table->max_size;
  1506. #else
  1507. table_size +=
  1508. (uint64)sizeof(uint32)
  1509. * (table->possible_grow ? table->max_size : table->init_size);
  1510. #endif
  1511. }
  1512. total_size += table_size;
  1513. /* The offset of WASMModuleInstanceExtra, make it 8-byte aligned */
  1514. total_size = (total_size + 7LL) & ~7LL;
  1515. extra_info_offset = (uint32)total_size;
  1516. total_size += sizeof(WASMModuleInstanceExtra);
  1517. /* Allocate the memory for module instance with memory instances,
  1518. global data, table data appended at the end */
  1519. if (!(module_inst =
  1520. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1521. return NULL;
  1522. }
  1523. module_inst->module_type = Wasm_Module_Bytecode;
  1524. module_inst->module = module;
  1525. module_inst->e =
  1526. (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
  1527. #if WASM_ENABLE_MULTI_MODULE != 0
  1528. module_inst->e->sub_module_inst_list =
  1529. &module_inst->e->sub_module_inst_list_head;
  1530. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1531. error_buf, error_buf_size);
  1532. if (!ret) {
  1533. LOG_DEBUG("build a sub module list failed");
  1534. goto fail;
  1535. }
  1536. #endif
  1537. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1538. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1539. error_buf, error_buf_size))) {
  1540. goto fail;
  1541. }
  1542. #endif
  1543. /* Instantiate global firstly to get the mutable data size */
  1544. global_count = module->import_global_count + module->global_count;
  1545. if (global_count
  1546. && !(globals = globals_instantiate(module, module_inst, error_buf,
  1547. error_buf_size))) {
  1548. goto fail;
  1549. }
  1550. module_inst->e->global_count = global_count;
  1551. module_inst->e->globals = globals;
  1552. module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
  1553. + module_inst_mem_inst_size;
  1554. module_inst->global_data_size = module->global_data_size;
  1555. first_table = (WASMTableInstance *)(module_inst->global_data
  1556. + module->global_data_size);
  1557. module_inst->memory_count =
  1558. module->import_memory_count + module->memory_count;
  1559. module_inst->table_count = module->import_table_count + module->table_count;
  1560. module_inst->e->function_count =
  1561. module->import_function_count + module->function_count;
  1562. /* export */
  1563. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1564. #if WASM_ENABLE_MULTI_MODULE != 0
  1565. module_inst->export_table_count =
  1566. get_export_count(module, EXPORT_KIND_TABLE);
  1567. module_inst->export_memory_count =
  1568. get_export_count(module, EXPORT_KIND_MEMORY);
  1569. module_inst->export_global_count =
  1570. get_export_count(module, EXPORT_KIND_GLOBAL);
  1571. #endif
  1572. /* Instantiate memories/tables/functions */
  1573. if ((module_inst->memory_count > 0
  1574. && !(module_inst->memories = memories_instantiate(
  1575. module, module_inst, heap_size, error_buf, error_buf_size)))
  1576. || (module_inst->table_count > 0
  1577. && !(module_inst->tables =
  1578. tables_instantiate(module, module_inst, first_table,
  1579. error_buf, error_buf_size)))
  1580. || (module_inst->e->function_count > 0
  1581. && !(module_inst->e->functions = functions_instantiate(
  1582. module, module_inst, error_buf, error_buf_size)))
  1583. || (module_inst->export_func_count > 0
  1584. && !(module_inst->export_functions = export_functions_instantiate(
  1585. module, module_inst, module_inst->export_func_count,
  1586. error_buf, error_buf_size)))
  1587. #if WASM_ENABLE_MULTI_MODULE != 0
  1588. || (module_inst->export_global_count > 0
  1589. && !(module_inst->export_globals = export_globals_instantiate(
  1590. module, module_inst, module_inst->export_global_count,
  1591. error_buf, error_buf_size)))
  1592. #endif
  1593. #if WASM_ENABLE_JIT != 0
  1594. || (module_inst->e->function_count > 0
  1595. && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  1596. #endif
  1597. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1598. || (module_inst->e->function_count > 0
  1599. && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
  1600. #endif
  1601. ) {
  1602. goto fail;
  1603. }
  1604. if (global_count > 0) {
  1605. /* Initialize the global data */
  1606. global_data = module_inst->global_data;
  1607. global_data_end = global_data + module->global_data_size;
  1608. global = globals;
  1609. for (i = 0; i < global_count; i++, global++) {
  1610. switch (global->type) {
  1611. case VALUE_TYPE_I32:
  1612. case VALUE_TYPE_F32:
  1613. #if WASM_ENABLE_REF_TYPES != 0
  1614. case VALUE_TYPE_FUNCREF:
  1615. case VALUE_TYPE_EXTERNREF:
  1616. #endif
  1617. *(int32 *)global_data = global->initial_value.i32;
  1618. global_data += sizeof(int32);
  1619. break;
  1620. case VALUE_TYPE_I64:
  1621. case VALUE_TYPE_F64:
  1622. bh_memcpy_s(global_data,
  1623. (uint32)(global_data_end - global_data),
  1624. &global->initial_value.i64, sizeof(int64));
  1625. global_data += sizeof(int64);
  1626. break;
  1627. #if WASM_ENABLE_SIMD != 0
  1628. case VALUE_TYPE_V128:
  1629. bh_memcpy_s(global_data, (uint32)sizeof(V128),
  1630. &global->initial_value.v128, sizeof(V128));
  1631. global_data += sizeof(V128);
  1632. break;
  1633. #endif
  1634. default:
  1635. bh_assert(0);
  1636. }
  1637. }
  1638. bh_assert(global_data == global_data_end);
  1639. }
  1640. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1641. goto fail;
  1642. }
  1643. /* Initialize the memory data with data segment section */
  1644. for (i = 0; i < module->data_seg_count; i++) {
  1645. WASMMemoryInstance *memory = NULL;
  1646. uint8 *memory_data = NULL;
  1647. uint32 memory_size = 0;
  1648. WASMDataSeg *data_seg = module->data_segments[i];
  1649. #if WASM_ENABLE_BULK_MEMORY != 0
  1650. if (data_seg->is_passive)
  1651. continue;
  1652. #endif
  1653. /* has check it in loader */
  1654. memory = module_inst->memories[data_seg->memory_index];
  1655. bh_assert(memory);
  1656. memory_data = memory->memory_data;
  1657. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1658. bh_assert(memory_data || memory_size == 0);
  1659. bh_assert(data_seg->base_offset.init_expr_type
  1660. == INIT_EXPR_TYPE_I32_CONST
  1661. || data_seg->base_offset.init_expr_type
  1662. == INIT_EXPR_TYPE_GET_GLOBAL);
  1663. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1664. if (!check_global_init_expr(module,
  1665. data_seg->base_offset.u.global_index,
  1666. error_buf, error_buf_size)) {
  1667. goto fail;
  1668. }
  1669. if (!globals
  1670. || globals[data_seg->base_offset.u.global_index].type
  1671. != VALUE_TYPE_I32) {
  1672. set_error_buf(error_buf, error_buf_size,
  1673. "data segment does not fit");
  1674. goto fail;
  1675. }
  1676. base_offset =
  1677. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  1678. }
  1679. else {
  1680. base_offset = (uint32)data_seg->base_offset.u.i32;
  1681. }
  1682. /* check offset */
  1683. if (base_offset > memory_size) {
  1684. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1685. memory_size);
  1686. #if WASM_ENABLE_REF_TYPES != 0
  1687. set_error_buf(error_buf, error_buf_size,
  1688. "out of bounds memory access");
  1689. #else
  1690. set_error_buf(error_buf, error_buf_size,
  1691. "data segment does not fit");
  1692. #endif
  1693. goto fail;
  1694. }
  1695. /* check offset + length(could be zero) */
  1696. length = data_seg->data_length;
  1697. if (base_offset + length > memory_size) {
  1698. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1699. base_offset, length, memory_size);
  1700. #if WASM_ENABLE_REF_TYPES != 0
  1701. set_error_buf(error_buf, error_buf_size,
  1702. "out of bounds memory access");
  1703. #else
  1704. set_error_buf(error_buf, error_buf_size,
  1705. "data segment does not fit");
  1706. #endif
  1707. goto fail;
  1708. }
  1709. if (memory_data) {
  1710. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1711. data_seg->data, length);
  1712. }
  1713. }
  1714. /* Initialize the table data with table segment section */
  1715. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1716. i++) {
  1717. WASMTableSeg *table_seg = module->table_segments + i;
  1718. /* has check it in loader */
  1719. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1720. uint32 *table_data;
  1721. #if WASM_ENABLE_REF_TYPES != 0
  1722. uint8 tbl_elem_type;
  1723. uint32 tbl_init_size, tbl_max_size;
  1724. #endif
  1725. bh_assert(table);
  1726. #if WASM_ENABLE_REF_TYPES != 0
  1727. (void)wasm_runtime_get_table_inst_elem_type(
  1728. (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
  1729. &tbl_elem_type, &tbl_init_size, &tbl_max_size);
  1730. if (tbl_elem_type != VALUE_TYPE_FUNCREF
  1731. && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
  1732. set_error_buf(error_buf, error_buf_size,
  1733. "elements segment does not fit");
  1734. goto fail;
  1735. }
  1736. (void)tbl_init_size;
  1737. (void)tbl_max_size;
  1738. #endif
  1739. table_data = table->elems;
  1740. #if WASM_ENABLE_MULTI_MODULE != 0
  1741. if (table_seg->table_index < module->import_table_count
  1742. && module_inst->e->table_insts_linked[table_seg->table_index]) {
  1743. table_data =
  1744. module_inst->e->table_insts_linked[table_seg->table_index]
  1745. ->elems;
  1746. }
  1747. #endif
  1748. bh_assert(table_data);
  1749. #if WASM_ENABLE_REF_TYPES != 0
  1750. if (!wasm_elem_is_active(table_seg->mode))
  1751. continue;
  1752. #endif
  1753. #if WASM_ENABLE_REF_TYPES != 0
  1754. bh_assert(table_seg->base_offset.init_expr_type
  1755. == INIT_EXPR_TYPE_I32_CONST
  1756. || table_seg->base_offset.init_expr_type
  1757. == INIT_EXPR_TYPE_GET_GLOBAL
  1758. || table_seg->base_offset.init_expr_type
  1759. == INIT_EXPR_TYPE_FUNCREF_CONST
  1760. || table_seg->base_offset.init_expr_type
  1761. == INIT_EXPR_TYPE_REFNULL_CONST);
  1762. #else
  1763. bh_assert(table_seg->base_offset.init_expr_type
  1764. == INIT_EXPR_TYPE_I32_CONST
  1765. || table_seg->base_offset.init_expr_type
  1766. == INIT_EXPR_TYPE_GET_GLOBAL);
  1767. #endif
  1768. /* init vec(funcidx) or vec(expr) */
  1769. if (table_seg->base_offset.init_expr_type
  1770. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1771. if (!check_global_init_expr(module,
  1772. table_seg->base_offset.u.global_index,
  1773. error_buf, error_buf_size)) {
  1774. goto fail;
  1775. }
  1776. if (!globals
  1777. || globals[table_seg->base_offset.u.global_index].type
  1778. != VALUE_TYPE_I32) {
  1779. set_error_buf(error_buf, error_buf_size,
  1780. "elements segment does not fit");
  1781. goto fail;
  1782. }
  1783. table_seg->base_offset.u.i32 =
  1784. globals[table_seg->base_offset.u.global_index]
  1785. .initial_value.i32;
  1786. }
  1787. /* check offset since length might negative */
  1788. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1789. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1790. table_seg->base_offset.u.i32, table->cur_size);
  1791. #if WASM_ENABLE_REF_TYPES != 0
  1792. set_error_buf(error_buf, error_buf_size,
  1793. "out of bounds table access");
  1794. #else
  1795. set_error_buf(error_buf, error_buf_size,
  1796. "elements segment does not fit");
  1797. #endif
  1798. goto fail;
  1799. }
  1800. /* check offset + length(could be zero) */
  1801. length = table_seg->function_count;
  1802. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1803. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1804. table_seg->base_offset.u.i32, length, table->cur_size);
  1805. #if WASM_ENABLE_REF_TYPES != 0
  1806. set_error_buf(error_buf, error_buf_size,
  1807. "out of bounds table access");
  1808. #else
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "elements segment does not fit");
  1811. #endif
  1812. goto fail;
  1813. }
  1814. /**
  1815. * Check function index in the current module inst for now.
  1816. * will check the linked table inst owner in future.
  1817. * so loader check is enough
  1818. */
  1819. bh_memcpy_s(
  1820. table_data + table_seg->base_offset.u.i32,
  1821. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1822. * sizeof(uint32)),
  1823. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1824. }
  1825. /* Initialize the thread related data */
  1826. if (stack_size == 0)
  1827. stack_size = DEFAULT_WASM_STACK_SIZE;
  1828. #if WASM_ENABLE_SPEC_TEST != 0
  1829. if (stack_size < 128 * 1024)
  1830. stack_size = 128 * 1024;
  1831. #endif
  1832. module_inst->default_wasm_stack_size = stack_size;
  1833. if (module->malloc_function != (uint32)-1) {
  1834. module_inst->e->malloc_function =
  1835. &module_inst->e->functions[module->malloc_function];
  1836. }
  1837. if (module->free_function != (uint32)-1) {
  1838. module_inst->e->free_function =
  1839. &module_inst->e->functions[module->free_function];
  1840. }
  1841. if (module->retain_function != (uint32)-1) {
  1842. module_inst->e->retain_function =
  1843. &module_inst->e->functions[module->retain_function];
  1844. }
  1845. #if WASM_ENABLE_LIBC_WASI != 0
  1846. /* The sub-instance will get the wasi_ctx from main-instance */
  1847. if (!is_sub_inst) {
  1848. if (!wasm_runtime_init_wasi(
  1849. (WASMModuleInstanceCommon *)module_inst,
  1850. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1851. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1852. module->wasi_args.env, module->wasi_args.env_count,
  1853. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  1854. module->wasi_args.ns_lookup_pool,
  1855. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  1856. module->wasi_args.argc, module->wasi_args.stdio[0],
  1857. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  1858. error_buf, error_buf_size)) {
  1859. goto fail;
  1860. }
  1861. }
  1862. #endif
  1863. #if WASM_ENABLE_DEBUG_INTERP != 0
  1864. if (!is_sub_inst) {
  1865. /* Add module instance into module's instance list */
  1866. os_mutex_lock(&module->instance_list_lock);
  1867. if (module->instance_list) {
  1868. LOG_WARNING(
  1869. "warning: multiple instances referencing to the same module "
  1870. "may cause unexpected behaviour during debugging");
  1871. }
  1872. module_inst->e->next = module->instance_list;
  1873. module->instance_list = module_inst;
  1874. os_mutex_unlock(&module->instance_list_lock);
  1875. }
  1876. #endif
  1877. /* Set running mode before executing wasm functions */
  1878. if (!set_running_mode(module_inst, wasm_runtime_get_default_running_mode(),
  1879. true)) {
  1880. set_error_buf(error_buf, error_buf_size,
  1881. "set instance running mode failed");
  1882. goto fail;
  1883. }
  1884. if (module->start_function != (uint32)-1) {
  1885. /* TODO: fix start function can be import function issue */
  1886. if (module->start_function >= module->import_function_count)
  1887. module_inst->e->start_function =
  1888. &module_inst->e->functions[module->start_function];
  1889. }
  1890. if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
  1891. exec_env_main)) {
  1892. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1893. goto fail;
  1894. }
  1895. #if WASM_ENABLE_MEMORY_TRACING != 0
  1896. wasm_runtime_dump_module_inst_mem_consumption(
  1897. (WASMModuleInstanceCommon *)module_inst);
  1898. #endif
  1899. (void)global_data_end;
  1900. return module_inst;
  1901. fail:
  1902. wasm_deinstantiate(module_inst, false);
  1903. return NULL;
  1904. }
  1905. void
  1906. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1907. {
  1908. if (!module_inst)
  1909. return;
  1910. if (module_inst->exec_env_singleton) {
  1911. /* wasm_exec_env_destroy will call
  1912. wasm_cluster_wait_for_all_except_self to wait for other
  1913. threads, so as to destroy their exec_envs and module
  1914. instances first, and avoid accessing the shared resources
  1915. of current module instance after it is deinstantiated. */
  1916. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1917. }
  1918. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  1919. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1920. && WASM_ENABLE_LAZY_JIT != 0)
  1921. /* Remove instance from module's instance list before freeing
  1922. func_ptrs and fast_jit_func_ptrs of the instance, to avoid
  1923. accessing the freed memory in the jit backend compilation
  1924. threads */
  1925. {
  1926. WASMModule *module = module_inst->module;
  1927. WASMModuleInstance *instance_prev = NULL, *instance;
  1928. os_mutex_lock(&module->instance_list_lock);
  1929. instance = module->instance_list;
  1930. while (instance) {
  1931. if (instance == module_inst) {
  1932. if (!instance_prev)
  1933. module->instance_list = instance->e->next;
  1934. else
  1935. instance_prev->e->next = instance->e->next;
  1936. break;
  1937. }
  1938. instance_prev = instance;
  1939. instance = instance->e->next;
  1940. }
  1941. os_mutex_unlock(&module->instance_list_lock);
  1942. }
  1943. #endif
  1944. #if WASM_ENABLE_JIT != 0
  1945. if (module_inst->func_ptrs)
  1946. wasm_runtime_free(module_inst->func_ptrs);
  1947. #endif
  1948. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1949. && WASM_ENABLE_LAZY_JIT != 0
  1950. if (module_inst->fast_jit_func_ptrs
  1951. && module_inst->fast_jit_func_ptrs
  1952. != module_inst->module->fast_jit_func_ptrs)
  1953. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1954. #endif
  1955. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1956. if (module_inst->func_type_indexes)
  1957. wasm_runtime_free(module_inst->func_type_indexes);
  1958. #endif
  1959. #if WASM_ENABLE_MULTI_MODULE != 0
  1960. sub_module_deinstantiate(module_inst);
  1961. #endif
  1962. #if WASM_ENABLE_LIBC_WASI != 0
  1963. /* Destroy wasi resource before freeing app heap, since some fields of
  1964. wasi contex are allocated from app heap, and if app heap is freed,
  1965. these fields will be set to NULL, we cannot free their internal data
  1966. which may allocated from global heap. */
  1967. /* Only destroy wasi ctx in the main module instance */
  1968. if (!is_sub_inst)
  1969. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst);
  1970. #endif
  1971. if (module_inst->memory_count > 0)
  1972. memories_deinstantiate(module_inst, module_inst->memories,
  1973. module_inst->memory_count);
  1974. if (module_inst->import_func_ptrs) {
  1975. wasm_runtime_free(module_inst->import_func_ptrs);
  1976. }
  1977. tables_deinstantiate(module_inst);
  1978. functions_deinstantiate(module_inst->e->functions,
  1979. module_inst->e->function_count);
  1980. globals_deinstantiate(module_inst->e->globals);
  1981. export_functions_deinstantiate(module_inst->export_functions);
  1982. #if WASM_ENABLE_MULTI_MODULE != 0
  1983. export_globals_deinstantiate(module_inst->export_globals);
  1984. #endif
  1985. #if WASM_ENABLE_REF_TYPES != 0
  1986. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  1987. #endif
  1988. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1989. if (module_inst->frames) {
  1990. bh_vector_destroy(module_inst->frames);
  1991. wasm_runtime_free(module_inst->frames);
  1992. module_inst->frames = NULL;
  1993. }
  1994. #endif
  1995. if (module_inst->e->c_api_func_imports)
  1996. wasm_runtime_free(module_inst->e->c_api_func_imports);
  1997. #if WASM_ENABLE_WASI_NN != 0
  1998. if (!is_sub_inst)
  1999. wasi_nn_destroy(module_inst);
  2000. #endif
  2001. wasm_runtime_free(module_inst);
  2002. }
  2003. WASMFunctionInstance *
  2004. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  2005. const char *signature)
  2006. {
  2007. uint32 i;
  2008. for (i = 0; i < module_inst->export_func_count; i++)
  2009. if (!strcmp(module_inst->export_functions[i].name, name))
  2010. return module_inst->export_functions[i].function;
  2011. (void)signature;
  2012. return NULL;
  2013. }
  2014. #if WASM_ENABLE_MULTI_MODULE != 0
  2015. WASMGlobalInstance *
  2016. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  2017. {
  2018. uint32 i;
  2019. for (i = 0; i < module_inst->export_global_count; i++)
  2020. if (!strcmp(module_inst->export_globals[i].name, name))
  2021. return module_inst->export_globals[i].global;
  2022. return NULL;
  2023. }
  2024. WASMMemoryInstance *
  2025. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  2026. {
  2027. /**
  2028. * using a strong assumption that one module instance only has
  2029. * one memory instance
  2030. */
  2031. (void)module_inst->export_memories;
  2032. return module_inst->memories[0];
  2033. }
  2034. WASMTableInstance *
  2035. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  2036. {
  2037. /**
  2038. * using a strong assumption that one module instance only has
  2039. * one table instance
  2040. */
  2041. (void)module_inst->export_tables;
  2042. return module_inst->tables[0];
  2043. }
  2044. #endif
  2045. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2046. static void
  2047. call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
  2048. WASMExecEnv *exec_env,
  2049. WASMFunctionInstance *function, unsigned argc,
  2050. uint32 argv[])
  2051. {
  2052. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2053. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  2054. uint32 page_size = os_getpagesize();
  2055. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  2056. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  2057. uint8 *prev_top = exec_env->wasm_stack.s.top;
  2058. #ifdef BH_PLATFORM_WINDOWS
  2059. int result;
  2060. bool has_exception;
  2061. char exception[EXCEPTION_BUF_LEN];
  2062. #endif
  2063. bool ret = true;
  2064. /* Check native stack overflow firstly to ensure we have enough
  2065. native stack to run the following codes before actually calling
  2066. the aot function in invokeNative function. */
  2067. RECORD_STACK_USAGE(exec_env, (uint8 *)&exec_env_tls);
  2068. if ((uint8 *)&exec_env_tls < exec_env->native_stack_boundary
  2069. + page_size * (guard_page_count + 1)) {
  2070. wasm_set_exception(module_inst, "native stack overflow");
  2071. return;
  2072. }
  2073. if (exec_env_tls && (exec_env_tls != exec_env)) {
  2074. wasm_set_exception(module_inst, "invalid exec env");
  2075. return;
  2076. }
  2077. if (!os_thread_signal_inited()) {
  2078. wasm_set_exception(module_inst, "thread signal env not inited");
  2079. return;
  2080. }
  2081. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  2082. wasm_runtime_set_exec_env_tls(exec_env);
  2083. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  2084. #ifndef BH_PLATFORM_WINDOWS
  2085. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2086. #else
  2087. __try {
  2088. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2089. } __except (wasm_copy_exception(module_inst, NULL)
  2090. ? EXCEPTION_EXECUTE_HANDLER
  2091. : EXCEPTION_CONTINUE_SEARCH) {
  2092. /* exception was thrown in wasm_exception_handler */
  2093. ret = false;
  2094. }
  2095. has_exception = wasm_copy_exception(module_inst, exception);
  2096. if (has_exception && strstr(exception, "native stack overflow")) {
  2097. /* After a stack overflow, the stack was left
  2098. in a damaged state, let the CRT repair it */
  2099. result = _resetstkoflw();
  2100. bh_assert(result != 0);
  2101. }
  2102. #endif
  2103. }
  2104. else {
  2105. /* Exception has been set in signal handler before calling longjmp */
  2106. ret = false;
  2107. }
  2108. /* Note: can't check wasm_get_exception(module_inst) here, there may be
  2109. * exception which is not caught by hardware (e.g. uninitialized elements),
  2110. * then the stack-frame is already freed inside wasm_interp_call_wasm */
  2111. if (!ret) {
  2112. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2113. if (wasm_interp_create_call_stack(exec_env)) {
  2114. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  2115. }
  2116. #endif
  2117. /* Restore operand frames */
  2118. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  2119. exec_env->wasm_stack.s.top = prev_top;
  2120. }
  2121. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  2122. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  2123. if (!exec_env->jmpbuf_stack_top) {
  2124. wasm_runtime_set_exec_env_tls(NULL);
  2125. }
  2126. if (!ret) {
  2127. os_sigreturn();
  2128. os_signal_unmask();
  2129. }
  2130. (void)jmpbuf_node_pop;
  2131. }
  2132. #define interp_call_wasm call_wasm_with_hw_bound_check
  2133. #else
  2134. #define interp_call_wasm wasm_interp_call_wasm
  2135. #endif
  2136. bool
  2137. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  2138. unsigned argc, uint32 argv[])
  2139. {
  2140. WASMModuleInstance *module_inst =
  2141. (WASMModuleInstance *)exec_env->module_inst;
  2142. /* set thread handle and stack boundary */
  2143. wasm_exec_env_set_thread_info(exec_env);
  2144. interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2145. return !wasm_copy_exception(module_inst, NULL);
  2146. }
  2147. #if WASM_ENABLE_PERF_PROFILING != 0
  2148. void
  2149. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  2150. {
  2151. WASMExportFuncInstance *export_func;
  2152. WASMFunctionInstance *func_inst;
  2153. char *func_name;
  2154. uint32 i, j;
  2155. os_printf("Performance profiler data:\n");
  2156. for (i = 0; i < module_inst->e->function_count; i++) {
  2157. func_inst = module_inst->e->functions + i;
  2158. if (func_inst->is_import_func) {
  2159. func_name = func_inst->u.func_import->field_name;
  2160. }
  2161. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2162. else if (func_inst->u.func->field_name) {
  2163. func_name = func_inst->u.func->field_name;
  2164. }
  2165. #endif
  2166. else {
  2167. func_name = NULL;
  2168. for (j = 0; j < module_inst->export_func_count; j++) {
  2169. export_func = module_inst->export_functions + j;
  2170. if (export_func->function == func_inst) {
  2171. func_name = export_func->name;
  2172. break;
  2173. }
  2174. }
  2175. }
  2176. if (func_name)
  2177. os_printf(" func %s, execution time: %.3f ms, execution count: %d "
  2178. "times\n",
  2179. func_name,
  2180. module_inst->e->functions[i].total_exec_time / 1000.0f,
  2181. module_inst->e->functions[i].total_exec_cnt);
  2182. else
  2183. os_printf(" func %d, execution time: %.3f ms, execution count: %d "
  2184. "times\n",
  2185. i, module_inst->e->functions[i].total_exec_time / 1000.0f,
  2186. module_inst->e->functions[i].total_exec_cnt);
  2187. }
  2188. }
  2189. #endif
  2190. uint32
  2191. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  2192. WASMExecEnv *exec_env, uint32 size,
  2193. void **p_native_addr)
  2194. {
  2195. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2196. uint8 *addr = NULL;
  2197. uint32 offset = 0;
  2198. if (!memory) {
  2199. wasm_set_exception(module_inst, "uninitialized memory");
  2200. return 0;
  2201. }
  2202. if (memory->heap_handle) {
  2203. addr = mem_allocator_malloc(memory->heap_handle, size);
  2204. }
  2205. else if (module_inst->e->malloc_function && module_inst->e->free_function) {
  2206. if (!execute_malloc_function(
  2207. module_inst, exec_env, module_inst->e->malloc_function,
  2208. module_inst->e->retain_function, size, &offset)) {
  2209. return 0;
  2210. }
  2211. /* If we use app's malloc function,
  2212. the default memory may be changed while memory growing */
  2213. memory = wasm_get_default_memory(module_inst);
  2214. addr = offset ? memory->memory_data + offset : NULL;
  2215. }
  2216. if (!addr) {
  2217. if (memory->heap_handle
  2218. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2219. wasm_runtime_show_app_heap_corrupted_prompt();
  2220. wasm_set_exception(module_inst, "app heap corrupted");
  2221. }
  2222. else {
  2223. LOG_WARNING("warning: allocate %u bytes memory failed", size);
  2224. }
  2225. return 0;
  2226. }
  2227. if (p_native_addr)
  2228. *p_native_addr = addr;
  2229. return (uint32)(addr - memory->memory_data);
  2230. }
  2231. uint32
  2232. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  2233. WASMExecEnv *exec_env, uint32 ptr, uint32 size,
  2234. void **p_native_addr)
  2235. {
  2236. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2237. uint8 *addr = NULL;
  2238. if (!memory) {
  2239. wasm_set_exception(module_inst, "uninitialized memory");
  2240. return 0;
  2241. }
  2242. if (memory->heap_handle) {
  2243. addr = mem_allocator_realloc(
  2244. memory->heap_handle, ptr ? memory->memory_data + ptr : NULL, size);
  2245. }
  2246. /* Only support realloc in WAMR's app heap */
  2247. (void)exec_env;
  2248. if (!addr) {
  2249. if (memory->heap_handle
  2250. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2251. wasm_set_exception(module_inst, "app heap corrupted");
  2252. }
  2253. else {
  2254. wasm_set_exception(module_inst, "out of memory");
  2255. }
  2256. return 0;
  2257. }
  2258. if (p_native_addr)
  2259. *p_native_addr = addr;
  2260. return (uint32)(addr - memory->memory_data);
  2261. }
  2262. void
  2263. wasm_module_free_internal(WASMModuleInstance *module_inst,
  2264. WASMExecEnv *exec_env, uint32 ptr)
  2265. {
  2266. if (ptr) {
  2267. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2268. uint8 *addr;
  2269. if (!memory) {
  2270. return;
  2271. }
  2272. addr = memory->memory_data + ptr;
  2273. if (memory->heap_handle && memory->heap_data <= addr
  2274. && addr < memory->heap_data_end) {
  2275. mem_allocator_free(memory->heap_handle, addr);
  2276. }
  2277. else if (module_inst->e->malloc_function
  2278. && module_inst->e->free_function && memory->memory_data <= addr
  2279. && addr < memory->memory_data_end) {
  2280. execute_free_function(module_inst, exec_env,
  2281. module_inst->e->free_function, ptr);
  2282. }
  2283. }
  2284. }
  2285. uint32
  2286. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  2287. void **p_native_addr)
  2288. {
  2289. return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
  2290. }
  2291. uint32
  2292. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  2293. void **p_native_addr)
  2294. {
  2295. return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
  2296. p_native_addr);
  2297. }
  2298. void
  2299. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  2300. {
  2301. wasm_module_free_internal(module_inst, NULL, ptr);
  2302. }
  2303. uint32
  2304. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  2305. uint32 size)
  2306. {
  2307. char *buffer;
  2308. uint32 buffer_offset =
  2309. wasm_module_malloc(module_inst, size, (void **)&buffer);
  2310. if (buffer_offset != 0) {
  2311. buffer = wasm_runtime_addr_app_to_native(
  2312. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  2313. bh_memcpy_s(buffer, size, src, size);
  2314. }
  2315. return buffer_offset;
  2316. }
  2317. #if WASM_ENABLE_REF_TYPES != 0
  2318. bool
  2319. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  2320. uint32 inc_size, uint32 init_val)
  2321. {
  2322. uint32 total_size, *new_table_data_start, i;
  2323. WASMTableInstance *table_inst;
  2324. if (!inc_size) {
  2325. return true;
  2326. }
  2327. bh_assert(table_idx < module_inst->table_count);
  2328. table_inst = wasm_get_table_inst(module_inst, table_idx);
  2329. if (!table_inst) {
  2330. return false;
  2331. }
  2332. if (inc_size > UINT32_MAX - table_inst->cur_size) {
  2333. return false;
  2334. }
  2335. total_size = table_inst->cur_size + inc_size;
  2336. if (total_size > table_inst->max_size) {
  2337. return false;
  2338. }
  2339. /* fill in */
  2340. new_table_data_start = table_inst->elems + table_inst->cur_size;
  2341. for (i = 0; i < inc_size; ++i) {
  2342. new_table_data_start[i] = init_val;
  2343. }
  2344. table_inst->cur_size = total_size;
  2345. return true;
  2346. }
  2347. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2348. static bool
  2349. call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2350. uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
  2351. {
  2352. WASMModuleInstance *module_inst = NULL;
  2353. WASMTableInstance *table_inst = NULL;
  2354. uint32 func_idx = 0;
  2355. WASMFunctionInstance *func_inst = NULL;
  2356. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2357. bh_assert(module_inst);
  2358. table_inst = module_inst->tables[tbl_idx];
  2359. if (!table_inst) {
  2360. wasm_set_exception(module_inst, "unknown table");
  2361. goto got_exception;
  2362. }
  2363. if (elem_idx >= table_inst->cur_size) {
  2364. wasm_set_exception(module_inst, "undefined element");
  2365. goto got_exception;
  2366. }
  2367. func_idx = table_inst->elems[elem_idx];
  2368. if (func_idx == NULL_REF) {
  2369. wasm_set_exception(module_inst, "uninitialized element");
  2370. goto got_exception;
  2371. }
  2372. /**
  2373. * we insist to call functions owned by the module itself
  2374. **/
  2375. if (func_idx >= module_inst->e->function_count) {
  2376. wasm_set_exception(module_inst, "unknown function");
  2377. goto got_exception;
  2378. }
  2379. func_inst = module_inst->e->functions + func_idx;
  2380. if (check_type_idx) {
  2381. WASMType *cur_type = module_inst->module->types[type_idx];
  2382. WASMType *cur_func_type;
  2383. if (func_inst->is_import_func)
  2384. cur_func_type = func_inst->u.func_import->func_type;
  2385. else
  2386. cur_func_type = func_inst->u.func->func_type;
  2387. if (cur_type != cur_func_type) {
  2388. wasm_set_exception(module_inst, "indirect call type mismatch");
  2389. goto got_exception;
  2390. }
  2391. }
  2392. interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
  2393. return !wasm_copy_exception(module_inst, NULL);
  2394. got_exception:
  2395. return false;
  2396. }
  2397. bool
  2398. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2399. uint32 argc, uint32 argv[])
  2400. {
  2401. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2402. }
  2403. #if WASM_ENABLE_THREAD_MGR != 0
  2404. bool
  2405. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  2406. {
  2407. WASMModuleInstance *module_inst =
  2408. (WASMModuleInstance *)exec_env->module_inst;
  2409. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2410. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  2411. /* Check the aux stack space */
  2412. uint32 data_end = module_inst->module->aux_data_end;
  2413. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2414. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2415. if ((is_stack_before_data && (size > start_offset))
  2416. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2417. return false;
  2418. #endif
  2419. if (stack_top_idx != (uint32)-1) {
  2420. /* The aux stack top is a wasm global,
  2421. set the initial value for the global */
  2422. uint8 *global_addr =
  2423. module_inst->global_data
  2424. + module_inst->e->globals[stack_top_idx].data_offset;
  2425. *(int32 *)global_addr = start_offset;
  2426. /* The aux stack boundary is a constant value,
  2427. set the value to exec_env */
  2428. exec_env->aux_stack_boundary.boundary = start_offset - size;
  2429. exec_env->aux_stack_bottom.bottom = start_offset;
  2430. return true;
  2431. }
  2432. return false;
  2433. }
  2434. bool
  2435. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  2436. {
  2437. WASMModuleInstance *module_inst =
  2438. (WASMModuleInstance *)exec_env->module_inst;
  2439. /* The aux stack information is resolved in loader
  2440. and store in module */
  2441. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2442. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  2443. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2444. if (start_offset)
  2445. *start_offset = stack_bottom;
  2446. if (size)
  2447. *size = total_aux_stack_size;
  2448. return true;
  2449. }
  2450. return false;
  2451. }
  2452. #endif
  2453. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2454. void
  2455. wasm_get_module_mem_consumption(const WASMModule *module,
  2456. WASMModuleMemConsumption *mem_conspn)
  2457. {
  2458. uint32 i, size;
  2459. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2460. mem_conspn->module_struct_size = sizeof(WASMModule);
  2461. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2462. for (i = 0; i < module->type_count; i++) {
  2463. WASMType *type = module->types[i];
  2464. size = offsetof(WASMType, types)
  2465. + sizeof(uint8) * (type->param_count + type->result_count);
  2466. mem_conspn->types_size += size;
  2467. }
  2468. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2469. mem_conspn->functions_size =
  2470. sizeof(WASMFunction *) * module->function_count;
  2471. for (i = 0; i < module->function_count; i++) {
  2472. WASMFunction *func = module->functions[i];
  2473. WASMType *type = func->func_type;
  2474. size = sizeof(WASMFunction) + func->local_count
  2475. + sizeof(uint16) * (type->param_count + func->local_count);
  2476. #if WASM_ENABLE_FAST_INTERP != 0
  2477. size +=
  2478. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  2479. #endif
  2480. mem_conspn->functions_size += size;
  2481. }
  2482. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2483. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2484. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2485. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2486. mem_conspn->table_segs_size =
  2487. sizeof(WASMTableSeg) * module->table_seg_count;
  2488. for (i = 0; i < module->table_seg_count; i++) {
  2489. WASMTableSeg *table_seg = &module->table_segments[i];
  2490. mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count;
  2491. }
  2492. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  2493. for (i = 0; i < module->data_seg_count; i++) {
  2494. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2495. }
  2496. if (module->const_str_list) {
  2497. StringNode *node = module->const_str_list, *node_next;
  2498. while (node) {
  2499. node_next = node->next;
  2500. mem_conspn->const_strs_size +=
  2501. sizeof(StringNode) + strlen(node->str) + 1;
  2502. node = node_next;
  2503. }
  2504. }
  2505. mem_conspn->total_size += mem_conspn->module_struct_size;
  2506. mem_conspn->total_size += mem_conspn->types_size;
  2507. mem_conspn->total_size += mem_conspn->imports_size;
  2508. mem_conspn->total_size += mem_conspn->functions_size;
  2509. mem_conspn->total_size += mem_conspn->tables_size;
  2510. mem_conspn->total_size += mem_conspn->memories_size;
  2511. mem_conspn->total_size += mem_conspn->globals_size;
  2512. mem_conspn->total_size += mem_conspn->exports_size;
  2513. mem_conspn->total_size += mem_conspn->table_segs_size;
  2514. mem_conspn->total_size += mem_conspn->data_segs_size;
  2515. mem_conspn->total_size += mem_conspn->const_strs_size;
  2516. }
  2517. void
  2518. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2519. WASMModuleInstMemConsumption *mem_conspn)
  2520. {
  2521. uint32 i, size;
  2522. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2523. mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
  2524. - (uint8 *)module_inst
  2525. + sizeof(WASMModuleInstanceExtra);
  2526. mem_conspn->memories_size =
  2527. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  2528. for (i = 0; i < module_inst->memory_count; i++) {
  2529. WASMMemoryInstance *memory = module_inst->memories[i];
  2530. size = memory->num_bytes_per_page * memory->cur_page_count;
  2531. mem_conspn->memories_size += size;
  2532. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  2533. /* size of app heap structure */
  2534. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  2535. /* Module instance structures have been appened into the end of
  2536. module instance */
  2537. }
  2538. mem_conspn->tables_size =
  2539. sizeof(WASMTableInstance *) * module_inst->table_count;
  2540. /* Table instance structures and table elements have been appened into
  2541. the end of module instance */
  2542. mem_conspn->functions_size =
  2543. sizeof(WASMFunctionInstance) * module_inst->e->function_count;
  2544. mem_conspn->globals_size =
  2545. sizeof(WASMGlobalInstance) * module_inst->e->global_count;
  2546. /* Global data has been appened into the end of module instance */
  2547. mem_conspn->exports_size =
  2548. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  2549. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2550. mem_conspn->total_size += mem_conspn->memories_size;
  2551. mem_conspn->total_size += mem_conspn->functions_size;
  2552. mem_conspn->total_size += mem_conspn->tables_size;
  2553. mem_conspn->total_size += mem_conspn->globals_size;
  2554. mem_conspn->total_size += mem_conspn->exports_size;
  2555. }
  2556. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  2557. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2558. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2559. bool
  2560. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
  2561. {
  2562. WASMModuleInstance *module_inst =
  2563. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2564. WASMInterpFrame *first_frame,
  2565. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2566. uint32 n = 0;
  2567. /* count frames includes a function */
  2568. first_frame = cur_frame;
  2569. while (cur_frame) {
  2570. if (cur_frame->function) {
  2571. n++;
  2572. }
  2573. cur_frame = cur_frame->prev_frame;
  2574. }
  2575. /* release previous stack frames and create new ones */
  2576. if (!bh_vector_destroy(module_inst->frames)
  2577. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame),
  2578. false)) {
  2579. return false;
  2580. }
  2581. cur_frame = first_frame;
  2582. n = 0;
  2583. while (cur_frame) {
  2584. WASMCApiFrame frame = { 0 };
  2585. WASMFunctionInstance *func_inst = cur_frame->function;
  2586. const char *func_name = NULL;
  2587. const uint8 *func_code_base = NULL;
  2588. if (!func_inst) {
  2589. cur_frame = cur_frame->prev_frame;
  2590. continue;
  2591. }
  2592. /* place holder, will overwrite it in wasm_c_api */
  2593. frame.instance = module_inst;
  2594. frame.module_offset = 0;
  2595. frame.func_index = (uint32)(func_inst - module_inst->e->functions);
  2596. func_code_base = wasm_get_func_code(func_inst);
  2597. if (!cur_frame->ip || !func_code_base) {
  2598. frame.func_offset = 0;
  2599. }
  2600. else {
  2601. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  2602. }
  2603. /* look for the function name */
  2604. if (func_inst->is_import_func) {
  2605. func_name = func_inst->u.func_import->field_name;
  2606. }
  2607. else {
  2608. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2609. func_name = func_inst->u.func->field_name;
  2610. #endif
  2611. /* if custom name section is not generated,
  2612. search symbols from export table */
  2613. if (!func_name) {
  2614. uint32 i;
  2615. for (i = 0; i < module_inst->export_func_count; i++) {
  2616. WASMExportFuncInstance *export_func =
  2617. module_inst->export_functions + i;
  2618. if (export_func->function == func_inst) {
  2619. func_name = export_func->name;
  2620. break;
  2621. }
  2622. }
  2623. }
  2624. }
  2625. frame.func_name_wp = func_name;
  2626. if (!bh_vector_append(module_inst->frames, &frame)) {
  2627. bh_vector_destroy(module_inst->frames);
  2628. return false;
  2629. }
  2630. cur_frame = cur_frame->prev_frame;
  2631. n++;
  2632. }
  2633. return true;
  2634. }
  2635. #define PRINT_OR_DUMP() \
  2636. do { \
  2637. total_len += \
  2638. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  2639. if ((!print) && buf && (len == 0)) { \
  2640. return total_len; \
  2641. } \
  2642. } while (0)
  2643. uint32
  2644. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  2645. uint32 len)
  2646. {
  2647. WASMModuleInstance *module_inst =
  2648. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2649. uint32 n = 0, total_len = 0, total_frames;
  2650. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  2651. * will be truncated */
  2652. char line_buf[256];
  2653. if (!module_inst->frames) {
  2654. return 0;
  2655. }
  2656. total_frames = (uint32)bh_vector_size(module_inst->frames);
  2657. if (total_frames == 0) {
  2658. return 0;
  2659. }
  2660. snprintf(line_buf, sizeof(line_buf), "\n");
  2661. PRINT_OR_DUMP();
  2662. while (n < total_frames) {
  2663. WASMCApiFrame frame = { 0 };
  2664. uint32 line_length, i;
  2665. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  2666. return 0;
  2667. }
  2668. /* function name not exported, print number instead */
  2669. if (frame.func_name_wp == NULL) {
  2670. line_length =
  2671. snprintf(line_buf, sizeof(line_buf),
  2672. "#%02" PRIu32 " $f%" PRIu32 "\n", n, frame.func_index);
  2673. }
  2674. else {
  2675. line_length =
  2676. snprintf(line_buf, sizeof(line_buf), "#%02" PRIu32 " %s\n", n,
  2677. frame.func_name_wp);
  2678. }
  2679. if (line_length >= sizeof(line_buf)) {
  2680. uint32 line_buffer_len = sizeof(line_buf);
  2681. /* If line too long, ensure the last character is '\n' */
  2682. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  2683. line_buf[i] = '.';
  2684. }
  2685. line_buf[line_buffer_len - 2] = '\n';
  2686. }
  2687. PRINT_OR_DUMP();
  2688. n++;
  2689. }
  2690. snprintf(line_buf, sizeof(line_buf), "\n");
  2691. PRINT_OR_DUMP();
  2692. return total_len + 1;
  2693. }
  2694. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  2695. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2696. || WASM_ENABLE_WAMR_COMPILER != 0
  2697. void
  2698. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2699. {
  2700. if (id != EXCE_ALREADY_THROWN)
  2701. wasm_set_exception_with_id(module_inst, id);
  2702. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2703. wasm_runtime_access_exce_check_guard_page();
  2704. #endif
  2705. }
  2706. bool
  2707. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  2708. uint32 app_buf_addr, uint32 app_buf_size,
  2709. void **p_native_addr)
  2710. {
  2711. bool ret = wasm_check_app_addr_and_convert(
  2712. module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
  2713. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2714. if (!ret)
  2715. wasm_runtime_access_exce_check_guard_page();
  2716. #endif
  2717. return ret;
  2718. }
  2719. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2720. || WASM_ENABLE_WAMR_COMPILER != 0 */
  2721. #if WASM_ENABLE_FAST_JIT != 0
  2722. bool
  2723. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2724. uint32 type_idx, uint32 argc, uint32 *argv)
  2725. {
  2726. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
  2727. type_idx);
  2728. }
  2729. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2730. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  2731. bool
  2732. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2733. uint32 argc, uint32 *argv)
  2734. {
  2735. bool ret;
  2736. #if WASM_ENABLE_JIT != 0
  2737. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  2738. return aot_call_indirect(exec_env, tbl_idx, elem_idx, argc, argv);
  2739. }
  2740. #endif
  2741. ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2742. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2743. if (!ret)
  2744. wasm_runtime_access_exce_check_guard_page();
  2745. #endif
  2746. return ret;
  2747. }
  2748. bool
  2749. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  2750. uint32 *argv)
  2751. {
  2752. WASMModuleInstance *module_inst;
  2753. WASMModule *module;
  2754. uint32 *func_type_indexes;
  2755. uint32 func_type_idx;
  2756. WASMType *func_type;
  2757. void *func_ptr;
  2758. WASMFunctionImport *import_func;
  2759. CApiFuncImport *c_api_func_import = NULL;
  2760. const char *signature;
  2761. void *attachment;
  2762. char buf[96];
  2763. bool ret = false;
  2764. #if WASM_ENABLE_JIT != 0
  2765. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  2766. return aot_invoke_native(exec_env, func_idx, argc, argv);
  2767. }
  2768. #endif
  2769. module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  2770. module = module_inst->module;
  2771. func_type_indexes = module_inst->func_type_indexes;
  2772. func_type_idx = func_type_indexes[func_idx];
  2773. func_type = module->types[func_type_idx];
  2774. func_ptr = module_inst->func_ptrs[func_idx];
  2775. bh_assert(func_idx < module->import_function_count);
  2776. import_func = &module->import_functions[func_idx].u.function;
  2777. if (import_func->call_conv_wasm_c_api) {
  2778. if (module_inst->e->c_api_func_imports) {
  2779. c_api_func_import = module_inst->e->c_api_func_imports + func_idx;
  2780. func_ptr = c_api_func_import->func_ptr_linked;
  2781. }
  2782. else {
  2783. c_api_func_import = NULL;
  2784. func_ptr = NULL;
  2785. }
  2786. }
  2787. if (!func_ptr) {
  2788. snprintf(buf, sizeof(buf),
  2789. "failed to call unlinked import function (%s, %s)",
  2790. import_func->module_name, import_func->field_name);
  2791. wasm_set_exception(module_inst, buf);
  2792. goto fail;
  2793. }
  2794. attachment = import_func->attachment;
  2795. if (import_func->call_conv_wasm_c_api) {
  2796. ret = wasm_runtime_invoke_c_api_native(
  2797. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  2798. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  2799. }
  2800. else if (!import_func->call_conv_raw) {
  2801. signature = import_func->signature;
  2802. ret =
  2803. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  2804. attachment, argv, argc, argv);
  2805. }
  2806. else {
  2807. signature = import_func->signature;
  2808. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  2809. signature, attachment, argv, argc,
  2810. argv);
  2811. }
  2812. fail:
  2813. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2814. if (!ret)
  2815. wasm_runtime_access_exce_check_guard_page();
  2816. #endif
  2817. return ret;
  2818. }
  2819. #if WASM_ENABLE_BULK_MEMORY != 0
  2820. bool
  2821. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  2822. uint32 offset, uint32 len, uint32 dst)
  2823. {
  2824. WASMMemoryInstance *memory_inst;
  2825. WASMModule *module;
  2826. uint8 *data = NULL;
  2827. uint8 *maddr;
  2828. uint64 seg_len = 0;
  2829. #if WASM_ENABLE_JIT != 0
  2830. if (Wasm_Module_AoT == module_inst->module_type) {
  2831. return aot_memory_init(module_inst, seg_index, offset, len, dst);
  2832. }
  2833. #endif
  2834. memory_inst = wasm_get_default_memory(module_inst);
  2835. module = module_inst->module;
  2836. seg_len = module->data_segments[seg_index]->data_length;
  2837. data = module->data_segments[seg_index]->data;
  2838. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  2839. dst, len))
  2840. return false;
  2841. if ((uint64)offset + (uint64)len > seg_len) {
  2842. wasm_set_exception(module_inst, "out of bounds memory access");
  2843. return false;
  2844. }
  2845. maddr = wasm_runtime_addr_app_to_native(
  2846. (WASMModuleInstanceCommon *)module_inst, dst);
  2847. bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len);
  2848. return true;
  2849. }
  2850. bool
  2851. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
  2852. {
  2853. #if WASM_ENABLE_JIT != 0
  2854. if (Wasm_Module_AoT == module_inst->module_type) {
  2855. return aot_data_drop(module_inst, seg_index);
  2856. }
  2857. #endif
  2858. module_inst->module->data_segments[seg_index]->data_length = 0;
  2859. /* Currently we can't free the dropped data segment
  2860. as they are stored in wasm bytecode */
  2861. return true;
  2862. }
  2863. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
  2864. #if WASM_ENABLE_REF_TYPES != 0
  2865. void
  2866. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
  2867. {
  2868. WASMTableSeg *tbl_segs;
  2869. #if WASM_ENABLE_JIT != 0
  2870. if (Wasm_Module_AoT == module_inst->module_type) {
  2871. return aot_drop_table_seg(module_inst, tbl_seg_idx);
  2872. }
  2873. #endif
  2874. tbl_segs = module_inst->module->table_segments;
  2875. tbl_segs[tbl_seg_idx].is_dropped = true;
  2876. }
  2877. void
  2878. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2879. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  2880. uint32 dst_offset)
  2881. {
  2882. WASMTableInstance *tbl_inst;
  2883. WASMTableSeg *tbl_seg;
  2884. #if WASM_ENABLE_JIT != 0
  2885. if (Wasm_Module_AoT == module_inst->module_type) {
  2886. return aot_table_init(module_inst, tbl_idx, tbl_seg_idx, length,
  2887. src_offset, dst_offset);
  2888. }
  2889. #endif
  2890. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2891. tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
  2892. bh_assert(tbl_inst);
  2893. bh_assert(tbl_seg);
  2894. if (!length) {
  2895. return;
  2896. }
  2897. if (length + src_offset > tbl_seg->function_count
  2898. || dst_offset + length > tbl_inst->cur_size) {
  2899. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2900. return;
  2901. }
  2902. if (tbl_seg->is_dropped) {
  2903. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2904. return;
  2905. }
  2906. if (!wasm_elem_is_passive(tbl_seg->mode)) {
  2907. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2908. return;
  2909. }
  2910. bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems)
  2911. + dst_offset * sizeof(uint32),
  2912. (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset),
  2913. tbl_seg->func_indexes + src_offset,
  2914. (uint32)(length * sizeof(uint32)));
  2915. }
  2916. void
  2917. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  2918. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  2919. uint32 dst_offset)
  2920. {
  2921. WASMTableInstance *src_tbl_inst;
  2922. WASMTableInstance *dst_tbl_inst;
  2923. #if WASM_ENABLE_JIT != 0
  2924. if (Wasm_Module_AoT == module_inst->module_type) {
  2925. aot_table_copy(module_inst, src_tbl_idx, dst_tbl_idx, length,
  2926. src_offset, dst_offset);
  2927. return;
  2928. }
  2929. #endif
  2930. src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
  2931. dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
  2932. bh_assert(src_tbl_inst);
  2933. bh_assert(dst_tbl_inst);
  2934. if ((uint64)dst_offset + length > dst_tbl_inst->cur_size
  2935. || (uint64)src_offset + length > src_tbl_inst->cur_size) {
  2936. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2937. return;
  2938. }
  2939. /* if src_offset >= dst_offset, copy from front to back */
  2940. /* if src_offset < dst_offset, copy from back to front */
  2941. /* merge all together */
  2942. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
  2943. + sizeof(uint32) * dst_offset,
  2944. (uint32)sizeof(uint32) * (dst_tbl_inst->cur_size - dst_offset),
  2945. (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
  2946. + sizeof(uint32) * src_offset,
  2947. (uint32)sizeof(uint32) * length);
  2948. }
  2949. void
  2950. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2951. uint32 length, uint32 val, uint32 data_offset)
  2952. {
  2953. WASMTableInstance *tbl_inst;
  2954. #if WASM_ENABLE_JIT != 0
  2955. if (Wasm_Module_AoT == module_inst->module_type) {
  2956. aot_table_fill(module_inst, tbl_idx, length, val, data_offset);
  2957. return;
  2958. }
  2959. #endif
  2960. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2961. bh_assert(tbl_inst);
  2962. if (data_offset + length > tbl_inst->cur_size) {
  2963. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2964. return;
  2965. }
  2966. for (; length != 0; data_offset++, length--) {
  2967. tbl_inst->elems[data_offset] = val;
  2968. }
  2969. }
  2970. uint32
  2971. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2972. uint32 inc_size, uint32 init_val)
  2973. {
  2974. WASMTableInstance *tbl_inst;
  2975. uint32 i, orig_size, total_size;
  2976. #if WASM_ENABLE_JIT != 0
  2977. if (Wasm_Module_AoT == module_inst->module_type) {
  2978. return aot_table_grow(module_inst, tbl_idx, inc_size, init_val);
  2979. }
  2980. #endif
  2981. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2982. if (!tbl_inst) {
  2983. return (uint32)-1;
  2984. }
  2985. orig_size = tbl_inst->cur_size;
  2986. if (!inc_size) {
  2987. return orig_size;
  2988. }
  2989. if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
  2990. return (uint32)-1;
  2991. }
  2992. total_size = tbl_inst->cur_size + inc_size;
  2993. if (total_size > tbl_inst->max_size) {
  2994. return (uint32)-1;
  2995. }
  2996. /* fill in */
  2997. for (i = 0; i < inc_size; ++i) {
  2998. tbl_inst->elems[tbl_inst->cur_size + i] = init_val;
  2999. }
  3000. tbl_inst->cur_size = total_size;
  3001. return orig_size;
  3002. }
  3003. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  3004. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  3005. bool
  3006. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  3007. {
  3008. WASMModuleInstance *module_inst;
  3009. WASMInterpFrame *frame;
  3010. uint32 size;
  3011. #if WASM_ENABLE_JIT != 0
  3012. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  3013. return aot_alloc_frame(exec_env, func_index);
  3014. }
  3015. #endif
  3016. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  3017. size = wasm_interp_interp_frame_size(0);
  3018. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  3019. if (!frame) {
  3020. wasm_set_exception(module_inst, "wasm operand stack overflow");
  3021. return false;
  3022. }
  3023. frame->function = module_inst->e->functions + func_index;
  3024. frame->ip = NULL;
  3025. frame->sp = frame->lp;
  3026. #if WASM_ENABLE_PERF_PROFILING != 0
  3027. frame->time_started = os_time_get_boot_microsecond();
  3028. #endif
  3029. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3030. wasm_exec_env_set_cur_frame(exec_env, frame);
  3031. return true;
  3032. }
  3033. void
  3034. llvm_jit_free_frame(WASMExecEnv *exec_env)
  3035. {
  3036. WASMInterpFrame *frame;
  3037. WASMInterpFrame *prev_frame;
  3038. #if WASM_ENABLE_JIT != 0
  3039. if (Wasm_Module_AoT == exec_env->module_inst->module_type) {
  3040. aot_free_frame(exec_env);
  3041. return;
  3042. }
  3043. #endif
  3044. frame = wasm_exec_env_get_cur_frame(exec_env);
  3045. prev_frame = frame->prev_frame;
  3046. #if WASM_ENABLE_PERF_PROFILING != 0
  3047. if (frame->function) {
  3048. frame->function->total_exec_time +=
  3049. os_time_get_boot_microsecond() - frame->time_started;
  3050. frame->function->total_exec_cnt++;
  3051. }
  3052. #endif
  3053. wasm_exec_env_free_wasm_frame(exec_env, frame);
  3054. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3055. }
  3056. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  3057. || WASM_ENABLE_PERF_PROFILING != 0 */
  3058. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  3059. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  3060. void
  3061. wasm_propagate_wasi_args(WASMModule *module)
  3062. {
  3063. if (!module->import_count)
  3064. return;
  3065. bh_assert(&module->import_module_list_head);
  3066. WASMRegisteredModule *node =
  3067. bh_list_first_elem(&module->import_module_list_head);
  3068. while (node) {
  3069. WASIArguments *wasi_args_impt_mod =
  3070. &((WASMModule *)(node->module))->wasi_args;
  3071. bh_assert(wasi_args_impt_mod);
  3072. bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
  3073. &module->wasi_args, sizeof(WASIArguments));
  3074. node = bh_list_elem_next(node);
  3075. }
  3076. }
  3077. #endif