wasm_runtime.c 127 KB

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