wasm_runtime.c 150 KB

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