ソースを参照

Check ValueKind before extracting a constant int value (#2595)

Only when the value kind is LLVMConstantIntValueKind and the value
is not undef and not poison can we extract the value of a constant int.

Fixes #2557 and #2559.
liang.he 2 年 前
コミット
cd0cec5beb

+ 13 - 0
core/iwasm/compilation/aot_compiler.h

@@ -363,6 +363,19 @@ check_type_compatible(uint8 src_type, uint8 dst_type)
         }                                                                   \
     } while (0)
 
+/* if val is a constant integer and its value is not undef or poison */
+static inline bool
+LLVMIsEfficientConstInt(LLVMValueRef val)
+{
+    return LLVMIsConstant(val)
+           && LLVMGetValueKind(val) == LLVMConstantIntValueKind
+           && !LLVMIsUndef(val)
+#if LLVM_VERSION_NUMBER >= 12
+           && !LLVMIsPoison(addr)
+#endif
+        ;
+}
+
 bool
 aot_compile_wasm(AOTCompContext *comp_ctx);
 

+ 4 - 3
core/iwasm/compilation/aot_emit_control.c

@@ -4,6 +4,7 @@
  */
 
 #include "aot_emit_control.h"
+#include "aot_compiler.h"
 #include "aot_emit_exception.h"
 #include "../aot/aot_runtime.h"
 #include "../interpreter/wasm_loader.h"
@@ -469,7 +470,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
                                                    p_frame_ip);
         }
 
-        if (!LLVMIsConstant(value)) {
+        if (!LLVMIsEfficientConstInt(value)) {
             /* Compare value is not constant, create condition br IR */
             /* Create entry block */
             format_block_name(name, sizeof(name), block->block_index,
@@ -835,7 +836,7 @@ aot_compile_op_br_if(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
         return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
     }
 
-    if (!LLVMIsConstant(value_cmp)) {
+    if (!LLVMIsEfficientConstInt(value_cmp)) {
         /* Compare value is not constant, create condition br IR */
         if (!(block_dst = get_target_block(func_ctx, br_depth))) {
             return false;
@@ -972,7 +973,7 @@ aot_compile_op_br_table(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
         return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
     }
 
-    if (!LLVMIsConstant(value_cmp)) {
+    if (!LLVMIsEfficientConstInt(value_cmp)) {
         /* Compare value is not constant, create switch IR */
         for (i = 0; i <= br_count; i++) {
             target_block = get_target_block(func_ctx, br_depths[i]);

+ 3 - 10
core/iwasm/compilation/aot_emit_memory.c

@@ -4,6 +4,7 @@
  */
 
 #include "aot_emit_memory.h"
+#include "aot_compiler.h"
 #include "aot_emit_exception.h"
 #include "../aot/aot_runtime.h"
 #include "aot_intrinsic.h"
@@ -145,11 +146,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
      * have been thrown when converting float to integer before
      */
     /* return addres directly if constant offset and inside memory space */
-    if (LLVMIsConstant(addr) && !LLVMIsUndef(addr)
-#if LLVM_VERSION_NUMBER >= 12
-        && !LLVMIsPoison(addr)
-#endif
-    ) {
+    if (LLVMIsEfficientConstInt(addr)) {
         uint64 mem_offset =
             (uint64)LLVMConstIntGetZExtValue(addr) + (uint64)offset;
         uint32 num_bytes_per_page =
@@ -911,11 +908,7 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
      * have been thrown when converting float to integer before
      */
     /* return addres directly if constant offset and inside memory space */
-    if (!LLVMIsUndef(offset) && !LLVMIsUndef(bytes)
-#if LLVM_VERSION_NUMBER >= 12
-        && !LLVMIsPoison(offset) && !LLVMIsPoison(bytes)
-#endif
-        && LLVMIsConstant(offset) && LLVMIsConstant(bytes)) {
+    if (LLVMIsEfficientConstInt(offset) && LLVMIsEfficientConstInt(bytes)) {
         uint64 mem_offset = (uint64)LLVMConstIntGetZExtValue(offset);
         uint64 mem_len = (uint64)LLVMConstIntGetZExtValue(bytes);
         uint32 num_bytes_per_page =

+ 5 - 5
core/iwasm/compilation/aot_emit_numberic.c

@@ -54,13 +54,13 @@
     } while (0)
 
 #if LLVM_VERSION_NUMBER >= 12
-#define IS_CONST_ZERO(val)                                          \
-    (!LLVMIsUndef(val) && !LLVMIsPoison(val) && LLVMIsConstant(val) \
-     && ((is_i32 && (int32)LLVMConstIntGetZExtValue(val) == 0)      \
+#define IS_CONST_ZERO(val)                                     \
+    (LLVMIsEfficientConstInt(val)                              \
+     && ((is_i32 && (int32)LLVMConstIntGetZExtValue(val) == 0) \
          || (!is_i32 && (int64)LLVMConstIntGetSExtValue(val) == 0)))
 #else
 #define IS_CONST_ZERO(val)                                     \
-    (!LLVMIsUndef(val) && LLVMIsConstant(val)                  \
+    (LLVMIsEfficientConstInt(val)                              \
      && ((is_i32 && (int32)LLVMConstIntGetZExtValue(val) == 0) \
          || (!is_i32 && (int64)LLVMConstIntGetSExtValue(val) == 0)))
 #endif
@@ -473,7 +473,7 @@ compile_int_div(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
         return aot_handle_next_reachable_block(comp_ctx, func_ctx, p_frame_ip);
     }
 
-    if (LLVMIsConstant(right)) {
+    if (LLVMIsEfficientConstInt(right)) {
         int64 right_val = (int64)LLVMConstIntGetSExtValue(right);
         switch (right_val) {
             case 0: