Apply clang-format for core/shared and product-mini files (#785)

Apply clang-format for core/shared and product-mini files
This commit is contained in:
Wenyong Huang 2021-10-14 09:12:07 +08:00 committed by GitHub
parent fb4afc7ca4
commit 17f62ad472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
107 changed files with 3436 additions and 2898 deletions

View File

@ -44,6 +44,7 @@
#ifndef COAP_CONSTANTS_H_ #ifndef COAP_CONSTANTS_H_
#define COAP_CONSTANTS_H_ #define COAP_CONSTANTS_H_
/* clang-format off */
#define COAP_DEFAULT_PORT 5683 #define COAP_DEFAULT_PORT 5683
#define COAP_DEFAULT_SECURE_PORT 5684 #define COAP_DEFAULT_SECURE_PORT 5684
@ -65,128 +66,129 @@
#define COAP_HEADER_OPTION_DELTA_MASK 0xF0 #define COAP_HEADER_OPTION_DELTA_MASK 0xF0
#define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F #define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
/* clang-format on */
/* CoAP message types */ /* CoAP message types */
typedef enum { typedef enum {
COAP_TYPE_CON, /* confirmables */ COAP_TYPE_CON, /* confirmables */
COAP_TYPE_NON, /* non-confirmables */ COAP_TYPE_NON, /* non-confirmables */
COAP_TYPE_ACK, /* acknowledgements */ COAP_TYPE_ACK, /* acknowledgements */
COAP_TYPE_RST /* reset */ COAP_TYPE_RST /* reset */
} coap_message_type_t; } coap_message_type_t;
/* clang-format off */
/* CoAP request method codes */ /* CoAP request method codes */
typedef enum { typedef enum {
COAP_GET = 1, COAP_GET = 1,
COAP_POST, COAP_POST, COAP_PUT,
COAP_PUT, COAP_DELETE
COAP_DELETE
} coap_method_t; } coap_method_t;
/* clang-format on */
/* CoAP response codes */ /* CoAP response codes */
typedef enum { typedef enum {
COAP_NO_ERROR = 0, COAP_NO_ERROR = 0,
CREATED_2_01 = 65, /* CREATED */ CREATED_2_01 = 65, /* CREATED */
DELETED_2_02 = 66, /* DELETED */ DELETED_2_02 = 66, /* DELETED */
VALID_2_03 = 67, /* NOT_MODIFIED */ VALID_2_03 = 67, /* NOT_MODIFIED */
CHANGED_2_04 = 68, /* CHANGED */ CHANGED_2_04 = 68, /* CHANGED */
CONTENT_2_05 = 69, /* OK */ CONTENT_2_05 = 69, /* OK */
CONTINUE_2_31 = 95, /* CONTINUE */ CONTINUE_2_31 = 95, /* CONTINUE */
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
BAD_OPTION_4_02 = 130, /* BAD_OPTION */ BAD_OPTION_4_02 = 130, /* BAD_OPTION */
FORBIDDEN_4_03 = 131, /* FORBIDDEN */ FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */ NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */ REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */ PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
/* Erbium errors */ /* Erbium errors */
MEMORY_ALLOCATION_ERROR = 192, MEMORY_ALLOCATION_ERROR = 192,
PACKET_SERIALIZATION_ERROR, PACKET_SERIALIZATION_ERROR,
/* Erbium hooks */ /* Erbium hooks */
MANUAL_RESPONSE, MANUAL_RESPONSE,
PING_RESPONSE PING_RESPONSE
} coap_status_t; } coap_status_t;
/* CoAP header option numbers */ /* CoAP header option numbers */
typedef enum { typedef enum {
COAP_OPTION_IF_MATCH = 1, /* 0-8 B */ COAP_OPTION_IF_MATCH = 1, /* 0-8 B */
COAP_OPTION_URI_HOST = 3, /* 1-255 B */ COAP_OPTION_URI_HOST = 3, /* 1-255 B */
COAP_OPTION_ETAG = 4, /* 1-8 B */ COAP_OPTION_ETAG = 4, /* 1-8 B */
COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */ COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */
COAP_OPTION_OBSERVE = 6, /* 0-3 B */ COAP_OPTION_OBSERVE = 6, /* 0-3 B */
COAP_OPTION_URI_PORT = 7, /* 0-2 B */ COAP_OPTION_URI_PORT = 7, /* 0-2 B */
COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */ COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */
COAP_OPTION_URI_PATH = 11, /* 0-255 B */ COAP_OPTION_URI_PATH = 11, /* 0-255 B */
COAP_OPTION_CONTENT_FORMAT = 12, /* 0-2 B */ COAP_OPTION_CONTENT_FORMAT = 12, /* 0-2 B */
COAP_OPTION_MAX_AGE = 14, /* 0-4 B */ COAP_OPTION_MAX_AGE = 14, /* 0-4 B */
COAP_OPTION_URI_QUERY = 15, /* 0-255 B */ COAP_OPTION_URI_QUERY = 15, /* 0-255 B */
COAP_OPTION_ACCEPT = 17, /* 0-2 B */ COAP_OPTION_ACCEPT = 17, /* 0-2 B */
COAP_OPTION_LOCATION_QUERY = 20, /* 0-255 B */ COAP_OPTION_LOCATION_QUERY = 20, /* 0-255 B */
COAP_OPTION_BLOCK2 = 23, /* 1-3 B */ COAP_OPTION_BLOCK2 = 23, /* 1-3 B */
COAP_OPTION_BLOCK1 = 27, /* 1-3 B */ COAP_OPTION_BLOCK1 = 27, /* 1-3 B */
COAP_OPTION_SIZE2 = 28, /* 0-4 B */ COAP_OPTION_SIZE2 = 28, /* 0-4 B */
COAP_OPTION_PROXY_URI = 35, /* 1-1034 B */ COAP_OPTION_PROXY_URI = 35, /* 1-1034 B */
COAP_OPTION_PROXY_SCHEME = 39, /* 1-255 B */ COAP_OPTION_PROXY_SCHEME = 39, /* 1-255 B */
COAP_OPTION_SIZE1 = 60, /* 0-4 B */ COAP_OPTION_SIZE1 = 60, /* 0-4 B */
} coap_option_t; } coap_option_t;
/* CoAP Content-Formats */ /* CoAP Content-Formats */
typedef enum { typedef enum {
TEXT_PLAIN = 0, TEXT_PLAIN = 0,
TEXT_XML = 1, TEXT_XML = 1,
TEXT_CSV = 2, TEXT_CSV = 2,
TEXT_HTML = 3, TEXT_HTML = 3,
IMAGE_GIF = 21, IMAGE_GIF = 21,
IMAGE_JPEG = 22, IMAGE_JPEG = 22,
IMAGE_PNG = 23, IMAGE_PNG = 23,
IMAGE_TIFF = 24, IMAGE_TIFF = 24,
AUDIO_RAW = 25, AUDIO_RAW = 25,
VIDEO_RAW = 26, VIDEO_RAW = 26,
APPLICATION_LINK_FORMAT = 40, APPLICATION_LINK_FORMAT = 40,
APPLICATION_XML = 41, APPLICATION_XML = 41,
APPLICATION_OCTET_STREAM = 42, APPLICATION_OCTET_STREAM = 42,
APPLICATION_RDF_XML = 43, APPLICATION_RDF_XML = 43,
APPLICATION_SOAP_XML = 44, APPLICATION_SOAP_XML = 44,
APPLICATION_ATOM_XML = 45, APPLICATION_ATOM_XML = 45,
APPLICATION_XMPP_XML = 46, APPLICATION_XMPP_XML = 46,
APPLICATION_EXI = 47, APPLICATION_EXI = 47,
APPLICATION_FASTINFOSET = 48, APPLICATION_FASTINFOSET = 48,
APPLICATION_SOAP_FASTINFOSET = 49, APPLICATION_SOAP_FASTINFOSET = 49,
APPLICATION_JSON = 50, APPLICATION_JSON = 50,
APPLICATION_X_OBIX_BINARY = 51 APPLICATION_X_OBIX_BINARY = 51
} coap_content_format_t; } coap_content_format_t;
/** /**
* Resource flags for allowed methods and special functionalities. * Resource flags for allowed methods and special functionalities.
*/ */
typedef enum { typedef enum {
NO_FLAGS = 0, NO_FLAGS = 0,
/* methods to handle */ /* methods to handle */
METHOD_GET = (1 << 0), METHOD_GET = (1 << 0),
METHOD_POST = (1 << 1), METHOD_POST = (1 << 1),
METHOD_PUT = (1 << 2), METHOD_PUT = (1 << 2),
METHOD_DELETE = (1 << 3), METHOD_DELETE = (1 << 3),
/* special flags */ /* special flags */
HAS_SUB_RESOURCES = (1 << 4), HAS_SUB_RESOURCES = (1 << 4),
IS_SEPARATE = (1 << 5), IS_SEPARATE = (1 << 5),
IS_OBSERVABLE = (1 << 6), IS_OBSERVABLE = (1 << 6),
IS_PERIODIC = (1 << 7) IS_PERIODIC = (1 << 7)
} coap_resource_flags_t; } coap_resource_flags_t;
#endif /* COAP_CONSTANTS_H_ */ #endif /* COAP_CONSTANTS_H_ */
/** @} */

View File

@ -5,15 +5,11 @@
#include "ems_gc_internal.h" #include "ems_gc_internal.h"
static inline bool static inline bool
hmu_is_in_heap(void *hmu, hmu_is_in_heap(void *hmu, gc_uint8 *heap_base_addr, gc_uint8 *heap_end_addr)
gc_uint8 *heap_base_addr,
gc_uint8 *heap_end_addr)
{ {
gc_uint8 *addr = (gc_uint8 *)hmu; gc_uint8 *addr = (gc_uint8 *)hmu;
return (addr >= heap_base_addr && addr < heap_end_addr) return (addr >= heap_base_addr && addr < heap_end_addr) ? true : false;
? true : false;
} }
/** /**
@ -38,8 +34,7 @@ remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p)
parent = p->parent; parent = p->parent;
if (!parent || p == root /* p can not be the ROOT node */ if (!parent || p == root /* p can not be the ROOT node */
|| !hmu_is_in_heap(p, base_addr, end_addr) || !hmu_is_in_heap(p, base_addr, end_addr)
|| (parent != root || (parent != root && !hmu_is_in_heap(parent, base_addr, end_addr))) {
&& !hmu_is_in_heap(parent, base_addr, end_addr))) {
goto fail; goto fail;
} }
@ -139,8 +134,8 @@ unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
gc_size_t size; gc_size_t size;
bh_assert(gci_is_heap_valid(heap)); bh_assert(gci_is_heap_valid(heap));
bh_assert(hmu && (gc_uint8*) hmu >= heap->base_addr bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr
&& (gc_uint8*) hmu < heap->base_addr + heap->current_size); && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
if (hmu_get_ut(hmu) != HMU_FC) { if (hmu_get_ut(hmu) != HMU_FC) {
heap->is_heap_corrupted = true; heap->is_heap_corrupted = true;
@ -162,7 +157,7 @@ unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
return false; return false;
} }
node_next = get_hmu_normal_node_next(node); node_next = get_hmu_normal_node_next(node);
if ((hmu_t*)node == hmu) { if ((hmu_t *)node == hmu) {
if (!node_prev) /* list head */ if (!node_prev) /* list head */
heap->kfc_normal_list[node_idx].next = node_next; heap->kfc_normal_list[node_idx].next = node_next;
else else
@ -191,15 +186,15 @@ hmu_set_free_size(hmu_t *hmu)
bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC); bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
size = hmu_get_size(hmu); size = hmu_get_size(hmu);
*((uint32*)((char*) hmu + size) - 1) = size; *((uint32 *)((char *)hmu + size) - 1) = size;
} }
/** /**
* Add free chunk back to KFC * Add free chunk back to KFC
* *
* @param heap should not be NULL and it should be a valid heap * @param heap should not be NULL and it should be a valid heap
* @param hmu should not be NULL and it should be a HMU of length @size inside @heap * @param hmu should not be NULL and it should be a HMU of length @size inside
* hmu should be 8-bytes aligned * @heap hmu should be 8-bytes aligned
* @param size should be positive and multiple of 8 * @param size should be positive and multiple of 8
* hmu with size @size will be added into KFC as a new FC. * hmu with size @size will be added into KFC as a new FC.
*/ */
@ -212,11 +207,12 @@ gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
uint32 node_idx; uint32 node_idx;
bh_assert(gci_is_heap_valid(heap)); bh_assert(gci_is_heap_valid(heap));
bh_assert(hmu && (gc_uint8*)hmu >= heap->base_addr bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr
&& (gc_uint8*)hmu < heap->base_addr + heap->current_size); && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0); bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
bh_assert(size > 0 bh_assert(size > 0
&& ((gc_uint8*)hmu) + size <= heap->base_addr + heap->current_size); && ((gc_uint8 *)hmu) + size
<= heap->base_addr + heap->current_size);
bh_assert(!(size & 7)); bh_assert(!(size & 7));
base_addr = heap->base_addr; base_addr = heap->base_addr;
@ -227,7 +223,7 @@ gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
hmu_set_free_size(hmu); hmu_set_free_size(hmu);
if (HMU_IS_FC_NORMAL(size)) { if (HMU_IS_FC_NORMAL(size)) {
np = (hmu_normal_node_t*)hmu; np = (hmu_normal_node_t *)hmu;
if (!hmu_is_in_heap(np, base_addr, end_addr)) { if (!hmu_is_in_heap(np, base_addr, end_addr)) {
heap->is_heap_corrupted = true; heap->is_heap_corrupted = true;
return false; return false;
@ -240,7 +236,7 @@ gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
} }
/* big block*/ /* big block*/
node = (hmu_tree_node_t*)hmu; node = (hmu_tree_node_t *)hmu;
node->size = size; node->size = size;
node->left = node->right = node->parent = NULL; node->left = node->right = node->parent = NULL;
@ -308,7 +304,7 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
/* find a non-empty slot in normal_node_list with good size*/ /* find a non-empty slot in normal_node_list with good size*/
init_node_idx = (size >> 3); init_node_idx = (size >> 3);
for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT; for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
node_idx++) { node_idx++) {
normal_head = heap->kfc_normal_list + node_idx; normal_head = heap->kfc_normal_list + node_idx;
if (normal_head->next) if (normal_head->next)
break; break;
@ -333,7 +329,7 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
if ((gc_size_t)node_idx != (uint32)init_node_idx if ((gc_size_t)node_idx != (uint32)init_node_idx
/* with bigger size*/ /* with bigger size*/
&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { && ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) {
rest = (hmu_t*) (((char *) p) + size); rest = (hmu_t *)(((char *)p) + size);
if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) { if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) {
return NULL; return NULL;
} }
@ -341,7 +337,7 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
} }
else { else {
size = node_idx << 3; size = node_idx << 3;
next = (hmu_t*) ((char*) p + size); next = (hmu_t *)((char *)p + size);
if (hmu_is_in_heap(next, base_addr, end_addr)) if (hmu_is_in_heap(next, base_addr, end_addr))
hmu_mark_pinuse(next); hmu_mark_pinuse(next);
} }
@ -349,11 +345,11 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
heap->total_free_size -= size; heap->total_free_size -= size;
if ((heap->current_size - heap->total_free_size) if ((heap->current_size - heap->total_free_size)
> heap->highmark_size) > heap->highmark_size)
heap->highmark_size = heap->current_size heap->highmark_size =
- heap->total_free_size; heap->current_size - heap->total_free_size;
hmu_set_size((hmu_t* )p, size); hmu_set_size((hmu_t *)p, size);
return (hmu_t*)p; return (hmu_t *)p;
} }
} }
@ -389,14 +385,14 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
return NULL; return NULL;
if (last_tp->size >= size + GC_SMALLEST_SIZE) { if (last_tp->size >= size + GC_SMALLEST_SIZE) {
rest = (hmu_t*)((char*)last_tp + size); rest = (hmu_t *)((char *)last_tp + size);
if (!gci_add_fc(heap, rest, last_tp->size - size)) if (!gci_add_fc(heap, rest, last_tp->size - size))
return NULL; return NULL;
hmu_mark_pinuse(rest); hmu_mark_pinuse(rest);
} }
else { else {
size = last_tp->size; size = last_tp->size;
next = (hmu_t*)((char*)last_tp + size); next = (hmu_t *)((char *)last_tp + size);
if (hmu_is_in_heap(next, base_addr, end_addr)) if (hmu_is_in_heap(next, base_addr, end_addr))
hmu_mark_pinuse(next); hmu_mark_pinuse(next);
} }
@ -405,8 +401,8 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
if ((heap->current_size - heap->total_free_size) > heap->highmark_size) if ((heap->current_size - heap->total_free_size) > heap->highmark_size)
heap->highmark_size = heap->current_size - heap->total_free_size; heap->highmark_size = heap->current_size - heap->total_free_size;
hmu_set_size((hmu_t*)last_tp, size); hmu_set_size((hmu_t *)last_tp, size);
return (hmu_t*)last_tp; return (hmu_t *)last_tp;
} }
return NULL; return NULL;
@ -444,13 +440,12 @@ gc_object_t
gc_alloc_vo(void *vheap, gc_size_t size) gc_alloc_vo(void *vheap, gc_size_t size)
#else #else
gc_object_t gc_object_t
gc_alloc_vo_internal(void *vheap, gc_size_t size, gc_alloc_vo_internal(void *vheap, gc_size_t size, const char *file, int line)
const char *file, int line)
#endif #endif
{ {
gc_heap_t* heap = (gc_heap_t*) vheap; gc_heap_t *heap = (gc_heap_t *)vheap;
hmu_t *hmu = NULL; hmu_t *hmu = NULL;
gc_object_t ret = (gc_object_t) NULL; gc_object_t ret = (gc_object_t)NULL;
gc_size_t tot_size = 0, tot_size_unaligned; gc_size_t tot_size = 0, tot_size_unaligned;
/* hmu header + prefix + obj + suffix */ /* hmu header + prefix + obj + suffix */
@ -489,7 +484,7 @@ gc_alloc_vo_internal(void *vheap, gc_size_t size,
ret = hmu_to_obj(hmu); ret = hmu_to_obj(hmu);
if (tot_size > tot_size_unaligned) if (tot_size > tot_size_unaligned)
/* clear buffer appended by GC_ALIGN_8() */ /* clear buffer appended by GC_ALIGN_8() */
memset((uint8*)ret + size, 0, tot_size - tot_size_unaligned); memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
finish: finish:
os_mutex_unlock(&heap->lock); os_mutex_unlock(&heap->lock);
@ -501,13 +496,13 @@ gc_object_t
gc_realloc_vo(void *vheap, void *ptr, gc_size_t size) gc_realloc_vo(void *vheap, void *ptr, gc_size_t size)
#else #else
gc_object_t gc_object_t
gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file,
const char *file, int line) int line)
#endif #endif
{ {
gc_heap_t* heap = (gc_heap_t*) vheap; gc_heap_t *heap = (gc_heap_t *)vheap;
hmu_t *hmu = NULL, *hmu_old = NULL, *hmu_next; hmu_t *hmu = NULL, *hmu_old = NULL, *hmu_next;
gc_object_t ret = (gc_object_t) NULL, obj_old = (gc_object_t)ptr; gc_object_t ret = (gc_object_t)NULL, obj_old = (gc_object_t)ptr;
gc_size_t tot_size, tot_size_unaligned, tot_size_old = 0, tot_size_next; gc_size_t tot_size, tot_size_unaligned, tot_size_old = 0, tot_size_next;
gc_size_t obj_size, obj_size_old; gc_size_t obj_size, obj_size_old;
gc_uint8 *base_addr, *end_addr; gc_uint8 *base_addr, *end_addr;
@ -540,24 +535,24 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
os_mutex_lock(&heap->lock); os_mutex_lock(&heap->lock);
if (hmu_old) { if (hmu_old) {
hmu_next = (hmu_t*)((char *)hmu_old + tot_size_old); hmu_next = (hmu_t *)((char *)hmu_old + tot_size_old);
if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) { if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) {
ut = hmu_get_ut(hmu_next); ut = hmu_get_ut(hmu_next);
tot_size_next = hmu_get_size(hmu_next); tot_size_next = hmu_get_size(hmu_next);
if (ut == HMU_FC if (ut == HMU_FC && tot_size <= tot_size_old + tot_size_next) {
&& tot_size <= tot_size_old + tot_size_next) {
/* current node and next node meets requirement */ /* current node and next node meets requirement */
if (!unlink_hmu(heap, hmu_next)) { if (!unlink_hmu(heap, hmu_next)) {
os_mutex_unlock(&heap->lock); os_mutex_unlock(&heap->lock);
return NULL; return NULL;
} }
hmu_set_size(hmu_old, tot_size); hmu_set_size(hmu_old, tot_size);
memset((char*)hmu_old + tot_size_old, 0, tot_size - tot_size_old); memset((char *)hmu_old + tot_size_old, 0,
tot_size - tot_size_old);
#if BH_ENABLE_GC_VERIFY != 0 #if BH_ENABLE_GC_VERIFY != 0
hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line); hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line);
#endif #endif
if (tot_size < tot_size_old + tot_size_next) { if (tot_size < tot_size_old + tot_size_next) {
hmu_next = (hmu_t*)((char*)hmu_old + tot_size); hmu_next = (hmu_t *)((char *)hmu_old + tot_size);
tot_size_next = tot_size_old + tot_size_next - tot_size; tot_size_next = tot_size_old + tot_size_next - tot_size;
if (!gci_add_fc(heap, hmu_next, tot_size_next)) { if (!gci_add_fc(heap, hmu_next, tot_size_next)) {
os_mutex_unlock(&heap->lock); os_mutex_unlock(&heap->lock);
@ -570,7 +565,6 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size,
} }
} }
hmu = alloc_hmu_ex(heap, tot_size); hmu = alloc_hmu_ex(heap, tot_size);
if (!hmu) if (!hmu)
goto finish; goto finish;
@ -596,8 +590,8 @@ finish:
obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE; obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
memset(ret, 0, obj_size); memset(ret, 0, obj_size);
if (obj_old) { if (obj_old) {
obj_size_old = tot_size_old - HMU_SIZE obj_size_old =
- OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE; tot_size_old - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
bh_memcpy_s(ret, obj_size, obj_old, obj_size_old); bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
} }
} }
@ -619,7 +613,7 @@ gci_is_heap_valid(gc_heap_t *heap)
{ {
if (!heap) if (!heap)
return GC_FALSE; return GC_FALSE;
if (heap->heap_id != (gc_handle_t) heap) if (heap->heap_id != (gc_handle_t)heap)
return GC_FALSE; return GC_FALSE;
return GC_TRUE; return GC_TRUE;
@ -630,11 +624,10 @@ int
gc_free_vo(void *vheap, gc_object_t obj) gc_free_vo(void *vheap, gc_object_t obj)
#else #else
int int
gc_free_vo_internal(void *vheap, gc_object_t obj, gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
const char *file, int line)
#endif #endif
{ {
gc_heap_t* heap = (gc_heap_t*) vheap; gc_heap_t *heap = (gc_heap_t *)vheap;
gc_uint8 *base_addr, *end_addr; gc_uint8 *base_addr, *end_addr;
hmu_t *hmu = NULL; hmu_t *hmu = NULL;
hmu_t *prev = NULL; hmu_t *prev = NULL;
@ -678,7 +671,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj,
heap->total_free_size += size; heap->total_free_size += size;
if (!hmu_get_pinuse(hmu)) { if (!hmu_get_pinuse(hmu)) {
prev = (hmu_t*) ((char*) hmu - *((int*) hmu - 1)); prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1));
if (hmu_is_in_heap(prev, base_addr, end_addr) if (hmu_is_in_heap(prev, base_addr, end_addr)
&& hmu_get_ut(prev) == HMU_FC) { && hmu_get_ut(prev) == HMU_FC) {
@ -691,7 +684,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj,
} }
} }
next = (hmu_t*) ((char*) hmu + size); next = (hmu_t *)((char *)hmu + size);
if (hmu_is_in_heap(next, base_addr, end_addr)) { if (hmu_is_in_heap(next, base_addr, end_addr)) {
if (hmu_get_ut(next) == HMU_FC) { if (hmu_get_ut(next) == HMU_FC) {
size += hmu_get_size(next); size += hmu_get_size(next);
@ -699,7 +692,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj,
ret = GC_ERROR; ret = GC_ERROR;
goto out; goto out;
} }
next = (hmu_t*)((char*) hmu + size); next = (hmu_t *)((char *)hmu + size);
} }
} }
@ -711,8 +704,8 @@ gc_free_vo_internal(void *vheap, gc_object_t obj,
if (hmu_is_in_heap(next, base_addr, end_addr)) { if (hmu_is_in_heap(next, base_addr, end_addr)) {
hmu_unmark_pinuse(next); hmu_unmark_pinuse(next);
} }
}
} else { else {
ret = GC_ERROR; ret = GC_ERROR;
goto out; goto out;
} }
@ -750,14 +743,14 @@ gci_dump(gc_heap_t *heap)
int i = 0, p, mark; int i = 0, p, mark;
char inuse = 'U'; char inuse = 'U';
cur = (hmu_t*)heap->base_addr; cur = (hmu_t *)heap->base_addr;
end = (hmu_t*)((char*)heap->base_addr + heap->current_size); end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
while(cur < end) { while (cur < end) {
ut = hmu_get_ut(cur); ut = hmu_get_ut(cur);
size = hmu_get_size(cur); size = hmu_get_size(cur);
p = hmu_get_pinuse(cur); p = hmu_get_pinuse(cur);
mark = hmu_is_jo_marked (cur); mark = hmu_is_jo_marked(cur);
if (ut == HMU_VO) if (ut == HMU_VO)
inuse = 'V'; inuse = 'V';
@ -772,9 +765,9 @@ gci_dump(gc_heap_t *heap)
return; return;
} }
os_printf("#%d %08x %x %x %d %c %d\n", os_printf("#%d %08x %x %x %d %c %d\n", i,
i, (int32)((char*) cur - (char*) heap->base_addr), (int32)((char *)cur - (char *)heap->base_addr), ut, p, mark,
ut, p, mark, inuse, (int32)hmu_obj_size(size)); inuse, (int32)hmu_obj_size(size));
#if BH_ENABLE_GC_VERIFY != 0 #if BH_ENABLE_GC_VERIFY != 0
if (inuse == 'V') { if (inuse == 'V') {
gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1); gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
@ -782,10 +775,9 @@ gci_dump(gc_heap_t *heap)
} }
#endif #endif
cur = (hmu_t*)((char *)cur + size); cur = (hmu_t *)((char *)cur + size);
i++; i++;
} }
bh_assert(cur == end); bh_assert(cur == end);
} }

View File

@ -8,8 +8,6 @@
* @date Wed Aug 3 10:46:38 2011 * @date Wed Aug 3 10:46:38 2011
* *
* @brief This file defines GC modules types and interfaces. * @brief This file defines GC modules types and interfaces.
*
*
*/ */
#ifndef _EMS_GC_H #ifndef _EMS_GC_H
@ -33,15 +31,15 @@ extern "C" {
#define GC_MAX_HEAP_SIZE (256 * BH_KB) #define GC_MAX_HEAP_SIZE (256 * BH_KB)
typedef void * gc_handle_t; typedef void *gc_handle_t;
typedef void * gc_object_t; typedef void *gc_object_t;
typedef int64 gc_int64; typedef int64 gc_int64;
typedef uint32 gc_uint32; typedef uint32 gc_uint32;
typedef int32 gc_int32; typedef int32 gc_int32;
typedef uint16 gc_uint16; typedef uint16 gc_uint16;
typedef int16 gc_int16; typedef int16 gc_int16;
typedef uint8 gc_uint8; typedef uint8 gc_uint8;
typedef int8 gc_int8; typedef int8 gc_int8;
typedef uint32 gc_size_t; typedef uint32 gc_size_t;
typedef enum { typedef enum {
@ -105,8 +103,7 @@ gc_get_heap_struct_size(void);
* @return GC_SUCCESS if success, GC_ERROR otherwise * @return GC_SUCCESS if success, GC_ERROR otherwise
*/ */
int int
gc_migrate(gc_handle_t handle, gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size);
char *pool_buf_new, gc_size_t pool_buf_size);
/** /**
* Check whether the heap is corrupted * Check whether the heap is corrupted
@ -126,7 +123,7 @@ gc_is_heap_corrupted(gc_handle_t handle);
* @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE * @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
*/ */
void * void *
gc_heap_stats(void *heap, uint32* stats, int size); gc_heap_stats(void *heap, uint32 *stats, int size);
#if BH_ENABLE_GC_VERIFY == 0 #if BH_ENABLE_GC_VERIFY == 0
@ -142,17 +139,16 @@ gc_free_vo(void *heap, gc_object_t obj);
#else /* else of BH_ENABLE_GC_VERIFY */ #else /* else of BH_ENABLE_GC_VERIFY */
gc_object_t gc_object_t
gc_alloc_vo_internal(void *heap, gc_size_t size, gc_alloc_vo_internal(void *heap, gc_size_t size, const char *file, int line);
const char *file, int line);
gc_object_t gc_object_t
gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, const char *file,
const char *file, int line); int line);
int int
gc_free_vo_internal(void *heap, gc_object_t obj, gc_free_vo_internal(void *heap, gc_object_t obj, const char *file, int line);
const char *file, int line);
/* clang-format off */
#define gc_alloc_vo(heap, size) \ #define gc_alloc_vo(heap, size) \
gc_alloc_vo_internal(heap, size, __FILE__, __LINE__) gc_alloc_vo_internal(heap, size, __FILE__, __LINE__)
@ -161,6 +157,7 @@ gc_free_vo_internal(void *heap, gc_object_t obj,
#define gc_free_vo(heap, obj) \ #define gc_free_vo(heap, obj) \
gc_free_vo_internal(heap, obj, __FILE__, __LINE__) gc_free_vo_internal(heap, obj, __FILE__, __LINE__)
/* clang-format on */
#endif /* end of BH_ENABLE_GC_VERIFY */ #endif /* end of BH_ENABLE_GC_VERIFY */
@ -169,4 +166,3 @@ gc_free_vo_internal(void *heap, gc_object_t obj,
#endif #endif
#endif #endif

View File

@ -60,8 +60,8 @@ hmu_init_prefix_and_suffix(hmu_t *hmu, gc_size_t tot_size,
void void
hmu_verify(void *vheap, hmu_t *hmu); hmu_verify(void *vheap, hmu_t *hmu);
#define SKIP_OBJ_PREFIX(p) ((void*)((gc_uint8*)(p) + OBJ_PREFIX_SIZE)) #define SKIP_OBJ_PREFIX(p) ((void *)((gc_uint8 *)(p) + OBJ_PREFIX_SIZE))
#define SKIP_OBJ_SUFFIX(p) ((void*)((gc_uint8*)(p) + OBJ_SUFFIX_SIZE)) #define SKIP_OBJ_SUFFIX(p) ((void *)((gc_uint8 *)(p) + OBJ_SUFFIX_SIZE))
#define OBJ_EXTRA_SIZE (HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE) #define OBJ_EXTRA_SIZE (HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE)
@ -70,8 +70,8 @@ hmu_verify(void *vheap, hmu_t *hmu);
#define OBJ_PREFIX_SIZE 0 #define OBJ_PREFIX_SIZE 0
#define OBJ_SUFFIX_SIZE 0 #define OBJ_SUFFIX_SIZE 0
#define SKIP_OBJ_PREFIX(p) ((void*)((gc_uint8*)(p) + OBJ_PREFIX_SIZE)) #define SKIP_OBJ_PREFIX(p) ((void *)((gc_uint8 *)(p) + OBJ_PREFIX_SIZE))
#define SKIP_OBJ_SUFFIX(p) ((void*)((gc_uint8*)(p) + OBJ_SUFFIX_SIZE)) #define SKIP_OBJ_SUFFIX(p) ((void *)((gc_uint8 *)(p) + OBJ_SUFFIX_SIZE))
#define OBJ_EXTRA_SIZE (HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE) #define OBJ_EXTRA_SIZE (HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE)
@ -81,8 +81,11 @@ hmu_verify(void *vheap, hmu_t *hmu);
#define GC_ALIGN_8(s) (((uint32)(s) + 7) & (uint32)~7) #define GC_ALIGN_8(s) (((uint32)(s) + 7) & (uint32)~7)
#define GC_SMALLEST_SIZE GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + 8) #define GC_SMALLEST_SIZE \
#define GC_GET_REAL_SIZE(x) GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + (((x) > 8) ? (x): 8)) GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + 8)
#define GC_GET_REAL_SIZE(x) \
GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE \
+ (((x) > 8) ? (x) : 8))
/** /**
* hmu bit operation * hmu bit operation
@ -92,12 +95,17 @@ hmu_verify(void *vheap, hmu_t *hmu);
#define GETBIT(v, offset) ((v) & ((uint32)1 << (offset)) ? 1 : 0) #define GETBIT(v, offset) ((v) & ((uint32)1 << (offset)) ? 1 : 0)
#define CLRBIT(v, offset) (v) &= (~((uint32)1 << (offset))) #define CLRBIT(v, offset) (v) &= (~((uint32)1 << (offset)))
#define SETBITS(v, offset, size, value) do { \ /* clang-format off */
(v) &= ~((((uint32)1 << size) - 1) << offset); \ #define SETBITS(v, offset, size, value) \
(v) |= ((uint32)value << offset); \ do { \
} while(0) (v) &= ~((((uint32)1 << size) - 1) << offset); \
#define CLRBITS(v, offset, size) (v) &= ~((((uint32)1 << size) - 1) << offset) (v) |= ((uint32)value << offset); \
#define GETBITS(v, offset, size) (((v) & (((((uint32)1 << size) - 1) << offset))) >> offset) } while (0)
#define CLRBITS(v, offset, size) \
(v) &= ~((((uint32)1 << size) - 1) << offset)
#define GETBITS(v, offset, size) \
(((v) & (((((uint32)1 << size) - 1) << offset))) >> offset)
/* clang-format on */
/** /**
* gc object layout definition * gc object layout definition
@ -105,30 +113,35 @@ hmu_verify(void *vheap, hmu_t *hmu);
#define HMU_SIZE (sizeof(hmu_t)) #define HMU_SIZE (sizeof(hmu_t))
#define hmu_to_obj(hmu) (gc_object_t)(SKIP_OBJ_PREFIX((hmu_t*) (hmu) + 1)) #define hmu_to_obj(hmu) (gc_object_t)(SKIP_OBJ_PREFIX((hmu_t *)(hmu) + 1))
#define obj_to_hmu(obj) ((hmu_t *)((gc_uint8*)(obj) - OBJ_PREFIX_SIZE) - 1) #define obj_to_hmu(obj) ((hmu_t *)((gc_uint8 *)(obj)-OBJ_PREFIX_SIZE) - 1)
#define HMU_UT_SIZE 2 #define HMU_UT_SIZE 2
#define HMU_UT_OFFSET 30 #define HMU_UT_OFFSET 30
#define hmu_get_ut(hmu) GETBITS ((hmu)->header, HMU_UT_OFFSET, HMU_UT_SIZE) /* clang-format off */
#define hmu_set_ut(hmu, type) SETBITS ((hmu)->header, HMU_UT_OFFSET, HMU_UT_SIZE, type) #define hmu_get_ut(hmu) \
#define hmu_is_ut_valid(tp) (tp >= HMU_TYPE_MIN && tp <= HMU_TYPE_MAX) GETBITS((hmu)->header, HMU_UT_OFFSET, HMU_UT_SIZE)
#define hmu_set_ut(hmu, type) \
SETBITS((hmu)->header, HMU_UT_OFFSET, HMU_UT_SIZE, type)
#define hmu_is_ut_valid(tp) \
(tp >= HMU_TYPE_MIN && tp <= HMU_TYPE_MAX)
/* clang-format on */
/* P in use bit means the previous chunk is in use */ /* P in use bit means the previous chunk is in use */
#define HMU_P_OFFSET 29 #define HMU_P_OFFSET 29
#define hmu_mark_pinuse(hmu) SETBIT ((hmu)->header, HMU_P_OFFSET) #define hmu_mark_pinuse(hmu) SETBIT((hmu)->header, HMU_P_OFFSET)
#define hmu_unmark_pinuse(hmu) CLRBIT ((hmu)->header, HMU_P_OFFSET) #define hmu_unmark_pinuse(hmu) CLRBIT((hmu)->header, HMU_P_OFFSET)
#define hmu_get_pinuse(hmu) GETBIT ((hmu)->header, HMU_P_OFFSET) #define hmu_get_pinuse(hmu) GETBIT((hmu)->header, HMU_P_OFFSET)
#define HMU_JO_VT_SIZE 27 #define HMU_JO_VT_SIZE 27
#define HMU_JO_VT_OFFSET 0 #define HMU_JO_VT_OFFSET 0
#define HMU_JO_MB_OFFSET 28 #define HMU_JO_MB_OFFSET 28
#define hmu_mark_jo(hmu) SETBIT ((hmu)->header, HMU_JO_MB_OFFSET) #define hmu_mark_jo(hmu) SETBIT((hmu)->header, HMU_JO_MB_OFFSET)
#define hmu_unmark_jo(hmu) CLRBIT ((hmu)->header, HMU_JO_MB_OFFSET) #define hmu_unmark_jo(hmu) CLRBIT((hmu)->header, HMU_JO_MB_OFFSET)
#define hmu_is_jo_marked(hmu) GETBIT ((hmu)->header, HMU_JO_MB_OFFSET) #define hmu_is_jo_marked(hmu) GETBIT((hmu)->header, HMU_JO_MB_OFFSET)
/** /**
* The hmu size is divisible by 8, its lowest 3 bits are 0, so we only * The hmu size is divisible by 8, its lowest 3 bits are 0, so we only
@ -141,11 +154,13 @@ hmu_verify(void *vheap, hmu_t *hmu);
#define HMU_VO_FB_OFFSET 28 #define HMU_VO_FB_OFFSET 28
#define hmu_is_vo_freed(hmu) GETBIT ((hmu)->header, HMU_VO_FB_OFFSET) #define hmu_is_vo_freed(hmu) GETBIT((hmu)->header, HMU_VO_FB_OFFSET)
#define hmu_unfree_vo(hmu) CLRBIT ((hmu)->header, HMU_VO_FB_OFFSET) #define hmu_unfree_vo(hmu) CLRBIT((hmu)->header, HMU_VO_FB_OFFSET)
#define hmu_get_size(hmu) (GETBITS ((hmu)->header, HMU_SIZE_OFFSET, HMU_SIZE_SIZE) << 3) #define hmu_get_size(hmu) \
#define hmu_set_size(hmu, size) SETBITS ((hmu)->header, HMU_SIZE_OFFSET, HMU_SIZE_SIZE, ((size) >> 3)) (GETBITS((hmu)->header, HMU_SIZE_OFFSET, HMU_SIZE_SIZE) << 3)
#define hmu_set_size(hmu, size) \
SETBITS((hmu)->header, HMU_SIZE_OFFSET, HMU_SIZE_SIZE, ((size) >> 3))
/** /**
* HMU free chunk management * HMU free chunk management
@ -173,17 +188,16 @@ static inline hmu_normal_node_t *
get_hmu_normal_node_next(hmu_normal_node_t *node) get_hmu_normal_node_next(hmu_normal_node_t *node)
{ {
return node->next_offset return node->next_offset
? (hmu_normal_node_t *)((uint8*)node + node->next_offset) ? (hmu_normal_node_t *)((uint8 *)node + node->next_offset)
: NULL; : NULL;
} }
static inline void static inline void
set_hmu_normal_node_next(hmu_normal_node_t *node, hmu_normal_node_t *next) set_hmu_normal_node_next(hmu_normal_node_t *node, hmu_normal_node_t *next)
{ {
if (next) { if (next) {
bh_assert((uint8*)next - (uint8*)node < INT32_MAX); bh_assert((uint8 *)next - (uint8 *)node < INT32_MAX);
node->next_offset = (gc_int32)(intptr_t) node->next_offset = (gc_int32)(intptr_t)((uint8 *)next - (uint8 *)node);
((uint8*)next - (uint8*)node);
} }
else { else {
node->next_offset = 0; node->next_offset = 0;
@ -247,4 +261,4 @@ gci_dump(gc_heap_t *heap);
} }
#endif #endif
#endif #endif /* end of _EMS_GC_INTERNAL_H */

View File

@ -11,8 +11,8 @@
* Set default value to prefix and suffix * Set default value to prefix and suffix
* @param hmu should not be NULL and should have been correctly initilized * @param hmu should not be NULL and should have been correctly initilized
* (except prefix and suffix part) * (except prefix and suffix part)
* @param tot_size is offered here because hmu_get_size can not be used till now. * @param tot_size is offered here because hmu_get_size can not be used
* tot_size should not be smaller than OBJ_EXTRA_SIZE. * till now. tot_size should not be smaller than OBJ_EXTRA_SIZE.
* For VO, tot_size should be equal to object total size. * For VO, tot_size should be equal to object total size.
*/ */
void void
@ -30,16 +30,17 @@ hmu_init_prefix_and_suffix(hmu_t *hmu, gc_size_t tot_size,
bh_assert(hmu_get_ut(hmu) != HMU_VO || hmu_get_size(hmu) >= tot_size); bh_assert(hmu_get_ut(hmu) != HMU_VO || hmu_get_size(hmu) >= tot_size);
prefix = (gc_object_prefix_t *)(hmu + 1); prefix = (gc_object_prefix_t *)(hmu + 1);
suffix = (gc_object_suffix_t *)((gc_uint8*)hmu + tot_size - OBJ_SUFFIX_SIZE); suffix =
(gc_object_suffix_t *)((gc_uint8 *)hmu + tot_size - OBJ_SUFFIX_SIZE);
prefix->file_name = file_name; prefix->file_name = file_name;
prefix->line_no = line_no; prefix->line_no = line_no;
prefix->size = tot_size; prefix->size = tot_size;
for(i = 0;i < GC_OBJECT_PREFIX_PADDING_CNT;i++) { for (i = 0; i < GC_OBJECT_PREFIX_PADDING_CNT; i++) {
prefix->padding[i] = GC_OBJECT_PADDING_VALUE; prefix->padding[i] = GC_OBJECT_PADDING_VALUE;
} }
for(i = 0;i < GC_OBJECT_SUFFIX_PADDING_CNT;i++) { for (i = 0; i < GC_OBJECT_SUFFIX_PADDING_CNT; i++) {
suffix->padding[i] = GC_OBJECT_PADDING_VALUE; suffix->padding[i] = GC_OBJECT_PADDING_VALUE;
} }
} }
@ -61,17 +62,17 @@ hmu_verify(void *vheap, hmu_t *hmu)
prefix = (gc_object_prefix_t *)(hmu + 1); prefix = (gc_object_prefix_t *)(hmu + 1);
size = prefix->size; size = prefix->size;
suffix = (gc_object_suffix_t *)((gc_uint8*)hmu + size - OBJ_SUFFIX_SIZE); suffix = (gc_object_suffix_t *)((gc_uint8 *)hmu + size - OBJ_SUFFIX_SIZE);
if (ut == HMU_VO || ut == HMU_JO) { if (ut == HMU_VO || ut == HMU_JO) {
/* check padding*/ /* check padding*/
for (i = 0;i < GC_OBJECT_PREFIX_PADDING_CNT;i++) { for (i = 0; i < GC_OBJECT_PREFIX_PADDING_CNT; i++) {
if (prefix->padding[i] != GC_OBJECT_PADDING_VALUE) { if (prefix->padding[i] != GC_OBJECT_PADDING_VALUE) {
is_padding_ok = 0; is_padding_ok = 0;
break; break;
} }
} }
for (i = 0;i < GC_OBJECT_SUFFIX_PADDING_CNT;i++) { for (i = 0; i < GC_OBJECT_SUFFIX_PADDING_CNT; i++) {
if (suffix->padding[i] != GC_OBJECT_PADDING_VALUE) { if (suffix->padding[i] != GC_OBJECT_PADDING_VALUE) {
is_padding_ok = 0; is_padding_ok = 0;
break; break;
@ -88,4 +89,3 @@ hmu_verify(void *vheap, hmu_t *hmu)
} }
#endif /* end of BH_ENABLE_GC_VERIFY */ #endif /* end of BH_ENABLE_GC_VERIFY */

View File

@ -22,7 +22,7 @@ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
/* init all data structures*/ /* init all data structures*/
heap->current_size = heap_max_size; heap->current_size = heap_max_size;
heap->base_addr = (gc_uint8*)base_addr; heap->base_addr = (gc_uint8 *)base_addr;
heap->heap_id = (gc_handle_t)heap; heap->heap_id = (gc_handle_t)heap;
heap->total_free_size = heap->current_size; heap->total_free_size = heap->current_size;
@ -34,7 +34,7 @@ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
hmu_set_ut(&root->hmu_header, HMU_FC); hmu_set_ut(&root->hmu_header, HMU_FC);
hmu_set_size(&root->hmu_header, sizeof *root); hmu_set_size(&root->hmu_header, sizeof *root);
q = (hmu_tree_node_t *) heap->base_addr; q = (hmu_tree_node_t *)heap->base_addr;
memset(q, 0, sizeof *q); memset(q, 0, sizeof *q);
hmu_set_ut(&q->hmu_header, HMU_FC); hmu_set_ut(&q->hmu_header, HMU_FC);
hmu_set_size(&q->hmu_header, heap->current_size); hmu_set_size(&q->hmu_header, heap->current_size);
@ -53,18 +53,19 @@ gc_handle_t
gc_init_with_pool(char *buf, gc_size_t buf_size) gc_init_with_pool(char *buf, gc_size_t buf_size)
{ {
char *buf_end = buf + buf_size; char *buf_end = buf + buf_size;
char *buf_aligned = (char*)(((uintptr_t) buf + 7) & (uintptr_t)~7); char *buf_aligned = (char *)(((uintptr_t)buf + 7) & (uintptr_t)~7);
char *base_addr = buf_aligned + sizeof(gc_heap_t); char *base_addr = buf_aligned + sizeof(gc_heap_t);
gc_heap_t *heap = (gc_heap_t*)buf_aligned; gc_heap_t *heap = (gc_heap_t *)buf_aligned;
gc_size_t heap_max_size; gc_size_t heap_max_size;
if (buf_size < APP_HEAP_SIZE_MIN) { if (buf_size < APP_HEAP_SIZE_MIN) {
os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", buf_size,
buf_size, APP_HEAP_SIZE_MIN); APP_HEAP_SIZE_MIN);
return NULL; return NULL;
} }
base_addr = (char*) (((uintptr_t) base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING; base_addr =
(char *)(((uintptr_t)base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7; heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
#if WASM_ENABLE_MEMORY_TRACING != 0 #if WASM_ENABLE_MEMORY_TRACING != 0
@ -81,7 +82,7 @@ gc_handle_t
gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size, gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
char *pool_buf, gc_size_t pool_buf_size) char *pool_buf, gc_size_t pool_buf_size)
{ {
gc_heap_t *heap = (gc_heap_t*)struct_buf; gc_heap_t *heap = (gc_heap_t *)struct_buf;
char *base_addr = pool_buf + GC_HEAD_PADDING; char *base_addr = pool_buf + GC_HEAD_PADDING;
char *pool_buf_end = pool_buf + pool_buf_size; char *pool_buf_end = pool_buf + pool_buf_size;
gc_size_t heap_max_size; gc_size_t heap_max_size;
@ -103,8 +104,8 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
} }
if (pool_buf_size < APP_HEAP_SIZE_MIN) { if (pool_buf_size < APP_HEAP_SIZE_MIN) {
os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", pool_buf_size,
pool_buf_size, APP_HEAP_SIZE_MIN); APP_HEAP_SIZE_MIN);
return NULL; return NULL;
} }
@ -115,8 +116,7 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
struct_buf_size + pool_buf_size); struct_buf_size + pool_buf_size);
os_printf(" heap struct size: %u\n", sizeof(gc_heap_t)); os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
os_printf(" actual heap size: %u\n", heap_max_size); os_printf(" actual heap size: %u\n", heap_max_size);
os_printf(" padding bytes: %u\n", os_printf(" padding bytes: %u\n", pool_buf_size - heap_max_size);
pool_buf_size - heap_max_size);
#endif #endif
return gc_init_internal(heap, base_addr, heap_max_size); return gc_init_internal(heap, base_addr, heap_max_size);
} }
@ -124,16 +124,17 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
int int
gc_destroy_with_pool(gc_handle_t handle) gc_destroy_with_pool(gc_handle_t handle)
{ {
gc_heap_t *heap = (gc_heap_t *) handle; gc_heap_t *heap = (gc_heap_t *)handle;
#if BH_ENABLE_GC_VERIFY != 0 #if BH_ENABLE_GC_VERIFY != 0
hmu_t *cur = (hmu_t*)heap->base_addr; hmu_t *cur = (hmu_t *)heap->base_addr;
hmu_t *end = (hmu_t*)((char*)heap->base_addr + heap->current_size); hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
if (!heap->is_heap_corrupted if (!heap->is_heap_corrupted
&& (hmu_t*)((char *)cur + hmu_get_size(cur)) != end) { && (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
os_printf("Memory leak detected:\n"); os_printf("Memory leak detected:\n");
gci_dump(heap); gci_dump(heap);
#if WASM_ENABLE_SPEC_TEST != 0 #if WASM_ENABLE_SPEC_TEST != 0
while (1); while (1) {
}
#endif #endif
} }
#endif #endif
@ -157,13 +158,12 @@ adjust_ptr(uint8 **p_ptr, intptr_t offset)
} }
int int
gc_migrate(gc_handle_t handle, gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size)
char *pool_buf_new, gc_size_t pool_buf_size)
{ {
gc_heap_t *heap = (gc_heap_t *)handle; gc_heap_t *heap = (gc_heap_t *)handle;
char *base_addr_new = pool_buf_new + GC_HEAD_PADDING; char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
char *pool_buf_end = pool_buf_new + pool_buf_size; char *pool_buf_end = pool_buf_new + pool_buf_size;
intptr_t offset = (uint8*)base_addr_new - (uint8*)heap->base_addr; intptr_t offset = (uint8 *)base_addr_new - (uint8 *)heap->base_addr;
hmu_t *cur = NULL, *end = NULL; hmu_t *cur = NULL, *end = NULL;
hmu_tree_node_t *tree_node; hmu_tree_node_t *tree_node;
gc_size_t heap_max_size, size; gc_size_t heap_max_size, size;
@ -175,8 +175,7 @@ gc_migrate(gc_handle_t handle,
heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7; heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7;
if (pool_buf_end < base_addr_new if (pool_buf_end < base_addr_new || heap_max_size < heap->current_size) {
|| heap_max_size < heap->current_size) {
os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n"); os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n");
return GC_ERROR; return GC_ERROR;
} }
@ -184,13 +183,13 @@ gc_migrate(gc_handle_t handle,
if (offset == 0) if (offset == 0)
return 0; return 0;
heap->base_addr = (uint8*)base_addr_new; heap->base_addr = (uint8 *)base_addr_new;
adjust_ptr((uint8**)&heap->kfc_tree_root.left, offset); adjust_ptr((uint8 **)&heap->kfc_tree_root.left, offset);
adjust_ptr((uint8**)&heap->kfc_tree_root.right, offset); adjust_ptr((uint8 **)&heap->kfc_tree_root.right, offset);
adjust_ptr((uint8**)&heap->kfc_tree_root.parent, offset); adjust_ptr((uint8 **)&heap->kfc_tree_root.parent, offset);
cur = (hmu_t*)heap->base_addr; cur = (hmu_t *)heap->base_addr;
end = (hmu_t*)((char*)heap->base_addr + heap->current_size); end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
while (cur < end) { while (cur < end) {
size = hmu_get_size(cur); size = hmu_get_size(cur);
@ -198,14 +197,14 @@ gc_migrate(gc_handle_t handle,
if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) { if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) {
tree_node = (hmu_tree_node_t *)cur; tree_node = (hmu_tree_node_t *)cur;
adjust_ptr((uint8**)&tree_node->left, offset); adjust_ptr((uint8 **)&tree_node->left, offset);
adjust_ptr((uint8**)&tree_node->right, offset); adjust_ptr((uint8 **)&tree_node->right, offset);
if (tree_node->parent != &heap->kfc_tree_root) if (tree_node->parent != &heap->kfc_tree_root)
/* The root node belongs to heap structure, /* The root node belongs to heap structure,
it is fixed part and isn't changed. */ it is fixed part and isn't changed. */
adjust_ptr((uint8**)&tree_node->parent, offset); adjust_ptr((uint8 **)&tree_node->parent, offset);
} }
cur = (hmu_t*)((char *)cur + size); cur = (hmu_t *)((char *)cur + size);
} }
bh_assert(cur == end); bh_assert(cur == end);
@ -229,35 +228,34 @@ gci_verify_heap(gc_heap_t *heap)
bh_assert(heap && gci_is_heap_valid(heap)); bh_assert(heap && gci_is_heap_valid(heap));
cur = (hmu_t *)heap->base_addr; cur = (hmu_t *)heap->base_addr;
end = (hmu_t *)(heap->base_addr + heap->current_size); end = (hmu_t *)(heap->base_addr + heap->current_size);
while(cur < end) { while (cur < end) {
hmu_verify(heap, cur); hmu_verify(heap, cur);
cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur)); cur = (hmu_t *)((gc_uint8 *)cur + hmu_get_size(cur));
} }
bh_assert(cur == end); bh_assert(cur == end);
} }
#endif #endif
void * void *
gc_heap_stats(void *heap_arg, uint32* stats, int size) gc_heap_stats(void *heap_arg, uint32 *stats, int size)
{ {
int i; int i;
gc_heap_t *heap = (gc_heap_t *) heap_arg; gc_heap_t *heap = (gc_heap_t *)heap_arg;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
switch (i) { switch (i) {
case GC_STAT_TOTAL: case GC_STAT_TOTAL:
stats[i] = heap->current_size; stats[i] = heap->current_size;
break; break;
case GC_STAT_FREE: case GC_STAT_FREE:
stats[i] = heap->total_free_size; stats[i] = heap->total_free_size;
break; break;
case GC_STAT_HIGHMARK: case GC_STAT_HIGHMARK:
stats[i] = heap->highmark_size; stats[i] = heap->highmark_size;
break; break;
default: default:
break; break;
} }
} }
return heap; return heap;
} }

View File

@ -9,9 +9,10 @@
#include "ems/ems_gc.h" #include "ems/ems_gc.h"
mem_allocator_t mem_allocator_create(void *mem, uint32_t size) mem_allocator_t
mem_allocator_create(void *mem, uint32_t size)
{ {
return gc_init_with_pool((char *) mem, size); return gc_init_with_pool((char *)mem, size);
} }
mem_allocator_t mem_allocator_t
@ -20,15 +21,14 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
void *pool_buf, void *pool_buf,
uint32_t pool_buf_size) uint32_t pool_buf_size)
{ {
return gc_init_with_struct_and_pool((char *)struct_buf, return gc_init_with_struct_and_pool((char *)struct_buf, struct_buf_size,
struct_buf_size, pool_buf, pool_buf_size);
pool_buf,
pool_buf_size);
} }
void mem_allocator_destroy(mem_allocator_t allocator) void
mem_allocator_destroy(mem_allocator_t allocator)
{ {
gc_destroy_with_pool((gc_handle_t) allocator); gc_destroy_with_pool((gc_handle_t)allocator);
} }
uint32 uint32
@ -40,33 +40,33 @@ mem_allocator_get_heap_struct_size()
void * void *
mem_allocator_malloc(mem_allocator_t allocator, uint32_t size) mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
{ {
return gc_alloc_vo((gc_handle_t) allocator, size); return gc_alloc_vo((gc_handle_t)allocator, size);
} }
void * void *
mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size) mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
{ {
return gc_realloc_vo((gc_handle_t) allocator, ptr, size); return gc_realloc_vo((gc_handle_t)allocator, ptr, size);
} }
void mem_allocator_free(mem_allocator_t allocator, void *ptr) void
mem_allocator_free(mem_allocator_t allocator, void *ptr)
{ {
if (ptr) if (ptr)
gc_free_vo((gc_handle_t) allocator, ptr); gc_free_vo((gc_handle_t)allocator, ptr);
} }
int int
mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
char *pool_buf_new, uint32 pool_buf_size) uint32 pool_buf_size)
{ {
return gc_migrate((gc_handle_t) allocator, return gc_migrate((gc_handle_t)allocator, pool_buf_new, pool_buf_size);
pool_buf_new, pool_buf_size);
} }
bool bool
mem_allocator_is_heap_corrupted(mem_allocator_t allocator) mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
{ {
return gc_is_heap_corrupted((gc_handle_t) allocator); return gc_is_heap_corrupted((gc_handle_t)allocator);
} }
#else /* else of DEFAULT_MEM_ALLOCATOR */ #else /* else of DEFAULT_MEM_ALLOCATOR */
@ -76,23 +76,23 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
typedef struct mem_allocator_tlsf { typedef struct mem_allocator_tlsf {
tlsf_t tlsf; tlsf_t tlsf;
korp_mutex lock; korp_mutex lock;
}mem_allocator_tlsf; } mem_allocator_tlsf;
mem_allocator_t mem_allocator_t
mem_allocator_create(void *mem, uint32_t size) mem_allocator_create(void *mem, uint32_t size)
{ {
mem_allocator_tlsf *allocator_tlsf; mem_allocator_tlsf *allocator_tlsf;
tlsf_t tlsf; tlsf_t tlsf;
char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3); char *mem_aligned = (char *)(((uintptr_t)mem + 3) & ~3);
if (size < 1024) { if (size < 1024) {
printf("Create mem allocator failed: pool size must be " printf("Create mem allocator failed: pool size must be "
"at least 1024 bytes.\n"); "at least 1024 bytes.\n");
return NULL; return NULL;
} }
size -= mem_aligned - (char*)mem; size -= mem_aligned - (char *)mem;
mem = (void*)mem_aligned; mem = (void *)mem_aligned;
tlsf = tlsf_create_with_pool(mem, size); tlsf = tlsf_create_with_pool(mem, size);
if (!tlsf) { if (!tlsf) {
@ -174,12 +174,10 @@ mem_allocator_free(mem_allocator_t allocator, void *ptr)
} }
int int
mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_t allocator_old)
mem_allocator_t allocator_old)
{ {
return tlsf_migrate((mem_allocator_tlsf *) allocator, return tlsf_migrate((mem_allocator_tlsf *)allocator,
(mem_allocator_tlsf *) allocator_old); (mem_allocator_tlsf *)allocator_old);
} }
#endif /* end of DEFAULT_MEM_ALLOCATOR */ #endif /* end of DEFAULT_MEM_ALLOCATOR */

View File

@ -39,8 +39,8 @@ void
mem_allocator_free(mem_allocator_t allocator, void *ptr); mem_allocator_free(mem_allocator_t allocator, void *ptr);
int int
mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
char *pool_buf_new, uint32 pool_buf_size); uint32 pool_buf_size);
bool bool
mem_allocator_is_heap_corrupted(mem_allocator_t allocator); mem_allocator_is_heap_corrupted(mem_allocator_t allocator);
@ -50,4 +50,3 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator);
#endif #endif
#endif /* #ifndef __MEM_ALLOC_H */ #endif /* #ifndef __MEM_ALLOC_H */

View File

@ -38,8 +38,7 @@ os_realloc(void *ptr, unsigned size)
void void
os_free(void *ptr) os_free(void *ptr)
{ {}
}
void * void *
os_mmap(void *hint, size_t size, int prot, int flags) os_mmap(void *hint, size_t size, int prot, int flags)
@ -63,5 +62,4 @@ os_mprotect(void *addr, size_t size, int prot)
void void
os_dcache_flush() os_dcache_flush()
{ {}
}

View File

@ -6,14 +6,16 @@
#include "platform_api_vmcore.h" #include "platform_api_vmcore.h"
#include "platform_api_extension.h" #include "platform_api_extension.h"
/* clang-format off */
#define bh_assert(v) do { \ #define bh_assert(v) do { \
if (!(v)) { \ if (!(v)) { \
printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ printf("\nASSERTION FAILED: %s, at %s, line %d\n", \
#v, __FILE__, __LINE__); \ #v, __FILE__, __LINE__); \
aos_reboot(); \ aos_reboot(); \
while (1); \ while (1); \
} \ } \
} while (0) } while (0)
/* clang-format on */
struct os_thread_data; struct os_thread_data;
typedef struct os_thread_wait_node { typedef struct os_thread_wait_node {
@ -138,8 +140,8 @@ os_thread_wrapper(void *arg)
} }
int int
os_thread_create(korp_tid *p_tid, thread_start_routine_t start, os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
void *arg, unsigned int stack_size) unsigned int stack_size)
{ {
return os_thread_create_with_prio(p_tid, start, arg, stack_size, return os_thread_create_with_prio(p_tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
@ -170,13 +172,12 @@ os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
if (aos_mutex_new(&thread_data->wait_list_lock)) if (aos_mutex_new(&thread_data->wait_list_lock))
goto fail2; goto fail2;
snprintf(thread_name, sizeof(thread_name), "%s%d", snprintf(thread_name, sizeof(thread_name), "%s%d", "wasm-thread-",
"wasm-thread-", ++thread_name_index); ++thread_name_index);
/* Create the thread */ /* Create the thread */
if (aos_task_new_ext((aos_task_t*)thread_data, thread_name, if (aos_task_new_ext((aos_task_t *)thread_data, thread_name,
os_thread_wrapper, thread_data, os_thread_wrapper, thread_data, stack_size, prio))
stack_size, prio))
goto fail3; goto fail3;
aos_msleep(10); aos_msleep(10);
@ -199,7 +200,7 @@ os_self_thread()
} }
int int
os_thread_join (korp_tid thread, void **value_ptr) os_thread_join(korp_tid thread, void **value_ptr)
{ {
(void)value_ptr; (void)value_ptr;
os_thread_data *thread_data, *curr_thread_data; os_thread_data *thread_data, *curr_thread_data;
@ -209,7 +210,7 @@ os_thread_join (korp_tid thread, void **value_ptr)
curr_thread_data->wait_node.next = NULL; curr_thread_data->wait_node.next = NULL;
/* Get thread data */ /* Get thread data */
thread_data = (os_thread_data*)thread; thread_data = (os_thread_data *)thread;
aos_mutex_lock(&thread_data->wait_list_lock, AOS_WAIT_FOREVER); aos_mutex_lock(&thread_data->wait_list_lock, AOS_WAIT_FOREVER);
if (!thread_data->thread_wait_list) if (!thread_data->thread_wait_list)
@ -272,8 +273,8 @@ os_cond_destroy(korp_cond *cond)
} }
static int static int
os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
bool timed, uint32 mills) uint32 mills)
{ {
os_thread_wait_node *node = &thread_data_current()->wait_node; os_thread_wait_node *node = &thread_data_current()->wait_node;
@ -352,9 +353,9 @@ os_cond_signal(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
uint8 *os_thread_get_stack_boundary() uint8 *
os_thread_get_stack_boundary()
{ {
/* TODO: get alios stack boundary */ /* TODO: get alios stack boundary */
return NULL; return NULL;
} }

View File

@ -10,4 +10,3 @@ os_time_get_boot_microsecond()
{ {
return (uint64)aos_now_ms() * 1000; return (uint64)aos_now_ms() * 1000;
} }

View File

@ -34,13 +34,14 @@ typedef aos_mutex_t korp_mutex;
struct os_thread_wait_node; struct os_thread_wait_node;
typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct os_thread_wait_node *os_thread_wait_list;
typedef struct korp_cond { typedef struct korp_cond {
aos_mutex_t wait_list_lock; aos_mutex_t wait_list_lock;
os_thread_wait_list thread_wait_list; os_thread_wait_list thread_wait_list;
} korp_cond; } korp_cond;
#define os_printf printf #define os_printf printf
#define os_vprintf vprintf #define os_vprintf vprintf
/* clang-format off */
/* math functions which are not provided by os*/ /* math functions which are not provided by os*/
double sqrt(double x); double sqrt(double x);
double floor(double x); double floor(double x);
@ -58,6 +59,6 @@ float rintf(float x);
float truncf(float x); float truncf(float x);
int signbit(double x); int signbit(double x);
int isnan(double x); int isnan(double x);
/* clang-format on */
#endif /* end of _BH_PLATFORM_H */ #endif /* end of _BH_PLATFORM_H */

View File

@ -19,10 +19,10 @@ bh_platform_init()
void void
bh_platform_destroy() bh_platform_destroy()
{ {}
}
int os_printf(const char *fmt, ...) int
os_printf(const char *fmt, ...)
{ {
int ret; int ret;
va_list ap; va_list ap;
@ -34,14 +34,16 @@ int os_printf(const char *fmt, ...)
return ret; return ret;
} }
int os_vprintf(const char *fmt, va_list ap) int
os_vprintf(const char *fmt, va_list ap)
{ {
return __android_log_vprint(ANDROID_LOG_INFO, "wasm_runtime::", fmt, ap); return __android_log_vprint(ANDROID_LOG_INFO, "wasm_runtime::", fmt, ap);
} }
#if __ANDROID_API__ < 19 #if __ANDROID_API__ < 19
int futimens(int __dir_fd, const struct timespec __times[2]) int
futimens(int __dir_fd, const struct timespec __times[2])
{ {
API_NOT_SUPPORT_ERROR(futimens, 19); API_NOT_SUPPORT_ERROR(futimens, 19);
return -1; return -1;
@ -51,32 +53,37 @@ int futimens(int __dir_fd, const struct timespec __times[2])
#if __ANDROID_API__ < 21 #if __ANDROID_API__ < 21
int posix_fallocate(int __fd, off_t __offset, off_t __length) int
posix_fallocate(int __fd, off_t __offset, off_t __length)
{ {
API_NOT_SUPPORT_ERROR(posix_fallocate, 21); API_NOT_SUPPORT_ERROR(posix_fallocate, 21);
return -1; return -1;
} }
int posix_fadvise(int fd, off_t offset, off_t len, int advice) int
posix_fadvise(int fd, off_t offset, off_t len, int advice)
{ {
API_NOT_SUPPORT_ERROR(posix_fadvise, 21); API_NOT_SUPPORT_ERROR(posix_fadvise, 21);
return -1; return -1;
} }
int linkat(int __old_dir_fd, const char *__old_path, int
int __new_dir_fd, const char *__new_path, int __flags) linkat(int __old_dir_fd, const char *__old_path, int __new_dir_fd,
const char *__new_path, int __flags)
{ {
API_NOT_SUPPORT_ERROR(linkat, 21); API_NOT_SUPPORT_ERROR(linkat, 21);
return -1; return -1;
} }
int symlinkat(const char *__old_path, int __new_dir_fd, const char *__new_path) int
symlinkat(const char *__old_path, int __new_dir_fd, const char *__new_path)
{ {
API_NOT_SUPPORT_ERROR(symlinkat, 21); API_NOT_SUPPORT_ERROR(symlinkat, 21);
return -1; return -1;
} }
ssize_t readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_size) ssize_t
readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_size)
{ {
API_NOT_SUPPORT_ERROR(readlinkat, 21); API_NOT_SUPPORT_ERROR(readlinkat, 21);
return -1; return -1;
@ -86,13 +93,15 @@ ssize_t readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_s
#if __ANDROID_API__ < 23 #if __ANDROID_API__ < 23
long telldir(DIR *__dir) long
telldir(DIR *__dir)
{ {
API_NOT_SUPPORT_ERROR(telldir, 23); API_NOT_SUPPORT_ERROR(telldir, 23);
return -1; return -1;
} }
void seekdir(DIR *__dir, long __location) void
seekdir(DIR *__dir, long __location)
{ {
API_NOT_SUPPORT_ERROR(seekdir, 23); API_NOT_SUPPORT_ERROR(seekdir, 23);
} }
@ -101,13 +110,15 @@ void seekdir(DIR *__dir, long __location)
#if __ANDROID_API__ < 24 #if __ANDROID_API__ < 24
ssize_t preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset) ssize_t
preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset)
{ {
API_NOT_SUPPORT_ERROR(preadv, 24); API_NOT_SUPPORT_ERROR(preadv, 24);
return -1; return -1;
} }
ssize_t pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset) ssize_t
pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset)
{ {
API_NOT_SUPPORT_ERROR(pwritev, 24); API_NOT_SUPPORT_ERROR(pwritev, 24);
return -1; return -1;

View File

@ -59,10 +59,8 @@ typedef pthread_t korp_thread;
#define os_thread_local_attribute __thread #define os_thread_local_attribute __thread
#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) \ #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_AARCH64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64) || defined(BUILD_TARGET_RISCV64_LP64)
#include <setjmp.h> #include <setjmp.h>
@ -79,15 +77,20 @@ typedef jmp_buf korp_jmpbuf;
typedef void (*os_signal_handler)(void *sig_addr); typedef void (*os_signal_handler)(void *sig_addr);
int os_thread_signal_init(os_signal_handler handler); int
os_thread_signal_init(os_signal_handler handler);
void os_thread_signal_destroy(); void
os_thread_signal_destroy();
bool os_thread_signal_inited(); bool
os_thread_signal_inited();
void os_signal_unmask(); void
os_signal_unmask();
void os_sigreturn(); void
os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
@ -95,38 +98,48 @@ typedef long int __syscall_slong_t;
#if __ANDROID_API__ < 19 #if __ANDROID_API__ < 19
int futimens(int __dir_fd, const struct timespec __times[2]); int
futimens(int __dir_fd, const struct timespec __times[2]);
#endif #endif
#if __ANDROID_API__ < 21 #if __ANDROID_API__ < 21
int posix_fallocate(int __fd, off_t __offset, off_t __length); int
posix_fallocate(int __fd, off_t __offset, off_t __length);
int posix_fadvise(int fd, off_t offset, off_t len, int advice); int
posix_fadvise(int fd, off_t offset, off_t len, int advice);
int linkat(int __old_dir_fd, const char *__old_path, int
int __new_dir_fd, const char *__new_path, int __flags); linkat(int __old_dir_fd, const char *__old_path, int __new_dir_fd,
const char *__new_path, int __flags);
int symlinkat(const char *__old_path, int __new_dir_fd, const char *__new_path); int
symlinkat(const char *__old_path, int __new_dir_fd, const char *__new_path);
ssize_t readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_size); ssize_t
readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_size);
#endif #endif
#if __ANDROID_API__ < 23 #if __ANDROID_API__ < 23
long telldir(DIR *__dir); long
telldir(DIR *__dir);
void seekdir(DIR *__dir, long __location); void
seekdir(DIR *__dir, long __location);
#endif #endif
#if __ANDROID_API__ < 24 #if __ANDROID_API__ < 24
ssize_t preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset); ssize_t
preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset);
ssize_t pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset); ssize_t
pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset);
#endif #endif
@ -135,4 +148,3 @@ ssize_t pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset
#endif #endif
#endif /* end of _PLATFORM_INTERNAL_H */ #endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -5,7 +5,6 @@
#include "platform_api_vmcore.h" #include "platform_api_vmcore.h"
void * void *
os_malloc(unsigned size) os_malloc(unsigned size)
{ {
@ -20,6 +19,4 @@ os_realloc(void *ptr, unsigned size)
void void
os_free(void *ptr) os_free(void *ptr)
{ {}
}

View File

@ -6,6 +6,7 @@
#include "platform_api_vmcore.h" #include "platform_api_vmcore.h"
#include "platform_api_extension.h" #include "platform_api_extension.h"
/* clang-format off */
#define bh_assert(v) do { \ #define bh_assert(v) do { \
if (!(v)) { \ if (!(v)) { \
int _count = 1; \ int _count = 1; \
@ -15,7 +16,8 @@
os_printf("%d\n", _count / (_count - 1)); \ os_printf("%d\n", _count / (_count - 1)); \
while (1); \ while (1); \
} \ } \
} while (0) } while (0)
/* clang-format on */
struct os_thread_data; struct os_thread_data;
typedef struct os_thread_wait_node { typedef struct os_thread_wait_node {
@ -56,7 +58,8 @@ static os_thread_data supervisor_thread_data;
/* Thread name index */ /* Thread name index */
static int thread_name_index; static int thread_name_index;
static void thread_data_list_add(os_thread_data *thread_data) static void
thread_data_list_add(os_thread_data *thread_data)
{ {
xSemaphoreTake(thread_data_lock, portMAX_DELAY); xSemaphoreTake(thread_data_lock, portMAX_DELAY);
if (!thread_data_list) if (!thread_data_list)
@ -79,7 +82,8 @@ static void thread_data_list_add(os_thread_data *thread_data)
xSemaphoreGive(thread_data_lock); xSemaphoreGive(thread_data_lock);
} }
static void thread_data_list_remove(os_thread_data *thread_data) static void
thread_data_list_remove(os_thread_data *thread_data)
{ {
xSemaphoreTake(thread_data_lock, portMAX_DELAY); xSemaphoreTake(thread_data_lock, portMAX_DELAY);
if (thread_data_list) { if (thread_data_list) {
@ -204,15 +208,17 @@ os_thread_wrapper(void *arg)
vTaskDelete(NULL); vTaskDelete(NULL);
} }
int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, int
unsigned int stack_size) os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{ {
return os_thread_create_with_prio(p_tid, start, arg, stack_size, return os_thread_create_with_prio(p_tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
} }
int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, int
void *arg, unsigned int stack_size, int prio) os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{ {
os_thread_data *thread_data; os_thread_data *thread_data;
char thread_name[32]; char thread_name[32];
@ -235,13 +241,13 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
if (!(thread_data->wait_list_lock = xSemaphoreCreateMutex())) if (!(thread_data->wait_list_lock = xSemaphoreCreateMutex()))
goto fail2; goto fail2;
snprintf(thread_name, sizeof(thread_name), "%s%d", snprintf(thread_name, sizeof(thread_name), "%s%d", "wasm-thread-",
"wasm-thread-", ++thread_name_index); ++thread_name_index);
/* Create the thread */ /* Create the thread */
if (pdPASS != xTaskCreate(os_thread_wrapper, thread_name, if (pdPASS
stack_size / 4, thread_data, != xTaskCreate(os_thread_wrapper, thread_name, stack_size / 4,
prio, &thread_data->handle)) thread_data, prio, &thread_data->handle))
goto fail3; goto fail3;
thread_data_list_add(thread_data); thread_data_list_add(thread_data);
@ -257,12 +263,14 @@ fail1:
return BHT_ERROR; return BHT_ERROR;
} }
korp_tid os_self_thread() korp_tid
os_self_thread()
{ {
return xTaskGetCurrentTaskHandle(); return xTaskGetCurrentTaskHandle();
} }
int os_thread_join(korp_tid thread, void **value_ptr) int
os_thread_join(korp_tid thread, void **value_ptr)
{ {
os_thread_data *thread_data, *curr_thread_data; os_thread_data *thread_data, *curr_thread_data;
TaskHandle_t handle = thread; TaskHandle_t handle = thread;
@ -293,7 +301,8 @@ int os_thread_join(korp_tid thread, void **value_ptr)
return BHT_OK; return BHT_OK;
} }
int os_mutex_init(korp_mutex *mutex) int
os_mutex_init(korp_mutex *mutex)
{ {
SemaphoreHandle_t semaphore; SemaphoreHandle_t semaphore;
@ -304,7 +313,8 @@ int os_mutex_init(korp_mutex *mutex)
return BHT_OK; return BHT_OK;
} }
int os_recursive_mutex_init(korp_mutex *mutex) int
os_recursive_mutex_init(korp_mutex *mutex)
{ {
SemaphoreHandle_t semaphore; SemaphoreHandle_t semaphore;
@ -315,13 +325,15 @@ int os_recursive_mutex_init(korp_mutex *mutex)
return BHT_OK; return BHT_OK;
} }
int os_mutex_destroy(korp_mutex *mutex) int
os_mutex_destroy(korp_mutex *mutex)
{ {
vSemaphoreDelete(mutex->sem); vSemaphoreDelete(mutex->sem);
return BHT_OK; return BHT_OK;
} }
int os_mutex_lock(korp_mutex *mutex) int
os_mutex_lock(korp_mutex *mutex)
{ {
int ret = -1; int ret = -1;
@ -332,7 +344,8 @@ int os_mutex_lock(korp_mutex *mutex)
return ret == pdPASS ? BHT_OK : BHT_ERROR; return ret == pdPASS ? BHT_OK : BHT_ERROR;
} }
int os_mutex_unlock(korp_mutex *mutex) int
os_mutex_unlock(korp_mutex *mutex)
{ {
int ret = -1; int ret = -1;
@ -343,7 +356,8 @@ int os_mutex_unlock(korp_mutex *mutex)
return ret == pdPASS ? BHT_OK : BHT_ERROR; return ret == pdPASS ? BHT_OK : BHT_ERROR;
} }
int os_cond_init(korp_cond *cond) int
os_cond_init(korp_cond *cond)
{ {
if (!(cond->wait_list_lock = xSemaphoreCreateMutex())) if (!(cond->wait_list_lock = xSemaphoreCreateMutex()))
return BHT_ERROR; return BHT_ERROR;
@ -352,15 +366,15 @@ int os_cond_init(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_cond_destroy(korp_cond *cond) int
os_cond_destroy(korp_cond *cond)
{ {
vSemaphoreDelete(cond->wait_list_lock); vSemaphoreDelete(cond->wait_list_lock);
return BHT_OK; return BHT_OK;
} }
static int static int
os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, int mills)
bool timed, int mills)
{ {
os_thread_wait_node *node = &thread_data_current()->wait_node; os_thread_wait_node *node = &thread_data_current()->wait_node;
@ -399,12 +413,14 @@ os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
return BHT_OK; return BHT_OK;
} }
int os_cond_wait(korp_cond *cond, korp_mutex *mutex) int
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
{ {
return os_cond_wait_internal(cond, mutex, false, 0); return os_cond_wait_internal(cond, mutex, false, 0);
} }
int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
{ {
if (useconds == BHT_WAIT_FOREVER) { if (useconds == BHT_WAIT_FOREVER) {
return os_cond_wait_internal(cond, mutex, false, 0); return os_cond_wait_internal(cond, mutex, false, 0);
@ -425,7 +441,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
} }
} }
int os_cond_signal(korp_cond *cond) int
os_cond_signal(korp_cond *cond)
{ {
/* Signal the head wait node of wait list */ /* Signal the head wait node of wait list */
xSemaphoreTake(cond->wait_list_lock, portMAX_DELAY); xSemaphoreTake(cond->wait_list_lock, portMAX_DELAY);
@ -435,4 +452,3 @@ int os_cond_signal(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }

View File

@ -11,4 +11,3 @@ os_time_get_boot_microsecond()
TickType_t ticks = xTaskGetTickCount(); TickType_t ticks = xTaskGetTickCount();
return (uint64)1000 * 1000 / configTICK_RATE_HZ * ticks; return (uint64)1000 * 1000 / configTICK_RATE_HZ * ticks;
} }

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,3 @@ os_free(void *ptr)
{ {
free(ptr); free(ptr);
} }

View File

@ -50,21 +50,19 @@ os_mmap(void *hint, size_t size, int prot, int flags)
* (mmap's first argument) to meet the requirement. * (mmap's first argument) to meet the requirement.
*/ */
if (!hint && !(flags & MMAP_MAP_FIXED) && (flags & MMAP_MAP_32BIT)) { if (!hint && !(flags & MMAP_MAP_FIXED) && (flags & MMAP_MAP_32BIT)) {
uint8 *stack_addr = (uint8*)&map_prot; uint8 *stack_addr = (uint8 *)&map_prot;
uint8 *text_addr = (uint8*)os_mmap; uint8 *text_addr = (uint8 *)os_mmap;
/* hint address begins with 1MB */ /* hint address begins with 1MB */
static uint8 *hint_addr = (uint8 *)(uintptr_t)BH_MB; static uint8 *hint_addr = (uint8 *)(uintptr_t)BH_MB;
if ((hint_addr - text_addr >= 0 if ((hint_addr - text_addr >= 0 && hint_addr - text_addr < 100 * BH_MB)
&& hint_addr - text_addr < 100 * BH_MB)
|| (text_addr - hint_addr >= 0 || (text_addr - hint_addr >= 0
&& text_addr - hint_addr < 100 * BH_MB)) { && text_addr - hint_addr < 100 * BH_MB)) {
/* hint address is possibly in text section, skip it */ /* hint address is possibly in text section, skip it */
hint_addr += 100 * BH_MB; hint_addr += 100 * BH_MB;
} }
if ((hint_addr - stack_addr >= 0 if ((hint_addr - stack_addr >= 0 && hint_addr - stack_addr < 8 * BH_MB)
&& hint_addr - stack_addr < 8 * BH_MB)
|| (stack_addr - hint_addr >= 0 || (stack_addr - hint_addr >= 0
&& stack_addr - hint_addr < 8 * BH_MB)) { && stack_addr - hint_addr < 8 * BH_MB)) {
/* hint address is possibly in native stack area, skip it */ /* hint address is possibly in native stack area, skip it */
@ -72,8 +70,7 @@ os_mmap(void *hint, size_t size, int prot, int flags)
} }
/* try 10 times, step with 1MB each time */ /* try 10 times, step with 1MB each time */
for (i = 0; for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
i++) { i++) {
addr = mmap(hint_addr, request_size, map_prot, map_flags, -1, 0); addr = mmap(hint_addr, request_size, map_prot, map_flags, -1, 0);
if (addr != MAP_FAILED) { if (addr != MAP_FAILED) {
@ -114,7 +111,7 @@ os_munmap(void *addr, size_t size)
if (addr) { if (addr) {
if (munmap(addr, request_size)) { if (munmap(addr, request_size)) {
os_printf("os_munmap error addr:%p, size:0x%"PRIx64", errno:%d\n", os_printf("os_munmap error addr:%p, size:0x%" PRIx64 ", errno:%d\n",
addr, request_size, errno); addr, request_size, errno);
} }
} }
@ -144,5 +141,4 @@ os_mprotect(void *addr, size_t size, int prot)
void void
os_dcache_flush(void) os_dcache_flush(void)
{ {}
}

View File

@ -22,7 +22,8 @@ typedef struct {
static os_thread_local_attribute os_signal_handler signal_handler; static os_thread_local_attribute os_signal_handler signal_handler;
#endif #endif
static void *os_thread_wrapper(void *arg) static void *
os_thread_wrapper(void *arg)
{ {
thread_wrapper_arg *targ = arg; thread_wrapper_arg *targ = arg;
thread_start_routine_t start_func = targ->start; thread_start_routine_t start_func = targ->start;
@ -44,8 +45,9 @@ static void *os_thread_wrapper(void *arg)
return NULL; return NULL;
} }
int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start, int
void *arg, unsigned int stack_size, int prio) os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{ {
pthread_attr_t tattr; pthread_attr_t tattr;
thread_wrapper_arg *targ; thread_wrapper_arg *targ;
@ -63,7 +65,7 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_ERROR; return BHT_ERROR;
} }
targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ)); targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
if (!targ) { if (!targ) {
pthread_attr_destroy(&tattr); pthread_attr_destroy(&tattr);
return BHT_ERROR; return BHT_ERROR;
@ -85,24 +87,28 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_OK; return BHT_OK;
} }
int os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg, int
unsigned int stack_size) os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{ {
return os_thread_create_with_prio(tid, start, arg, stack_size, return os_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
} }
korp_tid os_self_thread() korp_tid
os_self_thread()
{ {
return (korp_tid) pthread_self(); return (korp_tid)pthread_self();
} }
int os_mutex_init(korp_mutex *mutex) int
os_mutex_init(korp_mutex *mutex)
{ {
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR; return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
} }
int os_recursive_mutex_init(korp_mutex *mutex) int
os_recursive_mutex_init(korp_mutex *mutex)
{ {
int ret; int ret;
@ -120,7 +126,8 @@ int os_recursive_mutex_init(korp_mutex *mutex)
return ret == 0 ? BHT_OK : BHT_ERROR; return ret == 0 ? BHT_OK : BHT_ERROR;
} }
int os_mutex_destroy(korp_mutex *mutex) int
os_mutex_destroy(korp_mutex *mutex)
{ {
int ret; int ret;
@ -130,7 +137,8 @@ int os_mutex_destroy(korp_mutex *mutex)
return ret == 0 ? BHT_OK : BHT_ERROR; return ret == 0 ? BHT_OK : BHT_ERROR;
} }
int os_mutex_lock(korp_mutex *mutex) int
os_mutex_lock(korp_mutex *mutex)
{ {
int ret; int ret;
@ -140,7 +148,8 @@ int os_mutex_lock(korp_mutex *mutex)
return ret == 0 ? BHT_OK : BHT_ERROR; return ret == 0 ? BHT_OK : BHT_ERROR;
} }
int os_mutex_unlock(korp_mutex *mutex) int
os_mutex_unlock(korp_mutex *mutex)
{ {
int ret; int ret;
@ -150,7 +159,8 @@ int os_mutex_unlock(korp_mutex *mutex)
return ret == 0 ? BHT_OK : BHT_ERROR; return ret == 0 ? BHT_OK : BHT_ERROR;
} }
int os_cond_init(korp_cond *cond) int
os_cond_init(korp_cond *cond)
{ {
assert(cond); assert(cond);
@ -160,7 +170,8 @@ int os_cond_init(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_cond_destroy(korp_cond *cond) int
os_cond_destroy(korp_cond *cond)
{ {
assert(cond); assert(cond);
@ -170,7 +181,8 @@ int os_cond_destroy(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_cond_wait(korp_cond *cond, korp_mutex *mutex) int
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
{ {
assert(cond); assert(cond);
assert(mutex); assert(mutex);
@ -181,7 +193,8 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex)
return BHT_OK; return BHT_OK;
} }
static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec) static void
msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
{ {
struct timeval tv; struct timeval tv;
time_t tv_sec_new; time_t tv_sec_new;
@ -201,8 +214,7 @@ static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
} }
tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000); tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000);
if (tv.tv_usec * 1000 >= tv.tv_usec if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) {
&& tv_nsec_new >= tv.tv_usec * 1000) {
ts->tv_nsec = tv_nsec_new; ts->tv_nsec = tv_nsec_new;
} }
else { else {
@ -218,7 +230,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
} }
} }
int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
{ {
int ret; int ret;
struct timespec abstime; struct timespec abstime;
@ -236,7 +249,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
return ret; return ret;
} }
int os_cond_signal(korp_cond *cond) int
os_cond_signal(korp_cond *cond)
{ {
assert(cond); assert(cond);
@ -246,17 +260,20 @@ int os_cond_signal(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_thread_join(korp_tid thread, void **value_ptr) int
os_thread_join(korp_tid thread, void **value_ptr)
{ {
return pthread_join(thread, value_ptr); return pthread_join(thread, value_ptr);
} }
int os_thread_detach(korp_tid thread) int
os_thread_detach(korp_tid thread)
{ {
return pthread_detach(thread); return pthread_detach(thread);
} }
void os_thread_exit(void *retval) void
os_thread_exit(void *retval)
{ {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
os_thread_signal_destroy(); os_thread_signal_destroy();
@ -268,7 +285,8 @@ void os_thread_exit(void *retval)
static os_thread_local_attribute uint8 *thread_stack_boundary = NULL; static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
#endif #endif
uint8 *os_thread_get_stack_boundary() uint8 *
os_thread_get_stack_boundary()
{ {
pthread_t self; pthread_t self;
#ifdef __linux__ #ifdef __linux__
@ -286,15 +304,15 @@ uint8 *os_thread_get_stack_boundary()
page_size = getpagesize(); page_size = getpagesize();
self = pthread_self(); self = pthread_self();
max_stack_size = (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) max_stack_size =
& ~(page_size - 1); (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) & ~(page_size - 1);
if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT) if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT)
max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT; max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT;
#ifdef __linux__ #ifdef __linux__
if (pthread_getattr_np(self, &attr) == 0) { if (pthread_getattr_np(self, &attr) == 0) {
pthread_attr_getstack(&attr, (void**)&addr, &stack_size); pthread_attr_getstack(&attr, (void **)&addr, &stack_size);
pthread_attr_getguardsize(&attr, &guard_size); pthread_attr_getguardsize(&attr, &guard_size);
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
if (stack_size > max_stack_size) if (stack_size > max_stack_size)
@ -306,7 +324,7 @@ uint8 *os_thread_get_stack_boundary()
} }
(void)stack_size; (void)stack_size;
#elif defined(__APPLE__) || defined(__NuttX__) #elif defined(__APPLE__) || defined(__NuttX__)
if ((addr = (uint8*)pthread_get_stackaddr_np(self))) { if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
stack_size = pthread_get_stacksize_np(self); stack_size = pthread_get_stacksize_np(self);
if (stack_size > max_stack_size) if (stack_size > max_stack_size)
addr -= max_stack_size; addr -= max_stack_size;
@ -349,8 +367,7 @@ touch_pages(uint8 *stack_min_addr, uint32 page_size)
{ {
uint8 sum = 0; uint8 sum = 0;
while (1) { while (1) {
volatile uint8 *touch_addr = volatile uint8 *touch_addr = (volatile uint8 *)os_alloca(page_size / 2);
(volatile uint8*)os_alloca(page_size / 2);
if (touch_addr < stack_min_addr + page_size) { if (touch_addr < stack_min_addr + page_size) {
sum += *(stack_min_addr + page_size - 1); sum += *(stack_min_addr + page_size - 1);
break; break;
@ -378,7 +395,8 @@ init_stack_guard_pages()
(void)touch_pages(stack_min_addr, page_size); (void)touch_pages(stack_min_addr, page_size);
/* First time to call aot function, protect guard pages */ /* First time to call aot function, protect guard pages */
if (os_mprotect(stack_min_addr, page_size * guard_page_count, if (os_mprotect(stack_min_addr, page_size * guard_page_count,
MMAP_PROT_NONE) != 0) { MMAP_PROT_NONE)
!= 0) {
return false; return false;
} }
return true; return true;
@ -413,8 +431,7 @@ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext)
mask_signals(SIG_BLOCK); mask_signals(SIG_BLOCK);
if (signal_handler if (signal_handler && (sig_num == SIGSEGV || sig_num == SIGBUS)) {
&& (sig_num == SIGSEGV || sig_num == SIGBUS)) {
signal_handler(sig_addr); signal_handler(sig_addr);
} }
@ -427,8 +444,7 @@ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext)
os_printf("unhandled SIGBUS, si_addr: %p\n", sig_addr); os_printf("unhandled SIGBUS, si_addr: %p\n", sig_addr);
break; break;
default: default:
os_printf("unhandle signal %d, si_addr: %p\n", os_printf("unhandle signal %d, si_addr: %p\n", sig_num, sig_addr);
sig_num, sig_addr);
break; break;
} }
@ -452,8 +468,7 @@ os_thread_signal_init(os_signal_handler handler)
} }
/* Initialize memory for signal alternate stack of current thread */ /* Initialize memory for signal alternate stack of current thread */
if (!(map_addr = os_mmap(NULL, map_size, if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE,
MMAP_PROT_READ | MMAP_PROT_WRITE,
MMAP_MAP_NONE))) { MMAP_MAP_NONE))) {
os_printf("Failed to mmap memory for alternate stack\n"); os_printf("Failed to mmap memory for alternate stack\n");
goto fail1; goto fail1;
@ -533,7 +548,7 @@ void
os_sigreturn() os_sigreturn()
{ {
#if defined(__APPLE__) #if defined(__APPLE__)
#define UC_RESET_ALT_STACK 0x80000000 #define UC_RESET_ALT_STACK 0x80000000
extern int __sigreturn(void *, int); extern int __sigreturn(void *, int);
/* It's necessary to call __sigreturn to restore the sigaltstack state /* It's necessary to call __sigreturn to restore the sigaltstack state
@ -542,4 +557,3 @@ os_sigreturn()
#endif #endif
} }
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */ #endif /* end of OS_ENABLE_HW_BOUND_CHECK */

View File

@ -13,6 +13,5 @@ os_time_get_boot_microsecond()
return 0; return 0;
} }
return ((uint64) ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000; return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
} }

View File

@ -13,8 +13,7 @@ bh_platform_init()
void void
bh_platform_destroy() bh_platform_destroy()
{ {}
}
int int
os_printf(const char *format, ...) os_printf(const char *format, ...)
@ -42,4 +41,3 @@ os_vprintf(const char *format, va_list ap)
return BH_VPRINTF(format, ap); return BH_VPRINTF(format, ap);
#endif #endif
} }

View File

@ -60,10 +60,8 @@ typedef pthread_t korp_thread;
#define os_thread_local_attribute __thread #define os_thread_local_attribute __thread
#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) \ #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_AARCH64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64) || defined(BUILD_TARGET_RISCV64_LP64)
#include <setjmp.h> #include <setjmp.h>
@ -80,15 +78,20 @@ typedef jmp_buf korp_jmpbuf;
typedef void (*os_signal_handler)(void *sig_addr); typedef void (*os_signal_handler)(void *sig_addr);
int os_thread_signal_init(os_signal_handler handler); int
os_thread_signal_init(os_signal_handler handler);
void os_thread_signal_destroy(); void
os_thread_signal_destroy();
bool os_thread_signal_inited(); bool
os_thread_signal_inited();
void os_signal_unmask(); void
os_signal_unmask();
void os_sigreturn(); void
os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
@ -97,4 +100,3 @@ void os_sigreturn();
#endif #endif
#endif /* end of _PLATFORM_INTERNAL_H */ #endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -6,7 +6,6 @@
#include "platform_api_vmcore.h" #include "platform_api_vmcore.h"
#include "platform_api_extension.h" #include "platform_api_extension.h"
int errno = 0; int errno = 0;
int int
@ -27,7 +26,8 @@ bh_platform_destroy()
os_thread_sys_destroy(); os_thread_sys_destroy();
} }
int os_printf(const char *format, ...) int
os_printf(const char *format, ...)
{ {
int ret = 0; int ret = 0;
va_list ap; va_list ap;
@ -66,8 +66,7 @@ os_mprotect(void *addr, size_t size, int prot)
void void
os_dcache_flush() os_dcache_flush()
{ {}
}
int int
atoi(const char *nptr) atoi(const char *nptr)
@ -112,11 +111,10 @@ memmove(void *dest, const void *src, size_t n)
*d++ = *s++; *d++ = *s++;
} }
else { else {
const char *lasts = s + (n-1); const char *lasts = s + (n - 1);
char *lastd = d + (n-1); char *lastd = d + (n - 1);
while (n--) while (n--)
*lastd-- = *lasts--; *lastd-- = *lasts--;
} }
return dest; return dest;
} }

View File

@ -6,9 +6,9 @@
#include "platform_api_vmcore.h" #include "platform_api_vmcore.h"
#include "platform_api_extension.h" #include "platform_api_extension.h"
uint8 *os_thread_get_stack_boundary() uint8 *
os_thread_get_stack_boundary()
{ {
/* TODO: implement os_thread_get_stack_boundary */ /* TODO: implement os_thread_get_stack_boundary */
return NULL; return NULL;
} }

View File

@ -45,9 +45,12 @@ typedef struct korp_cond {
os_thread_wait_list thread_wait_list; os_thread_wait_list thread_wait_list;
} korp_cond; } korp_cond;
int os_printf(const char *format, ...); int
int os_vprintf(const char *format, va_list ap); os_printf(const char *format, ...);
int
os_vprintf(const char *format, va_list ap);
/* clang-format off */
/* math functions which are not provided by os */ /* math functions which are not provided by os */
double sqrt(double x); double sqrt(double x);
double floor(double x); double floor(double x);
@ -93,5 +96,6 @@ uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort); uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong); uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort); uint16_t ntohs(uint16_t netshort);
/* clang-format on */
#endif #endif

View File

@ -30,7 +30,6 @@ extern "C" {
* 2. To build the app-mgr and app-framework, you must implement it * 2. To build the app-mgr and app-framework, you must implement it
*/ */
/** /**
* Ceates a thread * Ceates a thread
* *
@ -41,8 +40,9 @@ extern "C" {
* *
* @return 0 if success. * @return 0 if success.
*/ */
int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, int
unsigned int stack_size); os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
unsigned int stack_size);
/** /**
* Creates a thread with priority * Creates a thread with priority
@ -55,8 +55,9 @@ int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
* *
* @return 0 if success. * @return 0 if success.
*/ */
int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, int
void *arg, unsigned int stack_size, int prio); os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio);
/** /**
* Waits for the thread specified by thread to terminate * Waits for the thread specified by thread to terminate
@ -66,7 +67,8 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
* *
* @return return 0 if success * @return return 0 if success
*/ */
int os_thread_join(korp_tid thread, void **retval); int
os_thread_join(korp_tid thread, void **retval);
/** /**
* Detach the thread specified by thread * Detach the thread specified by thread
@ -82,7 +84,8 @@ int os_thread_detach(korp_tid);
* *
* @param retval the return value of the current thread * @param retval the return value of the current thread
*/ */
void os_thread_exit(void *retval); void
os_thread_exit(void *retval);
/** /**
* Initialize current thread environment if current thread * Initialize current thread environment if current thread
@ -90,12 +93,14 @@ void os_thread_exit(void *retval);
* *
* @return 0 if success, -1 otherwise * @return 0 if success, -1 otherwise
*/ */
int os_thread_env_init(); int
os_thread_env_init();
/** /**
* Destroy current thread environment * Destroy current thread environment
*/ */
void os_thread_env_destroy(); void
os_thread_env_destroy();
/** /**
* Suspend execution of the calling thread for (at least) * Suspend execution of the calling thread for (at least)
@ -103,7 +108,8 @@ void os_thread_env_destroy();
* *
* @return 0 if success, -1 otherwise * @return 0 if success, -1 otherwise
*/ */
int os_usleep(uint32 usec); int
os_usleep(uint32 usec);
/** /**
* Creates a recursive mutex * Creates a recursive mutex
@ -112,7 +118,8 @@ int os_usleep(uint32 usec);
* *
* @return 0 if success * @return 0 if success
*/ */
int os_recursive_mutex_init(korp_mutex *mutex); int
os_recursive_mutex_init(korp_mutex *mutex);
/** /**
* This function creates a condition variable * This function creates a condition variable
@ -121,7 +128,8 @@ int os_recursive_mutex_init(korp_mutex *mutex);
* *
* @return 0 if success * @return 0 if success
*/ */
int os_cond_init(korp_cond *cond); int
os_cond_init(korp_cond *cond);
/** /**
* This function destroys condition variable * This function destroys condition variable
@ -130,7 +138,8 @@ int os_cond_init(korp_cond *cond);
* *
* @return 0 if success * @return 0 if success
*/ */
int os_cond_destroy(korp_cond *cond); int
os_cond_destroy(korp_cond *cond);
/** /**
* Wait a condition variable. * Wait a condition variable.
@ -140,7 +149,8 @@ int os_cond_destroy(korp_cond *cond);
* *
* @return 0 if success * @return 0 if success
*/ */
int os_cond_wait(korp_cond *cond, korp_mutex *mutex); int
os_cond_wait(korp_cond *cond, korp_mutex *mutex);
/** /**
* Wait a condition varible or return if time specified passes. * Wait a condition varible or return if time specified passes.
@ -151,7 +161,8 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex);
* *
* @return 0 if success * @return 0 if success
*/ */
int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds); int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds);
/** /**
* Signals the condition variable * Signals the condition variable
@ -160,7 +171,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds);
* *
* @return 0 if success * @return 0 if success
*/ */
int os_cond_signal(korp_cond *cond); int
os_cond_signal(korp_cond *cond);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -25,23 +25,28 @@ extern "C" {
* *
* @return 0 if success * @return 0 if success
*/ */
int bh_platform_init(void); int
bh_platform_init(void);
/** /**
* Destroy the platform internal resources if needed, * Destroy the platform internal resources if needed,
* this function is called by wasm_runtime_destroy() * this function is called by wasm_runtime_destroy()
*/ */
void bh_platform_destroy(void); void
bh_platform_destroy(void);
/** /**
******** memory allocator APIs ********** ******** memory allocator APIs **********
*/ */
void *os_malloc(unsigned size); void *
os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size); void *
os_realloc(void *ptr, unsigned size);
void os_free(void *ptr); void
os_free(void *ptr);
/** /**
* Note: the above APIs can simply return NULL if wasm runtime * Note: the above APIs can simply return NULL if wasm runtime
@ -49,28 +54,32 @@ void os_free(void *ptr);
* Refer to wasm_runtime_full_init(). * Refer to wasm_runtime_full_init().
*/ */
int
os_printf(const char *format, ...);
int os_printf(const char *format, ...); int
os_vprintf(const char *format, va_list ap);
int os_vprintf(const char *format, va_list ap);
/** /**
* Get microseconds after boot. * Get microseconds after boot.
*/ */
uint64 os_time_get_boot_microsecond(void); uint64
os_time_get_boot_microsecond(void);
/** /**
* Get current thread id. * Get current thread id.
* Implementation optional: Used by runtime for logging only. * Implementation optional: Used by runtime for logging only.
*/ */
korp_tid os_self_thread(void); korp_tid
os_self_thread(void);
/** /**
* Get current thread's stack boundary address, used for runtime * Get current thread's stack boundary address, used for runtime
* to check the native stack overflow. Return NULL if it is not * to check the native stack overflow. Return NULL if it is not
* easy to implement, but may have potential issue. * easy to implement, but may have potential issue.
*/ */
uint8 *os_thread_get_stack_boundary(void); uint8 *
os_thread_get_stack_boundary(void);
/** /**
************** mutext APIs *********** ************** mutext APIs ***********
@ -78,14 +87,17 @@ uint8 *os_thread_get_stack_boundary(void);
* app-mgr: Must be implemented * app-mgr: Must be implemented
*/ */
int os_mutex_init(korp_mutex *mutex); int
os_mutex_init(korp_mutex *mutex);
int os_mutex_destroy(korp_mutex *mutex); int
os_mutex_destroy(korp_mutex *mutex);
int os_mutex_lock(korp_mutex *mutex); int
os_mutex_lock(korp_mutex *mutex);
int os_mutex_unlock(korp_mutex *mutex);
int
os_mutex_unlock(korp_mutex *mutex);
/************************************************** /**************************************************
* Section 2 * * Section 2 *
@ -110,9 +122,12 @@ enum {
MMAP_MAP_FIXED = 2 MMAP_MAP_FIXED = 2
}; };
void *os_mmap(void *hint, size_t size, int prot, int flags); void *
void os_munmap(void *addr, size_t size); os_mmap(void *hint, size_t size, int prot, int flags);
int os_mprotect(void *addr, size_t size, int prot); void
os_munmap(void *addr, size_t size);
int
os_mprotect(void *addr, size_t size, int prot);
/** /**
* Flush cpu data cache, in some CPUs, after applying relocation to the * Flush cpu data cache, in some CPUs, after applying relocation to the
@ -120,7 +135,8 @@ int os_mprotect(void *addr, size_t size, int prot);
* which may cause unexpected behaviour when executing the AOT code. * which may cause unexpected behaviour when executing the AOT code.
* Implement this function if required, or just leave it empty. * Implement this function if required, or just leave it empty.
*/ */
void os_dcache_flush(void); void
os_dcache_flush(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -42,24 +42,27 @@ extern "C" {
__declspec(dllexport) void *BH_MALLOC(unsigned int size); __declspec(dllexport) void *BH_MALLOC(unsigned int size);
__declspec(dllexport) void BH_FREE(void *ptr); __declspec(dllexport) void BH_FREE(void *ptr);
#else #else
__declspec(dllimport) void* BH_MALLOC(unsigned int size); __declspec(dllimport) void *BH_MALLOC(unsigned int size);
__declspec(dllimport) void BH_FREE(void* ptr); __declspec(dllimport) void BH_FREE(void *ptr);
#endif #endif
#else #else
void *BH_MALLOC(unsigned int size); void *
void BH_FREE(void *ptr); BH_MALLOC(unsigned int size);
void
BH_FREE(void *ptr);
#endif #endif
#if defined(BH_VPRINTF) #if defined(BH_VPRINTF)
#if defined(MSVC) #if defined(MSVC)
__declspec(dllimport) int BH_VPRINTF(const char *format, va_list ap); __declspec(dllimport) int BH_VPRINTF(const char *format, va_list ap);
#else #else
int BH_VPRINTF(const char *format, va_list ap); int
BH_VPRINTF(const char *format, va_list ap);
#endif #endif
#endif #endif
#ifndef NULL #ifndef NULL
#define NULL (void*)0 #define NULL (void *)0
#endif #endif
#ifndef __cplusplus #ifndef __cplusplus
@ -99,8 +102,7 @@ typedef double float64;
typedef uint64_t uint64; typedef uint64_t uint64;
typedef int64_t int64; typedef int64_t int64;
typedef void* (*thread_start_routine_t)(void*); typedef void *(*thread_start_routine_t)(void *);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -40,7 +40,7 @@ extern "C" {
#define _STACK_SIZE_ADJUSTMENT (32 * 1024) #define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet threads's native part. */ /* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT) #define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Default thread priority */ /* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 0 #define BH_THREAD_DEFAULT_PRIORITY 0
@ -50,14 +50,15 @@ typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex; typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond; typedef pthread_cond_t korp_cond;
typedef void (*os_print_function_t)(const char* message); typedef void (*os_print_function_t)(const char *message);
void os_set_print_function(os_print_function_t pf); void
os_set_print_function(os_print_function_t pf);
char *strcpy(char *dest, const char *src); char *
strcpy(char *dest, const char *src);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _PLATFORM_INTERNAL_H */ #endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -13,97 +13,129 @@
#define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
/** fd **/ /** fd **/
int ocall_open(int *p_fd, const char *pathname, int flags, int
bool has_mode, unsigned mode); ocall_open(int *p_fd, const char *pathname, int flags, bool has_mode,
unsigned mode);
int ocall_openat(int *p_fd, int dirfd, const char *pathname, int flags, int
bool has_mode, unsigned mode); ocall_openat(int *p_fd, int dirfd, const char *pathname, int flags,
bool has_mode, unsigned mode);
int ocall_read(ssize_t *p_ret, int fd, void *buf, size_t read_size); int
ocall_read(ssize_t *p_ret, int fd, void *buf, size_t read_size);
int ocall_close(int *p_ret, int fd); int
ocall_close(int *p_ret, int fd);
int ocall_lseek(off_t *p_ret, int fd, off_t offset, int whence); int
ocall_lseek(off_t *p_ret, int fd, off_t offset, int whence);
int ocall_ftruncate(int *p_ret, int fd, off_t length); int
ocall_ftruncate(int *p_ret, int fd, off_t length);
int ocall_fsync(int *p_ret, int fd); int
ocall_fsync(int *p_ret, int fd);
int ocall_fdatasync(int *p_ret, int fd); int
ocall_fdatasync(int *p_ret, int fd);
int ocall_isatty(int *p_ret, int fd); int
ocall_isatty(int *p_ret, int fd);
/** fd end **/ /** fd end **/
/** DIR **/ /** DIR **/
int ocall_fdopendir(int fd, void **p_dirp); int
ocall_fdopendir(int fd, void **p_dirp);
int ocall_readdir(void **p_dirent, void *dirp); int
ocall_readdir(void **p_dirent, void *dirp);
int ocall_rewinddir(void *dirp); int
ocall_rewinddir(void *dirp);
int ocall_seekdir(void *dirp, long loc); int
ocall_seekdir(void *dirp, long loc);
int ocall_telldir(long *p_dir, void *dirp); int
ocall_telldir(long *p_dir, void *dirp);
int ocall_closedir(int *p_ret, void *dirp); int
ocall_closedir(int *p_ret, void *dirp);
/** DIR end **/ /** DIR end **/
/** stat **/ /** stat **/
int ocall_stat(int *p_ret, const char *pathname, int
void *buf, unsigned int buf_len); ocall_stat(int *p_ret, const char *pathname, void *buf, unsigned int buf_len);
int ocall_fstat(int *p_ret, int fd, void *buf, unsigned int buf_len); int
int ocall_fstatat(int *p_ret, int dirfd, const char *pathname, ocall_fstat(int *p_ret, int fd, void *buf, unsigned int buf_len);
void *buf, unsigned int buf_len, int flags); int
ocall_fstatat(int *p_ret, int dirfd, const char *pathname, void *buf,
unsigned int buf_len, int flags);
/** stat end **/ /** stat end **/
/** link **/ /** link **/
int ocall_mkdirat(int *p_ret, int dirfd, const char * pathname, int
unsigned mode); ocall_mkdirat(int *p_ret, int dirfd, const char *pathname, unsigned mode);
int ocall_link(int *p_ret, const char *oldpath, const char *newpath); int
int ocall_linkat(int *p_ret, int olddirfd, const char *oldpath, ocall_link(int *p_ret, const char *oldpath, const char *newpath);
int newdirfd, const char *newpath, int flags); int
int ocall_unlinkat(int *p_ret, int dirfd, const char *pathname, ocall_linkat(int *p_ret, int olddirfd, const char *oldpath, int newdirfd,
int flags); const char *newpath, int flags);
int ocall_readlinkat(ssize_t *p_ret, int dirfd, const char *pathname, int
char *buf, size_t bufsiz); ocall_unlinkat(int *p_ret, int dirfd, const char *pathname, int flags);
int ocall_renameat(int *p_ret, int olddirfd,const char *oldpath, int
int newdirfd,const char *newpath); ocall_readlinkat(ssize_t *p_ret, int dirfd, const char *pathname, char *buf,
int ocall_symlinkat(int *p_ret, const char *target, int newdirfd, size_t bufsiz);
const char *linkpath); int
ocall_renameat(int *p_ret, int olddirfd, const char *oldpath, int newdirfd,
const char *newpath);
int
ocall_symlinkat(int *p_ret, const char *target, int newdirfd,
const char *linkpath);
/** link end **/ /** link end **/
/** control **/ /** control **/
int ocall_ioctl(int *p_ret, int fd, unsigned long request, void *arg, int
unsigned int arg_len); ocall_ioctl(int *p_ret, int fd, unsigned long request, void *arg,
int ocall_fcntl(int *p_ret, int fd, int cmd); unsigned int arg_len);
int ocall_fcntl_long(int *p_ret, int fd, int cmd, long arg); int
ocall_fcntl(int *p_ret, int fd, int cmd);
int
ocall_fcntl_long(int *p_ret, int fd, int cmd, long arg);
/** control end **/ /** control end **/
/** **/ /** **/
int ocall_realpath(int *p_ret, const char *path, char *buf, int
unsigned int buf_len); ocall_realpath(int *p_ret, const char *path, char *buf, unsigned int buf_len);
int ocall_posix_fallocate(int *p_ret, int fd, off_t offset, off_t len); int
int ocall_poll(int *p_ret, void *fds, unsigned nfds, int timeout, ocall_posix_fallocate(int *p_ret, int fd, off_t offset, off_t len);
unsigned int fds_len); int
int ocall_getopt(int *p_ret, int argc, char *argv_buf, ocall_poll(int *p_ret, void *fds, unsigned nfds, int timeout,
unsigned int argv_buf_len, const char *optstring); unsigned int fds_len);
int ocall_getrandom(ssize_t *p_ret, void *buf, size_t buflen, int
unsigned int flags); ocall_getopt(int *p_ret, int argc, char *argv_buf, unsigned int argv_buf_len,
int ocall_getentropy(int *p_ret, void *buffer, size_t length); const char *optstring);
int ocall_sched_yield(int *p_ret); int
ocall_getrandom(ssize_t *p_ret, void *buf, size_t buflen, unsigned int flags);
int
ocall_getentropy(int *p_ret, void *buffer, size_t length);
int
ocall_sched_yield(int *p_ret);
/** struct iovec **/ /** struct iovec **/
ssize_t ocall_readv(ssize_t *p_ret, int fd, char *iov_buf, ssize_t
unsigned int buf_size, int iovcnt, ocall_readv(ssize_t *p_ret, int fd, char *iov_buf, unsigned int buf_size,
bool has_offset, off_t offset); int iovcnt, bool has_offset, off_t offset);
ssize_t ocall_writev(ssize_t *p_ret, int fd, char *iov_buf, ssize_t
unsigned int buf_size, int iovcnt, ocall_writev(ssize_t *p_ret, int fd, char *iov_buf, unsigned int buf_size,
bool has_offset, off_t offset); int iovcnt, bool has_offset, off_t offset);
/** iovec end **/ /** iovec end **/
int ocall_get_errno(int *p_ret); int
ocall_get_errno(int *p_ret);
int open(const char *pathname, int flags, ...) int
open(const char *pathname, int flags, ...)
{ {
int fd; int fd;
bool has_mode = false; bool has_mode = false;
@ -130,7 +162,8 @@ int open(const char *pathname, int flags, ...)
return fd; return fd;
} }
int openat(int dirfd, const char *pathname, int flags, ...) int
openat(int dirfd, const char *pathname, int flags, ...)
{ {
int fd; int fd;
bool has_mode = false; bool has_mode = false;
@ -144,8 +177,8 @@ int openat(int dirfd, const char *pathname, int flags, ...)
has_mode = true; has_mode = true;
} }
if (SGX_SUCCESS != ocall_openat(&fd, dirfd, pathname, flags, if (SGX_SUCCESS
has_mode, mode)) { != ocall_openat(&fd, dirfd, pathname, flags, has_mode, mode)) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -158,7 +191,8 @@ int openat(int dirfd, const char *pathname, int flags, ...)
return fd; return fd;
} }
int close(int fd) int
close(int fd)
{ {
int ret; int ret;
@ -171,7 +205,8 @@ int close(int fd)
return ret; return ret;
} }
ssize_t read(int fd, void *buf, size_t size) ssize_t
read(int fd, void *buf, size_t size)
{ {
ssize_t ret; ssize_t ret;
int size_read_max = 2048, size_read, total_size_read = 0, count, i; int size_read_max = 2048, size_read, total_size_read = 0, count, i;
@ -184,9 +219,7 @@ ssize_t read(int fd, void *buf, size_t size)
count = (size + size_read_max - 1) / size_read_max; count = (size + size_read_max - 1) / size_read_max;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
size_read = (i < count - 1) size_read = (i < count - 1) ? size_read_max : size - size_read_max * i;
? size_read_max
: size - size_read_max * i;
if (ocall_read(&ret, fd, p, size_read) != SGX_SUCCESS) { if (ocall_read(&ret, fd, p, size_read) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
@ -208,7 +241,8 @@ ssize_t read(int fd, void *buf, size_t size)
return total_size_read; return total_size_read;
} }
DIR *fdopendir(int fd) DIR *
fdopendir(int fd)
{ {
DIR *result = NULL; DIR *result = NULL;
@ -232,7 +266,8 @@ DIR *fdopendir(int fd)
return result; return result;
} }
struct dirent *readdir(DIR *dirp) struct dirent *
readdir(DIR *dirp)
{ {
struct dirent *result; struct dirent *result;
@ -249,21 +284,24 @@ struct dirent *readdir(DIR *dirp)
return result; return result;
} }
void rewinddir(DIR *dirp) void
rewinddir(DIR *dirp)
{ {
if (ocall_rewinddir((void *)*dirp) != SGX_SUCCESS) { if (ocall_rewinddir((void *)*dirp) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
} }
} }
void seekdir(DIR *dirp, long loc) void
seekdir(DIR *dirp, long loc)
{ {
if (ocall_seekdir((void *)*dirp, loc) != SGX_SUCCESS) { if (ocall_seekdir((void *)*dirp, loc) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
} }
} }
long telldir(DIR *dirp) long
telldir(DIR *dirp)
{ {
long ret; long ret;
@ -276,7 +314,8 @@ long telldir(DIR *dirp)
return ret; return ret;
} }
int closedir(DIR *dirp) int
closedir(DIR *dirp)
{ {
int ret; int ret;
@ -291,8 +330,8 @@ int closedir(DIR *dirp)
} }
static ssize_t static ssize_t
readv_internal(int fd, const struct iovec *iov, int iovcnt, readv_internal(int fd, const struct iovec *iov, int iovcnt, bool has_offset,
bool has_offset, off_t offset) off_t offset)
{ {
ssize_t ret, size_left; ssize_t ret, size_left;
struct iovec *iov1; struct iovec *iov1;
@ -317,7 +356,7 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt,
memset(iov1, 0, (uint32)total_size); memset(iov1, 0, (uint32)total_size);
p = (char*)(uintptr_t)(sizeof(struct iovec) * iovcnt); p = (char *)(uintptr_t)(sizeof(struct iovec) * iovcnt);
for (i = 0; i < iovcnt; i++) { for (i = 0; i < iovcnt; i++) {
iov1[i].iov_len = iov[i].iov_len; iov1[i].iov_len = iov[i].iov_len;
@ -325,8 +364,9 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt,
p += iov[i].iov_len; p += iov[i].iov_len;
} }
if (ocall_readv(&ret, fd, (char *)iov1, (uint32)total_size, if (ocall_readv(&ret, fd, (char *)iov1, (uint32)total_size, iovcnt,
iovcnt, has_offset, offset) != SGX_SUCCESS) { has_offset, offset)
!= SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
BH_FREE(iov1); BH_FREE(iov1);
return -1; return -1;
@ -343,8 +383,7 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt,
size_left -= iov[i].iov_len; size_left -= iov[i].iov_len;
} }
else { else {
memcpy(iov[i].iov_base, (uintptr_t)p + (char *)iov1, memcpy(iov[i].iov_base, (uintptr_t)p + (char *)iov1, size_left);
size_left);
break; break;
} }
} }
@ -356,8 +395,8 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt,
} }
static ssize_t static ssize_t
writev_internal(int fd, const struct iovec *iov, int iovcnt, writev_internal(int fd, const struct iovec *iov, int iovcnt, bool has_offset,
bool has_offset, off_t offset) off_t offset)
{ {
ssize_t ret; ssize_t ret;
struct iovec *iov1; struct iovec *iov1;
@ -387,13 +426,13 @@ writev_internal(int fd, const struct iovec *iov, int iovcnt,
for (i = 0; i < iovcnt; i++) { for (i = 0; i < iovcnt; i++) {
iov1[i].iov_len = iov[i].iov_len; iov1[i].iov_len = iov[i].iov_len;
iov1[i].iov_base = p; iov1[i].iov_base = p;
memcpy((uintptr_t)p + (char *)iov1, iov[i].iov_base, memcpy((uintptr_t)p + (char *)iov1, iov[i].iov_base, iov[i].iov_len);
iov[i].iov_len);
p += iov[i].iov_len; p += iov[i].iov_len;
} }
if (ocall_writev(&ret, fd, (char *)iov1, (uint32)total_size, if (ocall_writev(&ret, fd, (char *)iov1, (uint32)total_size, iovcnt,
iovcnt, has_offset, offset) != SGX_SUCCESS) { has_offset, offset)
!= SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
BH_FREE(iov1); BH_FREE(iov1);
return -1; return -1;
@ -405,29 +444,32 @@ writev_internal(int fd, const struct iovec *iov, int iovcnt,
return ret; return ret;
} }
ssize_t readv(int fd, const struct iovec *iov, int iovcnt) ssize_t
readv(int fd, const struct iovec *iov, int iovcnt)
{ {
return readv_internal(fd, iov, iovcnt, false, 0); return readv_internal(fd, iov, iovcnt, false, 0);
} }
ssize_t writev(int fd, const struct iovec *iov, int iovcnt) ssize_t
writev(int fd, const struct iovec *iov, int iovcnt)
{ {
return writev_internal(fd, iov, iovcnt, false, 0); return writev_internal(fd, iov, iovcnt, false, 0);
} }
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, ssize_t
off_t offset) preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{ {
return readv_internal(fd, iov, iovcnt, true, offset); return readv_internal(fd, iov, iovcnt, true, offset);
} }
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, ssize_t
off_t offset) pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{ {
return writev_internal(fd, iov, iovcnt, true, offset); return writev_internal(fd, iov, iovcnt, true, offset);
} }
off_t lseek(int fd, off_t offset, int whence) off_t
lseek(int fd, off_t offset, int whence)
{ {
off_t ret; off_t ret;
if (ocall_lseek(&ret, fd, (long)offset, whence) != SGX_SUCCESS) { if (ocall_lseek(&ret, fd, (long)offset, whence) != SGX_SUCCESS) {
@ -439,7 +481,8 @@ off_t lseek(int fd, off_t offset, int whence)
return ret; return ret;
} }
int ftruncate(int fd, off_t length) int
ftruncate(int fd, off_t length)
{ {
int ret; int ret;
@ -452,16 +495,16 @@ int ftruncate(int fd, off_t length)
return ret; return ret;
} }
int stat(const char *pathname, struct stat *statbuf) int
stat(const char *pathname, struct stat *statbuf)
{ {
int ret; int ret;
if (statbuf == NULL) if (statbuf == NULL)
return -1; return -1;
if (ocall_stat(&ret, pathname, if (ocall_stat(&ret, pathname, (void *)statbuf, sizeof(struct stat))
(void *)statbuf, != SGX_SUCCESS) {
sizeof(struct stat)) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -471,15 +514,16 @@ int stat(const char *pathname, struct stat *statbuf)
return ret; return ret;
} }
int fstat(int fd, struct stat *statbuf) int
fstat(int fd, struct stat *statbuf)
{ {
int ret; int ret;
if (statbuf == NULL) if (statbuf == NULL)
return -1; return -1;
if (ocall_fstat(&ret, fd, (void *)statbuf, if (ocall_fstat(&ret, fd, (void *)statbuf, sizeof(struct stat))
sizeof(struct stat)) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -489,8 +533,8 @@ int fstat(int fd, struct stat *statbuf)
return ret; return ret;
} }
int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int
int flags) fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags)
{ {
int ret; int ret;
@ -498,7 +542,8 @@ int fstatat(int dirfd, const char *pathname, struct stat *statbuf,
return -1; return -1;
if (ocall_fstatat(&ret, dirfd, pathname, (void *)statbuf, if (ocall_fstatat(&ret, dirfd, pathname, (void *)statbuf,
sizeof(struct stat), flags) != SGX_SUCCESS) { sizeof(struct stat), flags)
!= SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -508,7 +553,8 @@ int fstatat(int dirfd, const char *pathname, struct stat *statbuf,
return ret; return ret;
} }
int fsync(int fd) int
fsync(int fd)
{ {
int ret; int ret;
@ -521,7 +567,8 @@ int fsync(int fd)
return ret; return ret;
} }
int fdatasync(int fd) int
fdatasync(int fd)
{ {
int ret; int ret;
@ -534,7 +581,8 @@ int fdatasync(int fd)
return ret; return ret;
} }
int mkdirat(int dirfd, const char *pathname, mode_t mode) int
mkdirat(int dirfd, const char *pathname, mode_t mode)
{ {
int ret; int ret;
@ -548,7 +596,8 @@ int mkdirat(int dirfd, const char *pathname, mode_t mode)
return ret; return ret;
} }
int link(const char *oldpath, const char *newpath) int
link(const char *oldpath, const char *newpath)
{ {
int ret; int ret;
@ -562,13 +611,14 @@ int link(const char *oldpath, const char *newpath)
return ret; return ret;
} }
int linkat(int olddirfd, const char *oldpath, int
int newdirfd, const char *newpath, int flags) linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath,
int flags)
{ {
int ret; int ret;
if (ocall_linkat(&ret, olddirfd, oldpath, newdirfd, newpath, if (ocall_linkat(&ret, olddirfd, oldpath, newdirfd, newpath, flags)
flags) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -578,7 +628,8 @@ int linkat(int olddirfd, const char *oldpath,
return ret; return ret;
} }
int unlinkat(int dirfd, const char *pathname, int flags) int
unlinkat(int dirfd, const char *pathname, int flags)
{ {
int ret; int ret;
@ -592,16 +643,15 @@ int unlinkat(int dirfd, const char *pathname, int flags)
return ret; return ret;
} }
ssize_t readlinkat(int dirfd, const char *pathname, ssize_t
char *buf, size_t bufsiz) readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
{ {
ssize_t ret; ssize_t ret;
if (buf == NULL) if (buf == NULL)
return -1; return -1;
if (ocall_readlinkat(&ret, dirfd, pathname, buf, if (ocall_readlinkat(&ret, dirfd, pathname, buf, bufsiz) != SGX_SUCCESS) {
bufsiz) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -611,12 +661,12 @@ ssize_t readlinkat(int dirfd, const char *pathname,
return ret; return ret;
} }
int symlinkat(const char *target, int newdirfd, const char *linkpath) int
symlinkat(const char *target, int newdirfd, const char *linkpath)
{ {
int ret; int ret;
if (ocall_symlinkat(&ret, target, if (ocall_symlinkat(&ret, target, newdirfd, linkpath) != SGX_SUCCESS) {
newdirfd, linkpath) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -626,13 +676,13 @@ int symlinkat(const char *target, int newdirfd, const char *linkpath)
return ret; return ret;
} }
int renameat(int olddirfd, const char *oldpath, int
int newdirfd, const char *newpath) renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
{ {
int ret; int ret;
if (ocall_renameat(&ret, olddirfd, oldpath, if (ocall_renameat(&ret, olddirfd, oldpath, newdirfd, newpath)
newdirfd, newpath) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -642,7 +692,8 @@ int renameat(int olddirfd, const char *oldpath,
return ret; return ret;
} }
int ioctl(int fd, unsigned long request, ...) int
ioctl(int fd, unsigned long request, ...)
{ {
int ret; int ret;
va_list args; va_list args;
@ -651,8 +702,8 @@ int ioctl(int fd, unsigned long request, ...)
case FIONREAD: case FIONREAD:
va_start(args, request); va_start(args, request);
int *arg = (int *)va_arg(args, int *); int *arg = (int *)va_arg(args, int *);
if (ocall_ioctl(&ret, fd, request, arg, if (ocall_ioctl(&ret, fd, request, arg, sizeof(*arg))
sizeof(*arg)) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
va_end(args); va_end(args);
return -1; return -1;
@ -670,7 +721,8 @@ int ioctl(int fd, unsigned long request, ...)
return ret; return ret;
} }
int fcntl(int fd, int cmd, ... /* arg */ ) int
fcntl(int fd, int cmd, ... /* arg */)
{ {
int ret; int ret;
va_list args; va_list args;
@ -707,7 +759,8 @@ int fcntl(int fd, int cmd, ... /* arg */ )
return ret; return ret;
} }
int isatty(int fd) int
isatty(int fd)
{ {
int ret; int ret;
@ -720,7 +773,8 @@ int isatty(int fd)
return ret; return ret;
} }
char *realpath(const char *path, char *resolved_path) char *
realpath(const char *path, char *resolved_path)
{ {
int ret; int ret;
char buf[PATH_MAX] = { 0 }; char buf[PATH_MAX] = { 0 };
@ -746,7 +800,8 @@ char *realpath(const char *path, char *resolved_path)
return resolved_path; return resolved_path;
} }
int posix_fallocate(int fd, off_t offset, off_t len) int
posix_fallocate(int fd, off_t offset, off_t len)
{ {
int ret; int ret;
@ -758,15 +813,16 @@ int posix_fallocate(int fd, off_t offset, off_t len)
return ret; return ret;
} }
int poll(struct pollfd *fds, nfds_t nfds, int timeout) int
poll(struct pollfd *fds, nfds_t nfds, int timeout)
{ {
int ret; int ret;
if (fds == NULL) if (fds == NULL)
return -1; return -1;
if (ocall_poll(&ret, fds, nfds, timeout, if (ocall_poll(&ret, fds, nfds, timeout, sizeof(*fds) * nfds)
sizeof(*fds) * nfds) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -776,8 +832,8 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout)
return ret; return ret;
} }
int getopt(int argc, char * const argv[], int
const char *optstring) getopt(int argc, char *const argv[], const char *optstring)
{ {
int ret; int ret;
char **argv1; char **argv1;
@ -805,8 +861,8 @@ int getopt(int argc, char * const argv[],
p += ((uintptr_t)strlen(argv[i]) + 1); p += ((uintptr_t)strlen(argv[i]) + 1);
} }
if (ocall_getopt(&ret, argc, (char *)argv1, total_size, if (ocall_getopt(&ret, argc, (char *)argv1, total_size, optstring)
optstring) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
BH_FREE(argv1); BH_FREE(argv1);
return -1; return -1;
@ -818,7 +874,8 @@ int getopt(int argc, char * const argv[],
return ret; return ret;
} }
int sched_yield(void) int
sched_yield(void)
{ {
int ret; int ret;
@ -831,7 +888,8 @@ int sched_yield(void)
return ret; return ret;
} }
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) ssize_t
getrandom(void *buf, size_t buflen, unsigned int flags)
{ {
ssize_t ret; ssize_t ret;
@ -844,7 +902,8 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
return ret; return ret;
} }
int getentropy(void *buffer, size_t length) int
getentropy(void *buffer, size_t length)
{ {
int ret; int ret;
@ -857,7 +916,8 @@ int getentropy(void *buffer, size_t length)
return ret; return ret;
} }
int get_errno(void) int
get_errno(void)
{ {
int ret; int ret;
@ -869,4 +929,3 @@ int get_errno(void)
} }
#endif #endif

View File

@ -12,45 +12,45 @@
extern "C" { extern "C" {
#endif #endif
#define F_DUPFD 0 #define F_DUPFD 0
#define F_GETFD 1 #define F_GETFD 1
#define F_SETFD 2 #define F_SETFD 2
#define F_GETFL 3 #define F_GETFL 3
#define F_SETFL 4 #define F_SETFL 4
#define FD_CLOEXEC 1 #define FD_CLOEXEC 1
#define O_PATH 010000000 #define O_PATH 010000000
#define O_SEARCH O_PATH #define O_SEARCH O_PATH
#define O_EXEC O_PATH #define O_EXEC O_PATH
#define O_ACCMODE (03|O_SEARCH) #define O_ACCMODE (03 | O_SEARCH)
#define O_RDONLY 00 #define O_RDONLY 00
#define O_WRONLY 01 #define O_WRONLY 01
#define O_RDWR 02 #define O_RDWR 02
#define O_CREAT 0100 #define O_CREAT 0100
#define O_EXCL 0200 #define O_EXCL 0200
#define O_NOCTTY 0400 #define O_NOCTTY 0400
#define O_TRUNC 01000 #define O_TRUNC 01000
#define O_APPEND 02000 #define O_APPEND 02000
#define O_NONBLOCK 04000 #define O_NONBLOCK 04000
#define O_DSYNC 010000 #define O_DSYNC 010000
#define O_SYNC 04010000 #define O_SYNC 04010000
#define O_RSYNC 04010000 #define O_RSYNC 04010000
#define O_DIRECTORY 0200000 #define O_DIRECTORY 0200000
#define O_NOFOLLOW 0400000 #define O_NOFOLLOW 0400000
#define O_CLOEXEC 02000000 #define O_CLOEXEC 02000000
#define O_ASYNC 020000 #define O_ASYNC 020000
#define O_DIRECT 040000 #define O_DIRECT 040000
#define O_LARGEFILE 0 #define O_LARGEFILE 0
#define O_NOATIME 01000000 #define O_NOATIME 01000000
#define O_PATH 010000000 #define O_PATH 010000000
#define O_TMPFILE 020200000 #define O_TMPFILE 020200000
#define O_NDELAY O_NONBLOCK #define O_NDELAY O_NONBLOCK
#define S_IFMT 0170000 #define S_IFMT 0170000
#define S_IFDIR 0040000 #define S_IFDIR 0040000
#define S_IFCHR 0020000 #define S_IFCHR 0020000
#define S_IFBLK 0060000 #define S_IFBLK 0060000
@ -63,13 +63,13 @@ extern "C" {
#define SEEK_CUR 1 #define SEEK_CUR 1
#define SEEK_END 2 #define SEEK_END 2
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) #define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) #define S_ISCHR(mode) (((mode)&S_IFMT) == S_IFCHR)
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) #define S_ISBLK(mode) (((mode)&S_IFMT) == S_IFBLK)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) #define S_ISREG(mode) (((mode)&S_IFMT) == S_IFREG)
#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) #define S_ISFIFO(mode) (((mode)&S_IFMT) == S_IFIFO)
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) #define S_ISLNK(mode) (((mode)&S_IFMT) == S_IFLNK)
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) #define S_ISSOCK(mode) (((mode)&S_IFMT) == S_IFSOCK)
#define DT_UNKNOWN 0 #define DT_UNKNOWN 0
#define DT_FIFO 1 #define DT_FIFO 1
@ -85,24 +85,24 @@ extern "C" {
#define AT_REMOVEDIR 0x200 #define AT_REMOVEDIR 0x200
#define AT_SYMLINK_FOLLOW 0x400 #define AT_SYMLINK_FOLLOW 0x400
#define POLLIN 0x001 #define POLLIN 0x001
#define POLLPRI 0x002 #define POLLPRI 0x002
#define POLLOUT 0x004 #define POLLOUT 0x004
#define POLLERR 0x008 #define POLLERR 0x008
#define POLLHUP 0x010 #define POLLHUP 0x010
#define POLLNVAL 0x020 #define POLLNVAL 0x020
#define POLLRDNORM 0x040 #define POLLRDNORM 0x040
#define POLLRDBAND 0x080 #define POLLRDBAND 0x080
#define POLLWRNORM 0x100 #define POLLWRNORM 0x100
#define POLLWRBAND 0x200 #define POLLWRBAND 0x200
#define FIONREAD 0x541B #define FIONREAD 0x541B
#define PATH_MAX 4096 #define PATH_MAX 4096
/* Special value used to indicate openat should use the current /* Special value used to indicate openat should use the current
working directory. */ working directory. */
#define AT_FDCWD -100 #define AT_FDCWD -100
typedef long __syscall_slong_t; typedef long __syscall_slong_t;
@ -138,7 +138,7 @@ struct stat {
mode_t st_mode; mode_t st_mode;
uid_t st_uid; uid_t st_uid;
gid_t st_gid; gid_t st_gid;
unsigned int __pad0; unsigned int __pad0;
dev_t st_rdev; dev_t st_rdev;
off_t st_size; off_t st_size;
blksize_t st_blksize; blksize_t st_blksize;
@ -156,76 +156,108 @@ struct iovec {
}; };
struct pollfd { struct pollfd {
int fd; int fd;
short events; short events;
short revents; short revents;
}; };
int open(const char *pathname, int flags, ...); int
int openat(int dirfd, const char *pathname, int flags, ...); open(const char *pathname, int flags, ...);
int close(int fd); int
openat(int dirfd, const char *pathname, int flags, ...);
int
close(int fd);
DIR *fdopendir(int fd); DIR *
int closedir(DIR *dirp); fdopendir(int fd);
void rewinddir(DIR *dirp); int
void seekdir(DIR *dirp, long loc); closedir(DIR *dirp);
struct dirent *readdir(DIR *dirp); void
long telldir(DIR *dirp); rewinddir(DIR *dirp);
void
seekdir(DIR *dirp, long loc);
struct dirent *
readdir(DIR *dirp);
long
telldir(DIR *dirp);
ssize_t read(int fd, void *buf, size_t count); ssize_t
ssize_t readv(int fd, const struct iovec *iov, int iovcnt); read(int fd, void *buf, size_t count);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt); ssize_t
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, readv(int fd, const struct iovec *iov, int iovcnt);
off_t offset); ssize_t
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, writev(int fd, const struct iovec *iov, int iovcnt);
off_t offset); ssize_t
preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
ssize_t
pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
off_t lseek(int fd, off_t offset, int whence); off_t
int ftruncate(int fd, off_t length); lseek(int fd, off_t offset, int whence);
int
ftruncate(int fd, off_t length);
int stat(const char *pathname, struct stat *statbuf); int
int fstat(int fd, struct stat *statbuf); stat(const char *pathname, struct stat *statbuf);
int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int
int flags); fstat(int fd, struct stat *statbuf);
int
fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);
int fsync(int fd); int
int fdatasync(int fd); fsync(int fd);
int
fdatasync(int fd);
int mkdirat(int dirfd, const char *pathname, mode_t mode); int
int link(const char *oldpath, const char *newpath); mkdirat(int dirfd, const char *pathname, mode_t mode);
int linkat(int olddirfd, const char *oldpath, int
int newdirfd, const char *newpath, int flags); link(const char *oldpath, const char *newpath);
int unlinkat(int dirfd, const char *pathname, int flags); int
ssize_t readlinkat(int dirfd, const char *pathname, linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath,
char *buf, size_t bufsiz); int flags);
int symlinkat(const char *target, int newdirfd, const char *linkpath); int
int renameat(int olddirfd, const char *oldpath, unlinkat(int dirfd, const char *pathname, int flags);
int newdirfd, const char *newpath); ssize_t
readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
int
symlinkat(const char *target, int newdirfd, const char *linkpath);
int
renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
int ioctl(int fd, unsigned long request, ...); int
int fcntl(int fd, int cmd, ... /* arg */ ); ioctl(int fd, unsigned long request, ...);
int
fcntl(int fd, int cmd, ... /* arg */);
int isatty(int fd); int
isatty(int fd);
char *realpath(const char *path, char *resolved_path); char *
realpath(const char *path, char *resolved_path);
int posix_fallocate(int fd, off_t offset, off_t len); int
posix_fallocate(int fd, off_t offset, off_t len);
int poll(struct pollfd *fds, nfds_t nfds, int timeout); int
poll(struct pollfd *fds, nfds_t nfds, int timeout);
int getopt(int argc, char * const argv[], int
const char *optstring); getopt(int argc, char *const argv[], const char *optstring);
int sched_yield(void); int
sched_yield(void);
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); ssize_t
int getentropy(void *buffer, size_t length); getrandom(void *buf, size_t buflen, unsigned int flags);
int
getentropy(void *buffer, size_t length);
int get_errno(void); int
get_errno(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _SGX_FILE_H */ #endif /* end of _SGX_FILE_H */

View File

@ -7,19 +7,19 @@
#include "platform_api_extension.h" #include "platform_api_extension.h"
#include "sgx_rsrv_mem_mngr.h" #include "sgx_rsrv_mem_mngr.h"
#define FIXED_BUFFER_SIZE (1<<9) #define FIXED_BUFFER_SIZE (1 << 9)
static os_print_function_t print_function = NULL; static os_print_function_t print_function = NULL;
int bh_platform_init() int
bh_platform_init()
{ {
return 0; return 0;
} }
void void
bh_platform_destroy() bh_platform_destroy()
{ {}
}
void * void *
os_malloc(unsigned size) os_malloc(unsigned size)
@ -39,22 +39,26 @@ os_free(void *ptr)
free(ptr); free(ptr);
} }
int putchar(int c) int
putchar(int c)
{ {
return 0; return 0;
} }
int puts(const char *s) int
puts(const char *s)
{ {
return 0; return 0;
} }
void os_set_print_function(os_print_function_t pf) void
os_set_print_function(os_print_function_t pf)
{ {
print_function = pf; print_function = pf;
} }
int os_printf(const char *message, ...) int
os_printf(const char *message, ...)
{ {
if (print_function != NULL) { if (print_function != NULL) {
char msg[FIXED_BUFFER_SIZE] = { '\0' }; char msg[FIXED_BUFFER_SIZE] = { '\0' };
@ -68,7 +72,8 @@ int os_printf(const char *message, ...)
return 0; return 0;
} }
int os_vprintf(const char * format, va_list arg) int
os_vprintf(const char *format, va_list arg)
{ {
if (print_function != NULL) { if (print_function != NULL) {
char msg[FIXED_BUFFER_SIZE] = { '\0' }; char msg[FIXED_BUFFER_SIZE] = { '\0' };
@ -79,20 +84,23 @@ int os_vprintf(const char * format, va_list arg)
return 0; return 0;
} }
char *strcpy(char *dest, const char *src) char *
strcpy(char *dest, const char *src)
{ {
const unsigned char *s = src; const unsigned char *s = src;
unsigned char *d = dest; unsigned char *d = dest;
while ((*d++ = *s++)); while ((*d++ = *s++))
;
return dest; return dest;
} }
void* os_mmap(void *hint, size_t size, int prot, int flags) void *
os_mmap(void *hint, size_t size, int prot, int flags)
{ {
int mprot = 0; int mprot = 0;
uint64 aligned_size, page_size; uint64 aligned_size, page_size;
void* ret = NULL; void *ret = NULL;
sgx_status_t st = 0; sgx_status_t st = 0;
page_size = getpagesize(); page_size = getpagesize();
@ -103,8 +111,8 @@ void* os_mmap(void *hint, size_t size, int prot, int flags)
ret = sgx_alloc_rsrv_mem(aligned_size); ret = sgx_alloc_rsrv_mem(aligned_size);
if (ret == NULL) { if (ret == NULL) {
os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.", os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.", size,
size, aligned_size, prot); aligned_size, prot);
return NULL; return NULL;
} }
@ -117,8 +125,8 @@ void* os_mmap(void *hint, size_t size, int prot, int flags)
st = sgx_tprotect_rsrv_mem(ret, aligned_size, mprot); st = sgx_tprotect_rsrv_mem(ret, aligned_size, mprot);
if (st != SGX_SUCCESS) { if (st != SGX_SUCCESS) {
os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.", os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.", size,
size, prot); prot);
sgx_free_rsrv_mem(ret, aligned_size); sgx_free_rsrv_mem(ret, aligned_size);
return NULL; return NULL;
} }
@ -126,7 +134,8 @@ void* os_mmap(void *hint, size_t size, int prot, int flags)
return ret; return ret;
} }
void os_munmap(void *addr, size_t size) void
os_munmap(void *addr, size_t size)
{ {
uint64 aligned_size, page_size; uint64 aligned_size, page_size;
@ -135,7 +144,8 @@ void os_munmap(void *addr, size_t size)
sgx_free_rsrv_mem(addr, aligned_size); sgx_free_rsrv_mem(addr, aligned_size);
} }
int os_mprotect(void *addr, size_t size, int prot) int
os_mprotect(void *addr, size_t size, int prot)
{ {
int mprot = 0; int mprot = 0;
sgx_status_t st = 0; sgx_status_t st = 0;
@ -152,14 +162,12 @@ int os_mprotect(void *addr, size_t size, int prot)
mprot |= SGX_PROT_EXEC; mprot |= SGX_PROT_EXEC;
st = sgx_tprotect_rsrv_mem(addr, aligned_size, mprot); st = sgx_tprotect_rsrv_mem(addr, aligned_size, mprot);
if (st != SGX_SUCCESS) if (st != SGX_SUCCESS)
os_printf("os_mprotect(addr=0x%"PRIx64", size=%u, prot=0x%x) failed.", os_printf("os_mprotect(addr=0x%" PRIx64 ", size=%u, prot=0x%x) failed.",
(uintptr_t)addr, size, prot); (uintptr_t)addr, size, prot);
return (st == SGX_SUCCESS? 0:-1); return (st == SGX_SUCCESS ? 0 : -1);
} }
void void
os_dcache_flush(void) os_dcache_flush(void)
{ {}
}

View File

@ -15,22 +15,27 @@
#ifndef SGX_THREAD_LOCK_INITIALIZER /* defined since sgxsdk-2.11 */ #ifndef SGX_THREAD_LOCK_INITIALIZER /* defined since sgxsdk-2.11 */
/* sgxsdk doesn't support pthread_rwlock related APIs until /* sgxsdk doesn't support pthread_rwlock related APIs until
version 2.11, we implement them by ourselves. */ version 2.11, we implement them by ourselves. */
int ocall_pthread_rwlock_init(int *p_ret, void **rwlock, void *attr); int
ocall_pthread_rwlock_init(int *p_ret, void **rwlock, void *attr);
int ocall_pthread_rwlock_destroy(int *p_ret, void **rwlock); int
ocall_pthread_rwlock_destroy(int *p_ret, void **rwlock);
int ocall_pthread_rwlock_rdlock(int *p_ret, void **rwlock); int
ocall_pthread_rwlock_rdlock(int *p_ret, void **rwlock);
int ocall_pthread_rwlock_wrlock(int *p_ret, void **rwlock); int
ocall_pthread_rwlock_wrlock(int *p_ret, void **rwlock);
int ocall_pthread_rwlock_unlock(int *p_ret, void **rwlock); int
ocall_pthread_rwlock_unlock(int *p_ret, void **rwlock);
int pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr) int
pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr)
{ {
int ret = -1; int ret = -1;
if (ocall_pthread_rwlock_init(&ret, (void **)rwlock, NULL) if (ocall_pthread_rwlock_init(&ret, (void **)rwlock, NULL) != SGX_SUCCESS) {
!= SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -38,7 +43,8 @@ int pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr)
return ret; return ret;
} }
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) int
pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
{ {
int ret = -1; int ret = -1;
@ -48,31 +54,34 @@ int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
return ret; return ret;
} }
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) int
pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
{ {
int ret = -1; int ret = -1;
if (ocall_pthread_rwlock_rdlock(&ret, (void*)*rwlock) != SGX_SUCCESS) { if (ocall_pthread_rwlock_rdlock(&ret, (void *)*rwlock) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
} }
return ret; return ret;
} }
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) int
pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
{ {
int ret = -1; int ret = -1;
if (ocall_pthread_rwlock_wrlock(&ret, (void*)*rwlock) != SGX_SUCCESS) { if (ocall_pthread_rwlock_wrlock(&ret, (void *)*rwlock) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
} }
return ret; return ret;
} }
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) int
pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{ {
int ret = -1; int ret = -1;
if (ocall_pthread_rwlock_unlock(&ret, (void*)*rwlock) != SGX_SUCCESS) { if (ocall_pthread_rwlock_unlock(&ret, (void *)*rwlock) != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
} }
return ret; return ret;
@ -80,4 +89,3 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
#endif /* end of SGX_THREAD_LOCK_INITIALIZER */ #endif /* end of SGX_THREAD_LOCK_INITIALIZER */
#endif #endif

View File

@ -15,12 +15,17 @@ extern "C" {
version 2.11, we implement them by ourselves. */ version 2.11, we implement them by ourselves. */
typedef uintptr_t pthread_rwlock_t; typedef uintptr_t pthread_rwlock_t;
int pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr); int
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr);
int
pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); int
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); int
pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int
pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
#endif /* end of SGX_THREAD_LOCK_INITIALIZER */ #endif /* end of SGX_THREAD_LOCK_INITIALIZER */
#ifdef __cplusplus #ifdef __cplusplus
@ -28,4 +33,3 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
#endif #endif
#endif /* end of _SGX_PTHREAD_H */ #endif /* end of _SGX_PTHREAD_H */

View File

@ -30,7 +30,8 @@
*/ */
/* /*
* This file is copied from https://github.com/intel/linux-sgx/blob/4589daddd58bec7367a6a9de3fe301e6de17671a/common/inc/internal/sgx_rsrv_mem_mngr.h * This file is copied from
* https://github.com/intel/linux-sgx/blob/4589daddd58bec7367a6a9de3fe301e6de17671a/common/inc/internal/sgx_rsrv_mem_mngr.h
* The reason we copied here is that the official SGX SDK release has * The reason we copied here is that the official SGX SDK release has
* not included this header file yet. * not included this header file yet.
*/ */
@ -43,48 +44,52 @@
#include "stdint.h" #include "stdint.h"
#include "sgx_error.h" #include "sgx_error.h"
#define SGX_PROT_READ 0x1 /* page can be read */ #define SGX_PROT_READ 0x1 /* page can be read */
#define SGX_PROT_WRITE 0x2 /* page can be written */ #define SGX_PROT_WRITE 0x2 /* page can be written */
#define SGX_PROT_EXEC 0x4 /* page can be executed */ #define SGX_PROT_EXEC 0x4 /* page can be executed */
#define SGX_PROT_NONE 0x0 /* page can not be accessed */ #define SGX_PROT_NONE 0x0 /* page can not be accessed */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* Allocate a range of EPC memory from the reserved memory area with RW permission /* Allocate a range of EPC memory from the reserved memory area with RW
* * permission
* Parameters: *
* Inputs: length [in]: Size of region to be allocated in bytes. Page aligned * Parameters:
* Return: Starting address of the new allocated memory area on success; otherwise NULL * Inputs: length [in]: Size of region to be allocated in bytes. Page aligned.
*/ * Return: Starting address of the new allocated memory area on success;
void * sgx_alloc_rsrv_mem(size_t length); * otherwise NULL
*/
void *
sgx_alloc_rsrv_mem(size_t length);
/* Free a range of EPC memory from the reserved memory area
*
* Parameters:
* Inputs: addr[in]: Starting address of region to be freed. Page aligned.
* length[in]: The length of the memory to be freed in bytes.
* Page aligned.
* Return: 0 on success; otherwise -1
*/
int
sgx_free_rsrv_mem(void *addr, size_t length);
/* Free a range of EPC memory from the reserved memory area /* Modify the access permissions of the pages in the reserved memory area.
* *
* Parameters: * Parameters:
* Inputs: addr[in]: Starting address of region to be freed. Page aligned. * Inputs: addr[in]: Starting address of region which needs to change access
* length[in]: The length of the memory to be freed in bytes. Page aligned * permission. Page aligned.
* Return: 0 on success; otherwise -1 * length[in]: The length of the memory to be manipulated in bytes.
*/ * Page aligned.
int sgx_free_rsrv_mem(void * addr, size_t length); * prot[in]: The target memory protection.
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
*/
/* Modify the access permissions of the pages in the reserved memory area. sgx_status_t
* sgx_tprotect_rsrv_mem(void *addr, size_t len, int prot);
* Parameters:
* Inputs: addr[in]: Starting address of region which needs to change access permission. Page aligned.
* length[in]: The length of the memory to be manipulated in bytes. Page aligned.
* prot[in]: The target memory protection.
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
*/
sgx_status_t sgx_tprotect_rsrv_mem(void *addr, size_t len, int prot);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -9,9 +9,11 @@
#define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
int ocall_raise(int *p_ret, int sig); int
ocall_raise(int *p_ret, int sig);
int raise(int sig) int
raise(int sig)
{ {
int ret; int ret;
@ -27,4 +29,3 @@ int raise(int sig)
} }
#endif #endif

View File

@ -11,47 +11,47 @@ extern "C" {
#endif #endif
/* Signals. */ /* Signals. */
#define SIGHUP 1 /* Hangup (POSIX). */ #define SIGHUP 1 /* Hangup (POSIX). */
#define SIGINT 2 /* Interrupt (ANSI). */ #define SIGINT 2 /* Interrupt (ANSI). */
#define SIGQUIT 3 /* Quit (POSIX). */ #define SIGQUIT 3 /* Quit (POSIX). */
#define SIGILL 4 /* Illegal instruction (ANSI). */ #define SIGILL 4 /* Illegal instruction (ANSI). */
#define SIGTRAP 5 /* Trace trap (POSIX). */ #define SIGTRAP 5 /* Trace trap (POSIX). */
#define SIGABRT 6 /* Abort (ANSI). */ #define SIGABRT 6 /* Abort (ANSI). */
#define SIGIOT 6 /* IOT trap (4.2 BSD). */ #define SIGIOT 6 /* IOT trap (4.2 BSD). */
#define SIGBUS 7 /* BUS error (4.2 BSD). */ #define SIGBUS 7 /* BUS error (4.2 BSD). */
#define SIGFPE 8 /* Floating-point exception (ANSI). */ #define SIGFPE 8 /* Floating-point exception (ANSI). */
#define SIGKILL 9 /* Kill, unblockable (POSIX). */ #define SIGKILL 9 /* Kill, unblockable (POSIX). */
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */ #define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
#define SIGSEGV 11 /* Segmentation violation (ANSI). */ #define SIGSEGV 11 /* Segmentation violation (ANSI). */
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */ #define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
#define SIGPIPE 13 /* Broken pipe (POSIX). */ #define SIGPIPE 13 /* Broken pipe (POSIX). */
#define SIGALRM 14 /* Alarm clock (POSIX). */ #define SIGALRM 14 /* Alarm clock (POSIX). */
#define SIGTERM 15 /* Termination (ANSI). */ #define SIGTERM 15 /* Termination (ANSI). */
#define SIGSTKFLT 16 /* Stack fault. */ #define SIGSTKFLT 16 /* Stack fault. */
#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */ #define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */
#define SIGCHLD 17 /* Child status has changed (POSIX). */ #define SIGCHLD 17 /* Child status has changed (POSIX). */
#define SIGCONT 18 /* Continue (POSIX). */ #define SIGCONT 18 /* Continue (POSIX). */
#define SIGSTOP 19 /* Stop, unblockable (POSIX). */ #define SIGSTOP 19 /* Stop, unblockable (POSIX). */
#define SIGTSTP 20 /* Keyboard stop (POSIX). */ #define SIGTSTP 20 /* Keyboard stop (POSIX). */
#define SIGTTIN 21 /* Background read from tty (POSIX). */ #define SIGTTIN 21 /* Background read from tty (POSIX). */
#define SIGTTOU 22 /* Background write to tty (POSIX). */ #define SIGTTOU 22 /* Background write to tty (POSIX). */
#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */ #define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */
#define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */ #define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */
#define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */ #define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */
#define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */ #define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */
#define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */ #define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */
#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */ #define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */
#define SIGPOLL SIGIO /* Pollable event occurred (System V). */ #define SIGPOLL SIGIO /* Pollable event occurred (System V). */
#define SIGIO 29 /* I/O now possible (4.2 BSD). */ #define SIGIO 29 /* I/O now possible (4.2 BSD). */
#define SIGPWR 30 /* Power failure restart (System V). */ #define SIGPWR 30 /* Power failure restart (System V). */
#define SIGSYS 31 /* Bad system call. */ #define SIGSYS 31 /* Bad system call. */
#define SIGUNUSED 31 #define SIGUNUSED 31
int raise(int sig); int
raise(int sig);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _SGX_SIGNAL_H */ #endif /* end of _SGX_SIGNAL_H */

View File

@ -9,18 +9,23 @@
#define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
int ocall_socket(int *p_ret, int domain, int type, int protocol); int
int ocall_getsockopt(int *p_ret, int sockfd, int level, int optname, ocall_socket(int *p_ret, int domain, int type, int protocol);
void *val_buf, unsigned int val_buf_size, int
void *len_buf); ocall_getsockopt(int *p_ret, int sockfd, int level, int optname, void *val_buf,
unsigned int val_buf_size, void *len_buf);
int ocall_sendmsg(ssize_t *p_ret, int sockfd, void *msg_buf, int
unsigned int msg_buf_size, int flags); ocall_sendmsg(ssize_t *p_ret, int sockfd, void *msg_buf,
int ocall_recvmsg(ssize_t *p_ret, int sockfd, void *msg_buf, unsigned int msg_buf_size, int flags);
unsigned int msg_buf_size, int flags); int
int ocall_shutdown(int *p_ret, int sockfd, int how); ocall_recvmsg(ssize_t *p_ret, int sockfd, void *msg_buf,
unsigned int msg_buf_size, int flags);
int
ocall_shutdown(int *p_ret, int sockfd, int how);
int socket(int domain, int type, int protocol) int
socket(int domain, int type, int protocol)
{ {
int ret; int ret;
@ -35,14 +40,15 @@ int socket(int domain, int type, int protocol)
return ret; return ret;
} }
int getsockopt(int sockfd, int level, int optname, int
void *optval, socklen_t *optlen) getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen)
{ {
int ret; int ret;
unsigned int val_buf_size = *optlen; unsigned int val_buf_size = *optlen;
if (ocall_getsockopt(&ret, sockfd, level, optname, optval, if (ocall_getsockopt(&ret, sockfd, level, optname, optval, val_buf_size,
val_buf_size, (void *)optlen) != SGX_SUCCESS) { (void *)optlen)
!= SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -53,7 +59,8 @@ int getsockopt(int sockfd, int level, int optname,
return ret; return ret;
} }
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) ssize_t
sendmsg(int sockfd, const struct msghdr *msg, int flags)
{ {
ssize_t ret; ssize_t ret;
int i; int i;
@ -77,7 +84,7 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
if (msg1 == NULL) if (msg1 == NULL)
return -1; return -1;
p = (char*)(uintptr_t)sizeof(struct msghdr); p = (char *)(uintptr_t)sizeof(struct msghdr);
if (msg->msg_name != NULL) { if (msg->msg_name != NULL) {
msg1->msg_name = p; msg1->msg_name = p;
@ -106,8 +113,8 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
} }
} }
if (ocall_sendmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, if (ocall_sendmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, flags)
flags) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -118,7 +125,8 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
return ret; return ret;
} }
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) ssize_t
recvmsg(int sockfd, struct msghdr *msg, int flags)
{ {
ssize_t ret; ssize_t ret;
int i; int i;
@ -144,7 +152,7 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
memset(msg1, 0, total_size); memset(msg1, 0, total_size);
p = (char*)(uintptr_t)sizeof(struct msghdr); p = (char *)(uintptr_t)sizeof(struct msghdr);
if (msg->msg_name != NULL) { if (msg->msg_name != NULL) {
msg1->msg_name = p; msg1->msg_name = p;
@ -167,8 +175,8 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
} }
} }
if (ocall_recvmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, if (ocall_recvmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, flags)
flags) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -203,7 +211,8 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
return ret; return ret;
} }
int shutdown(int sockfd, int how) int
shutdown(int sockfd, int how)
{ {
int ret; int ret;
@ -219,4 +228,3 @@ int shutdown(int sockfd, int how)
} }
#endif #endif

View File

@ -12,32 +12,32 @@
extern "C" { extern "C" {
#endif #endif
#define SOL_SOCKET 1 #define SOL_SOCKET 1
#define SOCK_STREAM 1 #define SOCK_STREAM 1
#define SOCK_DGRAM 2 #define SOCK_DGRAM 2
#define SO_TYPE 3 #define SO_TYPE 3
#define MSG_OOB 0x0001 #define MSG_OOB 0x0001
#define MSG_PEEK 0x0002 #define MSG_PEEK 0x0002
#define MSG_DONTROUTE 0x0004 #define MSG_DONTROUTE 0x0004
#define MSG_CTRUNC 0x0008 #define MSG_CTRUNC 0x0008
#define MSG_PROXY 0x0010 #define MSG_PROXY 0x0010
#define MSG_TRUNC 0x0020 #define MSG_TRUNC 0x0020
#define MSG_DONTWAIT 0x0040 #define MSG_DONTWAIT 0x0040
#define MSG_EOR 0x0080 #define MSG_EOR 0x0080
#define MSG_WAITALL 0x0100 #define MSG_WAITALL 0x0100
#define MSG_FIN 0x0200 #define MSG_FIN 0x0200
#define MSG_SYN 0x0400 #define MSG_SYN 0x0400
#define MSG_CONFIRM 0x0800 #define MSG_CONFIRM 0x0800
#define MSG_RST 0x1000 #define MSG_RST 0x1000
#define MSG_ERRQUEUE 0x2000 #define MSG_ERRQUEUE 0x2000
#define MSG_NOSIGNAL 0x4000 #define MSG_NOSIGNAL 0x4000
#define MSG_MORE 0x8000 #define MSG_MORE 0x8000
#define MSG_WAITFORONE 0x10000 #define MSG_WAITFORONE 0x10000
#define MSG_BATCH 0x40000 #define MSG_BATCH 0x40000
#define MSG_FASTOPEN 0x20000000 #define MSG_FASTOPEN 0x20000000
#define MSG_CMSG_CLOEXEC 0x40000000 #define MSG_CMSG_CLOEXEC 0x40000000
#define SHUT_RD 0 #define SHUT_RD 0
@ -54,21 +54,23 @@ struct msghdr {
int msg_flags; int msg_flags;
}; };
int
socket(int domain, int type, int protocol);
int socket(int domain, int type, int protocol); int
getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
int getsockopt(int sockfd, int level, int optname, ssize_t
void *optval, socklen_t *optlen); sendmsg(int sockfd, const struct msghdr *msg, int flags);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); ssize_t
recvmsg(int sockfd, struct msghdr *msg, int flags);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); int
shutdown(int sockfd, int how);
int shutdown(int sockfd, int how);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _SGX_SOCKET_H */ #endif /* end of _SGX_SOCKET_H */

View File

@ -12,9 +12,10 @@ typedef struct {
void *arg; void *arg;
} thread_wrapper_arg; } thread_wrapper_arg;
static void *os_thread_wrapper(void *arg) static void *
os_thread_wrapper(void *arg)
{ {
thread_wrapper_arg * targ = arg; thread_wrapper_arg *targ = arg;
thread_start_routine_t start_func = targ->start; thread_start_routine_t start_func = targ->start;
void *thread_arg = targ->arg; void *thread_arg = targ->arg;
os_printf("THREAD CREATED %p\n", &targ); os_printf("THREAD CREATED %p\n", &targ);
@ -23,15 +24,16 @@ static void *os_thread_wrapper(void *arg)
return NULL; return NULL;
} }
int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start, int
void *arg, unsigned int stack_size, int prio) os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{ {
thread_wrapper_arg *targ; thread_wrapper_arg *targ;
assert(tid); assert(tid);
assert(start); assert(start);
targ = (thread_wrapper_arg *) BH_MALLOC(sizeof(*targ)); targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
if (!targ) { if (!targ) {
return BHT_ERROR; return BHT_ERROR;
} }
@ -47,15 +49,17 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_OK; return BHT_OK;
} }
int os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg, int
unsigned int stack_size) os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{ {
return os_thread_create_with_prio(tid, start, arg, stack_size, return os_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
} }
#endif #endif
korp_tid os_self_thread() korp_tid
os_self_thread()
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
return pthread_self(); return pthread_self();
@ -64,7 +68,8 @@ korp_tid os_self_thread()
#endif #endif
} }
int os_mutex_init(korp_mutex *mutex) int
os_mutex_init(korp_mutex *mutex)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
@ -73,7 +78,8 @@ int os_mutex_init(korp_mutex *mutex)
return BHT_OK; return BHT_OK;
} }
int os_mutex_destroy(korp_mutex *mutex) int
os_mutex_destroy(korp_mutex *mutex)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
pthread_mutex_destroy(mutex); pthread_mutex_destroy(mutex);
@ -81,7 +87,8 @@ int os_mutex_destroy(korp_mutex *mutex)
return BHT_OK; return BHT_OK;
} }
int os_mutex_lock(korp_mutex *mutex) int
os_mutex_lock(korp_mutex *mutex)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
return pthread_mutex_lock(mutex); return pthread_mutex_lock(mutex);
@ -90,7 +97,8 @@ int os_mutex_lock(korp_mutex *mutex)
#endif #endif
} }
int os_mutex_unlock(korp_mutex *mutex) int
os_mutex_unlock(korp_mutex *mutex)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
return pthread_mutex_unlock(mutex); return pthread_mutex_unlock(mutex);
@ -99,7 +107,8 @@ int os_mutex_unlock(korp_mutex *mutex)
#endif #endif
} }
int os_cond_init(korp_cond *cond) int
os_cond_init(korp_cond *cond)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
pthread_cond_t c = PTHREAD_COND_INITIALIZER; pthread_cond_t c = PTHREAD_COND_INITIALIZER;
@ -108,7 +117,8 @@ int os_cond_init(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_cond_destroy(korp_cond *cond) int
os_cond_destroy(korp_cond *cond)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
pthread_cond_destroy(cond); pthread_cond_destroy(cond);
@ -116,7 +126,8 @@ int os_cond_destroy(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_cond_wait(korp_cond *cond, korp_mutex *mutex) int
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
assert(cond); assert(cond);
@ -129,14 +140,16 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex)
return BHT_OK; return BHT_OK;
} }
int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
{ {
os_printf("warning: SGX pthread_cond_timedwait isn't supported, " os_printf("warning: SGX pthread_cond_timedwait isn't supported, "
"calling pthread_cond_wait instead!\n"); "calling pthread_cond_wait instead!\n");
return BHT_ERROR; return BHT_ERROR;
} }
int os_cond_signal(korp_cond *cond) int
os_cond_signal(korp_cond *cond)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
assert(cond); assert(cond);
@ -148,7 +161,8 @@ int os_cond_signal(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
int os_thread_join(korp_tid thread, void **value_ptr) int
os_thread_join(korp_tid thread, void **value_ptr)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
return pthread_join(thread, value_ptr); return pthread_join(thread, value_ptr);
@ -157,13 +171,15 @@ int os_thread_join(korp_tid thread, void **value_ptr)
#endif #endif
} }
int os_thread_detach(korp_tid thread) int
os_thread_detach(korp_tid thread)
{ {
/* SGX pthread_detach isn't provided, return directly. */ /* SGX pthread_detach isn't provided, return directly. */
return 0; return 0;
} }
void os_thread_exit(void *retval) void
os_thread_exit(void *retval)
{ {
#ifndef SGX_DISABLE_PTHREAD #ifndef SGX_DISABLE_PTHREAD
pthread_exit(retval); pthread_exit(retval);
@ -172,9 +188,9 @@ void os_thread_exit(void *retval)
#endif #endif
} }
uint8 *os_thread_get_stack_boundary() uint8 *
os_thread_get_stack_boundary()
{ {
/* TODO: get sgx stack boundary */ /* TODO: get sgx stack boundary */
return NULL; return NULL;
} }

View File

@ -8,18 +8,22 @@
#define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__) #define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__)
#define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
int ocall_clock_gettime(int *p_ret, unsigned clock_id, int
void *tp_buf, unsigned int tp_buf_size); ocall_clock_gettime(int *p_ret, unsigned clock_id, void *tp_buf,
int ocall_clock_getres(int *p_ret, int clock_id, unsigned int tp_buf_size);
void *res_buf, unsigned int res_buf_size); int
int ocall_utimensat(int *p_ret, int dirfd, const char *pathname, ocall_clock_getres(int *p_ret, int clock_id, void *res_buf,
const void *times_buf, unsigned int times_buf_size, unsigned int res_buf_size);
int flags); int
int ocall_futimens(int *p_ret, int fd, ocall_utimensat(int *p_ret, int dirfd, const char *pathname,
const void *times_buf, unsigned int times_buf_size); const void *times_buf, unsigned int times_buf_size, int flags);
int ocall_clock_nanosleep(int *p_ret, unsigned clock_id, int flags, int
const void *req_buf, unsigned int req_buf_size, ocall_futimens(int *p_ret, int fd, const void *times_buf,
const void *rem_buf, unsigned int rem_buf_size); unsigned int times_buf_size);
int
ocall_clock_nanosleep(int *p_ret, unsigned clock_id, int flags,
const void *req_buf, unsigned int req_buf_size,
const void *rem_buf, unsigned int rem_buf_size);
uint64 uint64
os_time_get_boot_microsecond() os_time_get_boot_microsecond()
@ -30,12 +34,13 @@ os_time_get_boot_microsecond()
#ifndef SGX_DISABLE_WASI #ifndef SGX_DISABLE_WASI
int clock_getres(int clock_id, struct timespec *res) int
clock_getres(int clock_id, struct timespec *res)
{ {
int ret; int ret;
if (ocall_clock_getres(&ret, clock_id, (void *)res, if (ocall_clock_getres(&ret, clock_id, (void *)res, sizeof(struct timespec))
sizeof(struct timespec)) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -46,12 +51,13 @@ int clock_getres(int clock_id, struct timespec *res)
return ret; return ret;
} }
int clock_gettime(clockid_t clock_id, struct timespec *tp) int
clock_gettime(clockid_t clock_id, struct timespec *tp)
{ {
int ret; int ret;
if(ocall_clock_gettime(&ret, clock_id, (void *)tp, if (ocall_clock_gettime(&ret, clock_id, (void *)tp, sizeof(struct timespec))
sizeof(struct timespec)) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -62,14 +68,15 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
return ret; return ret;
} }
int utimensat(int dirfd, const char *pathname, int
const struct timespec times[2], int flags) utimensat(int dirfd, const char *pathname, const struct timespec times[2],
int flags)
{ {
int ret; int ret;
if (ocall_utimensat(&ret, dirfd, pathname, (void *)times, if (ocall_utimensat(&ret, dirfd, pathname, (void *)times,
sizeof(struct timespec) * 2, sizeof(struct timespec) * 2, flags)
flags) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -80,12 +87,13 @@ int utimensat(int dirfd, const char *pathname,
return ret; return ret;
} }
int futimens(int fd, const struct timespec times[2]) int
futimens(int fd, const struct timespec times[2])
{ {
int ret; int ret;
if (ocall_futimens(&ret, fd, (void *)times, if (ocall_futimens(&ret, fd, (void *)times, sizeof(struct timespec) * 2)
sizeof(struct timespec) * 2) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -96,16 +104,16 @@ int futimens(int fd, const struct timespec times[2])
return ret; return ret;
} }
int clock_nanosleep(clockid_t clock_id, int flags, int
const struct timespec *request, clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
struct timespec *remain) struct timespec *remain)
{ {
int ret; int ret;
if (ocall_clock_nanosleep(&ret, clock_id, flags, (void *)request, if (ocall_clock_nanosleep(&ret, clock_id, flags, (void *)request,
sizeof(struct timespec), sizeof(struct timespec), (void *)remain,
(void *)remain, sizeof(struct timespec))
sizeof(struct timespec)) != SGX_SUCCESS) { != SGX_SUCCESS) {
TRACE_OCALL_FAIL(); TRACE_OCALL_FAIL();
return -1; return -1;
} }
@ -117,4 +125,3 @@ int clock_nanosleep(clockid_t clock_id, int flags,
} }
#endif #endif

View File

@ -10,12 +10,12 @@
extern "C" { extern "C" {
#endif #endif
#define CLOCK_REALTIME 0 #define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1 #define CLOCK_MONOTONIC 1
#define CLOCK_PROCESS_CPUTIME_ID 2 #define CLOCK_PROCESS_CPUTIME_ID 2
#define CLOCK_THREAD_CPUTIME_ID 3 #define CLOCK_THREAD_CPUTIME_ID 3
#define UTIME_NOW 0x3fffffff #define UTIME_NOW 0x3fffffff
#define UTIME_OMIT 0x3ffffffe #define UTIME_OMIT 0x3ffffffe
#define TIMER_ABSTIME 1 #define TIMER_ABSTIME 1
@ -28,20 +28,23 @@ struct timespec {
long tv_nsec; long tv_nsec;
}; };
int clock_getres(int clock_id, struct timespec *res); int
clock_getres(int clock_id, struct timespec *res);
int clock_gettime(clockid_t clock_id, struct timespec *tp); int
clock_gettime(clockid_t clock_id, struct timespec *tp);
int utimensat(int dirfd, const char *pathname, int
const struct timespec times[2], int flags); utimensat(int dirfd, const char *pathname, const struct timespec times[2],
int futimens(int fd, const struct timespec times[2]); int flags);
int clock_nanosleep(clockid_t clock_id, int flags, int
const struct timespec *request, futimens(int fd, const struct timespec times[2]);
struct timespec *remain); int
clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
struct timespec *remain);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _SGX_TIME_H */ #endif /* end of _SGX_TIME_H */

View File

@ -19,8 +19,8 @@
#include <poll.h> #include <poll.h>
#include <errno.h> #include <errno.h>
int ocall_open(const char *pathname, int flags, int
bool has_mode, unsigned mode) ocall_open(const char *pathname, int flags, bool has_mode, unsigned mode)
{ {
if (has_mode) { if (has_mode) {
return open(pathname, flags, (mode_t)mode); return open(pathname, flags, (mode_t)mode);
@ -30,9 +30,9 @@ int ocall_open(const char *pathname, int flags,
} }
} }
int ocall_openat(int dirfd, int
const char *pathname, int flags, ocall_openat(int dirfd, const char *pathname, int flags, bool has_mode,
bool has_mode, unsigned mode) unsigned mode)
{ {
if (has_mode) { if (has_mode) {
return openat(dirfd, pathname, flags, (mode_t)mode); return openat(dirfd, pathname, flags, (mode_t)mode);
@ -42,12 +42,14 @@ int ocall_openat(int dirfd,
} }
} }
int ocall_close(int fd) int
ocall_close(int fd)
{ {
return close(fd); return close(fd);
} }
ssize_t ocall_read(int fd, void *buf, size_t read_size) ssize_t
ocall_read(int fd, void *buf, size_t read_size)
{ {
if (buf != NULL) { if (buf != NULL) {
return read(fd, buf, read_size); return read(fd, buf, read_size);
@ -57,45 +59,53 @@ ssize_t ocall_read(int fd, void *buf, size_t read_size)
} }
} }
off_t ocall_lseek(int fd, off_t offset, int whence) off_t
ocall_lseek(int fd, off_t offset, int whence)
{ {
return lseek(fd, offset, whence); return lseek(fd, offset, whence);
} }
int ocall_ftruncate(int fd, off_t length) int
ocall_ftruncate(int fd, off_t length)
{ {
return ftruncate(fd, length); return ftruncate(fd, length);
} }
int ocall_fsync(int fd) int
ocall_fsync(int fd)
{ {
return fsync(fd); return fsync(fd);
} }
int ocall_fdatasync(int fd) int
ocall_fdatasync(int fd)
{ {
return fdatasync(fd); return fdatasync(fd);
} }
int ocall_isatty(int fd) int
ocall_isatty(int fd)
{ {
return isatty(fd); return isatty(fd);
} }
void ocall_fdopendir(int fd, void **dirp) void
ocall_fdopendir(int fd, void **dirp)
{ {
if (dirp) { if (dirp) {
*(DIR **)dirp = fdopendir(fd); *(DIR **)dirp = fdopendir(fd);
} }
} }
void *ocall_readdir(void *dirp) void *
ocall_readdir(void *dirp)
{ {
DIR *p_dirp = (DIR *)dirp; DIR *p_dirp = (DIR *)dirp;
return readdir(p_dirp); return readdir(p_dirp);
} }
void ocall_rewinddir(void *dirp) void
ocall_rewinddir(void *dirp)
{ {
DIR *p_dirp = (DIR *)dirp; DIR *p_dirp = (DIR *)dirp;
if (p_dirp) { if (p_dirp) {
@ -103,7 +113,8 @@ void ocall_rewinddir(void *dirp)
} }
} }
void ocall_seekdir(void *dirp, long loc) void
ocall_seekdir(void *dirp, long loc)
{ {
DIR *p_dirp = (DIR *)dirp; DIR *p_dirp = (DIR *)dirp;
@ -112,7 +123,8 @@ void ocall_seekdir(void *dirp, long loc)
} }
} }
long ocall_telldir(void *dirp) long
ocall_telldir(void *dirp)
{ {
DIR *p_dirp = (DIR *)dirp; DIR *p_dirp = (DIR *)dirp;
if (p_dirp) { if (p_dirp) {
@ -121,7 +133,8 @@ long ocall_telldir(void *dirp)
return -1; return -1;
} }
int ocall_closedir(void* dirp) int
ocall_closedir(void *dirp)
{ {
DIR *p_dirp = (DIR *)dirp; DIR *p_dirp = (DIR *)dirp;
if (p_dirp) { if (p_dirp) {
@ -130,82 +143,91 @@ int ocall_closedir(void* dirp)
return -1; return -1;
} }
int ocall_stat(const char *pathname, int
void *buf, unsigned int buf_len) ocall_stat(const char *pathname, void *buf, unsigned int buf_len)
{ {
return stat(pathname, (struct stat *)buf); return stat(pathname, (struct stat *)buf);
} }
int ocall_fstat(int fd, void *buf, unsigned int buf_len) int
ocall_fstat(int fd, void *buf, unsigned int buf_len)
{ {
return fstat(fd, (struct stat *)buf); return fstat(fd, (struct stat *)buf);
} }
int ocall_fstatat(int dirfd, const char *pathname, void *buf, int
unsigned int buf_len, int flags) ocall_fstatat(int dirfd, const char *pathname, void *buf, unsigned int buf_len,
int flags)
{ {
return fstatat(dirfd, pathname, (struct stat *)buf, flags); return fstatat(dirfd, pathname, (struct stat *)buf, flags);
} }
int ocall_mkdirat(int dirfd, const char *pathname, unsigned mode) int
ocall_mkdirat(int dirfd, const char *pathname, unsigned mode)
{ {
return mkdirat(dirfd, pathname, (mode_t)mode); return mkdirat(dirfd, pathname, (mode_t)mode);
} }
int ocall_link(const char *oldpath, const char *newpath) int
ocall_link(const char *oldpath, const char *newpath)
{ {
return link(oldpath, newpath); return link(oldpath, newpath);
} }
int ocall_linkat(int olddirfd, const char *oldpath, int
int newdirfd, const char *newpath, int flags) ocall_linkat(int olddirfd, const char *oldpath, int newdirfd,
const char *newpath, int flags)
{ {
return linkat(olddirfd, oldpath, newdirfd, newpath, flags); return linkat(olddirfd, oldpath, newdirfd, newpath, flags);
} }
int ocall_unlinkat(int dirfd, const char *pathname, int flags) int
ocall_unlinkat(int dirfd, const char *pathname, int flags)
{ {
return unlinkat(dirfd, pathname, flags); return unlinkat(dirfd, pathname, flags);
} }
ssize_t ocall_readlinkat(int dirfd, const char *pathname, ssize_t
char *buf, size_t bufsiz) ocall_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
{ {
return readlinkat(dirfd, pathname, buf, bufsiz); return readlinkat(dirfd, pathname, buf, bufsiz);
} }
int ocall_renameat(int olddirfd, const char *oldpath, int
int newdirfd, const char *newpath) ocall_renameat(int olddirfd, const char *oldpath, int newdirfd,
const char *newpath)
{ {
return renameat(olddirfd, oldpath, newdirfd, newpath); return renameat(olddirfd, oldpath, newdirfd, newpath);
} }
int ocall_symlinkat(const char *target, int newdirfd, int
const char *linkpath) ocall_symlinkat(const char *target, int newdirfd, const char *linkpath)
{ {
return symlinkat(target, newdirfd, linkpath); return symlinkat(target, newdirfd, linkpath);
} }
int ocall_ioctl(int fd, unsigned long request, int
void *arg, unsigned int arg_len) ocall_ioctl(int fd, unsigned long request, void *arg, unsigned int arg_len)
{ {
/* support just int *arg temporally */ /* support just int *arg temporally */
return ioctl(fd, request, (int *)arg); return ioctl(fd, request, (int *)arg);
} }
int ocall_fcntl(int fd, int cmd) int
ocall_fcntl(int fd, int cmd)
{ {
return fcntl(fd, cmd); return fcntl(fd, cmd);
} }
int ocall_fcntl_long(int fd, int cmd, long arg) int
ocall_fcntl_long(int fd, int cmd, long arg)
{ {
return fcntl(fd, cmd, arg); return fcntl(fd, cmd, arg);
} }
ssize_t ocall_readv(int fd, char *iov_buf, ssize_t
unsigned int buf_size, int iovcnt, ocall_readv(int fd, char *iov_buf, unsigned int buf_size, int iovcnt,
bool has_offset, off_t offset) bool has_offset, off_t offset)
{ {
struct iovec *iov = (struct iovec *)iov_buf; struct iovec *iov = (struct iovec *)iov_buf;
ssize_t ret; ssize_t ret;
@ -223,9 +245,9 @@ ssize_t ocall_readv(int fd, char *iov_buf,
return ret; return ret;
} }
ssize_t ocall_writev(int fd, char *iov_buf, ssize_t
unsigned int buf_size, int iovcnt, ocall_writev(int fd, char *iov_buf, unsigned int buf_size, int iovcnt,
bool has_offset, off_t offset) bool has_offset, off_t offset)
{ {
struct iovec *iov = (struct iovec *)iov_buf; struct iovec *iov = (struct iovec *)iov_buf;
int i; int i;
@ -243,9 +265,10 @@ ssize_t ocall_writev(int fd, char *iov_buf,
return ret; return ret;
} }
int ocall_realpath(const char *path, char *buf, unsigned int buf_len) int
ocall_realpath(const char *path, char *buf, unsigned int buf_len)
{ {
char* val = NULL; char *val = NULL;
val = realpath(path, buf); val = realpath(path, buf);
if (val != NULL) { if (val != NULL) {
return 0; return 0;
@ -253,47 +276,53 @@ int ocall_realpath(const char *path, char *buf, unsigned int buf_len)
return -1; return -1;
} }
int ocall_posix_fallocate(int fd, off_t offset, off_t len) int
ocall_posix_fallocate(int fd, off_t offset, off_t len)
{ {
return posix_fallocate(fd, offset, len); return posix_fallocate(fd, offset, len);
} }
int ocall_poll(void *fds, unsigned nfds, int timeout, int
unsigned int fds_len) ocall_poll(void *fds, unsigned nfds, int timeout, unsigned int fds_len)
{ {
return poll((struct pollfd *)fds, (nfds_t)nfds, timeout); return poll((struct pollfd *)fds, (nfds_t)nfds, timeout);
} }
int ocall_getopt(int argc, char *argv_buf, unsigned int argv_buf_len, int
const char *optstring) ocall_getopt(int argc, char *argv_buf, unsigned int argv_buf_len,
const char *optstring)
{ {
int ret; int ret;
int i; int i;
char **argv = (char **)argv_buf; char **argv = (char **)argv_buf;
for (i=0; i < argc; i++) { for (i = 0; i < argc; i++) {
argv[i] = argv_buf + (uintptr_t)argv[i]; argv[i] = argv_buf + (uintptr_t)argv[i];
} }
return getopt(argc, argv, optstring); return getopt(argc, argv, optstring);
} }
ssize_t ocall_getrandom(void *buf, size_t buflen, unsigned int flags) ssize_t
ocall_getrandom(void *buf, size_t buflen, unsigned int flags)
{ {
return getrandom(buf, buflen, flags); return getrandom(buf, buflen, flags);
} }
int ocall_getentropy(void *buffer, size_t length) int
ocall_getentropy(void *buffer, size_t length)
{ {
return getentropy(buffer, length); return getentropy(buffer, length);
} }
int ocall_sched_yield() int
ocall_sched_yield()
{ {
return sched_yield(); return sched_yield();
} }
int ocall_get_errno() int
ocall_get_errno()
{ {
return errno; return errno;
} }

View File

@ -6,7 +6,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
int ocall_pthread_rwlock_init(void **rwlock, void *attr) int
ocall_pthread_rwlock_init(void **rwlock, void *attr)
{ {
int ret = 0; int ret = 0;
@ -15,7 +16,7 @@ int ocall_pthread_rwlock_init(void **rwlock, void *attr)
return -1; return -1;
ret = pthread_rwlock_init((pthread_rwlock_t *)*rwlock, NULL); ret = pthread_rwlock_init((pthread_rwlock_t *)*rwlock, NULL);
if (ret != 0) { if (ret != 0) {
free(*rwlock); free(*rwlock);
*rwlock = NULL; *rwlock = NULL;
} }
@ -23,7 +24,8 @@ int ocall_pthread_rwlock_init(void **rwlock, void *attr)
return ret; return ret;
} }
int ocall_pthread_rwlock_destroy(void *rwlock) int
ocall_pthread_rwlock_destroy(void *rwlock)
{ {
pthread_rwlock_t *lock = (pthread_rwlock_t *)rwlock; pthread_rwlock_t *lock = (pthread_rwlock_t *)rwlock;
int ret; int ret;
@ -33,18 +35,20 @@ int ocall_pthread_rwlock_destroy(void *rwlock)
return ret; return ret;
} }
int ocall_pthread_rwlock_rdlock(void *rwlock) int
ocall_pthread_rwlock_rdlock(void *rwlock)
{ {
return pthread_rwlock_rdlock((pthread_rwlock_t *)rwlock); return pthread_rwlock_rdlock((pthread_rwlock_t *)rwlock);
} }
int ocall_pthread_rwlock_wrlock(void *rwlock) int
ocall_pthread_rwlock_wrlock(void *rwlock)
{ {
return pthread_rwlock_wrlock((pthread_rwlock_t *)rwlock); return pthread_rwlock_wrlock((pthread_rwlock_t *)rwlock);
} }
int ocall_pthread_rwlock_unlock(void *rwlock) int
ocall_pthread_rwlock_unlock(void *rwlock)
{ {
return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock); return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock);
} }

View File

@ -4,7 +4,8 @@
*/ */
#include <signal.h> #include <signal.h>
int ocall_raise(int sig) int
ocall_raise(int sig)
{ {
return raise(sig); return raise(sig);
} }

View File

@ -7,20 +7,21 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
int ocall_socket(int domain, int type, int protocol) int
ocall_socket(int domain, int type, int protocol)
{ {
return socket(domain, type, protocol); return socket(domain, type, protocol);
} }
int ocall_getsockopt(int sockfd, int level, int optname, void *val_buf, int
unsigned int val_buf_size, void *len_buf) ocall_getsockopt(int sockfd, int level, int optname, void *val_buf,
unsigned int val_buf_size, void *len_buf)
{ {
return getsockopt(sockfd, level, optname, val_buf, return getsockopt(sockfd, level, optname, val_buf, (socklen_t *)len_buf);
(socklen_t *)len_buf);
} }
ssize_t ocall_sendmsg(int sockfd, void *msg_buf, ssize_t
unsigned int msg_buf_size, int flags) ocall_sendmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size, int flags)
{ {
struct msghdr *msg = (struct msghdr *)msg_buf; struct msghdr *msg = (struct msghdr *)msg_buf;
int i; int i;
@ -35,16 +36,16 @@ ssize_t ocall_sendmsg(int sockfd, void *msg_buf,
if (msg->msg_iov != NULL) { if (msg->msg_iov != NULL) {
msg->msg_iov = msg_buf + (unsigned)(uintptr_t)msg->msg_iov; msg->msg_iov = msg_buf + (unsigned)(uintptr_t)msg->msg_iov;
for (i = 0; i < msg->msg_iovlen; i++) { for (i = 0; i < msg->msg_iovlen; i++) {
msg->msg_iov[i].iov_base = msg_buf + (unsigned)(uintptr_t) msg->msg_iov[i].iov_base =
msg->msg_iov[i].iov_base; msg_buf + (unsigned)(uintptr_t)msg->msg_iov[i].iov_base;
} }
} }
return sendmsg(sockfd, msg, flags); return sendmsg(sockfd, msg, flags);
} }
ssize_t ocall_recvmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size, ssize_t
int flags) ocall_recvmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size, int flags)
{ {
struct msghdr *msg = (struct msghdr *)msg_buf; struct msghdr *msg = (struct msghdr *)msg_buf;
int i; int i;
@ -58,16 +59,17 @@ ssize_t ocall_recvmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size,
if (msg->msg_iov != NULL) { if (msg->msg_iov != NULL) {
msg->msg_iov = msg_buf + (unsigned)(uintptr_t)msg->msg_iov; msg->msg_iov = msg_buf + (unsigned)(uintptr_t)msg->msg_iov;
for (i = 0; i <msg->msg_iovlen; i++) { for (i = 0; i < msg->msg_iovlen; i++) {
msg->msg_iov[i].iov_base = msg_buf + (unsigned)(uintptr_t) msg->msg_iov[i].iov_base =
msg->msg_iov[i].iov_base; msg_buf + (unsigned)(uintptr_t)msg->msg_iov[i].iov_base;
} }
} }
return recvmsg(sockfd, msg, flags); return recvmsg(sockfd, msg, flags);
} }
int ocall_shutdown(int sockfd, int how) int
ocall_shutdown(int sockfd, int how)
{ {
return shutdown(sockfd, how); return shutdown(sockfd, how);
} }

View File

@ -7,41 +7,38 @@
#include <time.h> #include <time.h>
#include <fcntl.h> #include <fcntl.h>
/** time clock **/ /** time clock **/
int ocall_clock_gettime(unsigned clock_id, void *tp_buf, int
unsigned int tp_buf_size) ocall_clock_gettime(unsigned clock_id, void *tp_buf, unsigned int tp_buf_size)
{ {
return clock_gettime((clockid_t)clock_id, return clock_gettime((clockid_t)clock_id, (struct timespec *)tp_buf);
(struct timespec *)tp_buf);
} }
int ocall_clock_getres(int clock_id, void *res_buf, int
unsigned int res_buf_size) ocall_clock_getres(int clock_id, void *res_buf, unsigned int res_buf_size)
{ {
return clock_getres(clock_id, (struct timespec *)res_buf); return clock_getres(clock_id, (struct timespec *)res_buf);
} }
int ocall_utimensat(int dirfd, const char *pathname, int
const void *times_buf, ocall_utimensat(int dirfd, const char *pathname, const void *times_buf,
unsigned int times_buf_size, int flags) unsigned int times_buf_size, int flags)
{ {
return utimensat(dirfd, pathname, (struct timespec *)times_buf, flags); return utimensat(dirfd, pathname, (struct timespec *)times_buf, flags);
} }
int ocall_futimens(int fd, const void *times_buf, int
unsigned int times_buf_size) ocall_futimens(int fd, const void *times_buf, unsigned int times_buf_size)
{ {
return futimens(fd, (struct timespec *)times_buf); return futimens(fd, (struct timespec *)times_buf);
} }
int ocall_clock_nanosleep(unsigned clock_id, int flags, int
const void *req_buf, unsigned int req_buf_size, ocall_clock_nanosleep(unsigned clock_id, int flags, const void *req_buf,
const void *rem_buf, unsigned int rem_buf_size) unsigned int req_buf_size, const void *rem_buf,
unsigned int rem_buf_size)
{ {
return clock_nanosleep((clockid_t)clock_id, flags, return clock_nanosleep((clockid_t)clock_id, flags,
(struct timespec *)req_buf, (struct timespec *)req_buf,
(struct timespec *)rem_buf); (struct timespec *)rem_buf);
} }

View File

@ -13,8 +13,7 @@ bh_platform_init()
void void
bh_platform_destroy() bh_platform_destroy()
{ {}
}
int int
os_printf(const char *format, ...) os_printf(const char *format, ...)
@ -42,4 +41,3 @@ os_vprintf(const char *format, va_list ap)
return BH_VPRINTF(format, ap); return BH_VPRINTF(format, ap);
#endif #endif
} }

View File

@ -59,10 +59,8 @@ typedef pthread_t korp_thread;
#define os_thread_local_attribute __thread #define os_thread_local_attribute __thread
#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) \ #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_AARCH64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64) || defined(BUILD_TARGET_RISCV64_LP64)
#include <setjmp.h> #include <setjmp.h>
@ -79,15 +77,20 @@ typedef jmp_buf korp_jmpbuf;
typedef void (*os_signal_handler)(void *sig_addr); typedef void (*os_signal_handler)(void *sig_addr);
int os_thread_signal_init(os_signal_handler handler); int
os_thread_signal_init(os_signal_handler handler);
void os_thread_signal_destroy(); void
os_thread_signal_destroy();
bool os_thread_signal_inited(); bool
os_thread_signal_inited();
void os_signal_unmask(); void
os_signal_unmask();
void os_sigreturn(); void
os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
@ -96,4 +99,3 @@ void os_sigreturn();
#endif #endif
#endif /* end of _PLATFORM_INTERNAL_H */ #endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -38,7 +38,7 @@ typedef pthread_t korp_thread;
/* Default thread priority */ /* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 100 #define BH_THREAD_DEFAULT_PRIORITY 100
#define os_printf printf #define os_printf printf
#define os_vprintf vprintf #define os_vprintf vprintf
/* On NuttX, time_t is uint32_t */ /* On NuttX, time_t is uint32_t */

View File

@ -7,12 +7,12 @@
#ifndef _PLATFORM_INTERNAL_H #ifndef _PLATFORM_INTERNAL_H
#define _PLATFORM_INTERNAL_H #define _PLATFORM_INTERNAL_H
//Riot includes core /* Riot includes core */
#include <sched.h> #include <sched.h>
#include <thread.h> #include <thread.h>
#include <mutex.h> #include <mutex.h>
//Riot includes sys /* Riot includes sys */
#include <sema.h> #include <sema.h>
#include <inttypes.h> #include <inttypes.h>
@ -39,19 +39,20 @@ typedef thread_t korp_thread;
typedef kernel_pid_t korp_tid; typedef kernel_pid_t korp_tid;
typedef mutex_t korp_mutex; typedef mutex_t korp_mutex;
// typedef sema_t korp_sem; /* typedef sema_t korp_sem; */
struct os_thread_wait_node; struct os_thread_wait_node;
typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct os_thread_wait_node *os_thread_wait_list;
typedef struct korp_cond { typedef struct korp_cond {
mutex_t wait_list_lock; mutex_t wait_list_lock;
os_thread_wait_list thread_wait_list; os_thread_wait_list thread_wait_list;
} korp_cond; } korp_cond;
#define os_printf printf #define os_printf printf
#define os_vprintf vprintf #define os_vprintf vprintf
#if WA_MATH #if WA_MATH
/* clang-format off */
/* math functions which are not provided by os*/ /* math functions which are not provided by os*/
double sqrt(double x); double sqrt(double x);
double floor(double x); double floor(double x);
@ -69,7 +70,7 @@ float rintf(float x);
float truncf(float x); float truncf(float x);
int signbit(double x); int signbit(double x);
int isnan(double x); int isnan(double x);
/* clang-format on */
#endif #endif
#endif /* end of _BH_PLATFORM_H */ #endif /* end of _BH_PLATFORM_H */

View File

@ -9,19 +9,21 @@
#include <panic.h> #include <panic.h>
/* clang-format off */
#define bh_assert(v) do { \ #define bh_assert(v) do { \
if (!(v)) { \ if (!(v)) { \
printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ printf("\nASSERTION FAILED: %s, at %s, line %d\n", \
#v, __FILE__, __LINE__); \ #v, __FILE__, __LINE__); \
core_panic(0,0/*expr_string*/); \ core_panic(0, 0/*expr_string*/); \
while (1); \ while (1); \
} \ } \
} while (0) } while (0)
/* clang-format on */
struct os_thread_data; struct os_thread_data;
typedef struct os_thread_wait_node { typedef struct os_thread_wait_node {
sema_t sem; sema_t sem;
void * ret; void *ret;
os_thread_wait_list next; os_thread_wait_list next;
} os_thread_wait_node; } os_thread_wait_node;
@ -63,7 +65,8 @@ static mutex_t thread_data_lock;
/* Thread data list */ /* Thread data list */
static os_thread_data *thread_data_list = NULL; static os_thread_data *thread_data_list = NULL;
static void thread_data_list_add(os_thread_data *thread_data) static void
thread_data_list_add(os_thread_data *thread_data)
{ {
mutex_lock(&thread_data_lock); mutex_lock(&thread_data_lock);
if (!thread_data_list) if (!thread_data_list)
@ -86,7 +89,8 @@ static void thread_data_list_add(os_thread_data *thread_data)
mutex_unlock(&thread_data_lock); mutex_unlock(&thread_data_lock);
} }
static void thread_data_list_remove(os_thread_data *thread_data) static void
thread_data_list_remove(os_thread_data *thread_data)
{ {
mutex_lock(&thread_data_lock); mutex_lock(&thread_data_lock);
if (thread_data_list) { if (thread_data_list) {
@ -153,7 +157,7 @@ thread_data_current()
static void static void
os_thread_cleanup(void) os_thread_cleanup(void)
{ {
//TODO Check this (Join sema trigger, cleanup of thread_data) // TODO Check this (Join sema trigger, cleanup of thread_data)
os_thread_data *thread_data = thread_data_current(); os_thread_data *thread_data = thread_data_current();
bh_assert(thread_data != NULL); bh_assert(thread_data != NULL);
mutex_lock(&thread_data->wait_list_lock); mutex_lock(&thread_data->wait_list_lock);
@ -177,7 +181,7 @@ static void *
os_thread_wrapper(void *thread_data) os_thread_wrapper(void *thread_data)
{ {
/* Set thread custom data */ /* Set thread custom data */
os_thread_data * t = (os_thread_data*) thread_data; os_thread_data *t = (os_thread_data *)thread_data;
t->tid = thread_getpid(); t->tid = thread_getpid();
thread_data_list_add(t); thread_data_list_add(t);
@ -187,13 +191,13 @@ os_thread_wrapper(void *thread_data)
os_thread_cleanup(); // internal structures and joiners os_thread_cleanup(); // internal structures and joiners
BH_FREE(thread_data); BH_FREE(thread_data);
sched_task_exit();// stop thread //clean sched_task_exit(); // stop thread //clean
return NULL; //never reached return NULL; // never reached
} }
int int
os_thread_create(korp_tid *p_tid, thread_start_routine_t start, os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
void *arg, unsigned int stack_size) unsigned int stack_size)
{ {
return os_thread_create_with_prio(p_tid, start, arg, stack_size, return os_thread_create_with_prio(p_tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
@ -223,8 +227,8 @@ os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
thread_data->arg = arg; thread_data->arg = arg;
/* Create the thread &*/ /* Create the thread &*/
if (!((tid = thread_create( thread_data->stack, if (!((tid = thread_create(thread_data->stack, stack_size, prio, 0,
stack_size,prio,0, os_thread_wrapper, thread_data,"WASM")))) { os_thread_wrapper, thread_data, "WASM")))) {
BH_FREE(thread_data); BH_FREE(thread_data);
return BHT_ERROR; return BHT_ERROR;
} }
@ -240,15 +244,14 @@ os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
korp_tid korp_tid
os_self_thread() os_self_thread()
{ {
return (korp_tid) thread_getpid(); return (korp_tid)thread_getpid();
} }
int int
os_thread_join (korp_tid thread, void **value_ptr) os_thread_join(korp_tid thread, void **value_ptr)
{ {
// will test if thread is still working, // will test if thread is still working,
// wait if it is // wait if it is
// (void) value_ptr;
os_thread_data *thread_data; os_thread_data *thread_data;
os_thread_wait_node node; os_thread_wait_node node;
@ -257,8 +260,8 @@ os_thread_join (korp_tid thread, void **value_ptr)
/* Get thread data */ /* Get thread data */
thread_data = thread_data_list_lookup(thread); thread_data = thread_data_list_lookup(thread);
if(thread_data == NULL){ if (thread_data == NULL) {
//thread not found // thread not found
sema_destroy(&node.sem); sema_destroy(&node.sem);
return BHT_ERROR; return BHT_ERROR;
} }
@ -277,22 +280,21 @@ os_thread_join (korp_tid thread, void **value_ptr)
mutex_unlock(&thread_data->wait_list_lock); mutex_unlock(&thread_data->wait_list_lock);
sema_wait(&node.sem); sema_wait(&node.sem);
//get the return value pointer conted may not be availible after return // get the return value pointer conted may not be availible after return
if(value_ptr) (* value_ptr) = node.ret; if (value_ptr)
(*value_ptr) = node.ret;
/* Wait some time for the thread to be actually terminated */ /* Wait some time for the thread to be actually terminated */
// TODO: k_sleep(100); // TODO: k_sleep(100);
//TODO: bump target prio to make it finish and free its resources // TODO: bump target prio to make it finish and free its resources
thread_yield(); thread_yield();
//node has done its job // node has done its job
sema_destroy(&node.sem); sema_destroy(&node.sem);
return BHT_OK; return BHT_OK;
} }
// int vm_mutex_trylock(korp_mutex *mutex) // int vm_mutex_trylock(korp_mutex *mutex)
// { // {
// return mutex_trylock(mutex); // return mutex_trylock(mutex);
@ -308,7 +310,7 @@ os_mutex_init(korp_mutex *mutex)
int int
os_mutex_destroy(korp_mutex *mutex) os_mutex_destroy(korp_mutex *mutex)
{ {
(void) mutex; (void)mutex;
return BHT_OK; return BHT_OK;
} }
@ -316,15 +318,14 @@ int
os_mutex_lock(korp_mutex *mutex) os_mutex_lock(korp_mutex *mutex)
{ {
mutex_lock(mutex); mutex_lock(mutex);
return 0; //Riot mutexes do not return until success return 0; // Riot mutexes do not return until success
} }
int int
os_mutex_unlock(korp_mutex *mutex) os_mutex_unlock(korp_mutex *mutex)
{ {
mutex_unlock(mutex); mutex_unlock(mutex);
return 0; //Riot mutexes do not return until success return 0; // Riot mutexes do not return until success
} }
int int
@ -338,13 +339,13 @@ os_cond_init(korp_cond *cond)
int int
os_cond_destroy(korp_cond *cond) os_cond_destroy(korp_cond *cond)
{ {
(void) cond; (void)cond;
return BHT_OK; return BHT_OK;
} }
static int static int
os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
bool timed, uint64 useconds) uint64 useconds)
{ {
os_thread_wait_node *node; os_thread_wait_node *node;
@ -369,10 +370,10 @@ os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
/* Unlock mutex, wait sem and lock mutex again */ /* Unlock mutex, wait sem and lock mutex again */
mutex_unlock(mutex); mutex_unlock(mutex);
if(timed) if (timed)
sema_wait(&node->sem); sema_wait(&node->sem);
else else
sema_wait_timed(&node->sem,useconds); sema_wait_timed(&node->sem, useconds);
mutex_lock(mutex); mutex_lock(mutex);
/* Remove wait node from wait list */ /* Remove wait node from wait list */
@ -401,8 +402,8 @@ os_cond_wait(korp_cond *cond, korp_mutex *mutex)
int int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
{ {
return os_cond_wait_internal(cond, mutex, return os_cond_wait_internal(cond, mutex, (useconds != BHT_WAIT_FOREVER),
(useconds != BHT_WAIT_FOREVER), useconds); useconds);
} }
int int
@ -417,11 +418,12 @@ os_cond_signal(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
uint8 *os_thread_get_stack_boundary() uint8 *
os_thread_get_stack_boundary()
{ {
#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \ #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \
|| defined(MODULE_MPU_STACK_GUARD) || defined(MODULE_MPU_STACK_GUARD)
return (uint8*)thread_get_active()->stack_start; return (uint8 *)thread_get_active()->stack_start;
#else #else
return NULL; return NULL;
#endif #endif

View File

@ -12,4 +12,3 @@ os_time_get_boot_microsecond()
{ {
return xtimer_now_usec64(); return xtimer_now_usec64();
} }

View File

@ -44,5 +44,4 @@ typedef rt_int16_t int16_t;
typedef rt_uint64_t uint64_t; typedef rt_uint64_t uint64_t;
typedef rt_int64_t int64_t; typedef rt_int64_t int64_t;
#endif /* RTTHREAD_PLATFORM_INTERNAL_H */
#endif //RTTHREAD_PLATFORM_INTERNAL_H

View File

@ -8,67 +8,65 @@
#include <platform_api_extension.h> #include <platform_api_extension.h>
typedef struct os_malloc_list { typedef struct os_malloc_list {
void* real; void *real;
void* used; void *used;
rt_list_t node; rt_list_t node;
}os_malloc_list_t; } os_malloc_list_t;
int bh_platform_init(void) int
bh_platform_init(void)
{ {
return 0; return 0;
} }
void bh_platform_destroy(void) void
{ bh_platform_destroy(void)
} {}
void *os_malloc(unsigned size) void *
os_malloc(unsigned size)
{ {
void *buf_origin; void *buf_origin;
void *buf_fixed; void *buf_fixed;
rt_ubase_t *addr_field; rt_ubase_t *addr_field;
buf_origin = rt_malloc(size + 8 + sizeof(rt_ubase_t)); buf_origin = rt_malloc(size + 8 + sizeof(rt_ubase_t));
buf_fixed = buf_origin + sizeof(void*); buf_fixed = buf_origin + sizeof(void *);
if ((rt_ubase_t)buf_fixed & 0x7) if ((rt_ubase_t)buf_fixed & 0x7) {
{ buf_fixed = (void *)((rt_ubase_t)(buf_fixed + 8) & (~7));
buf_fixed = (void*)((rt_ubase_t)(buf_fixed+8)&(~7));
} }
addr_field = buf_fixed - sizeof(rt_ubase_t); addr_field = buf_fixed - sizeof(rt_ubase_t);
*addr_field = (rt_ubase_t )buf_origin; *addr_field = (rt_ubase_t)buf_origin;
return buf_fixed; return buf_fixed;
} }
void *os_realloc(void *ptr, unsigned size) void *
os_realloc(void *ptr, unsigned size)
{ {
void* mem_origin; void *mem_origin;
void* mem_new; void *mem_new;
void *mem_new_fixed; void *mem_new_fixed;
rt_ubase_t *addr_field; rt_ubase_t *addr_field;
if (!ptr) if (!ptr) {
{
return RT_NULL; return RT_NULL;
} }
addr_field = ptr - sizeof(rt_ubase_t); addr_field = ptr - sizeof(rt_ubase_t);
mem_origin = (void*)(*addr_field); mem_origin = (void *)(*addr_field);
mem_new = rt_realloc(mem_origin, size + 8 + sizeof(rt_ubase_t)); mem_new = rt_realloc(mem_origin, size + 8 + sizeof(rt_ubase_t));
if (mem_origin != mem_new) if (mem_origin != mem_new) {
{
mem_new_fixed = mem_new + sizeof(rt_ubase_t); mem_new_fixed = mem_new + sizeof(rt_ubase_t);
if ((rt_ubase_t)mem_new_fixed & 0x7) if ((rt_ubase_t)mem_new_fixed & 0x7) {
{ mem_new_fixed = (void *)((rt_ubase_t)(mem_new_fixed + 8) & (~7));
mem_new_fixed = (void*)((rt_ubase_t)(mem_new_fixed+8)&(~7));
} }
addr_field = mem_new_fixed - sizeof(rt_ubase_t); addr_field = mem_new_fixed - sizeof(rt_ubase_t);
*addr_field = (rt_ubase_t )mem_new; *addr_field = (rt_ubase_t)mem_new;
return mem_new_fixed; return mem_new_fixed;
} }
@ -76,115 +74,130 @@ void *os_realloc(void *ptr, unsigned size)
return ptr; return ptr;
} }
void os_free(void *ptr) void
os_free(void *ptr)
{ {
void* mem_origin; void *mem_origin;
rt_ubase_t *addr_field; rt_ubase_t *addr_field;
if (ptr) if (ptr) {
{
addr_field = ptr - sizeof(rt_ubase_t); addr_field = ptr - sizeof(rt_ubase_t);
mem_origin = (void*)(*addr_field); mem_origin = (void *)(*addr_field);
rt_free(mem_origin); rt_free(mem_origin);
} }
} }
static char wamr_vprint_buf[RT_CONSOLEBUF_SIZE * 2]; static char wamr_vprint_buf[RT_CONSOLEBUF_SIZE * 2];
int os_printf(const char *format, ...)
int
os_printf(const char *format, ...)
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
rt_size_t len = vsnprintf(wamr_vprint_buf, sizeof(wamr_vprint_buf)-1, format, ap); rt_size_t len =
vsnprintf(wamr_vprint_buf, sizeof(wamr_vprint_buf) - 1, format, ap);
wamr_vprint_buf[len] = 0x00; wamr_vprint_buf[len] = 0x00;
rt_kputs(wamr_vprint_buf); rt_kputs(wamr_vprint_buf);
va_end(ap); va_end(ap);
return 0; return 0;
} }
int os_vprintf(const char *format, va_list ap) int
os_vprintf(const char *format, va_list ap)
{ {
rt_size_t len = vsnprintf(wamr_vprint_buf, sizeof(wamr_vprint_buf)-1, format, ap); rt_size_t len =
vsnprintf(wamr_vprint_buf, sizeof(wamr_vprint_buf) - 1, format, ap);
wamr_vprint_buf[len] = 0; wamr_vprint_buf[len] = 0;
rt_kputs(wamr_vprint_buf); rt_kputs(wamr_vprint_buf);
return 0; return 0;
} }
uint64 os_time_get_boot_microsecond(void) uint64
os_time_get_boot_microsecond(void)
{ {
uint64 ret = rt_tick_get()*1000; uint64 ret = rt_tick_get() * 1000;
ret /= RT_TICK_PER_SECOND; ret /= RT_TICK_PER_SECOND;
return ret; return ret;
} }
korp_tid os_self_thread(void) korp_tid
os_self_thread(void)
{ {
return rt_thread_self(); return rt_thread_self();
} }
uint8 *os_thread_get_stack_boundary(void) uint8 *
os_thread_get_stack_boundary(void)
{ {
rt_thread_t tid = rt_thread_self(); rt_thread_t tid = rt_thread_self();
return tid->stack_addr; return tid->stack_addr;
} }
int os_mutex_init(korp_mutex *mutex) int
os_mutex_init(korp_mutex *mutex)
{ {
return rt_mutex_init(mutex, "wamr0", RT_IPC_FLAG_FIFO); return rt_mutex_init(mutex, "wamr0", RT_IPC_FLAG_FIFO);
} }
int os_mutex_destroy(korp_mutex *mutex) int
os_mutex_destroy(korp_mutex *mutex)
{ {
return rt_mutex_detach(mutex); return rt_mutex_detach(mutex);
} }
int os_mutex_lock(korp_mutex *mutex) int
os_mutex_lock(korp_mutex *mutex)
{ {
return rt_mutex_take(mutex, RT_WAITING_FOREVER); return rt_mutex_take(mutex, RT_WAITING_FOREVER);
} }
int os_mutex_unlock(korp_mutex *mutex) int
os_mutex_unlock(korp_mutex *mutex)
{ {
return rt_mutex_release(mutex); return rt_mutex_release(mutex);
} }
/* /*
* functions below was not implement * functions below was not implement
*/ */
int os_cond_init(korp_cond *cond) int
os_cond_init(korp_cond *cond)
{ {
return 0; return 0;
} }
int os_cond_destroy(korp_cond *cond) int
os_cond_destroy(korp_cond *cond)
{ {
return 0; return 0;
} }
int os_cond_wait(korp_cond *cond, korp_mutex *mutex) int
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
{ {
return 0; return 0;
} }
void *os_mmap(void *hint, size_t size, int prot, int flags) void *
os_mmap(void *hint, size_t size, int prot, int flags)
{ {
return rt_malloc(size); return rt_malloc(size);
} }
void os_munmap(void *addr, size_t size) void
os_munmap(void *addr, size_t size)
{ {
rt_free(addr); rt_free(addr);
} }
int os_mprotect(void *addr, size_t size, int prot) int
os_mprotect(void *addr, size_t size, int prot)
{ {
return 0; return 0;
} }
void os_dcache_flush(void) void
{ os_dcache_flush(void)
} {}

View File

@ -13,8 +13,7 @@ bh_platform_init()
void void
bh_platform_destroy() bh_platform_destroy()
{ {}
}
int int
os_printf(const char *format, ...) os_printf(const char *format, ...)
@ -42,4 +41,3 @@ os_vprintf(const char *format, va_list ap)
return BH_VPRINTF(format, ap); return BH_VPRINTF(format, ap);
#endif #endif
} }

View File

@ -58,8 +58,7 @@ typedef pthread_t korp_thread;
#define os_thread_local_attribute __thread #define os_thread_local_attribute __thread
#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) \ #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_AARCH64)
#include <setjmp.h> #include <setjmp.h>
@ -76,15 +75,20 @@ typedef jmp_buf korp_jmpbuf;
typedef void (*os_signal_handler)(void *sig_addr); typedef void (*os_signal_handler)(void *sig_addr);
int os_thread_signal_init(os_signal_handler handler); int
os_thread_signal_init(os_signal_handler handler);
void os_thread_signal_destroy(); void
os_thread_signal_destroy();
bool os_thread_signal_inited(); bool
os_thread_signal_inited();
void os_signal_unmask(); void
os_signal_unmask();
void os_sigreturn(); void
os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */ #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
@ -93,4 +97,3 @@ void os_sigreturn();
#endif #endif
#endif /* end of _PLATFORM_INTERNAL_H */ #endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -60,6 +60,4 @@ os_getpagesize()
void void
os_dcache_flush(void) os_dcache_flush(void)
{ {}
}

View File

@ -53,15 +53,17 @@ typedef struct korp_cond {
os_thread_wait_list thread_wait_list; os_thread_wait_list thread_wait_list;
} korp_cond; } korp_cond;
unsigned os_getpagesize(); unsigned
void *os_mem_commit(void *ptr, size_t size, int flags); os_getpagesize();
void os_mem_decommit(void *ptr, size_t size); void *
os_mem_commit(void *ptr, size_t size, int flags);
void
os_mem_decommit(void *ptr, size_t size);
#define os_thread_local_attribute __declspec(thread) #define os_thread_local_attribute __declspec(thread)
#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) \ #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
|| defined(BUILD_TARGET_AMD_64)
#include <setjmp.h> #include <setjmp.h>
@ -72,11 +74,14 @@ typedef jmp_buf korp_jmpbuf;
#define os_setjmp setjmp #define os_setjmp setjmp
#define os_longjmp longjmp #define os_longjmp longjmp
int os_thread_signal_init(); int
os_thread_signal_init();
void os_thread_signal_destroy(); void
os_thread_signal_destroy();
bool os_thread_signal_inited(); bool
os_thread_signal_inited();
#define os_signal_unmask() (void)0 #define os_signal_unmask() (void)0
#define os_sigreturn() (void)0 #define os_sigreturn() (void)0
@ -89,4 +94,3 @@ bool os_thread_signal_inited();
#endif #endif
#endif /* end of _PLATFORM_INTERNAL_H */ #endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -22,6 +22,3 @@ os_free(void *ptr)
{ {
free(ptr); free(ptr);
} }

View File

@ -79,8 +79,7 @@ os_munmap(void *addr, size_t size)
} }
} }
#if TRACE_MEMMAP != 0 #if TRACE_MEMMAP != 0
printf("Unmap memory, addr: %p, request_size: %zu\n", printf("Unmap memory, addr: %p, request_size: %zu\n", addr, request_size);
addr, request_size);
#endif #endif
} }
@ -95,8 +94,8 @@ os_mem_commit(void *addr, size_t size, int flags)
return NULL; return NULL;
#if TRACE_MEMMAP != 0 #if TRACE_MEMMAP != 0
printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", addr,
addr, request_size, protect); request_size, protect);
#endif #endif
return VirtualAlloc((LPVOID)addr, request_size, MEM_COMMIT, protect); return VirtualAlloc((LPVOID)addr, request_size, MEM_COMMIT, protect);
} }
@ -111,8 +110,8 @@ os_mem_decommit(void *addr, size_t size)
return; return;
#if TRACE_MEMMAP != 0 #if TRACE_MEMMAP != 0
printf("Decommit memory, addr: %p, request_size: %zu\n", printf("Decommit memory, addr: %p, request_size: %zu\n", addr,
addr, request_size); request_size);
#endif #endif
VirtualFree((LPVOID)addr, request_size, MEM_DECOMMIT); VirtualFree((LPVOID)addr, request_size, MEM_DECOMMIT);
} }
@ -134,4 +133,3 @@ os_mprotect(void *addr, size_t size, int prot)
#endif #endif
return VirtualProtect((LPVOID)addr, request_size, protect, NULL); return VirtualProtect((LPVOID)addr, request_size, protect, NULL);
} }

View File

@ -47,11 +47,16 @@ static os_thread_data supervisor_thread_data;
/* Thread data key */ /* Thread data key */
static DWORD thread_data_key; static DWORD thread_data_key;
int os_sem_init(korp_sem* sem); int
int os_sem_destroy(korp_sem* sem); os_sem_init(korp_sem *sem);
int os_sem_wait(korp_sem* sem); int
int os_sem_reltimed_wait(korp_sem* sem, uint64 useconds); os_sem_destroy(korp_sem *sem);
int os_sem_signal(korp_sem* sem); int
os_sem_wait(korp_sem *sem);
int
os_sem_reltimed_wait(korp_sem *sem, uint64 useconds);
int
os_sem_signal(korp_sem *sem);
int int
os_thread_sys_init() os_thread_sys_init()
@ -141,8 +146,7 @@ os_thread_cleanup(void *retval)
BH_FREE(thread_data); BH_FREE(thread_data);
} }
static unsigned __stdcall static unsigned __stdcall os_thread_wrapper(void *arg)
os_thread_wrapper(void *arg)
{ {
os_thread_data *thread_data = arg; os_thread_data *thread_data = arg;
os_thread_data *parent = thread_data->parent; os_thread_data *parent = thread_data->parent;
@ -202,9 +206,8 @@ os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
goto fail3; goto fail3;
os_mutex_lock(&parent->wait_lock); os_mutex_lock(&parent->wait_lock);
if (!_beginthreadex(NULL, stack_size, if (!_beginthreadex(NULL, stack_size, os_thread_wrapper, thread_data, 0,
os_thread_wrapper, thread_data, NULL)) {
0, NULL)) {
os_mutex_unlock(&parent->wait_lock); os_mutex_unlock(&parent->wait_lock);
goto fail4; goto fail4;
} }
@ -371,7 +374,7 @@ os_sem_wait(korp_sem *sem)
if (ret == WAIT_OBJECT_0) if (ret == WAIT_OBJECT_0)
return BHT_OK; return BHT_OK;
else if(ret == WAIT_TIMEOUT) else if (ret == WAIT_TIMEOUT)
return (int)WAIT_TIMEOUT; return (int)WAIT_TIMEOUT;
else /* WAIT_FAILED or others */ else /* WAIT_FAILED or others */
return BHT_ERROR; return BHT_ERROR;
@ -404,7 +407,7 @@ os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
if (ret == WAIT_OBJECT_0) if (ret == WAIT_OBJECT_0)
return BHT_OK; return BHT_OK;
else if(ret == WAIT_TIMEOUT) else if (ret == WAIT_TIMEOUT)
return (int)WAIT_TIMEOUT; return (int)WAIT_TIMEOUT;
else /* WAIT_FAILED or others */ else /* WAIT_FAILED or others */
return BHT_ERROR; return BHT_ERROR;
@ -414,8 +417,7 @@ int
os_sem_signal(korp_sem *sem) os_sem_signal(korp_sem *sem)
{ {
bh_assert(sem); bh_assert(sem);
return ReleaseSemaphore(*sem, 1, NULL) != FALSE return ReleaseSemaphore(*sem, 1, NULL) != FALSE ? BHT_OK : BHT_ERROR;
? BHT_OK: BHT_ERROR;
} }
int int
@ -478,8 +480,8 @@ os_cond_destroy(korp_cond *cond)
} }
static int static int
os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
bool timed, uint64 useconds) uint64 useconds)
{ {
os_thread_wait_node *node = &thread_data_current()->wait_node; os_thread_wait_node *node = &thread_data_current()->wait_node;
@ -567,7 +569,7 @@ os_thread_get_stack_boundary()
GetCurrentThreadStackLimits(&low_limit, &high_limit); GetCurrentThreadStackLimits(&low_limit, &high_limit);
/* 4 pages are set unaccessible by system, we reserved /* 4 pages are set unaccessible by system, we reserved
one more page at least for safety */ one more page at least for safety */
thread_stack_boundary = (uint8*)(uintptr_t)low_limit + page_size * 5; thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;
return thread_stack_boundary; return thread_stack_boundary;
} }
@ -601,4 +603,3 @@ os_thread_signal_inited()
return thread_signal_inited; return thread_signal_inited;
} }
#endif #endif

View File

@ -13,4 +13,3 @@ os_time_get_boot_microsecond()
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000; return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
} }

View File

@ -37,7 +37,6 @@
#include <arch/arm/aarch32/cortex_m/cmsis.h> #include <arch/arm/aarch32/cortex_m/cmsis.h>
#endif #endif
#ifndef BH_PLATFORM_ZEPHYR #ifndef BH_PLATFORM_ZEPHYR
#define BH_PLATFORM_ZEPHYR #define BH_PLATFORM_ZEPHYR
#endif #endif
@ -62,6 +61,7 @@ typedef struct korp_cond {
#define Z_TIMEOUT_MS(ms) ms #define Z_TIMEOUT_MS(ms) ms
#endif #endif
/* clang-format off */
void abort(void); void abort(void);
size_t strspn(const char *s, const char *accept); size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject); size_t strcspn(const char *s, const char *reject);
@ -91,6 +91,7 @@ double scalbn(double x, int n);
unsigned long long int strtoull(const char *nptr, char **endptr, int base); unsigned long long int strtoull(const char *nptr, char **endptr, int base);
double strtod(const char *nptr, char **endptr); double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr); float strtof(const char *nptr, char **endptr);
/* clang-format on */
/** /**
* @brief Allocate executable memroy * @brief Allocate executable memroy
@ -99,7 +100,7 @@ float strtof(const char *nptr, char **endptr);
* *
* @return the address of the allocated memory if not NULL * @return the address of the allocated memory if not NULL
*/ */
typedef void* (*exec_mem_alloc_func_t)(unsigned int size); typedef void *(*exec_mem_alloc_func_t)(unsigned int size);
/** /**
* @brief Release executable memroy * @brief Release executable memroy
@ -108,8 +109,12 @@ typedef void* (*exec_mem_alloc_func_t)(unsigned int size);
*/ */
typedef void (*exec_mem_free_func_t)(void *addr); typedef void (*exec_mem_free_func_t)(void *addr);
/* Below function are called by external project to set related function pointers that /* Below function are called by external project to set related function
* will be used to malloc/free executable memory. Otherwise default mechanise will be used. */ * pointers that will be used to malloc/free executable memory. Otherwise
void set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func, exec_mem_free_func_t free_func); * default mechanise will be used.
*/
void
set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func,
exec_mem_free_func_t free_func);
#endif #endif

View File

@ -29,9 +29,8 @@ disable_mpu_rasr_xn(void)
MPU->RASR |= ~MPU_RASR_XN_Msk; MPU->RASR |= ~MPU_RASR_XN_Msk;
} }
} }
} }
#endif /* end of CONFIG_ARM_MPU */ #endif /* end of CONFIG_ARM_MPU */
#endif #endif
static int static int
@ -84,8 +83,7 @@ os_realloc(void *ptr, unsigned size)
void void
os_free(void *ptr) os_free(void *ptr)
{ {}
}
#if 0 #if 0
struct out_context { struct out_context {
@ -203,17 +201,17 @@ os_dcache_flush()
SCB_CleanDCache(); SCB_CleanDCache();
irq_unlock(key); irq_unlock(key);
#elif defined(CONFIG_SOC_CVF_EM7D) && defined(CONFIG_ARC_MPU) \ #elif defined(CONFIG_SOC_CVF_EM7D) && defined(CONFIG_ARC_MPU) \
&& defined (CONFIG_CACHE_FLUSHING) && defined(CONFIG_CACHE_FLUSHING)
__asm__ __volatile__("sync"); __asm__ __volatile__("sync");
z_arc_v2_aux_reg_write(_ARC_V2_DC_FLSH, BIT(0)); z_arc_v2_aux_reg_write(_ARC_V2_DC_FLSH, BIT(0));
__asm__ __volatile__("sync"); __asm__ __volatile__("sync");
#endif #endif
} }
void set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func, void
exec_mem_free_func_t free_func) set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func,
exec_mem_free_func_t free_func)
{ {
exec_mem_alloc_func = alloc_func; exec_mem_alloc_func = alloc_func;
exec_mem_free_func = free_func; exec_mem_free_func = free_func;
} }

View File

@ -6,13 +6,15 @@
#include "platform_api_vmcore.h" #include "platform_api_vmcore.h"
#include "platform_api_extension.h" #include "platform_api_extension.h"
/* clang-format off */
#define bh_assert(v) do { \ #define bh_assert(v) do { \
if (!(v)) { \ if (!(v)) { \
printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ printf("\nASSERTION FAILED: %s, at %s, line %d\n", \
#v, __FILE__, __LINE__); \ #v, __FILE__, __LINE__); \
abort(); \ abort(); \
} \ } \
} while (0) } while (0)
/* clang-format on */
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) \ #if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) \
|| KERNEL_VERSION_NUMBER > 0x020300 /* version 2.3.0 */ || KERNEL_VERSION_NUMBER > 0x020300 /* version 2.3.0 */
@ -28,13 +30,13 @@
#endif #endif
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 #if BH_ENABLE_ZEPHYR_MPU_STACK != 0
static K_THREAD_STACK_ARRAY_DEFINE(mpu_stacks, static K_THREAD_STACK_ARRAY_DEFINE(mpu_stacks, BH_ZEPHYR_MPU_STACK_COUNT,
BH_ZEPHYR_MPU_STACK_COUNT,
BH_ZEPHYR_MPU_STACK_SIZE); BH_ZEPHYR_MPU_STACK_SIZE);
static bool mpu_stack_allocated[BH_ZEPHYR_MPU_STACK_COUNT]; static bool mpu_stack_allocated[BH_ZEPHYR_MPU_STACK_COUNT];
static struct k_mutex mpu_stack_lock; static struct k_mutex mpu_stack_lock;
static char *mpu_stack_alloc() static char *
mpu_stack_alloc()
{ {
int i; int i;
@ -43,14 +45,15 @@ static char *mpu_stack_alloc()
if (!mpu_stack_allocated[i]) { if (!mpu_stack_allocated[i]) {
mpu_stack_allocated[i] = true; mpu_stack_allocated[i] = true;
k_mutex_unlock(&mpu_stack_lock); k_mutex_unlock(&mpu_stack_lock);
return (char*)mpu_stacks[i]; return (char *)mpu_stacks[i];
} }
} }
k_mutex_unlock(&mpu_stack_lock); k_mutex_unlock(&mpu_stack_lock);
return NULL; return NULL;
} }
static void mpu_stack_free(char *stack) static void
mpu_stack_free(char *stack)
{ {
int i; int i;
@ -114,7 +117,8 @@ static struct k_mutex thread_obj_lock;
/* Thread object list */ /* Thread object list */
static os_thread_obj *thread_obj_list = NULL; static os_thread_obj *thread_obj_list = NULL;
static void thread_data_list_add(os_thread_data *thread_data) static void
thread_data_list_add(os_thread_data *thread_data)
{ {
k_mutex_lock(&thread_data_lock, K_FOREVER); k_mutex_lock(&thread_data_lock, K_FOREVER);
if (!thread_data_list) if (!thread_data_list)
@ -137,7 +141,8 @@ static void thread_data_list_add(os_thread_data *thread_data)
k_mutex_unlock(&thread_data_lock); k_mutex_unlock(&thread_data_lock);
} }
static void thread_data_list_remove(os_thread_data *thread_data) static void
thread_data_list_remove(os_thread_data *thread_data)
{ {
k_mutex_lock(&thread_data_lock, K_FOREVER); k_mutex_lock(&thread_data_lock, K_FOREVER);
if (thread_data_list) { if (thread_data_list) {
@ -174,7 +179,8 @@ thread_data_list_lookup(k_tid_t tid)
return NULL; return NULL;
} }
static void thread_obj_list_add(os_thread_obj *thread_obj) static void
thread_obj_list_add(os_thread_obj *thread_obj)
{ {
k_mutex_lock(&thread_obj_lock, K_FOREVER); k_mutex_lock(&thread_obj_lock, K_FOREVER);
if (!thread_obj_list) if (!thread_obj_list)
@ -187,7 +193,8 @@ static void thread_obj_list_add(os_thread_obj *thread_obj)
k_mutex_unlock(&thread_obj_lock); k_mutex_unlock(&thread_obj_lock);
} }
static void thread_obj_list_reclaim() static void
thread_obj_list_reclaim()
{ {
os_thread_obj *p, *p_prev; os_thread_obj *p, *p_prev;
k_mutex_lock(&thread_obj_lock, K_FOREVER); k_mutex_lock(&thread_obj_lock, K_FOREVER);
@ -199,12 +206,14 @@ static void thread_obj_list_reclaim()
thread_obj_list = p->next; thread_obj_list = p->next;
BH_FREE(p); BH_FREE(p);
p = thread_obj_list; p = thread_obj_list;
} else { /* p is not the head of list */ }
else { /* p is not the head of list */
p_prev->next = p->next; p_prev->next = p->next;
BH_FREE(p); BH_FREE(p);
p = p_prev->next; p = p_prev->next;
} }
} else { }
else {
p_prev = p; p_prev = p;
p = p->next; p = p->next;
} }
@ -212,7 +221,8 @@ static void thread_obj_list_reclaim()
k_mutex_unlock(&thread_obj_lock); k_mutex_unlock(&thread_obj_lock);
} }
int os_thread_sys_init() int
os_thread_sys_init()
{ {
if (is_thread_sys_inited) if (is_thread_sys_inited)
return BHT_OK; return BHT_OK;
@ -233,7 +243,8 @@ int os_thread_sys_init()
return BHT_OK; return BHT_OK;
} }
void os_thread_sys_destroy(void) void
os_thread_sys_destroy(void)
{ {
if (is_thread_sys_inited) { if (is_thread_sys_inited) {
is_thread_sys_inited = false; is_thread_sys_inited = false;
@ -247,7 +258,8 @@ thread_data_current()
return thread_data_list_lookup(tid); return thread_data_list_lookup(tid);
} }
static void os_thread_cleanup(void) static void
os_thread_cleanup(void)
{ {
os_thread_data *thread_data = thread_data_current(); os_thread_data *thread_data = thread_data_current();
@ -269,32 +281,35 @@ static void os_thread_cleanup(void)
thread_data_list_remove(thread_data); thread_data_list_remove(thread_data);
/* Set flag to true for the next thread creating to /* Set flag to true for the next thread creating to
free the thread object */ free the thread object */
((os_thread_obj*) thread_data->tid)->to_be_freed = true; ((os_thread_obj *)thread_data->tid)->to_be_freed = true;
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 #if BH_ENABLE_ZEPHYR_MPU_STACK != 0
mpu_stack_free(thread_data->stack); mpu_stack_free(thread_data->stack);
#endif #endif
BH_FREE(thread_data); BH_FREE(thread_data);
} }
static void os_thread_wrapper(void *start, void *arg, void *thread_data) static void
os_thread_wrapper(void *start, void *arg, void *thread_data)
{ {
/* Set thread custom data */ /* Set thread custom data */
((os_thread_data*) thread_data)->tid = k_current_get(); ((os_thread_data *)thread_data)->tid = k_current_get();
thread_data_list_add(thread_data); thread_data_list_add(thread_data);
((thread_start_routine_t) start)(arg); ((thread_start_routine_t)start)(arg);
os_thread_cleanup(); os_thread_cleanup();
} }
int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, int
unsigned int stack_size) os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{ {
return os_thread_create_with_prio(p_tid, start, arg, stack_size, return os_thread_create_with_prio(p_tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
} }
int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, int
void *arg, unsigned int stack_size, int prio) os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{ {
korp_tid tid; korp_tid tid;
os_thread_data *thread_data; os_thread_data *thread_data;
@ -347,7 +362,7 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
/* Set thread custom data */ /* Set thread custom data */
thread_data_list_add(thread_data); thread_data_list_add(thread_data);
thread_obj_list_add((os_thread_obj*)tid); thread_obj_list_add((os_thread_obj *)tid);
*p_tid = tid; *p_tid = tid;
return BHT_OK; return BHT_OK;
@ -362,14 +377,16 @@ fail1:
return BHT_ERROR; return BHT_ERROR;
} }
korp_tid os_self_thread() korp_tid
os_self_thread()
{ {
return (korp_tid)k_current_get(); return (korp_tid)k_current_get();
} }
int os_thread_join(korp_tid thread, void **value_ptr) int
os_thread_join(korp_tid thread, void **value_ptr)
{ {
(void) value_ptr; (void)value_ptr;
os_thread_data *thread_data; os_thread_data *thread_data;
os_thread_wait_node *node; os_thread_wait_node *node;
@ -407,30 +424,35 @@ int os_thread_join(korp_tid thread, void **value_ptr)
return BHT_OK; return BHT_OK;
} }
int os_mutex_init(korp_mutex *mutex) int
os_mutex_init(korp_mutex *mutex)
{ {
k_mutex_init(mutex); k_mutex_init(mutex);
return BHT_OK; return BHT_OK;
} }
int os_recursive_mutex_init(korp_mutex *mutex) int
os_recursive_mutex_init(korp_mutex *mutex)
{ {
k_mutex_init(mutex); k_mutex_init(mutex);
return BHT_OK; return BHT_OK;
} }
int os_mutex_destroy(korp_mutex *mutex) int
os_mutex_destroy(korp_mutex *mutex)
{ {
(void) mutex; (void)mutex;
return BHT_OK; return BHT_OK;
} }
int os_mutex_lock(korp_mutex *mutex) int
os_mutex_lock(korp_mutex *mutex)
{ {
return k_mutex_lock(mutex, K_FOREVER); return k_mutex_lock(mutex, K_FOREVER);
} }
int os_mutex_unlock(korp_mutex *mutex) int
os_mutex_unlock(korp_mutex *mutex)
{ {
#if KERNEL_VERSION_NUMBER >= 0x020200 /* version 2.2.0 */ #if KERNEL_VERSION_NUMBER >= 0x020200 /* version 2.2.0 */
return k_mutex_unlock(mutex); return k_mutex_unlock(mutex);
@ -440,21 +462,23 @@ int os_mutex_unlock(korp_mutex *mutex)
#endif #endif
} }
int os_cond_init(korp_cond *cond) int
os_cond_init(korp_cond *cond)
{ {
k_mutex_init(&cond->wait_list_lock); k_mutex_init(&cond->wait_list_lock);
cond->thread_wait_list = NULL; cond->thread_wait_list = NULL;
return BHT_OK; return BHT_OK;
} }
int os_cond_destroy(korp_cond *cond) int
os_cond_destroy(korp_cond *cond)
{ {
(void) cond; (void)cond;
return BHT_OK; return BHT_OK;
} }
static int os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, static int
bool timed, int mills) os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, int mills)
{ {
os_thread_wait_node *node; os_thread_wait_node *node;
@ -499,12 +523,14 @@ static int os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
return BHT_OK; return BHT_OK;
} }
int os_cond_wait(korp_cond *cond, korp_mutex *mutex) int
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
{ {
return os_cond_wait_internal(cond, mutex, false, 0); return os_cond_wait_internal(cond, mutex, false, 0);
} }
int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) int
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
{ {
if (useconds == BHT_WAIT_FOREVER) { if (useconds == BHT_WAIT_FOREVER) {
@ -526,7 +552,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
} }
} }
int os_cond_signal(korp_cond *cond) int
os_cond_signal(korp_cond *cond)
{ {
/* Signal the head wait node of wait list */ /* Signal the head wait node of wait list */
k_mutex_lock(&cond->wait_list_lock, K_FOREVER); k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
@ -537,13 +564,13 @@ int os_cond_signal(korp_cond *cond)
return BHT_OK; return BHT_OK;
} }
uint8 *os_thread_get_stack_boundary() uint8 *
os_thread_get_stack_boundary()
{ {
#if defined(CONFIG_THREAD_STACK_INFO) #if defined(CONFIG_THREAD_STACK_INFO)
korp_tid thread = k_current_get(); korp_tid thread = k_current_get();
return (uint8*)thread->stack_info.start; return (uint8 *)thread->stack_info.start;
#else #else
return NULL; return NULL;
#endif #endif
} }

View File

@ -10,4 +10,3 @@ os_time_get_boot_microsecond()
{ {
return k_uptime_get() * 1000; return k_uptime_get() * 1000;
} }

View File

@ -5,8 +5,9 @@
#include "bh_assert.h" #include "bh_assert.h"
void bh_assert_internal(int v, const char *file_name, int line_number, void
const char *expr_string) bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{ {
if (v) if (v)
return; return;
@ -17,9 +18,8 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
if (!expr_string) if (!expr_string)
expr_string = "NULL EXPR_STRING"; expr_string = "NULL EXPR_STRING";
os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n", os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n", expr_string,
expr_string, file_name, line_number); file_name, line_number);
abort(); abort();
} }

View File

@ -13,10 +13,11 @@ extern "C" {
#endif #endif
#if BH_DEBUG != 0 #if BH_DEBUG != 0
void bh_assert_internal(int v, const char *file_name, int line_number, void
const char *expr_string); bh_assert_internal(int v, const char *file_name, int line_number,
#define bh_assert(expr) bh_assert_internal((int)(uintptr_t)(expr), \ const char *expr_string);
__FILE__, __LINE__, #expr) #define bh_assert(expr) \
bh_assert_internal((int)(uintptr_t)(expr), __FILE__, __LINE__, #expr)
#else #else
#define bh_assert(expr) (void)0 #define bh_assert(expr) (void)0
#endif /* end of BH_DEBUG */ #endif /* end of BH_DEBUG */
@ -26,4 +27,3 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
#endif #endif
#endif /* end of _BH_ASSERT_H */ #endif /* end of _BH_ASSERT_H */

View File

@ -12,70 +12,67 @@
#define RSIZE_MAX 0x7FFFFFFF #define RSIZE_MAX 0x7FFFFFFF
int int
b_memcpy_s(void * s1, unsigned int s1max, b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
const void * s2, unsigned int n)
{ {
char *dest = (char*)s1; char *dest = (char *)s1;
char *src = (char*)s2; char *src = (char *)s2;
if (n == 0) { if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0; return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0;
}
int b_memmove_s(void * s1, unsigned int s1max,
const void * s2, unsigned int n)
{
char *dest = (char*)s1;
char *src = (char*)s2;
if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memmove(dest, src, n);
return 0;
} }
int int
b_strcat_s(char * s1, unsigned int s1max, const char * s2) b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
{ {
if (NULL == s1 || NULL == s2 char *dest = (char *)s1;
|| s1max < (strlen(s1) + strlen(s2) + 1) char *src = (char *)s2;
|| s1max > RSIZE_MAX) { if (n == 0) {
return -1; return 0;
} }
memcpy(s1 + strlen(s1), s2, strlen(s2) + 1); if (s1 == NULL || s1max > RSIZE_MAX) {
return 0; return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memmove(dest, src, n);
return 0;
} }
int int
b_strcpy_s(char * s1, unsigned int s1max, const char * s2) b_strcat_s(char *s1, unsigned int s1max, const char *s2)
{ {
if (NULL == s1 || NULL == s2 if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)
|| s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
|| s1max > RSIZE_MAX) { return -1;
return -1; }
}
memcpy(s1, s2, strlen(s2) + 1); memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
return 0; return 0;
}
int
b_strcpy_s(char *s1, unsigned int s1max, const char *s2)
{
if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}
memcpy(s1, s2, strlen(s2) + 1);
return 0;
} }
char * char *
@ -105,4 +102,3 @@ wa_strdup(const char *s)
} }
return s1; return s1;
} }

View File

@ -12,40 +12,50 @@
extern "C" { extern "C" {
#endif #endif
#define bh_memcpy_s(dest, dlen, src, slen) do { \ #define bh_memcpy_s(dest, dlen, src, slen) \
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \ do { \
(void)_ret; \ int _ret = slen == 0 ? 0 : b_memcpy_s(dest, dlen, src, slen); \
bh_assert (_ret == 0); \ (void)_ret; \
} while (0) bh_assert(_ret == 0); \
} while (0)
#define bh_memmove_s(dest, dlen, src, slen) do { \ #define bh_memmove_s(dest, dlen, src, slen) \
int _ret = slen == 0 ? 0 : b_memmove_s (dest, dlen, src, slen); \ do { \
(void)_ret; \ int _ret = slen == 0 ? 0 : b_memmove_s(dest, dlen, src, slen); \
bh_assert (_ret == 0); \ (void)_ret; \
} while (0) bh_assert(_ret == 0); \
} while (0)
#define bh_strcat_s(dest, dlen, src) do { \ #define bh_strcat_s(dest, dlen, src) \
int _ret = b_strcat_s (dest, dlen, src); \ do { \
(void)_ret; \ int _ret = b_strcat_s(dest, dlen, src); \
bh_assert (_ret == 0); \ (void)_ret; \
} while (0) bh_assert(_ret == 0); \
} while (0)
#define bh_strcpy_s(dest, dlen, src) do { \ #define bh_strcpy_s(dest, dlen, src) \
int _ret = b_strcpy_s (dest, dlen, src); \ do { \
(void)_ret; \ int _ret = b_strcpy_s(dest, dlen, src); \
bh_assert (_ret == 0); \ (void)_ret; \
} while (0) bh_assert(_ret == 0); \
} while (0)
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n); int
int b_memmove_s(void * s1, unsigned int s1max, const void * s2, unsigned int n); b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int b_strcat_s(char * s1, unsigned int s1max, const char * s2); int
int b_strcpy_s(char * s1, unsigned int s1max, const char * s2); b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int
b_strcat_s(char *s1, unsigned int s1max, const char *s2);
int
b_strcpy_s(char *s1, unsigned int s1max, const char *s2);
/* strdup with string allocated by BH_MALLOC */ /* strdup with string allocated by BH_MALLOC */
char *bh_strdup(const char *s); char *
bh_strdup(const char *s);
/* strdup with string allocated by WA_MALLOC */ /* strdup with string allocated by WA_MALLOC */
char *wa_strdup(const char *s); char *
wa_strdup(const char *s);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -25,11 +25,9 @@ struct HashMap {
HashMapElem *elements[1]; HashMapElem *elements[1];
}; };
HashMap* HashMap *
bh_hash_map_create(uint32 size, bool use_lock, bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
HashFunc hash_func, KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
KeyEqualFunc key_equal_func,
KeyDestroyFunc key_destroy_func,
ValueDestroyFunc value_destroy_func) ValueDestroyFunc value_destroy_func)
{ {
HashMap *map; HashMap *map;
@ -42,16 +40,15 @@ bh_hash_map_create(uint32 size, bool use_lock,
if (!hash_func || !key_equal_func) { if (!hash_func || !key_equal_func) {
LOG_ERROR("HashMap create failed: hash function or key equal function " LOG_ERROR("HashMap create failed: hash function or key equal function "
" is NULL.\n"); " is NULL.\n");
return NULL; return NULL;
} }
total_size = offsetof(HashMap, elements) + total_size = offsetof(HashMap, elements)
sizeof(HashMapElem *) * (uint64)size + + sizeof(HashMapElem *) * (uint64)size
(use_lock ? sizeof(korp_mutex) : 0); + (use_lock ? sizeof(korp_mutex) : 0);
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX || !(map = BH_MALLOC((uint32)total_size))) {
|| !(map = BH_MALLOC((uint32)total_size))) {
LOG_ERROR("HashMap create failed: alloc memory failed.\n"); LOG_ERROR("HashMap create failed: alloc memory failed.\n");
return NULL; return NULL;
} }
@ -59,9 +56,8 @@ bh_hash_map_create(uint32 size, bool use_lock,
memset(map, 0, (uint32)total_size); memset(map, 0, (uint32)total_size);
if (use_lock) { if (use_lock) {
map->lock = (korp_mutex*) map->lock = (korp_mutex *)((uint8 *)map + offsetof(HashMap, elements)
((uint8*)map + offsetof(HashMap, elements) + sizeof(HashMapElem *) * size);
+ sizeof(HashMapElem *) * size);
if (os_mutex_init(map->lock)) { if (os_mutex_init(map->lock)) {
LOG_ERROR("HashMap create failed: init map lock failed.\n"); LOG_ERROR("HashMap create failed: init map lock failed.\n");
BH_FREE(map); BH_FREE(map);
@ -124,7 +120,7 @@ fail:
return false; return false;
} }
void* void *
bh_hash_map_find(HashMap *map, void *key) bh_hash_map_find(HashMap *map, void *key)
{ {
uint32 index; uint32 index;
@ -161,8 +157,7 @@ bh_hash_map_find(HashMap *map, void *key)
} }
bool bool
bh_hash_map_update(HashMap *map, void *key, void *value, bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value)
void **p_old_value)
{ {
uint32 index; uint32 index;
HashMapElem *elem; HashMapElem *elem;
@ -199,8 +194,8 @@ bh_hash_map_update(HashMap *map, void *key, void *value,
} }
bool bool
bh_hash_map_remove(HashMap *map, void *key, bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
void **p_old_key, void **p_old_value) void **p_old_value)
{ {
uint32 index; uint32 index;
HashMapElem *elem, *prev; HashMapElem *elem, *prev;

View File

@ -51,11 +51,9 @@ typedef void (*TraverseCallbackFunc)(void *key, void *value, void *user_data);
* *
* @return the hash map created, NULL if failed * @return the hash map created, NULL if failed
*/ */
HashMap* HashMap *
bh_hash_map_create(uint32 size, bool use_lock, bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
HashFunc hash_func, KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
KeyEqualFunc key_equal_func,
KeyDestroyFunc key_destroy_func,
ValueDestroyFunc value_destroy_func); ValueDestroyFunc value_destroy_func);
/** /**
@ -79,7 +77,7 @@ bh_hash_map_insert(HashMap *map, void *key, void *value);
* *
* @return the value of the found element if success, NULL otherwise * @return the value of the found element if success, NULL otherwise
*/ */
void* void *
bh_hash_map_find(HashMap *map, void *key); bh_hash_map_find(HashMap *map, void *key);
/** /**
@ -95,8 +93,7 @@ bh_hash_map_find(HashMap *map, void *key);
* it will be copied to p_old_value for user to process. * it will be copied to p_old_value for user to process.
*/ */
bool bool
bh_hash_map_update(HashMap *map, void *key, void *value, bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value);
void **p_old_value);
/** /**
* Remove an element from the hash map * Remove an element from the hash map
@ -112,8 +109,8 @@ bh_hash_map_update(HashMap *map, void *key, void *value,
* p_old_key and p_old_value for user to process. * p_old_key and p_old_value for user to process.
*/ */
bool bool
bh_hash_map_remove(HashMap *map, void *key, bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
void **p_old_key, void **p_old_value); void **p_old_value);
/** /**
* Destroy the hashmap * Destroy the hashmap
@ -166,4 +163,3 @@ bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
#endif #endif
#endif /* endof WASM_HASHMAP_H */ #endif /* endof WASM_HASHMAP_H */

View File

@ -14,10 +14,12 @@
* @return <code>true</code> if the pointer has been in the list; * @return <code>true</code> if the pointer has been in the list;
* <code>false</code> otherwise. * <code>false</code> otherwise.
*/ */
static bool bh_list_is_elem_exist(bh_list *list, void *elem); static bool
bh_list_is_elem_exist(bh_list *list, void *elem);
#endif #endif
bh_list_status bh_list_init(bh_list *list) bh_list_status
bh_list_init(bh_list *list)
{ {
if (!list) if (!list)
return BH_LIST_ERROR; return BH_LIST_ERROR;
@ -27,23 +29,25 @@ bh_list_status bh_list_init(bh_list *list)
return BH_LIST_SUCCESS; return BH_LIST_SUCCESS;
} }
bh_list_status bh_list_insert(bh_list *list, void *elem) bh_list_status
bh_list_insert(bh_list *list, void *elem)
{ {
bh_list_link *p = NULL; bh_list_link *p = NULL;
if (!list || !elem) if (!list || !elem)
return BH_LIST_ERROR; return BH_LIST_ERROR;
#if BH_DEBUG != 0 #if BH_DEBUG != 0
bh_assert (!bh_list_is_elem_exist(list, elem)); bh_assert(!bh_list_is_elem_exist(list, elem));
#endif #endif
p = (bh_list_link *) elem; p = (bh_list_link *)elem;
p->next = (list->head).next; p->next = (list->head).next;
(list->head).next = p; (list->head).next = p;
list->len++; list->len++;
return BH_LIST_SUCCESS; return BH_LIST_SUCCESS;
} }
bh_list_status bh_list_remove(bh_list *list, void *elem) bh_list_status
bh_list_remove(bh_list *list, void *elem)
{ {
bh_list_link *cur = NULL; bh_list_link *cur = NULL;
bh_list_link *prev = NULL; bh_list_link *prev = NULL;
@ -71,32 +75,37 @@ bh_list_status bh_list_remove(bh_list *list, void *elem)
return BH_LIST_ERROR; return BH_LIST_ERROR;
} }
uint32 bh_list_length(bh_list *list) uint32
bh_list_length(bh_list *list)
{ {
return (list ? list->len : 0); return (list ? list->len : 0);
} }
void* bh_list_first_elem(bh_list *list) void *
bh_list_first_elem(bh_list *list)
{ {
return (list ? (list->head).next : NULL); return (list ? (list->head).next : NULL);
} }
void* bh_list_elem_next(void *node) void *
bh_list_elem_next(void *node)
{ {
return (node ? ((bh_list_link *) node)->next : NULL); return (node ? ((bh_list_link *)node)->next : NULL);
} }
#if BH_DEBUG != 0 #if BH_DEBUG != 0
static bool bh_list_is_elem_exist(bh_list *list, void *elem) static bool
bh_list_is_elem_exist(bh_list *list, void *elem)
{ {
bh_list_link *p = NULL; bh_list_link *p = NULL;
if (!list || !elem) return false; if (!list || !elem)
return false;
p = (list->head).next; p = (list->head).next;
while (p && p != elem) p = p->next; while (p && p != elem)
p = p->next;
return (p != NULL); return (p != NULL);
} }
#endif #endif

View File

@ -46,29 +46,34 @@ typedef enum bh_list_status {
* @return <code>BH_LIST_ERROR</code> if OK; * @return <code>BH_LIST_ERROR</code> if OK;
* <code>BH_LIST_ERROR</code> if list pointer is NULL. * <code>BH_LIST_ERROR</code> if list pointer is NULL.
*/ */
bh_list_status bh_list_init(bh_list *list); bh_list_status
bh_list_init(bh_list *list);
/** /**
* Insert an elem pointer into list. The list node memory is maintained by list while * Insert an elem pointer into list. The list node memory is maintained by list
* elem memory is the responsibility of list user. * while elem memory is the responsibility of list user.
* *
* @param list pointer to list. * @param list pointer to list.
* @param elem pointer to elem that will be inserted into list. * @param elem pointer to elem that will be inserted into list.
* @return <code>BH_LIST_ERROR</code> if OK; * @return <code>BH_LIST_ERROR</code> if OK;
* <code>BH_LIST_ERROR</code> if input is invalid or no memory available. * <code>BH_LIST_ERROR</code> if input is invalid or no memory
* available.
*/ */
extern bh_list_status bh_list_insert(bh_list *list, void *elem); extern bh_list_status
bh_list_insert(bh_list *list, void *elem);
/** /**
* Remove an elem pointer from list. The list node memory is maintained by list while * Remove an elem pointer from list. The list node memory is maintained by list
* elem memory is the responsibility of list user. * while elem memory is the responsibility of list user.
* *
* @param list pointer to list. * @param list pointer to list.
* @param elem pointer to elem that will be inserted into list. * @param elem pointer to elem that will be inserted into list.
* @return <code>BH_LIST_ERROR</code> if OK; * @return <code>BH_LIST_ERROR</code> if OK;
* <code>BH_LIST_ERROR</code> if element does not exist in given list. * <code>BH_LIST_ERROR</code> if element does not exist in given
* list.
*/ */
bh_list_status bh_list_remove(bh_list *list, void *elem); bh_list_status
bh_list_remove(bh_list *list, void *elem);
/** /**
* Get the list length. * Get the list length.
@ -76,7 +81,8 @@ bh_list_status bh_list_remove(bh_list *list, void *elem);
* @param list pointer to list. * @param list pointer to list.
* @return the length of the list. * @return the length of the list.
*/ */
uint32 bh_list_length(bh_list *list); uint32
bh_list_length(bh_list *list);
/** /**
* Get the first elem in the list. * Get the first elem in the list.
@ -84,7 +90,8 @@ uint32 bh_list_length(bh_list *list);
* @param list pointer to list. * @param list pointer to list.
* @return pointer to the first node. * @return pointer to the first node.
*/ */
void* bh_list_first_elem(bh_list *list); void *
bh_list_first_elem(bh_list *list);
/** /**
* Get the next elem of given list input elem. * Get the next elem of given list input elem.
@ -92,11 +99,11 @@ void* bh_list_first_elem(bh_list *list);
* @param node pointer to list node. * @param node pointer to list node.
* @return pointer to next list node. * @return pointer to next list node.
*/ */
void* bh_list_elem_next(void *node); void *
bh_list_elem_next(void *node);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* #ifndef _BH_LIST_H */ #endif /* #ifndef _BH_LIST_H */

View File

@ -71,9 +71,8 @@ bh_print_time(const char *prompt)
total_time_ms += curr_time_ms - last_time_ms; total_time_ms += curr_time_ms - last_time_ms;
os_printf("%-48s time of last stage: %u ms, total time: %u ms\n", os_printf("%-48s time of last stage: %u ms, total time: %u ms\n", prompt,
prompt, curr_time_ms - last_time_ms, total_time_ms); curr_time_ms - last_time_ms, total_time_ms);
last_time_ms = curr_time_ms; last_time_ms = curr_time_ms;
} }

View File

@ -52,19 +52,22 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...);
#endif #endif
#if BH_DEBUG != 0 #if BH_DEBUG != 0
#define LOG_FATAL(...) bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__) #define LOG_FATAL(...) \
bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
#else #else
#define LOG_FATAL(...) bh_log(BH_LOG_LEVEL_FATAL, __FUNCTION__, __LINE__, __VA_ARGS__) #define LOG_FATAL(...) \
bh_log(BH_LOG_LEVEL_FATAL, __FUNCTION__, __LINE__, __VA_ARGS__)
#endif #endif
#define LOG_ERROR(...) bh_log(BH_LOG_LEVEL_ERROR, NULL, 0, __VA_ARGS__) #define LOG_ERROR(...) bh_log(BH_LOG_LEVEL_ERROR, NULL, 0, __VA_ARGS__)
#define LOG_WARNING(...) bh_log(BH_LOG_LEVEL_WARNING, NULL, 0, __VA_ARGS__) #define LOG_WARNING(...) bh_log(BH_LOG_LEVEL_WARNING, NULL, 0, __VA_ARGS__)
#define LOG_VERBOSE(...) bh_log(BH_LOG_LEVEL_VERBOSE, NULL, 0, __VA_ARGS__) #define LOG_VERBOSE(...) bh_log(BH_LOG_LEVEL_VERBOSE, NULL, 0, __VA_ARGS__)
#if BH_DEBUG != 0 #if BH_DEBUG != 0
#define LOG_DEBUG(...) bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__) #define LOG_DEBUG(...) \
bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#else #else
#define LOG_DEBUG(...) (void)0 #define LOG_DEBUG(...) (void)0
#endif #endif
void void
@ -74,4 +77,4 @@ bh_print_time(const char *prompt);
} }
#endif #endif
#endif /* _BH_LOG_H */ #endif /* _BH_LOG_H */

View File

@ -18,8 +18,6 @@
#include "bh_vector.h" #include "bh_vector.h"
#include "runtime_timer.h" #include "runtime_timer.h"
/** /**
* WA_MALLOC/WA_FREE need to be redefined for both * WA_MALLOC/WA_FREE need to be redefined for both
* runtime native and WASM app respectively. * runtime native and WASM app respectively.
@ -38,4 +36,3 @@
#endif #endif
#endif /* #ifndef _BH_PLATFORM_H */ #endif /* #ifndef _BH_PLATFORM_H */

View File

@ -6,11 +6,11 @@
#include "bh_queue.h" #include "bh_queue.h"
typedef struct bh_queue_node { typedef struct bh_queue_node {
struct bh_queue_node * next; struct bh_queue_node *next;
struct bh_queue_node * prev; struct bh_queue_node *prev;
unsigned short tag; unsigned short tag;
unsigned int len; unsigned int len;
void * body; void *body;
bh_msg_cleaner msg_cleaner; bh_msg_cleaner msg_cleaner;
} bh_queue_node; } bh_queue_node;
@ -20,23 +20,26 @@ struct bh_queue {
unsigned int cnt; unsigned int cnt;
unsigned int max; unsigned int max;
unsigned int drops; unsigned int drops;
bh_queue_node * head; bh_queue_node *head;
bh_queue_node * tail; bh_queue_node *tail;
bool exit_loop_run; bool exit_loop_run;
}; };
char * bh_message_payload(bh_message_t message) char *
bh_message_payload(bh_message_t message)
{ {
return message->body; return message->body;
} }
uint32 bh_message_payload_len(bh_message_t message) uint32
bh_message_payload_len(bh_message_t message)
{ {
return message->len; return message->len;
} }
int bh_message_type(bh_message_t message) int
bh_message_type(bh_message_t message)
{ {
return message->tag; return message->tag;
} }
@ -68,7 +71,8 @@ bh_queue_create()
return queue; return queue;
} }
void bh_queue_destroy(bh_queue *queue) void
bh_queue_destroy(bh_queue *queue)
{ {
bh_queue_node *node; bh_queue_node *node;
@ -89,7 +93,8 @@ void bh_queue_destroy(bh_queue *queue)
bh_queue_free(queue); bh_queue_free(queue);
} }
bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg) bool
bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
{ {
if (queue->cnt >= queue->max) { if (queue->cnt >= queue->max) {
queue->drops++; queue->drops++;
@ -107,7 +112,8 @@ bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
queue->cnt = 1; queue->cnt = 1;
bh_queue_cond_signal(&queue->queue_wait_cond); bh_queue_cond_signal(&queue->queue_wait_cond);
} else { }
else {
msg->next = NULL; msg->next = NULL;
msg->prev = queue->tail; msg->prev = queue->tail;
queue->tail->next = msg; queue->tail->next = msg;
@ -120,8 +126,8 @@ bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
return true; return true;
} }
bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body, bool
unsigned int len) bh_post_msg(bh_queue *queue, unsigned short tag, void *body, unsigned int len)
{ {
bh_queue_node *msg = bh_new_msg(tag, body, len, NULL); bh_queue_node *msg = bh_new_msg(tag, body, len, NULL);
if (msg == NULL) { if (msg == NULL) {
@ -139,23 +145,24 @@ bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body,
return true; return true;
} }
bh_queue_node * bh_new_msg(unsigned short tag, void *body, unsigned int len, bh_queue_node *
void * handler) bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler)
{ {
bh_queue_node *msg = (bh_queue_node*) bh_queue_node *msg =
bh_queue_malloc(sizeof(bh_queue_node)); (bh_queue_node *)bh_queue_malloc(sizeof(bh_queue_node));
if (msg == NULL) if (msg == NULL)
return NULL; return NULL;
memset(msg, 0, sizeof(bh_queue_node)); memset(msg, 0, sizeof(bh_queue_node));
msg->len = len; msg->len = len;
msg->body = body; msg->body = body;
msg->tag = tag; msg->tag = tag;
msg->msg_cleaner = (bh_msg_cleaner) handler; msg->msg_cleaner = (bh_msg_cleaner)handler;
return msg; return msg;
} }
void bh_free_msg(bh_queue_node *msg) void
bh_free_msg(bh_queue_node *msg)
{ {
if (msg->msg_cleaner) { if (msg->msg_cleaner) {
msg->msg_cleaner(msg->body); msg->msg_cleaner(msg->body);
@ -171,7 +178,8 @@ void bh_free_msg(bh_queue_node *msg)
bh_queue_free(msg); bh_queue_free(msg);
} }
bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us) bh_message_t
bh_get_msg(bh_queue *queue, uint64 timeout_us)
{ {
bh_queue_node *msg = NULL; bh_queue_node *msg = NULL;
bh_queue_mutex_lock(&queue->queue_lock); bh_queue_mutex_lock(&queue->queue_lock);
@ -192,13 +200,15 @@ bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us)
if (queue->cnt == 0) { if (queue->cnt == 0) {
bh_assert(queue->head == NULL); bh_assert(queue->head == NULL);
bh_assert(queue->tail == NULL); bh_assert(queue->tail == NULL);
} else if (queue->cnt == 1) { }
else if (queue->cnt == 1) {
bh_assert(queue->head == queue->tail); bh_assert(queue->head == queue->tail);
msg = queue->head; msg = queue->head;
queue->head = queue->tail = NULL; queue->head = queue->tail = NULL;
queue->cnt = 0; queue->cnt = 0;
} else { }
else {
msg = queue->head; msg = queue->head;
queue->head = queue->head->next; queue->head = queue->head->next;
queue->head->prev = NULL; queue->head->prev = NULL;
@ -210,7 +220,8 @@ bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us)
return msg; return msg;
} }
unsigned bh_queue_get_message_count(bh_queue *queue) unsigned
bh_queue_get_message_count(bh_queue *queue)
{ {
if (!queue) if (!queue)
return 0; return 0;
@ -218,15 +229,15 @@ unsigned bh_queue_get_message_count(bh_queue *queue)
return queue->cnt; return queue->cnt;
} }
void bh_queue_enter_loop_run(bh_queue *queue, void
bh_queue_handle_msg_callback handle_cb, bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
void *arg) void *arg)
{ {
if (!queue) if (!queue)
return; return;
while (!queue->exit_loop_run) { while (!queue->exit_loop_run) {
bh_queue_node * message = bh_get_msg(queue, BHT_WAIT_FOREVER); bh_queue_node *message = bh_get_msg(queue, BHT_WAIT_FOREVER);
if (message) { if (message) {
handle_cb(message, arg); handle_cb(message, arg);
@ -235,7 +246,8 @@ void bh_queue_enter_loop_run(bh_queue *queue,
} }
} }
void bh_queue_exit_loop_run(bh_queue *queue) void
bh_queue_exit_loop_run(bh_queue *queue)
{ {
if (queue) { if (queue) {
queue->exit_loop_run = true; queue->exit_loop_run = true;

View File

@ -13,7 +13,7 @@ extern "C" {
#include "bh_platform.h" #include "bh_platform.h"
struct bh_queue_node; struct bh_queue_node;
typedef struct bh_queue_node * bh_message_t; typedef struct bh_queue_node *bh_message_t;
struct bh_queue; struct bh_queue;
typedef struct bh_queue bh_queue; typedef struct bh_queue bh_queue;
@ -45,25 +45,30 @@ bh_queue_create();
void void
bh_queue_destroy(bh_queue *queue); bh_queue_destroy(bh_queue *queue);
char * bh_message_payload(bh_message_t message); char *
uint32 bh_message_payload_len(bh_message_t message); bh_message_payload(bh_message_t message);
int bh_message_type(bh_message_t message); uint32
bh_message_payload_len(bh_message_t message);
int
bh_message_type(bh_message_t message);
bh_message_t bh_new_msg(unsigned short tag, void *body, unsigned int len, bh_message_t
void * handler); bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler);
void bh_free_msg(bh_message_t msg); void
bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body, bh_free_msg(bh_message_t msg);
unsigned int len); bool
bool bh_post_msg2(bh_queue *queue, bh_message_t msg); bh_post_msg(bh_queue *queue, unsigned short tag, void *body, unsigned int len);
bool
bh_post_msg2(bh_queue *queue, bh_message_t msg);
bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us); bh_message_t
bh_get_msg(bh_queue *queue, uint64 timeout_us);
unsigned unsigned
bh_queue_get_message_count(bh_queue *queue); bh_queue_get_message_count(bh_queue *queue);
void void
bh_queue_enter_loop_run(bh_queue *queue, bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
bh_queue_handle_msg_callback handle_cb,
void *arg); void *arg);
void void
bh_queue_exit_loop_run(bh_queue *queue); bh_queue_exit_loop_run(bh_queue *queue);
@ -73,4 +78,3 @@ bh_queue_exit_loop_run(bh_queue *queue);
#endif #endif
#endif /* #ifndef _BH_QUEUE_H */ #endif /* #ifndef _BH_QUEUE_H */

View File

@ -5,14 +5,13 @@
#include "bh_vector.h" #include "bh_vector.h"
static uint8* static uint8 *
alloc_vector_data(size_t length, size_t size_elem) alloc_vector_data(size_t length, size_t size_elem)
{ {
uint64 total_size = ((uint64)size_elem) * length; uint64 total_size = ((uint64)size_elem) * length;
uint8 *data; uint8 *data;
if (length > UINT32_MAX if (length > UINT32_MAX || size_elem > UINT32_MAX
|| size_elem > UINT32_MAX
|| total_size > UINT32_MAX) { || total_size > UINT32_MAX) {
return NULL; return NULL;
} }
@ -82,12 +81,13 @@ bh_vector_set(Vector *vector, uint32 index, const void *elem_buf)
return false; return false;
} }
memcpy(vector->data + vector->size_elem * index, memcpy(vector->data + vector->size_elem * index, elem_buf,
elem_buf, vector->size_elem); vector->size_elem);
return true; return true;
} }
bool bh_vector_get(const Vector *vector, uint32 index, void *elem_buf) bool
bh_vector_get(const Vector *vector, uint32 index, void *elem_buf)
{ {
if (!vector || !elem_buf) { if (!vector || !elem_buf) {
LOG_ERROR("Get vector elem failed: vector or elem buf is NULL.\n"); LOG_ERROR("Get vector elem failed: vector or elem buf is NULL.\n");
@ -104,7 +104,8 @@ bool bh_vector_get(const Vector *vector, uint32 index, void *elem_buf)
return true; return true;
} }
bool bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf) bool
bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
{ {
size_t i; size_t i;
uint8 *p; uint8 *p;
@ -135,7 +136,8 @@ bool bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
return true; return true;
} }
bool bh_vector_append(Vector *vector, const void *elem_buf) bool
bh_vector_append(Vector *vector, const void *elem_buf)
{ {
if (!vector || !elem_buf) { if (!vector || !elem_buf) {
LOG_ERROR("Append vector elem failed: vector or elem buf is NULL.\n"); LOG_ERROR("Append vector elem failed: vector or elem buf is NULL.\n");
@ -147,8 +149,8 @@ bool bh_vector_append(Vector *vector, const void *elem_buf)
return false; return false;
} }
memcpy(vector->data + vector->size_elem * vector->num_elems, memcpy(vector->data + vector->size_elem * vector->num_elems, elem_buf,
elem_buf, vector->size_elem); vector->size_elem);
vector->num_elems++; vector->num_elems++;
return true; return true;
} }

View File

@ -122,4 +122,3 @@ bh_vector_destroy(Vector *vector);
#endif #endif
#endif /* endof _WASM_VECTOR_H */ #endif /* endof _WASM_VECTOR_H */

View File

@ -12,7 +12,7 @@
#endif #endif
typedef struct _app_timer { typedef struct _app_timer {
struct _app_timer * next; struct _app_timer *next;
uint32 id; uint32 id;
uint32 interval; uint32 interval;
uint64 expiry; uint64 expiry;
@ -81,13 +81,13 @@ remove_timer_from(timer_ctx_t ctx, uint32 timer_id, bool active_list)
if (t->id == timer_id) { if (t->id == timer_id) {
if (prev == NULL) { if (prev == NULL) {
*head = t->next; *head = t->next;
PRINT("removed timer [%d] at head from list %d\n", PRINT("removed timer [%d] at head from list %d\n", t->id,
t->id, active_list); active_list);
} }
else { else {
prev->next = t->next; prev->next = t->next;
PRINT("removed timer [%d] after [%d] from list %d\n", PRINT("removed timer [%d] after [%d] from list %d\n", t->id,
t->id, prev->id, active_list); prev->id, active_list);
} }
os_mutex_unlock(&ctx->mutex); os_mutex_unlock(&ctx->mutex);
@ -143,8 +143,8 @@ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer)
else { else {
timer->next = t; timer->next = t;
prev->next = timer; prev->next = timer;
PRINT("rescheduled timer [%d] after [%d]\n", PRINT("rescheduled timer [%d] after [%d]\n", timer->id,
timer->id, prev->id); prev->id);
} }
goto out; goto out;
@ -158,8 +158,8 @@ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer)
if (prev) { if (prev) {
/* insert to the list end */ /* insert to the list end */
prev->next = timer; prev->next = timer;
PRINT("rescheduled timer [%d] at end, after [%d]\n", PRINT("rescheduled timer [%d] at end, after [%d]\n", timer->id,
timer->id, prev->id); prev->id);
} }
else { else {
/* insert at the begin */ /* insert at the begin */
@ -213,8 +213,8 @@ release_timer_list(app_timer_t **p_list)
timer_ctx_t timer_ctx_t
create_timer_ctx(timer_callback_f timer_handler, create_timer_ctx(timer_callback_f timer_handler,
check_timer_expiry_f expiery_checker, check_timer_expiry_f expiery_checker, int prealloc_num,
int prealloc_num, unsigned int owner) unsigned int owner)
{ {
timer_ctx_t ctx = (timer_ctx_t)BH_MALLOC(sizeof(struct _timer_ctx)); timer_ctx_t ctx = (timer_ctx_t)BH_MALLOC(sizeof(struct _timer_ctx));
@ -229,7 +229,7 @@ create_timer_ctx(timer_callback_f timer_handler,
ctx->owner = owner; ctx->owner = owner;
while (prealloc_num > 0) { while (prealloc_num > 0) {
app_timer_t *timer = (app_timer_t*)BH_MALLOC(sizeof(app_timer_t)); app_timer_t *timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t));
if (timer == NULL) if (timer == NULL)
goto cleanup; goto cleanup;
@ -283,7 +283,7 @@ timer_ctx_get_owner(timer_ctx_t ctx)
} }
void void
add_idle_timer(timer_ctx_t ctx, app_timer_t * timer) add_idle_timer(timer_ctx_t ctx, app_timer_t *timer)
{ {
os_mutex_lock(&ctx->mutex); os_mutex_lock(&ctx->mutex);
timer->next = ctx->idle_timers; timer->next = ctx->idle_timers;
@ -292,8 +292,7 @@ add_idle_timer(timer_ctx_t ctx, app_timer_t * timer)
} }
uint32 uint32
sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, bool auto_start)
bool auto_start)
{ {
app_timer_t *timer; app_timer_t *timer;
@ -307,7 +306,7 @@ sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
} }
} }
else { else {
timer = (app_timer_t*)BH_MALLOC(sizeof(app_timer_t)); timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t));
if (timer == NULL) if (timer == NULL)
return (uint32)-1; return (uint32)-1;
} }
@ -333,7 +332,7 @@ bool
sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id) sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id)
{ {
bool from_active; bool from_active;
app_timer_t * t = remove_timer(ctx, timer_id, &from_active); app_timer_t *t = remove_timer(ctx, timer_id, &from_active);
if (t == NULL) if (t == NULL)
return false; return false;
@ -348,7 +347,7 @@ bool
sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id) sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id)
{ {
bool from_active; bool from_active;
app_timer_t * t = remove_timer(ctx, timer_id, &from_active); app_timer_t *t = remove_timer(ctx, timer_id, &from_active);
if (t == NULL) if (t == NULL)
return false; return false;
@ -362,7 +361,7 @@ sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id)
bool bool
sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval) sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
{ {
app_timer_t * t = remove_timer(ctx, timer_id, NULL); app_timer_t *t = remove_timer(ctx, timer_id, NULL);
if (t == NULL) if (t == NULL)
return false; return false;
@ -376,7 +375,8 @@ sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
} }
/* /*
* API called by the timer manager from another thread or the kernel timer handler * API called by the timer manager from another thread or the kernel timer
* handler
*/ */
/** /**
@ -467,4 +467,3 @@ cleanup_app_timers(timer_ctx_t ctx)
os_mutex_unlock(&ctx->mutex); os_mutex_unlock(&ctx->mutex);
} }

View File

@ -12,28 +12,38 @@
extern "C" { extern "C" {
#endif #endif
uint64 bh_get_tick_ms(); uint64
uint32 bh_get_elpased_ms(uint32 *last_system_clock); bh_get_tick_ms();
uint32
bh_get_elpased_ms(uint32 *last_system_clock);
struct _timer_ctx; struct _timer_ctx;
typedef struct _timer_ctx * timer_ctx_t; typedef struct _timer_ctx *timer_ctx_t;
typedef void (*timer_callback_f)(unsigned int id, unsigned int owner); typedef void (*timer_callback_f)(unsigned int id, unsigned int owner);
typedef void (*check_timer_expiry_f)(timer_ctx_t ctx); typedef void (*check_timer_expiry_f)(timer_ctx_t ctx);
timer_ctx_t create_timer_ctx(timer_callback_f timer_handler, timer_ctx_t
check_timer_expiry_f, int prealloc_num, create_timer_ctx(timer_callback_f timer_handler, check_timer_expiry_f,
unsigned int owner); int prealloc_num, unsigned int owner);
void destroy_timer_ctx(timer_ctx_t); void destroy_timer_ctx(timer_ctx_t);
unsigned int timer_ctx_get_owner(timer_ctx_t ctx); unsigned int
timer_ctx_get_owner(timer_ctx_t ctx);
uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, uint32
bool auto_start); sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
bool sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id); bool auto_start);
bool sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id); bool
bool sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval); sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id);
void cleanup_app_timers(timer_ctx_t ctx); bool
uint32 check_app_timers(timer_ctx_t ctx); sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id);
uint32 get_expiry_ms(timer_ctx_t ctx); bool
sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval);
void
cleanup_app_timers(timer_ctx_t ctx);
uint32
check_app_timers(timer_ctx_t ctx);
uint32
get_expiry_ms(timer_ctx_t ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -9,19 +9,22 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
char* optarg = NULL; char *optarg = NULL;
int optind = 1; int optind = 1;
int getopt(int argc, char *const argv[], const char *optstring) int
getopt(int argc, char *const argv[], const char *optstring)
{ {
static int sp = 1; static int sp = 1;
int opt; int opt;
char *p; char *p;
if (sp == 1) { if (sp == 1) {
if ((optind >= argc) || (argv[optind][0] != '-') || (argv[optind][1] == 0)){ if ((optind >= argc) || (argv[optind][0] != '-')
|| (argv[optind][1] == 0)) {
return -1; return -1;
} else if (!strcmp(argv[optind], "--")) { }
else if (!strcmp(argv[optind], "--")) {
optind++; optind++;
return -1; return -1;
} }
@ -31,24 +34,26 @@ int getopt(int argc, char *const argv[], const char *optstring)
p = strchr(optstring, opt); p = strchr(optstring, opt);
if (opt == ':' || p == NULL) { if (opt == ':' || p == NULL) {
printf("illegal option : '-%c'\n", opt); printf("illegal option : '-%c'\n", opt);
if ( argv[optind][++sp] == '\0') { if (argv[optind][++sp] == '\0') {
optind ++; optind++;
sp = 1; sp = 1;
} }
return ('?'); return ('?');
} }
if (p[1] == ':') { if (p[1] == ':') {
if (argv[optind][sp + 1] != '\0') if (argv[optind][sp + 1] != '\0')
optarg = &argv[optind++][sp + 1]; optarg = &argv[optind++][sp + 1];
else if (++optind >= argc) { else if (++optind >= argc) {
printf("option '-%c' requires an argument :\n", opt); printf("option '-%c' requires an argument :\n", opt);
sp = 1; sp = 1;
return ('?'); return ('?');
} else { }
else {
optarg = argv[optind++]; optarg = argv[optind++];
} }
sp = 1; sp = 1;
} else { }
else {
if (argv[optind][++sp] == '\0') { if (argv[optind][++sp] == '\0') {
sp = 1; sp = 1;
optind++; optind++;

View File

@ -17,7 +17,8 @@ extern "C" {
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
int getopt(int argc, char *const argv[], const char *optstring); int
getopt(int argc, char *const argv[], const char *optstring);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -9,7 +9,7 @@
#endif #endif
#if defined(_WIN32) || defined(_WIN32_) #if defined(_WIN32) || defined(_WIN32_)
char* char *
bh_read_file_to_buffer(const char *filename, uint32 *ret_size) bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{ {
char *buffer; char *buffer;
@ -22,15 +22,13 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
return NULL; return NULL;
} }
if (_sopen_s(&file, filename, _O_RDONLY| _O_BINARY, _SH_DENYNO, 0)) { if (_sopen_s(&file, filename, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0)) {
printf("Read file to buffer failed: open file %s failed.\n", printf("Read file to buffer failed: open file %s failed.\n", filename);
filename);
return NULL; return NULL;
} }
if (fstat(file, &stat_buf) != 0) { if (fstat(file, &stat_buf) != 0) {
printf("Read file to buffer failed: fstat file %s failed.\n", printf("Read file to buffer failed: fstat file %s failed.\n", filename);
filename);
_close(file); _close(file);
return NULL; return NULL;
} }
@ -61,7 +59,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
return buffer; return buffer;
} }
#else /* else of defined(_WIN32) || defined(_WIN32_) */ #else /* else of defined(_WIN32) || defined(_WIN32_) */
char* char *
bh_read_file_to_buffer(const char *filename, uint32 *ret_size) bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{ {
char *buffer; char *buffer;
@ -75,14 +73,12 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
} }
if ((file = open(filename, O_RDONLY, 0)) == -1) { if ((file = open(filename, O_RDONLY, 0)) == -1) {
printf("Read file to buffer failed: open file %s failed.\n", printf("Read file to buffer failed: open file %s failed.\n", filename);
filename);
return NULL; return NULL;
} }
if (fstat(file, &stat_buf) != 0) { if (fstat(file, &stat_buf) != 0) {
printf("Read file to buffer failed: fstat file %s failed.\n", printf("Read file to buffer failed: fstat file %s failed.\n", filename);
filename);
close(file); close(file);
return NULL; return NULL;
} }

View File

@ -20,4 +20,3 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
#endif #endif
#endif /* end of _BH_FILE_H */ #endif /* end of _BH_FILE_H */

View File

@ -5,9 +5,11 @@
#include "stdio.h" #include "stdio.h"
void print_line(char* str); void
print_line(char *str);
int main() int
main()
{ {
print_line("Hello World!"); print_line("Hello World!");
print_line("Wasm Micro Runtime"); print_line("Wasm Micro Runtime");

View File

@ -6,7 +6,8 @@
#include "stdio.h" #include "stdio.h"
#include "string.h" #include "string.h"
void print_line(char* str) void
print_line(char *str)
{ {
printf("%s\n", str); printf("%s\n", str);
} }

View File

@ -6,7 +6,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int main(int argc, char **argv) int
main(int argc, char **argv)
{ {
char *buf; char *buf;

View File

@ -24,10 +24,10 @@ static char **app_argv;
* @return true if the main function is called, false otherwise. * @return true if the main function is called, false otherwise.
*/ */
bool bool
wasm_application_execute_main(wasm_module_inst_t module_inst, wasm_application_execute_main(wasm_module_inst_t module_inst, int32_t argc,
int32_t argc, char *argv[]); char *argv[]);
static void* static void *
app_instance_main(wasm_module_inst_t module_inst) app_instance_main(wasm_module_inst_t module_inst)
{ {
const char *exception; const char *exception;
@ -40,7 +40,8 @@ app_instance_main(wasm_module_inst_t module_inst)
static char global_heap_buf[256 * 1024] = { 0 }; static char global_heap_buf[256 * 1024] = { 0 };
void iwasm_main(void *arg1) void
iwasm_main(void *arg1)
{ {
uint8 *wasm_file_buf = NULL; uint8 *wasm_file_buf = NULL;
uint32 wasm_file_size; uint32 wasm_file_size;
@ -52,7 +53,7 @@ void iwasm_main(void *arg1)
int log_verbose_level = 2; int log_verbose_level = 2;
#endif #endif
(void) arg1; (void)arg1;
memset(&init_args, 0, sizeof(RuntimeInitArgs)); memset(&init_args, 0, sizeof(RuntimeInitArgs));
@ -71,22 +72,19 @@ void iwasm_main(void *arg1)
#endif #endif
/* load WASM byte buffer from byte buffer of include file */ /* load WASM byte buffer from byte buffer of include file */
wasm_file_buf = (uint8*) wasm_test_file; wasm_file_buf = (uint8 *)wasm_test_file;
wasm_file_size = sizeof(wasm_test_file); wasm_file_size = sizeof(wasm_test_file);
/* load WASM module */ /* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) { error_buf, sizeof(error_buf)))) {
printf("%s\n", error_buf); printf("%s\n", error_buf);
goto fail1; goto fail1;
} }
/* instantiate the module */ /* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, if (!(wasm_module_inst = wasm_runtime_instantiate(
8 * 1024, wasm_module, 8 * 1024, 8 * 1024, error_buf, sizeof(error_buf)))) {
8 * 1024,
error_buf,
sizeof(error_buf)))) {
printf("%s\n", error_buf); printf("%s\n", error_buf);
goto fail2; goto fail2;
} }
@ -108,10 +106,10 @@ fail1:
#define DEFAULT_THREAD_STACKSIZE (6 * 1024) #define DEFAULT_THREAD_STACKSIZE (6 * 1024)
#define DEFAULT_THREAD_PRIORITY 50 #define DEFAULT_THREAD_PRIORITY 50
bool iwasm_init(void) bool
iwasm_init(void)
{ {
int ret = aos_task_new("wasm-main", iwasm_main, NULL, int ret =
DEFAULT_THREAD_STACKSIZE); aos_task_new("wasm-main", iwasm_main, NULL, DEFAULT_THREAD_STACKSIZE);
return ret == 0 ? true : false; return ret == 0 ? true : false;
} }

View File

@ -7,10 +7,11 @@
#include <wasm_export.h> #include <wasm_export.h>
#define LOGI(...) \ #define LOGI(...) \
((void)__android_log_print(ANDROID_LOG_INFO, "wasm_jni::", __VA_ARGS__)) ((void)__android_log_print(ANDROID_LOG_INFO, "wasm_jni::", __VA_ARGS__))
static void * static void *
app_instance_main(wasm_module_inst_t module_inst) { app_instance_main(wasm_module_inst_t module_inst)
{
const char *exception; const char *exception;
wasm_application_execute_main(module_inst, 0, NULL); wasm_application_execute_main(module_inst, 0, NULL);
@ -58,20 +59,21 @@ static unsigned char wasm_test_file[] = {
}; };
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) { Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz)
{
wasm_module_t wasm_module = NULL; wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL; wasm_module_inst_t wasm_module_inst = NULL;
RuntimeInitArgs init_args; RuntimeInitArgs init_args;
uint wasm_file_size = 0; uint wasm_file_size = 0;
uint8_t *wasm_file_buf = NULL; uint8_t *wasm_file_buf = NULL;
char error_buf[128] = {0}; char error_buf[128] = { 0 };
memset(&init_args, 0, sizeof(RuntimeInitArgs)); memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void*)malloc; init_args.mem_alloc_option.allocator.malloc_func = (void *)malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void*)realloc; init_args.mem_alloc_option.allocator.realloc_func = (void *)realloc;
init_args.mem_alloc_option.allocator.free_func = (void*)free; init_args.mem_alloc_option.allocator.free_func = (void *)free;
LOGI("wasm_runtime_full_init"); LOGI("wasm_runtime_full_init");
/* initialize runtime environment */ /* initialize runtime environment */
@ -82,7 +84,7 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
/* load WASM byte buffer from a preinstall WASM bin file */ /* load WASM byte buffer from a preinstall WASM bin file */
LOGI("use an internal test file, gona to output Hello World in logcat\n"); LOGI("use an internal test file, gona to output Hello World in logcat\n");
wasm_file_buf = (uint8_t*) wasm_test_file; wasm_file_buf = (uint8_t *)wasm_test_file;
wasm_file_size = sizeof(wasm_test_file); wasm_file_size = sizeof(wasm_test_file);
/* load WASM module */ /* load WASM module */
@ -96,11 +98,10 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
/* instantiate the module */ /* instantiate the module */
LOGI("wasm_runtime_instantiate"); LOGI("wasm_runtime_instantiate");
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, if (!(wasm_module_inst =
64 * 1024, /* stack size */ wasm_runtime_instantiate(wasm_module, 64 * 1024, /* stack size */
64 * 1024, /* heap size */ 64 * 1024, /* heap size */
error_buf, error_buf, sizeof(error_buf)))) {
sizeof(error_buf)))) {
LOGI("%s\n", error_buf); LOGI("%s\n", error_buf);
LOGI("goto fail2\n"); LOGI("goto fail2\n");
goto fail2; goto fail2;
@ -121,7 +122,7 @@ fail2:
fail1: fail1:
// in our case, we don't need a free, but it is not a typical one // in our case, we don't need a free, but it is not a typical one
/* free the file buffer */ /* free the file buffer */
//bh_free((void *) wasm_file_buf); // bh_free((void *) wasm_file_buf);
/* destroy runtime environment */ /* destroy runtime environment */
LOGI("wasm_runtime_destroy"); LOGI("wasm_runtime_destroy");

View File

@ -14,7 +14,7 @@
static int app_argc; static int app_argc;
static char **app_argv; static char **app_argv;
static void* static void *
app_instance_main(wasm_module_inst_t module_inst) app_instance_main(wasm_module_inst_t module_inst)
{ {
const char *exception; const char *exception;
@ -27,7 +27,8 @@ app_instance_main(wasm_module_inst_t module_inst)
static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 }; static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 };
void iwasm_main(void) void
iwasm_main(void)
{ {
uint8 *wasm_file_buf = NULL; uint8 *wasm_file_buf = NULL;
uint32 wasm_file_size; uint32 wasm_file_size;
@ -60,7 +61,7 @@ void iwasm_main(void)
#endif #endif
/* load WASM byte buffer from byte buffer of include file */ /* load WASM byte buffer from byte buffer of include file */
wasm_file_buf = (uint8*)wasm_test_file; wasm_file_buf = (uint8 *)wasm_test_file;
wasm_file_size = sizeof(wasm_test_file); wasm_file_size = sizeof(wasm_test_file);
/* load WASM module */ /* load WASM module */
@ -73,11 +74,9 @@ void iwasm_main(void)
os_printf("### wasm runtime load module success.\n"); os_printf("### wasm runtime load module success.\n");
/* instantiate the module */ /* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, if (!(wasm_module_inst = wasm_runtime_instantiate(
CONFIG_APP_STACK_SIZE, wasm_module, CONFIG_APP_STACK_SIZE, CONFIG_APP_HEAP_SIZE,
CONFIG_APP_HEAP_SIZE, error_buf, sizeof(error_buf)))) {
error_buf,
sizeof(error_buf)))) {
os_printf("%s\n", error_buf); os_printf("%s\n", error_buf);
goto fail2; goto fail2;
} }

View File

@ -26,7 +26,7 @@
#define FALSE 0 #define FALSE 0
#endif #endif
#define TOKEN_FILENAME "enclave.token" #define TOKEN_FILENAME "enclave.token"
#define ENCLAVE_FILENAME "enclave.signed.so" #define ENCLAVE_FILENAME "enclave.signed.so"
#define MAX_PATH 1024 #define MAX_PATH 1024
@ -41,7 +41,7 @@ pal_get_enclave_id(void)
} }
void void
ocall_print(const char* str) ocall_print(const char *str)
{ {
printf("%s", str); printf("%s", str);
} }
@ -50,8 +50,7 @@ static char *
get_exe_path(char *path_buf, unsigned path_buf_size) get_exe_path(char *path_buf, unsigned path_buf_size)
{ {
ssize_t i; ssize_t i;
ssize_t size = readlink("/proc/self/exe", ssize_t size = readlink("/proc/self/exe", path_buf, path_buf_size - 1);
path_buf, path_buf_size - 1);
if (size < 0 || (size >= path_buf_size - 1)) { if (size < 0 || (size >= path_buf_size - 1)) {
return NULL; return NULL;
@ -99,8 +98,9 @@ enclave_init(sgx_enclave_id_t *p_eid)
home_dir = getpwuid(getuid())->pw_dir; home_dir = getpwuid(getuid())->pw_dir;
size_t home_dir_len = home_dir ? strlen(home_dir) : 0; size_t home_dir_len = home_dir ? strlen(home_dir) : 0;
if (home_dir != NULL && if (home_dir != NULL
home_dir_len <= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) { && home_dir_len
<= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) {
/* compose the token path */ /* compose the token path */
strncpy(token_path, home_dir, MAX_PATH); strncpy(token_path, home_dir, MAX_PATH);
strncat(token_path, "/", strlen("/")); strncat(token_path, "/", strlen("/"));
@ -123,18 +123,19 @@ enclave_init(sgx_enclave_id_t *p_eid)
if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) { if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
/* if token is invalid, clear the buffer */ /* if token is invalid, clear the buffer */
memset(&token, 0x0, sizeof(sgx_launch_token_t)); memset(&token, 0x0, sizeof(sgx_launch_token_t));
printf("Warning: Invalid launch token read from \"%s\".\n", token_path); printf("Warning: Invalid launch token read from \"%s\".\n",
token_path);
} }
} }
/* Step 2: call sgx_create_enclave to initialize an enclave instance */ /* Step 2: call sgx_create_enclave to initialize an enclave instance */
/* Debug Support: set 2nd parameter to 1 */ /* Debug Support: set 2nd parameter to 1 */
ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated,
&token, &updated, p_eid, NULL); p_eid, NULL);
if (ret != SGX_SUCCESS) if (ret != SGX_SUCCESS)
/* Try to load enclave.sign.so from the path of exe file */ /* Try to load enclave.sign.so from the path of exe file */
ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated,
&token, &updated, p_eid, NULL); p_eid, NULL);
if (ret != SGX_SUCCESS) { if (ret != SGX_SUCCESS) {
printf("Failed to create enclave from %s, error code: %d\n", printf("Failed to create enclave from %s, error code: %d\n",
ENCLAVE_FILENAME, ret); ENCLAVE_FILENAME, ret);
@ -145,8 +146,10 @@ enclave_init(sgx_enclave_id_t *p_eid)
/* Step 3: save the launch token if it is updated */ /* Step 3: save the launch token if it is updated */
if (updated == FALSE || fp == NULL) { if (updated == FALSE || fp == NULL) {
/* if the token is not updated, or file handler is invalid, do not perform saving */ /* if the token is not updated, or file handler is invalid,
if (fp != NULL) fclose(fp); do not perform saving */
if (fp != NULL)
fclose(fp);
return 0; return 0;
} }
@ -176,8 +179,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size)
} }
if (!(file = fopen(filename, "r"))) { if (!(file = fopen(filename, "r"))) {
printf("Read file to buffer failed: open file %s failed.\n", printf("Read file to buffer failed: open file %s failed.\n", filename);
filename);
return NULL; return NULL;
} }
@ -185,7 +187,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size)
file_size = ftell(file); file_size = ftell(file);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
if (!(buffer = (unsigned char*)malloc(file_size))) { if (!(buffer = (unsigned char *)malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n"); printf("Read file to buffer failed: alloc memory failed.\n");
fclose(file); fclose(file);
return NULL; return NULL;
@ -205,6 +207,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size)
return buffer; return buffer;
} }
/* clang-format off */
static int static int
print_help() print_help()
{ {
@ -227,6 +230,7 @@ print_help()
printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n"); printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
return 1; return 1;
} }
/* clang-format on */
/** /**
* Split a space separated strings into an array of strings * Split a space separated strings into an array of strings
@ -287,8 +291,8 @@ typedef enum EcallCmd {
} EcallCmd; } EcallCmd;
static void static void
app_instance_func(void *wasm_module_inst, const char *func_name, app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc,
int app_argc, char **app_argv); char **app_argv);
static void * static void *
app_instance_repl(void *module_inst, int app_argc, char **app_argv) app_instance_repl(void *module_inst, int app_argc, char **app_argv)
@ -315,8 +319,8 @@ app_instance_repl(void *module_inst, int app_argc, char **app_argv)
break; break;
} }
if (app_argc != 0) { if (app_argc != 0) {
app_instance_func(module_inst, app_argv[0], app_instance_func(module_inst, app_argv[0], app_argc - 1,
app_argc - 1, app_argv + 1); app_argv + 1);
} }
free(app_argv); free(app_argv);
} }
@ -349,9 +353,9 @@ set_log_verbose_level(int log_verbose_level)
/* Set log verbose level */ /* Set log verbose level */
if (log_verbose_level != 2) { if (log_verbose_level != 2) {
ecall_args[0] = log_verbose_level; ecall_args[0] = log_verbose_level;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL,
sizeof(uint64_t))) { (uint8_t *)ecall_args, sizeof(uint64_t))) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
return false; return false;
} }
@ -366,9 +370,9 @@ init_runtime(bool alloc_with_pool, uint32_t max_thread_num)
ecall_args[0] = alloc_with_pool; ecall_args[0] = alloc_with_pool;
ecall_args[1] = max_thread_num; ecall_args[1] = max_thread_num;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INIT_RUNTIME, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_INIT_RUNTIME, (uint8_t *)ecall_args,
sizeof(uint64_t) * 2)) { sizeof(uint64_t) * 2)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
return false; return false;
} }
@ -382,15 +386,15 @@ init_runtime(bool alloc_with_pool, uint32_t max_thread_num)
static void static void
destroy_runtime() destroy_runtime()
{ {
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, if (SGX_SUCCESS
NULL, 0)) { != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, NULL, 0)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
} }
static void * static void *
load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, char *error_buf,
char *error_buf, uint32_t error_buf_size) uint32_t error_buf_size)
{ {
uint64_t ecall_args[4]; uint64_t ecall_args[4];
@ -398,9 +402,9 @@ load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size,
ecall_args[1] = wasm_file_size; ecall_args[1] = wasm_file_size;
ecall_args[2] = (uint64_t)(uintptr_t)error_buf; ecall_args[2] = (uint64_t)(uintptr_t)error_buf;
ecall_args[3] = error_buf_size; ecall_args[3] = error_buf_size;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_LOAD_MODULE, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_LOAD_MODULE, (uint8_t *)ecall_args,
sizeof(uint64_t) * 4)) { sizeof(uint64_t) * 4)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
return NULL; return NULL;
} }
@ -414,16 +418,15 @@ unload_module(void *wasm_module)
uint64_t ecall_args[1]; uint64_t ecall_args[1];
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module; ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, (uint8_t *)ecall_args,
sizeof(uint64_t))) { sizeof(uint64_t))) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
} }
static void * static void *
instantiate_module(void *wasm_module, instantiate_module(void *wasm_module, uint32_t stack_size, uint32_t heap_size,
uint32_t stack_size, uint32_t heap_size,
char *error_buf, uint32_t error_buf_size) char *error_buf, uint32_t error_buf_size)
{ {
uint64_t ecall_args[5]; uint64_t ecall_args[5];
@ -433,9 +436,9 @@ instantiate_module(void *wasm_module,
ecall_args[2] = heap_size; ecall_args[2] = heap_size;
ecall_args[3] = (uint64_t)(uintptr_t)error_buf; ecall_args[3] = (uint64_t)(uintptr_t)error_buf;
ecall_args[4] = error_buf_size; ecall_args[4] = error_buf_size;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE,
sizeof(uint64_t) * 5)) { (uint8_t *)ecall_args, sizeof(uint64_t) * 5)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
return NULL; return NULL;
} }
@ -449,25 +452,24 @@ deinstantiate_module(void *wasm_module_inst)
uint64_t ecall_args[1]; uint64_t ecall_args[1];
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst; ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE,
sizeof(uint64_t))) { (uint8_t *)ecall_args, sizeof(uint64_t))) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
} }
static bool static bool
get_exception(void *wasm_module_inst, get_exception(void *wasm_module_inst, char *exception, uint32_t exception_size)
char *exception, uint32_t exception_size)
{ {
uint64_t ecall_args[3]; uint64_t ecall_args[3];
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst; ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
ecall_args[1] = (uint64_t)(uintptr_t)exception; ecall_args[1] = (uint64_t)(uintptr_t)exception;
ecall_args[2] = exception_size; ecall_args[2] = exception_size;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_GET_EXCEPTION, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_GET_EXCEPTION, (uint8_t *)ecall_args,
sizeof(uint64_t) * 3)) { sizeof(uint64_t) * 3)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
@ -475,16 +477,15 @@ get_exception(void *wasm_module_inst,
} }
static void static void
app_instance_main(void *wasm_module_inst, app_instance_main(void *wasm_module_inst, int app_argc, char **app_argv)
int app_argc, char **app_argv)
{ {
char exception[128]; char exception[128];
uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf; uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
int i, size; int i, size;
if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) { if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
if (!(ecall_args = (uint64_t *) if (!(ecall_args =
malloc(sizeof(uint64_t) * (app_argc + 2)))) { (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 2)))) {
printf("Allocate memory failed.\n"); printf("Allocate memory failed.\n");
return; return;
} }
@ -497,9 +498,9 @@ app_instance_main(void *wasm_module_inst,
} }
size = (uint32_t)sizeof(uint64_t) * (app_argc + 2); size = (uint32_t)sizeof(uint64_t) * (app_argc + 2);
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, (uint8_t *)ecall_args,
size)) { size)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
@ -513,15 +514,15 @@ app_instance_main(void *wasm_module_inst,
} }
static void static void
app_instance_func(void *wasm_module_inst, const char *func_name, app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc,
int app_argc, char **app_argv) char **app_argv)
{ {
uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf; uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
int i, size; int i, size;
if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) { if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
if (!(ecall_args = (uint64_t *) if (!(ecall_args =
malloc(sizeof(uint64_t) * (app_argc + 3)))) { (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 3)))) {
printf("Allocate memory failed.\n"); printf("Allocate memory failed.\n");
return; return;
} }
@ -535,9 +536,9 @@ app_instance_func(void *wasm_module_inst, const char *func_name,
} }
size = (uint32_t)sizeof(uint64_t) * (app_argc + 3); size = (uint32_t)sizeof(uint64_t) * (app_argc + 3);
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, (uint8_t *)ecall_args,
size)) { size)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
@ -547,11 +548,9 @@ app_instance_func(void *wasm_module_inst, const char *func_name,
} }
static bool static bool
set_wasi_args(void *wasm_module, set_wasi_args(void *wasm_module, const char **dir_list, uint32_t dir_list_size,
const char **dir_list, uint32_t dir_list_size, const char **env_list, uint32_t env_list_size, int stdinfd,
const char **env_list, uint32_t env_list_size, int stdoutfd, int stderrfd, char **argv, uint32_t argc)
int stdinfd, int stdoutfd, int stderrfd,
char **argv, uint32_t argc)
{ {
uint64_t ecall_args[10]; uint64_t ecall_args[10];
@ -565,9 +564,9 @@ set_wasi_args(void *wasm_module,
ecall_args[7] = stderrfd; ecall_args[7] = stderrfd;
ecall_args[8] = (uint64_t)(uintptr_t)argv; ecall_args[8] = (uint64_t)(uintptr_t)argv;
ecall_args[9] = argc; ecall_args[9] = argc;
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, if (SGX_SUCCESS
(uint8_t *)ecall_args, != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, (uint8_t *)ecall_args,
sizeof(uint64_t) * 10)) { sizeof(uint64_t) * 10)) {
printf("Call ecall_handle_command() failed.\n"); printf("Call ecall_handle_command() failed.\n");
} }
@ -693,29 +692,28 @@ main(int argc, char *argv[])
/* Load WASM byte buffer from WASM bin file */ /* Load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = if (!(wasm_file_buf =
(uint8_t *)read_file_to_buffer(wasm_file, &wasm_file_size))) { (uint8_t *)read_file_to_buffer(wasm_file, &wasm_file_size))) {
goto fail1; goto fail1;
} }
/* Load module */ /* Load module */
if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, error_buf,
error_buf, sizeof(error_buf)))) { sizeof(error_buf)))) {
printf("%s\n", error_buf); printf("%s\n", error_buf);
goto fail2; goto fail2;
} }
/* Set wasi arguments */ /* Set wasi arguments */
if (!set_wasi_args(wasm_module, dir_list, dir_list_size, if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list,
env_list, env_list_size, 0, 1, 2, argv, argc)) { env_list_size, 0, 1, 2, argv, argc)) {
printf("%s\n", "set wasi arguments failed.\n"); printf("%s\n", "set wasi arguments failed.\n");
goto fail3; goto fail3;
} }
/* Instantiate module */ /* Instantiate module */
if (!(wasm_module_inst = instantiate_module(wasm_module, if (!(wasm_module_inst =
stack_size, heap_size, instantiate_module(wasm_module, stack_size, heap_size, error_buf,
error_buf, sizeof(error_buf)))) {
sizeof(error_buf)))) {
printf("%s\n", error_buf); printf("%s\n", error_buf);
goto fail3; goto fail3;
} }
@ -723,8 +721,7 @@ main(int argc, char *argv[])
if (is_repl_mode) if (is_repl_mode)
app_instance_repl(wasm_module_inst, argc, argv); app_instance_repl(wasm_module_inst, argc, argv);
else if (func_name) else if (func_name)
app_instance_func(wasm_module_inst, func_name, app_instance_func(wasm_module_inst, func_name, argc - 1, argv + 1);
argc - 1, argv + 1);
else else
app_instance_main(wasm_module_inst, argc, argv); app_instance_main(wasm_module_inst, argc, argv);
@ -782,19 +779,19 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
int stderrfd = -1; int stderrfd = -1;
int argc = 2; int argc = 2;
char *argv[argc] = { (char*)"./iwasm", (char *)args->argv[0] }; char *argv[argc] = { (char *)"./iwasm", (char *)args->argv[0] };
uint8_t *wasm_files_buf = NULL; uint8_t *wasm_files_buf = NULL;
void *wasm_modules = NULL; void *wasm_modules = NULL;
int len = 0, i; int len = 0, i;
char *temp = (char *)args->argv[0]; char *temp = (char *)args->argv[0];
while(temp) { while (temp) {
len++; len++;
temp=(char *)args->argv[len]; temp = (char *)args->argv[len];
} }
if (len > sizeof(wasm_files)/sizeof(char *)) { if (len > sizeof(wasm_files) / sizeof(char *)) {
printf("Number of input files is out of range\n"); printf("Number of input files is out of range\n");
return -1; return -1;
} }
@ -829,8 +826,8 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
char error_buf[128] = { 0 }; char error_buf[128] = { 0 };
/* Load WASM byte buffer from WASM bin file */ /* Load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer(
(wasm_files[i], &wasm_file_size))) { wasm_files[i], &wasm_file_size))) {
printf("Failed to read file to buffer\n"); printf("Failed to read file to buffer\n");
destroy_runtime(); destroy_runtime();
return -1; return -1;
@ -846,10 +843,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
} }
/* Set wasi arguments */ /* Set wasi arguments */
if (!set_wasi_args(wasm_module, dir_list, dir_list_size, if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list,
env_list, env_list_size, env_list_size, stdinfd, stdoutfd, stderrfd, argv,
stdinfd, stdoutfd, stderrfd, argc)) {
argv, argc)) {
printf("%s\n", "set wasi arguments failed.\n"); printf("%s\n", "set wasi arguments failed.\n");
unload_module(wasm_module); unload_module(wasm_module);
free(wasm_file_buf); free(wasm_file_buf);
@ -858,10 +854,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
} }
/* Instantiate module */ /* Instantiate module */
if (!(wasm_module_inst[i] = instantiate_module(wasm_module, if (!(wasm_module_inst[i] =
stack_size, heap_size, instantiate_module(wasm_module, stack_size, heap_size,
error_buf, error_buf, sizeof(error_buf)))) {
sizeof(error_buf)))) {
printf("%s\n", error_buf); printf("%s\n", error_buf);
unload_module(wasm_module); unload_module(wasm_module);
free(wasm_file_buf); free(wasm_file_buf);
@ -884,37 +879,43 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
int int
wamr_pal_destroy(void) wamr_pal_destroy(void)
{ {
//sgx_destroy_enclave(g_eid); // sgx_destroy_enclave(g_eid);
return 0; return 0;
} }
int int
wamr_pal_exec(struct wamr_pal_exec_args *args) wamr_pal_exec(struct wamr_pal_exec_args *args)
{ {
//app_instance_main(wasm_module_inst[i], argc, argv); // app_instance_main(wasm_module_inst[i], argc, argv);
return 0; return 0;
} }
int int
wamr_pal_kill(int pid, int sig) wamr_pal_kill(int pid, int sig)
{ {
//deinstantiate_module(wasm_module_inst[i]); // deinstantiate_module(wasm_module_inst[i]);
//unload_module(wasm_module); // unload_module(wasm_module);
//free(wasm_file_buf); // free(wasm_file_buf);
return 0; return 0;
} }
int pal_get_version(void) __attribute__((weak, alias ("wamr_pal_get_version"))); int
pal_get_version(void) __attribute__((weak, alias("wamr_pal_get_version")));
int pal_init(const struct wamr_pal_attr *attr)\ int
__attribute__ ((weak, alias ("wamr_pal_init"))); pal_init(const struct wamr_pal_attr *attr)
__attribute__((weak, alias("wamr_pal_init")));
int pal_create_process(struct wamr_pal_create_process_args *args)\ int
__attribute__ ((weak, alias ("wamr_pal_create_process"))); pal_create_process(struct wamr_pal_create_process_args *args)
__attribute__((weak, alias("wamr_pal_create_process")));
int pal_exec(struct wamr_pal_exec_args *args)\ int
__attribute__ ((weak, alias ("wamr_pal_exec"))); pal_exec(struct wamr_pal_exec_args *args)
__attribute__((weak, alias("wamr_pal_exec")));
int pal_kill(int pid, int sig) __attribute__ ((weak, alias ("wamr_pal_kill"))); int
pal_kill(int pid, int sig) __attribute__((weak, alias("wamr_pal_kill")));
int pal_destroy(void) __attribute__ ((weak, alias ("wamr_pal_destroy"))); int
pal_destroy(void) __attribute__((weak, alias("wamr_pal_destroy")));

View File

@ -20,7 +20,8 @@ extern "C" {
* *
* @retval If > 0, then success; otherwise, it is an invalid version. * @retval If > 0, then success; otherwise, it is an invalid version.
*/ */
int wamr_pal_get_version(void); int
wamr_pal_get_version(void);
/* /*
* WAMR PAL attributes * WAMR PAL attributes
@ -29,19 +30,21 @@ typedef struct wamr_pal_attr {
// WAMR instance directory. // WAMR instance directory.
// //
// The default value is "."; that is, the current working directory // The default value is "."; that is, the current working directory
const char *instance_dir; const char *instance_dir;
// Log level. // Log level.
// //
// Specifies the log verbose level (0 to 5, default is 2) // Specifies the log verbose level (0 to 5, default is 2)
// large level with more log // large level with more log
// //
const char *log_level; const char *log_level;
} wamr_pal_attr_t; } wamr_pal_attr_t;
/* clang-format off */
#define WAMR_PAL_ATTR_INITVAL { \ #define WAMR_PAL_ATTR_INITVAL { \
.instance_dir = ".", \ .instance_dir = ".", \
.log_level = 2 \ .log_level = 2 \
} }
/* clang-format on */
/* /*
* The struct which consists of file descriptors of standard I/O * The struct which consists of file descriptors of standard I/O
@ -66,21 +69,23 @@ struct wamr_pal_create_process_args {
// Argments array pass to new process. // Argments array pass to new process.
// //
// The arguments to the command. By convention, the argv[0] should be the program name. // The arguments to the command. By convention, the argv[0] should be the
// And the last element of the array must be NULL to indicate the length of array. // program name. And the last element of the array must be NULL to indicate
// the length of array.
// //
// Mandatory field. Must not be NULL. // Mandatory field. Must not be NULL.
const char **argv; const char **argv;
// Untrusted environment variable array pass to new process. // Untrusted environment variable array pass to new process.
// //
// The untrusted env vars to the command. And the last element of the array must be // The untrusted env vars to the command. And the last element of the array
// NULL to indicate the length of array. // must be NULL to indicate the length of array.
// //
// Optional field. // Optional field.
const char **env; const char **env;
// File descriptors of the redirected standard I/O (i.e., stdin, stdout, stderr) // File descriptors of the redirected standard I/O (i.e., stdin, stdout,
// stderr)
// //
// If set to NULL, will use the original standard I/O file descriptors. // If set to NULL, will use the original standard I/O file descriptors.
// //
@ -115,32 +120,41 @@ struct wamr_pal_exec_args {
int *exit_value; int *exit_value;
}; };
int wamr_pal_init(const struct wamr_pal_attr *args); int
wamr_pal_init(const struct wamr_pal_attr *args);
int wamr_pal_create_process(struct wamr_pal_create_process_args *args); int
wamr_pal_create_process(struct wamr_pal_create_process_args *args);
int wamr_pal_destroy(void); int
wamr_pal_destroy(void);
int wamr_pal_exec(struct wamr_pal_exec_args *args); int
wamr_pal_exec(struct wamr_pal_exec_args *args);
int wamr_pal_kill(int pid, int sig); int
wamr_pal_kill(int pid, int sig);
int pal_get_version(void); int
pal_get_version(void);
int pal_init(const struct wamr_pal_attr *attr); int
pal_init(const struct wamr_pal_attr *attr);
int pal_create_process(struct wamr_pal_create_process_args *args); int
pal_create_process(struct wamr_pal_create_process_args *args);
int pal_exec(struct wamr_pal_exec_args *args); int
pal_exec(struct wamr_pal_exec_args *args);
int pal_kill(int pid, int sig); int
pal_kill(int pid, int sig);
int pal_destroy(void);
int
pal_destroy(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* __WAMR_PAL_API_H__ */ #endif /* __WAMR_PAL_API_H__ */

View File

@ -13,14 +13,15 @@
#include "bh_platform.h" #include "bh_platform.h"
extern "C" { extern "C" {
typedef void (*os_print_function_t)(const char* message); typedef void (*os_print_function_t)(const char *message);
extern void os_set_print_function(os_print_function_t pf); extern void
os_set_print_function(os_print_function_t pf);
void void
enclave_print(const char *message) enclave_print(const char *message)
{ {
ocall_print(message); ocall_print(message);
} }
} }
typedef enum EcallCmd { typedef enum EcallCmd {
@ -130,8 +131,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
bh_assert(argc == 4); bh_assert(argc == 4);
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(enclave_module = (EnclaveModule *) || !(enclave_module =
wasm_runtime_malloc((uint32)total_size))) { (EnclaveModule *)wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"WASM module load failed: " "WASM module load failed: "
"allocate memory failed."); "allocate memory failed.");
@ -140,14 +141,13 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
} }
memset(enclave_module, 0, (uint32)total_size); memset(enclave_module, 0, (uint32)total_size);
enclave_module->wasm_file = (uint8 *)enclave_module enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule);
+ sizeof(EnclaveModule); bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file,
bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file_size);
wasm_file, wasm_file_size);
if (!(enclave_module->module = if (!(enclave_module->module =
wasm_runtime_load(enclave_module->wasm_file, wasm_file_size, wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
error_buf, error_buf_size))) { error_buf, error_buf_size))) {
wasm_runtime_free(enclave_module); wasm_runtime_free(enclave_module);
*(void **)args_org = NULL; *(void **)args_org = NULL;
return; return;
@ -189,9 +189,8 @@ handle_cmd_instantiate_module(uint64 *args, uint32 argc)
bh_assert(argc == 5); bh_assert(argc == 5);
if (!(module_inst = if (!(module_inst =
wasm_runtime_instantiate(enclave_module->module, wasm_runtime_instantiate(enclave_module->module, stack_size,
stack_size, heap_size, heap_size, error_buf, error_buf_size))) {
error_buf, error_buf_size))) {
*(void **)args_org = NULL; *(void **)args_org = NULL;
return; return;
} }
@ -225,8 +224,7 @@ handle_cmd_get_exception(uint64 *args, uint32 argc)
bh_assert(argc == 3); bh_assert(argc == 3);
if ((exception1 = wasm_runtime_get_exception(module_inst))) { if ((exception1 = wasm_runtime_get_exception(module_inst))) {
snprintf(exception, exception_size, snprintf(exception, exception_size, "%s", exception1);
"%s", exception1);
args_org[0] = true; args_org[0] = true;
} }
else { else {
@ -339,15 +337,14 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
} }
if (total_size >= UINT32_MAX if (total_size >= UINT32_MAX
|| !(enclave_module->wasi_arg_buf = p = (char *) || !(enclave_module->wasi_arg_buf = p =
wasm_runtime_malloc((uint32)total_size))) { (char *)wasm_runtime_malloc((uint32)total_size))) {
*args_org = false; *args_org = false;
return; return;
} }
p1 = p + sizeof(char *) * dir_list_size p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size
+ sizeof(char *) * env_list_size + sizeof(char *) * wasi_argc;
+ sizeof(char *) * wasi_argc;
if (dir_list_size > 0) { if (dir_list_size > 0) {
enclave_module->wasi_dir_list = (char **)p; enclave_module->wasi_dir_list = (char **)p;
@ -385,17 +382,12 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
p += sizeof(char *) * wasi_argc; p += sizeof(char *) * wasi_argc;
} }
wasm_runtime_set_wasi_args_ex(enclave_module->module, wasm_runtime_set_wasi_args_ex(
(const char **)enclave_module->wasi_dir_list, enclave_module->module, (const char **)enclave_module->wasi_dir_list,
dir_list_size, dir_list_size, NULL, 0, (const char **)enclave_module->wasi_env_list,
NULL, 0, env_list_size, enclave_module->wasi_argv, enclave_module->wasi_argc,
(const char **)enclave_module->wasi_env_list, (stdinfd != -1) ? stdinfd : 0, (stdoutfd != -1) ? stdoutfd : 1,
env_list_size, (stderrfd != -1) ? stderrfd : 2);
enclave_module->wasi_argv,
enclave_module->wasi_argc,
(stdinfd != -1) ? stdinfd : 0,
(stdoutfd != -1) ? stdoutfd : 1,
(stderrfd != -1) ? stderrfd : 2);
*args_org = true; *args_org = true;
} }
@ -408,8 +400,7 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
#endif /* end of SGX_DISABLE_WASI */ #endif /* end of SGX_DISABLE_WASI */
void void
ecall_handle_command(unsigned cmd, ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
unsigned char *cmd_buf,
unsigned cmd_buf_size) unsigned cmd_buf_size)
{ {
uint64 *args = (uint64 *)cmd_buf; uint64 *args = (uint64 *)cmd_buf;
@ -494,11 +485,9 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
} }
/* instantiate the module */ /* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, if (!(wasm_module_inst =
16 * 1024, wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
16 * 1024, error_buf, sizeof(error_buf)))) {
error_buf,
sizeof(error_buf)))) {
ocall_print(error_buf); ocall_print(error_buf);
ocall_print("\n"); ocall_print("\n");
goto fail2; goto fail2;

View File

@ -8,7 +8,8 @@
#include "wasm_export.h" #include "wasm_export.h"
#include "bh_platform.h" #include "bh_platform.h"
void ecall_iwasm_test() void
ecall_iwasm_test()
{ {
ocall_print(" Prepare to invoke sgx_open ... ==>\n\n"); ocall_print(" Prepare to invoke sgx_open ... ==>\n\n");
@ -76,7 +77,7 @@ void ecall_iwasm_test()
02 : - O_RDWR */ 02 : - O_RDWR */
/** 1. open **/ /** 1. open **/
fd = open(file, O_RDWR); fd = open(file, O_RDWR);
if (fd !=-1) { if (fd != -1) {
ocall_print("\tOperation open test_open.txt success.\n"); ocall_print("\tOperation open test_open.txt success.\n");
} }
@ -107,7 +108,7 @@ void ecall_iwasm_test()
ocall_print("\tOperation fdatasync success.\n"); ocall_print("\tOperation fdatasync success.\n");
} }
/** isatty **/ /** isatty **/
ret = isatty(fd); ret = isatty(fd);
if (ret == 0) { if (ret == 0) {
ocall_print("\tOperation fisatty success.\n"); ocall_print("\tOperation fisatty success.\n");
@ -140,7 +141,7 @@ void ecall_iwasm_test()
p_dirent = readdir(dirp); p_dirent = readdir(dirp);
if (p_dirent != NULL) { if (p_dirent != NULL) {
ocall_print("\tOperation readdir success.\t"); ocall_print("\tOperation readdir success.\t");
ocall_print(p_dirent -> d_name); ocall_print(p_dirent->d_name);
ocall_print("\n"); ocall_print("\n");
} }
@ -158,7 +159,7 @@ void ecall_iwasm_test()
/** closedir **/ /** closedir **/
ret = closedir(dirp); ret = closedir(dirp);
if (ret == 0 ) { if (ret == 0) {
ocall_print("\tOperation closedir success. \n"); ocall_print("\tOperation closedir success. \n");
} }
/* 2. close */ /* 2. close */
@ -192,7 +193,7 @@ void ecall_iwasm_test()
ret = renameat(AT_FDCWD, rlt_dir_path, AT_FDCWD, rlt_dir_path_new); ret = renameat(AT_FDCWD, rlt_dir_path, AT_FDCWD, rlt_dir_path_new);
if (ret == 0) { if (ret == 0) {
ocall_print("\tOperation renameat ./tmp to " ocall_print("\tOperation renameat ./tmp to "
"./tmp_new success. \n"); "./tmp_new success. \n");
} }
renameat(AT_FDCWD, rlt_dir_path_new, AT_FDCWD, rlt_dir_path); renameat(AT_FDCWD, rlt_dir_path_new, AT_FDCWD, rlt_dir_path);
@ -220,7 +221,7 @@ void ecall_iwasm_test()
ret = symlinkat(file, AT_FDCWD, file_sf_ln); ret = symlinkat(file, AT_FDCWD, file_sf_ln);
if (ret == 0) { if (ret == 0) {
ocall_print("\tOperation symlinkat from test.txt " ocall_print("\tOperation symlinkat from test.txt "
"to text_sf_ln.txt success. \n"); "to text_sf_ln.txt success. \n");
} }
/** readlinkat **/ /** readlinkat **/
total_size = readlinkat(AT_FDCWD, file_sf_ln, buf, sizeof(buf)); total_size = readlinkat(AT_FDCWD, file_sf_ln, buf, sizeof(buf));
@ -336,22 +337,23 @@ void ecall_iwasm_test()
close(fd); close(fd);
/** getopt **/ /** getopt **/
while((ret = getopt(argc, argv, "f:abc")) != -1){ //get option from the getopt() method while ((ret = getopt(argc, argv, "f:abc"))
switch(ret){ != -1) { // get option from the getopt() method
//For option i, r, l, print that these are options switch (ret) {
case 'a': // For option i, r, l, print that these are options
case 'b': case 'a':
case 'c': case 'b':
ocall_print("\tGiven Option operation success. \n"); case 'c':
break; ocall_print("\tGiven Option operation success. \n");
case 'f': //here f is used for some file name break;
ocall_print("\tGiven File operation success.\n"); case 'f': // here f is used for some file name
break; ocall_print("\tGiven File operation success.\n");
case '?': //used for some unknown options break;
ocall_print("\tunknown option trigger success.\n"); case '?': // used for some unknown options
break; ocall_print("\tunknown option trigger success.\n");
} break;
} }
}
/** sched_yield **/ /** sched_yield **/
ret = sched_yield(); ret = sched_yield();

Some files were not shown because too many files have changed in this diff Show More