aot_runtime.c 104 KB

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