wasm_runtime.c 105 KB

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