forked from sslab-gatech/opensgx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgx-runtime.c
97 lines (83 loc) · 2.55 KB
/
sgx-runtime.c
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
/*
* Copyright (C) 2015, OpenSGX team, Georgia Tech & KAIST, All Rights Reserved
*
* This file is part of OpenSGX (https://github.com/sslab-gatech/opensgx).
*
* OpenSGX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSGX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSGX. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <sgx-kern.h>
#include <sgx-user.h>
#include <sgx-utils.h>
#include <sgx-crypto.h>
#include <sgx-loader.h>
#include <sgx-trampoline.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <sgx-malloc.h>
#include <err.h>
#include <string.h>
#include <fcntl.h>
#define is_aligned(addr, bytes) \
((((uintptr_t)(const void *)(addr)) & (bytes - 1)) == 0)
ENCCALL2(enclave2_call, int, char **)
int main(int argc, char **argv)
{
char *binary;
char *conf;
void *entry;
void *base_addr;
size_t npages;
unsigned long entry_offset;
int toff;
if (argc < 1) {
err(1, "Please specifiy binary to load\n");
}
binary = argv[1];
// handling for enclave argc and argv
if (argc > 2) {
if ( (strstr(argv[1], ".sgx") != NULL) && (strstr(argv[2], ".conf") != NULL) ) {
conf = argv[2];
} else {
conf = NULL;
}
} else {
conf = NULL;
}
if(!sgx_init())
err(1, "failed to init sgx");
base_addr = load_elf_enclave(binary, &npages, &entry, &toff);
if (base_addr == NULL) {
err(1, "Please provide valid binary/configuration files.");
}
entry_offset = (uint64_t)entry - (uint64_t)base_addr;
tcs_t *tcs = init_enclave(base_addr, entry_offset, npages, conf);
if (!tcs)
err(1, "failed to run enclave");
void (*aep)() = exception_handler;
int test = 0;
if (argc == 2)
sgx_enter(tcs, aep);
else if (argc == 3) {
if ((strstr(argv[1], ".sgx") != NULL) && (strstr(argv[2], ".conf") != NULL))
sgx_enter(tcs, aep);
else {
enclave2_call(tcs, aep, argc, argv);
}
}
else
enclave2_call(tcs, aep, argc, argv);
return 0;
}