Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)

* Implement memory profiler, optimize memory usage, modify code indent

* Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default

* Add a new extension library: connection

* Fix bug of reading magic number and version in big endian platform

* Re-org platform APIs: move most platform APIs from iwasm to shared-lib

* Enhance wasm loader to fix some security issues

* Fix issue about illegal load of EXC_RETURN into PC on stm32 board

* Updates that let a restricted version of the interpreter run in SGX

* Enable native/app address validation and conversion for wasm app

* Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused

* Refine binary size and fix several minor issues

Optimize interpreter LOAD/STORE opcodes to decrease the binary size
Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found
Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper
Add macros of global heap size, stack size, heap size for Zephyr main.c
Clear compile warning of wasm_application.c

* Add more strict security checks for libc wrapper API's

* Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files

* Enhance security of libc strcpy/sprintf wrapper function

* Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions

* Remove get_module_inst() and fix issue of call native

* Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate

* Refine interpreter call native process, refine memory boudary check

* Fix issues of invokeNative function of arm/mips/general version

* Add a switch to build simple sample without gui support

* Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code

* Re-org shared lib header files, remove unused info; fix compile issues of vxworks

* Add build target general

* Remove unused files

* Update license header

* test push

* Restore file

* Sync up with internal/feature

* Sync up with internal/feature

* Rename build_wamr_app to build_wasm_app

* Fix small issues of README

* Enhance malformed wasm file checking
Fix issue of print hex int and implement utf8 string check
Fix wasi file read/write right issue
Fix minor issue of build wasm app doc

* Sync up with internal/feature

* Sync up with internal/feature: fix interpreter arm issue, fix read leb issue

* Sync up with internal/feature

* Fix bug of config.h and rename wasi config.h to ssp_config.h

* Sync up with internal/feature

* Import wamr aot

* update document

* update document

* Update document, disable WASI in 32bit

* update document

* remove files

* update document

* Update document

* update document

* update document

* update samples

* Sync up with internal repo
This commit is contained in:
wenyongh
2020-01-21 13:26:14 +08:00
committed by Wang Xin
parent 2a4528c749
commit 46b93b9d22
464 changed files with 25137 additions and 7911 deletions

View File

@ -0,0 +1,127 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 2.8)
project (aot-compiler)
set (WAMR_BUILD_PLATFORM "linux")
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
add_definitions(-DWASM_ENABLE_INTERP=1)
add_definitions(-DBUILD_AOT_COMPILER=1)
# Set WAMR_BUILD_TARGET, currently values supported:
# "X86_64", "AMD_64", "X86_32", "ARM_32", "MIPS_32", "XTENSA_32"
if (NOT WAMR_BUILD_TARGET)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
# Build as X86_64 by default in 64-bit platform
set (WAMR_BUILD_TARGET "X86_64")
else ()
# Build as X86_32 by default in 32-bit platform
set (WAMR_BUILD_TARGET "X86_32")
endif ()
endif ()
string(TOUPPER ${WAMR_BUILD_TARGET} WAMR_BUILD_TARGET)
# Add definitions for the build target
if (WAMR_BUILD_TARGET STREQUAL "X86_64")
add_definitions(-DBUILD_TARGET_X86_64)
elseif (WAMR_BUILD_TARGET STREQUAL "AMD_64")
add_definitions(-DBUILD_TARGET_AMD_64)
elseif (WAMR_BUILD_TARGET STREQUAL "X86_32")
add_definitions(-DBUILD_TARGET_X86_32)
elseif (WAMR_BUILD_TARGET MATCHES "ARM.*")
add_definitions(-DBUILD_TARGET_ARM)
add_definitions(-DBUILD_TARGET="${WAMR_BUILD_TARGET}")
else ()
message (FATAL_ERROR "-- Build target isn't set")
endif ()
message ("-- Build as target ${WAMR_BUILD_TARGET}")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
# Add -fPIC flag if build as 64-bit
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -fPIC")
else ()
add_definitions (-m32)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif (NOT CMAKE_BUILD_TYPE)
message ("-- CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})
# Enable LLVM
set (LLVM_SRC_ROOT "${PROJECT_SOURCE_DIR}/../core/deps/llvm")
if (NOT EXISTS "${LLVM_SRC_ROOT}/build")
message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build")
endif ()
set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}")
find_package(LLVM REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic")
set (SHARED_DIR ../core/shared)
set (IWASM_DIR ../core/iwasm)
set (APP_FRAMEWORK_DIR ../core/app-framework)
include_directories (${SHARED_DIR}/include
${IWASM_DIR}/include)
enable_language (ASM)
include (${SHARED_DIR}/platform/${WAMR_BUILD_PLATFORM}/shared_platform.cmake)
include (${SHARED_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_DIR}/utils/shared_utils.cmake)
include (${IWASM_DIR}/libraries/libc-builtin/libc_builtin.cmake)
include (${IWASM_DIR}/common/iwasm_common.cmake)
include (${IWASM_DIR}/interpreter/iwasm_interp.cmake)
include (${IWASM_DIR}/aot/iwasm_aot.cmake)
include (${IWASM_DIR}/compilation/iwasm_compl.cmake)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
endif ()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
# We disable these flags by default to stay the same with wasm runtime
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch=thunk -mfunction-return=thunk")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE -ftrapv -D_FORTIFY_SOURCE=2")
# message ("-- CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
add_library (vmlib
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE}
${LIBC_BUILTIN_SOURCE}
${IWASM_COMMON_SOURCE}
${IWASM_INTERP_SOURCE}
${IWASM_AOT_SOURCE})
add_library (aotclib ${IWASM_COMPL_SOURCE})
add_executable (wamrc main.c ext_lib_export.c)
target_link_libraries (wamrc aotclib vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)

43
wamr-compiler/build_llvm.sh Executable file
View File

@ -0,0 +1,43 @@
#!/bin/sh
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
DEPS_DIR=${PWD}/../core/deps
cd ${DEPS_DIR}
if [ ! -d "llvm" ]; then
echo "Clone llvm to core/deps/ .."
git clone https://github.com/llvm-mirror/llvm.git
fi
cd llvm
mkdir -p build
cd build
if [ ! -f bin/llvm-lto ]; then
CORE_NUM=$(nproc --all)
if [ -z "${CORE_NUM}" ]; then
CORE_NUM=1
fi
echo "Build llvm with" ${CORE_NUM} "cores"
cmake .. \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF \
-DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON \
-DLLVM_INCLUDE_EXAMPLES:BOOL=OFF \
-DLLVM_INCLUDE_TESTS:BOOL=OFF \
-DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF \
-DLLVM_APPEND_VC_REV:BOOL=OFF
make -j ${CORE_NUM}
else
echo "llvm has already been built"
fi
cd ${PWD}

View File

@ -0,0 +1,10 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "lib_export.h"
static NativeSymbol extended_native_symbol_defs[] = { };
#include "ext_lib_export.h"

210
wamr-compiler/main.c Normal file
View File

@ -0,0 +1,210 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdlib.h>
#include "bh_platform.h"
#include "bh_memory.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "wasm_export.h"
#include "aot_export.h"
static int
print_help()
{
bh_printf("Usage: wamrc [options] -o output_file wasm_file\n");
bh_printf(" --target=<arch-name> Set the target arch, which has the general format: <arch><sub>\n");
bh_printf(" <arch> = x86_64, i386, arm, thumb, mips.\n");
bh_printf(" Default is host arch, e.g. x86_64\n");
bh_printf(" <sub> = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n");
bh_printf(" Use --target=help to list supported targets\n");
bh_printf(" --target-abi=<abi> Set the target ABI, e.g. gnu, eabi, gnueabihf, etc. (default: gnu)\n");
bh_printf(" Use --target-abi=help to list all the ABI supported\n");
bh_printf(" --cpu=<cpu> Set the target CPU (default: host CPU, e.g. skylake)\n");
bh_printf(" Use --cpu=help to list all the CPU supported\n");
bh_printf(" --cpu-features=<features> Enable or disable the CPU features\n");
bh_printf(" Use +feature to enable a feature, or -feature to disable it\n");
bh_printf(" For example, --cpu-features=+feature1,-feature2\n");
bh_printf(" Use --cpu-features=+help to list all the features supported\n");
bh_printf(" --opt-level=n Set the optimization level (0 to 3, default: 3)\n");
bh_printf(" --format=<format> Specifies the format of the output file\n");
bh_printf(" The format supported:\n");
bh_printf(" aot (default) AoT file\n");
bh_printf(" object Native object file\n");
bh_printf(" llvmir-unopt Unoptimized LLVM IR\n");
bh_printf(" llvmir-opt Optimized LLVM IR\n");
bh_printf("Examples: wamrc -o test.aot test.wasm\n");
bh_printf(" wamrc --target=i386 -o test.aot test.wasm\n");
bh_printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
return 1;
}
int
main(int argc, char *argv[])
{
char *wasm_file_name = NULL, *out_file_name = NULL;
uint8 *wasm_file = NULL;
uint32 wasm_file_size;
wasm_module_t wasm_module = NULL;
aot_comp_data_t comp_data = NULL;
aot_comp_context_t comp_ctx = NULL;
AOTCompOption option = { 0 };
char error_buf[128];
int log_verbose_level = 2;
option.opt_level = 3;
option.output_format = AOT_FORMAT_FILE;
/* Process options. */
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-o")) {
argc--, argv++;
if (argc < 2)
return print_help();
out_file_name = argv[0];
}
else if (!strncmp(argv[0], "--target=", 9)) {
if (argv[0][9] == '\0')
return print_help();
option.target_arch = argv[0] + 9;
}
else if (!strncmp(argv[0], "--target-abi=", 13)) {
if (argv[0][13] == '\0')
return print_help();
option.target_abi = argv[0] + 13;
}
else if (!strncmp(argv[0], "--cpu=", 6)) {
if (argv[0][6] == '\0')
return print_help();
option.target_cpu = argv[0] + 6;
}
else if (!strncmp(argv[0], "--cpu-features=", 15)) {
if (argv[0][15] == '\0')
return print_help();
option.cpu_features = argv[0] + 15;
}
else if (!strncmp(argv[0], "--opt-level=", 12)) {
if (argv[0][12] == '\0')
return print_help();
option.opt_level = (uint32)atoi(argv[0] + 12);
if (option.opt_level > 3)
option.opt_level = 3;
}
else if (!strncmp(argv[0], "--format=", 9)) {
if (argv[0][9] == '\0')
return print_help();
if (!strcmp(argv[0] + 9, "aot"))
option.output_format = AOT_FORMAT_FILE;
else if (!strcmp(argv[0] + 9, "object"))
option.output_format = AOT_OBJECT_FILE;
else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
option.output_format = AOT_LLVMIR_UNOPT_FILE;
else if (!strcmp(argv[0] + 9, "llvmir-opt"))
option.output_format = AOT_LLVMIR_OPT_FILE;
else {
bh_printf("Invalid format %s.\n", argv[0] + 9);
return print_help();
}
}
else
return print_help();
}
if (argc == 0)
return print_help();
wasm_file_name = argv[0];
if (bh_memory_init_with_allocator(malloc, free)) {
bh_printf("Init memory with memory allocator failed.\n");
return -1;
}
/* initialize runtime environment */
if (!wasm_runtime_init())
goto fail1;
bh_log_set_verbose_level(log_verbose_level);
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file = (uint8*)
bh_read_file_to_buffer(wasm_file_name, &wasm_file_size)))
goto fail2;
/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size,
error_buf, sizeof(error_buf)))) {
bh_printf("%s\n", error_buf);
goto fail3;
}
if (!(comp_data = aot_create_comp_data(wasm_module))) {
bh_printf("%s\n", aot_get_last_error());
goto fail4;
}
if (!(comp_ctx = aot_create_comp_context(comp_data,
&option))) {
bh_printf("%s\n", aot_get_last_error());
goto fail5;
}
if (!aot_compile_wasm(comp_ctx)) {
bh_printf("%s\n", aot_get_last_error());
goto fail6;
}
switch (option.output_format) {
case AOT_LLVMIR_UNOPT_FILE:
case AOT_LLVMIR_OPT_FILE:
if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
bh_printf("%s\n", aot_get_last_error());
goto fail6;
}
break;
case AOT_OBJECT_FILE:
if (!aot_emit_object_file(comp_ctx, out_file_name)) {
bh_printf("%s\n", aot_get_last_error());
goto fail6;
}
break;
case AOT_FORMAT_FILE:
if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
bh_printf("%s\n", aot_get_last_error());
goto fail6;
}
break;
default:
break;
}
bh_printf("Compile success, file %s was generated.\n", out_file_name);
fail6:
/* Destroy compiler context */
aot_destroy_comp_context(comp_ctx);
fail5:
/* Destroy compile data */
aot_destroy_comp_data(comp_data);
fail4:
/* Unload WASM module */
wasm_runtime_unload(wasm_module);
fail3:
/* free the file buffer */
bh_free(wasm_file);
fail2:
/* Destroy runtime environment */
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return 0;
}