aot_runtime.c 102 KB

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