Răsfoiți Sursa

GC AOT: Emit string with '\0' as terminator (#2221)

Huang Qi 2 ani în urmă
părinte
comite
190f6903f1

+ 1 - 1
.github/workflows/compilation_on_nuttx.yml

@@ -94,7 +94,7 @@ jobs:
       - name: Install RISC-V Compilers
         if: contains(matrix.nuttx_board_config, 'risc-v')
         run: |
-          curl -L https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz > riscv.tar.gz
+          curl -L -k https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz > riscv.tar.gz
           tar xvf riscv.tar.gz
           echo "$PWD/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin" >> $GITHUB_PATH
 

+ 1 - 1
.github/workflows/spec_test_on_nuttx.yml

@@ -52,7 +52,7 @@ jobs:
       - name: Install RISC-V Compilers
         if: contains(matrix.nuttx_board_config, 'risc-v')
         run: |
-          curl -L https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz > riscv.tar.gz
+          curl -L -k https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz > riscv.tar.gz
           tar xvf riscv.tar.gz
           echo "$PWD/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin" >> $GITHUB_PATH
 

+ 9 - 14
core/iwasm/aot/aot_loader.c

@@ -306,19 +306,18 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
     }
 
     /* Lookup const string set, use the string if found */
-    if (!(c_str = loader_malloc((uint32)len + 1, error_buf, error_buf_size))) {
+    if (!(c_str = loader_malloc((uint32)len, error_buf, error_buf_size))) {
         return NULL;
     }
 #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
     if (is_vram_word_align) {
-        bh_memcpy_wa(c_str, (uint32)(len + 1), str, (uint32)len);
+        bh_memcpy_wa(c_str, (uint32)len, str, (uint32)len);
     }
     else
 #endif
     {
-        bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len);
+        bh_memcpy_s(c_str, len, str, (uint32)len);
     }
-    c_str[len] = '\0';
 
     if ((value = bh_hash_map_find(set, c_str))) {
         wasm_runtime_free(c_str);
@@ -363,22 +362,18 @@ load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
         }
     }
 #endif
-    else if (p[str_len - 1] == '\0') {
-        /* The string is terminated with '\0', use it directly */
-        str = (char *)p;
-    }
     else if (is_load_from_file_buf) {
-        /* As the file buffer can be referred to after loading,
-           we use the 2 bytes of size to adjust the string:
-           move string 2 byte backward and then append '\0' */
-        str = (char *)(p - 2);
-        bh_memmove_s(str, (uint32)(str_len + 1), p, (uint32)str_len);
-        str[str_len] = '\0';
+        /* The string is always terminated with '\0', use it directly.
+         * In this case, the file buffer can be reffered to after loading.
+         */
+        bh_assert(p[str_len - 1] == '\0');
+        str = (char *)p;
     }
     else {
         /* Load from sections, the file buffer cannot be reffered to
            after loading, we must create another string and insert it
            into const string set */
+        bh_assert(p[str_len - 1] == '\0');
         if (!(str = const_str_set_insert((uint8 *)p, str_len, module,
 #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
                                          is_vram_word_align,

+ 5 - 8
core/iwasm/compilation/aot_emit_aot_file.c

@@ -179,10 +179,8 @@ get_file_header_size()
 static uint32
 get_string_size(AOTCompContext *comp_ctx, const char *s)
 {
-    /* string size (2 bytes) + string content */
-    return (uint32)sizeof(uint16) + (uint32)strlen(s) +
-           /* emit string with '\0' only in XIP mode */
-           (comp_ctx->is_indirect_mode ? 1 : 0);
+    /* string size (2 bytes) + string content + '\0' */
+    return (uint32)sizeof(uint16) + (uint32)strlen(s) + 1;
 }
 
 static uint32
@@ -1072,17 +1070,16 @@ static union {
         offset += len;                \
     } while (0)
 
+/* Emit string with '\0'
+ */
 #define EMIT_STR(s)                                   \
     do {                                              \
-        uint32 str_len = (uint32)strlen(s);           \
+        uint32 str_len = (uint32)strlen(s) + 1;       \
         if (str_len > INT16_MAX) {                    \
             aot_set_last_error("emit string failed: " \
                                "string too long");    \
             return false;                             \
         }                                             \
-        if (comp_ctx->is_indirect_mode)               \
-            /* emit '\0' only in XIP mode */          \
-            str_len++;                                \
         EMIT_U16(str_len);                            \
         EMIT_BUF(s, str_len);                         \
     } while (0)