Skip to content

Commit 74f74b6

Browse files
qdaomingwenyongh
authored andcommitted
Fix sgx porting issues: incorrect compile flags, porting func impl, document, etc. (#145)
* Fix sgx porting issues: incorrect compilation flags, porting function impl, document, etc. * Update bh_platform.c Add check for function bh_vprintf_sgx: check whether print_function is not NULL.
1 parent a7a7d04 commit 74f74b6

File tree

12 files changed

+64
-88
lines changed

12 files changed

+64
-88
lines changed

core/iwasm/products/linux-sgx/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_definitions(-DOPS_INPUT_OUTPUT=1)
1616
add_definitions(-DOPS_UNSAFE_BUFFERS=0)
1717
add_definitions(-DWASM_ENABLE_LOG=0)
1818
add_definitions(-Dbh_printf=bh_printf_sgx)
19+
add_definitions(-Dvprintf=bh_vprintf_sgx)
1920

2021
if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
2122
add_definitions(-DNVALGRIND)
@@ -78,13 +79,17 @@ endif ()
7879
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
7980
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
8081

82+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections")
83+
8184
set (SHARED_LIB_DIR ../../../shared-lib)
8285

8386
include_directories (.
8487
../../runtime/include
8588
../../runtime/platform/include
8689
${SHARED_LIB_DIR}/include
87-
$ENV{SGX_SDK}/include)
90+
$ENV{SGX_SDK}/include
91+
$ENV{SGX_SDK}/include/tlibc
92+
$ENV{SGX_SDK}/include/libcxx)
8893

8994
enable_language (ASM)
9095

core/iwasm/products/linux-sgx/enclave-sample/Enclave/Enclave.edl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
enclave {
7+
from "sgx_tstdc.edl" import *;
8+
79
trusted {
810
/* define ECALLs here. */
911
public void ecall_iwasm_main(void);

core/iwasm/runtime/platform/linux-sgx/wasm_native.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@
1414
#include "wasm_platform_log.h"
1515
#include "bh_common.h"
1616

17-
#include <sys/ioctl.h>
18-
#include <sys/uio.h>
19-
#include <sys/syscall.h>
2017
#include <sys/types.h>
21-
#include <sys/stat.h>
2218
#include <unistd.h>
23-
#include <pwd.h>
24-
#include <fcntl.h>
2519
#include <errno.h>
2620

2721

core/shared-lib/platform/linux-sgx/bh_assert.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/* for exception throwing */
1818
jmp_buf bh_test_jb;
1919
#endif
20+
#define FIXED_BUFFER_SIZE (1<<9)
2021

2122
void bh_assert_internal(int v, const char *file_name, int line_number,
2223
const char *expr_string)
@@ -29,7 +30,7 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
2930
if (!expr_string)
3031
expr_string = "NULL EXPR_STRING";
3132

32-
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
33+
bh_printf_sgx("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
3334
file_name, line_number);
3435

3536
#ifdef BH_TEST
@@ -44,15 +45,14 @@ void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
4445
{
4546
#ifndef JEFF_TEST_VERIFIER
4647
va_list args;
48+
char msg[FIXED_BUFFER_SIZE] = { '\0' };
4749

4850
va_start(args, fmt);
49-
bh_assert(file_name);
50-
51-
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
52-
vprintf(fmt, args);
53-
51+
vsnprintf(msg, FIXED_BUFFER_SIZE, fmt, args);
5452
va_end(args);
55-
printf("\n");
53+
bh_printf_sgx("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
54+
bh_printf_sgx(msg);
55+
bh_printf_sgx("\n");
5656
#endif
5757
}
5858

core/shared-lib/platform/linux-sgx/bh_definition.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int b_strcat_s(char * s1, size_t s1max, const char * s2)
3434
return -1;
3535
}
3636

37-
strcat(s1, s2);
37+
strncat(s1, s2, strlen(s2));
3838

3939
return 0;
4040
}

core/shared-lib/platform/linux-sgx/bh_platform.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
#include "bh_common.h"
77
#include "bh_platform.h"
88

9-
#include <sys/stat.h>
10-
#include <fcntl.h>
119
#include <unistd.h>
1210

13-
#define FIXED_BUFFER_SIZE (1<<14)
11+
#define FIXED_BUFFER_SIZE (1<<9)
1412
static bh_print_function_t print_function = NULL;
1513

1614
char *bh_strdup(const char *s)
@@ -26,24 +24,6 @@ char *bh_strdup(const char *s)
2624
return s1;
2725
}
2826

29-
const unsigned short ** __ctype_b_loc(void)
30-
{
31-
/* TODO */
32-
return NULL;
33-
}
34-
35-
const int32_t ** __ctype_toupper_loc(void)
36-
{
37-
/* TODO */
38-
return NULL;
39-
}
40-
41-
const int32_t ** __ctype_tolower_loc(void)
42-
{
43-
/* TODO */
44-
return NULL;
45-
}
46-
4727
int bh_platform_init()
4828
{
4929
return 0;
@@ -77,3 +57,14 @@ int bh_printf_sgx(const char *message, ...)
7757

7858
return 0;
7959
}
60+
61+
int bh_vprintf_sgx(const char * format, va_list arg)
62+
{
63+
if (print_function != NULL) {
64+
char msg[FIXED_BUFFER_SIZE] = { '\0' };
65+
vsnprintf(msg, FIXED_BUFFER_SIZE, format, arg);
66+
print_function(msg);
67+
}
68+
69+
return 0;
70+
}

core/shared-lib/platform/linux-sgx/bh_platform.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
#include <math.h>
2020
#include <stdarg.h>
2121
#include <ctype.h>
22-
#include <pthread.h>
2322
#include <limits.h>
24-
#include <fcntl.h>
2523
#include <errno.h>
24+
#include <sgx_thread.h>
2625

2726
#ifdef __cplusplus
2827
extern "C" {
2928
#endif
3029

3130
extern int bh_printf_sgx(const char *message, ...);
31+
extern int bh_vprintf_sgx(const char * format, va_list arg);
3232

3333
typedef uint64_t uint64;
3434
typedef int64_t int64;
@@ -53,12 +53,12 @@ typedef int64_t int64;
5353

5454
#define INVALID_THREAD_ID 0xFFffFFff
5555

56-
typedef int korp_tid;
57-
typedef int korp_mutex;
5856
typedef int korp_sem;
59-
typedef int korp_cond;
60-
typedef int korp_thread;
6157
typedef void* (*thread_start_routine_t)(void*);
58+
typedef sgx_thread_mutex_t korp_mutex;
59+
typedef sgx_thread_t korp_tid;
60+
typedef sgx_thread_t korp_thread;
61+
typedef sgx_thread_cond_t korp_cond;
6262

6363
#define wa_malloc bh_malloc
6464
#define wa_free bh_free

core/shared-lib/platform/linux-sgx/bh_platform_log.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@
88

99
void bh_log_emit(const char *fmt, va_list ap)
1010
{
11-
vprintf(fmt, ap);
12-
fflush(stdout);
11+
//TODO: stub impl
1312
}
1413

14+
/*
1515
int bh_fprintf(FILE *stream, const char *fmt, ...)
1616
{
17-
va_list ap;
18-
int ret;
19-
20-
va_start(ap, fmt);
21-
ret = vfprintf(stream ? stream : stdout, fmt, ap);
22-
va_end(ap);
23-
24-
return ret;
17+
return 0;
2518
}
19+
*/
2620

2721
int bh_fflush(void *stream)
2822
{
29-
return fflush(stream ? stream : stdout);
23+
//TODO: stub impl
24+
return 0;
3025
}

core/shared-lib/platform/linux-sgx/bh_thread.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "bh_memory.h"
99
#include <stdio.h>
1010
#include <stdlib.h>
11-
#include <sys/time.h>
11+
#include <sgx_thread.h>
1212

1313
int _vm_thread_sys_init()
1414
{
@@ -35,15 +35,15 @@ int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
3535

3636
korp_tid _vm_self_thread()
3737
{
38-
return 0;
38+
return sgx_thread_self();
3939
}
4040

4141
void vm_thread_exit(void * code)
4242
{
4343
}
4444

4545
// storage for one thread
46-
static void *_tls_store = NULL;
46+
static __thread void *_tls_store = NULL;
4747

4848
void *_vm_tls_get(unsigned idx)
4949
{
@@ -59,20 +59,22 @@ int _vm_tls_put(unsigned idx, void * tls)
5959

6060
int _vm_mutex_init(korp_mutex *mutex)
6161
{
62+
sgx_thread_mutex_t m = SGX_THREAD_MUTEX_INITIALIZER;
63+
*mutex = m;
6264
return BHT_OK;
63-
//return BHT_ERROR;
6465
}
6566

6667
int _vm_recursive_mutex_init(korp_mutex *mutex)
6768
{
69+
sgx_thread_mutex_t m = SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER;
70+
*mutex = m;
6871
return BHT_OK;
69-
//return BHT_ERROR;
7072
}
7173

7274
int _vm_mutex_destroy(korp_mutex *mutex)
7375
{
76+
sgx_thread_mutex_destroy(mutex);
7477
return BHT_OK;
75-
//return BHT_ERROR;
7678
}
7779

7880
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
@@ -81,12 +83,12 @@ int _vm_mutex_destroy(korp_mutex *mutex)
8183
Don't try to recover error for an existing unknown error.*/
8284
void vm_mutex_lock(korp_mutex *mutex)
8385
{
86+
sgx_thread_mutex_lock(mutex);
8487
}
8588

8689
int vm_mutex_trylock(korp_mutex *mutex)
8790
{
88-
return BHT_OK;
89-
//return BHT_ERROR;
91+
return (sgx_thread_mutex_trylock(mutex) == 0? BHT_OK: BHT_ERROR);
9092
}
9193

9294
/* Returned error (EINVAL, EAGAIN and EPERM) from
@@ -95,6 +97,7 @@ int vm_mutex_trylock(korp_mutex *mutex)
9597
Don't try to recover error for an existing unknown error.*/
9698
void vm_mutex_unlock(korp_mutex *mutex)
9799
{
100+
sgx_thread_mutex_unlock(mutex);
98101
}
99102

100103
int _vm_sem_init(korp_sem* sem, unsigned int c)

core/shared-lib/platform/linux-sgx/bh_time.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <unistd.h>
99
#include <stdio.h>
10-
#include <sys/timeb.h>
1110
#include <time.h>
1211

1312
/*
@@ -16,7 +15,8 @@
1615
*/
1716
uint64 _bh_time_get_tick_millisecond()
1817
{
19-
return sysconf(_SC_CLK_TCK);
18+
//TODO:
19+
return 0;
2020
}
2121

2222
/*
@@ -25,17 +25,14 @@ uint64 _bh_time_get_tick_millisecond()
2525
*/
2626
uint64 _bh_time_get_boot_millisecond()
2727
{
28-
struct timespec ts;
29-
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
30-
return 0;
31-
}
32-
33-
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
28+
//TODO
29+
return 0;
3430
}
3531

3632
uint32 bh_get_tick_sec()
3733
{
38-
return _bh_time_get_boot_millisecond() / 1000;
34+
//TODO
35+
return 0;
3936
}
4037

4138
/*
@@ -44,26 +41,13 @@ uint32 bh_get_tick_sec()
4441
*/
4542
uint64 _bh_time_get_millisecond_from_1970()
4643
{
47-
struct timeb tp;
48-
ftime(&tp);
49-
50-
return ((uint64) tp.time) * 1000 + tp.millitm
51-
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
44+
//TODO
45+
return 0;
5246
}
5347

5448
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
5549
{
56-
time_t time_sec = time / 1000;
57-
struct timeb tp;
58-
struct tm *ltp;
59-
60-
ftime(&tp);
61-
time_sec -= tp.timezone * 60;
62-
63-
ltp = localtime(&time_sec);
64-
if (ltp == NULL) {
65-
return 0;
66-
}
67-
return strftime(s, max, format, ltp);
50+
//TODO
51+
return 0;
6852
}
6953

0 commit comments

Comments
 (0)