aot_emit_memory.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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_IS_NOT_NULL(value, res, name) \
  12. do { \
  13. if (!(res = LLVMBuildIsNotNull(comp_ctx->builder, value, name))) { \
  14. aot_set_last_error("llvm build is not null failed."); \
  15. goto fail; \
  16. } \
  17. } while (0)
  18. #define BUILD_BR(llvm_block) \
  19. do { \
  20. if (!LLVMBuildBr(comp_ctx->builder, llvm_block)) { \
  21. aot_set_last_error("llvm build br failed."); \
  22. goto fail; \
  23. } \
  24. } while (0)
  25. #define BUILD_COND_BR(value_if, block_then, block_else) \
  26. do { \
  27. if (!LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
  28. block_else)) { \
  29. aot_set_last_error("llvm build cond br failed."); \
  30. goto fail; \
  31. } \
  32. } while (0)
  33. #define BUILD_TRUNC(value, data_type) \
  34. do { \
  35. if (!(value = LLVMBuildTrunc(comp_ctx->builder, value, data_type, \
  36. "val_trunc"))) { \
  37. aot_set_last_error("llvm build trunc failed."); \
  38. goto fail; \
  39. } \
  40. } while (0)
  41. #define BUILD_ICMP(op, left, right, res, name) \
  42. do { \
  43. if (!(res = \
  44. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  45. aot_set_last_error("llvm build icmp failed."); \
  46. goto fail; \
  47. } \
  48. } while (0)
  49. #define BUILD_OP(Op, left, right, res, name) \
  50. do { \
  51. if (!(res = LLVMBuild##Op(comp_ctx->builder, left, right, name))) { \
  52. aot_set_last_error("llvm build " #Op " fail."); \
  53. goto fail; \
  54. } \
  55. } while (0)
  56. #define ADD_BASIC_BLOCK(block, name) \
  57. do { \
  58. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  59. func_ctx->func, name))) { \
  60. aot_set_last_error("llvm add basic block failed."); \
  61. goto fail; \
  62. } \
  63. } while (0)
  64. #define SET_BUILD_POS(block) LLVMPositionBuilderAtEnd(comp_ctx->builder, block)
  65. static bool
  66. zero_extend_u64(AOTCompContext *comp_ctx, LLVMValueRef *value, const char *name)
  67. {
  68. if (comp_ctx->pointer_size == sizeof(uint64)) {
  69. /* zero extend to uint64 if the target is 64-bit */
  70. *value = LLVMBuildZExt(comp_ctx->builder, *value, I64_TYPE, name);
  71. if (!*value) {
  72. aot_set_last_error("llvm build zero extend failed.");
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. static LLVMValueRef
  79. get_memory_check_bound(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  80. uint32 bytes)
  81. {
  82. LLVMValueRef mem_check_bound = NULL;
  83. switch (bytes) {
  84. case 1:
  85. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_1byte;
  86. break;
  87. case 2:
  88. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_2bytes;
  89. break;
  90. case 4:
  91. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_4bytes;
  92. break;
  93. case 8:
  94. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_8bytes;
  95. break;
  96. case 16:
  97. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_16bytes;
  98. break;
  99. default:
  100. bh_assert(0);
  101. return NULL;
  102. }
  103. if (func_ctx->mem_space_unchanged)
  104. return mem_check_bound;
  105. if (!(mem_check_bound = LLVMBuildLoad2(
  106. comp_ctx->builder,
  107. (comp_ctx->pointer_size == sizeof(uint64)) ? I64_TYPE : I32_TYPE,
  108. mem_check_bound, "mem_check_bound"))) {
  109. aot_set_last_error("llvm build load failed.");
  110. return NULL;
  111. }
  112. return mem_check_bound;
  113. }
  114. #if defined(_WIN32) || defined(_WIN32_)
  115. static inline int
  116. ffs(int n)
  117. {
  118. int pos = 0;
  119. if (n == 0)
  120. return 0;
  121. while (!(n & 1)) {
  122. pos++;
  123. n >>= 1;
  124. }
  125. return pos + 1;
  126. }
  127. #endif
  128. static LLVMValueRef
  129. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
  130. #if WASM_ENABLE_SHARED_HEAP != 0
  131. uint32
  132. get_module_inst_extra_offset(AOTCompContext *comp_ctx);
  133. #define BUILD_LOAD_PTR(ptr, data_type, res) \
  134. do { \
  135. if (!(res = LLVMBuildLoad2(comp_ctx->builder, data_type, ptr, \
  136. "load_value"))) { \
  137. aot_set_last_error("llvm build load failed"); \
  138. goto fail; \
  139. } \
  140. } while (0)
  141. /* Update last used shared heap info(alloc ptr) in function ctx:
  142. * 1. shared_heap_start_off 2. shared_heap_end_off 3. shared_heap_base_addr_adj
  143. */
  144. bool
  145. aot_check_shared_heap_chain_and_update(AOTCompContext *comp_ctx,
  146. AOTFuncContext *func_ctx,
  147. LLVMBasicBlockRef check_succ,
  148. LLVMValueRef start_offset,
  149. LLVMValueRef bytes, bool is_memory64)
  150. {
  151. LLVMValueRef param_values[7], ret_value, func, value, cmp;
  152. LLVMTypeRef param_types[7], ret_type, func_type, func_ptr_type;
  153. param_types[0] = INT8_PTR_TYPE;
  154. param_types[1] = INTPTR_T_TYPE;
  155. param_types[2] = SIZE_T_TYPE;
  156. param_types[3] = INTPTR_T_PTR_TYPE;
  157. param_types[4] = INTPTR_T_PTR_TYPE;
  158. param_types[5] = INT8_PTR_TYPE;
  159. param_types[6] = INT8_TYPE;
  160. ret_type = INT8_TYPE;
  161. GET_AOT_FUNCTION(wasm_runtime_check_and_update_last_used_shared_heap, 7);
  162. /* Call function */
  163. param_values[0] = func_ctx->aot_inst;
  164. param_values[1] = start_offset;
  165. param_values[2] = bytes;
  166. /* pass alloc ptr */
  167. param_values[3] = func_ctx->shared_heap_start_off;
  168. param_values[4] = func_ctx->shared_heap_end_off;
  169. param_values[5] = func_ctx->shared_heap_base_addr_adj;
  170. param_values[6] = is_memory64 ? I8_ONE : I8_ZERO;
  171. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  172. param_values, 7, "call"))) {
  173. aot_set_last_error("llvm build call failed.");
  174. goto fail;
  175. }
  176. BUILD_ICMP(LLVMIntEQ, ret_value, I8_ZERO, cmp, "shared_heap_oob");
  177. if (!aot_emit_exception(comp_ctx, func_ctx,
  178. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  179. check_succ)) {
  180. goto fail;
  181. }
  182. return true;
  183. fail:
  184. return false;
  185. }
  186. /*
  187. * Setup the basic blocks for shared heap and shared chain memory checks.
  188. *
  189. * Arguments:
  190. * block_curr: The current basic block.
  191. * app_addr_in_cache_shared_heap: Output, block for cache shared heap.
  192. * app_addr_in_linear_mem: Output, block for linear memory.
  193. * app_addr_in_shared_heap_chain: Output, block for shared heap chain
  194. * (only for shared heap chain).
  195. * check_shared_heap_chain: Output, block for checking shared heap chain
  196. * (only for shared heap chain).
  197. *
  198. * Topology:
  199. * If enable_shared_heap:
  200. * block_curr -> app_addr_in_cache_shared_heap
  201. * -> app_addr_in_linear_mem
  202. * If enable_shared_chain:
  203. * block_curr -> app_addr_in_shared_heap_chain
  204. * -> app_addr_in_cache_shared_heap
  205. * -> check_shared_heap_chain
  206. * -> app_addr_in_linear_mem
  207. */
  208. static bool
  209. setup_shared_heap_blocks(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  210. LLVMBasicBlockRef block_curr,
  211. LLVMBasicBlockRef *app_addr_in_cache_shared_heap,
  212. LLVMBasicBlockRef *app_addr_in_linear_mem,
  213. LLVMBasicBlockRef *app_addr_in_shared_heap_chain,
  214. LLVMBasicBlockRef *check_shared_heap_chain)
  215. {
  216. ADD_BASIC_BLOCK(*app_addr_in_cache_shared_heap,
  217. "app_addr_in_cache_shared_heap");
  218. ADD_BASIC_BLOCK(*app_addr_in_linear_mem, "app_addr_in_linear_mem");
  219. if (comp_ctx->enable_shared_heap) {
  220. LLVMMoveBasicBlockAfter(*app_addr_in_cache_shared_heap, block_curr);
  221. LLVMMoveBasicBlockAfter(*app_addr_in_linear_mem,
  222. *app_addr_in_cache_shared_heap);
  223. }
  224. else if (comp_ctx->enable_shared_chain) {
  225. ADD_BASIC_BLOCK(*app_addr_in_shared_heap_chain,
  226. "app_addr_in_shared_heap_chain");
  227. ADD_BASIC_BLOCK(*check_shared_heap_chain, "check_shared_heap_chain");
  228. LLVMMoveBasicBlockAfter(*app_addr_in_shared_heap_chain, block_curr);
  229. LLVMMoveBasicBlockAfter(*app_addr_in_cache_shared_heap,
  230. *app_addr_in_shared_heap_chain);
  231. LLVMMoveBasicBlockAfter(*check_shared_heap_chain,
  232. *app_addr_in_cache_shared_heap);
  233. LLVMMoveBasicBlockAfter(*app_addr_in_linear_mem,
  234. *app_addr_in_cache_shared_heap);
  235. }
  236. return true;
  237. fail:
  238. return false;
  239. }
  240. /*
  241. * Build a branch to check if start_offset is in the shared heap chain region.
  242. *
  243. * Arguments:
  244. * start_offset: The offset to check.
  245. * app_addr_in_shared_heap_chain: Block to branch if in shared heap chain.
  246. * app_addr_in_linear_mem: Block to branch if not in shared heap chain.
  247. */
  248. static bool
  249. build_check_app_addr_in_shared_heap_chain(
  250. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  251. LLVMValueRef start_offset, LLVMBasicBlockRef app_addr_in_shared_heap_chain,
  252. LLVMBasicBlockRef app_addr_in_linear_mem)
  253. {
  254. LLVMValueRef is_in_shared_heap = NULL;
  255. /* Use start_offset > func_ctx->shared_heap_head_start_off to test
  256. * start_off falls in shared heap chain memory region. The shared heap
  257. * chain oob will be detected in app_addr_in_shared_heap block or
  258. * aot_check_shared_heap_chain_and_update function
  259. */
  260. BUILD_ICMP(LLVMIntUGT, start_offset, func_ctx->shared_heap_head_start_off,
  261. is_in_shared_heap, "shared_heap_lb_cmp");
  262. BUILD_COND_BR(is_in_shared_heap, app_addr_in_shared_heap_chain,
  263. app_addr_in_linear_mem);
  264. SET_BUILD_POS(app_addr_in_shared_heap_chain);
  265. return true;
  266. fail:
  267. return false;
  268. }
  269. /*
  270. * Build the conditional branch for cache shared heap or shared heap chain.
  271. *
  272. * Arguments:
  273. * cmp: The condition for being in cache shared heap.
  274. * app_addr_in_cache_shared_heap: Block for cache shared heap.
  275. * app_addr_in_linear_mem: Block for linear memory.
  276. * check_shared_heap_chain: Block for checking shared heap chain.
  277. * bytes: The access size in bytes.
  278. * start_offset: The offset to check.
  279. * is_memory64: Whether memory is 64-bit.
  280. */
  281. static bool
  282. build_shared_heap_conditional_branching(
  283. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVMValueRef cmp,
  284. LLVMBasicBlockRef app_addr_in_cache_shared_heap,
  285. LLVMBasicBlockRef app_addr_in_linear_mem,
  286. LLVMBasicBlockRef check_shared_heap_chain, LLVMValueRef bytes,
  287. LLVMValueRef start_offset, bool is_memory64)
  288. {
  289. if (comp_ctx->enable_shared_heap) {
  290. BUILD_COND_BR(cmp, app_addr_in_cache_shared_heap,
  291. app_addr_in_linear_mem);
  292. }
  293. else if (comp_ctx->enable_shared_chain) {
  294. BUILD_COND_BR(cmp, app_addr_in_cache_shared_heap,
  295. check_shared_heap_chain);
  296. SET_BUILD_POS(check_shared_heap_chain);
  297. if (!aot_check_shared_heap_chain_and_update(
  298. comp_ctx, func_ctx, app_addr_in_cache_shared_heap, start_offset,
  299. bytes, is_memory64))
  300. goto fail;
  301. }
  302. return true;
  303. fail:
  304. return false;
  305. }
  306. /*
  307. * Get the native address in the cache shared heap.
  308. *
  309. * Arguments:
  310. * start_offset: The offset to use for address calculation.
  311. * maddr: Output, the native address that in the cache shared heap.
  312. */
  313. static bool
  314. build_get_maddr_in_cache_shared_heap(AOTCompContext *comp_ctx,
  315. AOTFuncContext *func_ctx,
  316. LLVMValueRef start_offset,
  317. LLVMValueRef *maddr)
  318. {
  319. LLVMValueRef shared_heap_base_addr_adj;
  320. /* load the local variable */
  321. BUILD_LOAD_PTR(func_ctx->shared_heap_base_addr_adj, INT8_PTR_TYPE,
  322. shared_heap_base_addr_adj);
  323. if (!(*maddr = LLVMBuildInBoundsGEP2(
  324. comp_ctx->builder, INT8_TYPE, shared_heap_base_addr_adj,
  325. &start_offset, 1, "maddr_cache_shared_heap"))) {
  326. aot_set_last_error("llvm build inbounds gep failed");
  327. goto fail;
  328. }
  329. return true;
  330. fail:
  331. return false;
  332. }
  333. /*
  334. * Check for memory overflow in shared heap for normal memory access.
  335. *
  336. * Arguments:
  337. * block_curr: The current basic block.
  338. * block_maddr_phi: The phi block for memory address.
  339. * maddr_phi: The phi node for memory address.
  340. * start_offset: The first offset to check.
  341. * mem_base_addr: The base address of memory. Only used with segue.
  342. * bytes_u32: The access size in bytes.
  343. * is_memory64: Whether memory is wasm64 memory.
  344. * is_target_64bit: Whether target is 64-bit.
  345. * enable_segue: Whether to use segment register addressing.
  346. */
  347. static bool
  348. aot_check_shared_heap_memory_overflow(
  349. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  350. LLVMBasicBlockRef block_curr, LLVMBasicBlockRef block_maddr_phi,
  351. LLVMValueRef maddr_phi, LLVMValueRef start_offset,
  352. LLVMValueRef mem_base_addr, uint32 bytes_u32, bool is_memory64,
  353. bool is_target_64bit, bool enable_segue)
  354. {
  355. LLVMBasicBlockRef app_addr_in_cache_shared_heap, app_addr_in_linear_mem;
  356. LLVMBasicBlockRef app_addr_in_shared_heap_chain = NULL,
  357. check_shared_heap_chain = NULL;
  358. LLVMValueRef cmp, cmp1, cmp2, shared_heap_start_off, shared_heap_end_off,
  359. shared_heap_check_bound, maddr = NULL;
  360. /* On 64/32-bit target, the offset is 64/32-bit */
  361. LLVMTypeRef offset_type = is_target_64bit ? I64_TYPE : I32_TYPE;
  362. LLVMValueRef length, bytes;
  363. if (!setup_shared_heap_blocks(
  364. comp_ctx, func_ctx, block_curr, &app_addr_in_cache_shared_heap,
  365. &app_addr_in_linear_mem, &app_addr_in_shared_heap_chain,
  366. &check_shared_heap_chain))
  367. goto fail;
  368. LLVMMoveBasicBlockAfter(block_maddr_phi, app_addr_in_linear_mem);
  369. /* Early branching when it's not in shared heap chain at all */
  370. if (comp_ctx->enable_shared_chain
  371. && !build_check_app_addr_in_shared_heap_chain(
  372. comp_ctx, func_ctx, start_offset, app_addr_in_shared_heap_chain,
  373. app_addr_in_linear_mem))
  374. goto fail;
  375. /* Load the local variable of the function */
  376. BUILD_LOAD_PTR(func_ctx->shared_heap_start_off, offset_type,
  377. shared_heap_start_off);
  378. BUILD_LOAD_PTR(func_ctx->shared_heap_end_off, offset_type,
  379. shared_heap_end_off);
  380. /* Check if the app address is in the cache shared heap range.
  381. * If yes, branch to the cache branch; if not, check the shared heap chain
  382. */
  383. BUILD_ICMP(LLVMIntUGE, start_offset, shared_heap_start_off, cmp,
  384. "cmp_cache_shared_heap_start");
  385. length =
  386. is_target_64bit ? I64_CONST(bytes_u32 - 1) : I32_CONST(bytes_u32 - 1);
  387. CHECK_LLVM_CONST(length);
  388. BUILD_OP(Sub, shared_heap_end_off, length, shared_heap_check_bound,
  389. "cache_shared_heap_end_bound");
  390. BUILD_ICMP(LLVMIntULE, start_offset, shared_heap_check_bound, cmp1,
  391. "cmp_cache_shared_heap_end");
  392. BUILD_OP(And, cmp, cmp1, cmp2, "is_in_cache_shared_heap");
  393. /* Conditional branching based on whether in cached shared heap */
  394. bytes = is_target_64bit ? I64_CONST(bytes_u32) : I32_CONST(bytes_u32);
  395. if (!build_shared_heap_conditional_branching(
  396. comp_ctx, func_ctx, cmp2, app_addr_in_cache_shared_heap,
  397. app_addr_in_linear_mem, check_shared_heap_chain, bytes,
  398. start_offset, is_memory64))
  399. goto fail;
  400. SET_BUILD_POS(app_addr_in_cache_shared_heap);
  401. if (!build_get_maddr_in_cache_shared_heap(comp_ctx, func_ctx, start_offset,
  402. &maddr))
  403. goto fail;
  404. if (enable_segue) {
  405. LLVMValueRef mem_base_addr_u64, maddr_u64, offset_to_mem_base;
  406. if (!(maddr_u64 = LLVMBuildPtrToInt(comp_ctx->builder, maddr, I64_TYPE,
  407. "maddr_u64"))
  408. || !(mem_base_addr_u64 =
  409. LLVMBuildPtrToInt(comp_ctx->builder, mem_base_addr,
  410. I64_TYPE, "mem_base_addr_u64"))) {
  411. aot_set_last_error("llvm build ptr to int failed");
  412. goto fail;
  413. }
  414. if (!(offset_to_mem_base =
  415. LLVMBuildSub(comp_ctx->builder, maddr_u64, mem_base_addr_u64,
  416. "offset_to_mem_base"))) {
  417. aot_set_last_error("llvm build sub failed");
  418. goto fail;
  419. }
  420. if (!(maddr = LLVMBuildIntToPtr(comp_ctx->builder, offset_to_mem_base,
  421. INT8_PTR_TYPE_GS,
  422. "maddr_shared_heap_segue"))) {
  423. aot_set_last_error("llvm build int to ptr failed.");
  424. goto fail;
  425. }
  426. }
  427. LLVMAddIncoming(maddr_phi, &maddr, &app_addr_in_cache_shared_heap, 1);
  428. BUILD_BR(block_maddr_phi);
  429. SET_BUILD_POS(app_addr_in_linear_mem);
  430. return true;
  431. fail:
  432. return false;
  433. }
  434. /*
  435. * Check for memory overflow in shared heap for bulk memory access.
  436. *
  437. * Arguments:
  438. * block_curr: The current basic block.
  439. * block_maddr_phi: The phi block for memory address.
  440. * check_succ: The block to branch to on success.
  441. * maddr_phi: The phi node for memory address.
  442. * start_offset: The offset to check.
  443. * max_addr: The maximum address to check.
  444. * bytes: The access size in bytes (LLVMValueRef).
  445. * is_memory64: Whether memory is wasm64 memory.
  446. * is_target_64bit: Whether target is 64-bit.
  447. */
  448. static bool
  449. aot_check_bulk_memory_shared_heap_memory_overflow(
  450. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  451. LLVMBasicBlockRef block_curr, LLVMBasicBlockRef block_maddr_phi,
  452. LLVMBasicBlockRef check_succ, LLVMValueRef maddr_phi,
  453. LLVMValueRef start_offset, LLVMValueRef max_addr, LLVMValueRef bytes,
  454. bool is_memory64, bool is_target_64bit)
  455. {
  456. LLVMBasicBlockRef app_addr_in_cache_shared_heap, app_addr_in_linear_mem;
  457. LLVMBasicBlockRef app_addr_in_shared_heap_chain = NULL,
  458. check_shared_heap_chain = NULL;
  459. LLVMValueRef cmp, cmp1, cmp2, shared_heap_start_off, shared_heap_end_off,
  460. maddr = NULL, max_offset;
  461. /* On 64/32-bit target, the offset is 64/32-bit */
  462. LLVMTypeRef offset_type = is_target_64bit ? I64_TYPE : I32_TYPE;
  463. if (!setup_shared_heap_blocks(
  464. comp_ctx, func_ctx, block_curr, &app_addr_in_cache_shared_heap,
  465. &app_addr_in_linear_mem, &app_addr_in_shared_heap_chain,
  466. &check_shared_heap_chain))
  467. goto fail;
  468. LLVMMoveBasicBlockAfter(block_maddr_phi, check_succ);
  469. /* Early branching when it's not in shared heap chain at all */
  470. if (comp_ctx->enable_shared_chain
  471. && !build_check_app_addr_in_shared_heap_chain(
  472. comp_ctx, func_ctx, start_offset, app_addr_in_shared_heap_chain,
  473. app_addr_in_linear_mem))
  474. goto fail;
  475. /* Load the local variable of the function */
  476. BUILD_LOAD_PTR(func_ctx->shared_heap_start_off, offset_type,
  477. shared_heap_start_off);
  478. BUILD_LOAD_PTR(func_ctx->shared_heap_end_off, offset_type,
  479. shared_heap_end_off);
  480. /* Check if the app address is in the cache shared heap range.
  481. * If yes, branch to the cache branch; if not, check the shared heap chain
  482. */
  483. BUILD_ICMP(LLVMIntUGE, start_offset, shared_heap_start_off, cmp,
  484. "cmp_cache_shared_heap_start");
  485. BUILD_OP(Add, max_addr, is_target_64bit ? I64_NEG_ONE : I32_NEG_ONE,
  486. max_offset, "max_offset");
  487. BUILD_ICMP(LLVMIntULE, max_offset, shared_heap_end_off, cmp1,
  488. "cmp_cache_shared_heap_end");
  489. BUILD_OP(And, cmp, cmp1, cmp2, "is_in_cache_shared_heap");
  490. /* Conditional branching based on whether in cached shared heap */
  491. if (!build_shared_heap_conditional_branching(
  492. comp_ctx, func_ctx, cmp2, app_addr_in_cache_shared_heap,
  493. app_addr_in_linear_mem, check_shared_heap_chain, bytes,
  494. start_offset, is_memory64))
  495. goto fail;
  496. SET_BUILD_POS(app_addr_in_cache_shared_heap);
  497. if (!build_get_maddr_in_cache_shared_heap(comp_ctx, func_ctx, start_offset,
  498. &maddr))
  499. goto fail;
  500. LLVMAddIncoming(maddr_phi, &maddr, &app_addr_in_cache_shared_heap, 1);
  501. BUILD_BR(block_maddr_phi);
  502. SET_BUILD_POS(app_addr_in_linear_mem);
  503. return true;
  504. fail:
  505. return false;
  506. }
  507. #endif
  508. LLVMValueRef
  509. aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  510. mem_offset_t offset, uint32 bytes, bool enable_segue,
  511. unsigned int *alignp)
  512. {
  513. LLVMValueRef offset_const =
  514. MEMORY64_COND_VALUE(I64_CONST(offset), I32_CONST(offset));
  515. LLVMValueRef addr, maddr, offset1, cmp1, cmp;
  516. LLVMValueRef mem_base_addr, mem_check_bound;
  517. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  518. LLVMBasicBlockRef check_succ;
  519. AOTValue *aot_value_top;
  520. uint32 local_idx_of_aot_value = 0;
  521. uint64 const_value;
  522. bool is_target_64bit, is_local_of_aot_value = false;
  523. bool is_const = false;
  524. #if WASM_ENABLE_SHARED_MEMORY != 0
  525. bool is_shared_memory =
  526. comp_ctx->comp_data->memories[0].flags & SHARED_MEMORY_FLAG;
  527. #endif
  528. #if WASM_ENABLE_MEMORY64 == 0
  529. bool is_memory64 = false;
  530. #else
  531. bool is_memory64 = IS_MEMORY64;
  532. #endif
  533. #if WASM_ENABLE_SHARED_HEAP != 0
  534. LLVMValueRef maddr_phi = NULL;
  535. LLVMBasicBlockRef block_maddr_phi = NULL;
  536. #endif
  537. is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  538. if (comp_ctx->is_indirect_mode
  539. && aot_intrinsic_check_capability(
  540. comp_ctx, MEMORY64_COND_VALUE("i64.const", "i32.const"))) {
  541. WASMValue wasm_value;
  542. #if WASM_ENABLE_MEMORY64 != 0
  543. if (IS_MEMORY64) {
  544. wasm_value.i64 = offset;
  545. }
  546. else
  547. #endif
  548. {
  549. wasm_value.i32 = (int32)offset;
  550. }
  551. offset_const = aot_load_const_from_table(
  552. comp_ctx, func_ctx->native_symbol, &wasm_value,
  553. MEMORY64_COND_VALUE(VALUE_TYPE_I64, VALUE_TYPE_I32));
  554. if (!offset_const) {
  555. return NULL;
  556. }
  557. }
  558. else {
  559. CHECK_LLVM_CONST(offset_const);
  560. }
  561. /* Get memory base address and memory data size */
  562. if (func_ctx->mem_space_unchanged
  563. #if WASM_ENABLE_SHARED_MEMORY != 0
  564. || is_shared_memory
  565. #endif
  566. ) {
  567. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  568. }
  569. else {
  570. if (!(mem_base_addr = LLVMBuildLoad2(
  571. comp_ctx->builder, OPQ_PTR_TYPE,
  572. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  573. aot_set_last_error("llvm build load failed.");
  574. goto fail;
  575. }
  576. }
  577. aot_value_top =
  578. func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  579. if (aot_value_top) {
  580. /* aot_value_top is freed in the following POP_I32(addr),
  581. so save its fields here for further use */
  582. is_local_of_aot_value = aot_value_top->is_local;
  583. is_const = aot_value_top->is_const;
  584. local_idx_of_aot_value = aot_value_top->local_idx;
  585. const_value = aot_value_top->const_value;
  586. }
  587. POP_MEM_OFFSET(addr);
  588. /*
  589. * Note: not throw the integer-overflow-exception here since it must
  590. * have been thrown when converting float to integer before
  591. */
  592. /* return address directly if constant offset and inside memory space */
  593. if (LLVMIsEfficientConstInt(addr) || is_const) {
  594. uint64 value;
  595. if (LLVMIsEfficientConstInt(addr)) {
  596. value = (uint64)LLVMConstIntGetZExtValue(addr);
  597. }
  598. else {
  599. value = const_value;
  600. }
  601. uint64 mem_offset = value + (uint64)offset;
  602. uint32 num_bytes_per_page =
  603. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  604. uint32 init_page_count =
  605. comp_ctx->comp_data->memories[0].init_page_count;
  606. uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
  607. if (alignp != NULL) {
  608. /*
  609. * A note about max_align below:
  610. * the assumption here is the base address of a linear memory
  611. * has the natural alignment. for platforms using mmap, it can
  612. * be even larger. for now, use a conservative value.
  613. */
  614. const unsigned int max_align = 8;
  615. int shift = ffs((int)(unsigned int)mem_offset);
  616. if (shift == 0) {
  617. *alignp = max_align;
  618. }
  619. else {
  620. unsigned int align = 1 << (shift - 1);
  621. if (align > max_align) {
  622. align = max_align;
  623. }
  624. *alignp = align;
  625. }
  626. }
  627. if (mem_offset + bytes <= mem_data_size) {
  628. /* inside memory space */
  629. if (comp_ctx->pointer_size == sizeof(uint64))
  630. offset1 = I64_CONST(mem_offset);
  631. else
  632. offset1 = I32_CONST((uint32)mem_offset);
  633. CHECK_LLVM_CONST(offset1);
  634. if (!enable_segue) {
  635. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder,
  636. INT8_TYPE, mem_base_addr,
  637. &offset1, 1, "maddr"))) {
  638. aot_set_last_error("llvm build add failed.");
  639. goto fail;
  640. }
  641. }
  642. else {
  643. if (!(maddr = LLVMBuildIntToPtr(comp_ctx->builder, offset1,
  644. INT8_PTR_TYPE_GS, "maddr"))) {
  645. aot_set_last_error("llvm build IntToPtr failed.");
  646. goto fail;
  647. }
  648. }
  649. return maddr;
  650. }
  651. }
  652. else if (alignp != NULL) {
  653. *alignp = 1;
  654. }
  655. /* The overflow check needs to be done under following conditions:
  656. * 1. In 64-bit target, offset and addr will be extended to 64-bit
  657. * 1.1 offset + addr can overflow when it's memory64
  658. * 1.2 no overflow when it's memory32
  659. * 2. In 32-bit target, offset and addr will be 32-bit
  660. * 2.1 offset + addr can overflow when it's memory32
  661. */
  662. if (is_target_64bit) {
  663. if (!(offset_const = LLVMBuildZExt(comp_ctx->builder, offset_const,
  664. I64_TYPE, "offset_i64"))
  665. || !(addr = LLVMBuildZExt(comp_ctx->builder, addr, I64_TYPE,
  666. "addr_i64"))) {
  667. aot_set_last_error("llvm build zero extend failed.");
  668. goto fail;
  669. }
  670. }
  671. /* offset1 = offset + addr; */
  672. BUILD_OP(Add, offset_const, addr, offset1, "offset1");
  673. /* 1.1 offset + addr can overflow when it's memory64
  674. * 2.1 Or when it's on 32-bit platform */
  675. if (is_memory64 || !is_target_64bit) {
  676. /* Check whether integer overflow occurs in offset + addr */
  677. LLVMBasicBlockRef check_integer_overflow_end;
  678. ADD_BASIC_BLOCK(check_integer_overflow_end,
  679. "check_integer_overflow_end");
  680. LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);
  681. BUILD_ICMP(LLVMIntULT, offset1, offset_const, cmp1, "cmp1");
  682. if (!aot_emit_exception(comp_ctx, func_ctx,
  683. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp1,
  684. check_integer_overflow_end)) {
  685. goto fail;
  686. }
  687. SET_BUILD_POS(check_integer_overflow_end);
  688. block_curr = check_integer_overflow_end;
  689. }
  690. #if WASM_ENABLE_SHARED_HEAP != 0
  691. if (comp_ctx->enable_shared_heap
  692. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  693. ADD_BASIC_BLOCK(block_maddr_phi, "maddr_phi");
  694. SET_BUILD_POS(block_maddr_phi);
  695. if (!(maddr_phi =
  696. LLVMBuildPhi(comp_ctx->builder,
  697. enable_segue ? INT8_PTR_TYPE_GS : INT8_PTR_TYPE,
  698. "maddr_phi"))) {
  699. aot_set_last_error("llvm build phi failed");
  700. goto fail;
  701. }
  702. SET_BUILD_POS(block_curr);
  703. if (!aot_check_shared_heap_memory_overflow(
  704. comp_ctx, func_ctx, block_curr, block_maddr_phi, maddr_phi,
  705. offset1, mem_base_addr, bytes, is_memory64, is_target_64bit,
  706. enable_segue)) {
  707. goto fail;
  708. }
  709. }
  710. #endif
  711. if (comp_ctx->enable_bound_check
  712. && !(is_local_of_aot_value
  713. && aot_checked_addr_list_find(func_ctx, local_idx_of_aot_value,
  714. offset, bytes))) {
  715. uint32 init_page_count =
  716. comp_ctx->comp_data->memories[0].init_page_count;
  717. if (init_page_count == 0) {
  718. LLVMValueRef mem_size;
  719. if (!(mem_size = get_memory_curr_page_count(comp_ctx, func_ctx))) {
  720. goto fail;
  721. }
  722. BUILD_ICMP(LLVMIntEQ, mem_size,
  723. MEMORY64_COND_VALUE(I64_ZERO, I32_ZERO), cmp, "is_zero");
  724. ADD_BASIC_BLOCK(check_succ, "check_mem_size_succ");
  725. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  726. if (!aot_emit_exception(comp_ctx, func_ctx,
  727. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  728. check_succ)) {
  729. goto fail;
  730. }
  731. SET_BUILD_POS(check_succ);
  732. block_curr = check_succ;
  733. }
  734. if (!(mem_check_bound =
  735. get_memory_check_bound(comp_ctx, func_ctx, bytes))) {
  736. goto fail;
  737. }
  738. BUILD_ICMP(LLVMIntUGT, offset1, mem_check_bound, cmp, "cmp");
  739. /* Add basic blocks */
  740. ADD_BASIC_BLOCK(check_succ, "check_succ");
  741. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  742. if (!aot_emit_exception(comp_ctx, func_ctx,
  743. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  744. check_succ)) {
  745. goto fail;
  746. }
  747. SET_BUILD_POS(check_succ);
  748. if (is_local_of_aot_value) {
  749. if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
  750. offset, bytes))
  751. goto fail;
  752. }
  753. }
  754. if (!enable_segue) {
  755. /* maddr = mem_base_addr + offset1 */
  756. if (!(maddr =
  757. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  758. mem_base_addr, &offset1, 1, "maddr"))) {
  759. aot_set_last_error("llvm build add failed.");
  760. goto fail;
  761. }
  762. }
  763. else {
  764. LLVMValueRef maddr_base;
  765. if (!(maddr_base = LLVMBuildIntToPtr(comp_ctx->builder, addr,
  766. INT8_PTR_TYPE_GS, "maddr_base"))) {
  767. aot_set_last_error("llvm build int to ptr failed.");
  768. goto fail;
  769. }
  770. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  771. maddr_base, &offset_const, 1,
  772. "maddr"))) {
  773. aot_set_last_error("llvm build inboundgep failed.");
  774. goto fail;
  775. }
  776. }
  777. #if WASM_ENABLE_SHARED_HEAP != 0
  778. if (comp_ctx->enable_shared_heap
  779. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  780. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  781. LLVMAddIncoming(maddr_phi, &maddr, &block_curr, 1);
  782. if (!LLVMBuildBr(comp_ctx->builder, block_maddr_phi)) {
  783. aot_set_last_error("llvm build br failed");
  784. goto fail;
  785. }
  786. SET_BUILD_POS(block_maddr_phi);
  787. return maddr_phi;
  788. }
  789. else
  790. #endif
  791. return maddr;
  792. fail:
  793. return NULL;
  794. }
  795. #define BUILD_PTR_CAST(ptr_type) \
  796. do { \
  797. if (!(maddr = LLVMBuildBitCast(comp_ctx->builder, maddr, ptr_type, \
  798. "data_ptr"))) { \
  799. aot_set_last_error("llvm build bit cast failed."); \
  800. goto fail; \
  801. } \
  802. } while (0)
  803. #define BUILD_LOAD(data_type) \
  804. do { \
  805. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  806. "data"))) { \
  807. aot_set_last_error("llvm build load failed."); \
  808. goto fail; \
  809. } \
  810. LLVMSetAlignment(value, known_align); \
  811. } while (0)
  812. #define BUILD_STORE() \
  813. do { \
  814. LLVMValueRef res; \
  815. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  816. aot_set_last_error("llvm build store failed."); \
  817. goto fail; \
  818. } \
  819. LLVMSetAlignment(res, known_align); \
  820. } while (0)
  821. #define BUILD_SIGN_EXT(dst_type) \
  822. do { \
  823. if (!(value = LLVMBuildSExt(comp_ctx->builder, value, dst_type, \
  824. "data_s_ext"))) { \
  825. aot_set_last_error("llvm build sign ext failed."); \
  826. goto fail; \
  827. } \
  828. } while (0)
  829. #define BUILD_ZERO_EXT(dst_type) \
  830. do { \
  831. if (!(value = LLVMBuildZExt(comp_ctx->builder, value, dst_type, \
  832. "data_z_ext"))) { \
  833. aot_set_last_error("llvm build zero ext failed."); \
  834. goto fail; \
  835. } \
  836. } while (0)
  837. #if WASM_ENABLE_SHARED_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0
  838. bool
  839. check_memory_alignment(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  840. LLVMValueRef addr, uint32 align)
  841. {
  842. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  843. LLVMBasicBlockRef check_align_succ;
  844. LLVMValueRef align_mask = I32_CONST(((uint32)1 << align) - 1);
  845. LLVMValueRef res;
  846. CHECK_LLVM_CONST(align_mask);
  847. /* Convert pointer to int */
  848. if (!(addr = LLVMBuildPtrToInt(comp_ctx->builder, addr, I32_TYPE,
  849. "address"))) {
  850. aot_set_last_error("llvm build ptr to int failed.");
  851. goto fail;
  852. }
  853. /* The memory address should be aligned */
  854. BUILD_OP(And, addr, align_mask, res, "and");
  855. BUILD_ICMP(LLVMIntNE, res, I32_ZERO, res, "cmp");
  856. /* Add basic blocks */
  857. ADD_BASIC_BLOCK(check_align_succ, "check_align_succ");
  858. LLVMMoveBasicBlockAfter(check_align_succ, block_curr);
  859. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNALIGNED_ATOMIC, true,
  860. res, check_align_succ)) {
  861. goto fail;
  862. }
  863. SET_BUILD_POS(check_align_succ);
  864. return true;
  865. fail:
  866. return false;
  867. }
  868. #endif /* WASM_ENABLE_SHARED_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0 */
  869. #if WASM_ENABLE_SHARED_MEMORY != 0
  870. #define BUILD_ATOMIC_LOAD(align, data_type) \
  871. do { \
  872. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  873. goto fail; \
  874. } \
  875. if (!(value = LLVMBuildLoad2(comp_ctx->builder, data_type, maddr, \
  876. "data"))) { \
  877. aot_set_last_error("llvm build load failed."); \
  878. goto fail; \
  879. } \
  880. LLVMSetAlignment(value, 1 << align); \
  881. LLVMSetVolatile(value, true); \
  882. LLVMSetOrdering(value, LLVMAtomicOrderingSequentiallyConsistent); \
  883. } while (0)
  884. #define BUILD_ATOMIC_STORE(align) \
  885. do { \
  886. LLVMValueRef res; \
  887. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  888. goto fail; \
  889. } \
  890. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  891. aot_set_last_error("llvm build store failed."); \
  892. goto fail; \
  893. } \
  894. LLVMSetAlignment(res, 1 << align); \
  895. LLVMSetVolatile(res, true); \
  896. LLVMSetOrdering(res, LLVMAtomicOrderingSequentiallyConsistent); \
  897. } while (0)
  898. #endif
  899. bool
  900. aot_compile_op_i32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  901. uint32 align, mem_offset_t offset, uint32 bytes,
  902. bool sign, bool atomic)
  903. {
  904. LLVMValueRef maddr, value = NULL;
  905. LLVMTypeRef data_type;
  906. bool enable_segue = comp_ctx->enable_segue_i32_load;
  907. unsigned int known_align;
  908. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  909. enable_segue, &known_align)))
  910. return false;
  911. switch (bytes) {
  912. case 4:
  913. if (!enable_segue)
  914. BUILD_PTR_CAST(INT32_PTR_TYPE);
  915. else
  916. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  917. #if WASM_ENABLE_SHARED_MEMORY != 0
  918. if (atomic)
  919. BUILD_ATOMIC_LOAD(align, I32_TYPE);
  920. else
  921. #endif
  922. BUILD_LOAD(I32_TYPE);
  923. break;
  924. case 2:
  925. case 1:
  926. if (bytes == 2) {
  927. if (!enable_segue)
  928. BUILD_PTR_CAST(INT16_PTR_TYPE);
  929. else
  930. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  931. data_type = INT16_TYPE;
  932. }
  933. else {
  934. if (!enable_segue)
  935. BUILD_PTR_CAST(INT8_PTR_TYPE);
  936. else
  937. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  938. data_type = INT8_TYPE;
  939. }
  940. #if WASM_ENABLE_SHARED_MEMORY != 0
  941. if (atomic) {
  942. BUILD_ATOMIC_LOAD(align, data_type);
  943. BUILD_ZERO_EXT(I32_TYPE);
  944. }
  945. else
  946. #endif
  947. {
  948. BUILD_LOAD(data_type);
  949. if (sign)
  950. BUILD_SIGN_EXT(I32_TYPE);
  951. else
  952. BUILD_ZERO_EXT(I32_TYPE);
  953. }
  954. break;
  955. default:
  956. bh_assert(0);
  957. break;
  958. }
  959. PUSH_I32(value);
  960. (void)data_type;
  961. return true;
  962. fail:
  963. return false;
  964. }
  965. bool
  966. aot_compile_op_i64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  967. uint32 align, mem_offset_t offset, uint32 bytes,
  968. bool sign, bool atomic)
  969. {
  970. LLVMValueRef maddr, value = NULL;
  971. LLVMTypeRef data_type;
  972. bool enable_segue = comp_ctx->enable_segue_i64_load;
  973. unsigned int known_align;
  974. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  975. enable_segue, &known_align)))
  976. return false;
  977. switch (bytes) {
  978. case 8:
  979. if (!enable_segue)
  980. BUILD_PTR_CAST(INT64_PTR_TYPE);
  981. else
  982. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  983. #if WASM_ENABLE_SHARED_MEMORY != 0
  984. if (atomic)
  985. BUILD_ATOMIC_LOAD(align, I64_TYPE);
  986. else
  987. #endif
  988. BUILD_LOAD(I64_TYPE);
  989. break;
  990. case 4:
  991. case 2:
  992. case 1:
  993. if (bytes == 4) {
  994. if (!enable_segue)
  995. BUILD_PTR_CAST(INT32_PTR_TYPE);
  996. else
  997. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  998. data_type = I32_TYPE;
  999. }
  1000. else if (bytes == 2) {
  1001. if (!enable_segue)
  1002. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1003. else
  1004. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1005. data_type = INT16_TYPE;
  1006. }
  1007. else {
  1008. if (!enable_segue)
  1009. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1010. else
  1011. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1012. data_type = INT8_TYPE;
  1013. }
  1014. #if WASM_ENABLE_SHARED_MEMORY != 0
  1015. if (atomic) {
  1016. BUILD_ATOMIC_LOAD(align, data_type);
  1017. BUILD_ZERO_EXT(I64_TYPE);
  1018. }
  1019. else
  1020. #endif
  1021. {
  1022. BUILD_LOAD(data_type);
  1023. if (sign)
  1024. BUILD_SIGN_EXT(I64_TYPE);
  1025. else
  1026. BUILD_ZERO_EXT(I64_TYPE);
  1027. }
  1028. break;
  1029. default:
  1030. bh_assert(0);
  1031. break;
  1032. }
  1033. PUSH_I64(value);
  1034. (void)data_type;
  1035. return true;
  1036. fail:
  1037. return false;
  1038. }
  1039. bool
  1040. aot_compile_op_f32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1041. uint32 align, mem_offset_t offset)
  1042. {
  1043. LLVMValueRef maddr, value;
  1044. bool enable_segue = comp_ctx->enable_segue_f32_load;
  1045. unsigned int known_align;
  1046. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  1047. enable_segue, &known_align)))
  1048. return false;
  1049. if (!enable_segue)
  1050. BUILD_PTR_CAST(F32_PTR_TYPE);
  1051. else
  1052. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  1053. BUILD_LOAD(F32_TYPE);
  1054. PUSH_F32(value);
  1055. return true;
  1056. fail:
  1057. return false;
  1058. }
  1059. bool
  1060. aot_compile_op_f64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1061. uint32 align, mem_offset_t offset)
  1062. {
  1063. LLVMValueRef maddr, value;
  1064. bool enable_segue = comp_ctx->enable_segue_f64_load;
  1065. unsigned int known_align;
  1066. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  1067. enable_segue, &known_align)))
  1068. return false;
  1069. if (!enable_segue)
  1070. BUILD_PTR_CAST(F64_PTR_TYPE);
  1071. else
  1072. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  1073. BUILD_LOAD(F64_TYPE);
  1074. PUSH_F64(value);
  1075. return true;
  1076. fail:
  1077. return false;
  1078. }
  1079. bool
  1080. aot_compile_op_i32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1081. uint32 align, mem_offset_t offset, uint32 bytes,
  1082. bool atomic)
  1083. {
  1084. LLVMValueRef maddr, value;
  1085. bool enable_segue = comp_ctx->enable_segue_i32_store;
  1086. POP_I32(value);
  1087. unsigned int known_align;
  1088. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1089. enable_segue, &known_align)))
  1090. return false;
  1091. switch (bytes) {
  1092. case 4:
  1093. if (!enable_segue)
  1094. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1095. else
  1096. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1097. break;
  1098. case 2:
  1099. if (!enable_segue)
  1100. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1101. else
  1102. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1103. BUILD_TRUNC(value, INT16_TYPE);
  1104. break;
  1105. case 1:
  1106. if (!enable_segue)
  1107. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1108. else
  1109. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1110. BUILD_TRUNC(value, INT8_TYPE);
  1111. break;
  1112. default:
  1113. bh_assert(0);
  1114. break;
  1115. }
  1116. #if WASM_ENABLE_SHARED_MEMORY != 0
  1117. if (atomic)
  1118. BUILD_ATOMIC_STORE(align);
  1119. else
  1120. #endif
  1121. BUILD_STORE();
  1122. return true;
  1123. fail:
  1124. return false;
  1125. }
  1126. bool
  1127. aot_compile_op_i64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1128. uint32 align, mem_offset_t offset, uint32 bytes,
  1129. bool atomic)
  1130. {
  1131. LLVMValueRef maddr, value;
  1132. bool enable_segue = comp_ctx->enable_segue_i64_store;
  1133. POP_I64(value);
  1134. unsigned int known_align;
  1135. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1136. enable_segue, &known_align)))
  1137. return false;
  1138. switch (bytes) {
  1139. case 8:
  1140. if (!enable_segue)
  1141. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1142. else
  1143. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1144. break;
  1145. case 4:
  1146. if (!enable_segue)
  1147. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1148. else
  1149. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1150. BUILD_TRUNC(value, I32_TYPE);
  1151. break;
  1152. case 2:
  1153. if (!enable_segue)
  1154. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1155. else
  1156. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1157. BUILD_TRUNC(value, INT16_TYPE);
  1158. break;
  1159. case 1:
  1160. if (!enable_segue)
  1161. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1162. else
  1163. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1164. BUILD_TRUNC(value, INT8_TYPE);
  1165. break;
  1166. default:
  1167. bh_assert(0);
  1168. break;
  1169. }
  1170. #if WASM_ENABLE_SHARED_MEMORY != 0
  1171. if (atomic)
  1172. BUILD_ATOMIC_STORE(align);
  1173. else
  1174. #endif
  1175. BUILD_STORE();
  1176. return true;
  1177. fail:
  1178. return false;
  1179. }
  1180. bool
  1181. aot_compile_op_f32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1182. uint32 align, mem_offset_t offset)
  1183. {
  1184. LLVMValueRef maddr, value;
  1185. bool enable_segue = comp_ctx->enable_segue_f32_store;
  1186. POP_F32(value);
  1187. unsigned int known_align;
  1188. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
  1189. enable_segue, &known_align)))
  1190. return false;
  1191. if (!enable_segue)
  1192. BUILD_PTR_CAST(F32_PTR_TYPE);
  1193. else
  1194. BUILD_PTR_CAST(F32_PTR_TYPE_GS);
  1195. BUILD_STORE();
  1196. return true;
  1197. fail:
  1198. return false;
  1199. }
  1200. bool
  1201. aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1202. uint32 align, mem_offset_t offset)
  1203. {
  1204. LLVMValueRef maddr, value;
  1205. bool enable_segue = comp_ctx->enable_segue_f64_store;
  1206. POP_F64(value);
  1207. unsigned int known_align;
  1208. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
  1209. enable_segue, &known_align)))
  1210. return false;
  1211. if (!enable_segue)
  1212. BUILD_PTR_CAST(F64_PTR_TYPE);
  1213. else
  1214. BUILD_PTR_CAST(F64_PTR_TYPE_GS);
  1215. BUILD_STORE();
  1216. return true;
  1217. fail:
  1218. return false;
  1219. }
  1220. static LLVMValueRef
  1221. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1222. {
  1223. LLVMValueRef mem_size;
  1224. if (func_ctx->mem_space_unchanged) {
  1225. mem_size = func_ctx->mem_info[0].mem_cur_page_count_addr;
  1226. }
  1227. else {
  1228. if (!(mem_size = LLVMBuildLoad2(
  1229. comp_ctx->builder, I32_TYPE,
  1230. func_ctx->mem_info[0].mem_cur_page_count_addr, "mem_size"))) {
  1231. aot_set_last_error("llvm build load failed.");
  1232. goto fail;
  1233. }
  1234. }
  1235. return LLVMBuildIntCast(comp_ctx->builder, mem_size,
  1236. MEMORY64_COND_VALUE(I64_TYPE, I32_TYPE), "");
  1237. fail:
  1238. return NULL;
  1239. }
  1240. bool
  1241. aot_compile_op_memory_size(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1242. {
  1243. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  1244. if (mem_size)
  1245. PUSH_PAGE_COUNT(mem_size);
  1246. return mem_size ? true : false;
  1247. fail:
  1248. return false;
  1249. }
  1250. bool
  1251. aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1252. {
  1253. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  1254. LLVMValueRef delta, param_values[2], ret_value, func, value;
  1255. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  1256. int32 func_index;
  1257. #if WASM_ENABLE_MEMORY64 != 0
  1258. LLVMValueRef u32_max, u32_cmp_result;
  1259. #endif
  1260. if (!mem_size)
  1261. return false;
  1262. POP_PAGE_COUNT(delta);
  1263. /* TODO: multi-memory aot_enlarge_memory_with_idx() */
  1264. /* Function type of aot_enlarge_memory() */
  1265. param_types[0] = INT8_PTR_TYPE;
  1266. param_types[1] = I32_TYPE;
  1267. ret_type = INT8_TYPE;
  1268. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  1269. aot_set_last_error("llvm add function type failed.");
  1270. return false;
  1271. }
  1272. if (comp_ctx->is_jit_mode) {
  1273. /* JIT mode, call the function directly */
  1274. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1275. aot_set_last_error("llvm add pointer type failed.");
  1276. return false;
  1277. }
  1278. if (!(value = I64_CONST((uint64)(uintptr_t)wasm_enlarge_memory))
  1279. || !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
  1280. aot_set_last_error("create LLVM value failed.");
  1281. return false;
  1282. }
  1283. }
  1284. else if (comp_ctx->is_indirect_mode) {
  1285. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1286. aot_set_last_error("create LLVM function type failed.");
  1287. return false;
  1288. }
  1289. func_index =
  1290. aot_get_native_symbol_index(comp_ctx, "aot_enlarge_memory");
  1291. if (func_index < 0) {
  1292. return false;
  1293. }
  1294. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1295. func_ptr_type, func_index))) {
  1296. return false;
  1297. }
  1298. }
  1299. else {
  1300. char *func_name = "aot_enlarge_memory";
  1301. /* AOT mode, declare the function */
  1302. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  1303. && !(func =
  1304. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  1305. aot_set_last_error("llvm add function failed.");
  1306. return false;
  1307. }
  1308. }
  1309. /* Call function aot_enlarge_memory() */
  1310. param_values[0] = func_ctx->aot_inst;
  1311. param_values[1] = LLVMBuildTrunc(comp_ctx->builder, delta, I32_TYPE, "");
  1312. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1313. param_values, 2, "call"))) {
  1314. aot_set_last_error("llvm build call failed.");
  1315. return false;
  1316. }
  1317. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_grow_ret");
  1318. #if WASM_ENABLE_MEMORY64 != 0
  1319. if (IS_MEMORY64) {
  1320. if (!(u32_max = I64_CONST(UINT32_MAX))) {
  1321. aot_set_last_error("llvm build const failed");
  1322. return false;
  1323. }
  1324. BUILD_ICMP(LLVMIntULE, delta, u32_max, u32_cmp_result, "page_size_cmp");
  1325. BUILD_OP(And, ret_value, u32_cmp_result, ret_value, "and");
  1326. }
  1327. #endif
  1328. /* ret_value = ret_value == true ? pre_page_count : -1 */
  1329. if (!(ret_value = LLVMBuildSelect(
  1330. comp_ctx->builder, ret_value, mem_size,
  1331. MEMORY64_COND_VALUE(I64_NEG_ONE, I32_NEG_ONE), "mem_grow_ret"))) {
  1332. aot_set_last_error("llvm build select failed.");
  1333. return false;
  1334. }
  1335. PUSH_PAGE_COUNT(ret_value);
  1336. return true;
  1337. fail:
  1338. return false;
  1339. }
  1340. #if WASM_ENABLE_BULK_MEMORY_OPT != 0 || WASM_ENABLE_STRINGREF != 0
  1341. LLVMValueRef
  1342. check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1343. LLVMValueRef offset, LLVMValueRef bytes)
  1344. {
  1345. LLVMValueRef maddr, max_addr, cmp, cmp1, offset1;
  1346. LLVMValueRef mem_base_addr;
  1347. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1348. LLVMBasicBlockRef check_succ;
  1349. LLVMValueRef mem_size;
  1350. bool is_target_64bit;
  1351. #if WASM_ENABLE_MEMORY64 == 0
  1352. bool is_memory64 = false;
  1353. #else
  1354. bool is_memory64 = IS_MEMORY64;
  1355. #endif
  1356. #if WASM_ENABLE_SHARED_HEAP != 0
  1357. LLVMValueRef maddr_phi = NULL;
  1358. LLVMBasicBlockRef block_maddr_phi = NULL;
  1359. #endif
  1360. is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  1361. /* Get memory base address and memory data size */
  1362. #if WASM_ENABLE_SHARED_MEMORY != 0
  1363. bool is_shared_memory = comp_ctx->comp_data->memories[0].flags & 0x02;
  1364. if (func_ctx->mem_space_unchanged || is_shared_memory) {
  1365. #else
  1366. if (func_ctx->mem_space_unchanged) {
  1367. #endif
  1368. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  1369. }
  1370. else {
  1371. if (!(mem_base_addr = LLVMBuildLoad2(
  1372. comp_ctx->builder, OPQ_PTR_TYPE,
  1373. func_ctx->mem_info[0].mem_base_addr, "mem_base"))) {
  1374. aot_set_last_error("llvm build load failed.");
  1375. goto fail;
  1376. }
  1377. }
  1378. /*
  1379. * Note: not throw the integer-overflow-exception here since it must
  1380. * have been thrown when converting float to integer before
  1381. */
  1382. /* return address directly if constant offset and inside memory space */
  1383. if (LLVMIsEfficientConstInt(offset) && LLVMIsEfficientConstInt(bytes)) {
  1384. uint64 mem_offset = (uint64)LLVMConstIntGetZExtValue(offset);
  1385. uint64 mem_len = (uint64)LLVMConstIntGetZExtValue(bytes);
  1386. uint32 num_bytes_per_page =
  1387. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  1388. uint32 init_page_count =
  1389. comp_ctx->comp_data->memories[0].init_page_count;
  1390. uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
  1391. if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) {
  1392. /* inside memory space */
  1393. /* maddr = mem_base_addr + moffset */
  1394. /* Perform zero extension in advance to avoid LLVMBuildInBoundsGEP2
  1395. * interpreting a negative address due to sign extension when
  1396. * mem_offset >= 2GiB */
  1397. if (comp_ctx->pointer_size == sizeof(uint64)) {
  1398. offset1 = I64_CONST(mem_offset);
  1399. }
  1400. else {
  1401. offset1 = I32_CONST((uint32)mem_offset);
  1402. }
  1403. CHECK_LLVM_CONST(offset1);
  1404. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1405. mem_base_addr, &offset1, 1,
  1406. "maddr"))) {
  1407. aot_set_last_error("llvm build add failed.");
  1408. goto fail;
  1409. }
  1410. return maddr;
  1411. }
  1412. }
  1413. if (func_ctx->mem_space_unchanged) {
  1414. mem_size = func_ctx->mem_info[0].mem_data_size_addr;
  1415. }
  1416. else {
  1417. if (!(mem_size = LLVMBuildLoad2(
  1418. comp_ctx->builder, I64_TYPE,
  1419. func_ctx->mem_info[0].mem_data_size_addr, "mem_size"))) {
  1420. aot_set_last_error("llvm build load failed.");
  1421. goto fail;
  1422. }
  1423. }
  1424. ADD_BASIC_BLOCK(check_succ, "check_succ");
  1425. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  1426. /* Same logic with aot_check_memory_overflow, offset and bytes are 32/64
  1427. * bits on 32/64 bits platform */
  1428. if (is_target_64bit) {
  1429. offset =
  1430. LLVMBuildZExt(comp_ctx->builder, offset, I64_TYPE, "extend_offset");
  1431. bytes = LLVMBuildZExt(comp_ctx->builder, bytes, I64_TYPE, "extend_len");
  1432. if (!offset || !bytes) {
  1433. aot_set_last_error("llvm build zext failed.");
  1434. goto fail;
  1435. }
  1436. }
  1437. BUILD_OP(Add, offset, bytes, max_addr, "max_addr");
  1438. /* Check overflow when it's memory64 or it's on 32 bits platform */
  1439. if (is_memory64 || !is_target_64bit) {
  1440. /* Check whether integer overflow occurs in offset + bytes */
  1441. LLVMBasicBlockRef check_integer_overflow_end;
  1442. ADD_BASIC_BLOCK(check_integer_overflow_end,
  1443. "check_integer_overflow_end");
  1444. LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);
  1445. /* offset + bytes can overflow yet is valid(for example, 0xffffffff, 1),
  1446. * allow it to be 0(either 0, 0 or overflow and valid) */
  1447. BUILD_ICMP(LLVMIntULT, max_addr, offset, cmp, "cmp");
  1448. BUILD_ICMP(LLVMIntNE, max_addr, is_target_64bit ? I64_ZERO : I32_ZERO,
  1449. cmp1, "cmp1");
  1450. BUILD_OP(And, cmp, cmp1, cmp, "overflow");
  1451. if (!aot_emit_exception(comp_ctx, func_ctx,
  1452. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  1453. check_integer_overflow_end)) {
  1454. goto fail;
  1455. }
  1456. SET_BUILD_POS(check_integer_overflow_end);
  1457. block_curr = check_integer_overflow_end;
  1458. }
  1459. #if WASM_ENABLE_SHARED_HEAP != 0
  1460. if (comp_ctx->enable_shared_heap
  1461. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  1462. ADD_BASIC_BLOCK(block_maddr_phi, "maddr_phi");
  1463. SET_BUILD_POS(block_maddr_phi);
  1464. if (!(maddr_phi = LLVMBuildPhi(comp_ctx->builder, INT8_PTR_TYPE,
  1465. "maddr_phi"))) {
  1466. aot_set_last_error("llvm build phi failed");
  1467. goto fail;
  1468. }
  1469. SET_BUILD_POS(block_curr);
  1470. if (!aot_check_bulk_memory_shared_heap_memory_overflow(
  1471. comp_ctx, func_ctx, block_curr, block_maddr_phi, check_succ,
  1472. maddr_phi, offset, max_addr, bytes, is_memory64,
  1473. is_target_64bit)) {
  1474. goto fail;
  1475. }
  1476. }
  1477. #endif
  1478. /* mem_size is always 64-bit, extend max_addr on 32 bits platform */
  1479. if (!is_target_64bit
  1480. && !(max_addr = LLVMBuildZExt(comp_ctx->builder, max_addr, I64_TYPE,
  1481. "extend_max_addr"))) {
  1482. aot_set_last_error("llvm build zext failed.");
  1483. goto fail;
  1484. }
  1485. BUILD_ICMP(LLVMIntUGT, max_addr, mem_size, cmp, "cmp_max_mem_addr");
  1486. if (!aot_emit_exception(comp_ctx, func_ctx,
  1487. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  1488. check_succ)) {
  1489. goto fail;
  1490. }
  1491. /* maddr = mem_base_addr + offset */
  1492. if (!(maddr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  1493. mem_base_addr, &offset, 1, "maddr"))) {
  1494. aot_set_last_error("llvm build add failed.");
  1495. goto fail;
  1496. }
  1497. #if WASM_ENABLE_SHARED_HEAP != 0
  1498. if (comp_ctx->enable_shared_heap
  1499. || comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
  1500. block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1501. LLVMAddIncoming(maddr_phi, &maddr, &block_curr, 1);
  1502. if (!LLVMBuildBr(comp_ctx->builder, block_maddr_phi)) {
  1503. aot_set_last_error("llvm build br failed");
  1504. goto fail;
  1505. }
  1506. LLVMPositionBuilderAtEnd(comp_ctx->builder, block_maddr_phi);
  1507. return maddr_phi;
  1508. }
  1509. else
  1510. #endif
  1511. return maddr;
  1512. fail:
  1513. return NULL;
  1514. }
  1515. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_STRINGREF != 0 */
  1516. #if WASM_ENABLE_BULK_MEMORY != 0
  1517. bool
  1518. aot_compile_op_memory_init(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1519. uint32 seg_index)
  1520. {
  1521. LLVMValueRef seg, offset, dst, len, param_values[5], ret_value, func, value;
  1522. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1523. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1524. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1525. LLVMBasicBlockRef mem_init_fail, init_success;
  1526. seg = I32_CONST(seg_index);
  1527. POP_I32(len);
  1528. POP_I32(offset);
  1529. POP_MEM_OFFSET(dst);
  1530. if (!zero_extend_u64(comp_ctx, &dst, "dst64")) {
  1531. return false;
  1532. }
  1533. param_types[0] = INT8_PTR_TYPE;
  1534. param_types[1] = I32_TYPE;
  1535. param_types[2] = I32_TYPE;
  1536. param_types[3] = I32_TYPE;
  1537. param_types[4] = SIZE_T_TYPE;
  1538. ret_type = INT8_TYPE;
  1539. if (comp_ctx->is_jit_mode)
  1540. GET_AOT_FUNCTION(llvm_jit_memory_init, 5);
  1541. else
  1542. GET_AOT_FUNCTION(aot_memory_init, 5);
  1543. /* Call function aot_memory_init() */
  1544. param_values[0] = func_ctx->aot_inst;
  1545. param_values[1] = seg;
  1546. param_values[2] = offset;
  1547. param_values[3] = len;
  1548. param_values[4] = dst;
  1549. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1550. param_values, 5, "call"))) {
  1551. aot_set_last_error("llvm build call failed.");
  1552. return false;
  1553. }
  1554. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_init_ret");
  1555. ADD_BASIC_BLOCK(mem_init_fail, "mem_init_fail");
  1556. ADD_BASIC_BLOCK(init_success, "init_success");
  1557. LLVMMoveBasicBlockAfter(mem_init_fail, block_curr);
  1558. LLVMMoveBasicBlockAfter(init_success, block_curr);
  1559. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, init_success,
  1560. mem_init_fail)) {
  1561. aot_set_last_error("llvm build cond br failed.");
  1562. goto fail;
  1563. }
  1564. /* If memory.init failed, return this function
  1565. so the runtime can catch the exception */
  1566. LLVMPositionBuilderAtEnd(comp_ctx->builder, mem_init_fail);
  1567. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1568. goto fail;
  1569. }
  1570. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_success);
  1571. return true;
  1572. fail:
  1573. return false;
  1574. }
  1575. bool
  1576. aot_compile_op_data_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1577. uint32 seg_index)
  1578. {
  1579. LLVMValueRef seg, param_values[2], ret_value, func, value;
  1580. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  1581. seg = I32_CONST(seg_index);
  1582. CHECK_LLVM_CONST(seg);
  1583. param_types[0] = INT8_PTR_TYPE;
  1584. param_types[1] = I32_TYPE;
  1585. ret_type = INT8_TYPE;
  1586. if (comp_ctx->is_jit_mode)
  1587. GET_AOT_FUNCTION(llvm_jit_data_drop, 2);
  1588. else
  1589. GET_AOT_FUNCTION(aot_data_drop, 2);
  1590. /* Call function aot_data_drop() */
  1591. param_values[0] = func_ctx->aot_inst;
  1592. param_values[1] = seg;
  1593. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1594. param_values, 2, "call"))) {
  1595. aot_set_last_error("llvm build call failed.");
  1596. return false;
  1597. }
  1598. return true;
  1599. fail:
  1600. return false;
  1601. }
  1602. #endif /* end of WASM_ENABLE_BULK_MEMORY */
  1603. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  1604. bool
  1605. aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1606. {
  1607. LLVMValueRef src, dst, src_addr, dst_addr, len, res;
  1608. bool call_aot_memmove = false;
  1609. POP_MEM_OFFSET(len);
  1610. POP_MEM_OFFSET(src);
  1611. POP_MEM_OFFSET(dst);
  1612. if (!(src_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, src, len)))
  1613. return false;
  1614. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1615. return false;
  1616. if (!zero_extend_u64(comp_ctx, &len, "len64")) {
  1617. return false;
  1618. }
  1619. call_aot_memmove = comp_ctx->is_indirect_mode || comp_ctx->is_jit_mode;
  1620. if (call_aot_memmove) {
  1621. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1622. LLVMValueRef func, params[3];
  1623. param_types[0] = INT8_PTR_TYPE;
  1624. param_types[1] = INT8_PTR_TYPE;
  1625. param_types[2] = SIZE_T_TYPE;
  1626. ret_type = INT8_PTR_TYPE;
  1627. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1628. aot_set_last_error("create LLVM function type failed.");
  1629. return false;
  1630. }
  1631. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1632. aot_set_last_error("create LLVM function pointer type failed.");
  1633. return false;
  1634. }
  1635. if (comp_ctx->is_jit_mode) {
  1636. if (!(func = I64_CONST((uint64)(uintptr_t)aot_memmove))
  1637. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1638. aot_set_last_error("create LLVM value failed.");
  1639. return false;
  1640. }
  1641. }
  1642. else {
  1643. int32 func_index;
  1644. func_index = aot_get_native_symbol_index(comp_ctx, "memmove");
  1645. if (func_index < 0) {
  1646. return false;
  1647. }
  1648. if (!(func =
  1649. aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1650. func_ptr_type, func_index))) {
  1651. return false;
  1652. }
  1653. }
  1654. params[0] = dst_addr;
  1655. params[1] = src_addr;
  1656. params[2] = len;
  1657. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params,
  1658. 3, "call_memmove"))) {
  1659. aot_set_last_error("llvm build memmove failed.");
  1660. return false;
  1661. }
  1662. }
  1663. else {
  1664. if (!(res = LLVMBuildMemMove(comp_ctx->builder, dst_addr, 1, src_addr,
  1665. 1, len))) {
  1666. aot_set_last_error("llvm build memmove failed.");
  1667. return false;
  1668. }
  1669. }
  1670. return true;
  1671. fail:
  1672. return false;
  1673. }
  1674. static void *
  1675. jit_memset(void *s, int c, size_t n)
  1676. {
  1677. return memset(s, c, n);
  1678. }
  1679. bool
  1680. aot_compile_op_memory_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1681. {
  1682. LLVMValueRef val, dst, dst_addr, len, res;
  1683. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1684. LLVMValueRef func, params[3];
  1685. POP_MEM_OFFSET(len);
  1686. POP_I32(val);
  1687. POP_MEM_OFFSET(dst);
  1688. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  1689. return false;
  1690. if (!zero_extend_u64(comp_ctx, &len, "len64")) {
  1691. return false;
  1692. }
  1693. param_types[0] = INT8_PTR_TYPE;
  1694. param_types[1] = I32_TYPE;
  1695. param_types[2] = SIZE_T_TYPE;
  1696. ret_type = INT8_PTR_TYPE;
  1697. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  1698. aot_set_last_error("create LLVM function type failed.");
  1699. return false;
  1700. }
  1701. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  1702. aot_set_last_error("create LLVM function pointer type failed.");
  1703. return false;
  1704. }
  1705. if (comp_ctx->is_jit_mode) {
  1706. if (!(func = I64_CONST((uint64)(uintptr_t)jit_memset))
  1707. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  1708. aot_set_last_error("create LLVM value failed.");
  1709. return false;
  1710. }
  1711. }
  1712. else if (comp_ctx->is_indirect_mode) {
  1713. int32 func_index;
  1714. func_index = aot_get_native_symbol_index(comp_ctx, "memset");
  1715. if (func_index < 0) {
  1716. return false;
  1717. }
  1718. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  1719. func_ptr_type, func_index))) {
  1720. return false;
  1721. }
  1722. }
  1723. else {
  1724. if (!(func = LLVMGetNamedFunction(func_ctx->module, "memset"))
  1725. && !(func =
  1726. LLVMAddFunction(func_ctx->module, "memset", func_type))) {
  1727. aot_set_last_error("llvm add function failed.");
  1728. return false;
  1729. }
  1730. }
  1731. params[0] = dst_addr;
  1732. params[1] = val;
  1733. params[2] = len;
  1734. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, params, 3,
  1735. "call_memset"))) {
  1736. aot_set_last_error("llvm build memset failed.");
  1737. return false;
  1738. }
  1739. return true;
  1740. fail:
  1741. return false;
  1742. }
  1743. #endif /* end of WASM_ENABLE_BULK_MEMORY_OPT */
  1744. #if WASM_ENABLE_SHARED_MEMORY != 0
  1745. bool
  1746. aot_compile_op_atomic_rmw(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1747. uint8 atomic_op, uint8 op_type, uint32 align,
  1748. mem_offset_t offset, uint32 bytes)
  1749. {
  1750. LLVMValueRef maddr, value, result;
  1751. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1752. ? comp_ctx->enable_segue_i32_load
  1753. && comp_ctx->enable_segue_i32_store
  1754. : comp_ctx->enable_segue_i64_load
  1755. && comp_ctx->enable_segue_i64_store;
  1756. if (op_type == VALUE_TYPE_I32)
  1757. POP_I32(value);
  1758. else
  1759. POP_I64(value);
  1760. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1761. enable_segue, NULL)))
  1762. return false;
  1763. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1764. return false;
  1765. switch (bytes) {
  1766. case 8:
  1767. if (!enable_segue)
  1768. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1769. else
  1770. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1771. break;
  1772. case 4:
  1773. if (!enable_segue)
  1774. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1775. else
  1776. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1777. if (op_type == VALUE_TYPE_I64)
  1778. BUILD_TRUNC(value, I32_TYPE);
  1779. break;
  1780. case 2:
  1781. if (!enable_segue)
  1782. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1783. else
  1784. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1785. BUILD_TRUNC(value, INT16_TYPE);
  1786. break;
  1787. case 1:
  1788. if (!enable_segue)
  1789. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1790. else
  1791. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1792. BUILD_TRUNC(value, INT8_TYPE);
  1793. break;
  1794. default:
  1795. bh_assert(0);
  1796. break;
  1797. }
  1798. if (!(result = LLVMBuildAtomicRMW(
  1799. comp_ctx->builder, atomic_op, maddr, value,
  1800. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1801. goto fail;
  1802. }
  1803. LLVMSetVolatile(result, true);
  1804. if (op_type == VALUE_TYPE_I32) {
  1805. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1806. "result_i32"))) {
  1807. goto fail;
  1808. }
  1809. PUSH_I32(result);
  1810. }
  1811. else {
  1812. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1813. "result_i64"))) {
  1814. goto fail;
  1815. }
  1816. PUSH_I64(result);
  1817. }
  1818. return true;
  1819. fail:
  1820. return false;
  1821. }
  1822. bool
  1823. aot_compile_op_atomic_cmpxchg(AOTCompContext *comp_ctx,
  1824. AOTFuncContext *func_ctx, uint8 op_type,
  1825. uint32 align, mem_offset_t offset, uint32 bytes)
  1826. {
  1827. LLVMValueRef maddr, value, expect, result;
  1828. bool enable_segue = (op_type == VALUE_TYPE_I32)
  1829. ? comp_ctx->enable_segue_i32_load
  1830. && comp_ctx->enable_segue_i32_store
  1831. : comp_ctx->enable_segue_i64_load
  1832. && comp_ctx->enable_segue_i64_store;
  1833. if (op_type == VALUE_TYPE_I32) {
  1834. POP_I32(value);
  1835. POP_I32(expect);
  1836. }
  1837. else {
  1838. POP_I64(value);
  1839. POP_I64(expect);
  1840. }
  1841. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1842. enable_segue, NULL)))
  1843. return false;
  1844. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1845. return false;
  1846. switch (bytes) {
  1847. case 8:
  1848. if (!enable_segue)
  1849. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1850. else
  1851. BUILD_PTR_CAST(INT64_PTR_TYPE_GS);
  1852. break;
  1853. case 4:
  1854. if (!enable_segue)
  1855. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1856. else
  1857. BUILD_PTR_CAST(INT32_PTR_TYPE_GS);
  1858. if (op_type == VALUE_TYPE_I64) {
  1859. BUILD_TRUNC(value, I32_TYPE);
  1860. BUILD_TRUNC(expect, I32_TYPE);
  1861. }
  1862. break;
  1863. case 2:
  1864. if (!enable_segue)
  1865. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1866. else
  1867. BUILD_PTR_CAST(INT16_PTR_TYPE_GS);
  1868. BUILD_TRUNC(value, INT16_TYPE);
  1869. BUILD_TRUNC(expect, INT16_TYPE);
  1870. break;
  1871. case 1:
  1872. if (!enable_segue)
  1873. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1874. else
  1875. BUILD_PTR_CAST(INT8_PTR_TYPE_GS);
  1876. BUILD_TRUNC(value, INT8_TYPE);
  1877. BUILD_TRUNC(expect, INT8_TYPE);
  1878. break;
  1879. default:
  1880. bh_assert(0);
  1881. break;
  1882. }
  1883. if (!(result = LLVMBuildAtomicCmpXchg(
  1884. comp_ctx->builder, maddr, expect, value,
  1885. LLVMAtomicOrderingSequentiallyConsistent,
  1886. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1887. goto fail;
  1888. }
  1889. LLVMSetVolatile(result, true);
  1890. /* CmpXchg return {i32, i1} structure,
  1891. we need to extract the previous_value from the structure */
  1892. if (!(result = LLVMBuildExtractValue(comp_ctx->builder, result, 0,
  1893. "previous_value"))) {
  1894. goto fail;
  1895. }
  1896. if (op_type == VALUE_TYPE_I32) {
  1897. if (LLVMTypeOf(result) != I32_TYPE) {
  1898. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1899. "result_i32"))) {
  1900. goto fail;
  1901. }
  1902. }
  1903. PUSH_I32(result);
  1904. }
  1905. else {
  1906. if (LLVMTypeOf(result) != I64_TYPE) {
  1907. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1908. "result_i64"))) {
  1909. goto fail;
  1910. }
  1911. }
  1912. PUSH_I64(result);
  1913. }
  1914. return true;
  1915. fail:
  1916. return false;
  1917. }
  1918. bool
  1919. aot_compile_op_atomic_wait(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1920. uint8 op_type, uint32 align, mem_offset_t offset,
  1921. uint32 bytes)
  1922. {
  1923. LLVMValueRef maddr, value, timeout, expect, cmp;
  1924. LLVMValueRef param_values[5], ret_value, func, is_wait64;
  1925. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1926. LLVMBasicBlockRef wait_fail, wait_success;
  1927. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1928. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1929. POP_I64(timeout);
  1930. if (op_type == VALUE_TYPE_I32) {
  1931. POP_I32(expect);
  1932. is_wait64 = I8_CONST(false);
  1933. if (!(expect = LLVMBuildZExt(comp_ctx->builder, expect, I64_TYPE,
  1934. "expect_i64"))) {
  1935. goto fail;
  1936. }
  1937. }
  1938. else {
  1939. POP_I64(expect);
  1940. is_wait64 = I8_CONST(true);
  1941. }
  1942. CHECK_LLVM_CONST(is_wait64);
  1943. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  1944. false, NULL)))
  1945. return false;
  1946. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1947. return false;
  1948. param_types[0] = INT8_PTR_TYPE;
  1949. param_types[1] = INT8_PTR_TYPE;
  1950. param_types[2] = I64_TYPE;
  1951. param_types[3] = I64_TYPE;
  1952. param_types[4] = INT8_TYPE;
  1953. ret_type = I32_TYPE;
  1954. GET_AOT_FUNCTION(wasm_runtime_atomic_wait, 5);
  1955. /* Call function wasm_runtime_atomic_wait() */
  1956. param_values[0] = func_ctx->aot_inst;
  1957. param_values[1] = maddr;
  1958. param_values[2] = expect;
  1959. param_values[3] = timeout;
  1960. param_values[4] = is_wait64;
  1961. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  1962. param_values, 5, "call"))) {
  1963. aot_set_last_error("llvm build call failed.");
  1964. return false;
  1965. }
  1966. BUILD_ICMP(LLVMIntNE, ret_value, I32_NEG_ONE, cmp, "atomic_wait_ret");
  1967. ADD_BASIC_BLOCK(wait_fail, "atomic_wait_fail");
  1968. ADD_BASIC_BLOCK(wait_success, "wait_success");
  1969. LLVMMoveBasicBlockAfter(wait_fail, block_curr);
  1970. LLVMMoveBasicBlockAfter(wait_success, block_curr);
  1971. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, wait_success, wait_fail)) {
  1972. aot_set_last_error("llvm build cond br failed.");
  1973. goto fail;
  1974. }
  1975. /* If atomic wait failed, return this function
  1976. so the runtime can catch the exception */
  1977. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_fail);
  1978. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1979. goto fail;
  1980. }
  1981. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_success);
  1982. PUSH_I32(ret_value);
  1983. /* Insert suspend check point */
  1984. if (comp_ctx->enable_thread_mgr) {
  1985. if (!check_suspend_flags(comp_ctx, func_ctx, false))
  1986. return false;
  1987. }
  1988. return true;
  1989. fail:
  1990. return false;
  1991. }
  1992. bool
  1993. aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
  1994. AOTFuncContext *func_ctx, uint32 align,
  1995. mem_offset_t offset, uint32 bytes)
  1996. {
  1997. LLVMValueRef maddr, value, count;
  1998. LLVMValueRef param_values[3], ret_value, func;
  1999. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  2000. POP_I32(count);
  2001. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
  2002. false, NULL)))
  2003. return false;
  2004. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  2005. return false;
  2006. param_types[0] = INT8_PTR_TYPE;
  2007. param_types[1] = INT8_PTR_TYPE;
  2008. param_types[2] = I32_TYPE;
  2009. ret_type = I32_TYPE;
  2010. GET_AOT_FUNCTION(wasm_runtime_atomic_notify, 3);
  2011. /* Call function wasm_runtime_atomic_notify() */
  2012. param_values[0] = func_ctx->aot_inst;
  2013. param_values[1] = maddr;
  2014. param_values[2] = count;
  2015. if (!(ret_value = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  2016. param_values, 3, "call"))) {
  2017. aot_set_last_error("llvm build call failed.");
  2018. return false;
  2019. }
  2020. PUSH_I32(ret_value);
  2021. return true;
  2022. fail:
  2023. return false;
  2024. }
  2025. bool
  2026. aot_compiler_op_atomic_fence(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  2027. {
  2028. return LLVMBuildFence(comp_ctx->builder,
  2029. LLVMAtomicOrderingSequentiallyConsistent, false, "")
  2030. ? true
  2031. : false;
  2032. }
  2033. #endif /* end of WASM_ENABLE_SHARED_MEMORY */