Skip to content

Commit

Permalink
Merge pull request #966 from bytecodealliance/main
Browse files Browse the repository at this point in the history
Merge bytecodealliance:main into wenyongh:main
  • Loading branch information
wenyongh authored Nov 25, 2024
2 parents 65a26e9 + b0c6d5c commit d5cf269
Show file tree
Hide file tree
Showing 21 changed files with 361 additions and 114 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/[email protected].1
uses: github/codeql-action/[email protected].4
with:
languages: ${{ matrix.language }}

Expand All @@ -70,7 +70,7 @@ jobs:
- run: |
./.github/scripts/codeql_buildscript.sh
- name: Perform CodeQL Analysis
uses: github/codeql-action/[email protected].1
uses: github/codeql-action/[email protected].4
with:
category: "/language:${{matrix.language}}"
upload: false
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif

- name: Upload CodeQL results to code scanning
uses: github/codeql-action/[email protected].1
uses: github/codeql-action/[email protected].4
with:
sarif_file: ${{ steps.step1.outputs.sarif-output }}
category: "/language:${{matrix.language}}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compilation_on_android_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ jobs:
run: |
mkdir build
cd build
cmake .. -DWAMR_BUILD_DEBUG_INTERP=1
cmake .. -DWAMR_BUILD_DEBUG_INTERP=1 -DWAMR_BUILD_REF_TYPES=1
make
working-directory: product-mini/platforms/linux

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/supply_chain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@acb9cb18eec7e3a113ef83cff0be91e75cfd9526 # v2.2.4
uses: github/codeql-action/upload-sarif@a1695c562bbfa68dc5ab58c9b5e9f616b52bf5be # v2.2.4
with:
sarif_file: results.sarif
20 changes: 18 additions & 2 deletions build-scripts/build_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,27 @@ def build_llvm(llvm_dir, platform, backends, projects, use_clang=False, extra_fl
"default": [],
}

experimental_backends = ["ARC", "Xtensa"]
normal_backends = [s for s in backends if s not in experimental_backends]

LLVM_TARGETS_TO_BUILD = [
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(backends) + '"'
if backends
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(normal_backends) + '"'
if normal_backends
else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
]

# if not on ARC platform, but want to add expeirmental backend ARC as target
if platform != "ARC" and "ARC" in backends:
LLVM_TARGETS_TO_BUILD.extend(
LLVM_EXTRA_COMPILE_OPTIONS["arc"]
)

if platform != "Xtensa" and "Xtensa" in backends:
print(
"Currently it's not supported to build Xtensa backend on non-Xtensa platform"
)
return None

LLVM_PROJECTS_TO_BUILD = [
'-DLLVM_ENABLE_PROJECTS:STRING="' + ";".join(projects) + '"' if projects else ""
]
Expand Down Expand Up @@ -240,6 +255,7 @@ def main():
"X86",
"Xtensa",
],
default=[],
help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
)
parser.add_argument(
Expand Down
12 changes: 9 additions & 3 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
bool ret = false;
#endif

/* Check heap size */
/* Align and validate heap size */
heap_size = align_uint(heap_size, 8);
if (heap_size > APP_HEAP_SIZE_MAX)
heap_size = APP_HEAP_SIZE_MAX;
Expand Down Expand Up @@ -1905,7 +1905,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
goto fail;
}
for (i = 0; i < module->table_init_data_count; i++) {
if (wasm_elem_is_active(module->table_init_data_list[i]->mode))
if (wasm_elem_is_active(module->table_init_data_list[i]->mode)
|| wasm_elem_is_declarative(
module->table_init_data_list[i]->mode))
bh_bitmap_set_bit(common->elem_dropped, i);
}
}
Expand Down Expand Up @@ -2001,7 +2003,11 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
AOTTableInstance *table_inst;
table_elem_type_t *table_data;

table = &module->tables[i];
/* bypass imported table since AOTImportTable doesn't have init_expr */
if (i < module->import_table_count)
continue;

table = &module->tables[i - module->import_table_count];
bh_assert(table);

if (table->init_expr.init_expr_type == INIT_EXPR_NONE) {
Expand Down
10 changes: 8 additions & 2 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3631,8 +3631,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,

bh_memcpy_s(mapping_copy, max_len, map_dir_list[i],
(uint32)(strlen(map_dir_list[i]) + 1));
map_mapped = strtok(mapping_copy, "::");
map_host = strtok(NULL, "::");

const char *delim = "::";
char *delim_pos = strstr(mapping_copy, delim);
if (delim_pos) {
*delim_pos = '\0';
map_mapped = mapping_copy;
map_host = delim_pos + strlen(delim);
}

if (!map_mapped || !map_host) {
if (error_buf)
Expand Down
43 changes: 4 additions & 39 deletions core/iwasm/compilation/aot_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4093,39 +4093,6 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
return true;
}

#if !(defined(_WIN32) || defined(_WIN32_))
char *
aot_generate_tempfile_name(const char *prefix, const char *extension,
char *buffer, uint32 len)
{
int fd, name_len;

name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);

if ((fd = mkstemp(buffer)) <= 0) {
aot_set_last_error("make temp file failed.");
return NULL;
}

/* close and remove temp file */
close(fd);
unlink(buffer);

/* Check if buffer length is enough */
/* name_len + '.' + extension + '\0' */
if (name_len + 1 + strlen(extension) + 1 > len) {
aot_set_last_error("temp file name too long.");
return NULL;
}

snprintf(buffer + name_len, len - name_len, ".%s", extension);
return buffer;
}
#else

errno_t
_mktemp_s(char *nameTemplate, size_t sizeInChars);

char *
aot_generate_tempfile_name(const char *prefix, const char *extension,
char *buffer, uint32 len)
Expand All @@ -4134,7 +4101,8 @@ aot_generate_tempfile_name(const char *prefix, const char *extension,

name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);

if (_mktemp_s(buffer, name_len + 1) != 0) {
if (!bh_mkstemp(buffer, name_len + 1)) {
aot_set_last_error("make temp file failed.");
return NULL;
}

Expand All @@ -4148,7 +4116,6 @@ aot_generate_tempfile_name(const char *prefix, const char *extension,
snprintf(buffer + name_len, len - name_len, ".%s", extension);
return buffer;
}
#endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */

bool
aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
Expand Down Expand Up @@ -4227,7 +4194,6 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)

bh_print_time("Begin to emit object file");

#if !(defined(_WIN32) || defined(_WIN32_))
if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
char cmd[1024];
int ret;
Expand Down Expand Up @@ -4270,7 +4236,7 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
file_name, bc_file_name);
LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd);

ret = system(cmd);
ret = bh_system(cmd);
/* remove temp bitcode file */
unlink(bc_file_name);

Expand Down Expand Up @@ -4323,7 +4289,7 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
file_name, asm_file_name);
LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd);

ret = system(cmd);
ret = bh_system(cmd);
/* remove temp assembly file */
unlink(asm_file_name);

Expand All @@ -4336,7 +4302,6 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)

return true;
}
#endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */

if (!strncmp(LLVMGetTargetName(target), "arc", 3))
/* Emit to assembly file instead for arc target
Expand Down
20 changes: 3 additions & 17 deletions core/iwasm/compilation/aot_emit_aot_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -4292,10 +4292,6 @@ aot_obj_data_create(AOTCompContext *comp_ctx)

bh_print_time("Begin to emit object file");
if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
#if defined(_WIN32) || defined(_WIN32_)
aot_set_last_error("external toolchain not supported on Windows");
goto fail;
#else
/* Generate a temp file name */
int ret;
char obj_file_name[64];
Expand Down Expand Up @@ -4323,27 +4319,18 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
aot_set_last_error("create mem buffer with file failed.");
goto fail;
}
#endif /* end of defined(_WIN32) || defined(_WIN32_) */
}
else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
#if defined(_WIN32) || defined(_WIN32_)
aot_set_last_error("emit object file on Windows is unsupported.");
goto fail;
#else
/* Emit to assembly file instead for arc target
as it cannot emit to object file */
char file_name[] = "wasm-XXXXXX", buf[128];
int fd, ret;
int ret;

if ((fd = mkstemp(file_name)) <= 0) {
if (!bh_mkstemp(file_name, sizeof(file_name))) {
aot_set_last_error("make temp file failed.");
goto fail;
}

/* close and remove temp file */
close(fd);
unlink(file_name);

snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
comp_ctx->module, buf, LLVMAssemblyFile,
Expand All @@ -4364,7 +4351,7 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
"/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ",
"-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
/* TODO: use try..catch to handle possible exceptions */
ret = system(buf);
ret = bh_system(buf);
/* remove temp assembly file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s");
unlink(buf);
Expand All @@ -4391,7 +4378,6 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
aot_set_last_error("create mem buffer with file failed.");
goto fail;
}
#endif /* end of defined(_WIN32) || defined(_WIN32_) */
}
else {
if (LLVMTargetMachineEmitToMemoryBuffer(
Expand Down
38 changes: 33 additions & 5 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,24 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
}

/* offset1 = offset + addr; */
/* TODO: check whether integer overflow occurs when memory is 64-bit
and boundary check is enabled */
BUILD_OP(Add, offset_const, addr, offset1, "offset1");

if (is_memory64 && comp_ctx->enable_bound_check) {
/* Check whether integer overflow occurs in offset + addr */
LLVMBasicBlockRef check_integer_overflow_end;
ADD_BASIC_BLOCK(check_integer_overflow_end,
"check_integer_overflow_end");
LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);

BUILD_ICMP(LLVMIntULT, offset1, offset_const, cmp1, "cmp1");
if (!aot_emit_exception(comp_ctx, func_ctx,
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp1,
check_integer_overflow_end)) {
goto fail;
}
SET_BUILD_POS(check_integer_overflow_end);
}

if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
LLVMBasicBlockRef app_addr_in_shared_heap, app_addr_in_linear_mem;
LLVMValueRef is_in_shared_heap, shared_heap_check_bound = NULL;
Expand All @@ -303,7 +317,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);

if (!is_target_64bit) {
/* Check whether interger overflow occurs in addr + offset */
/* Check whether integer overflow occurs in addr + offset */
LLVMBasicBlockRef check_integer_overflow_end;
ADD_BASIC_BLOCK(check_integer_overflow_end,
"check_integer_overflow_end");
Expand Down Expand Up @@ -1215,10 +1229,24 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
goto fail;
}

/* TODO: check whether integer overflow occurs when memory is 64-bit
and boundary check is enabled */
BUILD_OP(Add, offset, bytes, max_addr, "max_addr");

if (is_memory64 && comp_ctx->enable_bound_check) {
/* Check whether integer overflow occurs in offset + addr */
LLVMBasicBlockRef check_integer_overflow_end;
ADD_BASIC_BLOCK(check_integer_overflow_end,
"check_integer_overflow_end");
LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr);

BUILD_ICMP(LLVMIntULT, max_addr, offset, cmp, "cmp");
if (!aot_emit_exception(comp_ctx, func_ctx,
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
check_integer_overflow_end)) {
goto fail;
}
SET_BUILD_POS(check_integer_overflow_end);
}

if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
LLVMBasicBlockRef app_addr_in_shared_heap, app_addr_in_linear_mem;
LLVMValueRef shared_heap_start_off, shared_heap_check_bound;
Expand Down
10 changes: 0 additions & 10 deletions core/iwasm/compilation/aot_llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2746,10 +2746,6 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
/* verify external llc compiler */
comp_ctx->external_llc_compiler = getenv("WAMRC_LLC_COMPILER");
if (comp_ctx->external_llc_compiler) {
#if defined(_WIN32) || defined(_WIN32_)
comp_ctx->external_llc_compiler = NULL;
LOG_WARNING("External LLC compiler not supported on Windows.");
#else
if (access(comp_ctx->external_llc_compiler, X_OK) != 0) {
LOG_WARNING("WAMRC_LLC_COMPILER [%s] not found, fallback to "
"default pipeline",
Expand All @@ -2761,17 +2757,12 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
LOG_VERBOSE("Using external LLC compiler [%s]",
comp_ctx->external_llc_compiler);
}
#endif
}

/* verify external asm compiler */
if (!comp_ctx->external_llc_compiler) {
comp_ctx->external_asm_compiler = getenv("WAMRC_ASM_COMPILER");
if (comp_ctx->external_asm_compiler) {
#if defined(_WIN32) || defined(_WIN32_)
comp_ctx->external_asm_compiler = NULL;
LOG_WARNING("External ASM compiler not supported on Windows.");
#else
if (access(comp_ctx->external_asm_compiler, X_OK) != 0) {
LOG_WARNING(
"WAMRC_ASM_COMPILER [%s] not found, fallback to "
Expand All @@ -2784,7 +2775,6 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
LOG_VERBOSE("Using external ASM compiler [%s]",
comp_ctx->external_asm_compiler);
}
#endif
}
}

Expand Down
Loading

0 comments on commit d5cf269

Please sign in to comment.