Просмотр исходного кода

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

Apply clang-format for core/shared and product-mini files
Wenyong Huang 4 лет назад
Родитель
Сommit
17f62ad472
100 измененных файлов с 2757 добавлено и 2320 удалено
  1. 99 97
      core/shared/coap/er-coap/coap-constants.h
  2. 57 65
      core/shared/mem-alloc/ems/ems_alloc.c
  3. 15 19
      core/shared/mem-alloc/ems/ems_gc.h
  4. 50 36
      core/shared/mem-alloc/ems/ems_gc_internal.h
  5. 9 9
      core/shared/mem-alloc/ems/ems_hmu.c
  6. 46 48
      core/shared/mem-alloc/ems/ems_kfc.c
  7. 25 27
      core/shared/mem-alloc/mem_alloc.c
  8. 2 3
      core/shared/mem-alloc/mem_alloc.h
  9. 2 4
      core/shared/platform/alios/alios_platform.c
  10. 21 20
      core/shared/platform/alios/alios_thread.c
  11. 0 1
      core/shared/platform/alios/alios_time.c
  12. 5 4
      core/shared/platform/alios/platform_internal.h
  13. 26 15
      core/shared/platform/android/platform_init.c
  14. 33 21
      core/shared/platform/android/platform_internal.h
  15. 1 4
      core/shared/platform/common/freertos/freertos_malloc.c
  16. 43 27
      core/shared/platform/common/freertos/freertos_thread.c
  17. 0 1
      core/shared/platform/common/freertos/freertos_time.c
  18. 549 458
      core/shared/platform/common/math/math.c
  19. 0 3
      core/shared/platform/common/posix/posix_malloc.c
  20. 7 11
      core/shared/platform/common/posix/posix_memmap.c
  21. 54 40
      core/shared/platform/common/posix/posix_thread.c
  22. 1 2
      core/shared/platform/common/posix/posix_time.c
  23. 1 3
      core/shared/platform/darwin/platform_init.c
  24. 12 10
      core/shared/platform/darwin/platform_internal.h
  25. 5 7
      core/shared/platform/esp-idf/espidf_platform.c
  26. 2 2
      core/shared/platform/esp-idf/espidf_thread.c
  27. 6 2
      core/shared/platform/esp-idf/platform_internal.h
  28. 28 16
      core/shared/platform/include/platform_api_extension.h
  29. 36 20
      core/shared/platform/include/platform_api_vmcore.h
  30. 10 8
      core/shared/platform/include/platform_common.h
  31. 6 5
      core/shared/platform/linux-sgx/platform_internal.h
  32. 201 142
      core/shared/platform/linux-sgx/sgx_file.c
  33. 146 114
      core/shared/platform/linux-sgx/sgx_file.h
  34. 32 24
      core/shared/platform/linux-sgx/sgx_platform.c
  35. 24 16
      core/shared/platform/linux-sgx/sgx_pthread.c
  36. 11 7
      core/shared/platform/linux-sgx/sgx_pthread.h
  37. 38 33
      core/shared/platform/linux-sgx/sgx_rsrv_mem_mngr.h
  38. 4 3
      core/shared/platform/linux-sgx/sgx_signal.c
  39. 37 37
      core/shared/platform/linux-sgx/sgx_signal.h
  40. 34 26
      core/shared/platform/linux-sgx/sgx_socket.c
  41. 31 29
      core/shared/platform/linux-sgx/sgx_socket.h
  42. 38 22
      core/shared/platform/linux-sgx/sgx_thread.c
  43. 39 32
      core/shared/platform/linux-sgx/sgx_time.c
  44. 16 13
      core/shared/platform/linux-sgx/sgx_time.h
  45. 87 58
      core/shared/platform/linux-sgx/untrusted/file.c
  46. 11 7
      core/shared/platform/linux-sgx/untrusted/pthread.c
  47. 2 1
      core/shared/platform/linux-sgx/untrusted/signal.c
  48. 17 15
      core/shared/platform/linux-sgx/untrusted/socket.c
  49. 14 17
      core/shared/platform/linux-sgx/untrusted/time.c
  50. 1 3
      core/shared/platform/linux/platform_init.c
  51. 12 10
      core/shared/platform/linux/platform_internal.h
  52. 1 1
      core/shared/platform/nuttx/platform_internal.h
  53. 8 7
      core/shared/platform/riot/platform_internal.h
  54. 40 38
      core/shared/platform/riot/riot_thread.c
  55. 0 1
      core/shared/platform/riot/riot_time.c
  56. 1 2
      core/shared/platform/rt-thread/platform_internal.h
  57. 68 55
      core/shared/platform/rt-thread/rtt_platform.c
  58. 1 3
      core/shared/platform/vxworks/platform_init.c
  59. 11 8
      core/shared/platform/vxworks/platform_internal.h
  60. 1 3
      core/shared/platform/windows/platform_init.c
  61. 13 9
      core/shared/platform/windows/platform_internal.h
  62. 0 3
      core/shared/platform/windows/win_malloc.c
  63. 5 7
      core/shared/platform/windows/win_memmap.c
  64. 19 18
      core/shared/platform/windows/win_thread.c
  65. 0 1
      core/shared/platform/windows/win_time.c
  66. 10 5
      core/shared/platform/zephyr/platform_internal.h
  67. 6 8
      core/shared/platform/zephyr/zephyr_platform.c
  68. 71 44
      core/shared/platform/zephyr/zephyr_thread.c
  69. 0 1
      core/shared/platform/zephyr/zephyr_time.c
  70. 5 5
      core/shared/utils/bh_assert.c
  71. 5 5
      core/shared/utils/bh_assert.h
  72. 45 49
      core/shared/utils/bh_common.c
  73. 40 30
      core/shared/utils/bh_common.h
  74. 14 19
      core/shared/utils/bh_hashmap.c
  75. 7 11
      core/shared/utils/bh_hashmap.h
  76. 23 14
      core/shared/utils/bh_list.c
  77. 20 13
      core/shared/utils/bh_list.h
  78. 2 3
      core/shared/utils/bh_log.c
  79. 9 6
      core/shared/utils/bh_log.h
  80. 0 3
      core/shared/utils/bh_platform.h
  81. 40 28
      core/shared/utils/bh_queue.c
  82. 18 14
      core/shared/utils/bh_queue.h
  83. 12 10
      core/shared/utils/bh_vector.c
  84. 0 1
      core/shared/utils/bh_vector.h
  85. 20 21
      core/shared/utils/runtime_timer.c
  86. 26 16
      core/shared/utils/runtime_timer.h
  87. 14 9
      core/shared/utils/uncommon/bh_getopt.c
  88. 2 1
      core/shared/utils/uncommon/bh_getopt.h
  89. 7 11
      core/shared/utils/uncommon/bh_read_file.c
  90. 0 1
      core/shared/utils/uncommon/bh_read_file.h
  91. 4 2
      product-mini/app-samples/hello-world-cmake/main.c
  92. 2 1
      product-mini/app-samples/hello-world-cmake/print.c
  93. 2 1
      product-mini/app-samples/hello-world/main.c
  94. 14 16
      product-mini/platforms/alios-things/src/main.c
  95. 15 14
      product-mini/platforms/android/wasm-jni.cpp
  96. 7 8
      product-mini/platforms/esp-idf/iwasm_main.c
  97. 111 110
      product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp
  98. 35 21
      product-mini/platforms/linux-sgx/enclave-sample/App/pal_api.h
  99. 32 43
      product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp
  100. 25 23
      product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_test.cpp

+ 99 - 97
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,
-
-  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 */
-
-  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 hooks */
-  MANUAL_RESPONSE,
-  PING_RESPONSE
+    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 */
+
+    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 */
+
+    /* Erbium errors */
+    MEMORY_ALLOCATION_ERROR = 192,
+    PACKET_SERIALIZATION_ERROR,
+
+    /* 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,
-
-  /* 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)
+    NO_FLAGS = 0,
+
+    /* 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)
 } coap_resource_flags_t;
 
 #endif /* COAP_CONSTANTS_H_ */
-/** @} */

+ 57 - 65
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);
 }
-

+ 15 - 19
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
-

+ 50 - 36
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 */

+ 9 - 9
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 */
-

+ 46 - 48
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;
 }
-

+ 25 - 27
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 */
-

+ 2 - 3
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 */
-

+ 2 - 4
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()
-{
-}
+{}

+ 21 - 20
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;
 }
-

+ 0 - 1
core/shared/platform/alios/alios_time.c

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

+ 5 - 4
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 */
-

+ 26 - 15
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;

+ 33 - 21
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 <setjmp.h>
@@ -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 */
-

+ 1 - 4
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)
-{
-}
-
+{}

+ 43 - 27
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;
 }
-

+ 0 - 1
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;
 }
-

Разница между файлами не показана из-за своего большого размера
+ 549 - 458
core/shared/platform/common/math/math.c


+ 0 - 3
core/shared/platform/common/posix/posix_malloc.c

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

+ 7 - 11
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)
-{
-}
+{}

+ 54 - 40
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 */
-

+ 1 - 2
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;
 }
-

+ 1 - 3
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
 }
-

+ 12 - 10
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 <setjmp.h>
@@ -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 */
-

+ 5 - 7
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;
 }
-

+ 2 - 2
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;
 }
-

+ 6 - 2
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

+ 28 - 16
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
 }

+ 36 - 20
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_destroy(korp_mutex *mutex);
+int
+os_mutex_init(korp_mutex *mutex);
 
-int os_mutex_lock(korp_mutex *mutex);
+int
+os_mutex_destroy(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
 }

+ 10 - 8
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
 }

+ 6 - 5
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 */
-

+ 201 - 142
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
-

+ 146 - 114
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_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_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_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_ASYNC      020000
-#define O_DIRECT     040000
-#define O_LARGEFILE       0
-#define O_NOATIME  01000000
-#define O_PATH    010000000
+#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_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);
-
-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);
-
-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 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 ioctl(int fd, unsigned long request, ...);
-int fcntl(int fd, int cmd, ... /* arg */ );
-
-int isatty(int fd);
-
-char *realpath(const char *path, char *resolved_path);
-
-int posix_fallocate(int fd, off_t offset, off_t len);
-
-int poll(struct pollfd *fds, nfds_t nfds, int timeout);
-
-int getopt(int argc, char * const argv[],
-           const char *optstring);
-
-int sched_yield(void);
-
-ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
-int getentropy(void *buffer, size_t length);
-
-int get_errno(void);
+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);
+
+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);
+
+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
+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
+isatty(int fd);
+
+char *
+realpath(const char *path, char *resolved_path);
+
+int
+posix_fallocate(int fd, off_t offset, off_t len);
+
+int
+poll(struct pollfd *fds, nfds_t nfds, int timeout);
+
+int
+getopt(int argc, char *const argv[], const char *optstring);
+
+int
+sched_yield(void);
+
+ssize_t
+getrandom(void *buf, size_t buflen, unsigned int flags);
+int
+getentropy(void *buffer, size_t length);
+
+int
+get_errno(void);
 
 #ifdef __cplusplus
 }
 #endif
 
 #endif /* end of _SGX_FILE_H */
-

+ 32 - 24
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)
-{
-}
-
+{}

+ 24 - 16
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
-

+ 11 - 7
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_wrlock(pthread_rwlock_t *rwlock);
-int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
-int pthread_rwlock_unlock(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);
 #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 */
-

+ 38 - 33
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);
-
-
-    /* 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);
-
+/* 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);
 
-    /* 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);
+/* 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);
 
 #ifdef __cplusplus
 }
 #endif
 
 #endif
-

+ 4 - 3
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
-

+ 37 - 37
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 */
-

+ 34 - 26
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_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
+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
+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
-

+ 31 - 29
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 */
-

+ 38 - 22
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;
 }
-

+ 39 - 32
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
-

+ 16 - 13
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 */
-

+ 87 - 58
core/shared/platform/linux-sgx/untrusted/file.c

@@ -19,8 +19,8 @@
 #include <poll.h>
 #include <errno.h>
 
-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;
 }

+ 11 - 7
core/shared/platform/linux-sgx/untrusted/pthread.c

@@ -6,7 +6,8 @@
 #include <stdlib.h>
 #include <pthread.h>
 
-int ocall_pthread_rwlock_init(void **rwlock, void *attr)
+int
+ocall_pthread_rwlock_init(void **rwlock, void *attr)
 {
     int ret = 0;
 
@@ -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);
 }
-

+ 2 - 1
core/shared/platform/linux-sgx/untrusted/signal.c

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

+ 17 - 15
core/shared/platform/linux-sgx/untrusted/socket.c

@@ -7,20 +7,21 @@
 #include <stdint.h>
 #include <stddef.h>
 
-int ocall_socket(int domain, int type, int protocol)
+int
+ocall_socket(int domain, int type, int protocol)
 {
     return socket(domain, type, protocol);
 }
 
-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->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);
 }

+ 14 - 17
core/shared/platform/linux-sgx/untrusted/time.c

@@ -7,41 +7,38 @@
 #include <time.h>
 #include <fcntl.h>
 
-
 /** 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);
-
 }

+ 1 - 3
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
 }
-

+ 12 - 10
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 <setjmp.h>
@@ -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 */
-

+ 1 - 1
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 */

+ 8 - 7
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 <sched.h>
 #include <thread.h>
 #include <mutex.h>
 
-//Riot includes sys
+/* Riot includes sys */
 #include <sema.h>
 
 #include <inttypes.h>
@@ -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 */
-

+ 40 - 38
core/shared/platform/riot/riot_thread.c

@@ -9,19 +9,21 @@
 
 #include <panic.h>
 
+/* 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

+ 0 - 1
core/shared/platform/riot/riot_time.c

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

+ 1 - 2
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 */

+ 68 - 55
core/shared/platform/rt-thread/rtt_platform.c

@@ -8,67 +8,65 @@
 #include <platform_api_extension.h>
 
 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)
+{}

+ 1 - 3
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
 }
-

+ 11 - 8
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 <setjmp.h>
@@ -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 */
-

+ 1 - 3
core/shared/platform/windows/platform_init.c

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

+ 13 - 9
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 <setjmp.h>
 
@@ -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 */
-

+ 0 - 3
core/shared/platform/windows/win_malloc.c

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

+ 5 - 7
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);
 }
-

+ 19 - 18
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
-

+ 0 - 1
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;
 }
-

+ 10 - 5
core/shared/platform/zephyr/platform_internal.h

@@ -37,7 +37,6 @@
 #include <arch/arm/aarch32/cortex_m/cmsis.h>
 #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

+ 6 - 8
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;
 }
-

+ 71 - 44
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
 }
-

+ 0 - 1
core/shared/platform/zephyr/zephyr_time.c

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

+ 5 - 5
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();
 }
-

+ 5 - 5
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 */
-

+ 45 - 49
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) {
-    return 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)
+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;
-  }
+    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;
+    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_strcat_s(char *s1, unsigned int s1max, const char *s2)
 {
-  if (NULL == s1 || NULL == s2
-      || s1max < (strlen(s1) + 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 + strlen(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)
+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;
-  }
+    if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)
+        || s1max > RSIZE_MAX) {
+        return -1;
+    }
 
-  memcpy(s1, s2, strlen(s2) + 1);
-  return 0;
+    memcpy(s1, s2, strlen(s2) + 1);
+    return 0;
 }
 
 char *
@@ -105,4 +102,3 @@ wa_strdup(const char *s)
     }
     return s1;
 }
-

+ 40 - 30
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_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_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);
+#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_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)
+
+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
 }

+ 14 - 19
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;

+ 7 - 11
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 */
-

+ 23 - 14
core/shared/utils/bh_list.c

@@ -14,10 +14,12 @@
  * @return        <code>true</code> if the pointer has been in the list;
  *                <code>false</code> otherwise.
  */
-static bool bh_list_is_elem_exist(bh_list *list, void *elem);
+static bool
+bh_list_is_elem_exist(bh_list *list, void *elem);
 #endif
 
-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
-

+ 20 - 13
core/shared/utils/bh_list.h

@@ -46,29 +46,34 @@ typedef enum bh_list_status {
  * @return        <code>BH_LIST_ERROR</code> if OK;
  *                <code>BH_LIST_ERROR</code> if list pointer is NULL.
  */
-bh_list_status bh_list_init(bh_list *list);
+bh_list_status
+bh_list_init(bh_list *list);
 
 /**
- * Insert an elem pointer into list. The list node memory is maintained by list while
- * 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        <code>BH_LIST_ERROR</code> if OK;
- *                <code>BH_LIST_ERROR</code> if input is invalid or no memory available.
+ *                <code>BH_LIST_ERROR</code> if input is invalid or no memory
+ * available.
  */
-extern bh_list_status bh_list_insert(bh_list *list, void *elem);
+extern bh_list_status
+bh_list_insert(bh_list *list, void *elem);
 
 /**
- * Remove an elem pointer from list. The list node memory is maintained by list while
- * 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        <code>BH_LIST_ERROR</code> if OK;
- *                <code>BH_LIST_ERROR</code> if element does not exist in given list.
+ *                <code>BH_LIST_ERROR</code> if element does not exist in given
+ * list.
  */
-bh_list_status bh_list_remove(bh_list *list, void *elem);
+bh_list_status
+bh_list_remove(bh_list *list, void *elem);
 
 /**
  * Get the list length.
@@ -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 */
-

+ 2 - 3
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;
 }
-

+ 9 - 6
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 */

+ 0 - 3
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 */
-

+ 40 - 28
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;

+ 18 - 14
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 */
-

+ 12 - 10
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;
 }

+ 0 - 1
core/shared/utils/bh_vector.h

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

+ 20 - 21
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);
 }
-

+ 26 - 16
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);
-
-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);
+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);
 
 #ifdef __cplusplus
 }

+ 14 - 9
core/shared/utils/uncommon/bh_getopt.c

@@ -9,19 +9,22 @@
 #include <stdio.h>
 #include <string.h>
 
-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++;

+ 2 - 1
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
 }

+ 7 - 11
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;
     }

+ 0 - 1
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 */
-

+ 4 - 2
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");

+ 2 - 1
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);
 }

+ 2 - 1
product-mini/app-samples/hello-world/main.c

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

+ 14 - 16
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;
 }
-

+ 15 - 14
product-mini/platforms/android/wasm-jni.cpp

@@ -7,10 +7,11 @@
 #include <wasm_export.h>
 
 #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");

+ 7 - 8
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;
     }

+ 111 - 110
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")));

+ 35 - 21
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__ */
-

+ 32 - 43
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;

+ 25 - 23
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();

Некоторые файлы не были показаны из-за большого количества измененных файлов