wasm_runtime.c 121 KB

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