wasm_runtime.c 119 KB

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