wasm_runtime.c 113 KB

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