aot_emit_memory.c 52 KB

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