aot_emit_memory.c 44 KB

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