forked from openenclave/oeedger8r-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f_emitter.h
339 lines (312 loc) · 11.5 KB
/
f_emitter.h
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
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#ifndef F_EMITTER_H
#define F_EMITTER_H
#include <fstream>
#include "ast.h"
#include "utils.h"
class FEmitter
{
Edl* edl_;
std::ofstream& file_;
bool ecall_;
public:
typedef FEmitter& R;
R out()
{
return *this;
}
template <typename T>
R operator<<(const T& t)
{
file_ << t << "\n";
return out();
}
public:
FEmitter(Edl* edl, std::ofstream& file)
: edl_(edl), file_(file), ecall_(true)
{
(void)edl_;
}
void emit(Function* f, bool ecall)
{
ecall_ = ecall;
std::string pfx = ecall_ ? "ecall_" : "ocall_";
std::string args_t = f->name_ + "_args_t";
out() << "static void " + pfx + f->name_ + "("
<< " uint8_t* input_buffer,"
<< " size_t input_buffer_size,"
<< " uint8_t* output_buffer,"
<< " size_t output_buffer_size,"
<< " size_t* output_bytes_written)"
<< "{"
<< " oe_result_t _result = OE_FAILURE;";
if (!ecall_)
out() << " OE_UNUSED(input_buffer_size);";
out() << ""
<< " /* Prepare parameters. */"
<< " " + args_t + "* pargs_in = (" + args_t + "*)input_buffer;"
<< " " + args_t + "* pargs_out = (" + args_t +
"*)output_buffer;"
<< ""
<< " size_t input_buffer_offset = 0;"
<< " size_t output_buffer_offset = 0;"
<< " OE_ADD_SIZE(input_buffer_offset, sizeof(*pargs_in));"
<< " OE_ADD_SIZE(output_buffer_offset, sizeof(*pargs_out));"
<< "";
if (ecall_)
ecall_buffer_checks();
else
ocall_buffer_checks();
out() << " /* Set in and in-out pointers. */";
set_in_in_out_pointers(f);
out() << " /* Set out and in-out pointers. */"
<< " /* In-out parameters are copied to output buffer. */";
set_out_in_out_pointers(f);
if (ecall)
{
out() << " /* Check that in/in-out strings are null terminated. "
"*/";
check_null_terminators(f);
out() << " /* lfence after checks. */"
<< " oe_lfence();"
<< "";
}
out() << " /* Call user function. */";
call_user_function(f);
propagate_errno(f);
out() << " /* Success. */"
<< " _result = OE_OK;"
<< " *output_bytes_written = output_buffer_offset;"
<< ""
<< "done:";
write_result();
out() << "}"
<< "";
}
void ecall_buffer_checks()
{
out() << " /* Make sure input and output buffers lie within the "
"enclave. */"
<< " /* oe_is_within_enclave explicitly checks if buffers "
"are null or not. */"
<< " if (!oe_is_within_enclave(input_buffer, "
"input_buffer_size))"
<< " goto done;"
<< ""
<< " if (!oe_is_within_enclave(output_buffer, "
"output_buffer_size))"
<< " goto done;"
<< "";
}
void ocall_buffer_checks()
{
out() << " /* Make sure input and output buffers are valid. */"
<< " if (!input_buffer || !output_buffer) {"
<< " _result = OE_INVALID_PARAMETER;"
<< " goto done;"
<< " }"
<< "";
}
void set_pointers_deep_copy(
const std::string& parent_condition,
const std::string& parent_expr,
const std::string& cmd,
Decl* parent_prop,
int level,
std::string indent = " ",
bool is_out = false)
{
UserType* ut = get_user_type_for_deep_copy(edl_, parent_prop);
if (!ut)
return;
iterate_deep_copyable_fields(ut, [&](Decl* prop) {
std::string op = *parent_expr.rbegin() == ']' ? "." : "->";
std::string expr = parent_expr + op + prop->name_;
std::string size = psize(prop, "pargs_in->" + parent_expr + op);
std::string cond = parent_condition + " && " + (is_out ? "!" : "") +
"pargs_in->" + expr;
std::string mt = mtype_str(prop);
out() << indent + "if (" + cond + ")"
<< indent + " " + cmd + "(" + expr + ", " + size + ", " +
mt + ");";
UserType* ut = get_user_type_for_deep_copy(edl_, prop);
if (!ut)
return;
std::string count =
count_attr_str(prop->attrs_->count_, "pargs_in->");
if (count == "1" || count == "")
{
set_pointers_deep_copy(
cond, expr, cmd, prop, level + 1, indent, is_out);
}
else
{
std::string idx = "_i_" + to_str(level);
std::string expr =
parent_expr + op + prop->name_ + "[" + idx + "]";
out() << indent + "for (size_t " + idx + " = 0; " + idx +
" < " + count + "; " + idx + "++)"
<< indent + "{";
std::string cond = parent_condition + " && pargs_in->" +
parent_expr + op + prop->name_;
set_pointers_deep_copy(
cond, expr, cmd, prop, level + 1, indent + " ", is_out);
out() << indent + "}";
}
});
}
void set_in_in_out_pointers(Function* f)
{
bool empty = true;
for (Decl* p : f->params_)
{
if (!p->attrs_ || !(p->attrs_->in_ || p->attrs_->inout_))
continue;
std::string size = psize(p, "pargs_in->");
std::string cmd = (p->attrs_->inout_) ? "OE_SET_IN_OUT_POINTER"
: "OE_SET_IN_POINTER";
out() << " if (pargs_in->" + p->name_ + ")"
<< " " + cmd + "(" + p->name_ + ", " + size + ", " +
mtype_str(p) + ");";
empty = false;
UserType* ut = get_user_type_for_deep_copy(edl_, p);
if (!ut)
continue;
std::string count = count_attr_str(p->attrs_->count_, "pargs_in->");
if (count == "1" || count == "")
{
std::string cond = "pargs_in->" + p->name_;
std::string expr = p->name_;
set_pointers_deep_copy(cond, expr, cmd, p, 2, " ");
}
else
{
std::string cond = "pargs_in->" + p->name_;
std::string expr = p->name_ + "[_i_1]";
out() << " for (size_t _i_1 = 0; _i_1 < " + count +
"; _i_1++)"
<< " {";
set_pointers_deep_copy(cond, expr, cmd, p, 2, " ");
out() << " }";
}
}
if (empty)
out() << " /* There were no in nor in-out parameters. */";
out() << "";
}
void set_out_in_out_pointers(Function* f)
{
bool empty = true;
for (Decl* p : f->params_)
{
if (!p->attrs_ || !(p->attrs_->out_ || p->attrs_->inout_))
continue;
std::string size = psize(p, "pargs_in->");
std::string cmd = (p->attrs_->inout_)
? "OE_COPY_AND_SET_IN_OUT_POINTER"
: "OE_SET_OUT_POINTER";
out() << " if (pargs_in->" + p->name_ + ")"
<< " " + cmd + "(" + p->name_ + ", " + size + ", " +
mtype_str(p) + ");";
empty = false;
UserType* ut = get_user_type_for_deep_copy(edl_, p);
if (!ut)
continue;
std::string count = count_attr_str(p->attrs_->count_, "pargs_in->");
if (count == "1" || count == "")
{
std::string cond = "pargs_in->" + p->name_;
std::string expr = p->name_;
set_pointers_deep_copy(
cond, expr, cmd, p, 2, " ", p->attrs_->out_);
}
else
{
std::string cond = "pargs_in->" + p->name_;
std::string expr = p->name_ + "[_i_1]";
out() << " for (size_t _i_1 = 0; _i_1 < " + count +
"; _i_1++)"
<< " {";
set_pointers_deep_copy(
cond, expr, cmd, p, 2, " ", p->attrs_->out_);
out() << " }";
}
}
if (empty)
out() << " /* There were no out nor in-out parameters. */";
out() << "";
}
void check_null_terminators(Function* f)
{
std::string check = "OE_CHECK_NULL_TERMINATOR";
bool strs = false;
for (Decl* p : f->params_)
{
if (!p->attrs_ || !(p->attrs_->string_ || p->attrs_->wstring_))
continue;
out() << " " + check + (p->attrs_->wstring_ ? "_WIDE" : "") +
"(pargs_in->" + p->name_ + ", pargs_in->" + p->name_ +
"_len);";
strs = true;
}
if (!strs)
out() << " /* There were no in nor in-out string parameters. */";
out() << "";
}
void call_user_function(Function* f)
{
std::string retstr =
(f->rtype_->tag_ != Void) ? "pargs_out->_retval = " : "";
out() << " " + retstr + f->name_ + "(";
size_t idx = 0;
for (Decl* p : f->params_)
{
std::string type = "";
if (p->dims_ && !p->dims_->empty())
{
type = decl_str("(*)", p->type_, p->dims_);
type = "*(" + type + ")";
}
else if (
p->type_->tag_ == Foreign && p->attrs_ && p->attrs_->isary_)
{
type = "/* foreign array */ *(" + p->type_->name_ + "*)";
}
else if (p->type_->tag_ == Ptr)
{
std::string s = atype_str(p->type_);
if (s.find("const ") != std::string::npos)
type = "(" + s + ")";
}
out() << " " + type + "pargs_in->" + p->name_ +
(++idx < f->params_.size() ? "," : ");");
}
if (idx == 0)
out() << " );";
out() << "";
}
void propagate_errno(Function* f)
{
if (ecall_)
return;
out() << " /* Propagate errno back to enclave. */";
if (f->errno_)
out() << " pargs_out->_ocall_errno = errno;";
else
out() << " /* Errno propagation not enabled. */";
out() << "";
}
void write_result()
{
std::string check = "output_buffer_size >= sizeof(*pargs_out)";
if (ecall_)
out() << " if (" + check +
" &&\n oe_is_within_enclave(pargs_out, "
"output_buffer_size))";
else
out() << " if (pargs_out && " + check + ")";
out() << " pargs_out->_result = _result;";
}
};
#endif // F_EMITTER_H