aot_runtime.c 101 KB

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