Add support for VxWorks (#43)

This adds iwasm support for VxWorks 7.
This commit is contained in:
Yiting Wang
2019-07-10 16:01:43 +08:00
committed by wenyongh
parent 5b0c8fa0ba
commit 08ebc6c773
16 changed files with 1478 additions and 0 deletions

View File

@ -0,0 +1,107 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required (VERSION 2.8)
project (iwasm)
set (PLATFORM "vxworks")
# Specify the compiler driver provided in the VSB
SET(CMAKE_C_COMPILER vx-cc)
SET(CMAKE_AR vx-ar)
SET(CMAKE_RANLIB vx-ranlib)
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# Enable repl mode if want to test spec cases
# add_definitions(-DWASM_ENABLE_REPL)
if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
add_definitions(-DNVALGRIND)
endif ()
# Currently build as 64-bit by default.
set (BUILD_AS_64BIT_SUPPORT "YES")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
# 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})
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_LIB_DIR ../../../shared-lib)
include_directories (.
../../runtime/include
../../runtime/platform/include
${SHARED_LIB_DIR}/include)
include (../../runtime/platform/${PLATFORM}/platform.cmake)
include (../../runtime/utils/utils.cmake)
include (../../runtime/vmcore-wasm/vmcore.cmake)
include (../../lib/native/base/wasm_lib_base.cmake)
include (../../lib/native/libc/wasm_libc.cmake)
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
include (${SHARED_LIB_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_LIB_DIR}/utils/shared_utils.cmake)
add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
add_executable (iwasm main.c ext_lib_export.c)
install (TARGETS iwasm DESTINATION bin)
target_link_libraries (iwasm vmlib -lm -ldl -lunix)
add_library (libiwasm SHARED
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})
install (TARGETS libiwasm DESTINATION lib)
set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
target_link_libraries (libiwasm -lm -ldl -lunix)

View File

@ -0,0 +1,21 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib_export.h"
static NativeSymbol extended_native_symbol_defs[] = { };
#include "ext_lib_export.h"

View File

@ -0,0 +1,238 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <string.h>
#include "wasm_assert.h"
#include "wasm_log.h"
#include "wasm_platform.h"
#include "wasm_platform_log.h"
#include "wasm_thread.h"
#include "wasm_export.h"
#include "wasm_memory.h"
#include "bh_memory.h"
static int app_argc;
static char **app_argv;
static int print_help()
{
wasm_printf("Usage: iwasm [-options] wasm_file [args...]\n");
wasm_printf("options:\n");
wasm_printf(" -f|--function name Specify function name to run in module\n"
" rather than main\n");
#if WASM_ENABLE_LOG != 0
wasm_printf(" -v=X Set log verbose level (0 to 2, default is 1),\n"
" larger level with more log\n");
#endif
wasm_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of `FUNC ARG...`\n");
return 1;
}
static void*
app_instance_main(wasm_module_inst_t module_inst)
{
const char *exception;
wasm_application_execute_main(module_inst, app_argc, app_argv);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
}
static void*
app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
{
const char *exception;
wasm_application_execute_func(module_inst, func_name, app_argc - 1,
app_argv + 1);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
}
/**
* Split a space separated strings into an array of strings
* Returns NULL on failure
* Memory must be freed by caller
* Based on: http://stackoverflow.com/a/11198630/471795
*/
static char **
split_string(char *str, int *count)
{
char **res = NULL;
char *p;
int idx = 0;
/* split string and append tokens to 'res' */
do {
p = strtok(str, " ");
str = NULL;
res = (char**) realloc(res, sizeof(char*) * (idx + 1));
if (res == NULL) {
return NULL;
}
res[idx++] = p;
} while (p);
if (count) {
*count = idx - 1;
}
return res;
}
static void*
app_instance_repl(wasm_module_inst_t module_inst)
{
char *cmd = NULL;
size_t len = 0;
ssize_t n;
while ((wasm_printf("webassembly> "), n = getline(&cmd, &len, stdin)) != -1) {
wasm_assert(n > 0);
if (cmd[n - 1] == '\n') {
if (n == 1)
continue;
else
cmd[n - 1] = '\0';
}
app_argv = split_string(cmd, &app_argc);
if (app_argv == NULL) {
LOG_ERROR("Wasm prepare param failed: split string failed.\n");
break;
}
if (app_argc != 0) {
wasm_application_execute_func(module_inst, app_argv[0],
app_argc - 1, app_argv + 1);
}
free(app_argv);
}
free(cmd);
return NULL;
}
static char global_heap_buf[512 * 1024] = { 0 };
int main(int argc, char *argv[])
{
char *wasm_file = NULL;
const char *func_name = NULL;
uint8 *wasm_file_buf = NULL;
int wasm_file_size;
wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL;
char error_buf[128];
#if WASM_ENABLE_LOG != 0
int log_verbose_level = 1;
#endif
bool is_repl_mode = false;
/* Process options. */
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
argc--, argv++;
if (argc < 2) {
print_help();
return 0;
}
func_name = argv[0];
}
#if WASM_ENABLE_LOG != 0
else if (!strncmp(argv[0], "-v=", 3)) {
log_verbose_level = atoi(argv[0] + 3);
if (log_verbose_level < 0 || log_verbose_level > 2)
return print_help();
}
#endif
else if (!strcmp(argv[0], "--repl"))
is_repl_mode = true;
else
return print_help();
}
if (argc == 0)
return print_help();
wasm_file = argv[0];
app_argc = argc;
app_argv = argv;
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
wasm_printf("Init global heap failed.\n");
return -1;
}
/* initialize runtime environment */
if (!wasm_runtime_init())
goto fail1;
wasm_log_set_verbose_level(log_verbose_level);
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = (uint8*) wasm_read_file_to_buffer(wasm_file,
&wasm_file_size)))
goto fail2;
/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail3;
}
/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
16 * 1024, /* stack size */
8 * 1024, /* heap size */
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail4;
}
if (is_repl_mode)
app_instance_repl(wasm_module_inst);
else if (func_name)
app_instance_func(wasm_module_inst, func_name);
else
app_instance_main(wasm_module_inst);
/* destroy the module instance */
wasm_runtime_deinstantiate(wasm_module_inst);
fail4:
/* unload the module */
wasm_runtime_unload(wasm_module);
fail3:
/* free the file buffer */
wasm_free(wasm_file_buf);
fail2:
/* destroy runtime environment */
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return 0;
}

View File

@ -0,0 +1,25 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L)
set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_LIB_DIR})
include_directories(${PLATFORM_LIB_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c)
set (WASM_PLATFORM_LIB_SOURCE ${source_all})

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_native.h"
void*
wasm_platform_native_func_lookup(const char *module_name,
const char *func_name)
{
return NULL;
}

View File

@ -0,0 +1,96 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_log.h"
#include "wasm_platform.h"
#include "wasm_memory.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dlfcn.h>
bool is_little_endian = false;
bool __is_little_endian()
{
union w
{
int a;
char b;
}c;
c.a = 1;
return (c.b == 1);
}
int
wasm_platform_init()
{
if (__is_little_endian())
is_little_endian = true;
return 0;
}
char*
wasm_read_file_to_buffer(const char *filename, int *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
LOG_ERROR("Read file to buffer failed: invalid filename or ret size.\n");
return NULL;
}
if ((file = open(filename, O_RDONLY, 0)) == -1) {
LOG_ERROR("Read file to buffer failed: open file %s failed.\n",
filename);
return NULL;
}
if (fstat(file, &stat_buf) != 0) {
LOG_ERROR("Read file to buffer failed: fstat file %s failed.\n",
filename);
close(file);
return NULL;
}
file_size = stat_buf.st_size;
if (!(buffer = wasm_malloc(file_size))) {
LOG_ERROR("Read file to buffer failed: alloc memory failed.\n");
close(file);
return NULL;
}
read_size = read(file, buffer, file_size);
close(file);
if (read_size < file_size) {
LOG_ERROR("Read file to buffer failed: read file content failed.\n");
wasm_free(buffer);
return NULL;
}
*ret_size = file_size;
return buffer;
}

View File

@ -0,0 +1,104 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _WASM_PLATFORM_H
#define _WASM_PLATFORM_H
#include <inttypes.h>
#include <stdbool.h>
#include "wasm_config.h"
#include "wasm_types.h"
typedef uint64_t uint64;
typedef int64_t int64;
typedef float float32;
typedef double float64;
#ifndef NULL
# define NULL ((void*) 0)
#endif
#define WASM_PLATFORM "VxWorks"
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <semaphore.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/**
* Return the offset of the given field in the given type.
*
* @param Type the type containing the filed
* @param field the field in the type
*
* @return the offset of field in Type
*/
#ifndef offsetof
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
#endif
typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
int wasm_platform_init();
extern bool is_little_endian;
#include <string.h>
/* The following operations declared in string.h may be defined as
macros on Linux, so don't declare them as functions here. */
/* memset */
/* memcpy */
/* memmove */
/* #include <stdio.h> */
/* Unit test framework is based on C++, where the declaration of
snprintf is different. */
#ifndef __cplusplus
int snprintf(char *buffer, size_t count, const char *format, ...);
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* #include <math.h> */
#ifndef __cplusplus
double sqrt(double x);
#endif
#include <stdio.h>
char*
wasm_read_file_to_buffer(const char *filename, int *ret_size);
void*
wasm_dlsym(void *handle, const char *symbol);
#ifdef __cplusplus
}
#endif
#endif