From 667282eea94066e5ed361b1e5a8e18e76aad0a91 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 30 Oct 2020 12:36:00 +0800 Subject: [PATCH] Implement stat and getentropy for sgx with ocall to run tensorflow (#436) --- core/shared/platform/linux-sgx/sgx_file.c | 39 ++++++++++++++++++- core/shared/platform/linux-sgx/sgx_file.h | 2 + core/shared/platform/linux-sgx/sgx_wamr.edl | 4 ++ .../platform/linux-sgx/untrusted/file.c | 11 ++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/core/shared/platform/linux-sgx/sgx_file.c b/core/shared/platform/linux-sgx/sgx_file.c index 1b34ea50..4e21b8b4 100644 --- a/core/shared/platform/linux-sgx/sgx_file.c +++ b/core/shared/platform/linux-sgx/sgx_file.c @@ -49,9 +49,11 @@ 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_fstatat(int *p_ret, int dirfd, const char *pathname, + void *buf, unsigned int buf_len, int flags); /** stat end **/ /** link **/ @@ -87,6 +89,7 @@ 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 **/ @@ -449,6 +452,25 @@ int ftruncate(int fd, off_t length) return ret; } +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) { + TRACE_OCALL_FAIL(); + return -1; + } + + if (ret == -1) + errno = get_errno(); + return ret; +} + int fstat(int fd, struct stat *statbuf) { int ret; @@ -822,6 +844,19 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) return ret; } +int getentropy(void *buffer, size_t length) +{ + int ret; + + if (ocall_getentropy(&ret, buffer, length) != SGX_SUCCESS) { + TRACE_OCALL_FAIL(); + return -1; + } + if (ret == -1) + errno = get_errno(); + return ret; +} + int get_errno(void) { int ret; diff --git a/core/shared/platform/linux-sgx/sgx_file.h b/core/shared/platform/linux-sgx/sgx_file.h index 7d046766..178e6e7f 100644 --- a/core/shared/platform/linux-sgx/sgx_file.h +++ b/core/shared/platform/linux-sgx/sgx_file.h @@ -183,6 +183,7 @@ ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, 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); @@ -218,6 +219,7 @@ int getopt(int argc, char * const argv[], 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); diff --git a/core/shared/platform/linux-sgx/sgx_wamr.edl b/core/shared/platform/linux-sgx/sgx_wamr.edl index 6eb17172..b8985396 100644 --- a/core/shared/platform/linux-sgx/sgx_wamr.edl +++ b/core/shared/platform/linux-sgx/sgx_wamr.edl @@ -30,6 +30,9 @@ enclave { long ocall_telldir([user_check]void *dirp); int ocall_closedir([user_check]void *dirp); + int ocall_stat([in, string]const char *pathname, + [out, size=buf_len]void *buf, + unsigned int buf_len); int ocall_fstat(int fd, [out, size=buf_len]void *buf, unsigned int buf_len); int ocall_fstatat(int dirfd, [in, string]const char *pathname, @@ -76,6 +79,7 @@ enclave { [in, string]const char *optstring); ssize_t ocall_getrandom([out, size=buflen]void *buf, size_t buflen, unsigned int flags); + int ocall_getentropy([out, size=length]void *buffer, size_t length); ssize_t ocall_readv(int fd, [in, out, size=buf_size]char *iov_buf, unsigned int buf_size, int iovcnt, diff --git a/core/shared/platform/linux-sgx/untrusted/file.c b/core/shared/platform/linux-sgx/untrusted/file.c index d19c8eda..6852f222 100644 --- a/core/shared/platform/linux-sgx/untrusted/file.c +++ b/core/shared/platform/linux-sgx/untrusted/file.c @@ -130,6 +130,12 @@ int ocall_closedir(void* dirp) return -1; } +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) { return fstat(fd, (struct stat *)buf); @@ -277,6 +283,11 @@ 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) +{ + return getentropy(buffer, length); +} + int ocall_sched_yield() { return sched_yield();