aot_emit_memory.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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_ICMP(op, left, right, res, name) \
  12. do { \
  13. if (!(res = \
  14. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  15. aot_set_last_error("llvm build icmp failed."); \
  16. goto fail; \
  17. } \
  18. } while (0)
  19. #define BUILD_OP(Op, left, right, res, name) \
  20. do { \
  21. if (!(res = LLVMBuild##Op(comp_ctx->builder, left, right, name))) { \
  22. aot_set_last_error("llvm build " #Op " fail."); \
  23. goto fail; \
  24. } \
  25. } while (0)
  26. #define ADD_BASIC_BLOCK(block, name) \
  27. do { \
  28. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  29. func_ctx->func, name))) { \
  30. aot_set_last_error("llvm add basic block failed."); \
  31. goto fail; \
  32. } \
  33. } while (0)
  34. #define SET_BUILD_POS(block) LLVMPositionBuilderAtEnd(comp_ctx->builder, block)
  35. static bool
  36. zero_extend_u64(AOTCompContext *comp_ctx, LLVMValueRef *value, const char *name)
  37. {
  38. if (comp_ctx->pointer_size == sizeof(uint64)) {
  39. /* zero extend to uint64 if the target is 64-bit */
  40. *value = LLVMBuildZExt(comp_ctx->builder, *value, I64_TYPE, name);
  41. if (!*value) {
  42. aot_set_last_error("llvm build zero extend failed.");
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. static LLVMValueRef
  49. get_memory_check_bound(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  50. uint32 bytes)
  51. {
  52. LLVMValueRef mem_check_bound = NULL;
  53. switch (bytes) {
  54. case 1:
  55. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_1byte;
  56. break;
  57. case 2:
  58. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_2bytes;
  59. break;
  60. case 4:
  61. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_4bytes;
  62. break;
  63. case 8:
  64. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_8bytes;
  65. break;
  66. case 16:
  67. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_16bytes;
  68. break;
  69. default:
  70. bh_assert(0);
  71. return NULL;
  72. }
  73. if (func_ctx->mem_space_unchanged)
  74. return mem_check_bound;
  75. if (!(mem_check_bound = LLVMBuildLoad2(
  76. comp_ctx->builder,
  77. (comp_ctx->pointer_size == sizeof(uint64)) ? I64_TYPE : I32_TYPE,
  78. mem_check_bound, "mem_check_bound"))) {
  79. aot_set_last_error("llvm build load failed.");
  80. return NULL;
  81. }
  82. return mem_check_bound;
  83. }
  84. static LLVMValueRef
  85. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
  86. LLVMValueRef
  87. aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  88. mem_offset_t offset, uint32 bytes, bool enable_segue)
  89. {
  90. LLVMValueRef offset_const =
  91. MEMORY64_COND_VALUE(I64_CONST(offset), I32_CONST(offset));
  92. LLVMValueRef addr, maddr, offset1, cmp1, cmp2, cmp;
  93. LLVMValueRef mem_base_addr, mem_check_bound;
  94. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  95. LLVMBasicBlockRef check_succ;
  96. AOTValue *aot_value_top;
  97. uint32 local_idx_of_aot_value = 0;
  98. bool is_target_64bit, is_local_of_aot_value = false;
  99. #if WASM_ENABLE_SHARED_MEMORY != 0
  100. bool is_shared_memory =
  101. comp_ctx->comp_data->memories[0].flags & SHARED_MEMORY_FLAG;
  102. #endif
  103. is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  104. if (comp_ctx->is_indirect_mode
  105. && aot_intrinsic_check_capability(
  106. comp_ctx, MEMORY64_COND_VALUE("i64.const", "i32.const"))) {
  107. WASMValue wasm_value;
  108. #if WASM_ENABLE_MEMORY64 != 0
  109. if (IS_MEMORY64) {
  110. wasm_value.i64 = offset;
  111. }
  112. else
  113. #endif
  114. {
  115. wasm_value.i32 = (int32)offset;
  116. }
  117. offset_const = aot_load_const_from_table(
  118. comp_ctx, func_ctx->native_symbol, &wasm_value,
  119. MEMORY64_COND_VALUE(VALUE_TYPE_I64, VALUE_TYPE_I32));
  120. if (!offset_const) {
  121. return NULL;
  122. }
  123. }
  124. else {
  125. CHECK_LLVM_CONST(offset_const);
  126. }
  127. /* Get memory base address and memory data size */
  128. if (func_ctx->mem_space_unchanged
  129. #if WASM_ENABLE_SHARED_MEMORY != 0
  130. || is_shared_memory
  131. #endif
  132. ) {
  133. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  134. }
  135. else {
  136. if (!(mem_base_addr = LLVMBuildLoad2(
  137. comp_ctx->builder, OPQ_PTR_TYPE,
  138. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  139. aot_set_last_error("llvm build load failed.");
  140. goto fail;
  141. }
  142. }
  143. aot_value_top =
  144. func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  145. if (aot_value_top) {
  146. /* aot_value_top is freed in the following POP_I32(addr),
  147. so save its fields here for further use */
  148. is_local_of_aot_value = aot_value_top->is_local;
  149. local_idx_of_aot_value = aot_value_top->local_idx;
  150. }
  151. POP_MEM_OFFSET(addr);
  152. /*
  153. * Note: not throw the integer-overflow-exception here since it must
  154. * have been thrown when converting float to integer before
  155. */
  156. /* return address directly if constant offset and inside memory space */
  157. if (LLVMIsEfficientConstInt(addr)) {
  158. uint64 mem_offset =
  159. (uint64)LLVMConstIntGetZExtValue(addr) + (uint64)offset;
  160. uint32 num_bytes_per_page =
  161. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  162. uint32 init_page_count =
  163. comp_ctx->comp_data->memories[0].init_page_count;
  164. uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
  165. if (mem_offset + bytes <= mem_data_size) {
  166. /* inside memory space */
  167. if (comp_ctx->pointer_size == sizeof(uint64))
  168. offset1 = I64_CONST(mem_offset);
  169. else
  170. offset1 = I32_CONST((uint32)mem_offset);
  171. CHECK_LLVM_CONST(offset1);
  172. if (!enable_segue) {
  173. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  174. INT8_TYPE, mem_base_addr,
  175. &offset1, 1, "maddr"))) {
  176. aot_set_last_error("llvm build add failed.");
  177. goto fail;
  178. }
  179. }
  180. else {
  181. if (!(maddr = LLVMBuildIntToPtr(comp_ctx->builder, offset1,
  182. INT8_PTR_TYPE_GS, "maddr"))) {
  183. aot_set_last_error("llvm build IntToPtr failed.");
  184. goto fail;
  185. }
  186. }
  187. return maddr;
  188. }
  189. }
  190. if (is_target_64bit) {
  191. if (!(offset_const = LLVMBuildZExt(comp_ctx->builder, offset_const,
  192. I64_TYPE, "offset_i64"))
  193. || !(addr = LLVMBuildZExt(comp_ctx->builder, addr, I64_TYPE,
  194. "addr_i64"))) {
  195. aot_set_last_error("llvm build zero extend failed.");
  196. goto fail;
  197. }
  198. }
  199. /* offset1 = offset + addr; */
  200. BUILD_OP(Add, offset_const, addr, offset1, "offset1");
  201. if (comp_ctx->enable_bound_check
  202. && !(is_local_of_aot_value
  203. && aot_checked_addr_list_find(func_ctx, local_idx_of_aot_value,
  204. offset, bytes))) {
  205. uint32 init_page_count =
  206. comp_ctx->comp_data->memories[0].init_page_count;
  207. if (init_page_count == 0) {
  208. LLVMValueRef mem_size;
  209. if (!(mem_size = get_memory_curr_page_count(comp_ctx, func_ctx))) {
  210. goto fail;
  211. }
  212. BUILD_ICMP(LLVMIntEQ, mem_size,
  213. MEMORY64_COND_VALUE(I64_ZERO, I32_ZERO), cmp, "is_zero");
  214. ADD_BASIC_BLOCK(check_succ, "check_mem_size_succ");
  215. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  216. if (!aot_emit_exception(comp_ctx, func_ctx,
  217. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  218. check_succ)) {
  219. goto fail;
  220. }
  221. SET_BUILD_POS(check_succ);
  222. block_curr = check_succ;
  223. }
  224. if (!(mem_check_bound =
  225. get_memory_check_bound(comp_ctx, func_ctx, bytes))) {
  226. goto fail;
  227. }
  228. if (is_target_64bit) {
  229. BUILD_ICMP(LLVMIntUGT, offset1, mem_check_bound, cmp, "cmp");
  230. }
  231. else {
  232. /* Check integer overflow */
  233. BUILD_ICMP(LLVMIntULT, offset1, addr, cmp1, "cmp1");
  234. BUILD_ICMP(LLVMIntUGT, offset1, mem_check_bound, cmp2, "cmp2");
  235. BUILD_OP(Or, cmp1, cmp2, cmp, "cmp");
  236. }
  237. /* Add basic blocks */
  238. ADD_BASIC_BLOCK(check_succ, "check_succ");
  239. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  240. if (!aot_emit_exception(comp_ctx, func_ctx,
  241. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  242. check_succ)) {
  243. goto fail;
  244. }
  245. SET_BUILD_POS(check_succ);
  246. if (is_local_of_aot_value) {
  247. if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
  248. offset, bytes))
  249. goto fail;
  250. }
  251. }
  252. if (!enable_segue) {
  253. /* maddr = mem_base_addr + offset1 */
  254. if (!(maddr =
  255. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  256. mem_base_addr, &offset1, 1, "maddr"))) {
  257. aot_set_last_error("llvm build add failed.");
  258. goto fail;
  259. }
  260. }
  261. else {
  262. LLVMValueRef maddr_base;
  263. if (!(maddr_base = LLVMBuildIntToPtr(comp_ctx->builder, addr,
  264. INT8_PTR_TYPE_GS, "maddr_base"))) {
  265. aot_set_last_error("llvm build int to ptr failed.");
  266. goto fail;
  267. }
  268. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  269. maddr_base, &offset_const, 1,
  270. "maddr"))) {
  271. aot_set_last_error("llvm build inboundgep failed.");
  272. goto fail;
  273. }
  274. }
  275. return maddr;
  276. fail:
  277. return NULL;
  278. }
  279. #define BUILD_PTR_CAST(ptr_type) \
  280. do { \
  281. if (!(maddr = LLVMBuildBitCast(comp_ctx->builder, maddr, ptr_type, \
  282. "data_ptr"))) { \
  283. aot_set_last_error("llvm build bit cast failed."); \
  284. goto fail; \
  285. } \
  286. } while (0)
  287. #define BUILD_LOAD(data_type) \
  288. do { \
  289. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  290. "data"))) { \
  291. aot_set_last_error("llvm build load failed."); \
  292. goto fail; \
  293. } \
  294. LLVMSetAlignment(value, 1); \
  295. } while (0)
  296. #define BUILD_TRUNC(value, data_type) \
  297. do { \
  298. if (!(value = LLVMBuildTrunc(comp_ctx->builder, value, data_type, \
  299. "val_trunc"))) { \
  300. aot_set_last_error("llvm build trunc failed."); \
  301. goto fail; \
  302. } \
  303. } while (0)
  304. #define BUILD_STORE() \
  305. do { \
  306. LLVMValueRef res; \
  307. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  308. aot_set_last_error("llvm build store failed."); \
  309. goto fail; \
  310. } \
  311. LLVMSetAlignment(res, 1); \
  312. } while (0)
  313. #define BUILD_SIGN_EXT(dst_type) \
  314. do { \
  315. if (!(value = LLVMBuildSExt(comp_ctx->builder, value, dst_type, \
  316. "data_s_ext"))) { \
  317. aot_set_last_error("llvm build sign ext failed."); \
  318. goto fail; \
  319. } \
  320. } while (0)
  321. #define BUILD_ZERO_EXT(dst_type) \
  322. do { \
  323. if (!(value = LLVMBuildZExt(comp_ctx->builder, value, dst_type, \
  324. "data_z_ext"))) { \
  325. aot_set_last_error("llvm build zero ext failed."); \
  326. goto fail; \
  327. } \
  328. } while (0)
  329. #if WASM_ENABLE_SHARED_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0
  330. bool
  331. check_memory_alignment(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  332. LLVMValueRef addr, uint32 align)
  333. {
  334. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  335. LLVMBasicBlockRef check_align_succ;
  336. LLVMValueRef align_mask = I32_CONST(((uint32)1 << align) - 1);
  337. LLVMValueRef res;
  338. CHECK_LLVM_CONST(align_mask);
  339. /* Convert pointer to int */
  340. if (!(addr = LLVMBuildPtrToInt(comp_ctx->builder, addr, I32_TYPE,
  341. "address"))) {
  342. aot_set_last_error("llvm build ptr to int failed.");
  343. goto fail;
  344. }
  345. /* The memory address should be aligned */
  346. BUILD_OP(And, addr, align_mask, res, "and");
  347. BUILD_ICMP(LLVMIntNE, res, I32_ZERO, res, "cmp");
  348. /* Add basic blocks */
  349. ADD_BASIC_BLOCK(check_align_succ, "check_align_succ");
  350. LLVMMoveBasicBlockAfter(check_align_succ, block_curr);
  351. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNALIGNED_ATOMIC, true,
  352. res, check_align_succ)) {
  353. goto fail;
  354. }
  355. SET_BUILD_POS(check_align_succ);
  356. return true;
  357. fail:
  358. return false;
  359. }
  360. #endif /* WASM_ENABLE_SHARED_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0 */
  361. #if WASM_ENABLE_SHARED_MEMORY != 0
  362. #define BUILD_ATOMIC_LOAD(align, data_type) \
  363. do { \
  364. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  365. goto fail; \
  366. } \
  367. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  368. "data"))) { \
  369. aot_set_last_error("llvm build load failed."); \
  370. goto fail; \
  371. } \
  372. LLVMSetAlignment(value, 1 << align); \
  373. LLVMSetVolatile(value, true); \
  374. LLVMSetOrdering(value, LLVMAtomicOrderingSequentiallyConsistent); \
  375. } while (0)
  376. #define BUILD_ATOMIC_STORE(align) \
  377. do { \
  378. LLVMValueRef res; \
  379. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  380. goto fail; \
  381. } \
  382. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  383. aot_set_last_error("llvm build store failed."); \
  384. goto fail; \
  385. } \
  386. LLVMSetAlignment(res, 1 << align); \
  387. LLVMSetVolatile(res, true); \
  388. LLVMSetOrdering(res, LLVMAtomicOrderingSequentiallyConsistent); \
  389. } while (0)
  390. #endif
  391. bool
  392. aot_compile_op_i32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  393. uint32 align, mem_offset_t offset, uint32 bytes,
  394. bool sign, bool atomic)
  395. {
  396. LLVMValueRef maddr, value = NULL;
  397. LLVMTypeRef data_type;
  398. bool enable_segue = comp_ctx->enable_segue_i32_load;
  399. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  400. enable_segue)))
  401. return false;
  402. switch (bytes) {
  403. case 4:
  404. if (!enable_segue)
  405. BUILD_PTR_CAST(INT32_PTR_TYPE);
  406. else
  407. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  408. #if WASM_ENABLE_SHARED_MEMORY != 0
  409. if (atomic)
  410. BUILD_ATOMIC_LOAD(align, I32_TYPE);
  411. else
  412. #endif
  413. BUILD_LOAD(I32_TYPE);
  414. break;
  415. case 2:
  416. case 1:
  417. if (bytes == 2) {
  418. if (!enable_segue)
  419. BUILD_PTR_CAST(INT16_PTR_TYPE);
  420. else
  421. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  422. data_type = INT16_TYPE;
  423. }
  424. else {
  425. if (!enable_segue)
  426. BUILD_PTR_CAST(INT8_PTR_TYPE);
  427. else
  428. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  429. data_type = INT8_TYPE;
  430. }
  431. #if WASM_ENABLE_SHARED_MEMORY != 0
  432. if (atomic) {
  433. BUILD_ATOMIC_LOAD(align, data_type);
  434. BUILD_ZERO_EXT(I32_TYPE);
  435. }
  436. else
  437. #endif
  438. {
  439. BUILD_LOAD(data_type);
  440. if (sign)
  441. BUILD_SIGN_EXT(I32_TYPE);
  442. else
  443. BUILD_ZERO_EXT(I32_TYPE);
  444. }
  445. break;
  446. default:
  447. bh_assert(0);
  448. break;
  449. }
  450. PUSH_I32(value);
  451. (void)data_type;
  452. return true;
  453. fail:
  454. return false;
  455. }
  456. bool
  457. aot_compile_op_i64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  458. uint32 align, mem_offset_t offset, uint32 bytes,
  459. bool sign, bool atomic)
  460. {
  461. LLVMValueRef maddr, value = NULL;
  462. LLVMTypeRef data_type;
  463. bool enable_segue = comp_ctx->enable_segue_i64_load;
  464. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  465. enable_segue)))
  466. return false;
  467. switch (bytes) {
  468. case 8:
  469. if (!enable_segue)
  470. BUILD_PTR_CAST(INT64_PTR_TYPE);
  471. else
  472. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  473. #if WASM_ENABLE_SHARED_MEMORY != 0
  474. if (atomic)
  475. BUILD_ATOMIC_LOAD(align, I64_TYPE);
  476. else
  477. #endif
  478. BUILD_LOAD(I64_TYPE);
  479. break;
  480. case 4:
  481. case 2:
  482. case 1:
  483. if (bytes == 4) {
  484. if (!enable_segue)
  485. BUILD_PTR_CAST(INT32_PTR_TYPE);
  486. else
  487. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  488. data_type = I32_TYPE;
  489. }
  490. else if (bytes == 2) {
  491. if (!enable_segue)
  492. BUILD_PTR_CAST(INT16_PTR_TYPE);
  493. else
  494. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  495. data_type = INT16_TYPE;
  496. }
  497. else {
  498. if (!enable_segue)
  499. BUILD_PTR_CAST(INT8_PTR_TYPE);
  500. else
  501. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  502. data_type = INT8_TYPE;
  503. }
  504. #if WASM_ENABLE_SHARED_MEMORY != 0
  505. if (atomic) {
  506. BUILD_ATOMIC_LOAD(align, data_type);
  507. BUILD_ZERO_EXT(I64_TYPE);
  508. }
  509. else
  510. #endif
  511. {
  512. BUILD_LOAD(data_type);
  513. if (sign)
  514. BUILD_SIGN_EXT(I64_TYPE);
  515. else
  516. BUILD_ZERO_EXT(I64_TYPE);
  517. }
  518. break;
  519. default:
  520. bh_assert(0);
  521. break;
  522. }
  523. PUSH_I64(value);
  524. (void)data_type;
  525. return true;
  526. fail:
  527. return false;
  528. }
  529. bool
  530. aot_compile_op_f32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  531. uint32 align, mem_offset_t offset)
  532. {
  533. LLVMValueRef maddr, value;
  534. bool enable_segue = comp_ctx->enable_segue_f32_load;
  535. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  536. enable_segue)))
  537. return false;
  538. if (!enable_segue)
  539. BUILD_PTR_CAST(F32_PTR_TYPE);
  540. else
  541. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  542. BUILD_LOAD(F32_TYPE);
  543. PUSH_F32(value);
  544. return true;
  545. fail:
  546. return false;
  547. }
  548. bool
  549. aot_compile_op_f64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  550. uint32 align, mem_offset_t offset)
  551. {
  552. LLVMValueRef maddr, value;
  553. bool enable_segue = comp_ctx->enable_segue_f64_load;
  554. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  555. enable_segue)))
  556. return false;
  557. if (!enable_segue)
  558. BUILD_PTR_CAST(F64_PTR_TYPE);
  559. else
  560. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  561. BUILD_LOAD(F64_TYPE);
  562. PUSH_F64(value);
  563. return true;
  564. fail:
  565. return false;
  566. }
  567. bool
  568. aot_compile_op_i32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  569. uint32 align, mem_offset_t offset, uint32 bytes,
  570. bool atomic)
  571. {
  572. LLVMValueRef maddr, value;
  573. bool enable_segue = comp_ctx->enable_segue_i32_store;
  574. POP_I32(value);
  575. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  576. enable_segue)))
  577. return false;
  578. switch (bytes) {
  579. case 4:
  580. if (!enable_segue)
  581. BUILD_PTR_CAST(INT32_PTR_TYPE);
  582. else
  583. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  584. break;
  585. case 2:
  586. if (!enable_segue)
  587. BUILD_PTR_CAST(INT16_PTR_TYPE);
  588. else
  589. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  590. BUILD_TRUNC(value, INT16_TYPE);
  591. break;
  592. case 1:
  593. if (!enable_segue)
  594. BUILD_PTR_CAST(INT8_PTR_TYPE);
  595. else
  596. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  597. BUILD_TRUNC(value, INT8_TYPE);
  598. break;
  599. default:
  600. bh_assert(0);
  601. break;
  602. }
  603. #if WASM_ENABLE_SHARED_MEMORY != 0
  604. if (atomic)
  605. BUILD_ATOMIC_STORE(align);
  606. else
  607. #endif
  608. BUILD_STORE();
  609. return true;
  610. fail:
  611. return false;
  612. }
  613. bool
  614. aot_compile_op_i64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  615. uint32 align, mem_offset_t offset, uint32 bytes,
  616. bool atomic)
  617. {
  618. LLVMValueRef maddr, value;
  619. bool enable_segue = comp_ctx->enable_segue_i64_store;
  620. POP_I64(value);
  621. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  622. enable_segue)))
  623. return false;
  624. switch (bytes) {
  625. case 8:
  626. if (!enable_segue)
  627. BUILD_PTR_CAST(INT64_PTR_TYPE);
  628. else
  629. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  630. break;
  631. case 4:
  632. if (!enable_segue)
  633. BUILD_PTR_CAST(INT32_PTR_TYPE);
  634. else
  635. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  636. BUILD_TRUNC(value, I32_TYPE);
  637. break;
  638. case 2:
  639. if (!enable_segue)
  640. BUILD_PTR_CAST(INT16_PTR_TYPE);
  641. else
  642. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  643. BUILD_TRUNC(value, INT16_TYPE);
  644. break;
  645. case 1:
  646. if (!enable_segue)
  647. BUILD_PTR_CAST(INT8_PTR_TYPE);
  648. else
  649. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  650. BUILD_TRUNC(value, INT8_TYPE);
  651. break;
  652. default:
  653. bh_assert(0);
  654. break;
  655. }
  656. #if WASM_ENABLE_SHARED_MEMORY != 0
  657. if (atomic)
  658. BUILD_ATOMIC_STORE(align);
  659. else
  660. #endif
  661. BUILD_STORE();
  662. return true;
  663. fail:
  664. return false;
  665. }
  666. bool
  667. aot_compile_op_f32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  668. uint32 align, mem_offset_t offset)
  669. {
  670. LLVMValueRef maddr, value;
  671. bool enable_segue = comp_ctx->enable_segue_f32_store;
  672. POP_F32(value);
  673. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  674. enable_segue)))
  675. return false;
  676. if (!enable_segue)
  677. BUILD_PTR_CAST(F32_PTR_TYPE);
  678. else
  679. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  680. BUILD_STORE();
  681. return true;
  682. fail:
  683. return false;
  684. }
  685. bool
  686. aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  687. uint32 align, mem_offset_t offset)
  688. {
  689. LLVMValueRef maddr, value;
  690. bool enable_segue = comp_ctx->enable_segue_f64_store;
  691. POP_F64(value);
  692. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  693. enable_segue)))
  694. return false;
  695. if (!enable_segue)
  696. BUILD_PTR_CAST(F64_PTR_TYPE);
  697. else
  698. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  699. BUILD_STORE();
  700. return true;
  701. fail:
  702. return false;
  703. }
  704. static LLVMValueRef
  705. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  706. {
  707. LLVMValueRef mem_size;
  708. if (func_ctx->mem_space_unchanged) {
  709. mem_size = func_ctx->mem_info[0].mem_cur_page_count_addr;
  710. }
  711. else {
  712. if (!(mem_size = LLVMBuildLoad2(
  713. comp_ctx->builder, I32_TYPE,
  714. func_ctx->mem_info[0].mem_cur_page_count_addr, "mem_size"))) {
  715. aot_set_last_error("llvm build load failed.");
  716. goto fail;
  717. }
  718. }
  719. return LLVMBuildIntCast(comp_ctx->builder, mem_size,
  720. MEMORY64_COND_VALUE(I64_TYPE, I32_TYPE), "");
  721. fail:
  722. return NULL;
  723. }
  724. bool
  725. aot_compile_op_memory_size(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  726. {
  727. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  728. if (mem_size)
  729. PUSH_PAGE_COUNT(mem_size);
  730. return mem_size ? true : false;
  731. fail:
  732. return false;
  733. }
  734. bool
  735. aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  736. {
  737. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  738. LLVMValueRef delta, param_values[2], ret_value, func, value;
  739. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  740. int32 func_index;
  741. #if WASM_ENABLE_MEMORY64 != 0
  742. LLVMValueRef u32_max, u32_cmp_result;
  743. #endif
  744. if (!mem_size)
  745. return false;
  746. POP_PAGE_COUNT(delta);
  747. /* Function type of aot_enlarge_memory() */
  748. param_types[0] = INT8_PTR_TYPE;
  749. param_types[1] = I32_TYPE;
  750. ret_type = INT8_TYPE;
  751. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  752. aot_set_last_error("llvm add function type failed.");
  753. return false;
  754. }
  755. if (comp_ctx->is_jit_mode) {
  756. /* JIT mode, call the function directly */
  757. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  758. aot_set_last_error("llvm add pointer type failed.");
  759. return false;
  760. }
  761. if (!(value = I64_CONST((uint64)(uintptr_t)wasm_enlarge_memory))
  762. || !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
  763. aot_set_last_error("create LLVM value failed.");
  764. return false;
  765. }
  766. }
  767. else if (comp_ctx->is_indirect_mode) {
  768. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  769. aot_set_last_error("create LLVM function type failed.");
  770. return false;
  771. }
  772. func_index =
  773. aot_get_native_symbol_index(comp_ctx, "aot_enlarge_memory");
  774. if (func_index < 0) {
  775. return false;
  776. }
  777. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  778. func_ptr_type, func_index))) {
  779. return false;
  780. }
  781. }
  782. else {
  783. char *func_name = "aot_enlarge_memory";
  784. /* AOT mode, delcare the function */
  785. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  786. && !(func =
  787. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  788. aot_set_last_error("llvm add function failed.");
  789. return false;
  790. }
  791. }
  792. /* Call function aot_enlarge_memory() */
  793. param_values[0] = func_ctx->aot_inst;
  794. param_values[1] = LLVMBuildTrunc(comp_ctx->builder, delta, I32_TYPE, "");
  795. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  796. param_values, 2, "call"))) {
  797. aot_set_last_error("llvm build call failed.");
  798. return false;
  799. }
  800. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_grow_ret");
  801. #if WASM_ENABLE_MEMORY64 != 0
  802. if (IS_MEMORY64) {
  803. if (!(u32_max = I64_CONST(UINT32_MAX))) {
  804. aot_set_last_error("llvm build const failed");
  805. return false;
  806. }
  807. BUILD_ICMP(LLVMIntULE, delta, u32_max, u32_cmp_result, "page_size_cmp");
  808. BUILD_OP(And, ret_value, u32_cmp_result, ret_value, "and");
  809. }
  810. #endif
  811. /* ret_value = ret_value == true ? pre_page_count : -1 */
  812. if (!(ret_value = LLVMBuildSelect(
  813. comp_ctx->builder, ret_value, mem_size,
  814. MEMORY64_COND_VALUE(I64_NEG_ONE, I32_NEG_ONE), "mem_grow_ret"))) {
  815. aot_set_last_error("llvm build select failed.");
  816. return false;
  817. }
  818. PUSH_PAGE_COUNT(ret_value);
  819. return true;
  820. fail:
  821. return false;
  822. }
  823. #if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0
  824. LLVMValueRef
  825. check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  826. LLVMValueRef offset, LLVMValueRef bytes)
  827. {
  828. LLVMValueRef maddr, max_addr, cmp;
  829. LLVMValueRef mem_base_addr;
  830. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  831. LLVMBasicBlockRef check_succ;
  832. LLVMValueRef mem_size;
  833. /* Get memory base address and memory data size */
  834. #if WASM_ENABLE_SHARED_MEMORY != 0
  835. bool is_shared_memory = comp_ctx->comp_data->memories[0].flags & 0x02;
  836. if (func_ctx->mem_space_unchanged || is_shared_memory) {
  837. #else
  838. if (func_ctx->mem_space_unchanged) {
  839. #endif
  840. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  841. }
  842. else {
  843. if (!(mem_base_addr = LLVMBuildLoad2(
  844. comp_ctx->builder, OPQ_PTR_TYPE,
  845. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  846. aot_set_last_error("llvm build load failed.");
  847. goto fail;
  848. }
  849. }
  850. /*
  851. * Note: not throw the integer-overflow-exception here since it must
  852. * have been thrown when converting float to integer before
  853. */
  854. /* return addres directly if constant offset and inside memory space */
  855. if (LLVMIsEfficientConstInt(offset) && LLVMIsEfficientConstInt(bytes)) {
  856. uint64 mem_offset = (uint64)LLVMConstIntGetZExtValue(offset);
  857. uint64 mem_len = (uint64)LLVMConstIntGetZExtValue(bytes);
  858. uint32 num_bytes_per_page =
  859. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  860. uint32 init_page_count =
  861. comp_ctx->comp_data->memories[0].init_page_count;
  862. uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
  863. if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) {
  864. /* inside memory space */
  865. /* maddr = mem_base_addr + moffset */
  866. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  867. mem_base_addr, &offset, 1,
  868. "maddr"))) {
  869. aot_set_last_error("llvm build add failed.");
  870. goto fail;
  871. }
  872. return maddr;
  873. }
  874. }
  875. if (func_ctx->mem_space_unchanged) {
  876. mem_size = func_ctx->mem_info[0].mem_data_size_addr;
  877. }
  878. else {
  879. if (!(mem_size = LLVMBuildLoad2(
  880. comp_ctx->builder, I64_TYPE,
  881. func_ctx->mem_info[0].mem_data_size_addr, "mem_size"))) {
  882. aot_set_last_error("llvm build load failed.");
  883. goto fail;
  884. }
  885. }
  886. ADD_BASIC_BLOCK(check_succ, "check_succ");
  887. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  888. offset =
  889. LLVMBuildZExt(comp_ctx->builder, offset, I64_TYPE, "extend_offset");
  890. bytes = LLVMBuildZExt(comp_ctx->builder, bytes, I64_TYPE, "extend_len");
  891. BUILD_OP(Add, offset, bytes, max_addr, "max_addr");
  892. BUILD_ICMP(LLVMIntUGT, max_addr, mem_size, cmp, "cmp_max_mem_addr");
  893. if (!aot_emit_exception(comp_ctx, func_ctx,
  894. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  895. check_succ)) {
  896. goto fail;
  897. }
  898. /* maddr = mem_base_addr + offset */
  899. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  900. mem_base_addr, &offset, 1, "maddr"))) {
  901. aot_set_last_error("llvm build add failed.");
  902. goto fail;
  903. }
  904. return maddr;
  905. fail:
  906. return NULL;
  907. }
  908. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 or WASM_ENABLE_STRINGREF != 0 */
  909. #if WASM_ENABLE_BULK_MEMORY != 0
  910. bool
  911. aot_compile_op_memory_init(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  912. uint32 seg_index)
  913. {
  914. LLVMValueRef seg, offset, dst, len, param_values[5], ret_value, func, value;
  915. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  916. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  917. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  918. LLVMBasicBlockRef mem_init_fail, init_success;
  919. seg = I32_CONST(seg_index);
  920. POP_I32(len);
  921. POP_I32(offset);
  922. POP_MEM_OFFSET(dst);
  923. if (!zero_extend_u64(comp_ctx, &dst, "dst64")) {
  924. return false;
  925. }
  926. param_types[0] = INT8_PTR_TYPE;
  927. param_types[1] = I32_TYPE;
  928. param_types[2] = I32_TYPE;
  929. param_types[3] = I32_TYPE;
  930. param_types[4] = SIZE_T_TYPE;
  931. ret_type = INT8_TYPE;
  932. if (comp_ctx->is_jit_mode)
  933. GET_AOT_FUNCTION(llvm_jit_memory_init, 5);
  934. else
  935. GET_AOT_FUNCTION(aot_memory_init, 5);
  936. /* Call function aot_memory_init() */
  937. param_values[0] = func_ctx->aot_inst;
  938. param_values[1] = seg;
  939. param_values[2] = offset;
  940. param_values[3] = len;
  941. param_values[4] = dst;
  942. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  943. param_values, 5, "call"))) {
  944. aot_set_last_error("llvm build call failed.");
  945. return false;
  946. }
  947. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_init_ret");
  948. ADD_BASIC_BLOCK(mem_init_fail, "mem_init_fail");
  949. ADD_BASIC_BLOCK(init_success, "init_success");
  950. LLVMMoveBasicBlockAfter(mem_init_fail, block_curr);
  951. LLVMMoveBasicBlockAfter(init_success, block_curr);
  952. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, init_success,
  953. mem_init_fail)) {
  954. aot_set_last_error("llvm build cond br failed.");
  955. goto fail;
  956. }
  957. /* If memory.init failed, return this function
  958. so the runtime can catch the exception */
  959. LLVMPositionBuilderAtEnd(comp_ctx->builder, mem_init_fail);
  960. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  961. goto fail;
  962. }
  963. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_success);
  964. return true;
  965. fail:
  966. return false;
  967. }
  968. bool
  969. aot_compile_op_data_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  970. uint32 seg_index)
  971. {
  972. LLVMValueRef seg, param_values[2], ret_value, func, value;
  973. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  974. seg = I32_CONST(seg_index);
  975. CHECK_LLVM_CONST(seg);
  976. param_types[0] = INT8_PTR_TYPE;
  977. param_types[1] = I32_TYPE;
  978. ret_type = INT8_TYPE;
  979. if (comp_ctx->is_jit_mode)
  980. GET_AOT_FUNCTION(llvm_jit_data_drop, 2);
  981. else
  982. GET_AOT_FUNCTION(aot_data_drop, 2);
  983. /* Call function aot_data_drop() */
  984. param_values[0] = func_ctx->aot_inst;
  985. param_values[1] = seg;
  986. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  987. param_values, 2, "call"))) {
  988. aot_set_last_error("llvm build call failed.");
  989. return false;
  990. }
  991. return true;
  992. fail:
  993. return false;
  994. }
  995. bool
  996. aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  997. {
  998. LLVMValueRef src, dst, src_addr, dst_addr, len, res;
  999. bool call_aot_memmove = false;
  1000. POP_MEM_OFFSET(len);
  1001. POP_MEM_OFFSET(src);
  1002. POP_MEM_OFFSET(dst);
  1003. if (!(src_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, src, len)))
  1004. return false;
  1005. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1006. return false;
  1007. if (!zero_extend_u64(comp_ctx, &len, "len64")) {
  1008. return false;
  1009. }
  1010. call_aot_memmove = comp_ctx->is_indirect_mode || comp_ctx->is_jit_mode;
  1011. if (call_aot_memmove) {
  1012. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1013. LLVMValueRef func, params[3];
  1014. param_types[0] = INT8_PTR_TYPE;
  1015. param_types[1] = INT8_PTR_TYPE;
  1016. param_types[2] = SIZE_T_TYPE;
  1017. ret_type = INT8_PTR_TYPE;
  1018. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1019. aot_set_last_error("create LLVM function type failed.");
  1020. return false;
  1021. }
  1022. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1023. aot_set_last_error("create LLVM function pointer type failed.");
  1024. return false;
  1025. }
  1026. if (comp_ctx->is_jit_mode) {
  1027. if (!(func = I64_CONST((uint64)(uintptr_t)aot_memmove))
  1028. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1029. aot_set_last_error("create LLVM value failed.");
  1030. return false;
  1031. }
  1032. }
  1033. else {
  1034. int32 func_index;
  1035. func_index = aot_get_native_symbol_index(comp_ctx, "memmove");
  1036. if (func_index < 0) {
  1037. return false;
  1038. }
  1039. if (!(func =
  1040. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1041. func_ptr_type, func_index))) {
  1042. return false;
  1043. }
  1044. }
  1045. params[0] = dst_addr;
  1046. params[1] = src_addr;
  1047. params[2] = len;
  1048. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params,
  1049. 3, "call_memmove"))) {
  1050. aot_set_last_error("llvm build memmove failed.");
  1051. return false;
  1052. }
  1053. }
  1054. else {
  1055. if (!(res = LLVMBuildMemMove(comp_ctx->builder, dst_addr, 1, src_addr,
  1056. 1, len))) {
  1057. aot_set_last_error("llvm build memmove failed.");
  1058. return false;
  1059. }
  1060. }
  1061. return true;
  1062. fail:
  1063. return false;
  1064. }
  1065. static void *
  1066. jit_memset(void *s, int c, size_t n)
  1067. {
  1068. return memset(s, c, n);
  1069. }
  1070. bool
  1071. aot_compile_op_memory_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1072. {
  1073. LLVMValueRef val, dst, dst_addr, len, res;
  1074. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1075. LLVMValueRef func, params[3];
  1076. POP_MEM_OFFSET(len);
  1077. POP_I32(val);
  1078. POP_MEM_OFFSET(dst);
  1079. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1080. return false;
  1081. if (!zero_extend_u64(comp_ctx, &len, "len64")) {
  1082. return false;
  1083. }
  1084. param_types[0] = INT8_PTR_TYPE;
  1085. param_types[1] = I32_TYPE;
  1086. param_types[2] = SIZE_T_TYPE;
  1087. ret_type = INT8_PTR_TYPE;
  1088. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1089. aot_set_last_error("create LLVM function type failed.");
  1090. return false;
  1091. }
  1092. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1093. aot_set_last_error("create LLVM function pointer type failed.");
  1094. return false;
  1095. }
  1096. if (comp_ctx->is_jit_mode) {
  1097. if (!(func = I64_CONST((uint64)(uintptr_t)jit_memset))
  1098. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1099. aot_set_last_error("create LLVM value failed.");
  1100. return false;
  1101. }
  1102. }
  1103. else if (comp_ctx->is_indirect_mode) {
  1104. int32 func_index;
  1105. func_index = aot_get_native_symbol_index(comp_ctx, "memset");
  1106. if (func_index < 0) {
  1107. return false;
  1108. }
  1109. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1110. func_ptr_type, func_index))) {
  1111. return false;
  1112. }
  1113. }
  1114. else {
  1115. if (!(func = LLVMGetNamedFunction(func_ctx->module, "memset"))
  1116. && !(func =
  1117. LLVMAddFunction(func_ctx->module, "memset", func_type))) {
  1118. aot_set_last_error("llvm add function failed.");
  1119. return false;
  1120. }
  1121. }
  1122. params[0] = dst_addr;
  1123. params[1] = val;
  1124. params[2] = len;
  1125. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params, 3,
  1126. "call_memset"))) {
  1127. aot_set_last_error("llvm build memset failed.");
  1128. return false;
  1129. }
  1130. return true;
  1131. fail:
  1132. return false;
  1133. }
  1134. #endif /* end of WASM_ENABLE_BULK_MEMORY */
  1135. #if WASM_ENABLE_SHARED_MEMORY != 0
  1136. bool
  1137. aot_compile_op_atomic_rmw(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1138. uint8 atomic_op, uint8 op_type, uint32 align,
  1139. mem_offset_t offset, uint32 bytes)
  1140. {
  1141. LLVMValueRef maddr, value, result;
  1142. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1143. ? comp_ctx->enable_segue_i32_load
  1144. && comp_ctx->enable_segue_i32_store
  1145. : comp_ctx->enable_segue_i64_load
  1146. && comp_ctx->enable_segue_i64_store;
  1147. if (op_type == VALUE_TYPE_I32)
  1148. POP_I32(value);
  1149. else
  1150. POP_I64(value);
  1151. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1152. enable_segue)))
  1153. return false;
  1154. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1155. return false;
  1156. switch (bytes) {
  1157. case 8:
  1158. if (!enable_segue)
  1159. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1160. else
  1161. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1162. break;
  1163. case 4:
  1164. if (!enable_segue)
  1165. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1166. else
  1167. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1168. if (op_type == VALUE_TYPE_I64)
  1169. BUILD_TRUNC(value, I32_TYPE);
  1170. break;
  1171. case 2:
  1172. if (!enable_segue)
  1173. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1174. else
  1175. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1176. BUILD_TRUNC(value, INT16_TYPE);
  1177. break;
  1178. case 1:
  1179. if (!enable_segue)
  1180. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1181. else
  1182. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1183. BUILD_TRUNC(value, INT8_TYPE);
  1184. break;
  1185. default:
  1186. bh_assert(0);
  1187. break;
  1188. }
  1189. if (!(result = LLVMBuildAtomicRMW(
  1190. comp_ctx->builder, atomic_op, maddr, value,
  1191. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1192. goto fail;
  1193. }
  1194. LLVMSetVolatile(result, true);
  1195. if (op_type == VALUE_TYPE_I32) {
  1196. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1197. "result_i32"))) {
  1198. goto fail;
  1199. }
  1200. PUSH_I32(result);
  1201. }
  1202. else {
  1203. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1204. "result_i64"))) {
  1205. goto fail;
  1206. }
  1207. PUSH_I64(result);
  1208. }
  1209. return true;
  1210. fail:
  1211. return false;
  1212. }
  1213. bool
  1214. aot_compile_op_atomic_cmpxchg(AOTCompContext *comp_ctx,
  1215. AOTFuncContext *func_ctx, uint8 op_type,
  1216. uint32 align, mem_offset_t offset, uint32 bytes)
  1217. {
  1218. LLVMValueRef maddr, value, expect, result;
  1219. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1220. ? comp_ctx->enable_segue_i32_load
  1221. && comp_ctx->enable_segue_i32_store
  1222. : comp_ctx->enable_segue_i64_load
  1223. && comp_ctx->enable_segue_i64_store;
  1224. if (op_type == VALUE_TYPE_I32) {
  1225. POP_I32(value);
  1226. POP_I32(expect);
  1227. }
  1228. else {
  1229. POP_I64(value);
  1230. POP_I64(expect);
  1231. }
  1232. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1233. enable_segue)))
  1234. return false;
  1235. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1236. return false;
  1237. switch (bytes) {
  1238. case 8:
  1239. if (!enable_segue)
  1240. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1241. else
  1242. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1243. break;
  1244. case 4:
  1245. if (!enable_segue)
  1246. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1247. else
  1248. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1249. if (op_type == VALUE_TYPE_I64) {
  1250. BUILD_TRUNC(value, I32_TYPE);
  1251. BUILD_TRUNC(expect, I32_TYPE);
  1252. }
  1253. break;
  1254. case 2:
  1255. if (!enable_segue)
  1256. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1257. else
  1258. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1259. BUILD_TRUNC(value, INT16_TYPE);
  1260. BUILD_TRUNC(expect, INT16_TYPE);
  1261. break;
  1262. case 1:
  1263. if (!enable_segue)
  1264. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1265. else
  1266. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1267. BUILD_TRUNC(value, INT8_TYPE);
  1268. BUILD_TRUNC(expect, INT8_TYPE);
  1269. break;
  1270. default:
  1271. bh_assert(0);
  1272. break;
  1273. }
  1274. if (!(result = LLVMBuildAtomicCmpXchg(
  1275. comp_ctx->builder, maddr, expect, value,
  1276. LLVMAtomicOrderingSequentiallyConsistent,
  1277. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1278. goto fail;
  1279. }
  1280. LLVMSetVolatile(result, true);
  1281. /* CmpXchg return {i32, i1} structure,
  1282. we need to extract the previous_value from the structure */
  1283. if (!(result = LLVMBuildExtractValue(comp_ctx->builder, result, 0,
  1284. "previous_value"))) {
  1285. goto fail;
  1286. }
  1287. if (op_type == VALUE_TYPE_I32) {
  1288. if (LLVMTypeOf(result) != I32_TYPE) {
  1289. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1290. "result_i32"))) {
  1291. goto fail;
  1292. }
  1293. }
  1294. PUSH_I32(result);
  1295. }
  1296. else {
  1297. if (LLVMTypeOf(result) != I64_TYPE) {
  1298. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1299. "result_i64"))) {
  1300. goto fail;
  1301. }
  1302. }
  1303. PUSH_I64(result);
  1304. }
  1305. return true;
  1306. fail:
  1307. return false;
  1308. }
  1309. bool
  1310. aot_compile_op_atomic_wait(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1311. uint8 op_type, uint32 align, mem_offset_t offset,
  1312. uint32 bytes)
  1313. {
  1314. LLVMValueRef maddr, value, timeout, expect, cmp;
  1315. LLVMValueRef param_values[5], ret_value, func, is_wait64;
  1316. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1317. LLVMBasicBlockRef wait_fail, wait_success;
  1318. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1319. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1320. POP_I64(timeout);
  1321. if (op_type == VALUE_TYPE_I32) {
  1322. POP_I32(expect);
  1323. is_wait64 = I8_CONST(false);
  1324. if (!(expect = LLVMBuildZExt(comp_ctx->builder, expect, I64_TYPE,
  1325. "expect_i64"))) {
  1326. goto fail;
  1327. }
  1328. }
  1329. else {
  1330. POP_I64(expect);
  1331. is_wait64 = I8_CONST(true);
  1332. }
  1333. CHECK_LLVM_CONST(is_wait64);
  1334. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1335. false)))
  1336. return false;
  1337. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1338. return false;
  1339. param_types[0] = INT8_PTR_TYPE;
  1340. param_types[1] = INT8_PTR_TYPE;
  1341. param_types[2] = I64_TYPE;
  1342. param_types[3] = I64_TYPE;
  1343. param_types[4] = INT8_TYPE;
  1344. ret_type = I32_TYPE;
  1345. GET_AOT_FUNCTION(wasm_runtime_atomic_wait, 5);
  1346. /* Call function wasm_runtime_atomic_wait() */
  1347. param_values[0] = func_ctx->aot_inst;
  1348. param_values[1] = maddr;
  1349. param_values[2] = expect;
  1350. param_values[3] = timeout;
  1351. param_values[4] = is_wait64;
  1352. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1353. param_values, 5, "call"))) {
  1354. aot_set_last_error("llvm build call failed.");
  1355. return false;
  1356. }
  1357. BUILD_ICMP(LLVMIntNE, ret_value, I32_NEG_ONE, cmp, "atomic_wait_ret");
  1358. ADD_BASIC_BLOCK(wait_fail, "atomic_wait_fail");
  1359. ADD_BASIC_BLOCK(wait_success, "wait_success");
  1360. LLVMMoveBasicBlockAfter(wait_fail, block_curr);
  1361. LLVMMoveBasicBlockAfter(wait_success, block_curr);
  1362. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, wait_success, wait_fail)) {
  1363. aot_set_last_error("llvm build cond br failed.");
  1364. goto fail;
  1365. }
  1366. /* If atomic wait failed, return this function
  1367. so the runtime can catch the exception */
  1368. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_fail);
  1369. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1370. goto fail;
  1371. }
  1372. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_success);
  1373. PUSH_I32(ret_value);
  1374. /* Insert suspend check point */
  1375. if (comp_ctx->enable_thread_mgr) {
  1376. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1377. return false;
  1378. }
  1379. return true;
  1380. fail:
  1381. return false;
  1382. }
  1383. bool
  1384. aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
  1385. AOTFuncContext *func_ctx, uint32 align,
  1386. mem_offset_t offset, uint32 bytes)
  1387. {
  1388. LLVMValueRef maddr, value, count;
  1389. LLVMValueRef param_values[3], ret_value, func;
  1390. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1391. POP_I32(count);
  1392. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1393. false)))
  1394. return false;
  1395. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1396. return false;
  1397. param_types[0] = INT8_PTR_TYPE;
  1398. param_types[1] = INT8_PTR_TYPE;
  1399. param_types[2] = I32_TYPE;
  1400. ret_type = I32_TYPE;
  1401. GET_AOT_FUNCTION(wasm_runtime_atomic_notify, 3);
  1402. /* Call function wasm_runtime_atomic_notify() */
  1403. param_values[0] = func_ctx->aot_inst;
  1404. param_values[1] = maddr;
  1405. param_values[2] = count;
  1406. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1407. param_values, 3, "call"))) {
  1408. aot_set_last_error("llvm build call failed.");
  1409. return false;
  1410. }
  1411. PUSH_I32(ret_value);
  1412. return true;
  1413. fail:
  1414. return false;
  1415. }
  1416. bool
  1417. aot_compiler_op_atomic_fence(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1418. {
  1419. return LLVMBuildFence(comp_ctx->builder,
  1420. LLVMAtomicOrderingSequentiallyConsistent, false, "")
  1421. ? true
  1422. : false;
  1423. }
  1424. #endif /* end of WASM_ENABLE_SHARED_MEMORY */