aot_emit_memory.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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 addres 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
  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. #define BUILD_ATOMIC_LOAD(align, data_type) \
  336. do { \
  337. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  338. goto fail; \
  339. } \
  340. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  341. "data"))) { \
  342. aot_set_last_error("llvm build load failed."); \
  343. goto fail; \
  344. } \
  345. LLVMSetAlignment(value, 1 << align); \
  346. LLVMSetVolatile(value, true); \
  347. LLVMSetOrdering(value, LLVMAtomicOrderingSequentiallyConsistent); \
  348. } while (0)
  349. #define BUILD_ATOMIC_STORE(align) \
  350. do { \
  351. LLVMValueRef res; \
  352. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  353. goto fail; \
  354. } \
  355. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  356. aot_set_last_error("llvm build store failed."); \
  357. goto fail; \
  358. } \
  359. LLVMSetAlignment(res, 1 << align); \
  360. LLVMSetVolatile(res, true); \
  361. LLVMSetOrdering(res, LLVMAtomicOrderingSequentiallyConsistent); \
  362. } while (0)
  363. #endif
  364. bool
  365. aot_compile_op_i32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  366. uint32 align, uint32 offset, uint32 bytes, bool sign,
  367. bool atomic)
  368. {
  369. LLVMValueRef maddr, value = NULL;
  370. LLVMTypeRef data_type;
  371. bool enable_segue = comp_ctx->enable_segue_i32_load;
  372. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  373. enable_segue)))
  374. return false;
  375. switch (bytes) {
  376. case 4:
  377. if (!enable_segue)
  378. BUILD_PTR_CAST(INT32_PTR_TYPE);
  379. else
  380. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  381. #if WASM_ENABLE_SHARED_MEMORY != 0
  382. if (atomic)
  383. BUILD_ATOMIC_LOAD(align, I32_TYPE);
  384. else
  385. #endif
  386. BUILD_LOAD(I32_TYPE);
  387. break;
  388. case 2:
  389. case 1:
  390. if (bytes == 2) {
  391. if (!enable_segue)
  392. BUILD_PTR_CAST(INT16_PTR_TYPE);
  393. else
  394. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  395. data_type = INT16_TYPE;
  396. }
  397. else {
  398. if (!enable_segue)
  399. BUILD_PTR_CAST(INT8_PTR_TYPE);
  400. else
  401. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  402. data_type = INT8_TYPE;
  403. }
  404. #if WASM_ENABLE_SHARED_MEMORY != 0
  405. if (atomic) {
  406. BUILD_ATOMIC_LOAD(align, data_type);
  407. BUILD_ZERO_EXT(I32_TYPE);
  408. }
  409. else
  410. #endif
  411. {
  412. BUILD_LOAD(data_type);
  413. if (sign)
  414. BUILD_SIGN_EXT(I32_TYPE);
  415. else
  416. BUILD_ZERO_EXT(I32_TYPE);
  417. }
  418. break;
  419. default:
  420. bh_assert(0);
  421. break;
  422. }
  423. PUSH_I32(value);
  424. (void)data_type;
  425. return true;
  426. fail:
  427. return false;
  428. }
  429. bool
  430. aot_compile_op_i64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  431. uint32 align, uint32 offset, uint32 bytes, bool sign,
  432. bool atomic)
  433. {
  434. LLVMValueRef maddr, value = NULL;
  435. LLVMTypeRef data_type;
  436. bool enable_segue = comp_ctx->enable_segue_i64_load;
  437. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  438. enable_segue)))
  439. return false;
  440. switch (bytes) {
  441. case 8:
  442. if (!enable_segue)
  443. BUILD_PTR_CAST(INT64_PTR_TYPE);
  444. else
  445. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  446. #if WASM_ENABLE_SHARED_MEMORY != 0
  447. if (atomic)
  448. BUILD_ATOMIC_LOAD(align, I64_TYPE);
  449. else
  450. #endif
  451. BUILD_LOAD(I64_TYPE);
  452. break;
  453. case 4:
  454. case 2:
  455. case 1:
  456. if (bytes == 4) {
  457. if (!enable_segue)
  458. BUILD_PTR_CAST(INT32_PTR_TYPE);
  459. else
  460. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  461. data_type = I32_TYPE;
  462. }
  463. else if (bytes == 2) {
  464. if (!enable_segue)
  465. BUILD_PTR_CAST(INT16_PTR_TYPE);
  466. else
  467. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  468. data_type = INT16_TYPE;
  469. }
  470. else {
  471. if (!enable_segue)
  472. BUILD_PTR_CAST(INT8_PTR_TYPE);
  473. else
  474. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  475. data_type = INT8_TYPE;
  476. }
  477. #if WASM_ENABLE_SHARED_MEMORY != 0
  478. if (atomic) {
  479. BUILD_ATOMIC_LOAD(align, data_type);
  480. BUILD_ZERO_EXT(I64_TYPE);
  481. }
  482. else
  483. #endif
  484. {
  485. BUILD_LOAD(data_type);
  486. if (sign)
  487. BUILD_SIGN_EXT(I64_TYPE);
  488. else
  489. BUILD_ZERO_EXT(I64_TYPE);
  490. }
  491. break;
  492. default:
  493. bh_assert(0);
  494. break;
  495. }
  496. PUSH_I64(value);
  497. (void)data_type;
  498. return true;
  499. fail:
  500. return false;
  501. }
  502. bool
  503. aot_compile_op_f32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  504. uint32 align, uint32 offset)
  505. {
  506. LLVMValueRef maddr, value;
  507. bool enable_segue = comp_ctx->enable_segue_f32_load;
  508. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  509. enable_segue)))
  510. return false;
  511. if (!enable_segue)
  512. BUILD_PTR_CAST(F32_PTR_TYPE);
  513. else
  514. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  515. BUILD_LOAD(F32_TYPE);
  516. PUSH_F32(value);
  517. return true;
  518. fail:
  519. return false;
  520. }
  521. bool
  522. aot_compile_op_f64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  523. uint32 align, uint32 offset)
  524. {
  525. LLVMValueRef maddr, value;
  526. bool enable_segue = comp_ctx->enable_segue_f64_load;
  527. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  528. enable_segue)))
  529. return false;
  530. if (!enable_segue)
  531. BUILD_PTR_CAST(F64_PTR_TYPE);
  532. else
  533. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  534. BUILD_LOAD(F64_TYPE);
  535. PUSH_F64(value);
  536. return true;
  537. fail:
  538. return false;
  539. }
  540. bool
  541. aot_compile_op_i32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  542. uint32 align, uint32 offset, uint32 bytes, bool atomic)
  543. {
  544. LLVMValueRef maddr, value;
  545. bool enable_segue = comp_ctx->enable_segue_i32_store;
  546. POP_I32(value);
  547. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  548. enable_segue)))
  549. return false;
  550. switch (bytes) {
  551. case 4:
  552. if (!enable_segue)
  553. BUILD_PTR_CAST(INT32_PTR_TYPE);
  554. else
  555. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  556. break;
  557. case 2:
  558. if (!enable_segue)
  559. BUILD_PTR_CAST(INT16_PTR_TYPE);
  560. else
  561. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  562. BUILD_TRUNC(value, INT16_TYPE);
  563. break;
  564. case 1:
  565. if (!enable_segue)
  566. BUILD_PTR_CAST(INT8_PTR_TYPE);
  567. else
  568. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  569. BUILD_TRUNC(value, INT8_TYPE);
  570. break;
  571. default:
  572. bh_assert(0);
  573. break;
  574. }
  575. #if WASM_ENABLE_SHARED_MEMORY != 0
  576. if (atomic)
  577. BUILD_ATOMIC_STORE(align);
  578. else
  579. #endif
  580. BUILD_STORE();
  581. return true;
  582. fail:
  583. return false;
  584. }
  585. bool
  586. aot_compile_op_i64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  587. uint32 align, uint32 offset, uint32 bytes, bool atomic)
  588. {
  589. LLVMValueRef maddr, value;
  590. bool enable_segue = comp_ctx->enable_segue_i64_store;
  591. POP_I64(value);
  592. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  593. enable_segue)))
  594. return false;
  595. switch (bytes) {
  596. case 8:
  597. if (!enable_segue)
  598. BUILD_PTR_CAST(INT64_PTR_TYPE);
  599. else
  600. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  601. break;
  602. case 4:
  603. if (!enable_segue)
  604. BUILD_PTR_CAST(INT32_PTR_TYPE);
  605. else
  606. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  607. BUILD_TRUNC(value, I32_TYPE);
  608. break;
  609. case 2:
  610. if (!enable_segue)
  611. BUILD_PTR_CAST(INT16_PTR_TYPE);
  612. else
  613. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  614. BUILD_TRUNC(value, INT16_TYPE);
  615. break;
  616. case 1:
  617. if (!enable_segue)
  618. BUILD_PTR_CAST(INT8_PTR_TYPE);
  619. else
  620. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  621. BUILD_TRUNC(value, INT8_TYPE);
  622. break;
  623. default:
  624. bh_assert(0);
  625. break;
  626. }
  627. #if WASM_ENABLE_SHARED_MEMORY != 0
  628. if (atomic)
  629. BUILD_ATOMIC_STORE(align);
  630. else
  631. #endif
  632. BUILD_STORE();
  633. return true;
  634. fail:
  635. return false;
  636. }
  637. bool
  638. aot_compile_op_f32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  639. uint32 align, uint32 offset)
  640. {
  641. LLVMValueRef maddr, value;
  642. bool enable_segue = comp_ctx->enable_segue_f32_store;
  643. POP_F32(value);
  644. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  645. enable_segue)))
  646. return false;
  647. if (!enable_segue)
  648. BUILD_PTR_CAST(F32_PTR_TYPE);
  649. else
  650. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  651. BUILD_STORE();
  652. return true;
  653. fail:
  654. return false;
  655. }
  656. bool
  657. aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  658. uint32 align, uint32 offset)
  659. {
  660. LLVMValueRef maddr, value;
  661. bool enable_segue = comp_ctx->enable_segue_f64_store;
  662. POP_F64(value);
  663. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  664. enable_segue)))
  665. return false;
  666. if (!enable_segue)
  667. BUILD_PTR_CAST(F64_PTR_TYPE);
  668. else
  669. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  670. BUILD_STORE();
  671. return true;
  672. fail:
  673. return false;
  674. }
  675. static LLVMValueRef
  676. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  677. {
  678. LLVMValueRef mem_size;
  679. if (func_ctx->mem_space_unchanged) {
  680. mem_size = func_ctx->mem_info[0].mem_cur_page_count_addr;
  681. }
  682. else {
  683. if (!(mem_size = LLVMBuildLoad2(
  684. comp_ctx->builder, I32_TYPE,
  685. func_ctx->mem_info[0].mem_cur_page_count_addr, "mem_size"))) {
  686. aot_set_last_error("llvm build load failed.");
  687. goto fail;
  688. }
  689. }
  690. return mem_size;
  691. fail:
  692. return NULL;
  693. }
  694. bool
  695. aot_compile_op_memory_size(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  696. {
  697. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  698. if (mem_size)
  699. PUSH_I32(mem_size);
  700. return mem_size ? true : false;
  701. fail:
  702. return false;
  703. }
  704. bool
  705. aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  706. {
  707. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  708. LLVMValueRef delta, param_values[2], ret_value, func, value;
  709. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  710. int32 func_index;
  711. if (!mem_size)
  712. return false;
  713. POP_I32(delta);
  714. /* Function type of aot_enlarge_memory() */
  715. param_types[0] = INT8_PTR_TYPE;
  716. param_types[1] = I32_TYPE;
  717. ret_type = INT8_TYPE;
  718. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  719. aot_set_last_error("llvm add function type failed.");
  720. return false;
  721. }
  722. if (comp_ctx->is_jit_mode) {
  723. /* JIT mode, call the function directly */
  724. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  725. aot_set_last_error("llvm add pointer type failed.");
  726. return false;
  727. }
  728. if (!(value = I64_CONST((uint64)(uintptr_t)wasm_enlarge_memory))
  729. || !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
  730. aot_set_last_error("create LLVM value failed.");
  731. return false;
  732. }
  733. }
  734. else if (comp_ctx->is_indirect_mode) {
  735. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  736. aot_set_last_error("create LLVM function type failed.");
  737. return false;
  738. }
  739. func_index =
  740. aot_get_native_symbol_index(comp_ctx, "aot_enlarge_memory");
  741. if (func_index < 0) {
  742. return false;
  743. }
  744. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  745. func_ptr_type, func_index))) {
  746. return false;
  747. }
  748. }
  749. else {
  750. char *func_name = "aot_enlarge_memory";
  751. /* AOT mode, delcare the function */
  752. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  753. && !(func =
  754. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  755. aot_set_last_error("llvm add function failed.");
  756. return false;
  757. }
  758. }
  759. /* Call function aot_enlarge_memory() */
  760. param_values[0] = func_ctx->aot_inst;
  761. param_values[1] = delta;
  762. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  763. param_values, 2, "call"))) {
  764. aot_set_last_error("llvm build call failed.");
  765. return false;
  766. }
  767. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_grow_ret");
  768. /* ret_value = ret_value == true ? delta : pre_page_count */
  769. if (!(ret_value = LLVMBuildSelect(comp_ctx->builder, ret_value, mem_size,
  770. I32_NEG_ONE, "mem_grow_ret"))) {
  771. aot_set_last_error("llvm build select failed.");
  772. return false;
  773. }
  774. PUSH_I32(ret_value);
  775. return true;
  776. fail:
  777. return false;
  778. }
  779. #if WASM_ENABLE_BULK_MEMORY != 0
  780. static LLVMValueRef
  781. check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  782. LLVMValueRef offset, LLVMValueRef bytes)
  783. {
  784. LLVMValueRef maddr, max_addr, cmp;
  785. LLVMValueRef mem_base_addr;
  786. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  787. LLVMBasicBlockRef check_succ;
  788. LLVMValueRef mem_size;
  789. /* Get memory base address and memory data size */
  790. #if WASM_ENABLE_SHARED_MEMORY != 0
  791. bool is_shared_memory =
  792. comp_ctx->comp_data->memories[0].memory_flags & 0x02;
  793. if (func_ctx->mem_space_unchanged || is_shared_memory) {
  794. #else
  795. if (func_ctx->mem_space_unchanged) {
  796. #endif
  797. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  798. }
  799. else {
  800. if (!(mem_base_addr = LLVMBuildLoad2(
  801. comp_ctx->builder, OPQ_PTR_TYPE,
  802. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  803. aot_set_last_error("llvm build load failed.");
  804. goto fail;
  805. }
  806. }
  807. /*
  808. * Note: not throw the integer-overflow-exception here since it must
  809. * have been thrown when converting float to integer before
  810. */
  811. /* return addres directly if constant offset and inside memory space */
  812. if (LLVMIsEfficientConstInt(offset) && LLVMIsEfficientConstInt(bytes)) {
  813. uint64 mem_offset = (uint64)LLVMConstIntGetZExtValue(offset);
  814. uint64 mem_len = (uint64)LLVMConstIntGetZExtValue(bytes);
  815. uint32 num_bytes_per_page =
  816. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  817. uint32 init_page_count =
  818. comp_ctx->comp_data->memories[0].mem_init_page_count;
  819. uint32 mem_data_size = num_bytes_per_page * init_page_count;
  820. if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) {
  821. /* inside memory space */
  822. /* maddr = mem_base_addr + moffset */
  823. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  824. mem_base_addr, &offset, 1,
  825. "maddr"))) {
  826. aot_set_last_error("llvm build add failed.");
  827. goto fail;
  828. }
  829. return maddr;
  830. }
  831. }
  832. if (func_ctx->mem_space_unchanged) {
  833. mem_size = func_ctx->mem_info[0].mem_data_size_addr;
  834. }
  835. else {
  836. if (!(mem_size = LLVMBuildLoad2(
  837. comp_ctx->builder, I32_TYPE,
  838. func_ctx->mem_info[0].mem_data_size_addr, "mem_size"))) {
  839. aot_set_last_error("llvm build load failed.");
  840. goto fail;
  841. }
  842. }
  843. ADD_BASIC_BLOCK(check_succ, "check_succ");
  844. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  845. offset =
  846. LLVMBuildZExt(comp_ctx->builder, offset, I64_TYPE, "extend_offset");
  847. bytes = LLVMBuildZExt(comp_ctx->builder, bytes, I64_TYPE, "extend_len");
  848. mem_size =
  849. LLVMBuildZExt(comp_ctx->builder, mem_size, I64_TYPE, "extend_size");
  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. bool
  868. aot_compile_op_memory_init(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  869. uint32 seg_index)
  870. {
  871. LLVMValueRef seg, offset, dst, len, param_values[5], ret_value, func, value;
  872. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  873. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  874. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  875. LLVMBasicBlockRef mem_init_fail, init_success;
  876. seg = I32_CONST(seg_index);
  877. POP_I32(len);
  878. POP_I32(offset);
  879. POP_I32(dst);
  880. param_types[0] = INT8_PTR_TYPE;
  881. param_types[1] = I32_TYPE;
  882. param_types[2] = I32_TYPE;
  883. param_types[3] = I32_TYPE;
  884. param_types[4] = I32_TYPE;
  885. ret_type = INT8_TYPE;
  886. if (comp_ctx->is_jit_mode)
  887. GET_AOT_FUNCTION(llvm_jit_memory_init, 5);
  888. else
  889. GET_AOT_FUNCTION(aot_memory_init, 5);
  890. /* Call function aot_memory_init() */
  891. param_values[0] = func_ctx->aot_inst;
  892. param_values[1] = seg;
  893. param_values[2] = offset;
  894. param_values[3] = len;
  895. param_values[4] = dst;
  896. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  897. param_values, 5, "call"))) {
  898. aot_set_last_error("llvm build call failed.");
  899. return false;
  900. }
  901. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_init_ret");
  902. ADD_BASIC_BLOCK(mem_init_fail, "mem_init_fail");
  903. ADD_BASIC_BLOCK(init_success, "init_success");
  904. LLVMMoveBasicBlockAfter(mem_init_fail, block_curr);
  905. LLVMMoveBasicBlockAfter(init_success, block_curr);
  906. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, init_success,
  907. mem_init_fail)) {
  908. aot_set_last_error("llvm build cond br failed.");
  909. goto fail;
  910. }
  911. /* If memory.init failed, return this function
  912. so the runtime can catch the exception */
  913. LLVMPositionBuilderAtEnd(comp_ctx->builder, mem_init_fail);
  914. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  915. goto fail;
  916. }
  917. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_success);
  918. return true;
  919. fail:
  920. return false;
  921. }
  922. bool
  923. aot_compile_op_data_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  924. uint32 seg_index)
  925. {
  926. LLVMValueRef seg, param_values[2], ret_value, func, value;
  927. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  928. seg = I32_CONST(seg_index);
  929. CHECK_LLVM_CONST(seg);
  930. param_types[0] = INT8_PTR_TYPE;
  931. param_types[1] = I32_TYPE;
  932. ret_type = INT8_TYPE;
  933. if (comp_ctx->is_jit_mode)
  934. GET_AOT_FUNCTION(llvm_jit_data_drop, 2);
  935. else
  936. GET_AOT_FUNCTION(aot_data_drop, 2);
  937. /* Call function aot_data_drop() */
  938. param_values[0] = func_ctx->aot_inst;
  939. param_values[1] = seg;
  940. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  941. param_values, 2, "call"))) {
  942. aot_set_last_error("llvm build call failed.");
  943. return false;
  944. }
  945. return true;
  946. fail:
  947. return false;
  948. }
  949. bool
  950. aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  951. {
  952. LLVMValueRef src, dst, src_addr, dst_addr, len, res;
  953. bool call_aot_memmove = false;
  954. POP_I32(len);
  955. POP_I32(src);
  956. POP_I32(dst);
  957. if (!(src_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, src, len)))
  958. return false;
  959. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  960. return false;
  961. call_aot_memmove = comp_ctx->is_indirect_mode || comp_ctx->is_jit_mode;
  962. if (call_aot_memmove) {
  963. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  964. LLVMValueRef func, params[3];
  965. param_types[0] = INT8_PTR_TYPE;
  966. param_types[1] = INT8_PTR_TYPE;
  967. param_types[2] = I32_TYPE;
  968. ret_type = INT8_PTR_TYPE;
  969. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  970. aot_set_last_error("create LLVM function type failed.");
  971. return false;
  972. }
  973. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  974. aot_set_last_error("create LLVM function pointer type failed.");
  975. return false;
  976. }
  977. if (comp_ctx->is_jit_mode) {
  978. if (!(func = I64_CONST((uint64)(uintptr_t)aot_memmove))
  979. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  980. aot_set_last_error("create LLVM value failed.");
  981. return false;
  982. }
  983. }
  984. else {
  985. int32 func_index;
  986. func_index = aot_get_native_symbol_index(comp_ctx, "memmove");
  987. if (func_index < 0) {
  988. return false;
  989. }
  990. if (!(func =
  991. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  992. func_ptr_type, func_index))) {
  993. return false;
  994. }
  995. }
  996. params[0] = dst_addr;
  997. params[1] = src_addr;
  998. params[2] = len;
  999. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params,
  1000. 3, "call_memmove"))) {
  1001. aot_set_last_error("llvm build memmove failed.");
  1002. return false;
  1003. }
  1004. }
  1005. else {
  1006. if (!(res = LLVMBuildMemMove(comp_ctx->builder, dst_addr, 1, src_addr,
  1007. 1, len))) {
  1008. aot_set_last_error("llvm build memmove failed.");
  1009. return false;
  1010. }
  1011. }
  1012. return true;
  1013. fail:
  1014. return false;
  1015. }
  1016. static void *
  1017. jit_memset(void *s, int c, size_t n)
  1018. {
  1019. return memset(s, c, n);
  1020. }
  1021. bool
  1022. aot_compile_op_memory_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1023. {
  1024. LLVMValueRef val, dst, dst_addr, len, res;
  1025. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1026. LLVMValueRef func, params[3];
  1027. POP_I32(len);
  1028. POP_I32(val);
  1029. POP_I32(dst);
  1030. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1031. return false;
  1032. param_types[0] = INT8_PTR_TYPE;
  1033. param_types[1] = I32_TYPE;
  1034. param_types[2] = I32_TYPE;
  1035. ret_type = INT8_PTR_TYPE;
  1036. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1037. aot_set_last_error("create LLVM function type failed.");
  1038. return false;
  1039. }
  1040. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1041. aot_set_last_error("create LLVM function pointer type failed.");
  1042. return false;
  1043. }
  1044. if (comp_ctx->is_jit_mode) {
  1045. if (!(func = I64_CONST((uint64)(uintptr_t)jit_memset))
  1046. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1047. aot_set_last_error("create LLVM value failed.");
  1048. return false;
  1049. }
  1050. }
  1051. else if (comp_ctx->is_indirect_mode) {
  1052. int32 func_index;
  1053. func_index = aot_get_native_symbol_index(comp_ctx, "memset");
  1054. if (func_index < 0) {
  1055. return false;
  1056. }
  1057. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1058. func_ptr_type, func_index))) {
  1059. return false;
  1060. }
  1061. }
  1062. else {
  1063. if (!(func = LLVMGetNamedFunction(func_ctx->module, "memset"))
  1064. && !(func =
  1065. LLVMAddFunction(func_ctx->module, "memset", func_type))) {
  1066. aot_set_last_error("llvm add function failed.");
  1067. return false;
  1068. }
  1069. }
  1070. params[0] = dst_addr;
  1071. params[1] = val;
  1072. params[2] = len;
  1073. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params, 3,
  1074. "call_memset"))) {
  1075. aot_set_last_error("llvm build memset failed.");
  1076. return false;
  1077. }
  1078. return true;
  1079. fail:
  1080. return false;
  1081. }
  1082. #endif /* end of WASM_ENABLE_BULK_MEMORY */
  1083. #if WASM_ENABLE_SHARED_MEMORY != 0
  1084. bool
  1085. aot_compile_op_atomic_rmw(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1086. uint8 atomic_op, uint8 op_type, uint32 align,
  1087. uint32 offset, uint32 bytes)
  1088. {
  1089. LLVMValueRef maddr, value, result;
  1090. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1091. ? comp_ctx->enable_segue_i32_load
  1092. && comp_ctx->enable_segue_i32_store
  1093. : comp_ctx->enable_segue_i64_load
  1094. && comp_ctx->enable_segue_i64_store;
  1095. if (op_type == VALUE_TYPE_I32)
  1096. POP_I32(value);
  1097. else
  1098. POP_I64(value);
  1099. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1100. enable_segue)))
  1101. return false;
  1102. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1103. return false;
  1104. switch (bytes) {
  1105. case 8:
  1106. if (!enable_segue)
  1107. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1108. else
  1109. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1110. break;
  1111. case 4:
  1112. if (!enable_segue)
  1113. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1114. else
  1115. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1116. if (op_type == VALUE_TYPE_I64)
  1117. BUILD_TRUNC(value, I32_TYPE);
  1118. break;
  1119. case 2:
  1120. if (!enable_segue)
  1121. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1122. else
  1123. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1124. BUILD_TRUNC(value, INT16_TYPE);
  1125. break;
  1126. case 1:
  1127. if (!enable_segue)
  1128. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1129. else
  1130. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1131. BUILD_TRUNC(value, INT8_TYPE);
  1132. break;
  1133. default:
  1134. bh_assert(0);
  1135. break;
  1136. }
  1137. if (!(result = LLVMBuildAtomicRMW(
  1138. comp_ctx->builder, atomic_op, maddr, value,
  1139. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1140. goto fail;
  1141. }
  1142. LLVMSetVolatile(result, true);
  1143. if (op_type == VALUE_TYPE_I32) {
  1144. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1145. "result_i32"))) {
  1146. goto fail;
  1147. }
  1148. PUSH_I32(result);
  1149. }
  1150. else {
  1151. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1152. "result_i64"))) {
  1153. goto fail;
  1154. }
  1155. PUSH_I64(result);
  1156. }
  1157. return true;
  1158. fail:
  1159. return false;
  1160. }
  1161. bool
  1162. aot_compile_op_atomic_cmpxchg(AOTCompContext *comp_ctx,
  1163. AOTFuncContext *func_ctx, uint8 op_type,
  1164. uint32 align, uint32 offset, uint32 bytes)
  1165. {
  1166. LLVMValueRef maddr, value, expect, result;
  1167. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1168. ? comp_ctx->enable_segue_i32_load
  1169. && comp_ctx->enable_segue_i32_store
  1170. : comp_ctx->enable_segue_i64_load
  1171. && comp_ctx->enable_segue_i64_store;
  1172. if (op_type == VALUE_TYPE_I32) {
  1173. POP_I32(value);
  1174. POP_I32(expect);
  1175. }
  1176. else {
  1177. POP_I64(value);
  1178. POP_I64(expect);
  1179. }
  1180. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1181. enable_segue)))
  1182. return false;
  1183. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1184. return false;
  1185. switch (bytes) {
  1186. case 8:
  1187. if (!enable_segue)
  1188. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1189. else
  1190. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1191. break;
  1192. case 4:
  1193. if (!enable_segue)
  1194. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1195. else
  1196. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1197. if (op_type == VALUE_TYPE_I64) {
  1198. BUILD_TRUNC(value, I32_TYPE);
  1199. BUILD_TRUNC(expect, I32_TYPE);
  1200. }
  1201. break;
  1202. case 2:
  1203. if (!enable_segue)
  1204. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1205. else
  1206. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1207. BUILD_TRUNC(value, INT16_TYPE);
  1208. BUILD_TRUNC(expect, INT16_TYPE);
  1209. break;
  1210. case 1:
  1211. if (!enable_segue)
  1212. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1213. else
  1214. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1215. BUILD_TRUNC(value, INT8_TYPE);
  1216. BUILD_TRUNC(expect, INT8_TYPE);
  1217. break;
  1218. default:
  1219. bh_assert(0);
  1220. break;
  1221. }
  1222. if (!(result = LLVMBuildAtomicCmpXchg(
  1223. comp_ctx->builder, maddr, expect, value,
  1224. LLVMAtomicOrderingSequentiallyConsistent,
  1225. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1226. goto fail;
  1227. }
  1228. LLVMSetVolatile(result, true);
  1229. /* CmpXchg return {i32, i1} structure,
  1230. we need to extrack the previous_value from the structure */
  1231. if (!(result = LLVMBuildExtractValue(comp_ctx->builder, result, 0,
  1232. "previous_value"))) {
  1233. goto fail;
  1234. }
  1235. if (op_type == VALUE_TYPE_I32) {
  1236. if (LLVMTypeOf(result) != I32_TYPE) {
  1237. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1238. "result_i32"))) {
  1239. goto fail;
  1240. }
  1241. }
  1242. PUSH_I32(result);
  1243. }
  1244. else {
  1245. if (LLVMTypeOf(result) != I64_TYPE) {
  1246. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1247. "result_i64"))) {
  1248. goto fail;
  1249. }
  1250. }
  1251. PUSH_I64(result);
  1252. }
  1253. return true;
  1254. fail:
  1255. return false;
  1256. }
  1257. bool
  1258. aot_compile_op_atomic_wait(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1259. uint8 op_type, uint32 align, uint32 offset,
  1260. uint32 bytes)
  1261. {
  1262. LLVMValueRef maddr, value, timeout, expect, cmp;
  1263. LLVMValueRef param_values[5], ret_value, func, is_wait64;
  1264. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1265. LLVMBasicBlockRef wait_fail, wait_success;
  1266. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1267. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1268. POP_I64(timeout);
  1269. if (op_type == VALUE_TYPE_I32) {
  1270. POP_I32(expect);
  1271. is_wait64 = I8_CONST(false);
  1272. if (!(expect = LLVMBuildZExt(comp_ctx->builder, expect, I64_TYPE,
  1273. "expect_i64"))) {
  1274. goto fail;
  1275. }
  1276. }
  1277. else {
  1278. POP_I64(expect);
  1279. is_wait64 = I8_CONST(true);
  1280. }
  1281. CHECK_LLVM_CONST(is_wait64);
  1282. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1283. false)))
  1284. return false;
  1285. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1286. return false;
  1287. param_types[0] = INT8_PTR_TYPE;
  1288. param_types[1] = INT8_PTR_TYPE;
  1289. param_types[2] = I64_TYPE;
  1290. param_types[3] = I64_TYPE;
  1291. param_types[4] = INT8_TYPE;
  1292. ret_type = I32_TYPE;
  1293. GET_AOT_FUNCTION(wasm_runtime_atomic_wait, 5);
  1294. /* Call function wasm_runtime_atomic_wait() */
  1295. param_values[0] = func_ctx->aot_inst;
  1296. param_values[1] = maddr;
  1297. param_values[2] = expect;
  1298. param_values[3] = timeout;
  1299. param_values[4] = is_wait64;
  1300. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1301. param_values, 5, "call"))) {
  1302. aot_set_last_error("llvm build call failed.");
  1303. return false;
  1304. }
  1305. BUILD_ICMP(LLVMIntNE, ret_value, I32_NEG_ONE, cmp, "atomic_wait_ret");
  1306. ADD_BASIC_BLOCK(wait_fail, "atomic_wait_fail");
  1307. ADD_BASIC_BLOCK(wait_success, "wait_success");
  1308. LLVMMoveBasicBlockAfter(wait_fail, block_curr);
  1309. LLVMMoveBasicBlockAfter(wait_success, block_curr);
  1310. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, wait_success, wait_fail)) {
  1311. aot_set_last_error("llvm build cond br failed.");
  1312. goto fail;
  1313. }
  1314. /* If atomic wait failed, return this function
  1315. so the runtime can catch the exception */
  1316. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_fail);
  1317. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1318. goto fail;
  1319. }
  1320. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_success);
  1321. PUSH_I32(ret_value);
  1322. #if WASM_ENABLE_THREAD_MGR != 0
  1323. /* Insert suspend check point */
  1324. if (comp_ctx->enable_thread_mgr) {
  1325. if (!check_suspend_flags(comp_ctx, func_ctx))
  1326. return false;
  1327. }
  1328. #endif
  1329. return true;
  1330. fail:
  1331. return false;
  1332. }
  1333. bool
  1334. aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
  1335. AOTFuncContext *func_ctx, uint32 align,
  1336. uint32 offset, uint32 bytes)
  1337. {
  1338. LLVMValueRef maddr, value, count;
  1339. LLVMValueRef param_values[3], ret_value, func;
  1340. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1341. POP_I32(count);
  1342. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1343. false)))
  1344. return false;
  1345. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1346. return false;
  1347. param_types[0] = INT8_PTR_TYPE;
  1348. param_types[1] = INT8_PTR_TYPE;
  1349. param_types[2] = I32_TYPE;
  1350. ret_type = I32_TYPE;
  1351. GET_AOT_FUNCTION(wasm_runtime_atomic_notify, 3);
  1352. /* Call function wasm_runtime_atomic_notify() */
  1353. param_values[0] = func_ctx->aot_inst;
  1354. param_values[1] = maddr;
  1355. param_values[2] = count;
  1356. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1357. param_values, 3, "call"))) {
  1358. aot_set_last_error("llvm build call failed.");
  1359. return false;
  1360. }
  1361. PUSH_I32(ret_value);
  1362. return true;
  1363. fail:
  1364. return false;
  1365. }
  1366. bool
  1367. aot_compiler_op_atomic_fence(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1368. {
  1369. return LLVMBuildFence(comp_ctx->builder,
  1370. LLVMAtomicOrderingSequentiallyConsistent, false, "")
  1371. ? true
  1372. : false;
  1373. }
  1374. #endif /* end of WASM_ENABLE_SHARED_MEMORY */