Sfoglia il codice sorgente

fix unaligned 64bit access on MCU (#192)

Xu Jun 6 anni fa
parent
commit
381859d530

+ 10 - 7
core/iwasm/interpreter/wasm_interp_fast.c

@@ -534,13 +534,16 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
 #if WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0
 #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
 #else
-#define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) do {\
-    src_type1 val1;                                                         \
-    src_type2 val2;                                                         \
-    val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_ip + 2);          \
-    val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_ip);              \
-    val1 operation##= val2;                                                 \
-    PUT_##src_op_type##_TO_ADDR(frame_ip + 4, val1);                        \
+#define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) do {          \
+    src_type1 val1;                                                                   \
+    src_type2 val2;                                                                   \
+    val1 =                                                                            \
+      (src_type1)GET_##src_op_type##_FROM_ADDR(frame_lp + (*(int16*)(frame_ip + 2))); \
+    val2 =                                                                            \
+      (src_type2)GET_##src_op_type##_FROM_ADDR(frame_lp + (*(int16*)(frame_ip)));     \
+    val1 operation##= val2;                                                           \
+    PUT_##src_op_type##_TO_ADDR(frame_lp + (*(int16*)(frame_ip + 4)), val1);          \
+    frame_ip += 6;                                                                    \
   } while (0)
 #endif
 

+ 4 - 2
core/iwasm/interpreter/wasm_loader.c

@@ -3054,7 +3054,8 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type,
         }
         c->slot_index = operand_offset;
         ctx->num_const ++;
-        LOG_OP("#### new const [%d]: %ld\n", num_const, (int64)val);
+        LOG_OP("#### new const [%d]: %ld\n",
+            ctx->num_const, (int64)c->value.i64);
     }
     /* use negetive index for const */
     operand_offset = -(operand_offset + 1);
@@ -4300,7 +4301,8 @@ handle_next_reachable_block:
 #if WASM_ENABLE_FAST_INTERP != 0
                 skip_label();
                 disable_emit = true;
-                f64 = *(float64 *)p_org;
+                /* Some MCU may require 8-byte align */
+                memcpy((uint8*)&f64, p_org, sizeof(float64));
                 GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
 #endif
                 PUSH_F64();