aot_emit_memory.c 41 KB

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