wasm_runtime.c 156 KB

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