aot_runtime.c 117 KB

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