aot_runtime.c 116 KB

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