wasm_runtime.c 104 KB

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