diff --git a/core/shared/coap/er-coap/coap-constants.h b/core/shared/coap/er-coap/coap-constants.h index d2898f89..1de2ed9d 100644 --- a/core/shared/coap/er-coap/coap-constants.h +++ b/core/shared/coap/er-coap/coap-constants.h @@ -44,6 +44,7 @@ #ifndef COAP_CONSTANTS_H_ #define COAP_CONSTANTS_H_ +/* clang-format off */ #define COAP_DEFAULT_PORT 5683 #define COAP_DEFAULT_SECURE_PORT 5684 @@ -65,128 +66,129 @@ #define COAP_HEADER_OPTION_DELTA_MASK 0xF0 #define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F +/* clang-format on */ /* CoAP message types */ typedef enum { - COAP_TYPE_CON, /* confirmables */ - COAP_TYPE_NON, /* non-confirmables */ - COAP_TYPE_ACK, /* acknowledgements */ - COAP_TYPE_RST /* reset */ + COAP_TYPE_CON, /* confirmables */ + COAP_TYPE_NON, /* non-confirmables */ + COAP_TYPE_ACK, /* acknowledgements */ + COAP_TYPE_RST /* reset */ } coap_message_type_t; +/* clang-format off */ /* CoAP request method codes */ typedef enum { - COAP_GET = 1, - COAP_POST, - COAP_PUT, - COAP_DELETE + COAP_GET = 1, + COAP_POST, COAP_PUT, + COAP_DELETE } coap_method_t; +/* clang-format on */ /* CoAP response codes */ typedef enum { - COAP_NO_ERROR = 0, + COAP_NO_ERROR = 0, - CREATED_2_01 = 65, /* CREATED */ - DELETED_2_02 = 66, /* DELETED */ - VALID_2_03 = 67, /* NOT_MODIFIED */ - CHANGED_2_04 = 68, /* CHANGED */ - CONTENT_2_05 = 69, /* OK */ - CONTINUE_2_31 = 95, /* CONTINUE */ + CREATED_2_01 = 65, /* CREATED */ + DELETED_2_02 = 66, /* DELETED */ + VALID_2_03 = 67, /* NOT_MODIFIED */ + CHANGED_2_04 = 68, /* CHANGED */ + CONTENT_2_05 = 69, /* OK */ + CONTINUE_2_31 = 95, /* CONTINUE */ - BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ - UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ - BAD_OPTION_4_02 = 130, /* BAD_OPTION */ - FORBIDDEN_4_03 = 131, /* FORBIDDEN */ - NOT_FOUND_4_04 = 132, /* NOT_FOUND */ - METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ - NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ - PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ - REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */ - UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ + BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ + UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ + BAD_OPTION_4_02 = 130, /* BAD_OPTION */ + FORBIDDEN_4_03 = 131, /* FORBIDDEN */ + NOT_FOUND_4_04 = 132, /* NOT_FOUND */ + METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ + NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ + PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ + REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */ + UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ - INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ - NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ - BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ - SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ - GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ - PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */ + INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ + NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ + BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ + SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ + GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ + PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */ - /* Erbium errors */ - MEMORY_ALLOCATION_ERROR = 192, - PACKET_SERIALIZATION_ERROR, + /* Erbium errors */ + MEMORY_ALLOCATION_ERROR = 192, + PACKET_SERIALIZATION_ERROR, - /* Erbium hooks */ - MANUAL_RESPONSE, - PING_RESPONSE + /* Erbium hooks */ + MANUAL_RESPONSE, + PING_RESPONSE } coap_status_t; /* CoAP header option numbers */ typedef enum { - COAP_OPTION_IF_MATCH = 1, /* 0-8 B */ - COAP_OPTION_URI_HOST = 3, /* 1-255 B */ - COAP_OPTION_ETAG = 4, /* 1-8 B */ - COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */ - COAP_OPTION_OBSERVE = 6, /* 0-3 B */ - COAP_OPTION_URI_PORT = 7, /* 0-2 B */ - COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */ - COAP_OPTION_URI_PATH = 11, /* 0-255 B */ - COAP_OPTION_CONTENT_FORMAT = 12, /* 0-2 B */ - COAP_OPTION_MAX_AGE = 14, /* 0-4 B */ - COAP_OPTION_URI_QUERY = 15, /* 0-255 B */ - COAP_OPTION_ACCEPT = 17, /* 0-2 B */ - COAP_OPTION_LOCATION_QUERY = 20, /* 0-255 B */ - COAP_OPTION_BLOCK2 = 23, /* 1-3 B */ - COAP_OPTION_BLOCK1 = 27, /* 1-3 B */ - COAP_OPTION_SIZE2 = 28, /* 0-4 B */ - COAP_OPTION_PROXY_URI = 35, /* 1-1034 B */ - COAP_OPTION_PROXY_SCHEME = 39, /* 1-255 B */ - COAP_OPTION_SIZE1 = 60, /* 0-4 B */ + COAP_OPTION_IF_MATCH = 1, /* 0-8 B */ + COAP_OPTION_URI_HOST = 3, /* 1-255 B */ + COAP_OPTION_ETAG = 4, /* 1-8 B */ + COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */ + COAP_OPTION_OBSERVE = 6, /* 0-3 B */ + COAP_OPTION_URI_PORT = 7, /* 0-2 B */ + COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */ + COAP_OPTION_URI_PATH = 11, /* 0-255 B */ + COAP_OPTION_CONTENT_FORMAT = 12, /* 0-2 B */ + COAP_OPTION_MAX_AGE = 14, /* 0-4 B */ + COAP_OPTION_URI_QUERY = 15, /* 0-255 B */ + COAP_OPTION_ACCEPT = 17, /* 0-2 B */ + COAP_OPTION_LOCATION_QUERY = 20, /* 0-255 B */ + COAP_OPTION_BLOCK2 = 23, /* 1-3 B */ + COAP_OPTION_BLOCK1 = 27, /* 1-3 B */ + COAP_OPTION_SIZE2 = 28, /* 0-4 B */ + COAP_OPTION_PROXY_URI = 35, /* 1-1034 B */ + COAP_OPTION_PROXY_SCHEME = 39, /* 1-255 B */ + COAP_OPTION_SIZE1 = 60, /* 0-4 B */ } coap_option_t; /* CoAP Content-Formats */ typedef enum { - TEXT_PLAIN = 0, - TEXT_XML = 1, - TEXT_CSV = 2, - TEXT_HTML = 3, - IMAGE_GIF = 21, - IMAGE_JPEG = 22, - IMAGE_PNG = 23, - IMAGE_TIFF = 24, - AUDIO_RAW = 25, - VIDEO_RAW = 26, - APPLICATION_LINK_FORMAT = 40, - APPLICATION_XML = 41, - APPLICATION_OCTET_STREAM = 42, - APPLICATION_RDF_XML = 43, - APPLICATION_SOAP_XML = 44, - APPLICATION_ATOM_XML = 45, - APPLICATION_XMPP_XML = 46, - APPLICATION_EXI = 47, - APPLICATION_FASTINFOSET = 48, - APPLICATION_SOAP_FASTINFOSET = 49, - APPLICATION_JSON = 50, - APPLICATION_X_OBIX_BINARY = 51 + TEXT_PLAIN = 0, + TEXT_XML = 1, + TEXT_CSV = 2, + TEXT_HTML = 3, + IMAGE_GIF = 21, + IMAGE_JPEG = 22, + IMAGE_PNG = 23, + IMAGE_TIFF = 24, + AUDIO_RAW = 25, + VIDEO_RAW = 26, + APPLICATION_LINK_FORMAT = 40, + APPLICATION_XML = 41, + APPLICATION_OCTET_STREAM = 42, + APPLICATION_RDF_XML = 43, + APPLICATION_SOAP_XML = 44, + APPLICATION_ATOM_XML = 45, + APPLICATION_XMPP_XML = 46, + APPLICATION_EXI = 47, + APPLICATION_FASTINFOSET = 48, + APPLICATION_SOAP_FASTINFOSET = 49, + APPLICATION_JSON = 50, + APPLICATION_X_OBIX_BINARY = 51 } coap_content_format_t; /** * Resource flags for allowed methods and special functionalities. */ typedef enum { - NO_FLAGS = 0, + NO_FLAGS = 0, - /* methods to handle */ - METHOD_GET = (1 << 0), - METHOD_POST = (1 << 1), - METHOD_PUT = (1 << 2), - METHOD_DELETE = (1 << 3), + /* methods to handle */ + METHOD_GET = (1 << 0), + METHOD_POST = (1 << 1), + METHOD_PUT = (1 << 2), + METHOD_DELETE = (1 << 3), - /* special flags */ - HAS_SUB_RESOURCES = (1 << 4), - IS_SEPARATE = (1 << 5), - IS_OBSERVABLE = (1 << 6), - IS_PERIODIC = (1 << 7) + /* special flags */ + HAS_SUB_RESOURCES = (1 << 4), + IS_SEPARATE = (1 << 5), + IS_OBSERVABLE = (1 << 6), + IS_PERIODIC = (1 << 7) } coap_resource_flags_t; #endif /* COAP_CONSTANTS_H_ */ -/** @} */ diff --git a/core/shared/mem-alloc/ems/ems_alloc.c b/core/shared/mem-alloc/ems/ems_alloc.c index 92094d7d..1d432a76 100644 --- a/core/shared/mem-alloc/ems/ems_alloc.c +++ b/core/shared/mem-alloc/ems/ems_alloc.c @@ -5,15 +5,11 @@ #include "ems_gc_internal.h" - static inline bool -hmu_is_in_heap(void *hmu, - gc_uint8 *heap_base_addr, - gc_uint8 *heap_end_addr) +hmu_is_in_heap(void *hmu, gc_uint8 *heap_base_addr, gc_uint8 *heap_end_addr) { gc_uint8 *addr = (gc_uint8 *)hmu; - return (addr >= heap_base_addr && addr < heap_end_addr) - ? true : false; + return (addr >= heap_base_addr && addr < heap_end_addr) ? true : false; } /** @@ -38,8 +34,7 @@ remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p) parent = p->parent; if (!parent || p == root /* p can not be the ROOT node */ || !hmu_is_in_heap(p, base_addr, end_addr) - || (parent != root - && !hmu_is_in_heap(parent, base_addr, end_addr))) { + || (parent != root && !hmu_is_in_heap(parent, base_addr, end_addr))) { goto fail; } @@ -139,8 +134,8 @@ unlink_hmu(gc_heap_t *heap, hmu_t *hmu) gc_size_t size; bh_assert(gci_is_heap_valid(heap)); - bh_assert(hmu && (gc_uint8*) hmu >= heap->base_addr - && (gc_uint8*) hmu < heap->base_addr + heap->current_size); + bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr + && (gc_uint8 *)hmu < heap->base_addr + heap->current_size); if (hmu_get_ut(hmu) != HMU_FC) { heap->is_heap_corrupted = true; @@ -162,7 +157,7 @@ unlink_hmu(gc_heap_t *heap, hmu_t *hmu) return false; } node_next = get_hmu_normal_node_next(node); - if ((hmu_t*)node == hmu) { + if ((hmu_t *)node == hmu) { if (!node_prev) /* list head */ heap->kfc_normal_list[node_idx].next = node_next; else @@ -191,15 +186,15 @@ hmu_set_free_size(hmu_t *hmu) bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC); size = hmu_get_size(hmu); - *((uint32*)((char*) hmu + size) - 1) = size; + *((uint32 *)((char *)hmu + size) - 1) = size; } /** * Add free chunk back to KFC * * @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 - * hmu should be 8-bytes aligned + * @param hmu should not be NULL and it should be a HMU of length @size inside + * @heap hmu should be 8-bytes aligned * @param size should be positive and multiple of 8 * 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; bh_assert(gci_is_heap_valid(heap)); - bh_assert(hmu && (gc_uint8*)hmu >= heap->base_addr - && (gc_uint8*)hmu < heap->base_addr + heap->current_size); + bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr + && (gc_uint8 *)hmu < heap->base_addr + heap->current_size); bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 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)); 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); 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)) { heap->is_heap_corrupted = true; return false; @@ -240,7 +236,7 @@ gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size) } /* big block*/ - node = (hmu_tree_node_t*)hmu; + node = (hmu_tree_node_t *)hmu; node->size = size; 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*/ init_node_idx = (size >> 3); for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT; - node_idx++) { + node_idx++) { normal_head = heap->kfc_normal_list + node_idx; if (normal_head->next) 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 /* with bigger 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)) { return NULL; } @@ -341,7 +337,7 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size) } else { 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)) hmu_mark_pinuse(next); } @@ -349,11 +345,11 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size) heap->total_free_size -= 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* )p, size); - return (hmu_t*)p; + hmu_set_size((hmu_t *)p, size); + return (hmu_t *)p; } } @@ -389,14 +385,14 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size) return NULL; 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)) return NULL; hmu_mark_pinuse(rest); } else { 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)) 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) heap->highmark_size = heap->current_size - heap->total_free_size; - hmu_set_size((hmu_t*)last_tp, size); - return (hmu_t*)last_tp; + hmu_set_size((hmu_t *)last_tp, size); + return (hmu_t *)last_tp; } return NULL; @@ -444,13 +440,12 @@ gc_object_t gc_alloc_vo(void *vheap, gc_size_t size) #else gc_object_t -gc_alloc_vo_internal(void *vheap, gc_size_t size, - const char *file, int line) +gc_alloc_vo_internal(void *vheap, gc_size_t size, const char *file, int line) #endif { - gc_heap_t* heap = (gc_heap_t*) vheap; + gc_heap_t *heap = (gc_heap_t *)vheap; 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; /* hmu header + prefix + obj + suffix */ @@ -489,7 +484,7 @@ gc_alloc_vo_internal(void *vheap, gc_size_t size, ret = hmu_to_obj(hmu); if (tot_size > tot_size_unaligned) /* 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: os_mutex_unlock(&heap->lock); @@ -501,13 +496,13 @@ gc_object_t gc_realloc_vo(void *vheap, void *ptr, gc_size_t size) #else gc_object_t -gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, - const char *file, int line) +gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file, + int line) #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; - 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 obj_size, obj_size_old; 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); 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)) { ut = hmu_get_ut(hmu_next); tot_size_next = hmu_get_size(hmu_next); - if (ut == HMU_FC - && tot_size <= tot_size_old + tot_size_next) { + if (ut == HMU_FC && tot_size <= tot_size_old + tot_size_next) { /* current node and next node meets requirement */ if (!unlink_hmu(heap, hmu_next)) { os_mutex_unlock(&heap->lock); return NULL; } 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 hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line); #endif 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; if (!gci_add_fc(heap, hmu_next, tot_size_next)) { 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); if (!hmu) goto finish; @@ -596,8 +590,8 @@ finish: obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE; memset(ret, 0, obj_size); if (obj_old) { - obj_size_old = tot_size_old - HMU_SIZE - - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE; + obj_size_old = + tot_size_old - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE; 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) return GC_FALSE; - if (heap->heap_id != (gc_handle_t) heap) + if (heap->heap_id != (gc_handle_t)heap) return GC_FALSE; return GC_TRUE; @@ -630,11 +624,10 @@ int gc_free_vo(void *vheap, gc_object_t obj) #else int -gc_free_vo_internal(void *vheap, gc_object_t obj, - const char *file, int line) +gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line) #endif { - gc_heap_t* heap = (gc_heap_t*) vheap; + gc_heap_t *heap = (gc_heap_t *)vheap; gc_uint8 *base_addr, *end_addr; hmu_t *hmu = NULL; hmu_t *prev = NULL; @@ -678,7 +671,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, heap->total_free_size += size; 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) && 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_get_ut(next) == HMU_FC) { size += hmu_get_size(next); @@ -699,7 +692,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, ret = GC_ERROR; 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)) { hmu_unmark_pinuse(next); } - - } else { + } + else { ret = GC_ERROR; goto out; } @@ -750,14 +743,14 @@ gci_dump(gc_heap_t *heap) int i = 0, p, mark; char inuse = 'U'; - cur = (hmu_t*)heap->base_addr; - end = (hmu_t*)((char*)heap->base_addr + heap->current_size); + cur = (hmu_t *)heap->base_addr; + end = (hmu_t *)((char *)heap->base_addr + heap->current_size); - while(cur < end) { + while (cur < end) { ut = hmu_get_ut(cur); size = hmu_get_size(cur); p = hmu_get_pinuse(cur); - mark = hmu_is_jo_marked (cur); + mark = hmu_is_jo_marked(cur); if (ut == HMU_VO) inuse = 'V'; @@ -772,9 +765,9 @@ gci_dump(gc_heap_t *heap) return; } - os_printf("#%d %08x %x %x %d %c %d\n", - i, (int32)((char*) cur - (char*) heap->base_addr), - ut, p, mark, inuse, (int32)hmu_obj_size(size)); + os_printf("#%d %08x %x %x %d %c %d\n", i, + (int32)((char *)cur - (char *)heap->base_addr), ut, p, mark, + inuse, (int32)hmu_obj_size(size)); #if BH_ENABLE_GC_VERIFY != 0 if (inuse == 'V') { gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1); @@ -782,10 +775,9 @@ gci_dump(gc_heap_t *heap) } #endif - cur = (hmu_t*)((char *)cur + size); + cur = (hmu_t *)((char *)cur + size); i++; } bh_assert(cur == end); } - diff --git a/core/shared/mem-alloc/ems/ems_gc.h b/core/shared/mem-alloc/ems/ems_gc.h index a9db04c7..9a74d004 100644 --- a/core/shared/mem-alloc/ems/ems_gc.h +++ b/core/shared/mem-alloc/ems/ems_gc.h @@ -8,8 +8,6 @@ * @date Wed Aug 3 10:46:38 2011 * * @brief This file defines GC modules types and interfaces. - * - * */ #ifndef _EMS_GC_H @@ -33,15 +31,15 @@ extern "C" { #define GC_MAX_HEAP_SIZE (256 * BH_KB) -typedef void * gc_handle_t; -typedef void * gc_object_t; -typedef int64 gc_int64; +typedef void *gc_handle_t; +typedef void *gc_object_t; +typedef int64 gc_int64; typedef uint32 gc_uint32; -typedef int32 gc_int32; +typedef int32 gc_int32; typedef uint16 gc_uint16; -typedef int16 gc_int16; -typedef uint8 gc_uint8; -typedef int8 gc_int8; +typedef int16 gc_int16; +typedef uint8 gc_uint8; +typedef int8 gc_int8; typedef uint32 gc_size_t; typedef enum { @@ -105,8 +103,7 @@ gc_get_heap_struct_size(void); * @return GC_SUCCESS if success, GC_ERROR otherwise */ int -gc_migrate(gc_handle_t handle, - char *pool_buf_new, gc_size_t pool_buf_size); +gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size); /** * 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 */ 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 @@ -142,17 +139,16 @@ gc_free_vo(void *heap, gc_object_t obj); #else /* else of BH_ENABLE_GC_VERIFY */ gc_object_t -gc_alloc_vo_internal(void *heap, gc_size_t size, - const char *file, int line); +gc_alloc_vo_internal(void *heap, gc_size_t size, const char *file, int line); gc_object_t -gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, - const char *file, int line); +gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, const char *file, + int line); int -gc_free_vo_internal(void *heap, gc_object_t obj, - const char *file, int line); +gc_free_vo_internal(void *heap, gc_object_t obj, const char *file, int line); +/* clang-format off */ #define gc_alloc_vo(heap, size) \ 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) \ gc_free_vo_internal(heap, obj, __FILE__, __LINE__) +/* clang-format on */ #endif /* end of BH_ENABLE_GC_VERIFY */ @@ -169,4 +166,3 @@ gc_free_vo_internal(void *heap, gc_object_t obj, #endif #endif - diff --git a/core/shared/mem-alloc/ems/ems_gc_internal.h b/core/shared/mem-alloc/ems/ems_gc_internal.h index b099211c..39b1ff8f 100644 --- a/core/shared/mem-alloc/ems/ems_gc_internal.h +++ b/core/shared/mem-alloc/ems/ems_gc_internal.h @@ -60,8 +60,8 @@ hmu_init_prefix_and_suffix(hmu_t *hmu, gc_size_t tot_size, void hmu_verify(void *vheap, hmu_t *hmu); -#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_PREFIX(p) ((void *)((gc_uint8 *)(p) + OBJ_PREFIX_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) @@ -70,8 +70,8 @@ hmu_verify(void *vheap, hmu_t *hmu); #define OBJ_PREFIX_SIZE 0 #define OBJ_SUFFIX_SIZE 0 -#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_PREFIX(p) ((void *)((gc_uint8 *)(p) + OBJ_PREFIX_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) @@ -81,8 +81,11 @@ hmu_verify(void *vheap, hmu_t *hmu); #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_GET_REAL_SIZE(x) GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + (((x) > 8) ? (x): 8)) +#define GC_SMALLEST_SIZE \ + 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 @@ -92,12 +95,17 @@ hmu_verify(void *vheap, hmu_t *hmu); #define GETBIT(v, offset) ((v) & ((uint32)1 << (offset)) ? 1 : 0) #define CLRBIT(v, offset) (v) &= (~((uint32)1 << (offset))) -#define SETBITS(v, offset, size, value) do { \ - (v) &= ~((((uint32)1 << size) - 1) << offset); \ - (v) |= ((uint32)value << 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 off */ +#define SETBITS(v, offset, size, value) \ + do { \ + (v) &= ~((((uint32)1 << size) - 1) << offset); \ + (v) |= ((uint32)value << 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 @@ -105,30 +113,35 @@ hmu_verify(void *vheap, hmu_t *hmu); #define HMU_SIZE (sizeof(hmu_t)) -#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 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 HMU_UT_SIZE 2 -#define HMU_UT_OFFSET 30 +#define HMU_UT_SIZE 2 +#define HMU_UT_OFFSET 30 -#define hmu_get_ut(hmu) 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 off */ +#define hmu_get_ut(hmu) \ + 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 */ #define HMU_P_OFFSET 29 -#define hmu_mark_pinuse(hmu) SETBIT ((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_mark_pinuse(hmu) SETBIT((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_JO_VT_SIZE 27 +#define HMU_JO_VT_SIZE 27 #define HMU_JO_VT_OFFSET 0 #define HMU_JO_MB_OFFSET 28 -#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_is_jo_marked(hmu) GETBIT ((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_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 @@ -141,11 +154,13 @@ hmu_verify(void *vheap, hmu_t *hmu); #define HMU_VO_FB_OFFSET 28 -#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_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_get_size(hmu) (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)) +#define hmu_get_size(hmu) \ + (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 @@ -173,17 +188,16 @@ static inline hmu_normal_node_t * get_hmu_normal_node_next(hmu_normal_node_t *node) { return node->next_offset - ? (hmu_normal_node_t *)((uint8*)node + node->next_offset) - : NULL; + ? (hmu_normal_node_t *)((uint8 *)node + node->next_offset) + : NULL; } static inline void set_hmu_normal_node_next(hmu_normal_node_t *node, hmu_normal_node_t *next) { if (next) { - bh_assert((uint8*)next - (uint8*)node < INT32_MAX); - node->next_offset = (gc_int32)(intptr_t) - ((uint8*)next - (uint8*)node); + bh_assert((uint8 *)next - (uint8 *)node < INT32_MAX); + node->next_offset = (gc_int32)(intptr_t)((uint8 *)next - (uint8 *)node); } else { node->next_offset = 0; @@ -247,4 +261,4 @@ gci_dump(gc_heap_t *heap); } #endif -#endif +#endif /* end of _EMS_GC_INTERNAL_H */ diff --git a/core/shared/mem-alloc/ems/ems_hmu.c b/core/shared/mem-alloc/ems/ems_hmu.c index d7cf3418..41745e16 100644 --- a/core/shared/mem-alloc/ems/ems_hmu.c +++ b/core/shared/mem-alloc/ems/ems_hmu.c @@ -11,8 +11,8 @@ * Set default value to prefix and suffix * @param hmu should not be NULL and should have been correctly initilized * (except prefix and suffix part) - * @param tot_size is offered here because hmu_get_size can not be used till now. - * tot_size should not be smaller than OBJ_EXTRA_SIZE. + * @param tot_size is offered here because hmu_get_size can not be used + * till now. tot_size should not be smaller than OBJ_EXTRA_SIZE. * For VO, tot_size should be equal to object total size. */ 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); 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->line_no = line_no; 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; } - 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; } } @@ -61,17 +62,17 @@ hmu_verify(void *vheap, hmu_t *hmu) prefix = (gc_object_prefix_t *)(hmu + 1); 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) { /* 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) { is_padding_ok = 0; 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) { is_padding_ok = 0; break; @@ -88,4 +89,3 @@ hmu_verify(void *vheap, hmu_t *hmu) } #endif /* end of BH_ENABLE_GC_VERIFY */ - diff --git a/core/shared/mem-alloc/ems/ems_kfc.c b/core/shared/mem-alloc/ems/ems_kfc.c index c4f7195b..e3633993 100644 --- a/core/shared/mem-alloc/ems/ems_kfc.c +++ b/core/shared/mem-alloc/ems/ems_kfc.c @@ -22,7 +22,7 @@ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size) /* init all data structures*/ 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->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_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); hmu_set_ut(&q->hmu_header, HMU_FC); 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) { 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); - gc_heap_t *heap = (gc_heap_t*)buf_aligned; + gc_heap_t *heap = (gc_heap_t *)buf_aligned; gc_size_t heap_max_size; if (buf_size < APP_HEAP_SIZE_MIN) { - os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", - buf_size, APP_HEAP_SIZE_MIN); + os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", buf_size, + APP_HEAP_SIZE_MIN); 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; #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, 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 *pool_buf_end = pool_buf + pool_buf_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) { - os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", - pool_buf_size, APP_HEAP_SIZE_MIN); + os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", pool_buf_size, + APP_HEAP_SIZE_MIN); 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); os_printf(" heap struct size: %u\n", sizeof(gc_heap_t)); os_printf(" actual heap size: %u\n", heap_max_size); - os_printf(" padding bytes: %u\n", - pool_buf_size - heap_max_size); + os_printf(" padding bytes: %u\n", pool_buf_size - heap_max_size); #endif 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 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 - hmu_t *cur = (hmu_t*)heap->base_addr; - hmu_t *end = (hmu_t*)((char*)heap->base_addr + heap->current_size); + hmu_t *cur = (hmu_t *)heap->base_addr; + hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size); 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"); gci_dump(heap); #if WASM_ENABLE_SPEC_TEST != 0 - while (1); + while (1) { + } #endif } #endif @@ -157,13 +158,12 @@ adjust_ptr(uint8 **p_ptr, intptr_t offset) } int -gc_migrate(gc_handle_t handle, - char *pool_buf_new, gc_size_t pool_buf_size) +gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size) { gc_heap_t *heap = (gc_heap_t *)handle; char *base_addr_new = pool_buf_new + GC_HEAD_PADDING; 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_tree_node_t *tree_node; 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; - if (pool_buf_end < base_addr_new - || heap_max_size < heap->current_size) { + if (pool_buf_end < base_addr_new || heap_max_size < heap->current_size) { os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n"); return GC_ERROR; } @@ -184,13 +183,13 @@ gc_migrate(gc_handle_t handle, if (offset == 0) return 0; - heap->base_addr = (uint8*)base_addr_new; - 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.parent, offset); + heap->base_addr = (uint8 *)base_addr_new; + 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.parent, offset); - cur = (hmu_t*)heap->base_addr; - end = (hmu_t*)((char*)heap->base_addr + heap->current_size); + cur = (hmu_t *)heap->base_addr; + end = (hmu_t *)((char *)heap->base_addr + heap->current_size); while (cur < end) { 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)) { tree_node = (hmu_tree_node_t *)cur; - adjust_ptr((uint8**)&tree_node->left, offset); - adjust_ptr((uint8**)&tree_node->right, offset); + adjust_ptr((uint8 **)&tree_node->left, offset); + adjust_ptr((uint8 **)&tree_node->right, offset); if (tree_node->parent != &heap->kfc_tree_root) /* The root node belongs to heap structure, 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); @@ -229,35 +228,34 @@ gci_verify_heap(gc_heap_t *heap) bh_assert(heap && gci_is_heap_valid(heap)); cur = (hmu_t *)heap->base_addr; end = (hmu_t *)(heap->base_addr + heap->current_size); - while(cur < end) { + while (cur < end) { 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); } #endif void * -gc_heap_stats(void *heap_arg, uint32* stats, int size) +gc_heap_stats(void *heap_arg, uint32 *stats, int size) { 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++) { switch (i) { - case GC_STAT_TOTAL: - stats[i] = heap->current_size; - break; - case GC_STAT_FREE: - stats[i] = heap->total_free_size; - break; - case GC_STAT_HIGHMARK: - stats[i] = heap->highmark_size; - break; - default: - break; + case GC_STAT_TOTAL: + stats[i] = heap->current_size; + break; + case GC_STAT_FREE: + stats[i] = heap->total_free_size; + break; + case GC_STAT_HIGHMARK: + stats[i] = heap->highmark_size; + break; + default: + break; } } return heap; } - diff --git a/core/shared/mem-alloc/mem_alloc.c b/core/shared/mem-alloc/mem_alloc.c index b460e6d1..16652965 100644 --- a/core/shared/mem-alloc/mem_alloc.c +++ b/core/shared/mem-alloc/mem_alloc.c @@ -9,9 +9,10 @@ #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 @@ -20,15 +21,14 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf, void *pool_buf, uint32_t pool_buf_size) { - return gc_init_with_struct_and_pool((char *)struct_buf, - struct_buf_size, - pool_buf, - pool_buf_size); + return gc_init_with_struct_and_pool((char *)struct_buf, struct_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 @@ -40,33 +40,33 @@ mem_allocator_get_heap_struct_size() void * 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 * 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) - gc_free_vo((gc_handle_t) allocator, ptr); + gc_free_vo((gc_handle_t)allocator, ptr); } int -mem_allocator_migrate(mem_allocator_t allocator, - char *pool_buf_new, uint32 pool_buf_size) +mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new, + uint32 pool_buf_size) { - return gc_migrate((gc_handle_t) allocator, - pool_buf_new, pool_buf_size); + return gc_migrate((gc_handle_t)allocator, pool_buf_new, pool_buf_size); } bool 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 */ @@ -76,23 +76,23 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator) typedef struct mem_allocator_tlsf { tlsf_t tlsf; korp_mutex lock; -}mem_allocator_tlsf; +} mem_allocator_tlsf; mem_allocator_t mem_allocator_create(void *mem, uint32_t size) { mem_allocator_tlsf *allocator_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) { printf("Create mem allocator failed: pool size must be " - "at least 1024 bytes.\n"); + "at least 1024 bytes.\n"); return NULL; } - size -= mem_aligned - (char*)mem; - mem = (void*)mem_aligned; + size -= mem_aligned - (char *)mem; + mem = (void *)mem_aligned; tlsf = tlsf_create_with_pool(mem, size); if (!tlsf) { @@ -174,12 +174,10 @@ mem_allocator_free(mem_allocator_t allocator, void *ptr) } int -mem_allocator_migrate(mem_allocator_t allocator, - mem_allocator_t allocator_old) +mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_t allocator_old) { - return tlsf_migrate((mem_allocator_tlsf *) allocator, - (mem_allocator_tlsf *) allocator_old); + return tlsf_migrate((mem_allocator_tlsf *)allocator, + (mem_allocator_tlsf *)allocator_old); } #endif /* end of DEFAULT_MEM_ALLOCATOR */ - diff --git a/core/shared/mem-alloc/mem_alloc.h b/core/shared/mem-alloc/mem_alloc.h index 8224889e..d5dc66f1 100644 --- a/core/shared/mem-alloc/mem_alloc.h +++ b/core/shared/mem-alloc/mem_alloc.h @@ -39,8 +39,8 @@ void mem_allocator_free(mem_allocator_t allocator, void *ptr); int -mem_allocator_migrate(mem_allocator_t allocator, - char *pool_buf_new, uint32 pool_buf_size); +mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new, + uint32 pool_buf_size); bool mem_allocator_is_heap_corrupted(mem_allocator_t allocator); @@ -50,4 +50,3 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator); #endif #endif /* #ifndef __MEM_ALLOC_H */ - diff --git a/core/shared/platform/alios/alios_platform.c b/core/shared/platform/alios/alios_platform.c index 81505482..64185873 100644 --- a/core/shared/platform/alios/alios_platform.c +++ b/core/shared/platform/alios/alios_platform.c @@ -38,8 +38,7 @@ os_realloc(void *ptr, unsigned size) void os_free(void *ptr) -{ -} +{} void * 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 os_dcache_flush() -{ -} +{} diff --git a/core/shared/platform/alios/alios_thread.c b/core/shared/platform/alios/alios_thread.c index f6165bd9..0efd2f39 100644 --- a/core/shared/platform/alios/alios_thread.c +++ b/core/shared/platform/alios/alios_thread.c @@ -6,14 +6,16 @@ #include "platform_api_vmcore.h" #include "platform_api_extension.h" +/* clang-format off */ #define bh_assert(v) do { \ - if (!(v)) { \ - printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ - #v, __FILE__, __LINE__); \ - aos_reboot(); \ - while (1); \ - } \ - } while (0) + if (!(v)) { \ + printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ + #v, __FILE__, __LINE__); \ + aos_reboot(); \ + while (1); \ + } \ +} while (0) +/* clang-format on */ struct os_thread_data; typedef struct os_thread_wait_node { @@ -138,8 +140,8 @@ os_thread_wrapper(void *arg) } int -os_thread_create(korp_tid *p_tid, thread_start_routine_t start, - void *arg, 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, 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)) goto fail2; - snprintf(thread_name, sizeof(thread_name), "%s%d", - "wasm-thread-", ++thread_name_index); + snprintf(thread_name, sizeof(thread_name), "%s%d", "wasm-thread-", + ++thread_name_index); /* Create the thread */ - if (aos_task_new_ext((aos_task_t*)thread_data, thread_name, - os_thread_wrapper, thread_data, - stack_size, prio)) + if (aos_task_new_ext((aos_task_t *)thread_data, thread_name, + os_thread_wrapper, thread_data, stack_size, prio)) goto fail3; aos_msleep(10); @@ -199,7 +200,7 @@ os_self_thread() } int -os_thread_join (korp_tid thread, void **value_ptr) +os_thread_join(korp_tid thread, void **value_ptr) { (void)value_ptr; 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; /* 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); if (!thread_data->thread_wait_list) @@ -272,8 +273,8 @@ os_cond_destroy(korp_cond *cond) } static int -os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, - bool timed, uint32 mills) +os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, + uint32 mills) { os_thread_wait_node *node = &thread_data_current()->wait_node; @@ -352,9 +353,9 @@ os_cond_signal(korp_cond *cond) return BHT_OK; } -uint8 *os_thread_get_stack_boundary() +uint8 * +os_thread_get_stack_boundary() { /* TODO: get alios stack boundary */ return NULL; } - diff --git a/core/shared/platform/alios/alios_time.c b/core/shared/platform/alios/alios_time.c index f7930497..54925273 100644 --- a/core/shared/platform/alios/alios_time.c +++ b/core/shared/platform/alios/alios_time.c @@ -10,4 +10,3 @@ os_time_get_boot_microsecond() { return (uint64)aos_now_ms() * 1000; } - diff --git a/core/shared/platform/alios/platform_internal.h b/core/shared/platform/alios/platform_internal.h index 7201fb0f..07b8f75e 100644 --- a/core/shared/platform/alios/platform_internal.h +++ b/core/shared/platform/alios/platform_internal.h @@ -34,13 +34,14 @@ typedef aos_mutex_t korp_mutex; struct os_thread_wait_node; typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct korp_cond { - aos_mutex_t wait_list_lock; - os_thread_wait_list thread_wait_list; + aos_mutex_t wait_list_lock; + os_thread_wait_list thread_wait_list; } korp_cond; -#define os_printf printf +#define os_printf printf #define os_vprintf vprintf +/* clang-format off */ /* math functions which are not provided by os*/ double sqrt(double x); double floor(double x); @@ -58,6 +59,6 @@ float rintf(float x); float truncf(float x); int signbit(double x); int isnan(double x); +/* clang-format on */ #endif /* end of _BH_PLATFORM_H */ - diff --git a/core/shared/platform/android/platform_init.c b/core/shared/platform/android/platform_init.c index 5353308c..d7ffab33 100644 --- a/core/shared/platform/android/platform_init.c +++ b/core/shared/platform/android/platform_init.c @@ -19,10 +19,10 @@ bh_platform_init() void bh_platform_destroy() -{ -} +{} -int os_printf(const char *fmt, ...) +int +os_printf(const char *fmt, ...) { int ret; va_list ap; @@ -34,14 +34,16 @@ int os_printf(const char *fmt, ...) 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); } #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); return -1; @@ -51,32 +53,37 @@ int futimens(int __dir_fd, const struct timespec __times[2]) #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); 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); return -1; } -int linkat(int __old_dir_fd, const char *__old_path, - int __new_dir_fd, const char *__new_path, int __flags) +int +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); 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); 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); 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 -long telldir(DIR *__dir) +long +telldir(DIR *__dir) { API_NOT_SUPPORT_ERROR(telldir, 23); return -1; } -void seekdir(DIR *__dir, long __location) +void +seekdir(DIR *__dir, long __location) { API_NOT_SUPPORT_ERROR(seekdir, 23); } @@ -101,13 +110,15 @@ void seekdir(DIR *__dir, long __location) #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); 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); return -1; diff --git a/core/shared/platform/android/platform_internal.h b/core/shared/platform/android/platform_internal.h index 703e6bf5..49f6b7f0 100644 --- a/core/shared/platform/android/platform_internal.h +++ b/core/shared/platform/android/platform_internal.h @@ -59,10 +59,8 @@ typedef pthread_t korp_thread; #define os_thread_local_attribute __thread #if WASM_DISABLE_HW_BOUND_CHECK == 0 -#if defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_AARCH64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ + || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) #include @@ -79,15 +77,20 @@ typedef jmp_buf korp_jmpbuf; 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 WASM_DISABLE_HW_BOUND_CHECK */ @@ -95,38 +98,48 @@ typedef long int __syscall_slong_t; #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 #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 __new_dir_fd, const char *__new_path, int __flags); +int +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 #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 #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 @@ -135,4 +148,3 @@ ssize_t pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset #endif #endif /* end of _PLATFORM_INTERNAL_H */ - diff --git a/core/shared/platform/common/freertos/freertos_malloc.c b/core/shared/platform/common/freertos/freertos_malloc.c index 70002163..19cf4d5f 100644 --- a/core/shared/platform/common/freertos/freertos_malloc.c +++ b/core/shared/platform/common/freertos/freertos_malloc.c @@ -5,7 +5,6 @@ #include "platform_api_vmcore.h" - void * os_malloc(unsigned size) { @@ -20,6 +19,4 @@ os_realloc(void *ptr, unsigned size) void os_free(void *ptr) -{ -} - +{} diff --git a/core/shared/platform/common/freertos/freertos_thread.c b/core/shared/platform/common/freertos/freertos_thread.c index 9b9dbea0..9f68bc8f 100644 --- a/core/shared/platform/common/freertos/freertos_thread.c +++ b/core/shared/platform/common/freertos/freertos_thread.c @@ -6,6 +6,7 @@ #include "platform_api_vmcore.h" #include "platform_api_extension.h" +/* clang-format off */ #define bh_assert(v) do { \ if (!(v)) { \ int _count = 1; \ @@ -15,7 +16,8 @@ os_printf("%d\n", _count / (_count - 1)); \ while (1); \ } \ - } while (0) +} while (0) +/* clang-format on */ struct os_thread_data; typedef struct os_thread_wait_node { @@ -56,7 +58,8 @@ static os_thread_data supervisor_thread_data; /* 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); if (!thread_data_list) @@ -79,7 +82,8 @@ static void thread_data_list_add(os_thread_data *thread_data) 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); if (thread_data_list) { @@ -204,15 +208,17 @@ os_thread_wrapper(void *arg) vTaskDelete(NULL); } -int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, - unsigned int stack_size) +int +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, BH_THREAD_DEFAULT_PRIORITY); } -int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, - void *arg, unsigned int stack_size, int prio) +int +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; 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())) goto fail2; - snprintf(thread_name, sizeof(thread_name), "%s%d", - "wasm-thread-", ++thread_name_index); + snprintf(thread_name, sizeof(thread_name), "%s%d", "wasm-thread-", + ++thread_name_index); /* Create the thread */ - if (pdPASS != xTaskCreate(os_thread_wrapper, thread_name, - stack_size / 4, thread_data, - prio, &thread_data->handle)) + if (pdPASS + != xTaskCreate(os_thread_wrapper, thread_name, stack_size / 4, + thread_data, prio, &thread_data->handle)) goto fail3; thread_data_list_add(thread_data); @@ -257,12 +263,14 @@ fail1: return BHT_ERROR; } -korp_tid os_self_thread() +korp_tid +os_self_thread() { 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; TaskHandle_t handle = thread; @@ -293,7 +301,8 @@ int os_thread_join(korp_tid thread, void **value_ptr) return BHT_OK; } -int os_mutex_init(korp_mutex *mutex) +int +os_mutex_init(korp_mutex *mutex) { SemaphoreHandle_t semaphore; @@ -304,7 +313,8 @@ int os_mutex_init(korp_mutex *mutex) return BHT_OK; } -int os_recursive_mutex_init(korp_mutex *mutex) +int +os_recursive_mutex_init(korp_mutex *mutex) { SemaphoreHandle_t semaphore; @@ -315,13 +325,15 @@ int os_recursive_mutex_init(korp_mutex *mutex) return BHT_OK; } -int os_mutex_destroy(korp_mutex *mutex) +int +os_mutex_destroy(korp_mutex *mutex) { vSemaphoreDelete(mutex->sem); return BHT_OK; } -int os_mutex_lock(korp_mutex *mutex) +int +os_mutex_lock(korp_mutex *mutex) { int ret = -1; @@ -332,7 +344,8 @@ int os_mutex_lock(korp_mutex *mutex) return ret == pdPASS ? BHT_OK : BHT_ERROR; } -int os_mutex_unlock(korp_mutex *mutex) +int +os_mutex_unlock(korp_mutex *mutex) { int ret = -1; @@ -343,7 +356,8 @@ int os_mutex_unlock(korp_mutex *mutex) 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())) return BHT_ERROR; @@ -352,15 +366,15 @@ int os_cond_init(korp_cond *cond) return BHT_OK; } -int os_cond_destroy(korp_cond *cond) +int +os_cond_destroy(korp_cond *cond) { vSemaphoreDelete(cond->wait_list_lock); return BHT_OK; } static int -os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, - bool timed, int mills) +os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, int mills) { 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; } -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); } -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) { 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 */ xSemaphoreTake(cond->wait_list_lock, portMAX_DELAY); @@ -435,4 +452,3 @@ int os_cond_signal(korp_cond *cond) return BHT_OK; } - diff --git a/core/shared/platform/common/freertos/freertos_time.c b/core/shared/platform/common/freertos/freertos_time.c index 72b1b584..4497d8c6 100644 --- a/core/shared/platform/common/freertos/freertos_time.c +++ b/core/shared/platform/common/freertos/freertos_time.c @@ -11,4 +11,3 @@ os_time_get_boot_microsecond() TickType_t ticks = xTaskGetTickCount(); return (uint64)1000 * 1000 / configTICK_RATE_HZ * ticks; } - diff --git a/core/shared/platform/common/math/math.c b/core/shared/platform/common/math/math.c index 6b4f25f2..e3b1a7a4 100644 --- a/core/shared/platform/common/math/math.c +++ b/core/shared/platform/common/math/math.c @@ -52,69 +52,63 @@ pdouble2pint(double *pdouble) return u.pint; } -typedef union -{ +typedef union { double value; - struct - { + struct { u_int32_t lsw; u_int32_t msw; } parts; - struct - { + struct { u_int64_t w; } xparts; } ieee_double_shape_type_little; -typedef union -{ +typedef union { double value; - struct - { + struct { u_int32_t msw; u_int32_t lsw; } parts; - struct - { + struct { u_int64_t w; } xparts; } ieee_double_shape_type_big; typedef union { - double d; + double d; struct { - unsigned int manl :32; - unsigned int manh :20; - unsigned int exp :11; - unsigned int sign :1; + unsigned int manl : 32; + unsigned int manh : 20; + unsigned int exp : 11; + unsigned int sign : 1; } bits; } IEEEd2bits_L; typedef union { - double d; + double d; struct { - unsigned int sign :1; - unsigned int exp :11; - unsigned int manh :20; - unsigned int manl :32; + unsigned int sign : 1; + unsigned int exp : 11; + unsigned int manh : 20; + unsigned int manl : 32; } bits; } IEEEd2bits_B; typedef union { - float f; + float f; struct { - unsigned int man :23; - unsigned int exp :8; - unsigned int sign :1; + unsigned int man : 23; + unsigned int exp : 8; + unsigned int sign : 1; } bits; } IEEEf2bits_L; typedef union { - float f; + float f; struct { - unsigned int sign :1; - unsigned int exp :8; - unsigned int man :23; + unsigned int sign : 1; + unsigned int exp : 8; + unsigned int man : 23; } bits; } IEEEf2bits_B; @@ -125,191 +119,192 @@ static union { #define is_little_endian() (__ue.b == 1) -#define __HIL(x) *(1+pdouble2pint(&x)) +#define __HIL(x) *(1 + pdouble2pint(&x)) #define __LOL(x) *(pdouble2pint(&x)) #define __HIB(x) *(pdouble2pint(&x)) -#define __LOB(x) *(1+pdouble2pint(&x)) +#define __LOB(x) *(1 + pdouble2pint(&x)) /* Get two 32 bit ints from a double. */ -#define EXTRACT_WORDS_L(ix0,ix1,d) \ - do { \ - ieee_double_shape_type_little ew_u; \ - ew_u.value = (d); \ - (ix0) = ew_u.parts.msw; \ - (ix1) = ew_u.parts.lsw; \ - } while (0) +#define EXTRACT_WORDS_L(ix0, ix1, d) \ + do { \ + ieee_double_shape_type_little ew_u; \ + ew_u.value = (d); \ + (ix0) = ew_u.parts.msw; \ + (ix1) = ew_u.parts.lsw; \ + } while (0) /* Set a double from two 32 bit ints. */ -#define INSERT_WORDS_L(d,ix0,ix1) \ - do { \ - ieee_double_shape_type_little iw_u; \ - iw_u.parts.msw = (ix0); \ - iw_u.parts.lsw = (ix1); \ - (d) = iw_u.value; \ - } while (0) +#define INSERT_WORDS_L(d, ix0, ix1) \ + do { \ + ieee_double_shape_type_little iw_u; \ + iw_u.parts.msw = (ix0); \ + iw_u.parts.lsw = (ix1); \ + (d) = iw_u.value; \ + } while (0) /* Get two 32 bit ints from a double. */ -#define EXTRACT_WORDS_B(ix0,ix1,d) \ - do { \ - ieee_double_shape_type_big ew_u; \ - ew_u.value = (d); \ - (ix0) = ew_u.parts.msw; \ - (ix1) = ew_u.parts.lsw; \ - } while (0) +#define EXTRACT_WORDS_B(ix0, ix1, d) \ + do { \ + ieee_double_shape_type_big ew_u; \ + ew_u.value = (d); \ + (ix0) = ew_u.parts.msw; \ + (ix1) = ew_u.parts.lsw; \ + } while (0) /* Set a double from two 32 bit ints. */ -#define INSERT_WORDS_B(d,ix0,ix1) \ - do { \ - ieee_double_shape_type_big iw_u; \ - iw_u.parts.msw = (ix0); \ - iw_u.parts.lsw = (ix1); \ - (d) = iw_u.value; \ - } while (0) +#define INSERT_WORDS_B(d, ix0, ix1) \ + do { \ + ieee_double_shape_type_big iw_u; \ + iw_u.parts.msw = (ix0); \ + iw_u.parts.lsw = (ix1); \ + (d) = iw_u.value; \ + } while (0) /* Get the more significant 32 bit int from a double. */ -#define GET_HIGH_WORD_L(i,d) \ - do { \ - ieee_double_shape_type_little gh_u; \ - gh_u.value = (d); \ - (i) = gh_u.parts.msw; \ - } while (0) +#define GET_HIGH_WORD_L(i, d) \ + do { \ + ieee_double_shape_type_little gh_u; \ + gh_u.value = (d); \ + (i) = gh_u.parts.msw; \ + } while (0) /* Get the more significant 32 bit int from a double. */ -#define GET_HIGH_WORD_B(i,d) \ - do { \ - ieee_double_shape_type_big gh_u; \ - gh_u.value = (d); \ - (i) = gh_u.parts.msw; \ - } while (0) +#define GET_HIGH_WORD_B(i, d) \ + do { \ + ieee_double_shape_type_big gh_u; \ + gh_u.value = (d); \ + (i) = gh_u.parts.msw; \ + } while (0) /* Set the more significant 32 bits of a double from an int. */ -#define SET_HIGH_WORD_L(d,v) \ - do { \ - ieee_double_shape_type_little sh_u; \ - sh_u.value = (d); \ - sh_u.parts.msw = (v); \ - (d) = sh_u.value; \ - } while (0) +#define SET_HIGH_WORD_L(d, v) \ + do { \ + ieee_double_shape_type_little sh_u; \ + sh_u.value = (d); \ + sh_u.parts.msw = (v); \ + (d) = sh_u.value; \ + } while (0) /* Set the more significant 32 bits of a double from an int. */ -#define SET_HIGH_WORD_B(d,v) \ - do { \ - ieee_double_shape_type_big sh_u; \ - sh_u.value = (d); \ - sh_u.parts.msw = (v); \ - (d) = sh_u.value; \ - } while (0) +#define SET_HIGH_WORD_B(d, v) \ + do { \ + ieee_double_shape_type_big sh_u; \ + sh_u.value = (d); \ + sh_u.parts.msw = (v); \ + (d) = sh_u.value; \ + } while (0) /* Set the less significant 32 bits of a double from an int. */ -#define SET_LOW_WORD_L(d,v) \ - do { \ - ieee_double_shape_type_little sh_u; \ - sh_u.value = (d); \ - sh_u.parts.lsw = (v); \ - (d) = sh_u.value; \ - } while (0) +#define SET_LOW_WORD_L(d, v) \ + do { \ + ieee_double_shape_type_little sh_u; \ + sh_u.value = (d); \ + sh_u.parts.lsw = (v); \ + (d) = sh_u.value; \ + } while (0) /* Set the more significant 32 bits of a double from an int. */ -#define SET_LOW_WORD_B(d,v) \ - do { \ - ieee_double_shape_type_big sh_u; \ - sh_u.value = (d); \ - sh_u.parts.lsw = (v); \ - (d) = sh_u.value; \ - } while (0) +#define SET_LOW_WORD_B(d, v) \ + do { \ + ieee_double_shape_type_big sh_u; \ + sh_u.value = (d); \ + sh_u.parts.lsw = (v); \ + (d) = sh_u.value; \ + } while (0) /* Get the less significant 32 bit int from a double. */ -#define GET_LOW_WORD_L(i,d) \ - do { \ - ieee_double_shape_type_little gl_u; \ - gl_u.value = (d); \ - (i) = gl_u.parts.lsw; \ - } while (0) +#define GET_LOW_WORD_L(i, d) \ + do { \ + ieee_double_shape_type_little gl_u; \ + gl_u.value = (d); \ + (i) = gl_u.parts.lsw; \ + } while (0) /* Get the less significant 32 bit int from a double. */ -#define GET_LOW_WORD_B(i,d) \ - do { \ - ieee_double_shape_type_big gl_u; \ - gl_u.value = (d); \ - (i) = gl_u.parts.lsw; \ - } while (0) +#define GET_LOW_WORD_B(i, d) \ + do { \ + ieee_double_shape_type_big gl_u; \ + gl_u.value = (d); \ + (i) = gl_u.parts.lsw; \ + } while (0) /* * A union which permits us to convert between a float and a 32 bit * int. */ -typedef union -{ +typedef union { float value; /* FIXME: Assumes 32 bit int. */ unsigned int word; } ieee_float_shape_type; /* Get a 32 bit int from a float. */ -#define GET_FLOAT_WORD(i,d) \ - do { \ - ieee_float_shape_type gf_u; \ - gf_u.value = (d); \ - (i) = gf_u.word; \ +#define GET_FLOAT_WORD(i, d) \ + do { \ + ieee_float_shape_type gf_u; \ + gf_u.value = (d); \ + (i) = gf_u.word; \ } while (0) /* Set a float from a 32 bit int. */ -#define SET_FLOAT_WORD(d,i) \ - do { \ - ieee_float_shape_type sf_u; \ - sf_u.word = (i); \ - (d) = sf_u.value; \ +#define SET_FLOAT_WORD(d, i) \ + do { \ + ieee_float_shape_type sf_u; \ + sf_u.word = (i); \ + (d) = sf_u.value; \ } while (0) /* Macro wrappers. */ -#define EXTRACT_WORDS(ix0,ix1,d) do { \ - if (is_little_endian()) \ - EXTRACT_WORDS_L(ix0,ix1,d); \ - else \ - EXTRACT_WORDS_B(ix0,ix1,d); \ -} while (0) - -#define INSERT_WORDS(d,ix0,ix1) do { \ - if (is_little_endian()) \ - INSERT_WORDS_L(d,ix0,ix1); \ - else \ - INSERT_WORDS_B(d,ix0,ix1); \ -} while (0) - -#define GET_HIGH_WORD(i,d) \ - do { \ - if (is_little_endian()) \ - GET_HIGH_WORD_L(i,d); \ - else \ - GET_HIGH_WORD_B(i,d); \ +#define EXTRACT_WORDS(ix0, ix1, d) \ + do { \ + if (is_little_endian()) \ + EXTRACT_WORDS_L(ix0, ix1, d); \ + else \ + EXTRACT_WORDS_B(ix0, ix1, d); \ } while (0) -#define SET_HIGH_WORD(d,v) \ - do { \ - if (is_little_endian()) \ - SET_HIGH_WORD_L(d,v); \ - else \ - SET_HIGH_WORD_B(d,v); \ +#define INSERT_WORDS(d, ix0, ix1) \ + do { \ + if (is_little_endian()) \ + INSERT_WORDS_L(d, ix0, ix1); \ + else \ + INSERT_WORDS_B(d, ix0, ix1); \ } while (0) -#define GET_LOW_WORD(d,v) \ - do { \ - if (is_little_endian()) \ - GET_LOW_WORD_L(d,v); \ - else \ - GET_LOW_WORD_B(d,v); \ +#define GET_HIGH_WORD(i, d) \ + do { \ + if (is_little_endian()) \ + GET_HIGH_WORD_L(i, d); \ + else \ + GET_HIGH_WORD_B(i, d); \ } while (0) -#define SET_LOW_WORD(d,v) \ - do { \ - if (is_little_endian()) \ - SET_LOW_WORD_L(d,v); \ - else \ - SET_LOW_WORD_B(d,v); \ +#define SET_HIGH_WORD(d, v) \ + do { \ + if (is_little_endian()) \ + SET_HIGH_WORD_L(d, v); \ + else \ + SET_HIGH_WORD_B(d, v); \ + } while (0) + +#define GET_LOW_WORD(d, v) \ + do { \ + if (is_little_endian()) \ + GET_LOW_WORD_L(d, v); \ + else \ + GET_LOW_WORD_B(d, v); \ + } while (0) + +#define SET_LOW_WORD(d, v) \ + do { \ + if (is_little_endian()) \ + SET_LOW_WORD_L(d, v); \ + else \ + SET_LOW_WORD_B(d, v); \ } while (0) #define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x)) @@ -320,18 +315,19 @@ typedef union * Attempt to get strict C99 semantics for assignment with non-C99 compilers. */ #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0 -#define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval)) +#define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval)) #else -#define STRICT_ASSIGN(type, lval, rval) do { \ - volatile type __lval; \ - \ - if (sizeof(type) >= sizeof(long double)) \ - (lval) = (rval); \ - else { \ - __lval = (rval); \ - (lval) = __lval; \ - } \ -} while (0) +#define STRICT_ASSIGN(type, lval, rval) \ + do { \ + volatile type __lval; \ + \ + if (sizeof(type) >= sizeof(long double)) \ + (lval) = (rval); \ + else { \ + __lval = (rval); \ + (lval) = __lval; \ + } \ + } while (0) #endif #ifdef __FDLIBM_STDC__ @@ -345,78 +341,78 @@ static const double #else static double #endif -tiny = 1.0e-300; + tiny = 1.0e-300; #ifdef __STDC__ static const double #else static double #endif -one= 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */ + one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */ #ifdef __STDC__ static const double #else static double #endif -TWO52[2]={ - 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ + TWO52[2] = { + 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ -}; + }; #ifdef __STDC__ static const double #else static double #endif -atanhi[] = { - 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ - 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ - 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ - 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ -}; + atanhi[] = { + 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ + 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ + 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ + 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ + }; #ifdef __STDC__ static const double #else static double #endif -atanlo[] = { - 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ - 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ - 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ - 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */ -}; + atanlo[] = { + 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ + 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ + 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ + 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */ + }; #ifdef __STDC__ static const double #else static double #endif -aT[] = { - 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */ - -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */ - 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */ - -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */ - 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */ - -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */ - 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */ - -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */ - 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */ - -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */ - 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */ -}; + aT[] = { + 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */ + -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */ + 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */ + -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */ + 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */ + -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */ + 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */ + -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */ + 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */ + -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */ + 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */ + }; #ifdef __STDC__ static const double #else static double #endif -zero = 0.0, -pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ -pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ -pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ -pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ + zero = 0.0, + pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ + pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ + pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ + pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ #ifdef __STDC__ static const double @@ -453,346 +449,441 @@ ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */ ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ -static double freebsd_sqrt(double x); -static double freebsd_floor(double x); -static double freebsd_ceil(double x); -static double freebsd_fabs(double x); -static double freebsd_rint(double x); -static int freebsd_isnan(double x); -static double freebsd_atan(double x); -static double freebsd_atan2(double y, double x); +static double +freebsd_sqrt(double x); +static double +freebsd_floor(double x); +static double +freebsd_ceil(double x); +static double +freebsd_fabs(double x); +static double +freebsd_rint(double x); +static int +freebsd_isnan(double x); +static double +freebsd_atan(double x); +static double +freebsd_atan2(double y, double x); - -static double freebsd_atan(double x) +static double +freebsd_atan(double x) { - double w,s1,s2,z; - int32_t ix,hx,id; + double w, s1, s2, z; + int32_t ix, hx, id; - GET_HIGH_WORD(hx,x); - ix = hx&0x7fffffff; - if(ix>=0x44100000) { /* if |x| >= 2^66 */ - u_int32_t low; - GET_LOW_WORD(low,x); - if(ix>0x7ff00000|| - (ix==0x7ff00000&&(low!=0))) - return x+x; /* NaN */ - if(hx>0) return atanhi[3]+*(volatile double *)&atanlo[3]; - else return -atanhi[3]-*(volatile double *)&atanlo[3]; - } if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ - if (ix < 0x3e400000) { /* |x| < 2^-27 */ - if(huge+x>one) return x; /* raise inexact */ - } - id = -1; - } else { - x = freebsd_fabs(x); - if (ix < 0x3ff30000) { /* |x| < 1.1875 */ - if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */ - id = 0; x = (2.0*x-one)/(2.0+x); - } else { /* 11/16<=|x|< 19/16 */ - id = 1; x = (x-one)/(x+one); - } - } else { - if (ix < 0x40038000) { /* |x| < 2.4375 */ - id = 2; x = (x-1.5)/(one+1.5*x); - } else { /* 2.4375 <= |x| < 2^66 */ - id = 3; x = -1.0/x; - } - }} + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x44100000) { /* if |x| >= 2^66 */ + u_int32_t low; + GET_LOW_WORD(low, x); + if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (low != 0))) + return x + x; /* NaN */ + if (hx > 0) + return atanhi[3] + *(volatile double *)&atanlo[3]; + else + return -atanhi[3] - *(volatile double *)&atanlo[3]; + } + if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ + if (ix < 0x3e400000) { /* |x| < 2^-27 */ + if (huge + x > one) + return x; /* raise inexact */ + } + id = -1; + } + else { + x = freebsd_fabs(x); + if (ix < 0x3ff30000) { /* |x| < 1.1875 */ + if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */ + id = 0; + x = (2.0 * x - one) / (2.0 + x); + } + else { /* 11/16<=|x|< 19/16 */ + id = 1; + x = (x - one) / (x + one); + } + } + else { + if (ix < 0x40038000) { /* |x| < 2.4375 */ + id = 2; + x = (x - 1.5) / (one + 1.5 * x); + } + else { /* 2.4375 <= |x| < 2^66 */ + id = 3; + x = -1.0 / x; + } + } + } /* end of argument reduction */ - z = x*x; - w = z*z; + z = x * x; + w = z * z; /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ - s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10]))))); - s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9])))); - if (id<0) return x - x*(s1+s2); - else { - z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x); - return (hx<0)? -z:z; - } + s1 = z + * (aT[0] + + w + * (aT[2] + + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10]))))); + s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9])))); + if (id < 0) + return x - x * (s1 + s2); + else { + z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x); + return (hx < 0) ? -z : z; + } } - -static double freebsd_atan2(double y, double x) +static double +freebsd_atan2(double y, double x) { double z; - int32_t k,m,hx,hy,ix,iy; - u_int32_t lx,ly; + int32_t k, m, hx, hy, ix, iy; + u_int32_t lx, ly; - EXTRACT_WORDS(hx,lx,x); - ix = hx&0x7fffffff; - EXTRACT_WORDS(hy,ly,y); - iy = hy&0x7fffffff; - if(((ix|((lx|-lx)>>31))>0x7ff00000)|| - ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ - return x+y; - if(hx==0x3ff00000&&lx==0) return freebsd_atan(y); /* x=1.0 */ - m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + EXTRACT_WORDS(hy, ly, y); + iy = hy & 0x7fffffff; + if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000) + || ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */ + return x + y; + if (hx == 0x3ff00000 && lx == 0) + return freebsd_atan(y); /* x=1.0 */ + m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */ /* when y = 0 */ - if((iy|ly)==0) { - switch(m) { - case 0: - case 1: return y; /* atan(+-0,+anything)=+-0 */ - case 2: return pi+tiny;/* atan(+0,-anything) = pi */ - case 3: - default: return -pi-tiny;/* atan(-0,-anything) =-pi */ - } - } + if ((iy | ly) == 0) { + switch (m) { + case 0: + case 1: + return y; /* atan(+-0,+anything)=+-0 */ + case 2: + return pi + tiny; /* atan(+0,-anything) = pi */ + case 3: + default: + return -pi - tiny; /* atan(-0,-anything) =-pi */ + } + } /* when x = 0 */ - if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; + if ((ix | lx) == 0) + return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny; /* when x is INF */ - if(ix==0x7ff00000) { - if(iy==0x7ff00000) { - switch(m) { - case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ - case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ - case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ - case 3: - default: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ - } - } else { - switch(m) { - case 0: return zero ; /* atan(+...,+INF) */ - case 1: return -zero ; /* atan(-...,+INF) */ - case 2: return pi+tiny ; /* atan(+...,-INF) */ - case 3: - default: return -pi-tiny ; /* atan(-...,-INF) */ - } - } - } + if (ix == 0x7ff00000) { + if (iy == 0x7ff00000) { + switch (m) { + case 0: + return pi_o_4 + tiny; /* atan(+INF,+INF) */ + case 1: + return -pi_o_4 - tiny; /* atan(-INF,+INF) */ + case 2: + return 3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/ + case 3: + default: + return -3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/ + } + } + else { + switch (m) { + case 0: + return zero; /* atan(+...,+INF) */ + case 1: + return -zero; /* atan(-...,+INF) */ + case 2: + return pi + tiny; /* atan(+...,-INF) */ + case 3: + default: + return -pi - tiny; /* atan(-...,-INF) */ + } + } + } /* when y is INF */ - if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; + if (iy == 0x7ff00000) + return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny; /* compute y/x */ - k = (iy-ix)>>20; - if(k > 60) { /* |y/x| > 2**60 */ - z=pi_o_2+0.5*pi_lo; - m&=1; - } - else if(hx<0&&k<-60) z=0.0; /* 0 > |y|/x > -2**-60 */ - else z=freebsd_atan(fabs(y/x)); /* safe to do y/x */ - switch (m) { - case 0: return z ; /* atan(+,+) */ - case 1: return -z ; /* atan(-,+) */ - case 2: return pi-(z-pi_lo);/* atan(+,-) */ - default: /* case 3 */ - return (z-pi_lo)-pi;/* atan(-,-) */ - } + k = (iy - ix) >> 20; + if (k > 60) { /* |y/x| > 2**60 */ + z = pi_o_2 + 0.5 * pi_lo; + m &= 1; + } + else if (hx < 0 && k < -60) + z = 0.0; /* 0 > |y|/x > -2**-60 */ + else + z = freebsd_atan(fabs(y / x)); /* safe to do y/x */ + switch (m) { + case 0: + return z; /* atan(+,+) */ + case 1: + return -z; /* atan(-,+) */ + case 2: + return pi - (z - pi_lo); /* atan(+,-) */ + default: /* case 3 */ + return (z - pi_lo) - pi; /* atan(-,-) */ + } } -static double freebsd_sqrt(double x) /* wrapper sqrt */ +static double +freebsd_sqrt(double x) /* wrapper sqrt */ { double z; int32_t sign = (int)0x80000000; - int32_t ix0,s0,q,m,t,i; - u_int32_t r,t1,s1,ix1,q1; + int32_t ix0, s0, q, m, t, i; + u_int32_t r, t1, s1, ix1, q1; - EXTRACT_WORDS(ix0,ix1,x); + EXTRACT_WORDS(ix0, ix1, x); /* take care of Inf and NaN */ - if((ix0&0x7ff00000)==0x7ff00000) { - return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf - sqrt(-inf)=sNaN */ + if ((ix0 & 0x7ff00000) == 0x7ff00000) { + return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf + sqrt(-inf)=sNaN */ } /* take care of zero */ - if(ix0<=0) { - if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */ - else if(ix0<0) - return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ + if (ix0 <= 0) { + if (((ix0 & (~sign)) | ix1) == 0) + return x; /* sqrt(+-0) = +-0 */ + else if (ix0 < 0) + return (x - x) / (x - x); /* sqrt(-ve) = sNaN */ } /* normalize x */ - m = (ix0>>20); - if(m==0) { /* subnormal x */ - while(ix0==0) { + m = (ix0 >> 20); + if (m == 0) { /* subnormal x */ + while (ix0 == 0) { m -= 21; - ix0 |= (ix1>>11); ix1 <<= 21; + ix0 |= (ix1 >> 11); + ix1 <<= 21; } - for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1; - m -= i-1; - ix0 |= (ix1>>(32-i)); + for (i = 0; (ix0 & 0x00100000) == 0; i++) + ix0 <<= 1; + m -= i - 1; + ix0 |= (ix1 >> (32 - i)); ix1 <<= i; } - m -= 1023; /* unbias exponent */ - ix0 = (ix0&0x000fffff)|0x00100000; - if(m&1){ /* odd m, double x to make it even */ - ix0 += ix0 + ((ix1&sign)>>31); + m -= 1023; /* unbias exponent */ + ix0 = (ix0 & 0x000fffff) | 0x00100000; + if (m & 1) { /* odd m, double x to make it even */ + ix0 += ix0 + ((ix1 & sign) >> 31); ix1 += ix1; } - m >>= 1; /* m = [m/2] */ + m >>= 1; /* m = [m/2] */ /* generate sqrt(x) bit by bit */ - ix0 += ix0 + ((ix1&sign)>>31); + ix0 += ix0 + ((ix1 & sign) >> 31); ix1 += ix1; - q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ - r = 0x00200000; /* r = moving bit from right to left */ + q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ + r = 0x00200000; /* r = moving bit from right to left */ - while(r!=0) { - t = s0+r; - if(t<=ix0) { - s0 = t+r; + while (r != 0) { + t = s0 + r; + if (t <= ix0) { + s0 = t + r; ix0 -= t; - q += r; + q += r; } - ix0 += ix0 + ((ix1&sign)>>31); + ix0 += ix0 + ((ix1 & sign) >> 31); ix1 += ix1; - r>>=1; + r >>= 1; } r = sign; - while(r!=0) { - t1 = s1+r; - t = s0; - if((t>31); + ix0 += ix0 + ((ix1 & sign) >> 31); ix1 += ix1; - r>>=1; + r >>= 1; } /* use floating add to find out rounding direction */ - if((ix0|ix1)!=0) { - z = one-tiny; /* trigger inexact flag */ - if (z>=one) { - z = one+tiny; - if (q1==(u_int32_t)0xffffffff) { q1=0; q += 1;} - else if (z>one) { - if (q1==(u_int32_t)0xfffffffe) q+=1; - q1+=2; - } else - q1 += (q1&1); + if ((ix0 | ix1) != 0) { + z = one - tiny; /* trigger inexact flag */ + if (z >= one) { + z = one + tiny; + if (q1 == (u_int32_t)0xffffffff) { + q1 = 0; + q += 1; + } + else if (z > one) { + if (q1 == (u_int32_t)0xfffffffe) + q += 1; + q1 += 2; + } + else + q1 += (q1 & 1); } } - ix0 = (q>>1)+0x3fe00000; - ix1 = q1>>1; - if ((q&1)==1) ix1 |= sign; - ix0 += (m <<20); + ix0 = (q >> 1) + 0x3fe00000; + ix1 = q1 >> 1; + if ((q & 1) == 1) + ix1 |= sign; + ix0 += (m << 20); - INSERT_WORDS(z,ix0,ix1); + INSERT_WORDS(z, ix0, ix1); return z; } -static double freebsd_floor(double x) +static double +freebsd_floor(double x) { - int32_t i0,i1,j0; - u_int32_t i,j; + int32_t i0, i1, j0; + u_int32_t i, j; - EXTRACT_WORDS(i0,i1,x); + EXTRACT_WORDS(i0, i1, x); - j0 = ((i0>>20)&0x7ff)-0x3ff; - if(j0<20) { - if(j0<0) { /* raise inexact if x != 0 */ - if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ - if(i0>=0) {i0=i1=0;} - else if(((i0&0x7fffffff)|i1)!=0) - { i0=0xbff00000;i1=0;} - } - } else { - i = (0x000fffff)>>j0; - if(((i0&i)|i1)==0) return x; /* x is integral */ - if(huge+x>0.0) { /* raise inexact flag */ - if(i0<0) i0 += (0x00100000)>>j0; - i0 &= (~i); i1=0; - } - } - } else if (j0>51) { - if(j0==0x400) return x+x; /* inf or NaN */ - else return x; /* x is integral */ - } else { - i = ((u_int32_t)(0xffffffff))>>(j0-20); - if((i1&i)==0) return x; /* x is integral */ - if(huge+x>0.0) { /* raise inexact flag */ - if(i0<0) { - if(j0==20) i0+=1; - else { - j = i1+(1<<(52-j0)); - if(j> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + if (j0 < 0) { /* raise inexact if x != 0 */ + if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */ + if (i0 >= 0) { + i0 = i1 = 0; + } + else if (((i0 & 0x7fffffff) | i1) != 0) { + i0 = 0xbff00000; + i1 = 0; } } - i1 &= (~i); + } + else { + i = (0x000fffff) >> j0; + if (((i0 & i) | i1) == 0) + return x; /* x is integral */ + if (huge + x > 0.0) { /* raise inexact flag */ + if (i0 < 0) + i0 += (0x00100000) >> j0; + i0 &= (~i); + i1 = 0; + } } } - - INSERT_WORDS(x,i0,i1); - - return x; -} - -static double freebsd_ceil(double x) -{ - int32_t i0,i1,j0; - u_int32_t i,j; - EXTRACT_WORDS(i0,i1,x); - j0 = ((i0>>20)&0x7ff)-0x3ff; - if(j0<20) { - if(j0<0) { /* raise inexact if x != 0 */ - if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ - if(i0<0) {i0=0x80000000;i1=0;} - else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;} - } - } else { - i = (0x000fffff)>>j0; - if(((i0&i)|i1)==0) return x; /* x is integral */ - if(huge+x>0.0) { /* raise inexact flag */ - if(i0>0) i0 += (0x00100000)>>j0; - i0 &= (~i); i1=0; - } - } - } else if (j0>51) { - if(j0==0x400) return x+x; /* inf or NaN */ - else return x; /* x is integral */ - } else { - i = ((u_int32_t)(0xffffffff))>>(j0-20); - if((i1&i)==0) return x; /* x is integral */ - if(huge+x>0.0) { /* raise inexact flag */ - if(i0>0) { - if(j0==20) i0+=1; + else if (j0 > 51) { + if (j0 == 0x400) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + else { + i = ((u_int32_t)(0xffffffff)) >> (j0 - 20); + if ((i1 & i) == 0) + return x; /* x is integral */ + if (huge + x > 0.0) { /* raise inexact flag */ + if (i0 < 0) { + if (j0 == 20) + i0 += 1; else { - j = i1 + (1<<(52-j0)); - if(j>31)&1; - j0 = ((i0>>20)&0x7ff)-0x3ff; - if(j0<20) { - if(j0<0) { - if(((i0&0x7fffffff)|i1)==0) return x; - i1 |= (i0&0x0fffff); + int32_t i0, i1, j0; + u_int32_t i, j; + EXTRACT_WORDS(i0, i1, x); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + if (j0 < 0) { /* raise inexact if x != 0 */ + if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */ + if (i0 < 0) { + i0 = 0x80000000; + i1 = 0; + } + else if ((i0 | i1) != 0) { + i0 = 0x3ff00000; + i1 = 0; + } + } + } + else { + i = (0x000fffff) >> j0; + if (((i0 & i) | i1) == 0) + return x; /* x is integral */ + if (huge + x > 0.0) { /* raise inexact flag */ + if (i0 > 0) + i0 += (0x00100000) >> j0; + i0 &= (~i); + i1 = 0; + } + } + } + else if (j0 > 51) { + if (j0 == 0x400) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + else { + i = ((u_int32_t)(0xffffffff)) >> (j0 - 20); + if ((i1 & i) == 0) + return x; /* x is integral */ + if (huge + x > 0.0) { /* raise inexact flag */ + if (i0 > 0) { + if (j0 == 20) + i0 += 1; + else { + j = i1 + (1 << (52 - j0)); + if (j < i1) + i0 += 1; /* got a carry */ + i1 = j; + } + } + i1 &= (~i); + } + } + INSERT_WORDS(x, i0, i1); + return x; +} + +static double +freebsd_rint(double x) +{ + int32_t i0, j0, sx; + u_int32_t i, i1; + double w, t; + EXTRACT_WORDS(i0, i1, x); + sx = (i0 >> 31) & 1; + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + if (j0 < 0) { + if (((i0 & 0x7fffffff) | i1) == 0) + return x; + i1 |= (i0 & 0x0fffff); i0 &= 0xfffe0000; - i0 |= ((i1|-i1)>>12)&0x80000; - SET_HIGH_WORD(x,i0); - STRICT_ASSIGN(double,w,TWO52[sx]+x); - t = w-TWO52[sx]; - GET_HIGH_WORD(i0,t); - SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31)); + i0 |= ((i1 | -i1) >> 12) & 0x80000; + SET_HIGH_WORD(x, i0); + STRICT_ASSIGN(double, w, TWO52[sx] + x); + t = w - TWO52[sx]; + GET_HIGH_WORD(i0, t); + SET_HIGH_WORD(t, (i0 & 0x7fffffff) | (sx << 31)); return t; - } else { - i = (0x000fffff)>>j0; - if(((i0&i)|i1)==0) return x; /* x is integral */ - i>>=1; - if(((i0&i)|i1)!=0) { + } + else { + i = (0x000fffff) >> j0; + if (((i0 & i) | i1) == 0) + return x; /* x is integral */ + i >>= 1; + if (((i0 & i) | i1) != 0) { /* * Some bit is set after the 0.5 bit. To avoid the * possibility of errors from double rounding in @@ -801,26 +892,36 @@ static double freebsd_rint(double x) * adjustment is trickiest for j0==18 and j0==19 * since then it spans the word boundary. */ - if(j0==19) i1 = 0x40000000; else - if(j0==18) i1 = 0x80000000; else - i0 = (i0&(~i))|((0x20000)>>j0); + if (j0 == 19) + i1 = 0x40000000; + else if (j0 == 18) + i1 = 0x80000000; + else + i0 = (i0 & (~i)) | ((0x20000) >> j0); } } - } else if (j0>51) { - if(j0==0x400) return x+x; /* inf or NaN */ - else return x; /* x is integral */ - } else { - i = ((u_int32_t)(0xffffffff))>>(j0-20); - if((i1&i)==0) return x; /* x is integral */ - i>>=1; - if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20)); } - INSERT_WORDS(x,i0,i1); - STRICT_ASSIGN(double,w,TWO52[sx]+x); - return w-TWO52[sx]; + else if (j0 > 51) { + if (j0 == 0x400) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + else { + i = ((u_int32_t)(0xffffffff)) >> (j0 - 20); + if ((i1 & i) == 0) + return x; /* x is integral */ + i >>= 1; + if ((i1 & i) != 0) + i1 = (i1 & (~i)) | ((0x40000000) >> (j0 - 20)); + } + INSERT_WORDS(x, i0, i1); + STRICT_ASSIGN(double, w, TWO52[sx] + x); + return w - TWO52[sx]; } -static int freebsd_isnan(double d) +static int +freebsd_isnan(double d) { if (is_little_endian()) { IEEEd2bits_L u; @@ -834,128 +935,155 @@ static int freebsd_isnan(double d) } } -static double freebsd_fabs(double x) +static double +freebsd_fabs(double x) { u_int32_t high; - GET_HIGH_WORD(high,x); - SET_HIGH_WORD(x,high&0x7fffffff); + GET_HIGH_WORD(high, x); + SET_HIGH_WORD(x, high & 0x7fffffff); return x; } static const float huge_f = 1.0e30F; -static const float -TWO23[2]={ - 8.3886080000e+06, /* 0x4b000000 */ +static const float TWO23[2] = { + 8.3886080000e+06, /* 0x4b000000 */ -8.3886080000e+06, /* 0xcb000000 */ }; static float freebsd_truncf(float x) { - int32_t i0,j0; + int32_t i0, j0; u_int32_t i; - GET_FLOAT_WORD(i0,x); - j0 = ((i0>>23)&0xff)-0x7f; - if(j0<23) { - if(j0<0) { /* raise inexact if x != 0 */ - if(huge_f+x>0.0F) /* |x|<1, so return 0*sign(x) */ + GET_FLOAT_WORD(i0, x); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { /* raise inexact if x != 0 */ + if (huge_f + x > 0.0F) /* |x|<1, so return 0*sign(x) */ i0 &= 0x80000000; - } else { - i = (0x007fffff)>>j0; - if((i0&i)==0) return x; /* x is integral */ - if(huge_f+x>0.0F) /* raise inexact flag */ + } + else { + i = (0x007fffff) >> j0; + if ((i0 & i) == 0) + return x; /* x is integral */ + if (huge_f + x > 0.0F) /* raise inexact flag */ i0 &= (~i); } - } else { - if(j0==0x80) return x+x; /* inf or NaN */ - else return x; /* x is integral */ } - SET_FLOAT_WORD(x,i0); + else { + if (j0 == 0x80) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + SET_FLOAT_WORD(x, i0); return x; } static float freebsd_rintf(float x) { - int32_t i0,j0,sx; - float w,t; - GET_FLOAT_WORD(i0,x); - sx = (i0>>31)&1; - j0 = ((i0>>23)&0xff)-0x7f; - if(j0<23) { - if(j0<0) { - if((i0&0x7fffffff)==0) return x; - STRICT_ASSIGN(float,w,TWO23[sx]+x); - t = w-TWO23[sx]; - GET_FLOAT_WORD(i0,t); - SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31)); + int32_t i0, j0, sx; + float w, t; + GET_FLOAT_WORD(i0, x); + sx = (i0 >> 31) & 1; + j0 = ((i0 >> 23) & 0xff) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { + if ((i0 & 0x7fffffff) == 0) + return x; + STRICT_ASSIGN(float, w, TWO23[sx] + x); + t = w - TWO23[sx]; + GET_FLOAT_WORD(i0, t); + SET_FLOAT_WORD(t, (i0 & 0x7fffffff) | (sx << 31)); return t; } - STRICT_ASSIGN(float,w,TWO23[sx]+x); - return w-TWO23[sx]; + STRICT_ASSIGN(float, w, TWO23[sx] + x); + return w - TWO23[sx]; } - if(j0==0x80) return x+x; /* inf or NaN */ - else return x; /* x is integral */ + if (j0 == 0x80) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ } static float freebsd_ceilf(float x) { - int32_t i0,j0; + int32_t i0, j0; u_int32_t i; - GET_FLOAT_WORD(i0,x); - j0 = ((i0>>23)&0xff)-0x7f; - if(j0<23) { - if(j0<0) { /* raise inexact if x != 0 */ - if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */ - if(i0<0) {i0=0x80000000;} - else if(i0!=0) { i0=0x3f800000;} + GET_FLOAT_WORD(i0, x); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { /* raise inexact if x != 0 */ + if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */ + if (i0 < 0) { + i0 = 0x80000000; + } + else if (i0 != 0) { + i0 = 0x3f800000; + } } - } else { - i = (0x007fffff)>>j0; - if((i0&i)==0) return x; /* x is integral */ - if(huge_f+x>(float)0.0) { /* raise inexact flag */ - if(i0>0) i0 += (0x00800000)>>j0; + } + else { + i = (0x007fffff) >> j0; + if ((i0 & i) == 0) + return x; /* x is integral */ + if (huge_f + x > (float)0.0) { /* raise inexact flag */ + if (i0 > 0) + i0 += (0x00800000) >> j0; i0 &= (~i); } } - } else { - if(j0==0x80) return x+x; /* inf or NaN */ - else return x; /* x is integral */ } - SET_FLOAT_WORD(x,i0); + else { + if (j0 == 0x80) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + SET_FLOAT_WORD(x, i0); return x; } static float freebsd_floorf(float x) { - int32_t i0,j0; + int32_t i0, j0; u_int32_t i; - GET_FLOAT_WORD(i0,x); - j0 = ((i0>>23)&0xff)-0x7f; - if(j0<23) { - if(j0<0) { /* raise inexact if x != 0 */ - if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */ - if(i0>=0) {i0=0;} - else if((i0&0x7fffffff)!=0) - { i0=0xbf800000;} + GET_FLOAT_WORD(i0, x); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { /* raise inexact if x != 0 */ + if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */ + if (i0 >= 0) { + i0 = 0; + } + else if ((i0 & 0x7fffffff) != 0) { + i0 = 0xbf800000; + } } - } else { - i = (0x007fffff)>>j0; - if((i0&i)==0) return x; /* x is integral */ - if(huge_f+x>(float)0.0) { /* raise inexact flag */ - if(i0<0) i0 += (0x00800000)>>j0; + } + else { + i = (0x007fffff) >> j0; + if ((i0 & i) == 0) + return x; /* x is integral */ + if (huge_f + x > (float)0.0) { /* raise inexact flag */ + if (i0 < 0) + i0 += (0x00800000) >> j0; i0 &= (~i); } } - } else { - if(j0==0x80) return x+x; /* inf or NaN */ - else return x; /* x is integral */ } - SET_FLOAT_WORD(x,i0); + else { + if (j0 == 0x80) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + SET_FLOAT_WORD(x, i0); return x; } @@ -1040,314 +1168,371 @@ freebsd_fmaxf(float x, float y) static double freebsd_copysign(double x, double y) { - u_int32_t hx,hy; - GET_HIGH_WORD(hx,x); - GET_HIGH_WORD(hy,y); - SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000)); - return x; + u_int32_t hx, hy; + GET_HIGH_WORD(hx, x); + GET_HIGH_WORD(hy, y); + SET_HIGH_WORD(x, (hx & 0x7fffffff) | (hy & 0x80000000)); + return x; } static double freebsd_scalbn(double x, int n) { - int32_t k,hx,lx; - EXTRACT_WORDS(hx,lx,x); - k = (hx&0x7ff00000)>>20; /* extract exponent */ - if (k==0) { /* 0 or subnormal x */ - if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ + int32_t k, hx, lx; + EXTRACT_WORDS(hx, lx, x); + k = (hx & 0x7ff00000) >> 20; /* extract exponent */ + if (k == 0) { /* 0 or subnormal x */ + if ((lx | (hx & 0x7fffffff)) == 0) + return x; /* +-0 */ x *= two54; - GET_HIGH_WORD(hx,x); - k = ((hx&0x7ff00000)>>20) - 54; - if (n< -50000) return tiny*x; /*underflow*/ + GET_HIGH_WORD(hx, x); + k = ((hx & 0x7ff00000) >> 20) - 54; + if (n < -50000) + return tiny * x; /*underflow*/ + } + if (k == 0x7ff) + return x + x; /* NaN or Inf */ + k = k + n; + if (k > 0x7fe) + return huge * freebsd_copysign(huge, x); /* overflow */ + if (k > 0) /* normal result */ + { + SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20)); + return x; } - if (k==0x7ff) return x+x; /* NaN or Inf */ - k = k+n; - if (k > 0x7fe) return huge*freebsd_copysign(huge,x); /* overflow */ - if (k > 0) /* normal result */ - {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} if (k <= -54) { - if (n > 50000) /* in case integer overflow in n+k */ - return huge*freebsd_copysign(huge,x); /*overflow*/ - else return tiny*freebsd_copysign(tiny,x); /*underflow*/ + if (n > 50000) /* in case integer overflow in n+k */ + return huge * freebsd_copysign(huge, x); /*overflow*/ + else + return tiny * freebsd_copysign(tiny, x); /*underflow*/ } - k += 54; /* subnormal result */ - SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); - return x*twom54; + k += 54; /* subnormal result */ + SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20)); + return x * twom54; } static double freebsd_pow(double x, double y) { - double z,ax,z_h,z_l,p_h,p_l; - double y1,t1,t2,r,s,t,u,v,w; - int32_t i,j,k,yisint,n; - int32_t hx,hy,ix,iy; - u_int32_t lx,ly; + double z, ax, z_h, z_l, p_h, p_l; + double y1, t1, t2, r, s, t, u, v, w; + int32_t i, j, k, yisint, n; + int32_t hx, hy, ix, iy; + u_int32_t lx, ly; - EXTRACT_WORDS(hx,lx,x); - EXTRACT_WORDS(hy,ly,y); - ix = hx&0x7fffffff; iy = hy&0x7fffffff; + EXTRACT_WORDS(hx, lx, x); + EXTRACT_WORDS(hy, ly, y); + ix = hx & 0x7fffffff; + iy = hy & 0x7fffffff; /* y==zero: x**0 = 1 */ - if((iy|ly)==0) return one; + if ((iy | ly) == 0) + return one; /* x==1: 1**y = 1, even if y is NaN */ - if (hx==0x3ff00000 && lx == 0) return one; + if (hx == 0x3ff00000 && lx == 0) + return one; /* y!=zero: result is NaN if either arg is NaN */ - if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || - iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) - return (x+0.0)+(y+0.0); + if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000 + || ((iy == 0x7ff00000) && (ly != 0))) + return (x + 0.0) + (y + 0.0); /* determine if y is an odd int when x < 0 * yisint = 0 ... y is not an integer * yisint = 1 ... y is an odd int * yisint = 2 ... y is an even int */ - yisint = 0; - if(hx<0) { - if(iy>=0x43400000) yisint = 2; /* even integer y */ - else if(iy>=0x3ff00000) { - k = (iy>>20)-0x3ff; /* exponent */ - if(k>20) { - j = ly>>(52-k); - if((j<<(52-k))==ly) yisint = 2-(j&1); - } else if(ly==0) { - j = iy>>(20-k); - if((j<<(20-k))==iy) yisint = 2-(j&1); - } - } - } + yisint = 0; + if (hx < 0) { + if (iy >= 0x43400000) + yisint = 2; /* even integer y */ + else if (iy >= 0x3ff00000) { + k = (iy >> 20) - 0x3ff; /* exponent */ + if (k > 20) { + j = ly >> (52 - k); + if ((j << (52 - k)) == ly) + yisint = 2 - (j & 1); + } + else if (ly == 0) { + j = iy >> (20 - k); + if ((j << (20 - k)) == iy) + yisint = 2 - (j & 1); + } + } + } /* special value of y */ - if(ly==0) { - if (iy==0x7ff00000) { /* y is +-inf */ - if(((ix-0x3ff00000)|lx)==0) - return one; /* (-1)**+-inf is NaN */ - else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */ - return (hy>=0)? y: zero; - else /* (|x|<1)**-,+inf = inf,0 */ - return (hy<0)?-y: zero; - } - if(iy==0x3ff00000) { /* y is +-1 */ - if(hy<0) return one/x; else return x; - } - if(hy==0x40000000) return x*x; /* y is 2 */ - if(hy==0x40080000) return x*x*x; /* y is 3 */ - if(hy==0x40100000) { /* y is 4 */ - u = x*x; - return u*u; - } - if(hy==0x3fe00000) { /* y is 0.5 */ - if(hx>=0) /* x >= +0 */ - return sqrt(x); - } - } + if (ly == 0) { + if (iy == 0x7ff00000) { /* y is +-inf */ + if (((ix - 0x3ff00000) | lx) == 0) + return one; /* (-1)**+-inf is NaN */ + else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */ + return (hy >= 0) ? y : zero; + else /* (|x|<1)**-,+inf = inf,0 */ + return (hy < 0) ? -y : zero; + } + if (iy == 0x3ff00000) { /* y is +-1 */ + if (hy < 0) + return one / x; + else + return x; + } + if (hy == 0x40000000) + return x * x; /* y is 2 */ + if (hy == 0x40080000) + return x * x * x; /* y is 3 */ + if (hy == 0x40100000) { /* y is 4 */ + u = x * x; + return u * u; + } + if (hy == 0x3fe00000) { /* y is 0.5 */ + if (hx >= 0) /* x >= +0 */ + return sqrt(x); + } + } - ax = fabs(x); + ax = fabs(x); /* special value of x */ - if(lx==0) { - if(ix==0x7ff00000||ix==0||ix==0x3ff00000){ - z = ax; /*x is +-0,+-inf,+-1*/ - if(hy<0) z = one/z; /* z = (1/|x|) */ - if(hx<0) { - if(((ix-0x3ff00000)|yisint)==0) { - z = (z-z)/(z-z); /* (-1)**non-int is NaN */ - } else if(yisint==1) - z = -z; /* (x<0)**odd = -(|x|**odd) */ - } - return z; - } - } + if (lx == 0) { + if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) { + z = ax; /*x is +-0,+-inf,+-1*/ + if (hy < 0) + z = one / z; /* z = (1/|x|) */ + if (hx < 0) { + if (((ix - 0x3ff00000) | yisint) == 0) { + z = (z - z) / (z - z); /* (-1)**non-int is NaN */ + } + else if (yisint == 1) + z = -z; /* (x<0)**odd = -(|x|**odd) */ + } + return z; + } + } /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be - n = (hx>>31)+1; + n = (hx>>31)+1; but ANSI C says a right shift of a signed negative quantity is implementation defined. */ - n = ((u_int32_t)hx>>31)-1; + n = ((u_int32_t)hx >> 31) - 1; /* (x<0)**(non-int) is NaN */ - if((n|yisint)==0) return (x-x)/(x-x); + if ((n | yisint) == 0) + return (x - x) / (x - x); - s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ - if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */ + s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ + if ((n | (yisint - 1)) == 0) + s = -one; /* (-ve)**(odd int) */ /* |y| is huge */ - if(iy>0x41e00000) { /* if |y| > 2**31 */ - if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */ - if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny; - if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny; - } - /* over/underflow if x is not close to one */ - if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny; - if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny; - /* now |1-x| is tiny <= 2**-20, suffice to compute - log(x) by x-x^2/2+x^3/3-x^4/4 */ - t = ax-one; /* t has 20 trailing zeros */ - w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25)); - u = ivln2_h*t; /* ivln2_h has 21 sig. bits */ - v = t*ivln2_l-w*ivln2; - t1 = u+v; - SET_LOW_WORD(t1,0); - t2 = v-(t1-u); - } else { - double ss,s2,s_h,s_l,t_h,t_l; - n = 0; - /* take care subnormal number */ - if(ix<0x00100000) - {ax *= two53; n -= 53; GET_HIGH_WORD(ix,ax); } - n += ((ix)>>20)-0x3ff; - j = ix&0x000fffff; - /* determine interval */ - ix = j|0x3ff00000; /* normalize ix */ - if(j<=0x3988E) k=0; /* |x| 0x41e00000) { /* if |y| > 2**31 */ + if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */ + if (ix <= 0x3fefffff) + return (hy < 0) ? huge * huge : tiny * tiny; + if (ix >= 0x3ff00000) + return (hy > 0) ? huge * huge : tiny * tiny; + } + /* over/underflow if x is not close to one */ + if (ix < 0x3fefffff) + return (hy < 0) ? s * huge * huge : s * tiny * tiny; + if (ix > 0x3ff00000) + return (hy > 0) ? s * huge * huge : s * tiny * tiny; + /* now |1-x| is tiny <= 2**-20, suffice to compute + log(x) by x-x^2/2+x^3/3-x^4/4 */ + t = ax - one; /* t has 20 trailing zeros */ + w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); + u = ivln2_h * t; /* ivln2_h has 21 sig. bits */ + v = t * ivln2_l - w * ivln2; + t1 = u + v; + SET_LOW_WORD(t1, 0); + t2 = v - (t1 - u); + } + else { + double ss, s2, s_h, s_l, t_h, t_l; + n = 0; + /* take care subnormal number */ + if (ix < 0x00100000) { + ax *= two53; + n -= 53; + GET_HIGH_WORD(ix, ax); + } + n += ((ix) >> 20) - 0x3ff; + j = ix & 0x000fffff; + /* determine interval */ + ix = j | 0x3ff00000; /* normalize ix */ + if (j <= 0x3988E) + k = 0; /* |x|>1)|0x20000000)+0x00080000+(k<<18)); - t_l = ax - (t_h-bp[k]); - s_l = v*((u-s_h*t_h)-s_h*t_l); - /* compute log(ax) */ - s2 = ss*ss; - r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); - r += s_l*(s_h+ss); - s2 = s_h*s_h; - t_h = 3.0+s2+r; - SET_LOW_WORD(t_h,0); - t_l = r-((t_h-3.0)-s2); - /* u+v = ss*(1+...) */ - u = s_h*t_h; - v = s_l*t_h+t_l*ss; - /* 2/(3log2)*(ss+...) */ - p_h = u+v; - SET_LOW_WORD(p_h,0); - p_l = v-(p_h-u); - z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ - z_l = cp_l*p_h+p_l*cp+dp_l[k]; - /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ - t = (double)n; - t1 = (((z_h+z_l)+dp_h[k])+t); - SET_LOW_WORD(t1,0); - t2 = z_l-(((t1-t)-dp_h[k])-z_h); - } + /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ + u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */ + v = one / (ax + bp[k]); + ss = u * v; + s_h = ss; + SET_LOW_WORD(s_h, 0); + /* t_h=ax+bp[k] High */ + t_h = zero; + SET_HIGH_WORD(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18)); + t_l = ax - (t_h - bp[k]); + s_l = v * ((u - s_h * t_h) - s_h * t_l); + /* compute log(ax) */ + s2 = ss * ss; + r = s2 * s2 + * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); + r += s_l * (s_h + ss); + s2 = s_h * s_h; + t_h = 3.0 + s2 + r; + SET_LOW_WORD(t_h, 0); + t_l = r - ((t_h - 3.0) - s2); + /* u+v = ss*(1+...) */ + u = s_h * t_h; + v = s_l * t_h + t_l * ss; + /* 2/(3log2)*(ss+...) */ + p_h = u + v; + SET_LOW_WORD(p_h, 0); + p_l = v - (p_h - u); + z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */ + z_l = cp_l * p_h + p_l * cp + dp_l[k]; + /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ + t = (double)n; + t1 = (((z_h + z_l) + dp_h[k]) + t); + SET_LOW_WORD(t1, 0); + t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); + } /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ - y1 = y; - SET_LOW_WORD(y1,0); - p_l = (y-y1)*t1+y*t2; - p_h = y1*t1; - z = p_l+p_h; - EXTRACT_WORDS(j,i,z); - if (j>=0x40900000) { /* z >= 1024 */ - if(((j-0x40900000)|i)!=0) /* if z > 1024 */ - return s*huge*huge; /* overflow */ - else { - if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */ - } - } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */ - if(((j-0xc090cc00)|i)!=0) /* z < -1075 */ - return s*tiny*tiny; /* underflow */ - else { - if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */ - } - } + y1 = y; + SET_LOW_WORD(y1, 0); + p_l = (y - y1) * t1 + y * t2; + p_h = y1 * t1; + z = p_l + p_h; + EXTRACT_WORDS(j, i, z); + if (j >= 0x40900000) { /* z >= 1024 */ + if (((j - 0x40900000) | i) != 0) /* if z > 1024 */ + return s * huge * huge; /* overflow */ + else { + if (p_l + ovt > z - p_h) + return s * huge * huge; /* overflow */ + } + } + else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */ + if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */ + return s * tiny * tiny; /* underflow */ + else { + if (p_l <= z - p_h) + return s * tiny * tiny; /* underflow */ + } + } /* * compute 2**(p_h+p_l) */ - i = j&0x7fffffff; - k = (i>>20)-0x3ff; - n = 0; - if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ - n = j+(0x00100000>>(k+1)); - k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */ - t = zero; - SET_HIGH_WORD(t,n&~(0x000fffff>>k)); - n = ((n&0x000fffff)|0x00100000)>>(20-k); - if(j<0) n = -n; - p_h -= t; - } - t = p_l+p_h; - SET_LOW_WORD(t,0); - u = t*lg2_h; - v = (p_l-(t-p_h))*lg2+t*lg2_l; - z = u+v; - w = v-(z-u); - t = z*z; - t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); - r = (z*t1)/(t1-two)-(w+z*w); - z = one-(r-z); - GET_HIGH_WORD(j,z); - j += (n<<20); - if((j>>20)<=0) z = freebsd_scalbn(z,n); /* subnormal output */ - else SET_HIGH_WORD(z,j); - return s*z; + i = j & 0x7fffffff; + k = (i >> 20) - 0x3ff; + n = 0; + if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ + n = j + (0x00100000 >> (k + 1)); + k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */ + t = zero; + SET_HIGH_WORD(t, n & ~(0x000fffff >> k)); + n = ((n & 0x000fffff) | 0x00100000) >> (20 - k); + if (j < 0) + n = -n; + p_h -= t; + } + t = p_l + p_h; + SET_LOW_WORD(t, 0); + u = t * lg2_h; + v = (p_l - (t - p_h)) * lg2 + t * lg2_l; + z = u + v; + w = v - (z - u); + t = z * z; + t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); + r = (z * t1) / (t1 - two) - (w + z * w); + z = one - (r - z); + GET_HIGH_WORD(j, z); + j += (n << 20); + if ((j >> 20) <= 0) + z = freebsd_scalbn(z, n); /* subnormal output */ + else + SET_HIGH_WORD(z, j); + return s * z; } -double atan(double x) +double +atan(double x) { return freebsd_atan(x); } -double atan2(double y, double x) +double +atan2(double y, double x) { return freebsd_atan2(y, x); } -double sqrt(double x) +double +sqrt(double x) { return freebsd_sqrt(x); } -double floor(double x) +double +floor(double x) { return freebsd_floor(x); } -double ceil(double x) +double +ceil(double x) { return freebsd_ceil(x); } -double fmin(double x, double y) +double +fmin(double x, double y) { return x < y ? x : y; } -double fmax(double x, double y) +double +fmax(double x, double y) { return x > y ? x : y; } -double rint(double x) +double +rint(double x) { return freebsd_rint(x); } -double fabs(double x) +double +fabs(double x) { return freebsd_fabs(x); } -int isnan(double x) +int +isnan(double x) { return freebsd_isnan(x); } -double trunc(double x) +double +trunc(double x) { return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x); } -int signbit(double x) +int +signbit(double x) { return ((__HI(x) & 0x80000000) >> 31); } diff --git a/core/shared/platform/common/posix/posix_malloc.c b/core/shared/platform/common/posix/posix_malloc.c index e83fc7d7..660d1baa 100644 --- a/core/shared/platform/common/posix/posix_malloc.c +++ b/core/shared/platform/common/posix/posix_malloc.c @@ -22,6 +22,3 @@ os_free(void *ptr) { free(ptr); } - - - diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index 15a4de8b..72b51824 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -50,21 +50,19 @@ os_mmap(void *hint, size_t size, int prot, int flags) * (mmap's first argument) to meet the requirement. */ if (!hint && !(flags & MMAP_MAP_FIXED) && (flags & MMAP_MAP_32BIT)) { - uint8 *stack_addr = (uint8*)&map_prot; - uint8 *text_addr = (uint8*)os_mmap; + uint8 *stack_addr = (uint8 *)&map_prot; + uint8 *text_addr = (uint8 *)os_mmap; /* hint address begins with 1MB */ static uint8 *hint_addr = (uint8 *)(uintptr_t)BH_MB; - if ((hint_addr - text_addr >= 0 - && hint_addr - text_addr < 100 * BH_MB) + if ((hint_addr - text_addr >= 0 && hint_addr - text_addr < 100 * BH_MB) || (text_addr - hint_addr >= 0 && text_addr - hint_addr < 100 * BH_MB)) { /* hint address is possibly in text section, skip it */ hint_addr += 100 * BH_MB; } - if ((hint_addr - stack_addr >= 0 - && hint_addr - stack_addr < 8 * BH_MB) + if ((hint_addr - stack_addr >= 0 && hint_addr - stack_addr < 8 * BH_MB) || (stack_addr - hint_addr >= 0 && stack_addr - hint_addr < 8 * BH_MB)) { /* 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 */ - for (i = 0; - i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB); + for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB); i++) { addr = mmap(hint_addr, request_size, map_prot, map_flags, -1, 0); if (addr != MAP_FAILED) { @@ -114,7 +111,7 @@ os_munmap(void *addr, size_t size) if (addr) { 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); } } @@ -144,5 +141,4 @@ os_mprotect(void *addr, size_t size, int prot) void os_dcache_flush(void) -{ -} +{} diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index 3da44a69..1e7e4f37 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -22,7 +22,8 @@ typedef struct { static os_thread_local_attribute os_signal_handler signal_handler; #endif -static void *os_thread_wrapper(void *arg) +static void * +os_thread_wrapper(void *arg) { thread_wrapper_arg *targ = arg; thread_start_routine_t start_func = targ->start; @@ -44,8 +45,9 @@ static void *os_thread_wrapper(void *arg) return NULL; } -int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start, - void *arg, unsigned int stack_size, int prio) +int +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; 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; } - targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ)); + targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ)); if (!targ) { pthread_attr_destroy(&tattr); return BHT_ERROR; @@ -85,24 +87,28 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start, return BHT_OK; } -int os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg, - unsigned int stack_size) +int +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, 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; } -int os_recursive_mutex_init(korp_mutex *mutex) +int +os_recursive_mutex_init(korp_mutex *mutex) { int ret; @@ -120,7 +126,8 @@ int os_recursive_mutex_init(korp_mutex *mutex) return ret == 0 ? BHT_OK : BHT_ERROR; } -int os_mutex_destroy(korp_mutex *mutex) +int +os_mutex_destroy(korp_mutex *mutex) { int ret; @@ -130,7 +137,8 @@ int os_mutex_destroy(korp_mutex *mutex) return ret == 0 ? BHT_OK : BHT_ERROR; } -int os_mutex_lock(korp_mutex *mutex) +int +os_mutex_lock(korp_mutex *mutex) { int ret; @@ -140,7 +148,8 @@ int os_mutex_lock(korp_mutex *mutex) return ret == 0 ? BHT_OK : BHT_ERROR; } -int os_mutex_unlock(korp_mutex *mutex) +int +os_mutex_unlock(korp_mutex *mutex) { int ret; @@ -150,7 +159,8 @@ int os_mutex_unlock(korp_mutex *mutex) return ret == 0 ? BHT_OK : BHT_ERROR; } -int os_cond_init(korp_cond *cond) +int +os_cond_init(korp_cond *cond) { assert(cond); @@ -160,7 +170,8 @@ int os_cond_init(korp_cond *cond) return BHT_OK; } -int os_cond_destroy(korp_cond *cond) +int +os_cond_destroy(korp_cond *cond) { assert(cond); @@ -170,7 +181,8 @@ int os_cond_destroy(korp_cond *cond) 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(mutex); @@ -181,7 +193,8 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex) 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; 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); - if (tv.tv_usec * 1000 >= tv.tv_usec - && tv_nsec_new >= tv.tv_usec * 1000) { + if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) { ts->tv_nsec = tv_nsec_new; } 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; struct timespec abstime; @@ -236,7 +249,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) return ret; } -int os_cond_signal(korp_cond *cond) +int +os_cond_signal(korp_cond *cond) { assert(cond); @@ -246,17 +260,20 @@ int os_cond_signal(korp_cond *cond) 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); } -int os_thread_detach(korp_tid thread) +int +os_thread_detach(korp_tid thread) { return pthread_detach(thread); } -void os_thread_exit(void *retval) +void +os_thread_exit(void *retval) { #ifdef OS_ENABLE_HW_BOUND_CHECK os_thread_signal_destroy(); @@ -268,7 +285,8 @@ void os_thread_exit(void *retval) static os_thread_local_attribute uint8 *thread_stack_boundary = NULL; #endif -uint8 *os_thread_get_stack_boundary() +uint8 * +os_thread_get_stack_boundary() { pthread_t self; #ifdef __linux__ @@ -286,15 +304,15 @@ uint8 *os_thread_get_stack_boundary() page_size = getpagesize(); self = pthread_self(); - max_stack_size = (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) - & ~(page_size - 1); + max_stack_size = + (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) & ~(page_size - 1); if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT) max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT; #ifdef __linux__ 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_destroy(&attr); if (stack_size > max_stack_size) @@ -306,7 +324,7 @@ uint8 *os_thread_get_stack_boundary() } (void)stack_size; #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); if (stack_size > max_stack_size) addr -= max_stack_size; @@ -349,8 +367,7 @@ touch_pages(uint8 *stack_min_addr, uint32 page_size) { uint8 sum = 0; while (1) { - volatile uint8 *touch_addr = - (volatile uint8*)os_alloca(page_size / 2); + volatile uint8 *touch_addr = (volatile uint8 *)os_alloca(page_size / 2); if (touch_addr < stack_min_addr + page_size) { sum += *(stack_min_addr + page_size - 1); break; @@ -378,7 +395,8 @@ init_stack_guard_pages() (void)touch_pages(stack_min_addr, page_size); /* First time to call aot function, protect guard pages */ if (os_mprotect(stack_min_addr, page_size * guard_page_count, - MMAP_PROT_NONE) != 0) { + MMAP_PROT_NONE) + != 0) { return false; } return true; @@ -413,8 +431,7 @@ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext) mask_signals(SIG_BLOCK); - if (signal_handler - && (sig_num == SIGSEGV || sig_num == SIGBUS)) { + if (signal_handler && (sig_num == SIGSEGV || sig_num == SIGBUS)) { 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); break; default: - os_printf("unhandle signal %d, si_addr: %p\n", - sig_num, sig_addr); + os_printf("unhandle signal %d, si_addr: %p\n", sig_num, sig_addr); break; } @@ -452,8 +468,7 @@ os_thread_signal_init(os_signal_handler handler) } /* Initialize memory for signal alternate stack of current thread */ - if (!(map_addr = os_mmap(NULL, map_size, - MMAP_PROT_READ | MMAP_PROT_WRITE, + if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE, MMAP_MAP_NONE))) { os_printf("Failed to mmap memory for alternate stack\n"); goto fail1; @@ -533,7 +548,7 @@ void os_sigreturn() { #if defined(__APPLE__) - #define UC_RESET_ALT_STACK 0x80000000 +#define UC_RESET_ALT_STACK 0x80000000 extern int __sigreturn(void *, int); /* It's necessary to call __sigreturn to restore the sigaltstack state @@ -542,4 +557,3 @@ os_sigreturn() #endif } #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ - diff --git a/core/shared/platform/common/posix/posix_time.c b/core/shared/platform/common/posix/posix_time.c index 744dd4cb..bcf5ca3c 100644 --- a/core/shared/platform/common/posix/posix_time.c +++ b/core/shared/platform/common/posix/posix_time.c @@ -13,6 +13,5 @@ os_time_get_boot_microsecond() 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; } - diff --git a/core/shared/platform/darwin/platform_init.c b/core/shared/platform/darwin/platform_init.c index 17aeb8ba..2aae13fa 100644 --- a/core/shared/platform/darwin/platform_init.c +++ b/core/shared/platform/darwin/platform_init.c @@ -13,8 +13,7 @@ bh_platform_init() void bh_platform_destroy() -{ -} +{} int os_printf(const char *format, ...) @@ -42,4 +41,3 @@ os_vprintf(const char *format, va_list ap) return BH_VPRINTF(format, ap); #endif } - diff --git a/core/shared/platform/darwin/platform_internal.h b/core/shared/platform/darwin/platform_internal.h index 3fa85a30..77fdf0fd 100644 --- a/core/shared/platform/darwin/platform_internal.h +++ b/core/shared/platform/darwin/platform_internal.h @@ -60,10 +60,8 @@ typedef pthread_t korp_thread; #define os_thread_local_attribute __thread #if WASM_DISABLE_HW_BOUND_CHECK == 0 -#if defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_AARCH64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ + || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) #include @@ -80,15 +78,20 @@ typedef jmp_buf korp_jmpbuf; 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 WASM_DISABLE_HW_BOUND_CHECK */ @@ -97,4 +100,3 @@ void os_sigreturn(); #endif #endif /* end of _PLATFORM_INTERNAL_H */ - diff --git a/core/shared/platform/esp-idf/espidf_platform.c b/core/shared/platform/esp-idf/espidf_platform.c index 7660ada4..1fd99b91 100644 --- a/core/shared/platform/esp-idf/espidf_platform.c +++ b/core/shared/platform/esp-idf/espidf_platform.c @@ -6,7 +6,6 @@ #include "platform_api_vmcore.h" #include "platform_api_extension.h" - int errno = 0; int @@ -27,7 +26,8 @@ bh_platform_destroy() os_thread_sys_destroy(); } -int os_printf(const char *format, ...) +int +os_printf(const char *format, ...) { int ret = 0; va_list ap; @@ -66,8 +66,7 @@ os_mprotect(void *addr, size_t size, int prot) void os_dcache_flush() -{ -} +{} int atoi(const char *nptr) @@ -112,11 +111,10 @@ memmove(void *dest, const void *src, size_t n) *d++ = *s++; } else { - const char *lasts = s + (n-1); - char *lastd = d + (n-1); + const char *lasts = s + (n - 1); + char *lastd = d + (n - 1); while (n--) *lastd-- = *lasts--; } return dest; } - diff --git a/core/shared/platform/esp-idf/espidf_thread.c b/core/shared/platform/esp-idf/espidf_thread.c index d5228dc2..f4bcbc71 100644 --- a/core/shared/platform/esp-idf/espidf_thread.c +++ b/core/shared/platform/esp-idf/espidf_thread.c @@ -6,9 +6,9 @@ #include "platform_api_vmcore.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 */ return NULL; } - diff --git a/core/shared/platform/esp-idf/platform_internal.h b/core/shared/platform/esp-idf/platform_internal.h index 5703fa78..09f269fc 100644 --- a/core/shared/platform/esp-idf/platform_internal.h +++ b/core/shared/platform/esp-idf/platform_internal.h @@ -45,9 +45,12 @@ typedef struct korp_cond { os_thread_wait_list thread_wait_list; } korp_cond; -int os_printf(const char *format, ...); -int os_vprintf(const char *format, va_list ap); +int +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 */ double sqrt(double x); double floor(double x); @@ -93,5 +96,6 @@ uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); +/* clang-format on */ #endif diff --git a/core/shared/platform/include/platform_api_extension.h b/core/shared/platform/include/platform_api_extension.h index 7b92824c..11eee8b1 100644 --- a/core/shared/platform/include/platform_api_extension.h +++ b/core/shared/platform/include/platform_api_extension.h @@ -30,7 +30,6 @@ extern "C" { * 2. To build the app-mgr and app-framework, you must implement it */ - /** * Ceates a thread * @@ -41,8 +40,9 @@ extern "C" { * * @return 0 if success. */ -int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, - unsigned int stack_size); +int +os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, + unsigned int stack_size); /** * 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. */ -int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, - void *arg, unsigned int stack_size, int prio); +int +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 @@ -66,7 +67,8 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, * * @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 @@ -82,7 +84,8 @@ int os_thread_detach(korp_tid); * * @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 @@ -90,12 +93,14 @@ void os_thread_exit(void *retval); * * @return 0 if success, -1 otherwise */ -int os_thread_env_init(); +int +os_thread_env_init(); /** * Destroy current thread environment */ -void os_thread_env_destroy(); +void +os_thread_env_destroy(); /** * Suspend execution of the calling thread for (at least) @@ -103,7 +108,8 @@ void os_thread_env_destroy(); * * @return 0 if success, -1 otherwise */ -int os_usleep(uint32 usec); +int +os_usleep(uint32 usec); /** * Creates a recursive mutex @@ -112,7 +118,8 @@ int os_usleep(uint32 usec); * * @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 @@ -121,7 +128,8 @@ int os_recursive_mutex_init(korp_mutex *mutex); * * @return 0 if success */ -int os_cond_init(korp_cond *cond); +int +os_cond_init(korp_cond *cond); /** * This function destroys condition variable @@ -130,7 +138,8 @@ int os_cond_init(korp_cond *cond); * * @return 0 if success */ -int os_cond_destroy(korp_cond *cond); +int +os_cond_destroy(korp_cond *cond); /** * Wait a condition variable. @@ -140,7 +149,8 @@ int os_cond_destroy(korp_cond *cond); * * @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. @@ -151,7 +161,8 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex); * * @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 @@ -160,7 +171,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds); * * @return 0 if success */ -int os_cond_signal(korp_cond *cond); +int +os_cond_signal(korp_cond *cond); #ifdef __cplusplus } diff --git a/core/shared/platform/include/platform_api_vmcore.h b/core/shared/platform/include/platform_api_vmcore.h index 0f162fc0..c2f03c9e 100644 --- a/core/shared/platform/include/platform_api_vmcore.h +++ b/core/shared/platform/include/platform_api_vmcore.h @@ -25,23 +25,28 @@ extern "C" { * * @return 0 if success */ -int bh_platform_init(void); +int +bh_platform_init(void); /** * Destroy the platform internal resources if needed, * this function is called by wasm_runtime_destroy() */ -void bh_platform_destroy(void); +void +bh_platform_destroy(void); /** ******** 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 @@ -49,28 +54,32 @@ void os_free(void *ptr); * 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. */ -uint64 os_time_get_boot_microsecond(void); +uint64 +os_time_get_boot_microsecond(void); /** * Get current thread id. * 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 * to check the native stack overflow. Return NULL if it is not * easy to implement, but may have potential issue. */ -uint8 *os_thread_get_stack_boundary(void); +uint8 * +os_thread_get_stack_boundary(void); /** ************** mutext APIs *********** @@ -78,14 +87,17 @@ uint8 *os_thread_get_stack_boundary(void); * 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_unlock(korp_mutex *mutex); +int +os_mutex_lock(korp_mutex *mutex); +int +os_mutex_unlock(korp_mutex *mutex); /************************************************** * Section 2 * @@ -110,9 +122,12 @@ enum { MMAP_MAP_FIXED = 2 }; -void *os_mmap(void *hint, size_t size, int prot, int flags); -void os_munmap(void *addr, size_t size); -int os_mprotect(void *addr, size_t size, int prot); +void * +os_mmap(void *hint, size_t size, int prot, int flags); +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 @@ -120,7 +135,8 @@ int os_mprotect(void *addr, size_t size, int prot); * which may cause unexpected behaviour when executing the AOT code. * Implement this function if required, or just leave it empty. */ -void os_dcache_flush(void); +void +os_dcache_flush(void); #ifdef __cplusplus } diff --git a/core/shared/platform/include/platform_common.h b/core/shared/platform/include/platform_common.h index 92418f81..57629654 100644 --- a/core/shared/platform/include/platform_common.h +++ b/core/shared/platform/include/platform_common.h @@ -42,24 +42,27 @@ extern "C" { __declspec(dllexport) void *BH_MALLOC(unsigned int size); __declspec(dllexport) void BH_FREE(void *ptr); #else -__declspec(dllimport) void* BH_MALLOC(unsigned int size); -__declspec(dllimport) void BH_FREE(void* ptr); +__declspec(dllimport) void *BH_MALLOC(unsigned int size); +__declspec(dllimport) void BH_FREE(void *ptr); #endif #else -void *BH_MALLOC(unsigned int size); -void BH_FREE(void *ptr); +void * +BH_MALLOC(unsigned int size); +void +BH_FREE(void *ptr); #endif #if defined(BH_VPRINTF) #if defined(MSVC) __declspec(dllimport) int BH_VPRINTF(const char *format, va_list ap); #else -int BH_VPRINTF(const char *format, va_list ap); +int +BH_VPRINTF(const char *format, va_list ap); #endif #endif #ifndef NULL -#define NULL (void*)0 +#define NULL (void *)0 #endif #ifndef __cplusplus @@ -99,8 +102,7 @@ typedef double float64; typedef uint64_t uint64; typedef int64_t int64; -typedef void* (*thread_start_routine_t)(void*); - +typedef void *(*thread_start_routine_t)(void *); #ifdef __cplusplus } diff --git a/core/shared/platform/linux-sgx/platform_internal.h b/core/shared/platform/linux-sgx/platform_internal.h index 65b40872..4ce7ee35 100644 --- a/core/shared/platform/linux-sgx/platform_internal.h +++ b/core/shared/platform/linux-sgx/platform_internal.h @@ -40,7 +40,7 @@ extern "C" { #define _STACK_SIZE_ADJUSTMENT (32 * 1024) /* 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 */ #define BH_THREAD_DEFAULT_PRIORITY 0 @@ -50,14 +50,15 @@ typedef pthread_t korp_tid; typedef pthread_mutex_t korp_mutex; typedef pthread_cond_t korp_cond; -typedef void (*os_print_function_t)(const char* message); -void os_set_print_function(os_print_function_t pf); +typedef void (*os_print_function_t)(const char *message); +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 } #endif #endif /* end of _PLATFORM_INTERNAL_H */ - diff --git a/core/shared/platform/linux-sgx/sgx_file.c b/core/shared/platform/linux-sgx/sgx_file.c index 4e21b8b4..240c4a78 100644 --- a/core/shared/platform/linux-sgx/sgx_file.c +++ b/core/shared/platform/linux-sgx/sgx_file.c @@ -13,97 +13,129 @@ #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) /** fd **/ -int ocall_open(int *p_fd, const char *pathname, int flags, - bool has_mode, unsigned mode); +int +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, - bool has_mode, unsigned mode); +int +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 **/ /** 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 **/ /** stat **/ -int 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 ocall_fstatat(int *p_ret, int dirfd, const char *pathname, - void *buf, unsigned int buf_len, int flags); +int +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 +ocall_fstatat(int *p_ret, int dirfd, const char *pathname, void *buf, + unsigned int buf_len, int flags); /** stat end **/ /** link **/ -int 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 ocall_linkat(int *p_ret, int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, int flags); -int ocall_unlinkat(int *p_ret, int dirfd, const char *pathname, - int flags); -int ocall_readlinkat(ssize_t *p_ret, int dirfd, const char *pathname, - char *buf, size_t bufsiz); -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); +int +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 +ocall_linkat(int *p_ret, int olddirfd, const char *oldpath, int newdirfd, + const char *newpath, int flags); +int +ocall_unlinkat(int *p_ret, int dirfd, const char *pathname, int flags); +int +ocall_readlinkat(ssize_t *p_ret, int dirfd, const char *pathname, char *buf, + size_t bufsiz); +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 **/ /** control **/ -int ocall_ioctl(int *p_ret, int fd, unsigned long request, void *arg, - unsigned int arg_len); -int ocall_fcntl(int *p_ret, int fd, int cmd); -int ocall_fcntl_long(int *p_ret, int fd, int cmd, long arg); +int +ocall_ioctl(int *p_ret, int fd, unsigned long request, void *arg, + unsigned int arg_len); +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 **/ /** **/ -int 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 ocall_poll(int *p_ret, void *fds, unsigned nfds, int timeout, - unsigned int fds_len); -int ocall_getopt(int *p_ret, int argc, char *argv_buf, - unsigned int argv_buf_len, const char *optstring); -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); +int +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 +ocall_poll(int *p_ret, void *fds, unsigned nfds, int timeout, + unsigned int fds_len); +int +ocall_getopt(int *p_ret, int argc, char *argv_buf, unsigned int argv_buf_len, + const char *optstring); +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 **/ -ssize_t ocall_readv(ssize_t *p_ret, int fd, char *iov_buf, - unsigned int buf_size, int iovcnt, - bool has_offset, off_t offset); -ssize_t ocall_writev(ssize_t *p_ret, int fd, char *iov_buf, - unsigned int buf_size, int iovcnt, - bool has_offset, off_t offset); +ssize_t +ocall_readv(ssize_t *p_ret, int fd, char *iov_buf, unsigned int buf_size, + int iovcnt, bool has_offset, off_t offset); +ssize_t +ocall_writev(ssize_t *p_ret, int fd, char *iov_buf, unsigned int buf_size, + int iovcnt, bool has_offset, off_t offset); /** 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; bool has_mode = false; @@ -130,7 +162,8 @@ int open(const char *pathname, int flags, ...) return fd; } -int openat(int dirfd, const char *pathname, int flags, ...) +int +openat(int dirfd, const char *pathname, int flags, ...) { int fd; bool has_mode = false; @@ -144,8 +177,8 @@ int openat(int dirfd, const char *pathname, int flags, ...) has_mode = true; } - if (SGX_SUCCESS != ocall_openat(&fd, dirfd, pathname, flags, - has_mode, mode)) { + if (SGX_SUCCESS + != ocall_openat(&fd, dirfd, pathname, flags, has_mode, mode)) { TRACE_OCALL_FAIL(); return -1; } @@ -158,7 +191,8 @@ int openat(int dirfd, const char *pathname, int flags, ...) return fd; } -int close(int fd) +int +close(int fd) { int ret; @@ -171,7 +205,8 @@ int close(int fd) 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; 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; for (i = 0; i < count; i++) { - size_read = (i < count - 1) - ? size_read_max - : size - size_read_max * i; + size_read = (i < count - 1) ? size_read_max : size - size_read_max * i; if (ocall_read(&ret, fd, p, size_read) != SGX_SUCCESS) { TRACE_OCALL_FAIL(); @@ -208,7 +241,8 @@ ssize_t read(int fd, void *buf, size_t size) return total_size_read; } -DIR *fdopendir(int fd) +DIR * +fdopendir(int fd) { DIR *result = NULL; @@ -232,7 +266,8 @@ DIR *fdopendir(int fd) return result; } -struct dirent *readdir(DIR *dirp) +struct dirent * +readdir(DIR *dirp) { struct dirent *result; @@ -249,21 +284,24 @@ struct dirent *readdir(DIR *dirp) return result; } -void rewinddir(DIR *dirp) +void +rewinddir(DIR *dirp) { if (ocall_rewinddir((void *)*dirp) != SGX_SUCCESS) { TRACE_OCALL_FAIL(); } } -void seekdir(DIR *dirp, long loc) +void +seekdir(DIR *dirp, long loc) { if (ocall_seekdir((void *)*dirp, loc) != SGX_SUCCESS) { TRACE_OCALL_FAIL(); } } -long telldir(DIR *dirp) +long +telldir(DIR *dirp) { long ret; @@ -276,7 +314,8 @@ long telldir(DIR *dirp) return ret; } -int closedir(DIR *dirp) +int +closedir(DIR *dirp) { int ret; @@ -291,8 +330,8 @@ int closedir(DIR *dirp) } static ssize_t -readv_internal(int fd, const struct iovec *iov, int iovcnt, - bool has_offset, off_t offset) +readv_internal(int fd, const struct iovec *iov, int iovcnt, bool has_offset, + off_t offset) { ssize_t ret, size_left; struct iovec *iov1; @@ -317,7 +356,7 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt, 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++) { 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; } - if (ocall_readv(&ret, fd, (char *)iov1, (uint32)total_size, - iovcnt, has_offset, offset) != SGX_SUCCESS) { + if (ocall_readv(&ret, fd, (char *)iov1, (uint32)total_size, iovcnt, + has_offset, offset) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); BH_FREE(iov1); return -1; @@ -343,8 +383,7 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt, size_left -= iov[i].iov_len; } else { - memcpy(iov[i].iov_base, (uintptr_t)p + (char *)iov1, - size_left); + memcpy(iov[i].iov_base, (uintptr_t)p + (char *)iov1, size_left); break; } } @@ -356,8 +395,8 @@ readv_internal(int fd, const struct iovec *iov, int iovcnt, } static ssize_t -writev_internal(int fd, const struct iovec *iov, int iovcnt, - bool has_offset, off_t offset) +writev_internal(int fd, const struct iovec *iov, int iovcnt, bool has_offset, + off_t offset) { ssize_t ret; struct iovec *iov1; @@ -387,13 +426,13 @@ writev_internal(int fd, const struct iovec *iov, int iovcnt, for (i = 0; i < iovcnt; i++) { iov1[i].iov_len = iov[i].iov_len; iov1[i].iov_base = p; - memcpy((uintptr_t)p + (char *)iov1, iov[i].iov_base, - iov[i].iov_len); + memcpy((uintptr_t)p + (char *)iov1, iov[i].iov_base, iov[i].iov_len); p += iov[i].iov_len; } - if (ocall_writev(&ret, fd, (char *)iov1, (uint32)total_size, - iovcnt, has_offset, offset) != SGX_SUCCESS) { + if (ocall_writev(&ret, fd, (char *)iov1, (uint32)total_size, iovcnt, + has_offset, offset) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); BH_FREE(iov1); return -1; @@ -405,29 +444,32 @@ writev_internal(int fd, const struct iovec *iov, int iovcnt, 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); } -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); } -ssize_t preadv(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) { return readv_internal(fd, iov, iovcnt, true, offset); } -ssize_t pwritev(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) { 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; 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; } -int ftruncate(int fd, off_t length) +int +ftruncate(int fd, off_t length) { int ret; @@ -452,16 +495,16 @@ int ftruncate(int fd, off_t length) return ret; } -int stat(const char *pathname, struct stat *statbuf) +int +stat(const char *pathname, struct stat *statbuf) { int ret; if (statbuf == NULL) return -1; - if (ocall_stat(&ret, pathname, - (void *)statbuf, - sizeof(struct stat)) != SGX_SUCCESS) { + if (ocall_stat(&ret, pathname, (void *)statbuf, sizeof(struct stat)) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -471,15 +514,16 @@ int stat(const char *pathname, struct stat *statbuf) return ret; } -int fstat(int fd, struct stat *statbuf) +int +fstat(int fd, struct stat *statbuf) { int ret; if (statbuf == NULL) return -1; - if (ocall_fstat(&ret, fd, (void *)statbuf, - sizeof(struct stat)) != SGX_SUCCESS) { + if (ocall_fstat(&ret, fd, (void *)statbuf, sizeof(struct stat)) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -489,8 +533,8 @@ int fstat(int fd, struct stat *statbuf) return ret; } -int fstatat(int dirfd, const char *pathname, struct stat *statbuf, - int flags) +int +fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) { int ret; @@ -498,7 +542,8 @@ int fstatat(int dirfd, const char *pathname, struct stat *statbuf, return -1; if (ocall_fstatat(&ret, dirfd, pathname, (void *)statbuf, - sizeof(struct stat), flags) != SGX_SUCCESS) { + sizeof(struct stat), flags) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -508,7 +553,8 @@ int fstatat(int dirfd, const char *pathname, struct stat *statbuf, return ret; } -int fsync(int fd) +int +fsync(int fd) { int ret; @@ -521,7 +567,8 @@ int fsync(int fd) return ret; } -int fdatasync(int fd) +int +fdatasync(int fd) { int ret; @@ -534,7 +581,8 @@ int fdatasync(int fd) return ret; } -int mkdirat(int dirfd, const char *pathname, mode_t mode) +int +mkdirat(int dirfd, const char *pathname, mode_t mode) { int ret; @@ -548,7 +596,8 @@ int mkdirat(int dirfd, const char *pathname, mode_t mode) return ret; } -int link(const char *oldpath, const char *newpath) +int +link(const char *oldpath, const char *newpath) { int ret; @@ -562,13 +611,14 @@ int link(const char *oldpath, const char *newpath) return ret; } -int linkat(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, int flags) +int +linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, + int flags) { int ret; - if (ocall_linkat(&ret, olddirfd, oldpath, newdirfd, newpath, - flags) != SGX_SUCCESS) { + if (ocall_linkat(&ret, olddirfd, oldpath, newdirfd, newpath, flags) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -578,7 +628,8 @@ int linkat(int olddirfd, const char *oldpath, return ret; } -int unlinkat(int dirfd, const char *pathname, int flags) +int +unlinkat(int dirfd, const char *pathname, int flags) { int ret; @@ -592,16 +643,15 @@ int unlinkat(int dirfd, const char *pathname, int flags) return ret; } -ssize_t readlinkat(int dirfd, const char *pathname, - char *buf, size_t bufsiz) +ssize_t +readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { ssize_t ret; if (buf == NULL) return -1; - if (ocall_readlinkat(&ret, dirfd, pathname, buf, - bufsiz) != SGX_SUCCESS) { + if (ocall_readlinkat(&ret, dirfd, pathname, buf, bufsiz) != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -611,12 +661,12 @@ ssize_t readlinkat(int dirfd, const char *pathname, return ret; } -int symlinkat(const char *target, int newdirfd, const char *linkpath) +int +symlinkat(const char *target, int newdirfd, const char *linkpath) { int ret; - if (ocall_symlinkat(&ret, target, - newdirfd, linkpath) != SGX_SUCCESS) { + if (ocall_symlinkat(&ret, target, newdirfd, linkpath) != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -626,13 +676,13 @@ int symlinkat(const char *target, int newdirfd, const char *linkpath) return ret; } -int renameat(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath) +int +renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { int ret; - if (ocall_renameat(&ret, olddirfd, oldpath, - newdirfd, newpath) != SGX_SUCCESS) { + if (ocall_renameat(&ret, olddirfd, oldpath, newdirfd, newpath) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -642,7 +692,8 @@ int renameat(int olddirfd, const char *oldpath, return ret; } -int ioctl(int fd, unsigned long request, ...) +int +ioctl(int fd, unsigned long request, ...) { int ret; va_list args; @@ -651,8 +702,8 @@ int ioctl(int fd, unsigned long request, ...) case FIONREAD: va_start(args, request); int *arg = (int *)va_arg(args, int *); - if (ocall_ioctl(&ret, fd, request, arg, - sizeof(*arg)) != SGX_SUCCESS) { + if (ocall_ioctl(&ret, fd, request, arg, sizeof(*arg)) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); va_end(args); return -1; @@ -670,7 +721,8 @@ int ioctl(int fd, unsigned long request, ...) return ret; } -int fcntl(int fd, int cmd, ... /* arg */ ) +int +fcntl(int fd, int cmd, ... /* arg */) { int ret; va_list args; @@ -707,7 +759,8 @@ int fcntl(int fd, int cmd, ... /* arg */ ) return ret; } -int isatty(int fd) +int +isatty(int fd) { int ret; @@ -720,7 +773,8 @@ int isatty(int fd) return ret; } -char *realpath(const char *path, char *resolved_path) +char * +realpath(const char *path, char *resolved_path) { int ret; char buf[PATH_MAX] = { 0 }; @@ -746,7 +800,8 @@ char *realpath(const char *path, char *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; @@ -758,15 +813,16 @@ int posix_fallocate(int fd, off_t offset, off_t len) return ret; } -int poll(struct pollfd *fds, nfds_t nfds, int timeout) +int +poll(struct pollfd *fds, nfds_t nfds, int timeout) { int ret; if (fds == NULL) return -1; - if (ocall_poll(&ret, fds, nfds, timeout, - sizeof(*fds) * nfds) != SGX_SUCCESS) { + if (ocall_poll(&ret, fds, nfds, timeout, sizeof(*fds) * nfds) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -776,8 +832,8 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) return ret; } -int getopt(int argc, char * const argv[], - const char *optstring) +int +getopt(int argc, char *const argv[], const char *optstring) { int ret; char **argv1; @@ -805,8 +861,8 @@ int getopt(int argc, char * const argv[], p += ((uintptr_t)strlen(argv[i]) + 1); } - if (ocall_getopt(&ret, argc, (char *)argv1, total_size, - optstring) != SGX_SUCCESS) { + if (ocall_getopt(&ret, argc, (char *)argv1, total_size, optstring) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); BH_FREE(argv1); return -1; @@ -818,7 +874,8 @@ int getopt(int argc, char * const argv[], return ret; } -int sched_yield(void) +int +sched_yield(void) { int ret; @@ -831,7 +888,8 @@ int sched_yield(void) 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; @@ -844,7 +902,8 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) return ret; } -int getentropy(void *buffer, size_t length) +int +getentropy(void *buffer, size_t length) { int ret; @@ -857,7 +916,8 @@ int getentropy(void *buffer, size_t length) return ret; } -int get_errno(void) +int +get_errno(void) { int ret; @@ -869,4 +929,3 @@ int get_errno(void) } #endif - diff --git a/core/shared/platform/linux-sgx/sgx_file.h b/core/shared/platform/linux-sgx/sgx_file.h index 178e6e7f..83be395b 100644 --- a/core/shared/platform/linux-sgx/sgx_file.h +++ b/core/shared/platform/linux-sgx/sgx_file.h @@ -12,45 +12,45 @@ extern "C" { #endif -#define F_DUPFD 0 -#define F_GETFD 1 -#define F_SETFD 2 -#define F_GETFL 3 -#define F_SETFL 4 +#define F_DUPFD 0 +#define F_GETFD 1 +#define F_SETFD 2 +#define F_GETFL 3 +#define F_SETFL 4 #define FD_CLOEXEC 1 -#define O_PATH 010000000 -#define O_SEARCH O_PATH -#define O_EXEC O_PATH +#define O_PATH 010000000 +#define O_SEARCH O_PATH +#define O_EXEC O_PATH -#define O_ACCMODE (03|O_SEARCH) -#define O_RDONLY 00 -#define O_WRONLY 01 -#define O_RDWR 02 +#define O_ACCMODE (03 | O_SEARCH) +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 -#define O_CREAT 0100 -#define O_EXCL 0200 -#define O_NOCTTY 0400 -#define O_TRUNC 01000 -#define O_APPEND 02000 -#define O_NONBLOCK 04000 -#define O_DSYNC 010000 -#define O_SYNC 04010000 -#define O_RSYNC 04010000 +#define O_CREAT 0100 +#define O_EXCL 0200 +#define O_NOCTTY 0400 +#define O_TRUNC 01000 +#define O_APPEND 02000 +#define O_NONBLOCK 04000 +#define O_DSYNC 010000 +#define O_SYNC 04010000 +#define O_RSYNC 04010000 #define O_DIRECTORY 0200000 -#define O_NOFOLLOW 0400000 -#define O_CLOEXEC 02000000 +#define O_NOFOLLOW 0400000 +#define O_CLOEXEC 02000000 -#define O_ASYNC 020000 -#define O_DIRECT 040000 -#define O_LARGEFILE 0 -#define O_NOATIME 01000000 -#define O_PATH 010000000 +#define O_ASYNC 020000 +#define O_DIRECT 040000 +#define O_LARGEFILE 0 +#define O_NOATIME 01000000 +#define O_PATH 010000000 #define O_TMPFILE 020200000 #define O_NDELAY O_NONBLOCK -#define S_IFMT 0170000 +#define S_IFMT 0170000 #define S_IFDIR 0040000 #define S_IFCHR 0020000 #define S_IFBLK 0060000 @@ -63,13 +63,13 @@ extern "C" { #define SEEK_CUR 1 #define SEEK_END 2 -#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) -#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) -#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) -#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) -#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) -#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) -#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) +#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) +#define S_ISCHR(mode) (((mode)&S_IFMT) == S_IFCHR) +#define S_ISBLK(mode) (((mode)&S_IFMT) == S_IFBLK) +#define S_ISREG(mode) (((mode)&S_IFMT) == S_IFREG) +#define S_ISFIFO(mode) (((mode)&S_IFMT) == S_IFIFO) +#define S_ISLNK(mode) (((mode)&S_IFMT) == S_IFLNK) +#define S_ISSOCK(mode) (((mode)&S_IFMT) == S_IFSOCK) #define DT_UNKNOWN 0 #define DT_FIFO 1 @@ -85,24 +85,24 @@ extern "C" { #define AT_REMOVEDIR 0x200 #define AT_SYMLINK_FOLLOW 0x400 -#define POLLIN 0x001 -#define POLLPRI 0x002 -#define POLLOUT 0x004 -#define POLLERR 0x008 -#define POLLHUP 0x010 -#define POLLNVAL 0x020 +#define POLLIN 0x001 +#define POLLPRI 0x002 +#define POLLOUT 0x004 +#define POLLERR 0x008 +#define POLLHUP 0x010 +#define POLLNVAL 0x020 #define POLLRDNORM 0x040 #define POLLRDBAND 0x080 #define POLLWRNORM 0x100 #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 working directory. */ -#define AT_FDCWD -100 +#define AT_FDCWD -100 typedef long __syscall_slong_t; @@ -138,7 +138,7 @@ struct stat { mode_t st_mode; uid_t st_uid; gid_t st_gid; - unsigned int __pad0; + unsigned int __pad0; dev_t st_rdev; off_t st_size; blksize_t st_blksize; @@ -156,76 +156,108 @@ struct iovec { }; struct pollfd { - int fd; - short events; - short revents; + int fd; + short events; + short revents; }; -int open(const char *pathname, int flags, ...); -int openat(int dirfd, const char *pathname, int flags, ...); -int close(int fd); +int +open(const char *pathname, int flags, ...); +int +openat(int dirfd, const char *pathname, int flags, ...); +int +close(int fd); -DIR *fdopendir(int fd); -int closedir(DIR *dirp); -void rewinddir(DIR *dirp); -void seekdir(DIR *dirp, long loc); -struct dirent *readdir(DIR *dirp); -long telldir(DIR *dirp); +DIR * +fdopendir(int fd); +int +closedir(DIR *dirp); +void +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 readv(int fd, const struct iovec *iov, int iovcnt); -ssize_t writev(int fd, const struct iovec *iov, int iovcnt); -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); +ssize_t +read(int fd, void *buf, size_t count); +ssize_t +readv(int fd, const struct iovec *iov, int iovcnt); +ssize_t +writev(int fd, const struct iovec *iov, int iovcnt); +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); -int ftruncate(int fd, off_t length); +off_t +lseek(int fd, off_t offset, int whence); +int +ftruncate(int fd, off_t length); -int stat(const char *pathname, struct stat *statbuf); -int fstat(int fd, struct stat *statbuf); -int fstatat(int dirfd, const char *pathname, struct stat *statbuf, - int flags); +int +stat(const char *pathname, struct stat *statbuf); +int +fstat(int fd, struct stat *statbuf); +int +fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags); -int fsync(int fd); -int fdatasync(int fd); +int +fsync(int fd); +int +fdatasync(int fd); -int mkdirat(int dirfd, const char *pathname, mode_t mode); -int link(const char *oldpath, const char *newpath); -int linkat(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, int flags); -int unlinkat(int dirfd, const char *pathname, int flags); -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 +mkdirat(int dirfd, const char *pathname, mode_t mode); +int +link(const char *oldpath, const char *newpath); +int +linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, + int flags); +int +unlinkat(int dirfd, const char *pathname, int flags); +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 fcntl(int fd, int cmd, ... /* arg */ ); +int +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[], - const char *optstring); +int +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); -int getentropy(void *buffer, size_t length); +ssize_t +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 } #endif #endif /* end of _SGX_FILE_H */ - diff --git a/core/shared/platform/linux-sgx/sgx_platform.c b/core/shared/platform/linux-sgx/sgx_platform.c index abfb9911..a383698f 100644 --- a/core/shared/platform/linux-sgx/sgx_platform.c +++ b/core/shared/platform/linux-sgx/sgx_platform.c @@ -7,19 +7,19 @@ #include "platform_api_extension.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; -int bh_platform_init() +int +bh_platform_init() { return 0; } void bh_platform_destroy() -{ -} +{} void * os_malloc(unsigned size) @@ -39,22 +39,26 @@ os_free(void *ptr) free(ptr); } -int putchar(int c) +int +putchar(int c) { return 0; } -int puts(const char *s) +int +puts(const char *s) { 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; } -int os_printf(const char *message, ...) +int +os_printf(const char *message, ...) { if (print_function != NULL) { char msg[FIXED_BUFFER_SIZE] = { '\0' }; @@ -68,7 +72,8 @@ int os_printf(const char *message, ...) return 0; } -int os_vprintf(const char * format, va_list arg) +int +os_vprintf(const char *format, va_list arg) { if (print_function != NULL) { char msg[FIXED_BUFFER_SIZE] = { '\0' }; @@ -79,20 +84,23 @@ int os_vprintf(const char * format, va_list arg) return 0; } -char *strcpy(char *dest, const char *src) +char * +strcpy(char *dest, const char *src) { const unsigned char *s = src; unsigned char *d = dest; - while ((*d++ = *s++)); + while ((*d++ = *s++)) + ; 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; uint64 aligned_size, page_size; - void* ret = NULL; + void *ret = NULL; sgx_status_t st = 0; 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); if (ret == NULL) { - os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.", - size, aligned_size, prot); + os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.", size, + aligned_size, prot); 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); if (st != SGX_SUCCESS) { - os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.", - size, prot); + os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.", size, + prot); sgx_free_rsrv_mem(ret, aligned_size); return NULL; } @@ -126,7 +134,8 @@ void* os_mmap(void *hint, size_t size, int prot, int flags) return ret; } -void os_munmap(void *addr, size_t size) +void +os_munmap(void *addr, size_t 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); } -int os_mprotect(void *addr, size_t size, int prot) +int +os_mprotect(void *addr, size_t size, int prot) { int mprot = 0; sgx_status_t st = 0; @@ -152,14 +162,12 @@ int os_mprotect(void *addr, size_t size, int prot) mprot |= SGX_PROT_EXEC; st = sgx_tprotect_rsrv_mem(addr, aligned_size, mprot); 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); - return (st == SGX_SUCCESS? 0:-1); + return (st == SGX_SUCCESS ? 0 : -1); } void os_dcache_flush(void) -{ -} - +{} diff --git a/core/shared/platform/linux-sgx/sgx_pthread.c b/core/shared/platform/linux-sgx/sgx_pthread.c index cf000565..7801e353 100644 --- a/core/shared/platform/linux-sgx/sgx_pthread.c +++ b/core/shared/platform/linux-sgx/sgx_pthread.c @@ -15,22 +15,27 @@ #ifndef SGX_THREAD_LOCK_INITIALIZER /* defined since sgxsdk-2.11 */ /* sgxsdk doesn't support pthread_rwlock related APIs until 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; - if (ocall_pthread_rwlock_init(&ret, (void **)rwlock, NULL) - != SGX_SUCCESS) { + if (ocall_pthread_rwlock_init(&ret, (void **)rwlock, NULL) != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -38,7 +43,8 @@ int pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr) return ret; } -int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) +int +pthread_rwlock_destroy(pthread_rwlock_t *rwlock) { int ret = -1; @@ -48,31 +54,34 @@ int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) return ret; } -int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) +int +pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) { 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(); } return ret; } -int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) +int +pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) { 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(); } return ret; } -int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) +int +pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { 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(); } return ret; @@ -80,4 +89,3 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) #endif /* end of SGX_THREAD_LOCK_INITIALIZER */ #endif - diff --git a/core/shared/platform/linux-sgx/sgx_pthread.h b/core/shared/platform/linux-sgx/sgx_pthread.h index 513bd3ab..01a3ae04 100644 --- a/core/shared/platform/linux-sgx/sgx_pthread.h +++ b/core/shared/platform/linux-sgx/sgx_pthread.h @@ -15,12 +15,17 @@ extern "C" { version 2.11, we implement them by ourselves. */ typedef uintptr_t pthread_rwlock_t; -int pthread_rwlock_init(pthread_rwlock_t *rwlock, void *attr); -int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); +int +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 pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); -int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); +int +pthread_rwlock_wrlock(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 */ #ifdef __cplusplus @@ -28,4 +33,3 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); #endif #endif /* end of _SGX_PTHREAD_H */ - diff --git a/core/shared/platform/linux-sgx/sgx_rsrv_mem_mngr.h b/core/shared/platform/linux-sgx/sgx_rsrv_mem_mngr.h index b32a68ba..5555d4d9 100644 --- a/core/shared/platform/linux-sgx/sgx_rsrv_mem_mngr.h +++ b/core/shared/platform/linux-sgx/sgx_rsrv_mem_mngr.h @@ -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 * not included this header file yet. */ @@ -43,48 +44,52 @@ #include "stdint.h" #include "sgx_error.h" -#define SGX_PROT_READ 0x1 /* page can be read */ -#define SGX_PROT_WRITE 0x2 /* page can be written */ -#define SGX_PROT_EXEC 0x4 /* page can be executed */ -#define SGX_PROT_NONE 0x0 /* page can not be accessed */ +#define SGX_PROT_READ 0x1 /* page can be read */ +#define SGX_PROT_WRITE 0x2 /* page can be written */ +#define SGX_PROT_EXEC 0x4 /* page can be executed */ +#define SGX_PROT_NONE 0x0 /* page can not be accessed */ #ifdef __cplusplus extern "C" { #endif - /* 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 - * Return: Starting address of the new allocated memory area on success; otherwise NULL - */ - void * sgx_alloc_rsrv_mem(size_t length); +/* 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. + * Return: Starting address of the new allocated memory area on success; + * 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 - * - * 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); - - - /* Modify the access permissions of the pages in the reserved memory area. - * - * 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); - +/* Modify the access permissions of the pages in the reserved memory area. + * + * 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 } #endif #endif - diff --git a/core/shared/platform/linux-sgx/sgx_signal.c b/core/shared/platform/linux-sgx/sgx_signal.c index 698272ca..b52c1882 100644 --- a/core/shared/platform/linux-sgx/sgx_signal.c +++ b/core/shared/platform/linux-sgx/sgx_signal.c @@ -9,9 +9,11 @@ #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; @@ -27,4 +29,3 @@ int raise(int sig) } #endif - diff --git a/core/shared/platform/linux-sgx/sgx_signal.h b/core/shared/platform/linux-sgx/sgx_signal.h index b83a9235..494342be 100644 --- a/core/shared/platform/linux-sgx/sgx_signal.h +++ b/core/shared/platform/linux-sgx/sgx_signal.h @@ -11,47 +11,47 @@ extern "C" { #endif /* Signals. */ -#define SIGHUP 1 /* Hangup (POSIX). */ -#define SIGINT 2 /* Interrupt (ANSI). */ -#define SIGQUIT 3 /* Quit (POSIX). */ -#define SIGILL 4 /* Illegal instruction (ANSI). */ -#define SIGTRAP 5 /* Trace trap (POSIX). */ -#define SIGABRT 6 /* Abort (ANSI). */ -#define SIGIOT 6 /* IOT trap (4.2 BSD). */ -#define SIGBUS 7 /* BUS error (4.2 BSD). */ -#define SIGFPE 8 /* Floating-point exception (ANSI). */ -#define SIGKILL 9 /* Kill, unblockable (POSIX). */ -#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */ -#define SIGSEGV 11 /* Segmentation violation (ANSI). */ -#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */ -#define SIGPIPE 13 /* Broken pipe (POSIX). */ -#define SIGALRM 14 /* Alarm clock (POSIX). */ -#define SIGTERM 15 /* Termination (ANSI). */ -#define SIGSTKFLT 16 /* Stack fault. */ -#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */ -#define SIGCHLD 17 /* Child status has changed (POSIX). */ -#define SIGCONT 18 /* Continue (POSIX). */ -#define SIGSTOP 19 /* Stop, unblockable (POSIX). */ -#define SIGTSTP 20 /* Keyboard stop (POSIX). */ -#define SIGTTIN 21 /* Background read from tty (POSIX). */ -#define SIGTTOU 22 /* Background write to tty (POSIX). */ -#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */ -#define SIGXCPU 24 /* CPU 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 SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */ -#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */ -#define SIGPOLL SIGIO /* Pollable event occurred (System V). */ -#define SIGIO 29 /* I/O now possible (4.2 BSD). */ -#define SIGPWR 30 /* Power failure restart (System V). */ -#define SIGSYS 31 /* Bad system call. */ -#define SIGUNUSED 31 +#define SIGHUP 1 /* Hangup (POSIX). */ +#define SIGINT 2 /* Interrupt (ANSI). */ +#define SIGQUIT 3 /* Quit (POSIX). */ +#define SIGILL 4 /* Illegal instruction (ANSI). */ +#define SIGTRAP 5 /* Trace trap (POSIX). */ +#define SIGABRT 6 /* Abort (ANSI). */ +#define SIGIOT 6 /* IOT trap (4.2 BSD). */ +#define SIGBUS 7 /* BUS error (4.2 BSD). */ +#define SIGFPE 8 /* Floating-point exception (ANSI). */ +#define SIGKILL 9 /* Kill, unblockable (POSIX). */ +#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */ +#define SIGSEGV 11 /* Segmentation violation (ANSI). */ +#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */ +#define SIGPIPE 13 /* Broken pipe (POSIX). */ +#define SIGALRM 14 /* Alarm clock (POSIX). */ +#define SIGTERM 15 /* Termination (ANSI). */ +#define SIGSTKFLT 16 /* Stack fault. */ +#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */ +#define SIGCHLD 17 /* Child status has changed (POSIX). */ +#define SIGCONT 18 /* Continue (POSIX). */ +#define SIGSTOP 19 /* Stop, unblockable (POSIX). */ +#define SIGTSTP 20 /* Keyboard stop (POSIX). */ +#define SIGTTIN 21 /* Background read from tty (POSIX). */ +#define SIGTTOU 22 /* Background write to tty (POSIX). */ +#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */ +#define SIGXCPU 24 /* CPU 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 SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */ +#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */ +#define SIGPOLL SIGIO /* Pollable event occurred (System V). */ +#define SIGIO 29 /* I/O now possible (4.2 BSD). */ +#define SIGPWR 30 /* Power failure restart (System V). */ +#define SIGSYS 31 /* Bad system call. */ +#define SIGUNUSED 31 -int raise(int sig); +int +raise(int sig); #ifdef __cplusplus } #endif #endif /* end of _SGX_SIGNAL_H */ - diff --git a/core/shared/platform/linux-sgx/sgx_socket.c b/core/shared/platform/linux-sgx/sgx_socket.c index e4ed97d2..e9a824fd 100644 --- a/core/shared/platform/linux-sgx/sgx_socket.c +++ b/core/shared/platform/linux-sgx/sgx_socket.c @@ -9,18 +9,23 @@ #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) -int ocall_socket(int *p_ret, int domain, int type, int protocol); -int ocall_getsockopt(int *p_ret, int sockfd, int level, int optname, - void *val_buf, unsigned int val_buf_size, - void *len_buf); +int +ocall_socket(int *p_ret, int domain, int type, int protocol); +int +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, - unsigned int msg_buf_size, int flags); -int 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 +ocall_sendmsg(ssize_t *p_ret, int sockfd, void *msg_buf, + unsigned int msg_buf_size, int flags); +int +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; @@ -35,14 +40,15 @@ int socket(int domain, int type, int protocol) return ret; } -int getsockopt(int sockfd, int level, int optname, - void *optval, socklen_t *optlen) +int +getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) { int ret; unsigned int val_buf_size = *optlen; - if (ocall_getsockopt(&ret, sockfd, level, optname, optval, - val_buf_size, (void *)optlen) != SGX_SUCCESS) { + if (ocall_getsockopt(&ret, sockfd, level, optname, optval, val_buf_size, + (void *)optlen) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -53,7 +59,8 @@ int getsockopt(int sockfd, int level, int optname, 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; int i; @@ -77,7 +84,7 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) if (msg1 == NULL) return -1; - p = (char*)(uintptr_t)sizeof(struct msghdr); + p = (char *)(uintptr_t)sizeof(struct msghdr); if (msg->msg_name != NULL) { 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, - flags) != SGX_SUCCESS) { + if (ocall_sendmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, flags) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -118,7 +125,8 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) 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; int i; @@ -144,7 +152,7 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) memset(msg1, 0, total_size); - p = (char*)(uintptr_t)sizeof(struct msghdr); + p = (char *)(uintptr_t)sizeof(struct msghdr); if (msg->msg_name != NULL) { 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, - flags) != SGX_SUCCESS) { + if (ocall_recvmsg(&ret, sockfd, (void *)msg1, (uint32)total_size, flags) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -203,7 +211,8 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) return ret; } -int shutdown(int sockfd, int how) +int +shutdown(int sockfd, int how) { int ret; @@ -219,4 +228,3 @@ int shutdown(int sockfd, int how) } #endif - diff --git a/core/shared/platform/linux-sgx/sgx_socket.h b/core/shared/platform/linux-sgx/sgx_socket.h index c0703545..6e5b0438 100644 --- a/core/shared/platform/linux-sgx/sgx_socket.h +++ b/core/shared/platform/linux-sgx/sgx_socket.h @@ -12,32 +12,32 @@ extern "C" { #endif -#define SOL_SOCKET 1 +#define SOL_SOCKET 1 -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 -#define SO_TYPE 3 +#define SO_TYPE 3 -#define MSG_OOB 0x0001 -#define MSG_PEEK 0x0002 +#define MSG_OOB 0x0001 +#define MSG_PEEK 0x0002 #define MSG_DONTROUTE 0x0004 -#define MSG_CTRUNC 0x0008 -#define MSG_PROXY 0x0010 -#define MSG_TRUNC 0x0020 -#define MSG_DONTWAIT 0x0040 -#define MSG_EOR 0x0080 -#define MSG_WAITALL 0x0100 -#define MSG_FIN 0x0200 -#define MSG_SYN 0x0400 -#define MSG_CONFIRM 0x0800 -#define MSG_RST 0x1000 -#define MSG_ERRQUEUE 0x2000 -#define MSG_NOSIGNAL 0x4000 -#define MSG_MORE 0x8000 +#define MSG_CTRUNC 0x0008 +#define MSG_PROXY 0x0010 +#define MSG_TRUNC 0x0020 +#define MSG_DONTWAIT 0x0040 +#define MSG_EOR 0x0080 +#define MSG_WAITALL 0x0100 +#define MSG_FIN 0x0200 +#define MSG_SYN 0x0400 +#define MSG_CONFIRM 0x0800 +#define MSG_RST 0x1000 +#define MSG_ERRQUEUE 0x2000 +#define MSG_NOSIGNAL 0x4000 +#define MSG_MORE 0x8000 #define MSG_WAITFORONE 0x10000 -#define MSG_BATCH 0x40000 -#define MSG_FASTOPEN 0x20000000 +#define MSG_BATCH 0x40000 +#define MSG_FASTOPEN 0x20000000 #define MSG_CMSG_CLOEXEC 0x40000000 #define SHUT_RD 0 @@ -54,21 +54,23 @@ struct msghdr { 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, - void *optval, socklen_t *optlen); +ssize_t +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 } #endif #endif /* end of _SGX_SOCKET_H */ - diff --git a/core/shared/platform/linux-sgx/sgx_thread.c b/core/shared/platform/linux-sgx/sgx_thread.c index d1503b47..34405a10 100644 --- a/core/shared/platform/linux-sgx/sgx_thread.c +++ b/core/shared/platform/linux-sgx/sgx_thread.c @@ -12,9 +12,10 @@ typedef struct { void *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; void *thread_arg = targ->arg; os_printf("THREAD CREATED %p\n", &targ); @@ -23,15 +24,16 @@ static void *os_thread_wrapper(void *arg) return NULL; } -int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start, - void *arg, unsigned int stack_size, int prio) +int +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; assert(tid); assert(start); - targ = (thread_wrapper_arg *) BH_MALLOC(sizeof(*targ)); + targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ)); if (!targ) { return BHT_ERROR; } @@ -47,15 +49,17 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start, return BHT_OK; } -int os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg, - unsigned int stack_size) +int +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, BH_THREAD_DEFAULT_PRIORITY); } #endif -korp_tid os_self_thread() +korp_tid +os_self_thread() { #ifndef SGX_DISABLE_PTHREAD return pthread_self(); @@ -64,7 +68,8 @@ korp_tid os_self_thread() #endif } -int os_mutex_init(korp_mutex *mutex) +int +os_mutex_init(korp_mutex *mutex) { #ifndef SGX_DISABLE_PTHREAD pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; @@ -73,7 +78,8 @@ int os_mutex_init(korp_mutex *mutex) return BHT_OK; } -int os_mutex_destroy(korp_mutex *mutex) +int +os_mutex_destroy(korp_mutex *mutex) { #ifndef SGX_DISABLE_PTHREAD pthread_mutex_destroy(mutex); @@ -81,7 +87,8 @@ int os_mutex_destroy(korp_mutex *mutex) return BHT_OK; } -int os_mutex_lock(korp_mutex *mutex) +int +os_mutex_lock(korp_mutex *mutex) { #ifndef SGX_DISABLE_PTHREAD return pthread_mutex_lock(mutex); @@ -90,7 +97,8 @@ int os_mutex_lock(korp_mutex *mutex) #endif } -int os_mutex_unlock(korp_mutex *mutex) +int +os_mutex_unlock(korp_mutex *mutex) { #ifndef SGX_DISABLE_PTHREAD return pthread_mutex_unlock(mutex); @@ -99,7 +107,8 @@ int os_mutex_unlock(korp_mutex *mutex) #endif } -int os_cond_init(korp_cond *cond) +int +os_cond_init(korp_cond *cond) { #ifndef SGX_DISABLE_PTHREAD pthread_cond_t c = PTHREAD_COND_INITIALIZER; @@ -108,7 +117,8 @@ int os_cond_init(korp_cond *cond) return BHT_OK; } -int os_cond_destroy(korp_cond *cond) +int +os_cond_destroy(korp_cond *cond) { #ifndef SGX_DISABLE_PTHREAD pthread_cond_destroy(cond); @@ -116,7 +126,8 @@ int os_cond_destroy(korp_cond *cond) 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 assert(cond); @@ -129,14 +140,16 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex) 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, " "calling pthread_cond_wait instead!\n"); return BHT_ERROR; } -int os_cond_signal(korp_cond *cond) +int +os_cond_signal(korp_cond *cond) { #ifndef SGX_DISABLE_PTHREAD assert(cond); @@ -148,7 +161,8 @@ int os_cond_signal(korp_cond *cond) 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 return pthread_join(thread, value_ptr); @@ -157,13 +171,15 @@ int os_thread_join(korp_tid thread, void **value_ptr) #endif } -int os_thread_detach(korp_tid thread) +int +os_thread_detach(korp_tid thread) { /* SGX pthread_detach isn't provided, return directly. */ return 0; } -void os_thread_exit(void *retval) +void +os_thread_exit(void *retval) { #ifndef SGX_DISABLE_PTHREAD pthread_exit(retval); @@ -172,9 +188,9 @@ void os_thread_exit(void *retval) #endif } -uint8 *os_thread_get_stack_boundary() +uint8 * +os_thread_get_stack_boundary() { /* TODO: get sgx stack boundary */ return NULL; } - diff --git a/core/shared/platform/linux-sgx/sgx_time.c b/core/shared/platform/linux-sgx/sgx_time.c index 34fe1257..c945bbcc 100644 --- a/core/shared/platform/linux-sgx/sgx_time.c +++ b/core/shared/platform/linux-sgx/sgx_time.c @@ -8,18 +8,22 @@ #define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__) #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) -int ocall_clock_gettime(int *p_ret, unsigned clock_id, - void *tp_buf, unsigned int tp_buf_size); -int ocall_clock_getres(int *p_ret, int clock_id, - void *res_buf, unsigned int res_buf_size); -int ocall_utimensat(int *p_ret, int dirfd, const char *pathname, - const void *times_buf, unsigned int times_buf_size, - int flags); -int ocall_futimens(int *p_ret, int fd, - const void *times_buf, 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); +int +ocall_clock_gettime(int *p_ret, unsigned clock_id, void *tp_buf, + unsigned int tp_buf_size); +int +ocall_clock_getres(int *p_ret, int clock_id, void *res_buf, + unsigned int res_buf_size); +int +ocall_utimensat(int *p_ret, int dirfd, const char *pathname, + const void *times_buf, unsigned int times_buf_size, int flags); +int +ocall_futimens(int *p_ret, int fd, const void *times_buf, + 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 os_time_get_boot_microsecond() @@ -30,12 +34,13 @@ os_time_get_boot_microsecond() #ifndef SGX_DISABLE_WASI -int clock_getres(int clock_id, struct timespec *res) +int +clock_getres(int clock_id, struct timespec *res) { int ret; - if (ocall_clock_getres(&ret, clock_id, (void *)res, - sizeof(struct timespec)) != SGX_SUCCESS) { + if (ocall_clock_getres(&ret, clock_id, (void *)res, sizeof(struct timespec)) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -46,12 +51,13 @@ int clock_getres(int clock_id, struct timespec *res) return ret; } -int clock_gettime(clockid_t clock_id, struct timespec *tp) +int +clock_gettime(clockid_t clock_id, struct timespec *tp) { int ret; - if(ocall_clock_gettime(&ret, clock_id, (void *)tp, - sizeof(struct timespec)) != SGX_SUCCESS) { + if (ocall_clock_gettime(&ret, clock_id, (void *)tp, sizeof(struct timespec)) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -62,14 +68,15 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp) return ret; } -int utimensat(int dirfd, const char *pathname, - const struct timespec times[2], int flags) +int +utimensat(int dirfd, const char *pathname, const struct timespec times[2], + int flags) { int ret; if (ocall_utimensat(&ret, dirfd, pathname, (void *)times, - sizeof(struct timespec) * 2, - flags) != SGX_SUCCESS) { + sizeof(struct timespec) * 2, flags) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -80,12 +87,13 @@ int utimensat(int dirfd, const char *pathname, return ret; } -int futimens(int fd, const struct timespec times[2]) +int +futimens(int fd, const struct timespec times[2]) { int ret; - if (ocall_futimens(&ret, fd, (void *)times, - sizeof(struct timespec) * 2) != SGX_SUCCESS) { + if (ocall_futimens(&ret, fd, (void *)times, sizeof(struct timespec) * 2) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -96,16 +104,16 @@ int futimens(int fd, const struct timespec times[2]) return ret; } -int clock_nanosleep(clockid_t clock_id, int flags, - const struct timespec *request, - struct timespec *remain) +int +clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request, + struct timespec *remain) { int ret; if (ocall_clock_nanosleep(&ret, clock_id, flags, (void *)request, - sizeof(struct timespec), - (void *)remain, - sizeof(struct timespec)) != SGX_SUCCESS) { + sizeof(struct timespec), (void *)remain, + sizeof(struct timespec)) + != SGX_SUCCESS) { TRACE_OCALL_FAIL(); return -1; } @@ -117,4 +125,3 @@ int clock_nanosleep(clockid_t clock_id, int flags, } #endif - diff --git a/core/shared/platform/linux-sgx/sgx_time.h b/core/shared/platform/linux-sgx/sgx_time.h index 2aa83b8c..8267f1fa 100644 --- a/core/shared/platform/linux-sgx/sgx_time.h +++ b/core/shared/platform/linux-sgx/sgx_time.h @@ -10,12 +10,12 @@ extern "C" { #endif -#define CLOCK_REALTIME 0 -#define CLOCK_MONOTONIC 1 +#define CLOCK_REALTIME 0 +#define CLOCK_MONOTONIC 1 #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 TIMER_ABSTIME 1 @@ -28,20 +28,23 @@ struct timespec { 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, - const struct timespec times[2], int flags); -int futimens(int fd, const struct timespec times[2]); -int clock_nanosleep(clockid_t clock_id, int flags, - const struct timespec *request, - struct timespec *remain); +int +utimensat(int dirfd, const char *pathname, const struct timespec times[2], + int flags); +int +futimens(int fd, const struct timespec times[2]); +int +clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request, + struct timespec *remain); #ifdef __cplusplus } #endif #endif /* end of _SGX_TIME_H */ - diff --git a/core/shared/platform/linux-sgx/untrusted/file.c b/core/shared/platform/linux-sgx/untrusted/file.c index 6852f222..4da4be2d 100644 --- a/core/shared/platform/linux-sgx/untrusted/file.c +++ b/core/shared/platform/linux-sgx/untrusted/file.c @@ -19,8 +19,8 @@ #include #include -int ocall_open(const char *pathname, int flags, - bool has_mode, unsigned mode) +int +ocall_open(const char *pathname, int flags, bool has_mode, unsigned mode) { if (has_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, - const char *pathname, int flags, - bool has_mode, unsigned mode) +int +ocall_openat(int dirfd, const char *pathname, int flags, bool has_mode, + unsigned mode) { if (has_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); } -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) { 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); } -int ocall_ftruncate(int fd, off_t length) +int +ocall_ftruncate(int fd, off_t length) { return ftruncate(fd, length); } -int ocall_fsync(int fd) +int +ocall_fsync(int fd) { return fsync(fd); } -int ocall_fdatasync(int fd) +int +ocall_fdatasync(int fd) { return fdatasync(fd); } -int ocall_isatty(int fd) +int +ocall_isatty(int fd) { return isatty(fd); } -void ocall_fdopendir(int fd, void **dirp) +void +ocall_fdopendir(int fd, void **dirp) { if (dirp) { *(DIR **)dirp = fdopendir(fd); } } -void *ocall_readdir(void *dirp) +void * +ocall_readdir(void *dirp) { DIR *p_dirp = (DIR *)dirp; return readdir(p_dirp); } -void ocall_rewinddir(void *dirp) +void +ocall_rewinddir(void *dirp) { DIR *p_dirp = (DIR *)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; @@ -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; if (p_dirp) { @@ -121,7 +133,8 @@ long ocall_telldir(void *dirp) return -1; } -int ocall_closedir(void* dirp) +int +ocall_closedir(void *dirp) { DIR *p_dirp = (DIR *)dirp; if (p_dirp) { @@ -130,82 +143,91 @@ int ocall_closedir(void* dirp) return -1; } -int ocall_stat(const char *pathname, - void *buf, unsigned int buf_len) +int +ocall_stat(const char *pathname, void *buf, unsigned int buf_len) { 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); } -int ocall_fstatat(int dirfd, const char *pathname, void *buf, - unsigned int buf_len, int flags) +int +ocall_fstatat(int dirfd, const char *pathname, void *buf, unsigned int buf_len, + int 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); } -int ocall_link(const char *oldpath, const char *newpath) +int +ocall_link(const char *oldpath, const char *newpath) { return link(oldpath, newpath); } -int ocall_linkat(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, int flags) +int +ocall_linkat(int olddirfd, const char *oldpath, int newdirfd, + const char *newpath, int 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); } -ssize_t ocall_readlinkat(int dirfd, const char *pathname, - char *buf, size_t bufsiz) +ssize_t +ocall_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { return readlinkat(dirfd, pathname, buf, bufsiz); } -int ocall_renameat(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath) +int +ocall_renameat(int olddirfd, const char *oldpath, int newdirfd, + const char *newpath) { return renameat(olddirfd, oldpath, newdirfd, newpath); } -int ocall_symlinkat(const char *target, int newdirfd, - const char *linkpath) +int +ocall_symlinkat(const char *target, int newdirfd, const char *linkpath) { return symlinkat(target, newdirfd, linkpath); } -int ocall_ioctl(int fd, unsigned long request, - void *arg, unsigned int arg_len) +int +ocall_ioctl(int fd, unsigned long request, void *arg, unsigned int arg_len) { /* support just int *arg temporally */ 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, - unsigned int buf_size, int iovcnt, - bool has_offset, off_t offset) +ssize_t +ocall_readv(int fd, char *iov_buf, unsigned int buf_size, int iovcnt, + bool has_offset, off_t offset) { struct iovec *iov = (struct iovec *)iov_buf; ssize_t ret; @@ -223,9 +245,9 @@ ssize_t ocall_readv(int fd, char *iov_buf, return ret; } -ssize_t ocall_writev(int fd, char *iov_buf, - unsigned int buf_size, int iovcnt, - bool has_offset, off_t offset) +ssize_t +ocall_writev(int fd, char *iov_buf, unsigned int buf_size, int iovcnt, + bool has_offset, off_t offset) { struct iovec *iov = (struct iovec *)iov_buf; int i; @@ -243,9 +265,10 @@ ssize_t ocall_writev(int fd, char *iov_buf, 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); if (val != NULL) { return 0; @@ -253,47 +276,53 @@ int ocall_realpath(const char *path, char *buf, unsigned int buf_len) 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); } -int ocall_poll(void *fds, unsigned nfds, int timeout, - unsigned int fds_len) +int +ocall_poll(void *fds, unsigned nfds, int timeout, unsigned int fds_len) { return poll((struct pollfd *)fds, (nfds_t)nfds, timeout); } -int ocall_getopt(int argc, char *argv_buf, unsigned int argv_buf_len, - const char *optstring) +int +ocall_getopt(int argc, char *argv_buf, unsigned int argv_buf_len, + const char *optstring) { int ret; int i; 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]; } 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); } -int ocall_getentropy(void *buffer, size_t length) +int +ocall_getentropy(void *buffer, size_t length) { return getentropy(buffer, length); } -int ocall_sched_yield() +int +ocall_sched_yield() { return sched_yield(); } -int ocall_get_errno() +int +ocall_get_errno() { return errno; } diff --git a/core/shared/platform/linux-sgx/untrusted/pthread.c b/core/shared/platform/linux-sgx/untrusted/pthread.c index f3026874..890ef754 100644 --- a/core/shared/platform/linux-sgx/untrusted/pthread.c +++ b/core/shared/platform/linux-sgx/untrusted/pthread.c @@ -6,7 +6,8 @@ #include #include -int ocall_pthread_rwlock_init(void **rwlock, void *attr) +int +ocall_pthread_rwlock_init(void **rwlock, void *attr) { int ret = 0; @@ -15,7 +16,7 @@ int ocall_pthread_rwlock_init(void **rwlock, void *attr) return -1; ret = pthread_rwlock_init((pthread_rwlock_t *)*rwlock, NULL); - if (ret != 0) { + if (ret != 0) { free(*rwlock); *rwlock = NULL; } @@ -23,7 +24,8 @@ int ocall_pthread_rwlock_init(void **rwlock, void *attr) return ret; } -int ocall_pthread_rwlock_destroy(void *rwlock) +int +ocall_pthread_rwlock_destroy(void *rwlock) { pthread_rwlock_t *lock = (pthread_rwlock_t *)rwlock; int ret; @@ -33,18 +35,20 @@ int ocall_pthread_rwlock_destroy(void *rwlock) return ret; } -int ocall_pthread_rwlock_rdlock(void *rwlock) +int +ocall_pthread_rwlock_rdlock(void *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); } -int ocall_pthread_rwlock_unlock(void *rwlock) +int +ocall_pthread_rwlock_unlock(void *rwlock) { return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock); } - diff --git a/core/shared/platform/linux-sgx/untrusted/signal.c b/core/shared/platform/linux-sgx/untrusted/signal.c index 115fbfa5..b2eecfb7 100644 --- a/core/shared/platform/linux-sgx/untrusted/signal.c +++ b/core/shared/platform/linux-sgx/untrusted/signal.c @@ -4,7 +4,8 @@ */ #include -int ocall_raise(int sig) +int +ocall_raise(int sig) { return raise(sig); } \ No newline at end of file diff --git a/core/shared/platform/linux-sgx/untrusted/socket.c b/core/shared/platform/linux-sgx/untrusted/socket.c index 34b5d460..08d884bb 100644 --- a/core/shared/platform/linux-sgx/untrusted/socket.c +++ b/core/shared/platform/linux-sgx/untrusted/socket.c @@ -7,20 +7,21 @@ #include #include -int ocall_socket(int domain, int type, int protocol) +int +ocall_socket(int domain, int type, int protocol) { return socket(domain, type, protocol); } -int ocall_getsockopt(int sockfd, int level, int optname, void *val_buf, - unsigned int val_buf_size, void *len_buf) +int +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, - (socklen_t *)len_buf); + return getsockopt(sockfd, level, optname, val_buf, (socklen_t *)len_buf); } -ssize_t ocall_sendmsg(int sockfd, void *msg_buf, - unsigned int msg_buf_size, int flags) +ssize_t +ocall_sendmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size, int flags) { struct msghdr *msg = (struct msghdr *)msg_buf; int i; @@ -35,16 +36,16 @@ ssize_t ocall_sendmsg(int sockfd, void *msg_buf, if (msg->msg_iov != NULL) { msg->msg_iov = msg_buf + (unsigned)(uintptr_t)msg->msg_iov; 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); } -ssize_t ocall_recvmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size, - int flags) +ssize_t +ocall_recvmsg(int sockfd, void *msg_buf, unsigned int msg_buf_size, int flags) { struct msghdr *msg = (struct msghdr *)msg_buf; 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) { msg->msg_iov = msg_buf + (unsigned)(uintptr_t)msg->msg_iov; - for (i = 0; i msg_iovlen; i++) { - msg->msg_iov[i].iov_base = msg_buf + (unsigned)(uintptr_t) - msg->msg_iov[i].iov_base; + 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; } } return recvmsg(sockfd, msg, flags); } -int ocall_shutdown(int sockfd, int how) +int +ocall_shutdown(int sockfd, int how) { return shutdown(sockfd, how); } \ No newline at end of file diff --git a/core/shared/platform/linux-sgx/untrusted/time.c b/core/shared/platform/linux-sgx/untrusted/time.c index 1d15e8df..5fa387b0 100644 --- a/core/shared/platform/linux-sgx/untrusted/time.c +++ b/core/shared/platform/linux-sgx/untrusted/time.c @@ -7,41 +7,38 @@ #include #include - /** time clock **/ -int ocall_clock_gettime(unsigned clock_id, void *tp_buf, - unsigned int tp_buf_size) +int +ocall_clock_gettime(unsigned clock_id, void *tp_buf, unsigned int tp_buf_size) { - return clock_gettime((clockid_t)clock_id, - (struct timespec *)tp_buf); + return clock_gettime((clockid_t)clock_id, (struct timespec *)tp_buf); } -int ocall_clock_getres(int clock_id, void *res_buf, - unsigned int res_buf_size) +int +ocall_clock_getres(int clock_id, void *res_buf, unsigned int res_buf_size) { return clock_getres(clock_id, (struct timespec *)res_buf); } -int ocall_utimensat(int dirfd, const char *pathname, - const void *times_buf, - unsigned int times_buf_size, int flags) +int +ocall_utimensat(int dirfd, const char *pathname, const void *times_buf, + unsigned int times_buf_size, int flags) { return utimensat(dirfd, pathname, (struct timespec *)times_buf, flags); - } -int ocall_futimens(int fd, const void *times_buf, - unsigned int times_buf_size) +int +ocall_futimens(int fd, const void *times_buf, unsigned int times_buf_size) { return futimens(fd, (struct timespec *)times_buf); } -int ocall_clock_nanosleep(unsigned clock_id, int flags, - const void *req_buf, unsigned int req_buf_size, - const void *rem_buf, unsigned int rem_buf_size) +int +ocall_clock_nanosleep(unsigned clock_id, int flags, const void *req_buf, + unsigned int req_buf_size, const void *rem_buf, + unsigned int rem_buf_size) { return clock_nanosleep((clockid_t)clock_id, flags, (struct timespec *)req_buf, (struct timespec *)rem_buf); - } diff --git a/core/shared/platform/linux/platform_init.c b/core/shared/platform/linux/platform_init.c index 17aeb8ba..2aae13fa 100644 --- a/core/shared/platform/linux/platform_init.c +++ b/core/shared/platform/linux/platform_init.c @@ -13,8 +13,7 @@ bh_platform_init() void bh_platform_destroy() -{ -} +{} int os_printf(const char *format, ...) @@ -42,4 +41,3 @@ os_vprintf(const char *format, va_list ap) return BH_VPRINTF(format, ap); #endif } - diff --git a/core/shared/platform/linux/platform_internal.h b/core/shared/platform/linux/platform_internal.h index 3e5c7e3f..58b6a6ad 100644 --- a/core/shared/platform/linux/platform_internal.h +++ b/core/shared/platform/linux/platform_internal.h @@ -59,10 +59,8 @@ typedef pthread_t korp_thread; #define os_thread_local_attribute __thread #if WASM_DISABLE_HW_BOUND_CHECK == 0 -#if defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_AARCH64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ + || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) #include @@ -79,15 +77,20 @@ typedef jmp_buf korp_jmpbuf; 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 WASM_DISABLE_HW_BOUND_CHECK */ @@ -96,4 +99,3 @@ void os_sigreturn(); #endif #endif /* end of _PLATFORM_INTERNAL_H */ - diff --git a/core/shared/platform/nuttx/platform_internal.h b/core/shared/platform/nuttx/platform_internal.h index 14e44ad4..a4a2b697 100644 --- a/core/shared/platform/nuttx/platform_internal.h +++ b/core/shared/platform/nuttx/platform_internal.h @@ -38,7 +38,7 @@ typedef pthread_t korp_thread; /* Default thread priority */ #define BH_THREAD_DEFAULT_PRIORITY 100 -#define os_printf printf +#define os_printf printf #define os_vprintf vprintf /* On NuttX, time_t is uint32_t */ diff --git a/core/shared/platform/riot/platform_internal.h b/core/shared/platform/riot/platform_internal.h index 91d08cfe..0de7dfdd 100644 --- a/core/shared/platform/riot/platform_internal.h +++ b/core/shared/platform/riot/platform_internal.h @@ -7,12 +7,12 @@ #ifndef _PLATFORM_INTERNAL_H #define _PLATFORM_INTERNAL_H -//Riot includes core +/* Riot includes core */ #include #include #include -//Riot includes sys +/* Riot includes sys */ #include #include @@ -39,19 +39,20 @@ typedef thread_t korp_thread; typedef kernel_pid_t korp_tid; typedef mutex_t korp_mutex; -// typedef sema_t korp_sem; +/* typedef sema_t korp_sem; */ struct os_thread_wait_node; typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct korp_cond { - mutex_t wait_list_lock; - os_thread_wait_list thread_wait_list; + mutex_t wait_list_lock; + os_thread_wait_list thread_wait_list; } korp_cond; -#define os_printf printf +#define os_printf printf #define os_vprintf vprintf #if WA_MATH +/* clang-format off */ /* math functions which are not provided by os*/ double sqrt(double x); double floor(double x); @@ -69,7 +70,7 @@ float rintf(float x); float truncf(float x); int signbit(double x); int isnan(double x); +/* clang-format on */ #endif #endif /* end of _BH_PLATFORM_H */ - diff --git a/core/shared/platform/riot/riot_thread.c b/core/shared/platform/riot/riot_thread.c index e324a9d0..e103a096 100644 --- a/core/shared/platform/riot/riot_thread.c +++ b/core/shared/platform/riot/riot_thread.c @@ -9,19 +9,21 @@ #include +/* clang-format off */ #define bh_assert(v) do { \ if (!(v)) { \ printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ #v, __FILE__, __LINE__); \ - core_panic(0,0/*expr_string*/); \ + core_panic(0, 0/*expr_string*/); \ while (1); \ } \ - } while (0) +} while (0) +/* clang-format on */ struct os_thread_data; typedef struct os_thread_wait_node { sema_t sem; - void * ret; + void *ret; os_thread_wait_list next; } os_thread_wait_node; @@ -63,7 +65,8 @@ static mutex_t thread_data_lock; /* Thread data list */ 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); if (!thread_data_list) @@ -86,7 +89,8 @@ static void thread_data_list_add(os_thread_data *thread_data) 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); if (thread_data_list) { @@ -153,7 +157,7 @@ thread_data_current() static 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(); bh_assert(thread_data != NULL); mutex_lock(&thread_data->wait_list_lock); @@ -177,7 +181,7 @@ static void * os_thread_wrapper(void *thread_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(); thread_data_list_add(t); @@ -187,13 +191,13 @@ os_thread_wrapper(void *thread_data) os_thread_cleanup(); // internal structures and joiners BH_FREE(thread_data); - sched_task_exit();// stop thread //clean - return NULL; //never reached + sched_task_exit(); // stop thread //clean + return NULL; // never reached } int -os_thread_create(korp_tid *p_tid, thread_start_routine_t start, - void *arg, 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, 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; /* Create the thread &*/ - if (!((tid = thread_create( thread_data->stack, - stack_size,prio,0, os_thread_wrapper, thread_data,"WASM")))) { + if (!((tid = thread_create(thread_data->stack, stack_size, prio, 0, + os_thread_wrapper, thread_data, "WASM")))) { BH_FREE(thread_data); return BHT_ERROR; } @@ -240,15 +244,14 @@ os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, korp_tid os_self_thread() { - return (korp_tid) thread_getpid(); + return (korp_tid)thread_getpid(); } 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, // wait if it is -// (void) value_ptr; os_thread_data *thread_data; os_thread_wait_node node; @@ -257,8 +260,8 @@ os_thread_join (korp_tid thread, void **value_ptr) /* Get thread data */ thread_data = thread_data_list_lookup(thread); - if(thread_data == NULL){ - //thread not found + if (thread_data == NULL) { + // thread not found sema_destroy(&node.sem); return BHT_ERROR; } @@ -277,22 +280,21 @@ os_thread_join (korp_tid thread, void **value_ptr) mutex_unlock(&thread_data->wait_list_lock); sema_wait(&node.sem); - //get the return value pointer conted may not be availible after return - if(value_ptr) (* value_ptr) = node.ret; + // get the return value pointer conted may not be availible after return + if (value_ptr) + (*value_ptr) = node.ret; /* 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(); - //node has done its job + // node has done its job sema_destroy(&node.sem); return BHT_OK; } - - // int vm_mutex_trylock(korp_mutex *mutex) // { // return mutex_trylock(mutex); @@ -308,7 +310,7 @@ os_mutex_init(korp_mutex *mutex) int os_mutex_destroy(korp_mutex *mutex) { - (void) mutex; + (void)mutex; return BHT_OK; } @@ -316,15 +318,14 @@ int os_mutex_lock(korp_mutex *mutex) { mutex_lock(mutex); - return 0; //Riot mutexes do not return until success + return 0; // Riot mutexes do not return until success } int os_mutex_unlock(korp_mutex *mutex) { mutex_unlock(mutex); - return 0; //Riot mutexes do not return until success - + return 0; // Riot mutexes do not return until success } int @@ -338,13 +339,13 @@ os_cond_init(korp_cond *cond) int os_cond_destroy(korp_cond *cond) { - (void) cond; + (void)cond; return BHT_OK; } static int -os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, - bool timed, uint64 useconds) +os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, + uint64 useconds) { 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 */ mutex_unlock(mutex); - if(timed) + if (timed) sema_wait(&node->sem); else - sema_wait_timed(&node->sem,useconds); + sema_wait_timed(&node->sem, useconds); mutex_lock(mutex); /* Remove wait node from wait list */ @@ -401,8 +402,8 @@ os_cond_wait(korp_cond *cond, korp_mutex *mutex) int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) { - return os_cond_wait_internal(cond, mutex, - (useconds != BHT_WAIT_FOREVER), useconds); + return os_cond_wait_internal(cond, mutex, (useconds != BHT_WAIT_FOREVER), + useconds); } int @@ -417,11 +418,12 @@ os_cond_signal(korp_cond *cond) return BHT_OK; } -uint8 *os_thread_get_stack_boundary() +uint8 * +os_thread_get_stack_boundary() { #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \ || defined(MODULE_MPU_STACK_GUARD) - return (uint8*)thread_get_active()->stack_start; + return (uint8 *)thread_get_active()->stack_start; #else return NULL; #endif diff --git a/core/shared/platform/riot/riot_time.c b/core/shared/platform/riot/riot_time.c index 8b480f69..24bc1841 100644 --- a/core/shared/platform/riot/riot_time.c +++ b/core/shared/platform/riot/riot_time.c @@ -12,4 +12,3 @@ os_time_get_boot_microsecond() { return xtimer_now_usec64(); } - diff --git a/core/shared/platform/rt-thread/platform_internal.h b/core/shared/platform/rt-thread/platform_internal.h index 9fe1c9c6..4fc29e94 100644 --- a/core/shared/platform/rt-thread/platform_internal.h +++ b/core/shared/platform/rt-thread/platform_internal.h @@ -44,5 +44,4 @@ typedef rt_int16_t int16_t; typedef rt_uint64_t uint64_t; typedef rt_int64_t int64_t; - -#endif //RTTHREAD_PLATFORM_INTERNAL_H +#endif /* RTTHREAD_PLATFORM_INTERNAL_H */ diff --git a/core/shared/platform/rt-thread/rtt_platform.c b/core/shared/platform/rt-thread/rtt_platform.c index 493d766c..13eb4398 100644 --- a/core/shared/platform/rt-thread/rtt_platform.c +++ b/core/shared/platform/rt-thread/rtt_platform.c @@ -8,67 +8,65 @@ #include typedef struct os_malloc_list { - void* real; - void* used; + void *real; + void *used; rt_list_t node; -}os_malloc_list_t; +} os_malloc_list_t; -int bh_platform_init(void) +int +bh_platform_init(void) { 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_fixed; rt_ubase_t *addr_field; buf_origin = rt_malloc(size + 8 + sizeof(rt_ubase_t)); - buf_fixed = buf_origin + sizeof(void*); - if ((rt_ubase_t)buf_fixed & 0x7) - { - buf_fixed = (void*)((rt_ubase_t)(buf_fixed+8)&(~7)); + buf_fixed = buf_origin + sizeof(void *); + if ((rt_ubase_t)buf_fixed & 0x7) { + buf_fixed = (void *)((rt_ubase_t)(buf_fixed + 8) & (~7)); } 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; - } -void *os_realloc(void *ptr, unsigned size) +void * +os_realloc(void *ptr, unsigned size) { - void* mem_origin; - void* mem_new; + void *mem_origin; + void *mem_new; void *mem_new_fixed; rt_ubase_t *addr_field; - if (!ptr) - { + if (!ptr) { return RT_NULL; } 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)); - if (mem_origin != mem_new) - { + if (mem_origin != mem_new) { mem_new_fixed = mem_new + sizeof(rt_ubase_t); - if ((rt_ubase_t)mem_new_fixed & 0x7) - { - mem_new_fixed = (void*)((rt_ubase_t)(mem_new_fixed+8)&(~7)); + if ((rt_ubase_t)mem_new_fixed & 0x7) { + mem_new_fixed = (void *)((rt_ubase_t)(mem_new_fixed + 8) & (~7)); } 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; } @@ -76,115 +74,130 @@ void *os_realloc(void *ptr, unsigned size) return ptr; } -void os_free(void *ptr) +void +os_free(void *ptr) { - void* mem_origin; + void *mem_origin; rt_ubase_t *addr_field; - if (ptr) - { + if (ptr) { addr_field = ptr - sizeof(rt_ubase_t); - mem_origin = (void*)(*addr_field); + mem_origin = (void *)(*addr_field); rt_free(mem_origin); } - } - 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_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; rt_kputs(wamr_vprint_buf); va_end(ap); 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; rt_kputs(wamr_vprint_buf); 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; return ret; } -korp_tid os_self_thread(void) +korp_tid +os_self_thread(void) { 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(); 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); } -int os_mutex_destroy(korp_mutex *mutex) +int +os_mutex_destroy(korp_mutex *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); } -int os_mutex_unlock(korp_mutex *mutex) +int +os_mutex_unlock(korp_mutex *mutex) { return rt_mutex_release(mutex); } - /* * functions below was not implement */ -int os_cond_init(korp_cond *cond) +int +os_cond_init(korp_cond *cond) { return 0; } -int os_cond_destroy(korp_cond *cond) +int +os_cond_destroy(korp_cond *cond) { return 0; } -int os_cond_wait(korp_cond *cond, korp_mutex *mutex) +int +os_cond_wait(korp_cond *cond, korp_mutex *mutex) { 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); } -void os_munmap(void *addr, size_t size) +void +os_munmap(void *addr, size_t size) { 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; } -void os_dcache_flush(void) -{ -} +void +os_dcache_flush(void) +{} diff --git a/core/shared/platform/vxworks/platform_init.c b/core/shared/platform/vxworks/platform_init.c index 17aeb8ba..2aae13fa 100644 --- a/core/shared/platform/vxworks/platform_init.c +++ b/core/shared/platform/vxworks/platform_init.c @@ -13,8 +13,7 @@ bh_platform_init() void bh_platform_destroy() -{ -} +{} int os_printf(const char *format, ...) @@ -42,4 +41,3 @@ os_vprintf(const char *format, va_list ap) return BH_VPRINTF(format, ap); #endif } - diff --git a/core/shared/platform/vxworks/platform_internal.h b/core/shared/platform/vxworks/platform_internal.h index 56870b56..9fa30722 100644 --- a/core/shared/platform/vxworks/platform_internal.h +++ b/core/shared/platform/vxworks/platform_internal.h @@ -58,8 +58,7 @@ typedef pthread_t korp_thread; #define os_thread_local_attribute __thread #if WASM_DISABLE_HW_BOUND_CHECK == 0 -#if defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) \ +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) #include @@ -76,15 +75,20 @@ typedef jmp_buf korp_jmpbuf; 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 WASM_DISABLE_HW_BOUND_CHECK */ @@ -93,4 +97,3 @@ void os_sigreturn(); #endif #endif /* end of _PLATFORM_INTERNAL_H */ - diff --git a/core/shared/platform/windows/platform_init.c b/core/shared/platform/windows/platform_init.c index bdb75f85..7505e532 100644 --- a/core/shared/platform/windows/platform_init.c +++ b/core/shared/platform/windows/platform_init.c @@ -60,6 +60,4 @@ os_getpagesize() void os_dcache_flush(void) -{ -} - +{} diff --git a/core/shared/platform/windows/platform_internal.h b/core/shared/platform/windows/platform_internal.h index 0b6d3963..59830527 100644 --- a/core/shared/platform/windows/platform_internal.h +++ b/core/shared/platform/windows/platform_internal.h @@ -53,15 +53,17 @@ typedef struct korp_cond { os_thread_wait_list thread_wait_list; } korp_cond; -unsigned os_getpagesize(); -void *os_mem_commit(void *ptr, size_t size, int flags); -void os_mem_decommit(void *ptr, size_t size); +unsigned +os_getpagesize(); +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) #if WASM_DISABLE_HW_BOUND_CHECK == 0 -#if defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) #include @@ -72,11 +74,14 @@ typedef jmp_buf korp_jmpbuf; #define os_setjmp setjmp #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_sigreturn() (void)0 @@ -89,4 +94,3 @@ bool os_thread_signal_inited(); #endif #endif /* end of _PLATFORM_INTERNAL_H */ - diff --git a/core/shared/platform/windows/win_malloc.c b/core/shared/platform/windows/win_malloc.c index e83fc7d7..660d1baa 100644 --- a/core/shared/platform/windows/win_malloc.c +++ b/core/shared/platform/windows/win_malloc.c @@ -22,6 +22,3 @@ os_free(void *ptr) { free(ptr); } - - - diff --git a/core/shared/platform/windows/win_memmap.c b/core/shared/platform/windows/win_memmap.c index 4d45897d..7afe7396 100644 --- a/core/shared/platform/windows/win_memmap.c +++ b/core/shared/platform/windows/win_memmap.c @@ -79,8 +79,7 @@ os_munmap(void *addr, size_t size) } } #if TRACE_MEMMAP != 0 - printf("Unmap memory, addr: %p, request_size: %zu\n", - addr, request_size); + printf("Unmap memory, addr: %p, request_size: %zu\n", addr, request_size); #endif } @@ -95,8 +94,8 @@ os_mem_commit(void *addr, size_t size, int flags) return NULL; #if TRACE_MEMMAP != 0 - printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", - addr, request_size, protect); + printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", addr, + request_size, protect); #endif return VirtualAlloc((LPVOID)addr, request_size, MEM_COMMIT, protect); } @@ -111,8 +110,8 @@ os_mem_decommit(void *addr, size_t size) return; #if TRACE_MEMMAP != 0 - printf("Decommit memory, addr: %p, request_size: %zu\n", - addr, request_size); + printf("Decommit memory, addr: %p, request_size: %zu\n", addr, + request_size); #endif VirtualFree((LPVOID)addr, request_size, MEM_DECOMMIT); } @@ -134,4 +133,3 @@ os_mprotect(void *addr, size_t size, int prot) #endif return VirtualProtect((LPVOID)addr, request_size, protect, NULL); } - diff --git a/core/shared/platform/windows/win_thread.c b/core/shared/platform/windows/win_thread.c index 95792f72..3a123d6f 100644 --- a/core/shared/platform/windows/win_thread.c +++ b/core/shared/platform/windows/win_thread.c @@ -47,11 +47,16 @@ static os_thread_data supervisor_thread_data; /* Thread data key */ static DWORD thread_data_key; -int os_sem_init(korp_sem* sem); -int os_sem_destroy(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 +os_sem_init(korp_sem *sem); +int +os_sem_destroy(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 os_thread_sys_init() @@ -141,8 +146,7 @@ os_thread_cleanup(void *retval) BH_FREE(thread_data); } -static unsigned __stdcall -os_thread_wrapper(void *arg) +static unsigned __stdcall os_thread_wrapper(void *arg) { os_thread_data *thread_data = arg; 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; os_mutex_lock(&parent->wait_lock); - if (!_beginthreadex(NULL, stack_size, - os_thread_wrapper, thread_data, - 0, NULL)) { + if (!_beginthreadex(NULL, stack_size, os_thread_wrapper, thread_data, 0, + NULL)) { os_mutex_unlock(&parent->wait_lock); goto fail4; } @@ -371,7 +374,7 @@ os_sem_wait(korp_sem *sem) if (ret == WAIT_OBJECT_0) return BHT_OK; - else if(ret == WAIT_TIMEOUT) + else if (ret == WAIT_TIMEOUT) return (int)WAIT_TIMEOUT; else /* WAIT_FAILED or others */ return BHT_ERROR; @@ -404,7 +407,7 @@ os_sem_reltimed_wait(korp_sem *sem, uint64 useconds) if (ret == WAIT_OBJECT_0) return BHT_OK; - else if(ret == WAIT_TIMEOUT) + else if (ret == WAIT_TIMEOUT) return (int)WAIT_TIMEOUT; else /* WAIT_FAILED or others */ return BHT_ERROR; @@ -414,8 +417,7 @@ int os_sem_signal(korp_sem *sem) { bh_assert(sem); - return ReleaseSemaphore(*sem, 1, NULL) != FALSE - ? BHT_OK: BHT_ERROR; + return ReleaseSemaphore(*sem, 1, NULL) != FALSE ? BHT_OK : BHT_ERROR; } int @@ -478,8 +480,8 @@ os_cond_destroy(korp_cond *cond) } static int -os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, - bool timed, uint64 useconds) +os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, + uint64 useconds) { os_thread_wait_node *node = &thread_data_current()->wait_node; @@ -567,7 +569,7 @@ os_thread_get_stack_boundary() GetCurrentThreadStackLimits(&low_limit, &high_limit); /* 4 pages are set unaccessible by system, we reserved 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; } @@ -601,4 +603,3 @@ os_thread_signal_inited() return thread_signal_inited; } #endif - diff --git a/core/shared/platform/windows/win_time.c b/core/shared/platform/windows/win_time.c index a48e4191..ea324f20 100644 --- a/core/shared/platform/windows/win_time.c +++ b/core/shared/platform/windows/win_time.c @@ -13,4 +13,3 @@ os_time_get_boot_microsecond() return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000; } - diff --git a/core/shared/platform/zephyr/platform_internal.h b/core/shared/platform/zephyr/platform_internal.h index 9de485f8..d09c3a92 100644 --- a/core/shared/platform/zephyr/platform_internal.h +++ b/core/shared/platform/zephyr/platform_internal.h @@ -37,7 +37,6 @@ #include #endif - #ifndef BH_PLATFORM_ZEPHYR #define BH_PLATFORM_ZEPHYR #endif @@ -62,6 +61,7 @@ typedef struct korp_cond { #define Z_TIMEOUT_MS(ms) ms #endif +/* clang-format off */ void abort(void); size_t strspn(const char *s, const char *accept); 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); double strtod(const char *nptr, char **endptr); float strtof(const char *nptr, char **endptr); +/* clang-format on */ /** * @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 */ -typedef void* (*exec_mem_alloc_func_t)(unsigned int size); +typedef void *(*exec_mem_alloc_func_t)(unsigned int size); /** * @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); -/* Below function are called by external project to set related function pointers that - * will be used to malloc/free executable memory. Otherwise 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); +/* Below function are called by external project to set related function + * pointers that will be used to malloc/free executable memory. Otherwise + * 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 diff --git a/core/shared/platform/zephyr/zephyr_platform.c b/core/shared/platform/zephyr/zephyr_platform.c index 4ecc828d..55ad84f4 100644 --- a/core/shared/platform/zephyr/zephyr_platform.c +++ b/core/shared/platform/zephyr/zephyr_platform.c @@ -29,9 +29,8 @@ disable_mpu_rasr_xn(void) MPU->RASR |= ~MPU_RASR_XN_Msk; } } - } -#endif /* end of CONFIG_ARM_MPU */ +#endif /* end of CONFIG_ARM_MPU */ #endif static int @@ -84,8 +83,7 @@ os_realloc(void *ptr, unsigned size) void os_free(void *ptr) -{ -} +{} #if 0 struct out_context { @@ -203,17 +201,17 @@ os_dcache_flush() SCB_CleanDCache(); irq_unlock(key); #elif defined(CONFIG_SOC_CVF_EM7D) && defined(CONFIG_ARC_MPU) \ - && defined (CONFIG_CACHE_FLUSHING) + && defined(CONFIG_CACHE_FLUSHING) __asm__ __volatile__("sync"); z_arc_v2_aux_reg_write(_ARC_V2_DC_FLSH, BIT(0)); __asm__ __volatile__("sync"); #endif } -void set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func, - exec_mem_free_func_t free_func) +void +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_free_func = free_func; } - diff --git a/core/shared/platform/zephyr/zephyr_thread.c b/core/shared/platform/zephyr/zephyr_thread.c index 55cadb47..1ee2c5ce 100644 --- a/core/shared/platform/zephyr/zephyr_thread.c +++ b/core/shared/platform/zephyr/zephyr_thread.c @@ -6,13 +6,15 @@ #include "platform_api_vmcore.h" #include "platform_api_extension.h" +/* clang-format off */ #define bh_assert(v) do { \ if (!(v)) { \ printf("\nASSERTION FAILED: %s, at %s, line %d\n", \ #v, __FILE__, __LINE__); \ abort(); \ } \ - } while (0) +} while (0) +/* clang-format on */ #if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) \ || KERNEL_VERSION_NUMBER > 0x020300 /* version 2.3.0 */ @@ -28,13 +30,13 @@ #endif #if BH_ENABLE_ZEPHYR_MPU_STACK != 0 -static K_THREAD_STACK_ARRAY_DEFINE(mpu_stacks, - BH_ZEPHYR_MPU_STACK_COUNT, +static K_THREAD_STACK_ARRAY_DEFINE(mpu_stacks, BH_ZEPHYR_MPU_STACK_COUNT, BH_ZEPHYR_MPU_STACK_SIZE); static bool mpu_stack_allocated[BH_ZEPHYR_MPU_STACK_COUNT]; static struct k_mutex mpu_stack_lock; -static char *mpu_stack_alloc() +static char * +mpu_stack_alloc() { int i; @@ -43,14 +45,15 @@ static char *mpu_stack_alloc() if (!mpu_stack_allocated[i]) { mpu_stack_allocated[i] = true; k_mutex_unlock(&mpu_stack_lock); - return (char*)mpu_stacks[i]; + return (char *)mpu_stacks[i]; } } k_mutex_unlock(&mpu_stack_lock); return NULL; } -static void mpu_stack_free(char *stack) +static void +mpu_stack_free(char *stack) { int i; @@ -114,7 +117,8 @@ static struct k_mutex thread_obj_lock; /* Thread object list */ 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); 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); } -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); if (thread_data_list) { @@ -174,7 +179,8 @@ thread_data_list_lookup(k_tid_t tid) 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); 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); } -static void thread_obj_list_reclaim() +static void +thread_obj_list_reclaim() { os_thread_obj *p, *p_prev; k_mutex_lock(&thread_obj_lock, K_FOREVER); @@ -199,12 +206,14 @@ static void thread_obj_list_reclaim() thread_obj_list = p->next; BH_FREE(p); 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; BH_FREE(p); p = p_prev->next; } - } else { + } + else { p_prev = p; p = p->next; } @@ -212,7 +221,8 @@ static void thread_obj_list_reclaim() k_mutex_unlock(&thread_obj_lock); } -int os_thread_sys_init() +int +os_thread_sys_init() { if (is_thread_sys_inited) return BHT_OK; @@ -233,7 +243,8 @@ int os_thread_sys_init() return BHT_OK; } -void os_thread_sys_destroy(void) +void +os_thread_sys_destroy(void) { if (is_thread_sys_inited) { is_thread_sys_inited = false; @@ -247,7 +258,8 @@ thread_data_current() 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(); @@ -269,32 +281,35 @@ static void os_thread_cleanup(void) thread_data_list_remove(thread_data); /* Set flag to true for the next thread creating to 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 mpu_stack_free(thread_data->stack); #endif 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 */ - ((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_start_routine_t) start)(arg); + ((thread_start_routine_t)start)(arg); os_thread_cleanup(); } -int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg, - unsigned int stack_size) +int +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, BH_THREAD_DEFAULT_PRIORITY); } -int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, - void *arg, unsigned int stack_size, int prio) +int +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; 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 */ 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; return BHT_OK; @@ -362,14 +377,16 @@ fail1: return BHT_ERROR; } -korp_tid os_self_thread() +korp_tid +os_self_thread() { 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_wait_node *node; @@ -407,30 +424,35 @@ int os_thread_join(korp_tid thread, void **value_ptr) return BHT_OK; } -int os_mutex_init(korp_mutex *mutex) +int +os_mutex_init(korp_mutex *mutex) { k_mutex_init(mutex); return BHT_OK; } -int os_recursive_mutex_init(korp_mutex *mutex) +int +os_recursive_mutex_init(korp_mutex *mutex) { k_mutex_init(mutex); return BHT_OK; } -int os_mutex_destroy(korp_mutex *mutex) +int +os_mutex_destroy(korp_mutex *mutex) { - (void) mutex; + (void)mutex; return BHT_OK; } -int os_mutex_lock(korp_mutex *mutex) +int +os_mutex_lock(korp_mutex *mutex) { 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 */ return k_mutex_unlock(mutex); @@ -440,21 +462,23 @@ int os_mutex_unlock(korp_mutex *mutex) #endif } -int os_cond_init(korp_cond *cond) +int +os_cond_init(korp_cond *cond) { k_mutex_init(&cond->wait_list_lock); cond->thread_wait_list = NULL; return BHT_OK; } -int os_cond_destroy(korp_cond *cond) +int +os_cond_destroy(korp_cond *cond) { - (void) cond; + (void)cond; return BHT_OK; } -static int os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, - bool timed, int mills) +static int +os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed, int mills) { os_thread_wait_node *node; @@ -499,12 +523,14 @@ static int os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, 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); } -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) { @@ -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 */ k_mutex_lock(&cond->wait_list_lock, K_FOREVER); @@ -537,13 +564,13 @@ int os_cond_signal(korp_cond *cond) return BHT_OK; } -uint8 *os_thread_get_stack_boundary() +uint8 * +os_thread_get_stack_boundary() { #if defined(CONFIG_THREAD_STACK_INFO) korp_tid thread = k_current_get(); - return (uint8*)thread->stack_info.start; + return (uint8 *)thread->stack_info.start; #else return NULL; #endif } - diff --git a/core/shared/platform/zephyr/zephyr_time.c b/core/shared/platform/zephyr/zephyr_time.c index 6a9db080..99eb3b35 100644 --- a/core/shared/platform/zephyr/zephyr_time.c +++ b/core/shared/platform/zephyr/zephyr_time.c @@ -10,4 +10,3 @@ os_time_get_boot_microsecond() { return k_uptime_get() * 1000; } - diff --git a/core/shared/utils/bh_assert.c b/core/shared/utils/bh_assert.c index cf93b170..f341df4d 100644 --- a/core/shared/utils/bh_assert.c +++ b/core/shared/utils/bh_assert.c @@ -5,8 +5,9 @@ #include "bh_assert.h" -void bh_assert_internal(int v, const char *file_name, int line_number, - const char *expr_string) +void +bh_assert_internal(int v, const char *file_name, int line_number, + const char *expr_string) { if (v) return; @@ -17,9 +18,8 @@ void bh_assert_internal(int v, const char *file_name, int line_number, if (!expr_string) expr_string = "NULL EXPR_STRING"; - os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n", - expr_string, file_name, line_number); + os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n", expr_string, + file_name, line_number); abort(); } - diff --git a/core/shared/utils/bh_assert.h b/core/shared/utils/bh_assert.h index 3a83d62b..ba89d12c 100644 --- a/core/shared/utils/bh_assert.h +++ b/core/shared/utils/bh_assert.h @@ -13,10 +13,11 @@ extern "C" { #endif #if BH_DEBUG != 0 -void bh_assert_internal(int v, const char *file_name, int line_number, - const char *expr_string); -#define bh_assert(expr) bh_assert_internal((int)(uintptr_t)(expr), \ - __FILE__, __LINE__, #expr) +void +bh_assert_internal(int v, const char *file_name, int line_number, + const char *expr_string); +#define bh_assert(expr) \ + bh_assert_internal((int)(uintptr_t)(expr), __FILE__, __LINE__, #expr) #else #define bh_assert(expr) (void)0 #endif /* end of BH_DEBUG */ @@ -26,4 +27,3 @@ void bh_assert_internal(int v, const char *file_name, int line_number, #endif #endif /* end of _BH_ASSERT_H */ - diff --git a/core/shared/utils/bh_common.c b/core/shared/utils/bh_common.c index 44bdbed1..690d573d 100644 --- a/core/shared/utils/bh_common.c +++ b/core/shared/utils/bh_common.c @@ -12,70 +12,67 @@ #define RSIZE_MAX 0x7FFFFFFF int -b_memcpy_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) { - char *dest = (char*)s1; - char *src = (char*)s2; - if (n == 0) { + 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; + } + memcpy(dest, src, n); 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 -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 - || s1max < (strlen(s1) + strlen(s2) + 1) - || s1max > RSIZE_MAX) { - return -1; - } + char *dest = (char *)s1; + char *src = (char *)s2; + if (n == 0) { + return 0; + } - memcpy(s1 + strlen(s1), s2, strlen(s2) + 1); - 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 -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 - || s1max < (strlen(s2) + 1) - || s1max > RSIZE_MAX) { - return -1; - } + if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) + || s1max > RSIZE_MAX) { + return -1; + } - memcpy(s1, s2, strlen(s2) + 1); - return 0; + memcpy(s1 + strlen(s1), s2, strlen(s2) + 1); + 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 * @@ -105,4 +102,3 @@ wa_strdup(const char *s) } return s1; } - diff --git a/core/shared/utils/bh_common.h b/core/shared/utils/bh_common.h index cd3bcdac..eaeec510 100644 --- a/core/shared/utils/bh_common.h +++ b/core/shared/utils/bh_common.h @@ -12,40 +12,50 @@ extern "C" { #endif -#define bh_memcpy_s(dest, dlen, src, slen) do { \ - int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \ - (void)_ret; \ - bh_assert (_ret == 0); \ - } while (0) +#define bh_memcpy_s(dest, dlen, src, slen) \ + do { \ + int _ret = slen == 0 ? 0 : b_memcpy_s(dest, dlen, src, slen); \ + (void)_ret; \ + bh_assert(_ret == 0); \ + } while (0) -#define bh_memmove_s(dest, dlen, src, slen) do { \ - int _ret = slen == 0 ? 0 : b_memmove_s (dest, dlen, src, slen); \ - (void)_ret; \ - bh_assert (_ret == 0); \ - } while (0) +#define bh_memmove_s(dest, dlen, src, slen) \ + do { \ + int _ret = slen == 0 ? 0 : b_memmove_s(dest, dlen, src, slen); \ + (void)_ret; \ + bh_assert(_ret == 0); \ + } while (0) -#define bh_strcat_s(dest, dlen, src) do { \ - int _ret = b_strcat_s (dest, dlen, src); \ - (void)_ret; \ - bh_assert (_ret == 0); \ - } while (0) +#define bh_strcat_s(dest, dlen, src) \ + do { \ + int _ret = b_strcat_s(dest, dlen, src); \ + (void)_ret; \ + bh_assert(_ret == 0); \ + } while (0) -#define bh_strcpy_s(dest, dlen, src) do { \ - int _ret = b_strcpy_s (dest, dlen, src); \ - (void)_ret; \ - bh_assert (_ret == 0); \ - } while (0) +#define bh_strcpy_s(dest, dlen, src) \ + do { \ + int _ret = b_strcpy_s(dest, dlen, src); \ + (void)_ret; \ + bh_assert(_ret == 0); \ + } while (0) -int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n); -int 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); +int +b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n); +int +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 */ -char *bh_strdup(const char *s); +char * +bh_strdup(const char *s); /* strdup with string allocated by WA_MALLOC */ -char *wa_strdup(const char *s); +char * +wa_strdup(const char *s); #ifdef __cplusplus } diff --git a/core/shared/utils/bh_hashmap.c b/core/shared/utils/bh_hashmap.c index 7ed03601..70119c77 100644 --- a/core/shared/utils/bh_hashmap.c +++ b/core/shared/utils/bh_hashmap.c @@ -25,11 +25,9 @@ struct HashMap { HashMapElem *elements[1]; }; -HashMap* -bh_hash_map_create(uint32 size, bool use_lock, - HashFunc hash_func, - KeyEqualFunc key_equal_func, - KeyDestroyFunc key_destroy_func, +HashMap * +bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func, + KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func, ValueDestroyFunc value_destroy_func) { HashMap *map; @@ -42,16 +40,15 @@ bh_hash_map_create(uint32 size, bool use_lock, if (!hash_func || !key_equal_func) { LOG_ERROR("HashMap create failed: hash function or key equal function " - " is NULL.\n"); + " is NULL.\n"); return NULL; } - total_size = offsetof(HashMap, elements) + - sizeof(HashMapElem *) * (uint64)size + - (use_lock ? sizeof(korp_mutex) : 0); + total_size = offsetof(HashMap, elements) + + sizeof(HashMapElem *) * (uint64)size + + (use_lock ? sizeof(korp_mutex) : 0); - if (total_size >= UINT32_MAX - || !(map = BH_MALLOC((uint32)total_size))) { + if (total_size >= UINT32_MAX || !(map = BH_MALLOC((uint32)total_size))) { LOG_ERROR("HashMap create failed: alloc memory failed.\n"); return NULL; } @@ -59,9 +56,8 @@ bh_hash_map_create(uint32 size, bool use_lock, memset(map, 0, (uint32)total_size); if (use_lock) { - map->lock = (korp_mutex*) - ((uint8*)map + offsetof(HashMap, elements) - + sizeof(HashMapElem *) * size); + map->lock = (korp_mutex *)((uint8 *)map + offsetof(HashMap, elements) + + sizeof(HashMapElem *) * size); if (os_mutex_init(map->lock)) { LOG_ERROR("HashMap create failed: init map lock failed.\n"); BH_FREE(map); @@ -124,7 +120,7 @@ fail: return false; } -void* +void * bh_hash_map_find(HashMap *map, void *key) { uint32 index; @@ -161,8 +157,7 @@ bh_hash_map_find(HashMap *map, void *key) } bool -bh_hash_map_update(HashMap *map, void *key, void *value, - void **p_old_value) +bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value) { uint32 index; HashMapElem *elem; @@ -199,8 +194,8 @@ bh_hash_map_update(HashMap *map, void *key, void *value, } bool -bh_hash_map_remove(HashMap *map, void *key, - void **p_old_key, void **p_old_value) +bh_hash_map_remove(HashMap *map, void *key, void **p_old_key, + void **p_old_value) { uint32 index; HashMapElem *elem, *prev; diff --git a/core/shared/utils/bh_hashmap.h b/core/shared/utils/bh_hashmap.h index de4eefcf..ca61ae41 100644 --- a/core/shared/utils/bh_hashmap.h +++ b/core/shared/utils/bh_hashmap.h @@ -51,11 +51,9 @@ typedef void (*TraverseCallbackFunc)(void *key, void *value, void *user_data); * * @return the hash map created, NULL if failed */ -HashMap* -bh_hash_map_create(uint32 size, bool use_lock, - HashFunc hash_func, - KeyEqualFunc key_equal_func, - KeyDestroyFunc key_destroy_func, +HashMap * +bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func, + KeyEqualFunc key_equal_func, KeyDestroyFunc key_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 */ -void* +void * 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. */ bool -bh_hash_map_update(HashMap *map, void *key, void *value, - void **p_old_value); +bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value); /** * 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. */ bool -bh_hash_map_remove(HashMap *map, void *key, - void **p_old_key, void **p_old_value); +bh_hash_map_remove(HashMap *map, void *key, void **p_old_key, + void **p_old_value); /** * Destroy the hashmap @@ -166,4 +163,3 @@ bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback, #endif #endif /* endof WASM_HASHMAP_H */ - diff --git a/core/shared/utils/bh_list.c b/core/shared/utils/bh_list.c index 79f46adc..7102d42a 100644 --- a/core/shared/utils/bh_list.c +++ b/core/shared/utils/bh_list.c @@ -14,10 +14,12 @@ * @return true if the pointer has been in the list; * false 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 -bh_list_status bh_list_init(bh_list *list) +bh_list_status +bh_list_init(bh_list *list) { if (!list) return BH_LIST_ERROR; @@ -27,23 +29,25 @@ bh_list_status bh_list_init(bh_list *list) 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; if (!list || !elem) return BH_LIST_ERROR; #if BH_DEBUG != 0 - bh_assert (!bh_list_is_elem_exist(list, elem)); + bh_assert(!bh_list_is_elem_exist(list, elem)); #endif - p = (bh_list_link *) elem; + p = (bh_list_link *)elem; p->next = (list->head).next; (list->head).next = p; list->len++; 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 *prev = NULL; @@ -71,32 +75,37 @@ bh_list_status bh_list_remove(bh_list *list, void *elem) return BH_LIST_ERROR; } -uint32 bh_list_length(bh_list *list) +uint32 +bh_list_length(bh_list *list) { 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); } -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 -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; - if (!list || !elem) return false; + if (!list || !elem) + return false; p = (list->head).next; - while (p && p != elem) p = p->next; + while (p && p != elem) + p = p->next; return (p != NULL); } #endif - diff --git a/core/shared/utils/bh_list.h b/core/shared/utils/bh_list.h index f47b9374..eaf15f2f 100644 --- a/core/shared/utils/bh_list.h +++ b/core/shared/utils/bh_list.h @@ -46,29 +46,34 @@ typedef enum bh_list_status { * @return BH_LIST_ERROR if OK; * BH_LIST_ERROR 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 - * elem memory is the responsibility of list user. + * Insert an elem pointer into list. The list node memory is maintained by list + * while elem memory is the responsibility of list user. * * @param list pointer to list. * @param elem pointer to elem that will be inserted into list. * @return BH_LIST_ERROR if OK; - * BH_LIST_ERROR if input is invalid or no memory available. + * BH_LIST_ERROR 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 - * elem memory is the responsibility of list user. + * Remove an elem pointer from list. The list node memory is maintained by list + * while elem memory is the responsibility of list user. * * @param list pointer to list. * @param elem pointer to elem that will be inserted into list. * @return BH_LIST_ERROR if OK; - * BH_LIST_ERROR if element does not exist in given list. + * BH_LIST_ERROR 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. @@ -76,7 +81,8 @@ bh_list_status bh_list_remove(bh_list *list, void *elem); * @param list pointer to 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. @@ -84,7 +90,8 @@ uint32 bh_list_length(bh_list *list); * @param list pointer to list. * @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. @@ -92,11 +99,11 @@ void* bh_list_first_elem(bh_list *list); * @param node pointer to list node. * @return pointer to next list node. */ -void* bh_list_elem_next(void *node); +void * +bh_list_elem_next(void *node); #ifdef __cplusplus } #endif #endif /* #ifndef _BH_LIST_H */ - diff --git a/core/shared/utils/bh_log.c b/core/shared/utils/bh_log.c index a3b392e8..04d3e98a 100644 --- a/core/shared/utils/bh_log.c +++ b/core/shared/utils/bh_log.c @@ -71,9 +71,8 @@ bh_print_time(const char *prompt) total_time_ms += curr_time_ms - last_time_ms; - os_printf("%-48s time of last stage: %u ms, total time: %u ms\n", - prompt, curr_time_ms - last_time_ms, total_time_ms); + os_printf("%-48s time of last stage: %u ms, total time: %u ms\n", prompt, + curr_time_ms - last_time_ms, total_time_ms); last_time_ms = curr_time_ms; } - diff --git a/core/shared/utils/bh_log.h b/core/shared/utils/bh_log.h index dd24dc04..1578028b 100644 --- a/core/shared/utils/bh_log.h +++ b/core/shared/utils/bh_log.h @@ -52,19 +52,22 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...); #endif #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 -#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 -#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_VERBOSE(...) bh_log(BH_LOG_LEVEL_VERBOSE, NULL, 0, __VA_ARGS__) #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 -#define LOG_DEBUG(...) (void)0 +#define LOG_DEBUG(...) (void)0 #endif void @@ -74,4 +77,4 @@ bh_print_time(const char *prompt); } #endif -#endif /* _BH_LOG_H */ +#endif /* _BH_LOG_H */ diff --git a/core/shared/utils/bh_platform.h b/core/shared/utils/bh_platform.h index 94b156f4..86aef839 100644 --- a/core/shared/utils/bh_platform.h +++ b/core/shared/utils/bh_platform.h @@ -18,8 +18,6 @@ #include "bh_vector.h" #include "runtime_timer.h" - - /** * WA_MALLOC/WA_FREE need to be redefined for both * runtime native and WASM app respectively. @@ -38,4 +36,3 @@ #endif #endif /* #ifndef _BH_PLATFORM_H */ - diff --git a/core/shared/utils/bh_queue.c b/core/shared/utils/bh_queue.c index 602c2cdf..7c860d11 100644 --- a/core/shared/utils/bh_queue.c +++ b/core/shared/utils/bh_queue.c @@ -6,11 +6,11 @@ #include "bh_queue.h" typedef struct bh_queue_node { - struct bh_queue_node * next; - struct bh_queue_node * prev; + struct bh_queue_node *next; + struct bh_queue_node *prev; unsigned short tag; unsigned int len; - void * body; + void *body; bh_msg_cleaner msg_cleaner; } bh_queue_node; @@ -20,23 +20,26 @@ struct bh_queue { unsigned int cnt; unsigned int max; unsigned int drops; - bh_queue_node * head; - bh_queue_node * tail; + bh_queue_node *head; + bh_queue_node *tail; bool exit_loop_run; }; -char * bh_message_payload(bh_message_t message) +char * +bh_message_payload(bh_message_t message) { return message->body; } -uint32 bh_message_payload_len(bh_message_t message) +uint32 +bh_message_payload_len(bh_message_t message) { return message->len; } -int bh_message_type(bh_message_t message) +int +bh_message_type(bh_message_t message) { return message->tag; } @@ -68,7 +71,8 @@ bh_queue_create() return queue; } -void bh_queue_destroy(bh_queue *queue) +void +bh_queue_destroy(bh_queue *queue) { bh_queue_node *node; @@ -89,7 +93,8 @@ void bh_queue_destroy(bh_queue *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) { queue->drops++; @@ -107,7 +112,8 @@ bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg) queue->cnt = 1; bh_queue_cond_signal(&queue->queue_wait_cond); - } else { + } + else { msg->next = NULL; msg->prev = queue->tail; queue->tail->next = msg; @@ -120,8 +126,8 @@ bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg) return true; } -bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body, - unsigned int len) +bool +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); if (msg == NULL) { @@ -139,23 +145,24 @@ bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body, return true; } -bh_queue_node * bh_new_msg(unsigned short tag, void *body, unsigned int len, - void * handler) +bh_queue_node * +bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler) { - bh_queue_node *msg = (bh_queue_node*) - bh_queue_malloc(sizeof(bh_queue_node)); + bh_queue_node *msg = + (bh_queue_node *)bh_queue_malloc(sizeof(bh_queue_node)); if (msg == NULL) return NULL; memset(msg, 0, sizeof(bh_queue_node)); msg->len = len; msg->body = body; msg->tag = tag; - msg->msg_cleaner = (bh_msg_cleaner) handler; + msg->msg_cleaner = (bh_msg_cleaner)handler; return msg; } -void bh_free_msg(bh_queue_node *msg) +void +bh_free_msg(bh_queue_node *msg) { if (msg->msg_cleaner) { msg->msg_cleaner(msg->body); @@ -171,7 +178,8 @@ void bh_free_msg(bh_queue_node *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_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) { bh_assert(queue->head == NULL); bh_assert(queue->tail == NULL); - } else if (queue->cnt == 1) { + } + else if (queue->cnt == 1) { bh_assert(queue->head == queue->tail); msg = queue->head; queue->head = queue->tail = NULL; queue->cnt = 0; - } else { + } + else { msg = queue->head; queue->head = queue->head->next; queue->head->prev = NULL; @@ -210,7 +220,8 @@ bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us) return msg; } -unsigned bh_queue_get_message_count(bh_queue *queue) +unsigned +bh_queue_get_message_count(bh_queue *queue) { if (!queue) return 0; @@ -218,15 +229,15 @@ unsigned bh_queue_get_message_count(bh_queue *queue) return queue->cnt; } -void bh_queue_enter_loop_run(bh_queue *queue, - bh_queue_handle_msg_callback handle_cb, - void *arg) +void +bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb, + void *arg) { if (!queue) return; 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) { 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) { queue->exit_loop_run = true; diff --git a/core/shared/utils/bh_queue.h b/core/shared/utils/bh_queue.h index 09d1a742..394fed9e 100644 --- a/core/shared/utils/bh_queue.h +++ b/core/shared/utils/bh_queue.h @@ -13,7 +13,7 @@ extern "C" { #include "bh_platform.h" struct bh_queue_node; -typedef struct bh_queue_node * bh_message_t; +typedef struct bh_queue_node *bh_message_t; struct bh_queue; typedef struct bh_queue bh_queue; @@ -45,25 +45,30 @@ bh_queue_create(); void bh_queue_destroy(bh_queue *queue); -char * bh_message_payload(bh_message_t message); -uint32 bh_message_payload_len(bh_message_t message); -int bh_message_type(bh_message_t message); +char * +bh_message_payload(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, - void * handler); -void bh_free_msg(bh_message_t msg); -bool 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_new_msg(unsigned short tag, void *body, unsigned int len, void *handler); +void +bh_free_msg(bh_message_t msg); +bool +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 bh_queue_get_message_count(bh_queue *queue); void -bh_queue_enter_loop_run(bh_queue *queue, - 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 bh_queue_exit_loop_run(bh_queue *queue); @@ -73,4 +78,3 @@ bh_queue_exit_loop_run(bh_queue *queue); #endif #endif /* #ifndef _BH_QUEUE_H */ - diff --git a/core/shared/utils/bh_vector.c b/core/shared/utils/bh_vector.c index 33ea7265..426c0242 100644 --- a/core/shared/utils/bh_vector.c +++ b/core/shared/utils/bh_vector.c @@ -5,14 +5,13 @@ #include "bh_vector.h" -static uint8* +static uint8 * alloc_vector_data(size_t length, size_t size_elem) { uint64 total_size = ((uint64)size_elem) * length; uint8 *data; - if (length > UINT32_MAX - || size_elem > UINT32_MAX + if (length > UINT32_MAX || size_elem > UINT32_MAX || total_size > UINT32_MAX) { return NULL; } @@ -82,12 +81,13 @@ bh_vector_set(Vector *vector, uint32 index, const void *elem_buf) return false; } - memcpy(vector->data + vector->size_elem * index, - elem_buf, vector->size_elem); + memcpy(vector->data + vector->size_elem * index, elem_buf, + vector->size_elem); 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) { 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; } -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; uint8 *p; @@ -135,7 +136,8 @@ bool bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf) 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) { 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; } - memcpy(vector->data + vector->size_elem * vector->num_elems, - elem_buf, vector->size_elem); + memcpy(vector->data + vector->size_elem * vector->num_elems, elem_buf, + vector->size_elem); vector->num_elems++; return true; } diff --git a/core/shared/utils/bh_vector.h b/core/shared/utils/bh_vector.h index 59f432f7..072b1cb0 100644 --- a/core/shared/utils/bh_vector.h +++ b/core/shared/utils/bh_vector.h @@ -122,4 +122,3 @@ bh_vector_destroy(Vector *vector); #endif #endif /* endof _WASM_VECTOR_H */ - diff --git a/core/shared/utils/runtime_timer.c b/core/shared/utils/runtime_timer.c index 205b92ab..8fccf4c2 100644 --- a/core/shared/utils/runtime_timer.c +++ b/core/shared/utils/runtime_timer.c @@ -12,7 +12,7 @@ #endif typedef struct _app_timer { - struct _app_timer * next; + struct _app_timer *next; uint32 id; uint32 interval; 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 (prev == NULL) { *head = t->next; - PRINT("removed timer [%d] at head from list %d\n", - t->id, active_list); + PRINT("removed timer [%d] at head from list %d\n", t->id, + active_list); } else { prev->next = t->next; - PRINT("removed timer [%d] after [%d] from list %d\n", - t->id, prev->id, active_list); + PRINT("removed timer [%d] after [%d] from list %d\n", t->id, + prev->id, active_list); } os_mutex_unlock(&ctx->mutex); @@ -143,8 +143,8 @@ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer) else { timer->next = t; prev->next = timer; - PRINT("rescheduled timer [%d] after [%d]\n", - timer->id, prev->id); + PRINT("rescheduled timer [%d] after [%d]\n", timer->id, + prev->id); } goto out; @@ -158,8 +158,8 @@ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer) if (prev) { /* insert to the list end */ prev->next = timer; - PRINT("rescheduled timer [%d] at end, after [%d]\n", - timer->id, prev->id); + PRINT("rescheduled timer [%d] at end, after [%d]\n", timer->id, + prev->id); } else { /* insert at the begin */ @@ -213,8 +213,8 @@ release_timer_list(app_timer_t **p_list) timer_ctx_t create_timer_ctx(timer_callback_f timer_handler, - check_timer_expiry_f expiery_checker, - int prealloc_num, unsigned int owner) + check_timer_expiry_f expiery_checker, int prealloc_num, + unsigned int owner) { 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; 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) goto cleanup; @@ -283,7 +283,7 @@ timer_ctx_get_owner(timer_ctx_t ctx) } 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); timer->next = ctx->idle_timers; @@ -292,8 +292,7 @@ add_idle_timer(timer_ctx_t ctx, app_timer_t * timer) } uint32 -sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, - bool auto_start) +sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, bool auto_start) { app_timer_t *timer; @@ -307,7 +306,7 @@ sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, } } else { - timer = (app_timer_t*)BH_MALLOC(sizeof(app_timer_t)); + timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t)); if (timer == NULL) return (uint32)-1; } @@ -333,7 +332,7 @@ bool sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id) { 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) return false; @@ -348,7 +347,7 @@ bool sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id) { 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) return false; @@ -362,7 +361,7 @@ sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id) bool 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) 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); } - diff --git a/core/shared/utils/runtime_timer.h b/core/shared/utils/runtime_timer.h index db81d77b..00db2983 100644 --- a/core/shared/utils/runtime_timer.h +++ b/core/shared/utils/runtime_timer.h @@ -12,28 +12,38 @@ extern "C" { #endif -uint64 bh_get_tick_ms(); -uint32 bh_get_elpased_ms(uint32 *last_system_clock); +uint64 +bh_get_tick_ms(); +uint32 +bh_get_elpased_ms(uint32 *last_system_clock); 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 (*check_timer_expiry_f)(timer_ctx_t ctx); -timer_ctx_t create_timer_ctx(timer_callback_f timer_handler, - check_timer_expiry_f, int prealloc_num, - unsigned int owner); +timer_ctx_t +create_timer_ctx(timer_callback_f timer_handler, check_timer_expiry_f, + int prealloc_num, unsigned int owner); 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, - bool auto_start); -bool sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id); -bool sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id); -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); +uint32 +sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, + bool auto_start); +bool +sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id); +bool +sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id); +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 } diff --git a/core/shared/utils/uncommon/bh_getopt.c b/core/shared/utils/uncommon/bh_getopt.c index 67815e19..19e23a7b 100644 --- a/core/shared/utils/uncommon/bh_getopt.c +++ b/core/shared/utils/uncommon/bh_getopt.c @@ -9,19 +9,22 @@ #include #include -char* optarg = NULL; +char *optarg = NULL; 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; int opt; char *p; 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; - } else if (!strcmp(argv[optind], "--")) { + } + else if (!strcmp(argv[optind], "--")) { optind++; return -1; } @@ -31,24 +34,26 @@ int getopt(int argc, char *const argv[], const char *optstring) p = strchr(optstring, opt); if (opt == ':' || p == NULL) { printf("illegal option : '-%c'\n", opt); - if ( argv[optind][++sp] == '\0') { - optind ++; + if (argv[optind][++sp] == '\0') { + optind++; sp = 1; } return ('?'); } - if (p[1] == ':') { + if (p[1] == ':') { if (argv[optind][sp + 1] != '\0') optarg = &argv[optind++][sp + 1]; else if (++optind >= argc) { printf("option '-%c' requires an argument :\n", opt); sp = 1; return ('?'); - } else { + } + else { optarg = argv[optind++]; } sp = 1; - } else { + } + else { if (argv[optind][++sp] == '\0') { sp = 1; optind++; diff --git a/core/shared/utils/uncommon/bh_getopt.h b/core/shared/utils/uncommon/bh_getopt.h index e6243bfd..efd3ab40 100644 --- a/core/shared/utils/uncommon/bh_getopt.h +++ b/core/shared/utils/uncommon/bh_getopt.h @@ -17,7 +17,8 @@ extern "C" { extern char *optarg; 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 } diff --git a/core/shared/utils/uncommon/bh_read_file.c b/core/shared/utils/uncommon/bh_read_file.c index 5513fe52..e17f1de3 100644 --- a/core/shared/utils/uncommon/bh_read_file.c +++ b/core/shared/utils/uncommon/bh_read_file.c @@ -9,7 +9,7 @@ #endif #if defined(_WIN32) || defined(_WIN32_) -char* +char * bh_read_file_to_buffer(const char *filename, uint32 *ret_size) { char *buffer; @@ -22,15 +22,13 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size) return NULL; } - if (_sopen_s(&file, filename, _O_RDONLY| _O_BINARY, _SH_DENYNO, 0)) { - printf("Read file to buffer failed: open file %s failed.\n", - filename); + if (_sopen_s(&file, filename, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0)) { + printf("Read file to buffer failed: open file %s failed.\n", filename); return NULL; } if (fstat(file, &stat_buf) != 0) { - printf("Read file to buffer failed: fstat file %s failed.\n", - filename); + printf("Read file to buffer failed: fstat file %s failed.\n", filename); _close(file); return NULL; } @@ -61,7 +59,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size) return buffer; } #else /* else of defined(_WIN32) || defined(_WIN32_) */ -char* +char * bh_read_file_to_buffer(const char *filename, uint32 *ret_size) { 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) { - printf("Read file to buffer failed: open file %s failed.\n", - filename); + printf("Read file to buffer failed: open file %s failed.\n", filename); return NULL; } if (fstat(file, &stat_buf) != 0) { - printf("Read file to buffer failed: fstat file %s failed.\n", - filename); + printf("Read file to buffer failed: fstat file %s failed.\n", filename); close(file); return NULL; } diff --git a/core/shared/utils/uncommon/bh_read_file.h b/core/shared/utils/uncommon/bh_read_file.h index 7c947d62..bbebf847 100644 --- a/core/shared/utils/uncommon/bh_read_file.h +++ b/core/shared/utils/uncommon/bh_read_file.h @@ -20,4 +20,3 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size); #endif #endif /* end of _BH_FILE_H */ - diff --git a/product-mini/app-samples/hello-world-cmake/main.c b/product-mini/app-samples/hello-world-cmake/main.c index 1c356c47..b6dfd280 100644 --- a/product-mini/app-samples/hello-world-cmake/main.c +++ b/product-mini/app-samples/hello-world-cmake/main.c @@ -5,9 +5,11 @@ #include "stdio.h" -void print_line(char* str); +void +print_line(char *str); -int main() +int +main() { print_line("Hello World!"); print_line("Wasm Micro Runtime"); diff --git a/product-mini/app-samples/hello-world-cmake/print.c b/product-mini/app-samples/hello-world-cmake/print.c index 8eee6b6b..d98a8266 100644 --- a/product-mini/app-samples/hello-world-cmake/print.c +++ b/product-mini/app-samples/hello-world-cmake/print.c @@ -6,7 +6,8 @@ #include "stdio.h" #include "string.h" -void print_line(char* str) +void +print_line(char *str) { printf("%s\n", str); } \ No newline at end of file diff --git a/product-mini/app-samples/hello-world/main.c b/product-mini/app-samples/hello-world/main.c index d392fb1f..4778a4d9 100644 --- a/product-mini/app-samples/hello-world/main.c +++ b/product-mini/app-samples/hello-world/main.c @@ -6,7 +6,8 @@ #include #include -int main(int argc, char **argv) +int +main(int argc, char **argv) { char *buf; diff --git a/product-mini/platforms/alios-things/src/main.c b/product-mini/platforms/alios-things/src/main.c index 1c148432..6393e2e4 100644 --- a/product-mini/platforms/alios-things/src/main.c +++ b/product-mini/platforms/alios-things/src/main.c @@ -24,10 +24,10 @@ static char **app_argv; * @return true if the main function is called, false otherwise. */ bool -wasm_application_execute_main(wasm_module_inst_t module_inst, - int32_t argc, char *argv[]); +wasm_application_execute_main(wasm_module_inst_t module_inst, int32_t argc, + char *argv[]); -static void* +static void * app_instance_main(wasm_module_inst_t module_inst) { const char *exception; @@ -40,7 +40,8 @@ app_instance_main(wasm_module_inst_t module_inst) static char global_heap_buf[256 * 1024] = { 0 }; -void iwasm_main(void *arg1) +void +iwasm_main(void *arg1) { uint8 *wasm_file_buf = NULL; uint32 wasm_file_size; @@ -52,7 +53,7 @@ void iwasm_main(void *arg1) int log_verbose_level = 2; #endif - (void) arg1; + (void)arg1; memset(&init_args, 0, sizeof(RuntimeInitArgs)); @@ -71,22 +72,19 @@ void iwasm_main(void *arg1) #endif /* 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); /* load WASM module */ 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); goto fail1; } /* instantiate the module */ - if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, - 8 * 1024, - 8 * 1024, - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst = wasm_runtime_instantiate( + wasm_module, 8 * 1024, 8 * 1024, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail2; } @@ -108,10 +106,10 @@ fail1: #define DEFAULT_THREAD_STACKSIZE (6 * 1024) #define DEFAULT_THREAD_PRIORITY 50 -bool iwasm_init(void) +bool +iwasm_init(void) { - int ret = aos_task_new("wasm-main", iwasm_main, NULL, - DEFAULT_THREAD_STACKSIZE); + int ret = + aos_task_new("wasm-main", iwasm_main, NULL, DEFAULT_THREAD_STACKSIZE); return ret == 0 ? true : false; } - diff --git a/product-mini/platforms/android/wasm-jni.cpp b/product-mini/platforms/android/wasm-jni.cpp index 29417542..48ce6186 100644 --- a/product-mini/platforms/android/wasm-jni.cpp +++ b/product-mini/platforms/android/wasm-jni.cpp @@ -7,10 +7,11 @@ #include #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 * -app_instance_main(wasm_module_inst_t module_inst) { +app_instance_main(wasm_module_inst_t module_inst) +{ const char *exception; wasm_application_execute_main(module_inst, 0, NULL); @@ -58,20 +59,21 @@ static unsigned char wasm_test_file[] = { }; 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_inst_t wasm_module_inst = NULL; RuntimeInitArgs init_args; uint wasm_file_size = 0; uint8_t *wasm_file_buf = NULL; - char error_buf[128] = {0}; + char error_buf[128] = { 0 }; memset(&init_args, 0, sizeof(RuntimeInitArgs)); init_args.mem_alloc_type = Alloc_With_Allocator; - 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.free_func = (void*)free; + 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.free_func = (void *)free; LOGI("wasm_runtime_full_init"); /* 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 */ 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); /* load WASM module */ @@ -96,11 +98,10 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) { /* instantiate the module */ LOGI("wasm_runtime_instantiate"); - if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, - 64 * 1024, /* stack size */ - 64 * 1024, /* heap size */ - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst = + wasm_runtime_instantiate(wasm_module, 64 * 1024, /* stack size */ + 64 * 1024, /* heap size */ + error_buf, sizeof(error_buf)))) { LOGI("%s\n", error_buf); LOGI("goto fail2\n"); goto fail2; @@ -121,7 +122,7 @@ fail2: fail1: // in our case, we don't need a free, but it is not a typical one /* free the file buffer */ - //bh_free((void *) wasm_file_buf); + // bh_free((void *) wasm_file_buf); /* destroy runtime environment */ LOGI("wasm_runtime_destroy"); diff --git a/product-mini/platforms/esp-idf/iwasm_main.c b/product-mini/platforms/esp-idf/iwasm_main.c index fd0d0a9a..4f0d922e 100644 --- a/product-mini/platforms/esp-idf/iwasm_main.c +++ b/product-mini/platforms/esp-idf/iwasm_main.c @@ -14,7 +14,7 @@ static int app_argc; static char **app_argv; -static void* +static void * app_instance_main(wasm_module_inst_t module_inst) { 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 }; -void iwasm_main(void) +void +iwasm_main(void) { uint8 *wasm_file_buf = NULL; uint32 wasm_file_size; @@ -60,7 +61,7 @@ void iwasm_main(void) #endif /* 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); /* load WASM module */ @@ -73,11 +74,9 @@ void iwasm_main(void) os_printf("### wasm runtime load module success.\n"); /* instantiate the module */ - if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, - CONFIG_APP_STACK_SIZE, - CONFIG_APP_HEAP_SIZE, - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst = wasm_runtime_instantiate( + wasm_module, CONFIG_APP_STACK_SIZE, CONFIG_APP_HEAP_SIZE, + error_buf, sizeof(error_buf)))) { os_printf("%s\n", error_buf); goto fail2; } diff --git a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp index 14b8c415..bd6b0e36 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp @@ -26,7 +26,7 @@ #define FALSE 0 #endif -#define TOKEN_FILENAME "enclave.token" +#define TOKEN_FILENAME "enclave.token" #define ENCLAVE_FILENAME "enclave.signed.so" #define MAX_PATH 1024 @@ -41,7 +41,7 @@ pal_get_enclave_id(void) } void -ocall_print(const char* str) +ocall_print(const char *str) { printf("%s", str); } @@ -50,8 +50,7 @@ static char * get_exe_path(char *path_buf, unsigned path_buf_size) { ssize_t i; - ssize_t size = readlink("/proc/self/exe", - path_buf, path_buf_size - 1); + ssize_t size = readlink("/proc/self/exe", path_buf, path_buf_size - 1); if (size < 0 || (size >= path_buf_size - 1)) { return NULL; @@ -99,8 +98,9 @@ enclave_init(sgx_enclave_id_t *p_eid) home_dir = getpwuid(getuid())->pw_dir; size_t home_dir_len = home_dir ? strlen(home_dir) : 0; - if (home_dir != NULL && - home_dir_len <= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) { + if (home_dir != NULL + && home_dir_len + <= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) { /* compose the token path */ strncpy(token_path, home_dir, MAX_PATH); 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 token is invalid, clear the buffer */ 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 */ /* Debug Support: set 2nd parameter to 1 */ - ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, - &token, &updated, p_eid, NULL); + ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated, + p_eid, NULL); if (ret != SGX_SUCCESS) /* Try to load enclave.sign.so from the path of exe file */ - ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, - &token, &updated, p_eid, NULL); + ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated, + p_eid, NULL); if (ret != SGX_SUCCESS) { printf("Failed to create enclave from %s, error code: %d\n", 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 */ if (updated == FALSE || fp == NULL) { - /* if the token is not updated, or file handler is invalid, do not perform saving */ - if (fp != NULL) fclose(fp); + /* if the token is not updated, or file handler is invalid, + do not perform saving */ + if (fp != NULL) + fclose(fp); return 0; } @@ -176,8 +179,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size) } if (!(file = fopen(filename, "r"))) { - printf("Read file to buffer failed: open file %s failed.\n", - filename); + printf("Read file to buffer failed: open file %s failed.\n", filename); return NULL; } @@ -185,7 +187,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size) file_size = ftell(file); 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"); fclose(file); return NULL; @@ -205,6 +207,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size) return buffer; } +/* clang-format off */ static int print_help() { @@ -227,6 +230,7 @@ print_help() printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n"); return 1; } +/* clang-format on */ /** * Split a space separated strings into an array of strings @@ -287,8 +291,8 @@ typedef enum EcallCmd { } EcallCmd; static void -app_instance_func(void *wasm_module_inst, const char *func_name, - int app_argc, char **app_argv); +app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc, + char **app_argv); static void * 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; } if (app_argc != 0) { - app_instance_func(module_inst, app_argv[0], - app_argc - 1, app_argv + 1); + app_instance_func(module_inst, app_argv[0], app_argc - 1, + app_argv + 1); } free(app_argv); } @@ -349,9 +353,9 @@ set_log_verbose_level(int log_verbose_level) /* Set log verbose level */ if (log_verbose_level != 2) { ecall_args[0] = log_verbose_level; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL, - (uint8_t *)ecall_args, - sizeof(uint64_t))) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL, + (uint8_t *)ecall_args, sizeof(uint64_t))) { printf("Call ecall_handle_command() failed.\n"); 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[1] = max_thread_num; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INIT_RUNTIME, - (uint8_t *)ecall_args, - sizeof(uint64_t) * 2)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_INIT_RUNTIME, (uint8_t *)ecall_args, + sizeof(uint64_t) * 2)) { printf("Call ecall_handle_command() failed.\n"); return false; } @@ -382,15 +386,15 @@ init_runtime(bool alloc_with_pool, uint32_t max_thread_num) static void destroy_runtime() { - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, - NULL, 0)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, NULL, 0)) { printf("Call ecall_handle_command() failed.\n"); } } static void * -load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, - char *error_buf, uint32_t error_buf_size) +load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, char *error_buf, + uint32_t error_buf_size) { 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[2] = (uint64_t)(uintptr_t)error_buf; ecall_args[3] = error_buf_size; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_LOAD_MODULE, - (uint8_t *)ecall_args, - sizeof(uint64_t) * 4)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_LOAD_MODULE, (uint8_t *)ecall_args, + sizeof(uint64_t) * 4)) { printf("Call ecall_handle_command() failed.\n"); return NULL; } @@ -414,16 +418,15 @@ unload_module(void *wasm_module) uint64_t ecall_args[1]; ecall_args[0] = (uint64_t)(uintptr_t)wasm_module; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, - (uint8_t *)ecall_args, - sizeof(uint64_t))) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, (uint8_t *)ecall_args, + sizeof(uint64_t))) { printf("Call ecall_handle_command() failed.\n"); } } static void * -instantiate_module(void *wasm_module, - uint32_t stack_size, uint32_t heap_size, +instantiate_module(void *wasm_module, uint32_t stack_size, uint32_t heap_size, char *error_buf, uint32_t error_buf_size) { uint64_t ecall_args[5]; @@ -433,9 +436,9 @@ instantiate_module(void *wasm_module, ecall_args[2] = heap_size; ecall_args[3] = (uint64_t)(uintptr_t)error_buf; ecall_args[4] = error_buf_size; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE, - (uint8_t *)ecall_args, - sizeof(uint64_t) * 5)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE, + (uint8_t *)ecall_args, sizeof(uint64_t) * 5)) { printf("Call ecall_handle_command() failed.\n"); return NULL; } @@ -449,25 +452,24 @@ deinstantiate_module(void *wasm_module_inst) uint64_t ecall_args[1]; ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE, - (uint8_t *)ecall_args, - sizeof(uint64_t))) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE, + (uint8_t *)ecall_args, sizeof(uint64_t))) { printf("Call ecall_handle_command() failed.\n"); } } static bool -get_exception(void *wasm_module_inst, - char *exception, uint32_t exception_size) +get_exception(void *wasm_module_inst, char *exception, uint32_t exception_size) { uint64_t ecall_args[3]; ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst; ecall_args[1] = (uint64_t)(uintptr_t)exception; ecall_args[2] = exception_size; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_GET_EXCEPTION, - (uint8_t *)ecall_args, - sizeof(uint64_t) * 3)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_GET_EXCEPTION, (uint8_t *)ecall_args, + sizeof(uint64_t) * 3)) { printf("Call ecall_handle_command() failed.\n"); } @@ -475,16 +477,15 @@ get_exception(void *wasm_module_inst, } static void -app_instance_main(void *wasm_module_inst, - int app_argc, char **app_argv) +app_instance_main(void *wasm_module_inst, int app_argc, char **app_argv) { char exception[128]; uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf; int i, size; if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) { - if (!(ecall_args = (uint64_t *) - malloc(sizeof(uint64_t) * (app_argc + 2)))) { + if (!(ecall_args = + (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 2)))) { printf("Allocate memory failed.\n"); return; } @@ -497,9 +498,9 @@ app_instance_main(void *wasm_module_inst, } size = (uint32_t)sizeof(uint64_t) * (app_argc + 2); - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, - (uint8_t *)ecall_args, - size)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, (uint8_t *)ecall_args, + size)) { printf("Call ecall_handle_command() failed.\n"); } @@ -513,15 +514,15 @@ app_instance_main(void *wasm_module_inst, } static void -app_instance_func(void *wasm_module_inst, const char *func_name, - int app_argc, char **app_argv) +app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc, + char **app_argv) { uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf; int i, size; if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) { - if (!(ecall_args = (uint64_t *) - malloc(sizeof(uint64_t) * (app_argc + 3)))) { + if (!(ecall_args = + (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 3)))) { printf("Allocate memory failed.\n"); 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); - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, - (uint8_t *)ecall_args, - size)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, (uint8_t *)ecall_args, + size)) { printf("Call ecall_handle_command() failed.\n"); } @@ -547,11 +548,9 @@ app_instance_func(void *wasm_module_inst, const char *func_name, } static bool -set_wasi_args(void *wasm_module, - const char **dir_list, uint32_t dir_list_size, - const char **env_list, uint32_t env_list_size, - int stdinfd, int stdoutfd, int stderrfd, - char **argv, uint32_t argc) +set_wasi_args(void *wasm_module, const char **dir_list, uint32_t dir_list_size, + const char **env_list, uint32_t env_list_size, int stdinfd, + int stdoutfd, int stderrfd, char **argv, uint32_t argc) { uint64_t ecall_args[10]; @@ -565,9 +564,9 @@ set_wasi_args(void *wasm_module, ecall_args[7] = stderrfd; ecall_args[8] = (uint64_t)(uintptr_t)argv; ecall_args[9] = argc; - if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, - (uint8_t *)ecall_args, - sizeof(uint64_t) * 10)) { + if (SGX_SUCCESS + != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, (uint8_t *)ecall_args, + sizeof(uint64_t) * 10)) { printf("Call ecall_handle_command() failed.\n"); } @@ -693,29 +692,28 @@ main(int argc, char *argv[]) /* Load WASM byte buffer from WASM bin file */ 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; } /* Load module */ - if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, - error_buf, sizeof(error_buf)))) { + if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, error_buf, + sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail2; } /* Set wasi arguments */ - if (!set_wasi_args(wasm_module, dir_list, dir_list_size, - env_list, env_list_size, 0, 1, 2, argv, argc)) { + if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list, + env_list_size, 0, 1, 2, argv, argc)) { printf("%s\n", "set wasi arguments failed.\n"); goto fail3; } /* Instantiate module */ - if (!(wasm_module_inst = instantiate_module(wasm_module, - stack_size, heap_size, - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst = + instantiate_module(wasm_module, stack_size, heap_size, error_buf, + sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail3; } @@ -723,8 +721,7 @@ main(int argc, char *argv[]) if (is_repl_mode) app_instance_repl(wasm_module_inst, argc, argv); else if (func_name) - app_instance_func(wasm_module_inst, func_name, - argc - 1, argv + 1); + app_instance_func(wasm_module_inst, func_name, argc - 1, argv + 1); else 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 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; void *wasm_modules = NULL; int len = 0, i; char *temp = (char *)args->argv[0]; - while(temp) { + while (temp) { 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"); return -1; } @@ -829,8 +826,8 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) char error_buf[128] = { 0 }; /* Load WASM byte buffer from WASM bin file */ - if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer - (wasm_files[i], &wasm_file_size))) { + if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer( + wasm_files[i], &wasm_file_size))) { printf("Failed to read file to buffer\n"); destroy_runtime(); return -1; @@ -846,10 +843,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) } /* Set wasi arguments */ - if (!set_wasi_args(wasm_module, dir_list, dir_list_size, - env_list, env_list_size, - stdinfd, stdoutfd, stderrfd, - argv, argc)) { + if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list, + env_list_size, stdinfd, stdoutfd, stderrfd, argv, + argc)) { printf("%s\n", "set wasi arguments failed.\n"); unload_module(wasm_module); free(wasm_file_buf); @@ -858,10 +854,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) } /* Instantiate module */ - if (!(wasm_module_inst[i] = instantiate_module(wasm_module, - stack_size, heap_size, - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst[i] = + instantiate_module(wasm_module, stack_size, heap_size, + error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); unload_module(wasm_module); free(wasm_file_buf); @@ -884,37 +879,43 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) int wamr_pal_destroy(void) { - //sgx_destroy_enclave(g_eid); + // sgx_destroy_enclave(g_eid); return 0; } int 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; } int wamr_pal_kill(int pid, int sig) { - //deinstantiate_module(wasm_module_inst[i]); - //unload_module(wasm_module); - //free(wasm_file_buf); + // deinstantiate_module(wasm_module_inst[i]); + // unload_module(wasm_module); + // free(wasm_file_buf); 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)\ -__attribute__ ((weak, alias ("wamr_pal_init"))); +int +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)\ -__attribute__ ((weak, alias ("wamr_pal_create_process"))); +int +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)\ -__attribute__ ((weak, alias ("wamr_pal_exec"))); +int +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"))); diff --git a/product-mini/platforms/linux-sgx/enclave-sample/App/pal_api.h b/product-mini/platforms/linux-sgx/enclave-sample/App/pal_api.h index 55668f30..6f6ae732 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/App/pal_api.h +++ b/product-mini/platforms/linux-sgx/enclave-sample/App/pal_api.h @@ -20,7 +20,8 @@ extern "C" { * * @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 @@ -29,19 +30,21 @@ typedef struct wamr_pal_attr { // WAMR instance directory. // // The default value is "."; that is, the current working directory - const char *instance_dir; + const char *instance_dir; // Log level. // // Specifies the log verbose level (0 to 5, default is 2) // large level with more log // - const char *log_level; + const char *log_level; } wamr_pal_attr_t; +/* clang-format off */ #define WAMR_PAL_ATTR_INITVAL { \ .instance_dir = ".", \ .log_level = 2 \ } +/* clang-format on */ /* * 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. // - // The arguments to the command. By convention, the argv[0] should be the program name. - // And the last element of the array must be NULL to indicate the length of array. + // The arguments to the command. By convention, the argv[0] should be the + // program name. And the last element of the array must be NULL to indicate + // the length of array. // // Mandatory field. Must not be NULL. const char **argv; // Untrusted environment variable array pass to new process. // - // The untrusted env vars to the command. And the last element of the array must be - // NULL to indicate the length of array. + // The untrusted env vars to the command. And the last element of the array + // must be NULL to indicate the length of array. // // Optional field. 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. // @@ -115,32 +120,41 @@ struct wamr_pal_exec_args { 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_destroy(void); +int +pal_kill(int pid, int sig); +int +pal_destroy(void); #ifdef __cplusplus } #endif #endif /* __WAMR_PAL_API_H__ */ - diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp index 15ce6c29..b9fafe45 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -13,14 +13,15 @@ #include "bh_platform.h" extern "C" { - typedef void (*os_print_function_t)(const char* message); - extern void os_set_print_function(os_print_function_t pf); +typedef void (*os_print_function_t)(const char *message); +extern void +os_set_print_function(os_print_function_t pf); - void - enclave_print(const char *message) - { - ocall_print(message); - } +void +enclave_print(const char *message) +{ + ocall_print(message); +} } typedef enum EcallCmd { @@ -130,8 +131,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc) bh_assert(argc == 4); if (total_size >= UINT32_MAX - || !(enclave_module = (EnclaveModule *) - wasm_runtime_malloc((uint32)total_size))) { + || !(enclave_module = + (EnclaveModule *)wasm_runtime_malloc((uint32)total_size))) { set_error_buf(error_buf, error_buf_size, "WASM module load failed: " "allocate memory failed."); @@ -140,14 +141,13 @@ handle_cmd_load_module(uint64 *args, uint32 argc) } memset(enclave_module, 0, (uint32)total_size); - enclave_module->wasm_file = (uint8 *)enclave_module - + sizeof(EnclaveModule); - bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, - wasm_file, wasm_file_size); + enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule); + bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file, + wasm_file_size); if (!(enclave_module->module = - wasm_runtime_load(enclave_module->wasm_file, wasm_file_size, - error_buf, error_buf_size))) { + wasm_runtime_load(enclave_module->wasm_file, wasm_file_size, + error_buf, error_buf_size))) { wasm_runtime_free(enclave_module); *(void **)args_org = NULL; return; @@ -189,9 +189,8 @@ handle_cmd_instantiate_module(uint64 *args, uint32 argc) bh_assert(argc == 5); if (!(module_inst = - wasm_runtime_instantiate(enclave_module->module, - stack_size, heap_size, - error_buf, error_buf_size))) { + wasm_runtime_instantiate(enclave_module->module, stack_size, + heap_size, error_buf, error_buf_size))) { *(void **)args_org = NULL; return; } @@ -225,8 +224,7 @@ handle_cmd_get_exception(uint64 *args, uint32 argc) bh_assert(argc == 3); if ((exception1 = wasm_runtime_get_exception(module_inst))) { - snprintf(exception, exception_size, - "%s", exception1); + snprintf(exception, exception_size, "%s", exception1); args_org[0] = true; } else { @@ -339,15 +337,14 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc) } if (total_size >= UINT32_MAX - || !(enclave_module->wasi_arg_buf = p = (char *) - wasm_runtime_malloc((uint32)total_size))) { + || !(enclave_module->wasi_arg_buf = p = + (char *)wasm_runtime_malloc((uint32)total_size))) { *args_org = false; return; } - p1 = p + sizeof(char *) * dir_list_size - + sizeof(char *) * env_list_size - + sizeof(char *) * wasi_argc; + p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size + + sizeof(char *) * wasi_argc; if (dir_list_size > 0) { 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; } - wasm_runtime_set_wasi_args_ex(enclave_module->module, - (const char **)enclave_module->wasi_dir_list, - dir_list_size, - NULL, 0, - (const char **)enclave_module->wasi_env_list, - env_list_size, - enclave_module->wasi_argv, - enclave_module->wasi_argc, - (stdinfd != -1) ? stdinfd : 0, - (stdoutfd != -1) ? stdoutfd : 1, - (stderrfd != -1) ? stderrfd : 2); + wasm_runtime_set_wasi_args_ex( + enclave_module->module, (const char **)enclave_module->wasi_dir_list, + dir_list_size, NULL, 0, (const char **)enclave_module->wasi_env_list, + env_list_size, enclave_module->wasi_argv, enclave_module->wasi_argc, + (stdinfd != -1) ? stdinfd : 0, (stdoutfd != -1) ? stdoutfd : 1, + (stderrfd != -1) ? stderrfd : 2); *args_org = true; } @@ -408,8 +400,7 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc) #endif /* end of SGX_DISABLE_WASI */ void -ecall_handle_command(unsigned cmd, - unsigned char *cmd_buf, +ecall_handle_command(unsigned cmd, unsigned char *cmd_buf, unsigned cmd_buf_size) { 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 */ - if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, - 16 * 1024, - 16 * 1024, - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst = + wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024, + error_buf, sizeof(error_buf)))) { ocall_print(error_buf); ocall_print("\n"); goto fail2; diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_test.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_test.cpp index 12632a02..2ae6454d 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_test.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_test.cpp @@ -8,7 +8,8 @@ #include "wasm_export.h" #include "bh_platform.h" -void ecall_iwasm_test() +void +ecall_iwasm_test() { ocall_print(" Prepare to invoke sgx_open ... ==>\n\n"); @@ -76,7 +77,7 @@ void ecall_iwasm_test() 02 : - O_RDWR */ /** 1. open **/ fd = open(file, O_RDWR); - if (fd !=-1) { + if (fd != -1) { ocall_print("\tOperation open test_open.txt success.\n"); } @@ -107,7 +108,7 @@ void ecall_iwasm_test() ocall_print("\tOperation fdatasync success.\n"); } - /** isatty **/ + /** isatty **/ ret = isatty(fd); if (ret == 0) { ocall_print("\tOperation fisatty success.\n"); @@ -140,7 +141,7 @@ void ecall_iwasm_test() p_dirent = readdir(dirp); if (p_dirent != NULL) { ocall_print("\tOperation readdir success.\t"); - ocall_print(p_dirent -> d_name); + ocall_print(p_dirent->d_name); ocall_print("\n"); } @@ -158,7 +159,7 @@ void ecall_iwasm_test() /** closedir **/ ret = closedir(dirp); - if (ret == 0 ) { + if (ret == 0) { ocall_print("\tOperation closedir success. \n"); } /* 2. close */ @@ -192,7 +193,7 @@ void ecall_iwasm_test() ret = renameat(AT_FDCWD, rlt_dir_path, AT_FDCWD, rlt_dir_path_new); if (ret == 0) { 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); @@ -220,7 +221,7 @@ void ecall_iwasm_test() ret = symlinkat(file, AT_FDCWD, file_sf_ln); if (ret == 0) { ocall_print("\tOperation symlinkat from test.txt " - "to text_sf_ln.txt success. \n"); + "to text_sf_ln.txt success. \n"); } /** readlinkat **/ total_size = readlinkat(AT_FDCWD, file_sf_ln, buf, sizeof(buf)); @@ -336,22 +337,23 @@ void ecall_iwasm_test() close(fd); /** getopt **/ - while((ret = getopt(argc, argv, "f:abc")) != -1){ //get option from the getopt() method - switch(ret){ - //For option i, r, l, print that these are options - case 'a': - case 'b': - case 'c': - ocall_print("\tGiven Option operation success. \n"); - break; - case 'f': //here f is used for some file name - ocall_print("\tGiven File operation success.\n"); - break; - case '?': //used for some unknown options - ocall_print("\tunknown option trigger success.\n"); - break; - } - } + while ((ret = getopt(argc, argv, "f:abc")) + != -1) { // get option from the getopt() method + switch (ret) { + // For option i, r, l, print that these are options + case 'a': + case 'b': + case 'c': + ocall_print("\tGiven Option operation success. \n"); + break; + case 'f': // here f is used for some file name + ocall_print("\tGiven File operation success.\n"); + break; + case '?': // used for some unknown options + ocall_print("\tunknown option trigger success.\n"); + break; + } + } /** sched_yield **/ ret = sched_yield(); diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index de241751..ecdd7973 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -18,6 +18,7 @@ static char **app_argv; #define MODULE_PATH ("--module-path=") +/* clang-format off */ static int print_help() { @@ -55,6 +56,7 @@ print_help() #endif return 1; } +/* clang-format on */ static void * app_instance_main(wasm_module_inst_t module_inst) @@ -194,8 +196,8 @@ module_reader_callback(const char *module_name, uint8 **p_buffer, uint32 *p_size) { const char *format = "%s/%s.wasm"; - int sz = strlen(module_search_path) + strlen("/") + strlen(module_name) + - strlen(".wasm") + 1; + int sz = strlen(module_search_path) + strlen("/") + strlen(module_name) + + strlen(".wasm") + 1; char *wasm_file_name = BH_MALLOC(sz); if (!wasm_file_name) { return false; @@ -245,12 +247,12 @@ main(int argc, char *argv[]) uint32 env_list_size = 0; #endif #if WASM_ENABLE_DEBUG_INTERP != 0 - char * ip_addr = NULL; - //int platform_port = 0; + char *ip_addr = NULL; + /* int platform_port = 0; */ int instance_port = 0; #endif - /* Process options. */ + /* Process options. */ for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) { argc--, argv++; @@ -332,7 +334,7 @@ main(int argc, char *argv[]) #endif #if WASM_ENABLE_DEBUG_INTERP != 0 else if (!strncmp(argv[0], "-g=", 3)) { - char * port_str = strchr(argv[0] + 3, ':'); + char *port_str = strchr(argv[0] + 3, ':'); char *port_end; if (port_str == NULL) return print_help(); @@ -386,7 +388,7 @@ main(int argc, char *argv[]) /* load WASM byte buffer from WASM bin file */ if (!(wasm_file_buf = - (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) + (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) goto fail1; if (is_xip_mode) { @@ -394,15 +396,15 @@ main(int argc, char *argv[]) int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; int map_flags = MMAP_MAP_NONE; - if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, - map_prot, map_flags))) { + if (!(wasm_file_mapped = + os_mmap(NULL, (uint32)wasm_file_size, map_prot, map_flags))) { printf("mmap memory failed\n"); wasm_runtime_free(wasm_file_buf); goto fail1; } - bh_memcpy_s(wasm_file_mapped, wasm_file_size, - wasm_file_buf, wasm_file_size); + bh_memcpy_s(wasm_file_mapped, wasm_file_size, wasm_file_buf, + wasm_file_size); wasm_runtime_free(wasm_file_buf); wasm_file_buf = wasm_file_mapped; } @@ -425,8 +427,8 @@ main(int argc, char *argv[]) /* instantiate the module */ if (!(wasm_module_inst = - wasm_runtime_instantiate(wasm_module, stack_size, heap_size, - error_buf, sizeof(error_buf)))) { + wasm_runtime_instantiate(wasm_module, stack_size, heap_size, + error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail3; } diff --git a/product-mini/platforms/riot/iwasmt.c b/product-mini/platforms/riot/iwasmt.c index 7f6b75fb..955a1594 100644 --- a/product-mini/platforms/riot/iwasmt.c +++ b/product-mini/platforms/riot/iwasmt.c @@ -4,19 +4,17 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -// #include #include #include #include -// #include "platform_api_extension.h" #include "wasm_export.h" #include -/*provide some test program*/ +/* provide some test program */ #include "test_wasm.h" #define DEFAULT_THREAD_STACKSIZE (6 * 1024) @@ -25,31 +23,31 @@ static int app_argc; static char **app_argv; -static void* +static void * app_instance_main(wasm_module_inst_t module_inst) { const char *exception; wasm_application_execute_main(module_inst, app_argc, app_argv); - if ((exception = wasm_runtime_get_exception(module_inst))){ + if ((exception = wasm_runtime_get_exception(module_inst))) { puts(exception); } return NULL; } - void * iwasm_t(void *arg1) { - wasm_module_t wasm_module = (wasm_module_t) arg1; + wasm_module_t wasm_module = (wasm_module_t)arg1; wasm_module_inst_t wasm_module_inst = NULL; char error_buf[128]; - + /* instantiate the module */ - if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024, - 8 * 1024, error_buf, sizeof(error_buf)))) { + if (!(wasm_module_inst = wasm_runtime_instantiate( + wasm_module, 8 * 1024, 8 * 1024, error_buf, sizeof(error_buf)))) { puts(error_buf); - }else{ + } + else { app_instance_main(wasm_module_inst); /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); @@ -57,10 +55,14 @@ iwasm_t(void *arg1) return NULL; } +/* choose allocator */ +#define FUNC_ALLOC +/* #define POOL_ALLOC */ + void * iwasm_main(void *arg1) { - (void) arg1; /*unused*/ + (void)arg1; /* unused */ uint8_t *wasm_file_buf = NULL; unsigned wasm_file_buf_size = 0; wasm_module_t wasm_module = NULL; @@ -68,13 +70,9 @@ iwasm_main(void *arg1) RuntimeInitArgs init_args; -//chose allocator -#define FUNC_ALLOC -//#define POOL_ALLOC - memset(&init_args, 0, sizeof(RuntimeInitArgs)); #ifdef POOL_ALLOC - static char global_heap_buf[256 * 1024] = { 0 };//(256 kB) + static char global_heap_buf[256 * 1024] = { 0 }; /* 256 kB */ init_args.mem_alloc_type = Alloc_With_Pool; init_args.mem_alloc_option.pool.heap_buf = global_heap_buf; @@ -94,17 +92,16 @@ iwasm_main(void *arg1) return NULL; } - /* load WASM byte buffer from byte buffer of include file */ - wasm_file_buf = (uint8_t *) wasm_test_file; + wasm_file_buf = (uint8_t *)wasm_test_file; wasm_file_buf_size = sizeof(wasm_test_file); /* load WASM module */ if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_buf_size, error_buf, sizeof(error_buf)))) { puts(error_buf); - - }else{ + } + else { iwasm_t(wasm_module); wasm_runtime_unload(wasm_module); } @@ -116,14 +113,15 @@ iwasm_main(void *arg1) bool iwasm_init(void) { - struct{ - char * stack; - int stacksize; - uint8_t priority; - int flags; + /* clang-format off */ + struct { + char *stack; + int stacksize; + uint8_t priority; + int flags; thread_task_func_t task_func; - void * arg; - const char * name; + void *arg; + const char *name; } b = { .stacksize = DEFAULT_THREAD_STACKSIZE, .priority = 8, @@ -132,17 +130,20 @@ iwasm_init(void) .arg = NULL, .name = "simple_wamr" }; + /* clang-format on */ - b.stack=malloc(b.stacksize); - kernel_pid_t tpid = thread_create (b.stack, b.stacksize, b.priority, b.flags, b.task_func, b.arg, b.name); + b.stack = malloc(b.stacksize); + kernel_pid_t tpid = thread_create(b.stack, b.stacksize, b.priority, b.flags, + b.task_func, b.arg, b.name); - return tpid != 0 ? true : false;; + return tpid != 0 ? true : false; + ; } -#define telltruth(X) ((X) ? "true" : "false") +#define telltruth(X) ((X) ? "true" : "false") int main(void) { - printf("iwasm_initilised: %s\n",telltruth(iwasm_init())); + printf("iwasm_initilised: %s\n", telltruth(iwasm_init())); } diff --git a/product-mini/platforms/rt-thread/iwasm.c b/product-mini/platforms/rt-thread/iwasm.c index c7ab46a8..8ae4da76 100644 --- a/product-mini/platforms/rt-thread/iwasm.c +++ b/product-mini/platforms/rt-thread/iwasm.c @@ -15,17 +15,21 @@ #ifdef WAMR_ENABLE_RTT_EXPORT #ifdef WAMR_RTT_EXPORT_VPRINTF -static int wasm_vprintf(wasm_exec_env_t env, const char* fmt, va_list va) +static int +wasm_vprintf(wasm_exec_env_t env, const char *fmt, va_list va) { return vprintf(fmt, va); } -static int wasm_vsprintf(wasm_exec_env_t env, char* buf, const char* fmt, va_list va) +static int +wasm_vsprintf(wasm_exec_env_t env, char *buf, const char *fmt, va_list va) { return vsprintf(buf, fmt, va); } -static int wasm_vsnprintf(wasm_exec_env_t env, char *buf, int n, const char *fmt, va_list va) +static int +wasm_vsnprintf(wasm_exec_env_t env, char *buf, int n, const char *fmt, + va_list va) { return vsnprintf(buf, n, fmt, va); } @@ -33,118 +37,127 @@ static int wasm_vsnprintf(wasm_exec_env_t env, char *buf, int n, const char *fmt #endif /* WAMR_RTT_EXPORT_VPRINTF */ #ifdef WAMR_RTT_EXPORT_DEVICE_OPS -static rt_device_t wasm_rt_device_find(wasm_exec_env_t env, const char *name) +static rt_device_t +wasm_rt_device_find(wasm_exec_env_t env, const char *name) { return rt_device_find(name); } -static rt_err_t wasm_rt_device_open(wasm_exec_env_t env, rt_device_t dev, rt_uint16_t o_flag) +static rt_err_t +wasm_rt_device_open(wasm_exec_env_t env, rt_device_t dev, rt_uint16_t o_flag) { - return rt_device_open(dev , o_flag); + return rt_device_open(dev, o_flag); } -static rt_size_t wasm_rt_device_write(wasm_exec_env_t env, rt_device_t dev, rt_off_t offset, const void*buf, rt_size_t size) +static rt_size_t +wasm_rt_device_write(wasm_exec_env_t env, rt_device_t dev, rt_off_t offset, + const void *buf, rt_size_t size) { return rt_device_write(dev, offset, buf, size); } -static rt_size_t wasm_rt_device_read(wasm_exec_env_t env, rt_device_t dev, rt_off_t offset, void *buf, rt_size_t size) +static rt_size_t +wasm_rt_device_read(wasm_exec_env_t env, rt_device_t dev, rt_off_t offset, + void *buf, rt_size_t size) { return rt_device_read(dev, offset, buf, size); } -static rt_err_t wasm_rt_device_close(wasm_exec_env_t env, rt_device_t dev) +static rt_err_t +wasm_rt_device_close(wasm_exec_env_t env, rt_device_t dev) { return rt_device_close(dev); } -static rt_err_t wasm_rt_device_control(wasm_exec_env_t env, rt_device_t dev, int cmd, void *arg) +static rt_err_t +wasm_rt_device_control(wasm_exec_env_t env, rt_device_t dev, int cmd, void *arg) { return rt_device_control(dev, cmd, arg); } #endif /* WAMR_RTT_EXPORT_DEVICE_OPS */ +/* clang-format off */ static NativeSymbol native_export_symbols[] = { - #ifdef WAMR_RTT_EXPORT_VPRINTF - { - "vprintf", - wasm_vprintf, - "($*)i" - }, - { - "vsprintf", - wasm_vsprintf, - "($$*)i" - }, - { - "vsnprintf", - wasm_vsnprintf, - "($i$*)i" - }, + { + "vprintf", + wasm_vprintf, + "($*)i" + }, + { + "vsprintf", + wasm_vsprintf, + "($$*)i" + }, + { + "vsnprintf", + wasm_vsnprintf, + "($i$*)i" + }, #endif /* WAMR_RTT_EXPORT_VPRINTF */ #ifdef WAMR_RTT_EXPORT_DEVICE_OPS - { - "rt_device_find", - wasm_rt_device_find, - "($)i" - }, - { - "rt_device_open", - wasm_rt_device_open, - "(ii)i" - }, - { - "rt_device_write", - wasm_rt_device_write, - "(ii*~)i" - }, - { - "rt_device_read", - wasm_rt_device_read, - "(ii*~)i" - }, - { - "rt_device_close", - wasm_rt_device_close, - "(i)i" - }, - { - "rt_device_control", - wasm_rt_device_control, - "(ii*)i" - }, + { + "rt_device_find", + wasm_rt_device_find, + "($)i" + }, + { + "rt_device_open", + wasm_rt_device_open, + "(ii)i" + }, + { + "rt_device_write", + wasm_rt_device_write, + "(ii*~)i" + }, + { + "rt_device_read", + wasm_rt_device_read, + "(ii*~)i" + }, + { + "rt_device_close", + wasm_rt_device_close, + "(i)i" + }, + { + "rt_device_control", + wasm_rt_device_control, + "(ii*)i" + }, #ifdef WAMR_RTT_EXPORT_DEVICE_OPS_CPP - { - "_Z15rt_device_closeP9rt_device", - wasm_rt_device_close, - "(i)i" - }, - { - "_Z14rt_device_readP9rt_devicejPvj", - wasm_rt_device_read, - "(ii*~)i" - }, - { - "_Z15rt_device_writeP9rt_devicejPKvj", - wasm_rt_device_write, - "(ii*~)i" - }, - { - "_Z14rt_device_openP9rt_devicet", - wasm_rt_device_open, - "(ii)i" - }, - { - "_Z14rt_device_findPKc", - wasm_rt_device_find, - "($)i" - }, + { + "_Z15rt_device_closeP9rt_device", + wasm_rt_device_close, + "(i)i" + }, + { + "_Z14rt_device_readP9rt_devicejPvj", + wasm_rt_device_read, + "(ii*~)i" + }, + { + "_Z15rt_device_writeP9rt_devicejPKvj", + wasm_rt_device_write, + "(ii*~)i" + }, + { + "_Z14rt_device_openP9rt_devicet", + wasm_rt_device_open, + "(ii)i" + }, + { + "_Z14rt_device_findPKc", + wasm_rt_device_find, + "($)i" + }, #endif /* WAMR_RTT_EXPORT_DEVICE_OPS_CPP */ #endif /* WAMR_RTT_EXPORT_DEVICE_OPS */ }; +/* clang-format on */ #endif /* WAMR_ENABLE_RTT_EXPORT */ @@ -166,23 +179,22 @@ app_instance_main(wasm_module_inst_t module_inst, int app_argc, char **app_argv) return NULL; } -rt_uint8_t *my_read_file_to_buffer(char* filename, rt_uint32_t *size) +rt_uint8_t * +my_read_file_to_buffer(char *filename, rt_uint32_t *size) { struct stat f_stat; dfs_file_stat(filename, &f_stat); struct dfs_fd fd; - rt_uint8_t* buff = rt_malloc(f_stat.st_size); + rt_uint8_t *buff = rt_malloc(f_stat.st_size); *size = 0; - if (!buff) - { + if (!buff) { rt_set_errno(-ENOMEM); return RT_NULL; } int ret = dfs_file_open(&fd, filename, O_RDONLY); - if (ret) - { + if (ret) { rt_free(buff); rt_set_errno(ret); return RT_NULL; @@ -192,8 +204,7 @@ rt_uint8_t *my_read_file_to_buffer(char* filename, rt_uint32_t *size) dfs_file_close(&fd); - if (*size != f_stat.st_size) - { + if (*size != f_stat.st_size) { rt_free(buff); rt_set_errno(-EBADF); return RT_NULL; @@ -202,20 +213,23 @@ rt_uint8_t *my_read_file_to_buffer(char* filename, rt_uint32_t *size) return buff; } -void iwasm_help(void) +void +iwasm_help(void) { #ifdef WAMR_ENABLE_IWASM_PARAMS - rt_kputs("wrong input: iwasm [-t] [-m] [-s] <*.wasm> \n iwasm [-h]\n"); + rt_kputs("wrong input: iwasm [-t] [-m] [-s] <*.wasm> \n" + " iwasm [-h]\n"); rt_kputs("\t -h: show this tips.\n"); rt_kputs("\t -t: show time taking to run this app.\n"); rt_kputs("\t -m: show memory taking to run this app\n"); rt_kputs("\t wasm file name and exec params must behind of all vm-param\n"); #else rt_kputs("wrong input: iwasm <*.wasm> \n"); -#endif /* WAMR_ENABLE_PARAMS */ +#endif /* WAMR_ENABLE_PARAMS */ } -int iwasm(int argc, char **argv) +int +iwasm(int argc, char **argv) { rt_uint8_t *wasm_file_buf = NULL; rt_uint32_t wasm_file_size; @@ -224,52 +238,43 @@ int iwasm(int argc, char **argv) wasm_module_inst_t wasm_module_inst = NULL; RuntimeInitArgs init_args; static char error_buf[128] = { 0 }; - // avoid stack overflow + /* avoid stack overflow */ #ifdef WAMR_ENABLE_IWASM_PARAMS int i_arg_begin; bool show_mem = false; bool show_stack = false; bool show_time_exec = false; - for(i_arg_begin=1; i_arg_begin < argc; i_arg_begin++) - { - if (argv[i_arg_begin][0] != '-') - { + for (i_arg_begin = 1; i_arg_begin < argc; i_arg_begin++) { + if (argv[i_arg_begin][0] != '-') { break; } - if (argv[i_arg_begin][1] == 'm') - { + if (argv[i_arg_begin][1] == 'm') { show_mem = true; } - else if (argv[i_arg_begin][1] == 's') - { + else if (argv[i_arg_begin][1] == 's') { show_stack = true; } - else if (argv[i_arg_begin][1] == 't') - { + else if (argv[i_arg_begin][1] == 't') { show_time_exec = true; } - else if (argv[i_arg_begin][1] == 'h') - { + else if (argv[i_arg_begin][1] == 'h') { iwasm_help(); return 0; } - else if (argv[i_arg_begin][1] == 0x00) - { + else if (argv[i_arg_begin][1] == 0x00) { continue; } - else - { + else { rt_kprintf("[iwasm] unknown param: %s\n", argv[i_arg_begin]); } } -#else /* WAMR_ENABLE_PARAMS */ +#else /* WAMR_ENABLE_PARAMS */ #define i_arg_begin 1 -#endif /* WAMR_ENABLE_PARAMS */ +#endif /* WAMR_ENABLE_PARAMS */ - if (argc - i_arg_begin < 1) - { + if (argc - i_arg_begin < 1) { iwasm_help(); return -1; } @@ -281,56 +286,53 @@ int iwasm(int argc, char **argv) init_args.mem_alloc_option.allocator.free_func = os_free; #ifdef WAMR_ENABLE_RTT_EXPORT init_args.native_symbols = native_export_symbols; - init_args.n_native_symbols = sizeof(native_export_symbols) / sizeof(NativeSymbol); + init_args.n_native_symbols = + sizeof(native_export_symbols) / sizeof(NativeSymbol); init_args.native_module_name = "env"; #endif /* WAMR_ENABLE_RTT_EXPORT */ #ifdef WAMR_ENABLE_IWASM_PARAMS #if defined(RT_USING_HEAP) && defined(RT_USING_MEMHEAP_AS_HEAP) extern long list_memheap(void); - if (show_mem) - { + if (show_mem) { list_memheap(); } #else rt_uint32_t total, max, used; - if (show_mem) - { + if (show_mem) { rt_memory_info(&total, &used, &max); } #endif rt_thread_t tid; - if (show_stack) - { + if (show_stack) { tid = rt_thread_self(); - printf("thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr, tid->stack_size, tid->sp); + printf("thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr, + tid->stack_size, tid->sp); } -#endif /* WAMR_ENABLE_PARAMS */ +#endif /* WAMR_ENABLE_PARAMS */ - if (wasm_runtime_full_init(&init_args) == false) - { + if (wasm_runtime_full_init(&init_args) == false) { rt_kprintf("Init WASM runtime environment failed.\n"); return -1; } - wasm_file_buf = my_read_file_to_buffer (argv[i_arg_begin], &wasm_file_size); - if (!wasm_file_buf) - { + wasm_file_buf = my_read_file_to_buffer(argv[i_arg_begin], &wasm_file_size); + if (!wasm_file_buf) { rt_err_t err = rt_get_errno(); rt_kprintf("WASM load file to RAM failed: %d\n", err); goto fail1; } rt_memset(error_buf, 0x00, sizeof(error_buf)); - wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)); - if (!wasm_module) - { + wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, + sizeof(error_buf)); + if (!wasm_module) { rt_kprintf("%s\n", error_buf); goto fail2; } rt_memset(error_buf, 0x00, sizeof(error_buf)); - wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) - { + wasm_module_inst = wasm_runtime_instantiate( + wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)); + if (!wasm_module_inst) { rt_kprintf("%s\n", error_buf); goto fail3; } @@ -340,35 +342,33 @@ int iwasm(int argc, char **argv) if (show_time_exec) { ticks_exec = rt_tick_get(); } -#endif /* WAMR_ENABLE_PARAMS */ +#endif /* WAMR_ENABLE_PARAMS */ app_instance_main(wasm_module_inst, argc - i_arg_begin, &argv[i_arg_begin]); #ifdef WAMR_ENABLE_IWASM_PARAMS - if (show_time_exec) - { + if (show_time_exec) { ticks_exec = rt_tick_get() - ticks_exec; - printf("[iwasm] execute ticks took: %u [ticks/s = %u]\n", ticks_exec, RT_TICK_PER_SECOND); + printf("[iwasm] execute ticks took: %u [ticks/s = %u]\n", ticks_exec, + RT_TICK_PER_SECOND); } #if defined(RT_USING_HEAP) && defined(RT_USING_MEMHEAP_AS_HEAP) - if (show_mem) - { + if (show_mem) { list_memheap(); } #else rt_uint32_t total_after, max_after, used_after; - if (show_mem) - { + if (show_mem) { rt_memory_info(&total_after, &used_after, &max_after); rt_kprintf("[iwasm] memory took: %u\n", used_after - used); } #endif - if (show_stack) - { - printf("[iwasm] thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr, tid->stack_size, tid->sp); + if (show_stack) { + printf("[iwasm] thread stack addr: %p, size: %u, sp: %p\n", + tid->stack_addr, tid->stack_size, tid->sp); } -#endif /* WAMR_ENABLE_PARAMS */ +#endif /* WAMR_ENABLE_PARAMS */ /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 3a224d9c..04828041 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -15,6 +15,7 @@ static char **app_argv; #define MODULE_PATH ("--module-path=") +/* clang-format off */ static int print_help() { @@ -47,6 +48,7 @@ print_help() #endif return 1; } +/* clang-format on */ static void * app_instance_main(wasm_module_inst_t module_inst) @@ -113,37 +115,37 @@ split_string(char *str, int *count) static void * app_instance_repl(wasm_module_inst_t module_inst) { - char buffer[4096]; - char *cmd; - size_t n; + char buffer[4096]; + char *cmd; + size_t n; - while ((printf("webassembly> "), - cmd = fgets(buffer, sizeof(buffer), stdin)) != NULL) { - bh_assert(cmd); - n = strlen(cmd); - if (cmd[n - 1] == '\n') { - if (n == 1) - continue; - else - cmd[n - 1] = '\0'; - } - if (!strcmp(cmd, "__exit__")) { - printf("exit repl mode\n"); - break; - } - app_argv = split_string(cmd, &app_argc); - if (app_argv == NULL) { - LOG_ERROR("Wasm prepare param failed: split string failed.\n"); - break; - } - if (app_argc != 0) { - wasm_application_execute_func(module_inst, app_argv[0], - app_argc - 1, app_argv + 1); - } - free(app_argv); - } + while ((printf("webassembly> "), cmd = fgets(buffer, sizeof(buffer), stdin)) + != NULL) { + bh_assert(cmd); + n = strlen(cmd); + if (cmd[n - 1] == '\n') { + if (n == 1) + continue; + else + cmd[n - 1] = '\0'; + } + if (!strcmp(cmd, "__exit__")) { + printf("exit repl mode\n"); + break; + } + app_argv = split_string(cmd, &app_argc); + if (app_argv == NULL) { + LOG_ERROR("Wasm prepare param failed: split string failed.\n"); + break; + } + if (app_argc != 0) { + wasm_application_execute_func(module_inst, app_argv[0], + app_argc - 1, app_argv + 1); + } + free(app_argv); + } - return NULL; + return NULL; } #if WASM_ENABLE_LIBC_WASI != 0 @@ -175,7 +177,7 @@ static char global_heap_buf[10 * 1024 * 1024] = { 0 }; static char * handle_module_path(const char *module_path) { - // next character after = + /* next character after '=' */ return (strchr(module_path, '=')) + 1; } @@ -235,7 +237,7 @@ main(int argc, char *argv[]) uint32 env_list_size = 0; #endif - /* Process options. */ + /* Process options. */ for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) { argc--, argv++; @@ -348,7 +350,7 @@ main(int argc, char *argv[]) /* load WASM byte buffer from WASM bin file */ if (!(wasm_file_buf = - (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) + (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) goto fail1; #if WASM_ENABLE_MULTI_MODULE != 0 @@ -369,8 +371,8 @@ main(int argc, char *argv[]) /* instantiate the module */ if (!(wasm_module_inst = - wasm_runtime_instantiate(wasm_module, stack_size, heap_size, - error_buf, sizeof(error_buf)))) { + wasm_runtime_instantiate(wasm_module, stack_size, heap_size, + error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail3; } diff --git a/product-mini/platforms/zephyr/simple/src/main.c b/product-mini/platforms/zephyr/simple/src/main.c index f5b83e6d..27ce7cb9 100644 --- a/product-mini/platforms/zephyr/simple/src/main.c +++ b/product-mini/platforms/zephyr/simple/src/main.c @@ -57,10 +57,10 @@ static char **app_argv; * @return true if the main function is called, false otherwise. */ bool -wasm_application_execute_main(wasm_module_inst_t module_inst, - int argc, char *argv[]); +wasm_application_execute_main(wasm_module_inst_t module_inst, int argc, + char *argv[]); -static void* +static void * app_instance_main(wasm_module_inst_t module_inst) { const char *exception; @@ -69,15 +69,15 @@ app_instance_main(wasm_module_inst_t module_inst) unsigned argv[2] = { 0 }; if (wasm_runtime_lookup_function(module_inst, "main", NULL) - || wasm_runtime_lookup_function(module_inst, - "__main_argc_argv", NULL)) { + || wasm_runtime_lookup_function(module_inst, "__main_argc_argv", + NULL)) { LOG_VERBOSE("Calling main funciton\n"); wasm_application_execute_main(module_inst, app_argc, app_argv); } - else if ((func = wasm_runtime_lookup_function(module_inst, - "app_main", NULL))) { - exec_env = wasm_runtime_create_exec_env(module_inst, - CONFIG_APP_HEAP_SIZE); + else if ((func = wasm_runtime_lookup_function(module_inst, "app_main", + NULL))) { + exec_env = + wasm_runtime_create_exec_env(module_inst, CONFIG_APP_HEAP_SIZE); if (!exec_env) { os_printf("Create exec env failed\n"); return NULL; @@ -110,15 +110,17 @@ static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 }; /* esp32_technical_reference_manual: " -The capacity of Internal SRAM 1 is 128 KB. Either CPU can read and write this memory at addresses -0x3FFE_0000 ~ 0x3FFF_FFFF of the data bus, and also at addresses 0x400A_0000 ~ 0x400B_FFFF of the -instruction bus. +The capacity of Internal SRAM 1 is 128 KB. Either CPU can read and write this +memory at addresses 0x3FFE_0000 ~ 0x3FFF_FFFF of the data bus, and also at +addresses 0x400A_0000 ~ 0x400B_FFFF of the instruction bus. " -The custom linker script defines dram0_1_seg and map it to 0x400A_0000 ~ 0x400B_FFFF for instruction bus access. -Here we define the buffer that will be placed to dram0_1_seg. +The custom linker script defines dram0_1_seg and map it to 0x400A_0000 ~ +0x400B_FFFF for instruction bus access. Here we define the buffer that will be +placed to dram0_1_seg. */ -static char esp32_executable_memory_buf[100 * 1024] __attribute__((section (".aot_code_buf"))) = { 0 }; +static char esp32_executable_memory_buf[100 * 1024] + __attribute__((section(".aot_code_buf"))) = { 0 }; /* the poll allocator for executable memory */ static mem_allocator_t esp32_exec_mem_pool_allocator; @@ -127,8 +129,8 @@ static int esp32_exec_mem_init() { if (!(esp32_exec_mem_pool_allocator = - mem_allocator_create(esp32_executable_memory_buf, - sizeof(esp32_executable_memory_buf)))) + mem_allocator_create(esp32_executable_memory_buf, + sizeof(esp32_executable_memory_buf)))) return -1; return 0; @@ -153,7 +155,8 @@ esp32_exec_mem_free(void *addr) } #endif /* end of #ifdef CONFIG_BOARD_ESP32 */ -void iwasm_main(void *arg1, void *arg2, void *arg3) +void +iwasm_main(void *arg1, void *arg2, void *arg3) { int start, end; start = k_uptime_get_32(); @@ -167,9 +170,9 @@ void iwasm_main(void *arg1, void *arg2, void *arg3) int log_verbose_level = 2; #endif - (void) arg1; - (void) arg2; - (void) arg3; + (void)arg1; + (void)arg2; + (void)arg3; memset(&init_args, 0, sizeof(RuntimeInitArgs)); @@ -198,7 +201,7 @@ void iwasm_main(void *arg1, void *arg2, void *arg3) #endif /* 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); /* load WASM module */ @@ -213,11 +216,9 @@ void iwasm_main(void *arg1, void *arg2, void *arg3) } /* instantiate the module */ - if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, - CONFIG_APP_STACK_SIZE, - CONFIG_APP_HEAP_SIZE, - error_buf, - sizeof(error_buf)))) { + if (!(wasm_module_inst = wasm_runtime_instantiate( + wasm_module, CONFIG_APP_STACK_SIZE, CONFIG_APP_HEAP_SIZE, + error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail2; } @@ -253,16 +254,16 @@ fail1: K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE); static struct k_thread iwasm_main_thread; -bool iwasm_init(void) +bool +iwasm_init(void) { - k_tid_t tid = k_thread_create(&iwasm_main_thread, iwasm_main_thread_stack, - MAIN_THREAD_STACK_SIZE, - iwasm_main, NULL, NULL, NULL, - MAIN_THREAD_PRIORITY, 0, K_NO_WAIT); + k_tid_t tid = k_thread_create( + &iwasm_main_thread, iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE, + iwasm_main, NULL, NULL, NULL, MAIN_THREAD_PRIORITY, 0, K_NO_WAIT); return tid ? true : false; } -void main(void) +void +main(void) { iwasm_init(); } - diff --git a/product-mini/platforms/zephyr/simple/src/test_wasm_riscv64.h b/product-mini/platforms/zephyr/simple/src/test_wasm_riscv64.h index 526317af..1b45211d 100644 --- a/product-mini/platforms/zephyr/simple/src/test_wasm_riscv64.h +++ b/product-mini/platforms/zephyr/simple/src/test_wasm_riscv64.h @@ -4,38 +4,38 @@ */ unsigned char __aligned(4) wasm_test_file[] = { - 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, - 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, - 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, - 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, - 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, - 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, - 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, - 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x12, 0x03, - 0x7F, 0x01, 0x41, 0xC0, 0x01, 0x0B, 0x7F, 0x00, 0x41, 0x3A, 0x0B, 0x7F, - 0x00, 0x41, 0xC0, 0x01, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, 0x6D, - 0x6F, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x04, - 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, - 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, - 0x65, 0x03, 0x02, 0x0A, 0xB1, 0x01, 0x01, 0xAE, 0x01, 0x01, 0x03, 0x7F, - 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, 0x24, - 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x80, 0x80, 0x80, 0x00, 0x10, - 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, 0x10, - 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, 0x41, 0xA8, - 0x80, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, - 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, - 0x10, 0x41, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, 0x10, 0x6A, - 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, 0x04, 0x20, - 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x80, 0x80, 0x80, - 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, 0x8D, 0x80, - 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, - 0x00, 0x41, 0x93, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, 0x82, 0x80, - 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, 0x80, 0x00, - 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x20, 0x04, 0x0B, 0x0B, 0x40, 0x01, 0x00, 0x41, 0x00, 0x0B, 0x3A, 0x62, - 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, 0x70, 0x0A, 0x00, - 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, 0x3A, 0x20, 0x25, - 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, - 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, 0x20, 0x62, 0x75, - 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 + 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, + 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, + 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, + 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, + 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, + 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, + 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, + 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x12, 0x03, + 0x7F, 0x01, 0x41, 0xC0, 0x01, 0x0B, 0x7F, 0x00, 0x41, 0x3A, 0x0B, 0x7F, + 0x00, 0x41, 0xC0, 0x01, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, 0x6D, + 0x6F, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x04, + 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, + 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, + 0x65, 0x03, 0x02, 0x0A, 0xB1, 0x01, 0x01, 0xAE, 0x01, 0x01, 0x03, 0x7F, + 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, 0x24, + 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x80, 0x80, 0x80, 0x00, 0x10, + 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, 0x10, + 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, 0x41, 0xA8, + 0x80, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, + 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, + 0x10, 0x41, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, 0x10, 0x6A, + 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, 0x04, 0x20, + 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x80, 0x80, 0x80, + 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, 0x8D, 0x80, + 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, + 0x00, 0x41, 0x93, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, 0x82, 0x80, + 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, 0x80, 0x00, + 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, + 0x20, 0x04, 0x0B, 0x0B, 0x40, 0x01, 0x00, 0x41, 0x00, 0x0B, 0x3A, 0x62, + 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, 0x70, 0x0A, 0x00, + 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, 0x3A, 0x20, 0x25, + 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, + 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, 0x20, 0x62, 0x75, + 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 }; diff --git a/product-mini/platforms/zephyr/simple/src/wasm-app-riscv64/main.c b/product-mini/platforms/zephyr/simple/src/wasm-app-riscv64/main.c index 76063452..026cb8fd 100644 --- a/product-mini/platforms/zephyr/simple/src/wasm-app-riscv64/main.c +++ b/product-mini/platforms/zephyr/simple/src/wasm-app-riscv64/main.c @@ -6,7 +6,8 @@ #include #include -int main(int argc, char **argv) +int +main(int argc, char **argv) { char *buf;