aot_emit_memory.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_memory.h"
  6. #include "aot_compiler.h"
  7. #include "aot_emit_exception.h"
  8. #include "../aot/aot_runtime.h"
  9. #include "aot_intrinsic.h"
  10. #include "aot_emit_control.h"
  11. #define BUILD_IS_NOT_NULL(value, res, name) \
  12. do { \
  13. if (!(res = LLVMBuildIsNotNull(comp_ctx->builder, value, name))) { \
  14. aot_set_last_error("llvm build is not null failed."); \
  15. goto fail; \
  16. } \
  17. } while (0)
  18. #define BUILD_BR(llvm_block) \
  19. do { \
  20. if (!LLVMBuildBr(comp_ctx->builder, llvm_block)) { \
  21. aot_set_last_error("llvm build br failed."); \
  22. goto fail; \
  23. } \
  24. } while (0)
  25. #define BUILD_COND_BR(value_if, block_then, block_else) \
  26. do { \
  27. if (!LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
  28. block_else)) { \
  29. aot_set_last_error("llvm build cond br failed."); \
  30. goto fail; \
  31. } \
  32. } while (0)
  33. #define BUILD_TRUNC(value, data_type) \
  34. do { \
  35. if (!(value = LLVMBuildTrunc(comp_ctx->builder, value, data_type, \
  36. "val_trunc"))) { \
  37. aot_set_last_error("llvm build trunc failed."); \
  38. goto fail; \
  39. } \
  40. } while (0)
  41. #define BUILD_ICMP(op, left, right, res, name) \
  42. do { \
  43. if (!(res = \
  44. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  45. aot_set_last_error("llvm build icmp failed."); \
  46. goto fail; \
  47. } \
  48. } while (0)
  49. #define BUILD_OP(Op, left, right, res, name) \
  50. do { \
  51. if (!(res = LLVMBuild##Op(comp_ctx->builder, left, right, name))) { \
  52. aot_set_last_error("llvm build " #Op " fail."); \
  53. goto fail; \
  54. } \
  55. } while (0)
  56. #define ADD_BASIC_BLOCK(block, name) \
  57. do { \
  58. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  59. func_ctx->func, name))) { \
  60. aot_set_last_error("llvm add basic block failed."); \
  61. goto fail; \
  62. } \
  63. } while (0)
  64. #define SET_BUILD_POS(block) LLVMPositionBuilderAtEnd(comp_ctx->builder, block)
  65. static bool
  66. zero_extend_u64(AOTCompContext *comp_ctx, LLVMValueRef *value, const char *name)
  67. {
  68. if (comp_ctx->pointer_size == sizeof(uint64)) {
  69. /* zero extend to uint64 if the target is 64-bit */
  70. *value = LLVMBuildZExt(comp_ctx->builder, *value, I64_TYPE, name);
  71. if (!*value) {
  72. aot_set_last_error("llvm build zero extend failed.");
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. static LLVMValueRef
  79. get_memory_check_bound(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  80. uint32 bytes)
  81. {
  82. LLVMValueRef mem_check_bound = NULL;
  83. switch (bytes) {
  84. case 1:
  85. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_1byte;
  86. break;
  87. case 2:
  88. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_2bytes;
  89. break;
  90. case 4:
  91. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_4bytes;
  92. break;
  93. case 8:
  94. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_8bytes;
  95. break;
  96. case 16:
  97. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_16bytes;
  98. break;
  99. default:
  100. bh_assert(0);
  101. return NULL;
  102. }
  103. if (func_ctx->mem_space_unchanged)
  104. return mem_check_bound;
  105. if (!(mem_check_bound = LLVMBuildLoad2(
  106. comp_ctx->builder,
  107. (comp_ctx->pointer_size == sizeof(uint64)) ? I64_TYPE : I32_TYPE,
  108. mem_check_bound, "mem_check_bound"))) {
  109. aot_set_last_error("llvm build load failed.");
  110. return NULL;
  111. }
  112. return mem_check_bound;
  113. }
  114. #if defined(_WIN32) || defined(_WIN32_)
  115. static inline int
  116. ffs(int n)
  117. {
  118. int pos = 0;
  119. if (n == 0)
  120. return 0;
  121. while (!(n & 1)) {
  122. pos++;
  123. n >>= 1;
  124. }
  125. return pos + 1;
  126. }
  127. #endif
  128. static LLVMValueRef
  129. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
  130. #if WASM_ENABLE_SHARED_HEAP != 0
  131. uint32
  132. get_module_inst_extra_offset(AOTCompContext *comp_ctx);
  133. #define BUILD_LOAD_PTR(ptr, data_type, res) \
  134. do { \
  135. if (!(res = LLVMBuildLoad2(comp_ctx->builder, data_type, ptr, \
  136. "load_value"))) { \
  137. aot_set_last_error("llvm build load failed"); \
  138. goto fail; \
  139. } \
  140. } while (0)
  141. /* Update last used shared heap info(alloc ptr) in function ctx:
  142. * 1. shared_heap_start_off 2. shared_heap_end_off 3. shared_heap_base_addr_adj
  143. */
  144. bool
  145. aot_check_shared_heap_chain_and_update(AOTCompContext *comp_ctx,
  146. AOTFuncContext *func_ctx,
  147. LLVMBasicBlockRef check_succ,
  148. LLVMValueRef start_offset,
  149. LLVMValueRef bytes, bool is_memory64)
  150. {
  151. LLVMValueRef param_values[7], ret_value, func, value, cmp;
  152. LLVMTypeRef param_types[7], ret_type, func_type, func_ptr_type;
  153. param_types[0] = INT8_PTR_TYPE;
  154. param_types[1] = INTPTR_T_TYPE;
  155. param_types[2] = SIZE_T_TYPE;
  156. param_types[3] = INTPTR_T_PTR_TYPE;
  157. param_types[4] = INTPTR_T_PTR_TYPE;
  158. param_types[5] = INT8_PTR_TYPE;
  159. param_types[6] = INT8_TYPE;
  160. ret_type = INT8_TYPE;
  161. GET_AOT_FUNCTION(wasm_runtime_check_and_update_last_used_shared_heap, 7);
  162. /* Call function */
  163. param_values[0] = func_ctx->aot_inst;
  164. param_values[1] = start_offset;
  165. param_values[2] = bytes;
  166. /* pass alloc ptr */
  167. param_values[3] = func_ctx->shared_heap_start_off;
  168. param_values[4] = func_ctx->shared_heap_end_off;
  169. param_values[5] = func_ctx->shared_heap_base_addr_adj;
  170. param_values[6] = is_memory64 ? I8_ONE : I8_ZERO;
  171. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  172. param_values, 7, "call"))) {
  173. aot_set_last_error("llvm build call failed.");
  174. goto fail;
  175. }
  176. BUILD_ICMP(LLVMIntEQ, ret_value, I8_ZERO, cmp, "shared_heap_oob");
  177. if (!aot_emit_exception(comp_ctx, func_ctx,
  178. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  179. check_succ)) {
  180. goto fail;
  181. }
  182. return true;
  183. fail:
  184. return false;
  185. }
  186. /*
  187. * Setup the basic blocks for shared heap and shared chain memory checks.
  188. *
  189. * Arguments:
  190. * block_curr: The current basic block.
  191. * app_addr_in_cache_shared_heap: Output, block for cache shared heap.
  192. * app_addr_in_linear_mem: Output, block for linear memory.
  193. * app_addr_in_shared_heap_chain: Output, block for shared heap chain
  194. * (only for shared heap chain).
  195. * check_shared_heap_chain: Output, block for checking shared heap chain
  196. * (only for shared heap chain).
  197. *
  198. * Topology:
  199. * If enable_shared_heap:
  200. * block_curr -> app_addr_in_cache_shared_heap
  201. * -> app_addr_in_linear_mem
  202. * If enable_shared_chain:
  203. * block_curr -> app_addr_in_shared_heap_chain
  204. * -> app_addr_in_cache_shared_heap
  205. * -> check_shared_heap_chain
  206. * -> app_addr_in_linear_mem
  207. */
  208. static bool
  209. setup_shared_heap_blocks(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  210. LLVMBasicBlockRef block_curr,
  211. LLVMBasicBlockRef *app_addr_in_cache_shared_heap,
  212. LLVMBasicBlockRef *app_addr_in_linear_mem,
  213. LLVMBasicBlockRef *app_addr_in_shared_heap_chain,
  214. LLVMBasicBlockRef *check_shared_heap_chain)
  215. {
  216. ADD_BASIC_BLOCK(*app_addr_in_cache_shared_heap,
  217. "app_addr_in_cache_shared_heap");
  218. ADD_BASIC_BLOCK(*app_addr_in_linear_mem, "app_addr_in_linear_mem");
  219. if (comp_ctx->enable_shared_heap) {
  220. LLVMMoveBasicBlockAfter(*app_addr_in_cache_shared_heap, block_curr);
  221. LLVMMoveBasicBlockAfter(*app_addr_in_linear_mem,
  222. *app_addr_in_cache_shared_heap);
  223. }
  224. else if (comp_ctx->enable_shared_chain) {
  225. ADD_BASIC_BLOCK(*app_addr_in_shared_heap_chain,
  226. "app_addr_in_shared_heap_chain");
  227. ADD_BASIC_BLOCK(*check_shared_heap_chain, "check_shared_heap_chain");
  228. LLVMMoveBasicBlockAfter(*app_addr_in_shared_heap_chain, block_curr);
  229. LLVMMoveBasicBlockAfter(*app_addr_in_cache_shared_heap,
  230. *app_addr_in_shared_heap_chain);
  231. LLVMMoveBasicBlockAfter(*check_shared_heap_chain,
  232. *app_addr_in_cache_shared_heap);
  233. LLVMMoveBasicBlockAfter(*app_addr_in_linear_mem,
  234. *app_addr_in_cache_shared_heap);
  235. }
  236. return true;
  237. fail:
  238. return false;
  239. }
  240. /*
  241. * Build a branch to check if start_offset is in the shared heap chain region.
  242. *
  243. * Arguments:
  244. * start_offset: The offset to check.
  245. * app_addr_in_shared_heap_chain: Block to branch if in shared heap chain.
  246. * app_addr_in_linear_mem: Block to branch if not in shared heap chain.
  247. */
  248. static bool
  249. build_check_app_addr_in_shared_heap_chain(
  250. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  251. LLVMValueRef start_offset, LLVMBasicBlockRef app_addr_in_shared_heap_chain,
  252. LLVMBasicBlockRef app_addr_in_linear_mem)
  253. {
  254. LLVMValueRef is_in_shared_heap = NULL;
  255. /* Use start_offset > func_ctx->shared_heap_head_start_off to test
  256. * start_off falls in shared heap chain memory region. The shared heap
  257. * chain oob will be detected in app_addr_in_shared_heap block or
  258. * aot_check_shared_heap_chain_and_update function
  259. */
  260. BUILD_ICMP(LLVMIntUGT, start_offset, func_ctx->shared_heap_head_start_off,
  261. is_in_shared_heap, "shared_heap_lb_cmp");
  262. BUILD_COND_BR(is_in_shared_heap, app_addr_in_shared_heap_chain,
  263. app_addr_in_linear_mem);
  264. SET_BUILD_POS(app_addr_in_shared_heap_chain);
  265. return true;
  266. fail:
  267. return false;
  268. }
  269. /*
  270. * Build the conditional branch for cache shared heap or shared heap chain.
  271. *
  272. * Arguments:
  273. * cmp: The condition for being in cache shared heap.
  274. * app_addr_in_cache_shared_heap: Block for cache shared heap.
  275. * app_addr_in_linear_mem: Block for linear memory.
  276. * check_shared_heap_chain: Block for checking shared heap chain.
  277. * bytes: The access size in bytes.
  278. * start_offset: The offset to check.
  279. * is_memory64: Whether memory is 64-bit.
  280. */
  281. static bool
  282. build_shared_heap_conditional_branching(
  283. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVMValueRef cmp,
  284. LLVMBasicBlockRef app_addr_in_cache_shared_heap,
  285. LLVMBasicBlockRef app_addr_in_linear_mem,
  286. LLVMBasicBlockRef check_shared_heap_chain, LLVMValueRef bytes,
  287. LLVMValueRef start_offset, bool is_memory64)
  288. {
  289. if (comp_ctx->enable_shared_heap) {
  290. BUILD_COND_BR(cmp, app_addr_in_cache_shared_heap,
  291. app_addr_in_linear_mem);
  292. }
  293. else if (comp_ctx->enable_shared_chain) {
  294. BUILD_COND_BR(cmp, app_addr_in_cache_shared_heap,
  295. check_shared_heap_chain);
  296. SET_BUILD_POS(check_shared_heap_chain);
  297. if (!aot_check_shared_heap_chain_and_update(
  298. comp_ctx, func_ctx, app_addr_in_cache_shared_heap, start_offset,
  299. bytes, is_memory64))
  300. goto fail;
  301. }
  302. return true;
  303. fail:
  304. return false;
  305. }
  306. /*
  307. * Get the native address in the cache shared heap.
  308. *
  309. * Arguments:
  310. * start_offset: The offset to use for address calculation.
  311. * maddr: Output, the native address that in the cache shared heap.
  312. */
  313. static bool
  314. build_get_maddr_in_cache_shared_heap(AOTCompContext *comp_ctx,
  315. AOTFuncContext *func_ctx,
  316. LLVMValueRef start_offset,
  317. LLVMValueRef *maddr)
  318. {
  319. LLVMValueRef shared_heap_base_addr_adj;
  320. /* load the local variable */
  321. BUILD_LOAD_PTR(func_ctx->shared_heap_base_addr_adj, INT8_PTR_TYPE,
  322. shared_heap_base_addr_adj);
  323. if (!(*maddr = LLVMBuildInBoundsGEP2(
  324. comp_ctx->builder, INT8_TYPE, shared_heap_base_addr_adj,
  325. &start_offset, 1, "maddr_cache_shared_heap"))) {
  326. aot_set_last_error("llvm build inbounds gep failed");
  327. goto fail;
  328. }
  329. return true;
  330. fail:
  331. return false;
  332. }
  333. /*
  334. * Check for memory overflow in shared heap for normal memory access.
  335. *
  336. * Arguments:
  337. * block_curr: The current basic block.
  338. * block_maddr_phi: The phi block for memory address.
  339. * maddr_phi: The phi node for memory address.
  340. * start_offset: The first offset to check.
  341. * mem_base_addr: The base address of memory. Only used with segue.
  342. * bytes_u32: The access size in bytes.
  343. * is_memory64: Whether memory is wasm64 memory.
  344. * is_target_64bit: Whether target is 64-bit.
  345. * enable_segue: Whether to use segment register addressing.
  346. */
  347. static bool
  348. aot_check_shared_heap_memory_overflow(
  349. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  350. LLVMBasicBlockRef block_curr, LLVMBasicBlockRef block_maddr_phi,
  351. LLVMValueRef maddr_phi, LLVMValueRef start_offset,
  352. LLVMValueRef mem_base_addr, uint32 bytes_u32, bool is_memory64,
  353. bool is_target_64bit, bool enable_segue)
  354. {
  355. LLVMBasicBlockRef app_addr_in_cache_shared_heap, app_addr_in_linear_mem;
  356. LLVMBasicBlockRef app_addr_in_shared_heap_chain = NULL,
  357. check_shared_heap_chain = NULL;
  358. LLVMValueRef cmp, cmp1, cmp2, shared_heap_start_off, shared_heap_end_off,
  359. shared_heap_check_bound, maddr = NULL;
  360. /* On 64/32-bit target, the offset is 64/32-bit */
  361. LLVMTypeRef offset_type = is_target_64bit ? I64_TYPE : I32_TYPE;
  362. LLVMValueRef length, bytes;
  363. if (!setup_shared_heap_blocks(
  364. comp_ctx, func_ctx, block_curr, &app_addr_in_cache_shared_heap,
  365. &app_addr_in_linear_mem, &app_addr_in_shared_heap_chain,
  366. &check_shared_heap_chain))
  367. goto fail;
  368. LLVMMoveBasicBlockAfter(block_maddr_phi, app_addr_in_linear_mem);
  369. /* Early branching when it's not in shared heap chain at all */
  370. if (comp_ctx->enable_shared_chain
  371. && !build_check_app_addr_in_shared_heap_chain(
  372. comp_ctx, func_ctx, start_offset, app_addr_in_shared_heap_chain,
  373. app_addr_in_linear_mem))
  374. goto fail;
  375. /* Load the local variable of the function */
  376. BUILD_LOAD_PTR(func_ctx->shared_heap_start_off, offset_type,
  377. shared_heap_start_off);
  378. BUILD_LOAD_PTR(func_ctx->shared_heap_end_off, offset_type,
  379. shared_heap_end_off);
  380. /* Check if the app address is in the cache shared heap range.
  381. * If yes, branch to the cache branch; if not, check the shared heap chain
  382. */
  383. BUILD_ICMP(LLVMIntUGE, start_offset, shared_heap_start_off, cmp,
  384. "cmp_cache_shared_heap_start");
  385. length =
  386. is_target_64bit ? I64_CONST(bytes_u32 - 1) : I32_CONST(bytes_u32 - 1);
  387. CHECK_LLVM_CONST(length);
  388. BUILD_OP(Sub, shared_heap_end_off, length, shared_heap_check_bound,
  389. "cache_shared_heap_end_bound");
  390. BUILD_ICMP(LLVMIntULE, start_offset, shared_heap_check_bound, cmp1,
  391. "cmp_cache_shared_heap_end");
  392. BUILD_OP(And, cmp, cmp1, cmp2, "is_in_cache_shared_heap");
  393. /* Conditional branching based on whether in cached shared heap */
  394. bytes = is_target_64bit ? I64_CONST(bytes_u32) : I32_CONST(bytes_u32);
  395. if (!build_shared_heap_conditional_branching(
  396. comp_ctx, func_ctx, cmp2, app_addr_in_cache_shared_heap,
  397. app_addr_in_linear_mem, check_shared_heap_chain, bytes,
  398. start_offset, is_memory64))
  399. goto fail;
  400. SET_BUILD_POS(app_addr_in_cache_shared_heap);
  401. if (!build_get_maddr_in_cache_shared_heap(comp_ctx, func_ctx, start_offset,
  402. &maddr))
  403. goto fail;
  404. if (enable_segue) {
  405. LLVMValueRef mem_base_addr_u64, maddr_u64, offset_to_mem_base;
  406. if (!(maddr_u64 = LLVMBuildPtrToInt(comp_ctx->builder, maddr, I64_TYPE,
  407. "maddr_u64"))
  408. || !(mem_base_addr_u64 =
  409. LLVMBuildPtrToInt(comp_ctx->builder, mem_base_addr,
  410. I64_TYPE, "mem_base_addr_u64"))) {
  411. aot_set_last_error("llvm build ptr to int failed");
  412. goto fail;
  413. }
  414. if (!(offset_to_mem_base =
  415. LLVMBuildSub(comp_ctx->builder, maddr_u64, mem_base_addr_u64,
  416. "offset_to_mem_base"))) {
  417. aot_set_last_error("llvm build sub failed");
  418. goto fail;
  419. }
  420. if (!(maddr = LLVMBuildIntToPtr(comp_ctx->builder, offset_to_mem_base,
  421. INT8_PTR_TYPE_GS,
  422. "maddr_shared_heap_segue"))) {
  423. aot_set_last_error("llvm build int to ptr failed.");
  424. goto fail;
  425. }
  426. }
  427. LLVMAddIncoming(maddr_phi, &maddr, &app_addr_in_cache_shared_heap, 1);
  428. BUILD_BR(block_maddr_phi);
  429. SET_BUILD_POS(app_addr_in_linear_mem);
  430. return true;
  431. fail:
  432. return false;
  433. }
  434. /*
  435. * Check for memory overflow in shared heap for bulk memory access.
  436. *
  437. * Arguments:
  438. * block_curr: The current basic block.
  439. * block_maddr_phi: The phi block for memory address.
  440. * check_succ: The block to branch to on success.
  441. * maddr_phi: The phi node for memory address.
  442. * start_offset: The offset to check.
  443. * max_addr: The maximum address to check.
  444. * bytes: The access size in bytes (LLVMValueRef).
  445. * is_memory64: Whether memory is wasm64 memory.
  446. * is_target_64bit: Whether target is 64-bit.
  447. */
  448. static bool
  449. aot_check_bulk_memory_shared_heap_memory_overflow(
  450. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  451. LLVMBasicBlockRef block_curr, LLVMBasicBlockRef block_maddr_phi,
  452. LLVMBasicBlockRef check_succ, LLVMValueRef maddr_phi,
  453. LLVMValueRef start_offset, LLVMValueRef max_addr, LLVMValueRef bytes,
  454. bool is_memory64, bool is_target_64bit)
  455. {
  456. LLVMBasicBlockRef app_addr_in_cache_shared_heap, app_addr_in_linear_mem;
  457. LLVMBasicBlockRef app_addr_in_shared_heap_chain = NULL,
  458. check_shared_heap_chain = NULL;
  459. LLVMValueRef cmp, cmp1, cmp2, shared_heap_start_off, shared_heap_end_off,
  460. maddr = NULL, max_offset;
  461. /* On 64/32-bit target, the offset is 64/32-bit */
  462. LLVMTypeRef offset_type = is_target_64bit ? I64_TYPE : I32_TYPE;
  463. if (!setup_shared_heap_blocks(
  464. comp_ctx, func_ctx, block_curr, &app_addr_in_cache_shared_heap,
  465. &app_addr_in_linear_mem, &app_addr_in_shared_heap_chain,
  466. &check_shared_heap_chain))
  467. goto fail;
  468. LLVMMoveBasicBlockAfter(block_maddr_phi, check_succ);
  469. /* Early branching when it's not in shared heap chain at all */
  470. if (comp_ctx->enable_shared_chain
  471. && !build_check_app_addr_in_shared_heap_chain(
  472. comp_ctx, func_ctx, start_offset, app_addr_in_shared_heap_chain,
  473. app_addr_in_linear_mem))
  474. goto fail;
  475. /* Load the local variable of the function */
  476. BUILD_LOAD_PTR(func_ctx->shared_heap_start_off, offset_type,
  477. shared_heap_start_off);
  478. BUILD_LOAD_PTR(func_ctx->shared_heap_end_off, offset_type,
  479. shared_heap_end_off);
  480. /* Check if the app address is in the cache shared heap range.
  481. * If yes, branch to the cache branch; if not, check the shared heap chain
  482. */
  483. BUILD_ICMP(LLVMIntUGE, start_offset, shared_heap_start_off, cmp,
  484. "cmp_cache_shared_heap_start");
  485. BUILD_OP(Add, max_addr, is_target_64bit ? I64_NEG_ONE : I32_NEG_ONE,
  486. max_offset, "max_offset");
  487. BUILD_ICMP(LLVMIntULE, max_offset, shared_heap_end_off, cmp1,
  488. "cmp_cache_shared_heap_end");
  489. BUILD_OP(And, cmp, cmp1, cmp2, "is_in_cache_shared_heap");
  490. /* Conditional branching based on whether in cached shared heap */
  491. if (!build_shared_heap_conditional_branching(
  492. comp_ctx, func_ctx, cmp2, app_addr_in_cache_shared_heap,
  493. app_addr_in_linear_mem, check_shared_heap_chain, bytes,
  494. start_offset, is_memory64))
  495. goto fail;
  496. SET_BUILD_POS(app_addr_in_cache_shared_heap);
  497. if (!build_get_maddr_in_cache_shared_heap(comp_ctx, func_ctx, start_offset,
  498. &maddr))
  499. goto fail;
  500. LLVMAddIncoming(maddr_phi, &maddr, &app_addr_in_cache_shared_heap, 1);
  501. BUILD_BR(block_maddr_phi);
  502. SET_BUILD_POS(app_addr_in_linear_mem);
  503. return true;
  504. fail:
  505. return false;
  506. }
  507. #endif
  508. LLVMValueRef
  509. aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  510. mem_offset_t offset, uint32 bytes, bool enable_segue,
  511. unsigned int *alignp)
  512. {
  513. LLVMValueRef offset_const =
  514. MEMORY64_COND_VALUE(I64_CONST(offset), I32_CONST(offset));
  515. LLVMValueRef addr, maddr, offset1, cmp1, cmp;
  516. LLVMValueRef mem_base_addr, mem_check_bound;
  517. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  518. LLVMBasicBlockRef check_succ;
  519. AOTValue *aot_value_top;
  520. uint32 local_idx_of_aot_value = 0;
  521. uint64 const_value;
  522. bool is_target_64bit, is_local_of_aot_value = false;
  523. bool is_const = false;
  524. #if WASM_ENABLE_SHARED_MEMORY != 0
  525. bool is_shared_memory =
  526. comp_ctx->comp_data->memories[0].flags & SHARED_MEMORY_FLAG;
  527. #endif
  528. #if WASM_ENABLE_MEMORY64 == 0
  529. bool is_memory64 = false;
  530. #else
  531. bool is_memory64 = IS_MEMORY64;
  532. #endif
  533. #if WASM_ENABLE_SHARED_HEAP != 0
  534. LLVMValueRef maddr_phi = NULL;
  535. LLVMBasicBlockRef block_maddr_phi = NULL;
  536. #endif
  537. is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  538. if (comp_ctx->is_indirect_mode
  539. && aot_intrinsic_check_capability(
  540. comp_ctx, MEMORY64_COND_VALUE("i64.const", "i32.const"))) {
  541. WASMValue wasm_value;
  542. #if WASM_ENABLE_MEMORY64 != 0
  543. if (IS_MEMORY64) {
  544. wasm_value.i64 = offset;
  545. }
  546. else
  547. #endif
  548. {
  549. wasm_value.i32 = (int32)offset;
  550. }
  551. offset_const = aot_load_const_from_table(
  552. comp_ctx, func_ctx->native_symbol, &wasm_value,
  553. MEMORY64_COND_VALUE(VALUE_TYPE_I64, VALUE_TYPE_I32));
  554. if (!offset_const) {
  555. return NULL;
  556. }
  557. }
  558. else {
  559. CHECK_LLVM_CONST(offset_const);
  560. }
  561. /* Get memory base address and memory data size */
  562. if (func_ctx->mem_space_unchanged
  563. #if WASM_ENABLE_SHARED_MEMORY != 0
  564. || is_shared_memory
  565. #endif
  566. ) {
  567. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  568. }
  569. else {
  570. if (!(mem_base_addr = LLVMBuildLoad2(
  571. comp_ctx->builder, OPQ_PTR_TYPE,
  572. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  573. aot_set_last_error("llvm build load failed.");
  574. goto fail;
  575. }
  576. }
  577. aot_value_top =
  578. func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  579. if (aot_value_top) {
  580. /* aot_value_top is freed in the following POP_I32(addr),
  581. so save its fields here for further use */
  582. is_local_of_aot_value = aot_value_top->is_local;
  583. is_const = aot_value_top->is_const;
  584. local_idx_of_aot_value = aot_value_top->local_idx;
  585. const_value = aot_value_top->const_value;
  586. }
  587. POP_MEM_OFFSET(addr);
  588. /*
  589. * Note: not throw the integer-overflow-exception here since it must
  590. * have been thrown when converting float to integer before
  591. */
  592. /* return address directly if constant offset and inside memory space */
  593. if (LLVMIsEfficientConstInt(addr) || is_const) {
  594. uint64 value;
  595. if (LLVMIsEfficientConstInt(addr)) {
  596. value = (uint64)LLVMConstIntGetZExtValue(addr);
  597. }
  598. else {
  599. value = const_value;
  600. }
  601. uint64 mem_offset = value + (uint64)offset;
  602. uint32 num_bytes_per_page =
  603. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  604. uint32 init_page_count =
  605. comp_ctx->comp_data->memories[0].init_page_count;
  606. uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
  607. if (alignp != NULL) {
  608. /*
  609. * A note about max_align below:
  610. * the assumption here is the base address of a linear memory
  611. * has the natural alignment. for platforms using mmap, it can
  612. * be even larger. for now, use a conservative value.
  613. */
  614. const unsigned int max_align = 8;
  615. int shift = ffs((int)(unsigned int)mem_offset);
  616. if (shift == 0) {
  617. *alignp = max_align;
  618. }
  619. else {
  620. unsigned int align = 1 << (shift - 1);
  621. if (align > max_align) {
  622. align = max_align;
  623. }
  624. *alignp = align;
  625. }
  626. }
  627. if (mem_offset + bytes <= mem_data_size) {
  628. /* inside memory space */
  629. if (comp_ctx->pointer_size == sizeof(uint64))
  630. offset1 = I64_CONST(mem_offset);
  631. else
  632. offset1 = I32_CONST((uint32)mem_offset);
  633. CHECK_LLVM_CONST(offset1);
  634. if (!enable_segue) {
  635. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  636. INT8_TYPE, mem_base_addr,
  637. &offset1, 1, "maddr"))) {
  638. aot_set_last_error("llvm build add failed.");
  639. goto fail;
  640. }
  641. }
  642. else {
  643. if (!(maddr = LLVMBuildIntToPtr(comp_ctx->builder, offset1,
  644. INT8_PTR_TYPE_GS, "maddr"))) {
  645. aot_set_last_error("llvm build IntToPtr failed.");
  646. goto fail;
  647. }
  648. }
  649. return maddr;
  650. }
  651. }
  652. else if (alignp != NULL) {
  653. *alignp = 1;
  654. }
  655. /* The overflow check needs to be done under following conditions:
  656. * 1. In 64-bit target, offset and addr will be extended to 64-bit
  657. * 1.1 offset + addr can overflow when it's memory64
  658. * 1.2 no overflow when it's memory32
  659. * 2. In 32-bit target, offset and addr will be 32-bit
  660. * 2.1 offset + addr can overflow when it's memory32
  661. */
  662. if (is_target_64bit) {
  663. if (!(offset_const = LLVMBuildZExt(comp_ctx->builder, offset_const,
  664. I64_TYPE, "offset_i64"))
  665. || !(addr = LLVMBuildZExt(comp_ctx->builder, addr, I64_TYPE,
  666. "addr_i64"))) {
  667. aot_set_last_error("llvm build zero extend failed.");
  668. goto fail;
  669. }
  670. }
  671. /* offset1 = offset + addr; */
  672. BUILD_OP(Add, offset_const, addr, offset1, "offset1");
  673. /* 1.1 offset + addr can overflow when it's memory64
  674. * 2.1 Or when it's on 32-bit platform */
  675. if (is_memory64 || !is_target_64bit) {
  676. /* Check whether integer overflow occurs in offset + addr */
  677. LLVMBasicBlockRef check_integer_overflow_end;
  678. ADD_BASIC_BLOCK(check_integer_overflow_end,
  679. "check_integer_overflow_end");
  680. LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);
  681. BUILD_ICMP(LLVMIntULT, offset1, offset_const, cmp1, "cmp1");
  682. if (!aot_emit_exception(comp_ctx, func_ctx,
  683. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp1,
  684. check_integer_overflow_end)) {
  685. goto fail;
  686. }
  687. SET_BUILD_POS(check_integer_overflow_end);
  688. block_curr = check_integer_overflow_end;
  689. }
  690. #if WASM_ENABLE_SHARED_HEAP != 0
  691. if (comp_ctx->enable_shared_heap
  692. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  693. ADD_BASIC_BLOCK(block_maddr_phi, "maddr_phi");
  694. SET_BUILD_POS(block_maddr_phi);
  695. if (!(maddr_phi =
  696. LLVMBuildPhi(comp_ctx->builder,
  697. enable_segue ? INT8_PTR_TYPE_GS : INT8_PTR_TYPE,
  698. "maddr_phi"))) {
  699. aot_set_last_error("llvm build phi failed");
  700. goto fail;
  701. }
  702. SET_BUILD_POS(block_curr);
  703. if (!aot_check_shared_heap_memory_overflow(
  704. comp_ctx, func_ctx, block_curr, block_maddr_phi, maddr_phi,
  705. offset1, mem_base_addr, bytes, is_memory64, is_target_64bit,
  706. enable_segue)) {
  707. goto fail;
  708. }
  709. }
  710. #endif
  711. if (comp_ctx->enable_bound_check
  712. && !(is_local_of_aot_value
  713. && aot_checked_addr_list_find(func_ctx, local_idx_of_aot_value,
  714. offset, bytes))) {
  715. uint32 init_page_count =
  716. comp_ctx->comp_data->memories[0].init_page_count;
  717. if (init_page_count == 0) {
  718. LLVMValueRef mem_size;
  719. if (!(mem_size = get_memory_curr_page_count(comp_ctx, func_ctx))) {
  720. goto fail;
  721. }
  722. BUILD_ICMP(LLVMIntEQ, mem_size,
  723. MEMORY64_COND_VALUE(I64_ZERO, I32_ZERO), cmp, "is_zero");
  724. ADD_BASIC_BLOCK(check_succ, "check_mem_size_succ");
  725. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  726. if (!aot_emit_exception(comp_ctx, func_ctx,
  727. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  728. check_succ)) {
  729. goto fail;
  730. }
  731. SET_BUILD_POS(check_succ);
  732. block_curr = check_succ;
  733. }
  734. if (!(mem_check_bound =
  735. get_memory_check_bound(comp_ctx, func_ctx, bytes))) {
  736. goto fail;
  737. }
  738. BUILD_ICMP(LLVMIntUGT, offset1, mem_check_bound, cmp, "cmp");
  739. /* Add basic blocks */
  740. ADD_BASIC_BLOCK(check_succ, "check_succ");
  741. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  742. if (!aot_emit_exception(comp_ctx, func_ctx,
  743. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  744. check_succ)) {
  745. goto fail;
  746. }
  747. SET_BUILD_POS(check_succ);
  748. if (is_local_of_aot_value) {
  749. if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
  750. offset, bytes))
  751. goto fail;
  752. }
  753. }
  754. if (!enable_segue) {
  755. /* maddr = mem_base_addr + offset1 */
  756. if (!(maddr =
  757. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  758. mem_base_addr, &offset1, 1, "maddr"))) {
  759. aot_set_last_error("llvm build add failed.");
  760. goto fail;
  761. }
  762. }
  763. else {
  764. LLVMValueRef maddr_base;
  765. if (!(maddr_base = LLVMBuildIntToPtr(comp_ctx->builder, addr,
  766. INT8_PTR_TYPE_GS, "maddr_base"))) {
  767. aot_set_last_error("llvm build int to ptr failed.");
  768. goto fail;
  769. }
  770. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  771. maddr_base, &offset_const, 1,
  772. "maddr"))) {
  773. aot_set_last_error("llvm build inboundgep failed.");
  774. goto fail;
  775. }
  776. }
  777. #if WASM_ENABLE_SHARED_HEAP != 0
  778. if (comp_ctx->enable_shared_heap
  779. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  780. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  781. LLVMAddIncoming(maddr_phi, &maddr, &block_curr, 1);
  782. if (!LLVMBuildBr(comp_ctx->builder, block_maddr_phi)) {
  783. aot_set_last_error("llvm build br failed");
  784. goto fail;
  785. }
  786. SET_BUILD_POS(block_maddr_phi);
  787. return maddr_phi;
  788. }
  789. else
  790. #endif
  791. return maddr;
  792. fail:
  793. return NULL;
  794. }
  795. #define BUILD_PTR_CAST(ptr_type) \
  796. do { \
  797. if (!(maddr = LLVMBuildBitCast(comp_ctx->builder, maddr, ptr_type, \
  798. "data_ptr"))) { \
  799. aot_set_last_error("llvm build bit cast failed."); \
  800. goto fail; \
  801. } \
  802. } while (0)
  803. #define BUILD_LOAD(data_type) \
  804. do { \
  805. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  806. "data"))) { \
  807. aot_set_last_error("llvm build load failed."); \
  808. goto fail; \
  809. } \
  810. LLVMSetAlignment(value, known_align); \
  811. } while (0)
  812. #define BUILD_STORE() \
  813. do { \
  814. LLVMValueRef res; \
  815. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  816. aot_set_last_error("llvm build store failed."); \
  817. goto fail; \
  818. } \
  819. LLVMSetAlignment(res, known_align); \
  820. } while (0)
  821. #define BUILD_SIGN_EXT(dst_type) \
  822. do { \
  823. if (!(value = LLVMBuildSExt(comp_ctx->builder, value, dst_type, \
  824. "data_s_ext"))) { \
  825. aot_set_last_error("llvm build sign ext failed."); \
  826. goto fail; \
  827. } \
  828. } while (0)
  829. #define BUILD_ZERO_EXT(dst_type) \
  830. do { \
  831. if (!(value = LLVMBuildZExt(comp_ctx->builder, value, dst_type, \
  832. "data_z_ext"))) { \
  833. aot_set_last_error("llvm build zero ext failed."); \
  834. goto fail; \
  835. } \
  836. } while (0)
  837. #if WASM_ENABLE_SHARED_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0
  838. bool
  839. check_memory_alignment(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  840. LLVMValueRef addr, uint32 align)
  841. {
  842. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  843. LLVMBasicBlockRef check_align_succ;
  844. LLVMValueRef align_mask = I32_CONST(((uint32)1 << align) - 1);
  845. LLVMValueRef res;
  846. CHECK_LLVM_CONST(align_mask);
  847. /* Convert pointer to int */
  848. if (!(addr = LLVMBuildPtrToInt(comp_ctx->builder, addr, I32_TYPE,
  849. "address"))) {
  850. aot_set_last_error("llvm build ptr to int failed.");
  851. goto fail;
  852. }
  853. /* The memory address should be aligned */
  854. BUILD_OP(And, addr, align_mask, res, "and");
  855. BUILD_ICMP(LLVMIntNE, res, I32_ZERO, res, "cmp");
  856. /* Add basic blocks */
  857. ADD_BASIC_BLOCK(check_align_succ, "check_align_succ");
  858. LLVMMoveBasicBlockAfter(check_align_succ, block_curr);
  859. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNALIGNED_ATOMIC, true,
  860. res, check_align_succ)) {
  861. goto fail;
  862. }
  863. SET_BUILD_POS(check_align_succ);
  864. return true;
  865. fail:
  866. return false;
  867. }
  868. #endif /* WASM_ENABLE_SHARED_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0 */
  869. #if WASM_ENABLE_SHARED_MEMORY != 0
  870. #define BUILD_ATOMIC_LOAD(align, data_type) \
  871. do { \
  872. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  873. goto fail; \
  874. } \
  875. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  876. "data"))) { \
  877. aot_set_last_error("llvm build load failed."); \
  878. goto fail; \
  879. } \
  880. LLVMSetAlignment(value, 1 << align); \
  881. LLVMSetVolatile(value, true); \
  882. LLVMSetOrdering(value, LLVMAtomicOrderingSequentiallyConsistent); \
  883. } while (0)
  884. #define BUILD_ATOMIC_STORE(align) \
  885. do { \
  886. LLVMValueRef res; \
  887. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  888. goto fail; \
  889. } \
  890. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  891. aot_set_last_error("llvm build store failed."); \
  892. goto fail; \
  893. } \
  894. LLVMSetAlignment(res, 1 << align); \
  895. LLVMSetVolatile(res, true); \
  896. LLVMSetOrdering(res, LLVMAtomicOrderingSequentiallyConsistent); \
  897. } while (0)
  898. #endif
  899. bool
  900. aot_compile_op_i32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  901. uint32 align, mem_offset_t offset, uint32 bytes,
  902. bool sign, bool atomic)
  903. {
  904. LLVMValueRef maddr, value = NULL;
  905. LLVMTypeRef data_type;
  906. bool enable_segue = comp_ctx->enable_segue_i32_load;
  907. unsigned int known_align;
  908. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  909. enable_segue, &known_align)))
  910. return false;
  911. switch (bytes) {
  912. case 4:
  913. if (!enable_segue)
  914. BUILD_PTR_CAST(INT32_PTR_TYPE);
  915. else
  916. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  917. #if WASM_ENABLE_SHARED_MEMORY != 0
  918. if (atomic)
  919. BUILD_ATOMIC_LOAD(align, I32_TYPE);
  920. else
  921. #endif
  922. BUILD_LOAD(I32_TYPE);
  923. break;
  924. case 2:
  925. case 1:
  926. if (bytes == 2) {
  927. if (!enable_segue)
  928. BUILD_PTR_CAST(INT16_PTR_TYPE);
  929. else
  930. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  931. data_type = INT16_TYPE;
  932. }
  933. else {
  934. if (!enable_segue)
  935. BUILD_PTR_CAST(INT8_PTR_TYPE);
  936. else
  937. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  938. data_type = INT8_TYPE;
  939. }
  940. #if WASM_ENABLE_SHARED_MEMORY != 0
  941. if (atomic) {
  942. BUILD_ATOMIC_LOAD(align, data_type);
  943. BUILD_ZERO_EXT(I32_TYPE);
  944. }
  945. else
  946. #endif
  947. {
  948. BUILD_LOAD(data_type);
  949. if (sign)
  950. BUILD_SIGN_EXT(I32_TYPE);
  951. else
  952. BUILD_ZERO_EXT(I32_TYPE);
  953. }
  954. break;
  955. default:
  956. bh_assert(0);
  957. break;
  958. }
  959. PUSH_I32(value);
  960. (void)data_type;
  961. return true;
  962. fail:
  963. return false;
  964. }
  965. bool
  966. aot_compile_op_i64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  967. uint32 align, mem_offset_t offset, uint32 bytes,
  968. bool sign, bool atomic)
  969. {
  970. LLVMValueRef maddr, value = NULL;
  971. LLVMTypeRef data_type;
  972. bool enable_segue = comp_ctx->enable_segue_i64_load;
  973. unsigned int known_align;
  974. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  975. enable_segue, &known_align)))
  976. return false;
  977. switch (bytes) {
  978. case 8:
  979. if (!enable_segue)
  980. BUILD_PTR_CAST(INT64_PTR_TYPE);
  981. else
  982. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  983. #if WASM_ENABLE_SHARED_MEMORY != 0
  984. if (atomic)
  985. BUILD_ATOMIC_LOAD(align, I64_TYPE);
  986. else
  987. #endif
  988. BUILD_LOAD(I64_TYPE);
  989. break;
  990. case 4:
  991. case 2:
  992. case 1:
  993. if (bytes == 4) {
  994. if (!enable_segue)
  995. BUILD_PTR_CAST(INT32_PTR_TYPE);
  996. else
  997. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  998. data_type = I32_TYPE;
  999. }
  1000. else if (bytes == 2) {
  1001. if (!enable_segue)
  1002. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1003. else
  1004. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1005. data_type = INT16_TYPE;
  1006. }
  1007. else {
  1008. if (!enable_segue)
  1009. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1010. else
  1011. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1012. data_type = INT8_TYPE;
  1013. }
  1014. #if WASM_ENABLE_SHARED_MEMORY != 0
  1015. if (atomic) {
  1016. BUILD_ATOMIC_LOAD(align, data_type);
  1017. BUILD_ZERO_EXT(I64_TYPE);
  1018. }
  1019. else
  1020. #endif
  1021. {
  1022. BUILD_LOAD(data_type);
  1023. if (sign)
  1024. BUILD_SIGN_EXT(I64_TYPE);
  1025. else
  1026. BUILD_ZERO_EXT(I64_TYPE);
  1027. }
  1028. break;
  1029. default:
  1030. bh_assert(0);
  1031. break;
  1032. }
  1033. PUSH_I64(value);
  1034. (void)data_type;
  1035. return true;
  1036. fail:
  1037. return false;
  1038. }
  1039. bool
  1040. aot_compile_op_f32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1041. uint32 align, mem_offset_t offset)
  1042. {
  1043. LLVMValueRef maddr, value;
  1044. bool enable_segue = comp_ctx->enable_segue_f32_load;
  1045. unsigned int known_align;
  1046. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  1047. enable_segue, &known_align)))
  1048. return false;
  1049. if (!enable_segue)
  1050. BUILD_PTR_CAST(F32_PTR_TYPE);
  1051. else
  1052. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  1053. BUILD_LOAD(F32_TYPE);
  1054. PUSH_F32(value);
  1055. return true;
  1056. fail:
  1057. return false;
  1058. }
  1059. bool
  1060. aot_compile_op_f64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1061. uint32 align, mem_offset_t offset)
  1062. {
  1063. LLVMValueRef maddr, value;
  1064. bool enable_segue = comp_ctx->enable_segue_f64_load;
  1065. unsigned int known_align;
  1066. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  1067. enable_segue, &known_align)))
  1068. return false;
  1069. if (!enable_segue)
  1070. BUILD_PTR_CAST(F64_PTR_TYPE);
  1071. else
  1072. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  1073. BUILD_LOAD(F64_TYPE);
  1074. PUSH_F64(value);
  1075. return true;
  1076. fail:
  1077. return false;
  1078. }
  1079. bool
  1080. aot_compile_op_i32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1081. uint32 align, mem_offset_t offset, uint32 bytes,
  1082. bool atomic)
  1083. {
  1084. LLVMValueRef maddr, value;
  1085. bool enable_segue = comp_ctx->enable_segue_i32_store;
  1086. POP_I32(value);
  1087. unsigned int known_align;
  1088. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1089. enable_segue, &known_align)))
  1090. return false;
  1091. switch (bytes) {
  1092. case 4:
  1093. if (!enable_segue)
  1094. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1095. else
  1096. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1097. break;
  1098. case 2:
  1099. if (!enable_segue)
  1100. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1101. else
  1102. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1103. BUILD_TRUNC(value, INT16_TYPE);
  1104. break;
  1105. case 1:
  1106. if (!enable_segue)
  1107. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1108. else
  1109. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1110. BUILD_TRUNC(value, INT8_TYPE);
  1111. break;
  1112. default:
  1113. bh_assert(0);
  1114. break;
  1115. }
  1116. #if WASM_ENABLE_SHARED_MEMORY != 0
  1117. if (atomic)
  1118. BUILD_ATOMIC_STORE(align);
  1119. else
  1120. #endif
  1121. BUILD_STORE();
  1122. return true;
  1123. fail:
  1124. return false;
  1125. }
  1126. bool
  1127. aot_compile_op_i64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1128. uint32 align, mem_offset_t offset, uint32 bytes,
  1129. bool atomic)
  1130. {
  1131. LLVMValueRef maddr, value;
  1132. bool enable_segue = comp_ctx->enable_segue_i64_store;
  1133. POP_I64(value);
  1134. unsigned int known_align;
  1135. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1136. enable_segue, &known_align)))
  1137. return false;
  1138. switch (bytes) {
  1139. case 8:
  1140. if (!enable_segue)
  1141. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1142. else
  1143. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1144. break;
  1145. case 4:
  1146. if (!enable_segue)
  1147. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1148. else
  1149. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1150. BUILD_TRUNC(value, I32_TYPE);
  1151. break;
  1152. case 2:
  1153. if (!enable_segue)
  1154. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1155. else
  1156. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1157. BUILD_TRUNC(value, INT16_TYPE);
  1158. break;
  1159. case 1:
  1160. if (!enable_segue)
  1161. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1162. else
  1163. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1164. BUILD_TRUNC(value, INT8_TYPE);
  1165. break;
  1166. default:
  1167. bh_assert(0);
  1168. break;
  1169. }
  1170. #if WASM_ENABLE_SHARED_MEMORY != 0
  1171. if (atomic)
  1172. BUILD_ATOMIC_STORE(align);
  1173. else
  1174. #endif
  1175. BUILD_STORE();
  1176. return true;
  1177. fail:
  1178. return false;
  1179. }
  1180. bool
  1181. aot_compile_op_f32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1182. uint32 align, mem_offset_t offset)
  1183. {
  1184. LLVMValueRef maddr, value;
  1185. bool enable_segue = comp_ctx->enable_segue_f32_store;
  1186. POP_F32(value);
  1187. unsigned int known_align;
  1188. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  1189. enable_segue, &known_align)))
  1190. return false;
  1191. if (!enable_segue)
  1192. BUILD_PTR_CAST(F32_PTR_TYPE);
  1193. else
  1194. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  1195. BUILD_STORE();
  1196. return true;
  1197. fail:
  1198. return false;
  1199. }
  1200. bool
  1201. aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1202. uint32 align, mem_offset_t offset)
  1203. {
  1204. LLVMValueRef maddr, value;
  1205. bool enable_segue = comp_ctx->enable_segue_f64_store;
  1206. POP_F64(value);
  1207. unsigned int known_align;
  1208. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  1209. enable_segue, &known_align)))
  1210. return false;
  1211. if (!enable_segue)
  1212. BUILD_PTR_CAST(F64_PTR_TYPE);
  1213. else
  1214. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  1215. BUILD_STORE();
  1216. return true;
  1217. fail:
  1218. return false;
  1219. }
  1220. static LLVMValueRef
  1221. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1222. {
  1223. LLVMValueRef mem_size;
  1224. if (func_ctx->mem_space_unchanged) {
  1225. mem_size = func_ctx->mem_info[0].mem_cur_page_count_addr;
  1226. }
  1227. else {
  1228. if (!(mem_size = LLVMBuildLoad2(
  1229. comp_ctx->builder, I32_TYPE,
  1230. func_ctx->mem_info[0].mem_cur_page_count_addr, "mem_size"))) {
  1231. aot_set_last_error("llvm build load failed.");
  1232. goto fail;
  1233. }
  1234. }
  1235. return LLVMBuildIntCast(comp_ctx->builder, mem_size,
  1236. MEMORY64_COND_VALUE(I64_TYPE, I32_TYPE), "");
  1237. fail:
  1238. return NULL;
  1239. }
  1240. bool
  1241. aot_compile_op_memory_size(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1242. {
  1243. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  1244. if (mem_size)
  1245. PUSH_PAGE_COUNT(mem_size);
  1246. return mem_size ? true : false;
  1247. fail:
  1248. return false;
  1249. }
  1250. bool
  1251. aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1252. {
  1253. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  1254. LLVMValueRef delta, param_values[2], ret_value, func, value;
  1255. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  1256. int32 func_index;
  1257. #if WASM_ENABLE_MEMORY64 != 0
  1258. LLVMValueRef u32_max, u32_cmp_result;
  1259. #endif
  1260. if (!mem_size)
  1261. return false;
  1262. POP_PAGE_COUNT(delta);
  1263. /* TODO: multi-memory aot_enlarge_memory_with_idx() */
  1264. /* Function type of aot_enlarge_memory() */
  1265. param_types[0] = INT8_PTR_TYPE;
  1266. param_types[1] = I32_TYPE;
  1267. ret_type = INT8_TYPE;
  1268. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  1269. aot_set_last_error("llvm add function type failed.");
  1270. return false;
  1271. }
  1272. if (comp_ctx->is_jit_mode) {
  1273. /* JIT mode, call the function directly */
  1274. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1275. aot_set_last_error("llvm add pointer type failed.");
  1276. return false;
  1277. }
  1278. if (!(value = I64_CONST((uint64)(uintptr_t)wasm_enlarge_memory))
  1279. || !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
  1280. aot_set_last_error("create LLVM value failed.");
  1281. return false;
  1282. }
  1283. }
  1284. else if (comp_ctx->is_indirect_mode) {
  1285. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1286. aot_set_last_error("create LLVM function type failed.");
  1287. return false;
  1288. }
  1289. func_index =
  1290. aot_get_native_symbol_index(comp_ctx, "aot_enlarge_memory");
  1291. if (func_index < 0) {
  1292. return false;
  1293. }
  1294. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1295. func_ptr_type, func_index))) {
  1296. return false;
  1297. }
  1298. }
  1299. else {
  1300. char *func_name = "aot_enlarge_memory";
  1301. /* AOT mode, declare the function */
  1302. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  1303. && !(func =
  1304. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  1305. aot_set_last_error("llvm add function failed.");
  1306. return false;
  1307. }
  1308. }
  1309. /* Call function aot_enlarge_memory() */
  1310. param_values[0] = func_ctx->aot_inst;
  1311. param_values[1] = LLVMBuildTrunc(comp_ctx->builder, delta, I32_TYPE, "");
  1312. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1313. param_values, 2, "call"))) {
  1314. aot_set_last_error("llvm build call failed.");
  1315. return false;
  1316. }
  1317. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_grow_ret");
  1318. #if WASM_ENABLE_MEMORY64 != 0
  1319. if (IS_MEMORY64) {
  1320. if (!(u32_max = I64_CONST(UINT32_MAX))) {
  1321. aot_set_last_error("llvm build const failed");
  1322. return false;
  1323. }
  1324. BUILD_ICMP(LLVMIntULE, delta, u32_max, u32_cmp_result, "page_size_cmp");
  1325. BUILD_OP(And, ret_value, u32_cmp_result, ret_value, "and");
  1326. }
  1327. #endif
  1328. /* ret_value = ret_value == true ? pre_page_count : -1 */
  1329. if (!(ret_value = LLVMBuildSelect(
  1330. comp_ctx->builder, ret_value, mem_size,
  1331. MEMORY64_COND_VALUE(I64_NEG_ONE, I32_NEG_ONE), "mem_grow_ret"))) {
  1332. aot_set_last_error("llvm build select failed.");
  1333. return false;
  1334. }
  1335. PUSH_PAGE_COUNT(ret_value);
  1336. return true;
  1337. fail:
  1338. return false;
  1339. }
  1340. #if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0
  1341. LLVMValueRef
  1342. check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1343. LLVMValueRef offset, LLVMValueRef bytes)
  1344. {
  1345. LLVMValueRef maddr, max_addr, cmp, cmp1, offset1;
  1346. LLVMValueRef mem_base_addr;
  1347. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1348. LLVMBasicBlockRef check_succ;
  1349. LLVMValueRef mem_size;
  1350. bool is_target_64bit;
  1351. #if WASM_ENABLE_MEMORY64 == 0
  1352. bool is_memory64 = false;
  1353. #else
  1354. bool is_memory64 = IS_MEMORY64;
  1355. #endif
  1356. #if WASM_ENABLE_SHARED_HEAP != 0
  1357. LLVMValueRef maddr_phi = NULL;
  1358. LLVMBasicBlockRef block_maddr_phi = NULL;
  1359. #endif
  1360. is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  1361. /* Get memory base address and memory data size */
  1362. #if WASM_ENABLE_SHARED_MEMORY != 0
  1363. bool is_shared_memory = comp_ctx->comp_data->memories[0].flags & 0x02;
  1364. if (func_ctx->mem_space_unchanged || is_shared_memory) {
  1365. #else
  1366. if (func_ctx->mem_space_unchanged) {
  1367. #endif
  1368. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  1369. }
  1370. else {
  1371. if (!(mem_base_addr = LLVMBuildLoad2(
  1372. comp_ctx->builder, OPQ_PTR_TYPE,
  1373. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  1374. aot_set_last_error("llvm build load failed.");
  1375. goto fail;
  1376. }
  1377. }
  1378. /*
  1379. * Note: not throw the integer-overflow-exception here since it must
  1380. * have been thrown when converting float to integer before
  1381. */
  1382. /* return address directly if constant offset and inside memory space */
  1383. if (LLVMIsEfficientConstInt(offset) && LLVMIsEfficientConstInt(bytes)) {
  1384. uint64 mem_offset = (uint64)LLVMConstIntGetZExtValue(offset);
  1385. uint64 mem_len = (uint64)LLVMConstIntGetZExtValue(bytes);
  1386. uint32 num_bytes_per_page =
  1387. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  1388. uint32 init_page_count =
  1389. comp_ctx->comp_data->memories[0].init_page_count;
  1390. uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
  1391. if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) {
  1392. /* inside memory space */
  1393. /* maddr = mem_base_addr + moffset */
  1394. /* Perform zero extension in advance to avoid LLVMBuildInBoundsGEP2
  1395. * interpreting a negative address due to sign extension when
  1396. * mem_offset >= 2GiB */
  1397. if (comp_ctx->pointer_size == sizeof(uint64)) {
  1398. offset1 = I64_CONST(mem_offset);
  1399. }
  1400. else {
  1401. offset1 = I32_CONST((uint32)mem_offset);
  1402. }
  1403. CHECK_LLVM_CONST(offset1);
  1404. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1405. mem_base_addr, &offset1, 1,
  1406. "maddr"))) {
  1407. aot_set_last_error("llvm build add failed.");
  1408. goto fail;
  1409. }
  1410. return maddr;
  1411. }
  1412. }
  1413. if (func_ctx->mem_space_unchanged) {
  1414. mem_size = func_ctx->mem_info[0].mem_data_size_addr;
  1415. }
  1416. else {
  1417. if (!(mem_size = LLVMBuildLoad2(
  1418. comp_ctx->builder, I64_TYPE,
  1419. func_ctx->mem_info[0].mem_data_size_addr, "mem_size"))) {
  1420. aot_set_last_error("llvm build load failed.");
  1421. goto fail;
  1422. }
  1423. }
  1424. ADD_BASIC_BLOCK(check_succ, "check_succ");
  1425. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  1426. /* Same logic with aot_check_memory_overflow, offset and bytes are 32/64
  1427. * bits on 32/64 bits platform */
  1428. if (is_target_64bit) {
  1429. offset =
  1430. LLVMBuildZExt(comp_ctx->builder, offset, I64_TYPE, "extend_offset");
  1431. bytes = LLVMBuildZExt(comp_ctx->builder, bytes, I64_TYPE, "extend_len");
  1432. if (!offset || !bytes) {
  1433. aot_set_last_error("llvm build zext failed.");
  1434. goto fail;
  1435. }
  1436. }
  1437. BUILD_OP(Add, offset, bytes, max_addr, "max_addr");
  1438. /* Check overflow when it's memory64 or it's on 32 bits platform */
  1439. if (is_memory64 || !is_target_64bit) {
  1440. /* Check whether integer overflow occurs in offset + bytes */
  1441. LLVMBasicBlockRef check_integer_overflow_end;
  1442. ADD_BASIC_BLOCK(check_integer_overflow_end,
  1443. "check_integer_overflow_end");
  1444. LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);
  1445. /* offset + bytes can overflow yet is valid(for example, 0xffffffff, 1),
  1446. * allow it to be 0(either 0, 0 or overflow and valid) */
  1447. BUILD_ICMP(LLVMIntULT, max_addr, offset, cmp, "cmp");
  1448. BUILD_ICMP(LLVMIntNE, max_addr, is_target_64bit ? I64_ZERO : I32_ZERO,
  1449. cmp1, "cmp1");
  1450. BUILD_OP(And, cmp, cmp1, cmp, "overflow");
  1451. if (!aot_emit_exception(comp_ctx, func_ctx,
  1452. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  1453. check_integer_overflow_end)) {
  1454. goto fail;
  1455. }
  1456. SET_BUILD_POS(check_integer_overflow_end);
  1457. block_curr = check_integer_overflow_end;
  1458. }
  1459. #if WASM_ENABLE_SHARED_HEAP != 0
  1460. if (comp_ctx->enable_shared_heap
  1461. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  1462. ADD_BASIC_BLOCK(block_maddr_phi, "maddr_phi");
  1463. SET_BUILD_POS(block_maddr_phi);
  1464. if (!(maddr_phi = LLVMBuildPhi(comp_ctx->builder, INT8_PTR_TYPE,
  1465. "maddr_phi"))) {
  1466. aot_set_last_error("llvm build phi failed");
  1467. goto fail;
  1468. }
  1469. SET_BUILD_POS(block_curr);
  1470. if (!aot_check_bulk_memory_shared_heap_memory_overflow(
  1471. comp_ctx, func_ctx, block_curr, block_maddr_phi, check_succ,
  1472. maddr_phi, offset, max_addr, bytes, is_memory64,
  1473. is_target_64bit)) {
  1474. goto fail;
  1475. }
  1476. }
  1477. #endif
  1478. /* mem_size is always 64-bit, extend max_addr on 32 bits platform */
  1479. if (!is_target_64bit
  1480. && !(max_addr = LLVMBuildZExt(comp_ctx->builder, max_addr, I64_TYPE,
  1481. "extend_max_addr"))) {
  1482. aot_set_last_error("llvm build zext failed.");
  1483. goto fail;
  1484. }
  1485. BUILD_ICMP(LLVMIntUGT, max_addr, mem_size, cmp, "cmp_max_mem_addr");
  1486. if (!aot_emit_exception(comp_ctx, func_ctx,
  1487. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  1488. check_succ)) {
  1489. goto fail;
  1490. }
  1491. /* maddr = mem_base_addr + offset */
  1492. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1493. mem_base_addr, &offset, 1, "maddr"))) {
  1494. aot_set_last_error("llvm build add failed.");
  1495. goto fail;
  1496. }
  1497. #if WASM_ENABLE_SHARED_HEAP != 0
  1498. if (comp_ctx->enable_shared_heap
  1499. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  1500. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1501. LLVMAddIncoming(maddr_phi, &maddr, &block_curr, 1);
  1502. if (!LLVMBuildBr(comp_ctx->builder, block_maddr_phi)) {
  1503. aot_set_last_error("llvm build br failed");
  1504. goto fail;
  1505. }
  1506. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_maddr_phi);
  1507. return maddr_phi;
  1508. }
  1509. else
  1510. #endif
  1511. return maddr;
  1512. fail:
  1513. return NULL;
  1514. }
  1515. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0 */
  1516. #if WASM_ENABLE_BULK_MEMORY != 0
  1517. bool
  1518. aot_compile_op_memory_init(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1519. uint32 seg_index)
  1520. {
  1521. LLVMValueRef seg, offset, dst, len, param_values[5], ret_value, func, value;
  1522. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1523. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1524. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1525. LLVMBasicBlockRef mem_init_fail, init_success;
  1526. seg = I32_CONST(seg_index);
  1527. POP_I32(len);
  1528. POP_I32(offset);
  1529. POP_MEM_OFFSET(dst);
  1530. if (!zero_extend_u64(comp_ctx, &dst, "dst64")) {
  1531. return false;
  1532. }
  1533. param_types[0] = INT8_PTR_TYPE;
  1534. param_types[1] = I32_TYPE;
  1535. param_types[2] = I32_TYPE;
  1536. param_types[3] = I32_TYPE;
  1537. param_types[4] = SIZE_T_TYPE;
  1538. ret_type = INT8_TYPE;
  1539. if (comp_ctx->is_jit_mode)
  1540. GET_AOT_FUNCTION(llvm_jit_memory_init, 5);
  1541. else
  1542. GET_AOT_FUNCTION(aot_memory_init, 5);
  1543. /* Call function aot_memory_init() */
  1544. param_values[0] = func_ctx->aot_inst;
  1545. param_values[1] = seg;
  1546. param_values[2] = offset;
  1547. param_values[3] = len;
  1548. param_values[4] = dst;
  1549. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1550. param_values, 5, "call"))) {
  1551. aot_set_last_error("llvm build call failed.");
  1552. return false;
  1553. }
  1554. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_init_ret");
  1555. ADD_BASIC_BLOCK(mem_init_fail, "mem_init_fail");
  1556. ADD_BASIC_BLOCK(init_success, "init_success");
  1557. LLVMMoveBasicBlockAfter(mem_init_fail, block_curr);
  1558. LLVMMoveBasicBlockAfter(init_success, block_curr);
  1559. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, init_success,
  1560. mem_init_fail)) {
  1561. aot_set_last_error("llvm build cond br failed.");
  1562. goto fail;
  1563. }
  1564. /* If memory.init failed, return this function
  1565. so the runtime can catch the exception */
  1566. LLVMPositionBuilderAtEnd(comp_ctx->builder, mem_init_fail);
  1567. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1568. goto fail;
  1569. }
  1570. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_success);
  1571. return true;
  1572. fail:
  1573. return false;
  1574. }
  1575. bool
  1576. aot_compile_op_data_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1577. uint32 seg_index)
  1578. {
  1579. LLVMValueRef seg, param_values[2], ret_value, func, value;
  1580. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  1581. seg = I32_CONST(seg_index);
  1582. CHECK_LLVM_CONST(seg);
  1583. param_types[0] = INT8_PTR_TYPE;
  1584. param_types[1] = I32_TYPE;
  1585. ret_type = INT8_TYPE;
  1586. if (comp_ctx->is_jit_mode)
  1587. GET_AOT_FUNCTION(llvm_jit_data_drop, 2);
  1588. else
  1589. GET_AOT_FUNCTION(aot_data_drop, 2);
  1590. /* Call function aot_data_drop() */
  1591. param_values[0] = func_ctx->aot_inst;
  1592. param_values[1] = seg;
  1593. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1594. param_values, 2, "call"))) {
  1595. aot_set_last_error("llvm build call failed.");
  1596. return false;
  1597. }
  1598. return true;
  1599. fail:
  1600. return false;
  1601. }
  1602. bool
  1603. aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1604. {
  1605. LLVMValueRef src, dst, src_addr, dst_addr, len, res;
  1606. bool call_aot_memmove = false;
  1607. POP_MEM_OFFSET(len);
  1608. POP_MEM_OFFSET(src);
  1609. POP_MEM_OFFSET(dst);
  1610. if (!(src_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, src, len)))
  1611. return false;
  1612. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1613. return false;
  1614. if (!zero_extend_u64(comp_ctx, &len, "len64")) {
  1615. return false;
  1616. }
  1617. call_aot_memmove = comp_ctx->is_indirect_mode || comp_ctx->is_jit_mode;
  1618. if (call_aot_memmove) {
  1619. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1620. LLVMValueRef func, params[3];
  1621. param_types[0] = INT8_PTR_TYPE;
  1622. param_types[1] = INT8_PTR_TYPE;
  1623. param_types[2] = SIZE_T_TYPE;
  1624. ret_type = INT8_PTR_TYPE;
  1625. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1626. aot_set_last_error("create LLVM function type failed.");
  1627. return false;
  1628. }
  1629. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1630. aot_set_last_error("create LLVM function pointer type failed.");
  1631. return false;
  1632. }
  1633. if (comp_ctx->is_jit_mode) {
  1634. if (!(func = I64_CONST((uint64)(uintptr_t)aot_memmove))
  1635. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1636. aot_set_last_error("create LLVM value failed.");
  1637. return false;
  1638. }
  1639. }
  1640. else {
  1641. int32 func_index;
  1642. func_index = aot_get_native_symbol_index(comp_ctx, "memmove");
  1643. if (func_index < 0) {
  1644. return false;
  1645. }
  1646. if (!(func =
  1647. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1648. func_ptr_type, func_index))) {
  1649. return false;
  1650. }
  1651. }
  1652. params[0] = dst_addr;
  1653. params[1] = src_addr;
  1654. params[2] = len;
  1655. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params,
  1656. 3, "call_memmove"))) {
  1657. aot_set_last_error("llvm build memmove failed.");
  1658. return false;
  1659. }
  1660. }
  1661. else {
  1662. if (!(res = LLVMBuildMemMove(comp_ctx->builder, dst_addr, 1, src_addr,
  1663. 1, len))) {
  1664. aot_set_last_error("llvm build memmove failed.");
  1665. return false;
  1666. }
  1667. }
  1668. return true;
  1669. fail:
  1670. return false;
  1671. }
  1672. static void *
  1673. jit_memset(void *s, int c, size_t n)
  1674. {
  1675. return memset(s, c, n);
  1676. }
  1677. bool
  1678. aot_compile_op_memory_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1679. {
  1680. LLVMValueRef val, dst, dst_addr, len, res;
  1681. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1682. LLVMValueRef func, params[3];
  1683. POP_MEM_OFFSET(len);
  1684. POP_I32(val);
  1685. POP_MEM_OFFSET(dst);
  1686. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1687. return false;
  1688. if (!zero_extend_u64(comp_ctx, &len, "len64")) {
  1689. return false;
  1690. }
  1691. param_types[0] = INT8_PTR_TYPE;
  1692. param_types[1] = I32_TYPE;
  1693. param_types[2] = SIZE_T_TYPE;
  1694. ret_type = INT8_PTR_TYPE;
  1695. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1696. aot_set_last_error("create LLVM function type failed.");
  1697. return false;
  1698. }
  1699. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1700. aot_set_last_error("create LLVM function pointer type failed.");
  1701. return false;
  1702. }
  1703. if (comp_ctx->is_jit_mode) {
  1704. if (!(func = I64_CONST((uint64)(uintptr_t)jit_memset))
  1705. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1706. aot_set_last_error("create LLVM value failed.");
  1707. return false;
  1708. }
  1709. }
  1710. else if (comp_ctx->is_indirect_mode) {
  1711. int32 func_index;
  1712. func_index = aot_get_native_symbol_index(comp_ctx, "memset");
  1713. if (func_index < 0) {
  1714. return false;
  1715. }
  1716. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1717. func_ptr_type, func_index))) {
  1718. return false;
  1719. }
  1720. }
  1721. else {
  1722. if (!(func = LLVMGetNamedFunction(func_ctx->module, "memset"))
  1723. && !(func =
  1724. LLVMAddFunction(func_ctx->module, "memset", func_type))) {
  1725. aot_set_last_error("llvm add function failed.");
  1726. return false;
  1727. }
  1728. }
  1729. params[0] = dst_addr;
  1730. params[1] = val;
  1731. params[2] = len;
  1732. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params, 3,
  1733. "call_memset"))) {
  1734. aot_set_last_error("llvm build memset failed.");
  1735. return false;
  1736. }
  1737. return true;
  1738. fail:
  1739. return false;
  1740. }
  1741. #endif /* end of WASM_ENABLE_BULK_MEMORY */
  1742. #if WASM_ENABLE_SHARED_MEMORY != 0
  1743. bool
  1744. aot_compile_op_atomic_rmw(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1745. uint8 atomic_op, uint8 op_type, uint32 align,
  1746. mem_offset_t offset, uint32 bytes)
  1747. {
  1748. LLVMValueRef maddr, value, result;
  1749. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1750. ? comp_ctx->enable_segue_i32_load
  1751. && comp_ctx->enable_segue_i32_store
  1752. : comp_ctx->enable_segue_i64_load
  1753. && comp_ctx->enable_segue_i64_store;
  1754. if (op_type == VALUE_TYPE_I32)
  1755. POP_I32(value);
  1756. else
  1757. POP_I64(value);
  1758. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1759. enable_segue, NULL)))
  1760. return false;
  1761. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1762. return false;
  1763. switch (bytes) {
  1764. case 8:
  1765. if (!enable_segue)
  1766. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1767. else
  1768. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1769. break;
  1770. case 4:
  1771. if (!enable_segue)
  1772. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1773. else
  1774. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1775. if (op_type == VALUE_TYPE_I64)
  1776. BUILD_TRUNC(value, I32_TYPE);
  1777. break;
  1778. case 2:
  1779. if (!enable_segue)
  1780. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1781. else
  1782. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1783. BUILD_TRUNC(value, INT16_TYPE);
  1784. break;
  1785. case 1:
  1786. if (!enable_segue)
  1787. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1788. else
  1789. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1790. BUILD_TRUNC(value, INT8_TYPE);
  1791. break;
  1792. default:
  1793. bh_assert(0);
  1794. break;
  1795. }
  1796. if (!(result = LLVMBuildAtomicRMW(
  1797. comp_ctx->builder, atomic_op, maddr, value,
  1798. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1799. goto fail;
  1800. }
  1801. LLVMSetVolatile(result, true);
  1802. if (op_type == VALUE_TYPE_I32) {
  1803. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1804. "result_i32"))) {
  1805. goto fail;
  1806. }
  1807. PUSH_I32(result);
  1808. }
  1809. else {
  1810. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1811. "result_i64"))) {
  1812. goto fail;
  1813. }
  1814. PUSH_I64(result);
  1815. }
  1816. return true;
  1817. fail:
  1818. return false;
  1819. }
  1820. bool
  1821. aot_compile_op_atomic_cmpxchg(AOTCompContext *comp_ctx,
  1822. AOTFuncContext *func_ctx, uint8 op_type,
  1823. uint32 align, mem_offset_t offset, uint32 bytes)
  1824. {
  1825. LLVMValueRef maddr, value, expect, result;
  1826. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1827. ? comp_ctx->enable_segue_i32_load
  1828. && comp_ctx->enable_segue_i32_store
  1829. : comp_ctx->enable_segue_i64_load
  1830. && comp_ctx->enable_segue_i64_store;
  1831. if (op_type == VALUE_TYPE_I32) {
  1832. POP_I32(value);
  1833. POP_I32(expect);
  1834. }
  1835. else {
  1836. POP_I64(value);
  1837. POP_I64(expect);
  1838. }
  1839. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1840. enable_segue, NULL)))
  1841. return false;
  1842. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1843. return false;
  1844. switch (bytes) {
  1845. case 8:
  1846. if (!enable_segue)
  1847. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1848. else
  1849. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1850. break;
  1851. case 4:
  1852. if (!enable_segue)
  1853. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1854. else
  1855. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1856. if (op_type == VALUE_TYPE_I64) {
  1857. BUILD_TRUNC(value, I32_TYPE);
  1858. BUILD_TRUNC(expect, I32_TYPE);
  1859. }
  1860. break;
  1861. case 2:
  1862. if (!enable_segue)
  1863. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1864. else
  1865. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1866. BUILD_TRUNC(value, INT16_TYPE);
  1867. BUILD_TRUNC(expect, INT16_TYPE);
  1868. break;
  1869. case 1:
  1870. if (!enable_segue)
  1871. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1872. else
  1873. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1874. BUILD_TRUNC(value, INT8_TYPE);
  1875. BUILD_TRUNC(expect, INT8_TYPE);
  1876. break;
  1877. default:
  1878. bh_assert(0);
  1879. break;
  1880. }
  1881. if (!(result = LLVMBuildAtomicCmpXchg(
  1882. comp_ctx->builder, maddr, expect, value,
  1883. LLVMAtomicOrderingSequentiallyConsistent,
  1884. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1885. goto fail;
  1886. }
  1887. LLVMSetVolatile(result, true);
  1888. /* CmpXchg return {i32, i1} structure,
  1889. we need to extract the previous_value from the structure */
  1890. if (!(result = LLVMBuildExtractValue(comp_ctx->builder, result, 0,
  1891. "previous_value"))) {
  1892. goto fail;
  1893. }
  1894. if (op_type == VALUE_TYPE_I32) {
  1895. if (LLVMTypeOf(result) != I32_TYPE) {
  1896. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1897. "result_i32"))) {
  1898. goto fail;
  1899. }
  1900. }
  1901. PUSH_I32(result);
  1902. }
  1903. else {
  1904. if (LLVMTypeOf(result) != I64_TYPE) {
  1905. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1906. "result_i64"))) {
  1907. goto fail;
  1908. }
  1909. }
  1910. PUSH_I64(result);
  1911. }
  1912. return true;
  1913. fail:
  1914. return false;
  1915. }
  1916. bool
  1917. aot_compile_op_atomic_wait(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1918. uint8 op_type, uint32 align, mem_offset_t offset,
  1919. uint32 bytes)
  1920. {
  1921. LLVMValueRef maddr, value, timeout, expect, cmp;
  1922. LLVMValueRef param_values[5], ret_value, func, is_wait64;
  1923. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1924. LLVMBasicBlockRef wait_fail, wait_success;
  1925. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1926. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1927. POP_I64(timeout);
  1928. if (op_type == VALUE_TYPE_I32) {
  1929. POP_I32(expect);
  1930. is_wait64 = I8_CONST(false);
  1931. if (!(expect = LLVMBuildZExt(comp_ctx->builder, expect, I64_TYPE,
  1932. "expect_i64"))) {
  1933. goto fail;
  1934. }
  1935. }
  1936. else {
  1937. POP_I64(expect);
  1938. is_wait64 = I8_CONST(true);
  1939. }
  1940. CHECK_LLVM_CONST(is_wait64);
  1941. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1942. false, NULL)))
  1943. return false;
  1944. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1945. return false;
  1946. param_types[0] = INT8_PTR_TYPE;
  1947. param_types[1] = INT8_PTR_TYPE;
  1948. param_types[2] = I64_TYPE;
  1949. param_types[3] = I64_TYPE;
  1950. param_types[4] = INT8_TYPE;
  1951. ret_type = I32_TYPE;
  1952. GET_AOT_FUNCTION(wasm_runtime_atomic_wait, 5);
  1953. /* Call function wasm_runtime_atomic_wait() */
  1954. param_values[0] = func_ctx->aot_inst;
  1955. param_values[1] = maddr;
  1956. param_values[2] = expect;
  1957. param_values[3] = timeout;
  1958. param_values[4] = is_wait64;
  1959. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1960. param_values, 5, "call"))) {
  1961. aot_set_last_error("llvm build call failed.");
  1962. return false;
  1963. }
  1964. BUILD_ICMP(LLVMIntNE, ret_value, I32_NEG_ONE, cmp, "atomic_wait_ret");
  1965. ADD_BASIC_BLOCK(wait_fail, "atomic_wait_fail");
  1966. ADD_BASIC_BLOCK(wait_success, "wait_success");
  1967. LLVMMoveBasicBlockAfter(wait_fail, block_curr);
  1968. LLVMMoveBasicBlockAfter(wait_success, block_curr);
  1969. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, wait_success, wait_fail)) {
  1970. aot_set_last_error("llvm build cond br failed.");
  1971. goto fail;
  1972. }
  1973. /* If atomic wait failed, return this function
  1974. so the runtime can catch the exception */
  1975. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_fail);
  1976. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1977. goto fail;
  1978. }
  1979. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_success);
  1980. PUSH_I32(ret_value);
  1981. /* Insert suspend check point */
  1982. if (comp_ctx->enable_thread_mgr) {
  1983. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1984. return false;
  1985. }
  1986. return true;
  1987. fail:
  1988. return false;
  1989. }
  1990. bool
  1991. aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
  1992. AOTFuncContext *func_ctx, uint32 align,
  1993. mem_offset_t offset, uint32 bytes)
  1994. {
  1995. LLVMValueRef maddr, value, count;
  1996. LLVMValueRef param_values[3], ret_value, func;
  1997. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1998. POP_I32(count);
  1999. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  2000. false, NULL)))
  2001. return false;
  2002. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  2003. return false;
  2004. param_types[0] = INT8_PTR_TYPE;
  2005. param_types[1] = INT8_PTR_TYPE;
  2006. param_types[2] = I32_TYPE;
  2007. ret_type = I32_TYPE;
  2008. GET_AOT_FUNCTION(wasm_runtime_atomic_notify, 3);
  2009. /* Call function wasm_runtime_atomic_notify() */
  2010. param_values[0] = func_ctx->aot_inst;
  2011. param_values[1] = maddr;
  2012. param_values[2] = count;
  2013. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  2014. param_values, 3, "call"))) {
  2015. aot_set_last_error("llvm build call failed.");
  2016. return false;
  2017. }
  2018. PUSH_I32(ret_value);
  2019. return true;
  2020. fail:
  2021. return false;
  2022. }
  2023. bool
  2024. aot_compiler_op_atomic_fence(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2025. {
  2026. return LLVMBuildFence(comp_ctx->builder,
  2027. LLVMAtomicOrderingSequentiallyConsistent, false, "")
  2028. ? true
  2029. : false;
  2030. }
  2031. #endif /* end of WASM_ENABLE_SHARED_MEMORY */