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

Implement stat and getentropy for sgx with ocall to run tensorflow (#436)

Wenyong Huang 5 лет назад
Родитель
Сommit
667282eea9

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

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

+ 4 - 0
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,

+ 11 - 0
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();