wasm_runtime.c 107 KB

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