WebAssembly Micro Runtime first version

This commit is contained in:
Wang Xin
2019-05-07 10:18:18 +08:00
parent 15aa50914b
commit a75a5f0f41
252 changed files with 33487 additions and 0 deletions

View File

@ -0,0 +1,22 @@
# 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.
set (UTILS_LIB_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${UTILS_LIB_DIR})
file (GLOB_RECURSE source_all ${UTILS_LIB_DIR}/*.c )
set (WASM_UTILS_LIB_SOURCE ${source_all})

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.
*/
#include "wasm_platform.h"
static bool sort_flag = false;
typedef struct NativeSymbol {
const char *symbol;
void *func_ptr;
} NativeSymbol;
static bool
sort_symbol_ptr(NativeSymbol *ptr, int len)
{
int i, j;
NativeSymbol temp;
for (i = 0; i < len - 1; ++i) {
for (j = i + 1; j < len; ++j) {
if (strcmp((ptr+i)->symbol, (ptr+j)->symbol) > 0) {
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
return true;
}
static void *
lookup_symbol(NativeSymbol *ptr, int len, const char *symbol)
{
int low = 0, mid, ret;
int high = len - 1;
while (low <= high) {
mid = (low + high) / 2;
ret = strcmp(symbol, ptr[mid].symbol);
if (ret == 0)
return ptr[mid].func_ptr;
else if (ret < 0)
high = mid - 1;
else
low = mid + 1;
}
return NULL;
}
int
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
int
get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
static NativeSymbol *base_native_symbol_defs;
static NativeSymbol *ext_native_symbol_defs;
static int base_native_symbol_len;
static int ext_native_symbol_len;
void *
wasm_dlsym(void *handle, const char *symbol)
{
void *ret;
if (!sort_flag) {
base_native_symbol_len = get_base_lib_export_apis(&base_native_symbol_defs);
ext_native_symbol_len = get_ext_lib_export_apis(&ext_native_symbol_defs);
if (base_native_symbol_len > 0)
sort_symbol_ptr(base_native_symbol_defs, base_native_symbol_len);
if (ext_native_symbol_len > 0)
sort_symbol_ptr(ext_native_symbol_defs, ext_native_symbol_len);
sort_flag = true;
}
if (!symbol)
return NULL;
if ((ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
symbol))
|| (ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
symbol)))
return ret;
return NULL;
}

View File

@ -0,0 +1,301 @@
/*
* 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_hashmap.h"
#include "wasm_log.h"
#include "wasm_thread.h"
#include "wasm_memory.h"
typedef struct HashMapElem {
void *key;
void *value;
struct HashMapElem *next;
} HashMapElem;
struct HashMap {
/* size of element array */
uint32 size;
/* lock for elements */
korp_mutex *lock;
/* hash function of key */
HashFunc hash_func;
/* key equal function */
KeyEqualFunc key_equal_func;
KeyDestroyFunc key_destroy_func;
ValueDestroyFunc value_destroy_func;
HashMapElem *elements[1];
};
HashMap*
wasm_hash_map_create(uint32 size, bool use_lock,
HashFunc hash_func,
KeyEqualFunc key_equal_func,
KeyDestroyFunc key_destroy_func,
ValueDestroyFunc value_destroy_func)
{
HashMap *map;
uint32 total_size;
if (size > HASH_MAP_MAX_SIZE) {
LOG_ERROR("HashMap create failed: size is too large.\n");
return NULL;
}
if (!hash_func || !key_equal_func) {
LOG_ERROR("HashMap create failed: hash function or key equal function "
" is NULL.\n");
return NULL;
}
total_size = offsetof(HashMap, elements) +
sizeof(HashMapElem) * size +
(use_lock ? sizeof(korp_mutex) : 0);
if (!(map = wasm_malloc(total_size))) {
LOG_ERROR("HashMap create failed: alloc memory failed.\n");
return NULL;
}
memset(map, 0, total_size);
if (use_lock) {
map->lock = (korp_mutex*)
((uint8*)map + offsetof(HashMap, elements) + sizeof(HashMapElem) * size);
if (ws_mutex_init(map->lock, false)) {
LOG_ERROR("HashMap create failed: init map lock failed.\n");
wasm_free(map);
return NULL;
}
}
map->size = size;
map->hash_func = hash_func;
map->key_equal_func = key_equal_func;
map->key_destroy_func = key_destroy_func;
map->value_destroy_func = value_destroy_func;
return map;
}
bool
wasm_hash_map_insert(HashMap *map, void *key, void *value)
{
uint32 index;
HashMapElem *elem;
if (!map || !key) {
LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
return false;
}
if (map->lock) {
ws_mutex_lock(map->lock);
}
index = map->hash_func(key) % map->size;
elem = map->elements[index];
while (elem) {
if (map->key_equal_func(elem->key, key)) {
LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
goto fail;
}
elem = elem->next;
}
if (!(elem = wasm_malloc(sizeof(HashMapElem)))) {
LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
goto fail;
}
elem->key = key;
elem->value = value;
elem->next = map->elements[index];
map->elements[index] = elem;
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return true;
fail:
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return false;
}
void*
wasm_hash_map_find(HashMap *map, void *key)
{
uint32 index;
HashMapElem *elem;
void *value;
if (!map || !key) {
LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
return NULL;
}
if (map->lock) {
ws_mutex_lock(map->lock);
}
index = map->hash_func(key) % map->size;
elem = map->elements[index];
while (elem) {
if (map->key_equal_func(elem->key, key)) {
value = elem->value;
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return value;
}
elem = elem->next;
}
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return NULL;
}
bool
wasm_hash_map_update(HashMap *map, void *key, void *value,
void **p_old_value)
{
uint32 index;
HashMapElem *elem;
if (!map || !key) {
LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
return false;
}
if (map->lock) {
ws_mutex_lock(map->lock);
}
index = map->hash_func(key) % map->size;
elem = map->elements[index];
while (elem) {
if (map->key_equal_func(elem->key, key)) {
if (p_old_value)
*p_old_value = elem->value;
elem->value = value;
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return true;
}
elem = elem->next;
}
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return false;
}
bool
wasm_hash_map_remove(HashMap *map, void *key,
void **p_old_key, void **p_old_value)
{
uint32 index;
HashMapElem *elem, *prev;
if (!map || !key) {
LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
return false;
}
if (map->lock) {
ws_mutex_lock(map->lock);
}
index = map->hash_func(key) % map->size;
prev = elem = map->elements[index];
while (elem) {
if (map->key_equal_func(elem->key, key)) {
if (p_old_key)
*p_old_key = elem->key;
if (p_old_value)
*p_old_value = elem->value;
if (elem == map->elements[index])
map->elements[index] = elem->next;
else
prev->next = elem->next;
wasm_free(elem);
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return true;
}
prev = elem;
elem = elem->next;
}
if (map->lock) {
ws_mutex_unlock(map->lock);
}
return false;
}
bool
wasm_hash_map_destroy(HashMap *map)
{
uint32 index;
HashMapElem *elem, *next;
if (!map) {
LOG_ERROR("HashMap destroy failed: map is NULL.\n");
return false;
}
if (map->lock) {
ws_mutex_lock(map->lock);
}
for (index = 0; index < map->size; index++) {
elem = map->elements[index];
while (elem) {
next = elem->next;
if (map->key_destroy_func) {
map->key_destroy_func(elem->key);
}
if (map->value_destroy_func) {
map->value_destroy_func(elem->value);
}
wasm_free(elem);
elem = next;
}
}
if (map->lock) {
ws_mutex_unlock(map->lock);
ws_mutex_destroy(map->lock);
}
wasm_free(map);
return true;
}

View File

@ -0,0 +1,102 @@
/*
* 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_log.h"
#include "wasm_thread.h"
/**
* The verbose level of the log system. Only those verbose logs whose
* levels are less than or equal to this value are outputed.
*/
static int log_verbose_level;
/**
* The lock for protecting the global output stream of logs.
*/
static korp_mutex log_stream_lock;
int
_wasm_log_init ()
{
log_verbose_level = 1;
return ws_mutex_init (&log_stream_lock, false);
}
void
_wasm_log_set_verbose_level (int level)
{
log_verbose_level = level;
}
bool
_wasm_log_begin (int level)
{
korp_tid self;
if (level > log_verbose_level) {
return false;
}
/* Try to own the log stream and start the log output. */
ws_mutex_lock (&log_stream_lock);
self = ws_self_thread ();
wasm_printf ("[%X]: ", (int)self);
return true;
}
void
_wasm_log_vprintf (const char *fmt, va_list ap)
{
wasm_vprintf (fmt, ap);
}
void
_wasm_log_printf (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
_wasm_log_vprintf (fmt, ap);
va_end (ap);
}
void
_wasm_log_end ()
{
ws_mutex_unlock (&log_stream_lock);
}
void
_wasm_log (int level, const char *file, int line,
const char *fmt, ...)
{
if (_wasm_log_begin (level)) {
va_list ap;
if (file)
_wasm_log_printf ("%s:%d ", file, line);
va_start (ap, fmt);
_wasm_log_vprintf (fmt, ap);
va_end (ap);
_wasm_log_end ();
}
}

View File

@ -0,0 +1,217 @@
/*
* 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_vector.h"
#include "wasm_memory.h"
static uint8*
alloc_vector_data(uint32 length, uint32 size_elem)
{
uint64 total_size = ((uint64)size_elem) * length;
uint8 *data;
if (total_size > UINT32_MAX) {
return NULL;
}
if ((data = wasm_malloc((uint32)total_size))) {
memset(data, 0, (uint32)total_size);
}
return data;
}
static bool
extend_vector(Vector *vector, uint32 length)
{
uint8 *data;
if (length <= vector->max_elements)
return true;
if (length < vector->size_elem * 3 / 2)
length = vector->size_elem * 3 / 2;
if (!(data = alloc_vector_data(length, vector->size_elem))) {
return false;
}
memcpy(data, vector->data, vector->size_elem * vector->max_elements);
free(vector->data);
vector->data = data;
vector->max_elements = length;
return true;
}
bool
wasm_vector_init(Vector *vector, uint32 init_length, uint32 size_elem)
{
if (!vector) {
LOG_ERROR("Init vector failed: vector is NULL.\n");
return false;
}
if (init_length == 0) {
init_length = 4;
}
if (!(vector->data = alloc_vector_data(init_length, size_elem))) {
LOG_ERROR("Init vector failed: alloc memory failed.\n");
return false;
}
vector->size_elem = size_elem;
vector->max_elements = init_length;
vector->num_elements = 0;
return true;
}
bool
wasm_vector_set(Vector *vector, uint32 index, const void *elem_buf)
{
if (!vector || !elem_buf) {
LOG_ERROR("Set vector elem failed: vector or elem buf is NULL.\n");
return false;
}
if (index >= vector->num_elements) {
LOG_ERROR("Set vector elem failed: invalid elem index.\n");
return false;
}
memcpy(vector->data + vector->size_elem * index,
elem_buf, vector->size_elem);
return true;
}
bool wasm_vector_get(const Vector *vector, uint32 index, void *elem_buf)
{
if (!vector || !elem_buf) {
LOG_ERROR("Get vector elem failed: vector or elem buf is NULL.\n");
return false;
}
if (index >= vector->num_elements) {
LOG_ERROR("Get vector elem failed: invalid elem index.\n");
return false;
}
memcpy(elem_buf, vector->data + vector->size_elem * index,
vector->size_elem);
return true;
}
bool wasm_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
{
uint32 i;
uint8 *p;
if (!vector || !elem_buf) {
LOG_ERROR("Insert vector elem failed: vector or elem buf is NULL.\n");
return false;
}
if (index >= vector->num_elements) {
LOG_ERROR("Insert vector elem failed: invalid elem index.\n");
return false;
}
if (!extend_vector(vector, vector->num_elements + 1)) {
LOG_ERROR("Insert vector elem failed: extend vector failed.\n");
return false;
}
p = vector->data + vector->size_elem * vector->num_elements;
for (i = vector->num_elements - 1; i > index; i--) {
memcpy(p, p - vector->size_elem, vector->size_elem);
p -= vector->size_elem;
}
memcpy(p, elem_buf, vector->size_elem);
vector->num_elements++;
return true;
}
bool wasm_vector_append(Vector *vector, const void *elem_buf)
{
if (!vector || !elem_buf) {
LOG_ERROR("Append vector elem failed: vector or elem buf is NULL.\n");
return false;
}
if (!extend_vector(vector, vector->num_elements + 1)) {
LOG_ERROR("Append ector elem failed: extend vector failed.\n");
return false;
}
memcpy(vector->data + vector->size_elem * vector->num_elements,
elem_buf, vector->size_elem);
vector->num_elements++;
return true;
}
bool
wasm_vector_remove(Vector *vector, uint32 index, void *old_elem_buf)
{
uint32 i;
uint8 *p;
if (!vector) {
LOG_ERROR("Remove vector elem failed: vector is NULL.\n");
return false;
}
if (index >= vector->num_elements) {
LOG_ERROR("Remove vector elem failed: invalid elem index.\n");
return false;
}
p = vector->data + vector->size_elem * index;
if (old_elem_buf) {
memcpy(old_elem_buf, p, vector->size_elem);
}
for (i = index; i < vector->num_elements - 1; i++) {
memcpy(p, p + vector->size_elem, vector->size_elem);
p += vector->size_elem;
}
vector->num_elements--;
return true;
}
uint32
wasm_vector_size(const Vector *vector)
{
return vector ? vector->num_elements : 0;
}
bool
wasm_vector_destroy(Vector *vector)
{
if (!vector) {
LOG_ERROR("Destroy vector elem failed: vector is NULL.\n");
return false;
}
if (vector->data)
wasm_free(vector->data);
memset(vector, 0, sizeof(Vector));
return true;
}