summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/platform/linux-sgx/sgx_file.c
blob: a8ae8d2f9a4bcdc7c83e08f70106e0df2efbb8f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "platform_api_vmcore.h"
#include "sgx_error.h"
#include "sgx_file.h"

#if WASM_ENABLE_SGX_IPFS != 0
#include "sgx_ipfs.h"
#endif

#ifndef SGX_DISABLE_WASI

#define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__)
#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_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_close(int *p_ret, int fd);

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_fsync(int *p_ret, int fd);

int
ocall_fdatasync(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_readdir(void **p_dirent, void *dirp);

int
ocall_rewinddir(void *dirp);

int
ocall_seekdir(void *dirp, long loc);

int
ocall_telldir(long *p_dir, 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);
/** 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_readlink(ssize_t *p_ret, const char *pathname, char *buf, size_t bufsiz);
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);
/** 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_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);
/** iovec end **/

int
ocall_get_errno(int *p_ret);

int
open(const char *pathname, int flags, ...)
{
    int fd;
    bool has_mode = false;
    mode_t mode = 0;

    if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
        va_list ap;
        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);
        has_mode = true;
    }

    if (SGX_SUCCESS != ocall_open(&fd, pathname, flags, has_mode, mode)) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (fd >= 0 && (flags & O_CLOEXEC))
        fcntl(fd, F_SETFD, FD_CLOEXEC);

    if (fd == -1)
        errno = get_errno();
    return fd;
}

int
openat(int dirfd, const char *pathname, int flags, ...)
{
    int fd;
    bool has_mode = false;
    mode_t mode = 0;

    if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
        va_list ap;
        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);
        has_mode = true;
    }

    if (SGX_SUCCESS
        != ocall_openat(&fd, dirfd, pathname, flags, has_mode, mode)) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (fd >= 0 && (flags & O_CLOEXEC))
        fcntl(fd, F_SETFD, FD_CLOEXEC);

    if (fd == -1)
        errno = get_errno();

#if WASM_ENABLE_SGX_IPFS != 0
    struct stat sb;
    int ret = fstatat(dirfd, pathname, &sb, 0);
    if (ret < 0) {
        if (ocall_close(&ret, fd) != SGX_SUCCESS) {
            TRACE_OCALL_FAIL();
        }
        return -1;
    }

    // Ony files are managed by SGX IPFS
    if (S_ISREG(sb.st_mode)) {
        // When WAMR uses Intel SGX IPFS to enabled, it opens a second
        // file descriptor to interact with the secure file.
        // The first file descriptor opened earlier is used to interact
        // with the metadata of the file (e.g., time, flags, etc.).
        void *file_ptr = ipfs_fopen(fd, flags);
        if (file_ptr == NULL) {
            if (ocall_close(&ret, fd) != SGX_SUCCESS) {
                TRACE_OCALL_FAIL();
            }
            return -1;
        }
    }
#endif

    return fd;
}

int
close(int fd)
{
    int ret;

#if WASM_ENABLE_SGX_IPFS != 0
    // Close the IPFS file pointer in addition of the file descriptor
    ret = ipfs_close(fd);
    if (ret == -1)
        errno = get_errno();
#endif

    if (ocall_close(&ret, fd) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
    return ret;
}

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;
    char *p = buf;

    if (buf == NULL) {
        TRACE_FUNC();
        return -1;
    }

    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;

        if (ocall_read(&ret, fd, p, size_read) != SGX_SUCCESS) {
            TRACE_OCALL_FAIL();
            return -1;
        }
        if (ret == -1) {
            /* read failed */
            errno = get_errno();
            return -1;
        }

        p += ret;
        total_size_read += ret;

        if (ret < size_read)
            /* end of file */
            break;
    }
    return total_size_read;
}

DIR *
fdopendir(int fd)
{
    DIR *result = NULL;

    result = (DIR *)BH_MALLOC(sizeof(DIR));
    if (!result)
        return NULL;

    if (ocall_fdopendir(fd, (void **)result) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        BH_FREE(result);
        return NULL;
    }

    if ((void *)*result == NULL) { /* opendir failed */
        TRACE_FUNC();
        BH_FREE(result);
        errno = get_errno();
        return NULL;
    }

    return result;
}

struct dirent *
readdir(DIR *dirp)
{
    struct dirent *result;

    if (dirp == NULL)
        return NULL;

    if (ocall_readdir((void **)&result, (void *)*dirp) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return NULL;
    }

    if (!result)
        errno = get_errno();
    return result;
}

void
rewinddir(DIR *dirp)
{
    if (ocall_rewinddir((void *)*dirp) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
    }
}

void
seekdir(DIR *dirp, long loc)
{
    if (ocall_seekdir((void *)*dirp, loc) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
    }
}

long
telldir(DIR *dirp)
{
    long ret;

    if (ocall_telldir(&ret, (void *)*dirp) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
closedir(DIR *dirp)
{
    int ret;

    if (ocall_closedir(&ret, (void *)*dirp) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    BH_FREE(dirp);
    if (ret == -1)
        errno = get_errno();
    return ret;
}

static ssize_t
readv_internal(int fd, const struct iovec *iov, int iovcnt, bool has_offset,
               off_t offset)
{
    ssize_t ret, size_left;
    struct iovec *iov1;
    int i;
    char *p;
    uint64 total_size = sizeof(struct iovec) * (uint64)iovcnt;

    if (iov == NULL || iovcnt < 1)
        return -1;

    for (i = 0; i < iovcnt; i++) {
        total_size += iov[i].iov_len;
    }

    if (total_size >= UINT32_MAX)
        return -1;

#if WASM_ENABLE_SGX_IPFS != 0
    if (fd > 2) {
        return ipfs_read(fd, iov, iovcnt, has_offset, offset);
    }
#endif

    iov1 = BH_MALLOC((uint32)total_size);

    if (iov1 == NULL)
        return -1;

    memset(iov1, 0, (uint32)total_size);

    p = (char *)(uintptr_t)(sizeof(struct iovec) * iovcnt);

    for (i = 0; i < iovcnt; i++) {
        iov1[i].iov_len = iov[i].iov_len;
        iov1[i].iov_base = p;
        p += iov[i].iov_len;
    }

    if (ocall_readv(&ret, fd, (char *)iov1, (uint32)total_size, iovcnt,
                    has_offset, offset)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        BH_FREE(iov1);
        return -1;
    }

    p = (char *)(uintptr_t)(sizeof(struct iovec) * iovcnt);

    size_left = ret;
    for (i = 0; i < iovcnt; i++) {
        if (size_left > iov[i].iov_len) {
            memcpy(iov[i].iov_base, (uintptr_t)p + (char *)iov1,
                   iov[i].iov_len);
            p += iov[i].iov_len;
            size_left -= iov[i].iov_len;
        }
        else {
            memcpy(iov[i].iov_base, (uintptr_t)p + (char *)iov1, size_left);
            break;
        }
    }

    BH_FREE(iov1);
    if (ret == -1)
        errno = get_errno();
    return ret;
}

static ssize_t
writev_internal(int fd, const struct iovec *iov, int iovcnt, bool has_offset,
                off_t offset)
{
    ssize_t ret;
    struct iovec *iov1;
    int i;
    char *p;
    uint64 total_size = sizeof(struct iovec) * (uint64)iovcnt;

    if (iov == NULL || iovcnt < 1)
        return -1;

    for (i = 0; i < iovcnt; i++) {
        total_size += iov[i].iov_len;
    }

    if (total_size >= UINT32_MAX)
        return -1;

#if WASM_ENABLE_SGX_IPFS != 0
    if (fd > 2) {
        return ipfs_write(fd, iov, iovcnt, has_offset, offset);
    }
#endif

    iov1 = BH_MALLOC((uint32)total_size);

    if (iov1 == NULL)
        return -1;

    memset(iov1, 0, (uint32)total_size);

    p = (char *)(uintptr_t)(sizeof(struct iovec) * 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);
        p += iov[i].iov_len;
    }

    if (ocall_writev(&ret, fd, (char *)iov1, (uint32)total_size, iovcnt,
                     has_offset, offset)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        BH_FREE(iov1);
        return -1;
    }

    BH_FREE(iov1);
    if (ret == -1)
        errno = get_errno();
    return ret;
}

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)
{
    return writev_internal(fd, iov, iovcnt, false, 0);
}

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)
{
    return writev_internal(fd, iov, iovcnt, true, offset);
}

off_t
lseek(int fd, off_t offset, int whence)
{
    off_t ret;

#if WASM_ENABLE_SGX_IPFS != 0
    ret = ipfs_lseek(fd, offset, whence);
#else
    if (ocall_lseek(&ret, fd, (long)offset, whence) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
#endif

    return ret;
}

int
ftruncate(int fd, off_t length)
{
    int ret;

#if WASM_ENABLE_SGX_IPFS != 0
    ret = ipfs_ftruncate(fd, length);
#else
    if (ocall_ftruncate(&ret, fd, length) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
#endif

    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;

    if (statbuf == NULL)
        return -1;

    if (ocall_fstat(&ret, fd, (void *)statbuf, sizeof(struct stat))
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags)
{
    int ret;

    if (statbuf == NULL)
        return -1;

    if (ocall_fstatat(&ret, dirfd, pathname, (void *)statbuf,
                      sizeof(struct stat), flags)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
fsync(int fd)
{
    int ret;

#if WASM_ENABLE_SGX_IPFS != 0
    ret = ipfs_fflush(fd);
#else
    if (ocall_fsync(&ret, fd) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
#endif

    return ret;
}

int
fdatasync(int fd)
{
    int ret;

#if WASM_ENABLE_SGX_IPFS != 0
    ret = ipfs_fflush(fd);
#else
    if (ocall_fdatasync(&ret, fd) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
#endif

    return ret;
}

int
mkdirat(int dirfd, const char *pathname, mode_t mode)
{
    int ret;

    if (ocall_mkdirat(&ret, dirfd, pathname, mode) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
link(const char *oldpath, const char *newpath)
{
    int ret;

    if (ocall_link(&ret, oldpath, newpath) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

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) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
unlinkat(int dirfd, const char *pathname, int flags)
{
    int ret;

    if (ocall_unlinkat(&ret, dirfd, pathname, flags) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

ssize_t
readlink(const char *pathname, char *buf, size_t bufsiz)
{
    ssize_t ret;

    if (buf == NULL)
        return -1;

    if (ocall_readlink(&ret, pathname, buf, bufsiz) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

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) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
symlinkat(const char *target, int newdirfd, const char *linkpath)
{
    int ret;

    if (ocall_symlinkat(&ret, target, newdirfd, linkpath) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
{
    int ret;

    if (ocall_renameat(&ret, olddirfd, oldpath, newdirfd, newpath)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
ioctl(int fd, unsigned long request, ...)
{
    int ret;
    va_list args;

    switch (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) {
                TRACE_OCALL_FAIL();
                va_end(args);
                return -1;
            }
            va_end(args);
            break;

        default:
            os_printf("ioctl failed: unknown request", request);
            return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
fcntl(int fd, int cmd, ... /* arg */)
{
    int ret;
    va_list args;

    switch (cmd) {
        case F_GETFD:
        case F_GETFL:
            if (ocall_fcntl(&ret, fd, cmd) != SGX_SUCCESS) {
                TRACE_OCALL_FAIL();
                return -1;
            }
            break;

        case F_DUPFD:
        case F_SETFD:
        case F_SETFL:
            va_start(args, cmd);
            long arg_1 = (long)va_arg(args, long);
            if (ocall_fcntl_long(&ret, fd, cmd, arg_1) != SGX_SUCCESS) {
                TRACE_OCALL_FAIL();
                va_end(args);
                return -1;
            }
            va_end(args);
            break;

        default:
            os_printf("fcntl failed: unknown cmd %d.\n", cmd);
            return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
isatty(int fd)
{
    int ret;

    if (ocall_isatty(&ret, fd) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == 0)
        errno = get_errno();
    return ret;
}

char *
realpath(const char *path, char *resolved_path)
{
    int ret;
    char buf[PATH_MAX] = { 0 };

    if (ocall_realpath(&ret, path, buf, PATH_MAX) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return (char *)NULL;
    }

    if (ret != 0)
        return (char *)NULL;

    if (resolved_path) {
        strcpy(resolved_path, buf);
    }
    else {
        resolved_path = BH_MALLOC(strlen(buf) + 1);
        if (resolved_path == NULL)
            return NULL;
        strcpy(resolved_path, buf);
    }

    return resolved_path;
}

int
posix_fallocate(int fd, off_t offset, off_t len)
{
    int ret;

#if WASM_ENABLE_SGX_IPFS != 0
    ret = ipfs_posix_fallocate(fd, offset, len);
#else
    if (ocall_posix_fallocate(&ret, fd, offset, len) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
#endif

    return ret;
}

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) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
getopt(int argc, char *const argv[], const char *optstring)
{
    int ret;
    char **argv1;
    char *p;
    int i;
    uint64 total_size = sizeof(char *) * (uint64)argc;

    for (i = 0; i < argc; i++) {
        total_size += strlen(argv[i]) + 1;
    }

    if (total_size >= UINT32_MAX)
        return -1;

    argv1 = BH_MALLOC((uint32)total_size);

    if (argv1 == NULL)
        return -1;

    p = (char *)(uintptr_t)(sizeof(char *) * argc);

    for (i = 0; i < argc; i++) {
        argv1[i] = p;
        strcpy((char *)argv1 + (uintptr_t)p, argv[i]);
        p += ((uintptr_t)strlen(argv[i]) + 1);
    }

    if (ocall_getopt(&ret, argc, (char *)argv1, total_size, optstring)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        BH_FREE(argv1);
        return -1;
    }

    BH_FREE(argv1);
    if (ret == -1)
        errno = get_errno();
    return ret;
}

int
sched_yield(void)
{
    int ret;

    if (ocall_sched_yield(&ret) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    if (ret == -1)
        errno = get_errno();
    return ret;
}

ssize_t
getrandom(void *buf, size_t buflen, unsigned int flags)
{
    sgx_status_t ret;

    if (!buf || buflen > INT32_MAX || flags != 0) {
        errno = EINVAL;
        return -1;
    }

    ret = sgx_read_rand(buf, buflen);
    if (ret != SGX_SUCCESS) {
        errno = EFAULT;
        return -1;
    }

    return (ssize_t)buflen;
}

#define RDRAND_RETRIES 3

static int
rdrand64_step(uint64 *seed)
{
    uint8 ok;
    __asm__ volatile("rdseed %0; setc %1" : "=r"(*seed), "=qm"(ok));
    return (int)ok;
}

static int
rdrand64_retry(uint64 *rand, uint32 retries)
{
    uint32 count = 0;

    while (count++ <= retries) {
        if (rdrand64_step(rand)) {
            return -1;
        }
    }
    return 0;
}

static uint32
rdrand_get_bytes(uint8 *dest, uint32 n)
{
    uint8 *head_start = dest, *tail_start = NULL;
    uint64 *block_start;
    uint32 count, ltail, lhead, lblock;
    uint64 i, temp_rand;

    /* Get the address of the first 64-bit aligned block in the
       destination buffer. */
    if (((uintptr_t)head_start & (uintptr_t)7) == 0) {
        /* already 8-byte aligned */
        block_start = (uint64 *)head_start;
        lhead = 0;
        lblock = n & ~7;
    }
    else {
        /* next 8-byte aligned */
        block_start = (uint64 *)(((uintptr_t)head_start + 7) & ~(uintptr_t)7);
        lhead = (uint32)((uintptr_t)block_start - (uintptr_t)head_start);
        lblock = (n - lhead) & ~7;
    }

    /* Compute the number of 64-bit blocks and the remaining number
       of bytes (the tail) */
    ltail = n - lblock - lhead;
    if (ltail > 0) {
        tail_start = (uint8 *)block_start + lblock;
    }

    /* Populate the starting, mis-aligned section (the head) */
    if (lhead > 0) {
        if (!rdrand64_retry(&temp_rand, RDRAND_RETRIES)) {
            return 0;
        }
        memcpy(head_start, &temp_rand, lhead);
    }

    /* Populate the central, aligned blocks */
    count = lblock / 8;
    for (i = 0; i < count; i++, block_start++) {
        if (!rdrand64_retry(block_start, RDRAND_RETRIES)) {
            return i * 8 + lhead;
        }
    }

    /* Populate the tail */
    if (ltail > 0) {
        if (!rdrand64_retry(&temp_rand, RDRAND_RETRIES)) {
            return count * 8 + lhead;
        }

        memcpy(tail_start, &temp_rand, ltail);
    }

    return n;
}

int
getentropy(void *buffer, size_t length)
{
    uint32 size;

    if (!buffer || length > INT32_MAX) {
        errno = EINVAL;
        return -1;
    }

    if (length == 0) {
        return 0;
    }

    size = rdrand_get_bytes(buffer, (uint32)length);
    if (size != length) {
        errno = EFAULT;
        return -1;
    }

    return 0;
}

int
get_errno(void)
{
    int ret;

    if (ocall_get_errno(&ret) != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }
    return ret;
}

#endif