Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
591 lines (488 loc) · 22 KB
/
Copy pathmain.c
File metadata and controls
591 lines (488 loc) · 22 KB
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
#define _POSIX_MONOTONIC_CLOCK 1
// Included from hyperlight_guest_capi/include
#include "hyperlight_guest.h"
// Included from hyperlight_libc/third_party/picolibc
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "assert.h"
#include "stdio.h"
#include "errno.h"
#include "time.h"
#include "unistd.h"
#define GUEST_SCRATCH_SIZE (0x40000) // default scratch size
#define MAX_BUFFER_SIZE (1024)
#define printf_f(fmt, ...) \
({ \
int _r = printf(fmt, ##__VA_ARGS__); \
fflush(stdout); \
_r; \
})
static char big_array[1024 * 1024] = {0};
const char *echo(const char *str) { return str; }
float echo_float(float f) { return f; }
double echo_double(double d) { return d; }
int next_random(void) { return rand(); }
long next_random_long(void) { return random(); }
hl_ReturnValue *set_byte_array_to_zero(const hl_FunctionCall* params) {
hl_Vec input = params->parameters[0].value.VecBytes;
uint8_t *x = malloc(input.len);
for (uintptr_t i = 0; i < input.len; i++) {
x[i] = 0;
}
return hl_result_from_Bytes(x, input.len);
}
int print_output(const char *message) {
int res = printf_f("%s", message);
return res;
}
int check_libc_stubs(void) {
assert(write(1, NULL, 0) == 0);
assert(write(2, NULL, 0) == 0);
assert(write(1, NULL, 1) == -1 && errno == EINVAL);
assert(write(-1, "", 0) == -1 && errno == EBADF);
struct timespec before, after;
assert(clock_gettime(CLOCK_MONOTONIC, &before) == 0);
assert(clock_gettime(CLOCK_MONOTONIC, &after) == 0);
assert(after.tv_sec == before.tv_sec + 1);
assert(before.tv_nsec == 0 && after.tv_nsec == 0);
assert(clock_gettime(CLOCK_REALTIME, &after) == 0);
assert(clock_gettime(CLOCK_MONOTONIC, NULL) == -1 && errno == EINVAL);
assert(clock_gettime(-1, &after) == -1 && errno == EINVAL);
return 0;
}
__attribute__((optnone))
int stack_allocate(int32_t length) {
void *buffer = alloca(length);
(void)buffer;
return length;
}
__attribute__((optnone))
void stack_overflow_helper(int32_t i) {
if (i == 0) {
return;
}
char nums[16384] = {i};
(void)nums;
stack_overflow_helper(i - 1);
}
__attribute__((optnone))
int stack_overflow(int32_t i) {
stack_overflow_helper(i);
return i;
}
int buffer_overrun(const char *String) {
char buffer[17];
(void)buffer;
int length = strlen(String);
if (length > 0) {
strncpy(buffer, String, length);
}
int result = (int)(17 - length);
return result;
}
__attribute__((optnone))
int large_var(void) {
char buffer[GUEST_SCRATCH_SIZE + 1] = {0};
(void)buffer;
return GUEST_SCRATCH_SIZE;
}
int small_var(void) {
char buffer[1024] = {0};
(void)buffer;
return 1024;
}
int call_malloc(int32_t size) {
void *heap_memory = malloc(size);
if (NULL == heap_memory) {
hl_set_error(hl_ErrorCode_GuestError, "Malloc Failed");
}
return size;
}
int malloc_and_free(int32_t size) {
void *heap_memory = malloc(size);
if (NULL == heap_memory) {
hl_set_error(hl_ErrorCode_GuestError, "Malloc Failed");
}
free(heap_memory);
return size;
}
int print_two_args(const char *arg1, int32_t arg2) {
int result = printf_f("Message: arg1:%s arg2:%d.", arg1, arg2);
return result;
}
int print_three_args(const char *arg1, int32_t arg2, int64_t arg3) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld.", arg1, arg2, arg3);
return result;
}
int print_four_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s.", arg1, arg2,
arg3, arg4);
return result;
}
int print_five_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s.", arg1,
arg2, arg3, arg4, arg5);
return result;
}
int print_six_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5, bool arg6) {
int result =
printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s arg6:%s.", arg1,
arg2, arg3, arg4, arg5, arg6 ? "true" : "false");
return result;
}
int print_seven_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5, bool arg6,
bool arg7) {
int result = printf_f(
"Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s arg6:%s arg7:%s.", arg1,
arg2, arg3, arg4, arg5, arg6 ? "true" : "false", arg7 ? "true" : "false");
return result;
}
int print_eight_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5, bool arg6,
bool arg7, uint32_t arg8) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s "
"arg6:%s arg7:%s arg8:%d.",
arg1, arg2, arg3, arg4, arg5, arg6 ? "true" : "false",
arg7 ? "true" : "false", arg8);
return result;
}
int print_nine_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5, bool arg6,
bool arg7, uint32_t arg8, uint64_t arg9) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s "
"arg6:%s arg7:%s arg8:%d arg9:%lu.",
arg1, arg2, arg3, arg4, arg5, arg6 ? "true" : "false",
arg7 ? "true" : "false", arg8, arg9);
return result;
}
int print_ten_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5, bool arg6, bool arg7,
uint32_t arg8, uint64_t arg9, int32_t arg10) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s "
"arg6:%s arg7:%s arg8:%d arg9:%lu arg10:%d.",
arg1, arg2, arg3, arg4, arg5, arg6 ? "true" : "false",
arg7 ? "true" : "false", arg8, arg9, arg10);
return result;
}
int print_eleven_args(const char *arg1, int32_t arg2, int64_t arg3,
const char *arg4, const char *arg5, bool arg6,
bool arg7, uint32_t arg8, uint64_t arg9,
int32_t arg10, float arg11) {
int result = printf_f("Message: arg1:%s arg2:%d arg3:%ld arg4:%s arg5:%s "
"arg6:%s arg7:%s arg8:%d arg9:%lu arg10:%d arg11:%.3f.",
arg1, arg2, arg3, arg4, arg5, arg6 ? "true" : "false",
arg7 ? "true" : "false", arg8, arg9, arg10, arg11);
return result;
}
int set_static(void) {
int length = sizeof(big_array);
for (int l = 0; l < length; l++) {
big_array[l] = l;
}
return length;
}
hl_ReturnValue *get_size_prefixed_buffer(const hl_FunctionCall* params) {
hl_Vec input = params->parameters[0].value.VecBytes;
return hl_result_from_Bytes(input.data, input.len);
}
int guest_abort_with_code(int32_t code) {
hl_abort_with_code(code);
return -1;
}
int guest_abort_with_msg(int32_t code, const char *message) {
hl_abort_with_code_and_message(code, message);
return -1;
}
int execute_on_stack(void) {
uint8_t hlt = 0xF4;
((void (*)()) & hlt)();
return -1;
}
int log_message(const char *message, int64_t level) {
LOG((hl_Level)level, message);
return -1;
}
hl_ReturnValue *twenty_four_k_in_eight_k_out(const hl_FunctionCall* params) {
hl_Vec input = params->parameters[0].value.VecBytes;
assert(input.len == 24 * 1024);
return hl_result_from_Bytes(input.data, 8 * 1024);
}
int guest_function(const char *from_host) {
char guest_message[256] = "Hello from GuestFunction1, ";
int len = strlen(from_host);
strncat(guest_message, from_host, len);
hl_Parameter params = {.tag = hl_ParameterType_String,
.value = {.String = guest_message}};
const hl_FunctionCall host_call = {.function_name = "HostMethod1",
.parameters = ¶ms,
.parameters_len = 1,
.return_type = hl_ReturnType_Int};
hl_call_host_function(&host_call);
hl_get_host_return_value_as_Int();
return 0;
}
bool guest_fn_checks_if_host_returns_bool_value(int32_t a, int32_t b) {
hl_Parameter params[2];
params[0].tag = hl_ParameterType_Int;
params[0].value.Int = a;
params[1].tag = hl_ParameterType_Int;
params[1].value.Int = b;
const hl_FunctionCall host_call = {.function_name = "HostBool",
.parameters = params,
.parameters_len = 2,
.return_type = hl_ReturnType_Bool
};
hl_call_host_function(&host_call);
return hl_get_host_return_value_as_Bool();
}
float guest_fn_checks_if_host_returns_float_value(float a, float b) {
hl_Parameter params[2];
params[0].tag = hl_ParameterType_Float;
params[0].value.Float = a;
params[1].tag = hl_ParameterType_Float;
params[1].value.Float = b;
const hl_FunctionCall host_call = {.function_name = "HostAddFloat",
.parameters = params,
.parameters_len = 2,
.return_type = hl_ReturnType_Float
};
hl_call_host_function(&host_call);
return hl_get_host_return_value_as_Float();
}
double guest_fn_checks_if_host_returns_double_value(double a, double b) {
hl_Parameter params[2];
params[0].tag = hl_ParameterType_Double;
params[0].value.Double = a;
params[1].tag = hl_ParameterType_Double;
params[1].value.Double = b;
const hl_FunctionCall host_call = {.function_name = "HostAddDouble",
.parameters = params,
.parameters_len = 2,
.return_type = hl_ReturnType_Double
};
hl_call_host_function(&host_call);
return hl_get_host_return_value_as_Double();
}
const char* guest_fn_checks_if_host_returns_string_value() {
char guest_message[256] = "Guest Function";
hl_Parameter params;
params.tag = hl_ParameterType_String;
params.value.String = guest_message;
const hl_FunctionCall host_call = {.function_name = "HostAddStrings",
.parameters = ¶ms,
.parameters_len = 1,
.return_type = hl_ReturnType_String
};
hl_call_host_function(&host_call);
return hl_get_host_return_value_as_String();
}
hl_ReturnValue *round_trip_host_byte_chunks(const hl_FunctionCall *params) {
hl_ByteChunks input = params->parameters[0].value.ByteChunks;
assert(input.count > 1);
for (uintptr_t i = 0; i < input.count; i++) {
assert(input.chunks[i].data != NULL || input.chunks[i].len == 0);
}
hl_Parameter host_param = {
.tag = hl_ParameterType_ByteChunks,
.value = {.ByteChunks = input},
};
const hl_FunctionCall host_call = {
.function_name = "HostEchoByteChunks",
.parameters = &host_param,
.parameters_len = 1,
.return_type = hl_ReturnType_ByteChunks,
};
hl_call_host_function(&host_call);
hl_ByteChunks *output = hl_get_host_return_value_as_ByteChunks();
assert(output != NULL);
assert(output->count > 1);
hl_ReturnValue *result = hl_result_from_ByteChunks(*output);
hl_free_byte_chunks(output);
return result;
}
// Host return handles own their chunks until hl_free_byte_chunks.
static hl_ByteChunks *retained_host_chunks = NULL;
static int32_t byte_chunks_len(const hl_ByteChunks *value) {
uintptr_t len = 0;
if (value != NULL) {
for (uintptr_t i = 0; i < value->count; i++) {
len += value->chunks[i].len;
}
}
assert(len <= INT32_MAX);
return (int32_t)len;
}
hl_ReturnValue *retain_host_byte_chunks(const hl_FunctionCall *params) {
const hl_FunctionCall host_call = {
.function_name = "HostEchoByteChunks",
.parameters = params->parameters,
.parameters_len = 1,
.return_type = hl_ReturnType_ByteChunks,
};
hl_call_host_function(&host_call);
hl_ByteChunks *output = hl_get_host_return_value_as_ByteChunks();
assert(output != NULL);
hl_free_byte_chunks(retained_host_chunks);
retained_host_chunks = output;
return hl_result_from_Int(byte_chunks_len(output));
}
hl_ReturnValue *read_retained_host_byte_chunks(const hl_FunctionCall *params) {
(void)params;
if (retained_host_chunks == NULL) {
return hl_result_from_ByteChunks((hl_ByteChunks){0});
}
return hl_result_from_ByteChunks(*retained_host_chunks);
}
hl_ReturnValue *release_host_byte_chunks(const hl_FunctionCall *params) {
(void)params;
int32_t len = byte_chunks_len(retained_host_chunks);
hl_free_byte_chunks(retained_host_chunks);
retained_host_chunks = NULL;
return hl_result_from_Int(len);
}
hl_ReturnValue *return_null(const hl_FunctionCall *params) {
(void)params;
return NULL;
}
hl_ReturnValue *return_null_with_error(const hl_FunctionCall *params) {
(void)params;
hl_set_error(hl_ErrorCode_GuestError, "C registered error");
return NULL;
}
hl_ReturnValue *return_value_with_error(const hl_FunctionCall *params) {
(void)params;
hl_set_error(hl_ErrorCode_GuestError, "C registered error");
return hl_result_from_Bytes((const uint8_t *)big_array, 16 * 1024);
}
hl_ReturnValue *stash_host_return_and_error(const hl_FunctionCall *params) {
(void)params;
const hl_FunctionCall host_call = {
.function_name = "HostInt",
.parameters = NULL,
.parameters_len = 0,
.return_type = hl_ReturnType_Int,
};
hl_call_host_function(&host_call);
hl_set_error(hl_ErrorCode_GuestError, "C dispatch error");
return NULL;
}
int read_stashed_host_return(void) {
return hl_get_host_return_value_as_Int();
}
HYPERLIGHT_WRAP_FUNCTION(guest_fn_checks_if_host_returns_float_value, Float, 2, Float, Float)
HYPERLIGHT_WRAP_FUNCTION(guest_fn_checks_if_host_returns_double_value, Double, 2, Double, Double)
HYPERLIGHT_WRAP_FUNCTION(guest_fn_checks_if_host_returns_string_value, String, 0)
HYPERLIGHT_WRAP_FUNCTION(guest_fn_checks_if_host_returns_bool_value, Bool, 2, Int, Int)
HYPERLIGHT_WRAP_FUNCTION(echo, String, 1, String)
// HYPERLIGHT_WRAP_FUNCTION(set_byte_array_to_zero, 1, VecBytes) is not valid for functions that return VecBytes
HYPERLIGHT_WRAP_FUNCTION(guest_function, Int, 1, String)
HYPERLIGHT_WRAP_FUNCTION(print_output, Int, 1, String)
HYPERLIGHT_WRAP_FUNCTION(check_libc_stubs, Int, 0)
HYPERLIGHT_WRAP_FUNCTION(stack_allocate, Int, 1, Int)
HYPERLIGHT_WRAP_FUNCTION(stack_overflow, Int, 1, Int)
HYPERLIGHT_WRAP_FUNCTION(buffer_overrun, Int, 1, String)
HYPERLIGHT_WRAP_FUNCTION(large_var, Int, 0)
HYPERLIGHT_WRAP_FUNCTION(small_var, Int, 0)
HYPERLIGHT_WRAP_FUNCTION(call_malloc, Int, 1, Int)
HYPERLIGHT_WRAP_FUNCTION(malloc_and_free, Int, 1, Int)
HYPERLIGHT_WRAP_FUNCTION(print_two_args, Int, 2, String, Int)
HYPERLIGHT_WRAP_FUNCTION(print_three_args, Int, 3, String, Int, Long)
HYPERLIGHT_WRAP_FUNCTION(print_four_args, Int, 4, String, Int, Long, String)
HYPERLIGHT_WRAP_FUNCTION(print_five_args, Int, 5, String, Int, Long, String, String)
HYPERLIGHT_WRAP_FUNCTION(print_six_args, Int, 6, String, Int, Long, String, String, Bool)
HYPERLIGHT_WRAP_FUNCTION(print_seven_args, Int, 7, String, Int, Long, String, String, Bool, Bool)
HYPERLIGHT_WRAP_FUNCTION(print_eight_args, Int, 8, String, Int, Long, String, String, Bool, Bool, UInt)
HYPERLIGHT_WRAP_FUNCTION(print_nine_args, Int, 9, String, Int, Long, String, String, Bool, Bool, UInt, ULong)
HYPERLIGHT_WRAP_FUNCTION(print_ten_args, Int, 10, String, Int, Long, String, String, Bool, Bool, UInt, ULong, Int)
HYPERLIGHT_WRAP_FUNCTION(print_eleven_args, Int, 11, String, Int, Long, String, String, Bool, Bool, UInt, ULong, Int, Float)
HYPERLIGHT_WRAP_FUNCTION(echo_float, Float, 1, Float)
HYPERLIGHT_WRAP_FUNCTION(echo_double, Double, 1, Double)
HYPERLIGHT_WRAP_FUNCTION(next_random, Int, 0)
HYPERLIGHT_WRAP_FUNCTION(next_random_long, Long, 0)
HYPERLIGHT_WRAP_FUNCTION(set_static, Int, 0)
// HYPERLIGHT_WRAP_FUNCTION(get_size_prefixed_buffer, Int, 1, VecBytes) is not valid for functions that return VecBytes
HYPERLIGHT_WRAP_FUNCTION(guest_abort_with_msg, Int, 2, Int, String)
HYPERLIGHT_WRAP_FUNCTION(guest_abort_with_code, Int, 1, Int)
HYPERLIGHT_WRAP_FUNCTION(execute_on_stack, Int, 0)
HYPERLIGHT_WRAP_FUNCTION(log_message, Int, 2, String, Long)
// HYPERLIGHT_WRAP_FUNCTION(twenty_four_k_in_eight_k_out, VecBytes, 1, VecBytes) is not valid for functions that return VecBytes
HYPERLIGHT_WRAP_FUNCTION(read_stashed_host_return, Int, 0)
void hyperlight_main(void)
{
HYPERLIGHT_REGISTER_FUNCTION("GuestRetrievesFloatValue", guest_fn_checks_if_host_returns_float_value);
HYPERLIGHT_REGISTER_FUNCTION("GuestRetrievesDoubleValue", guest_fn_checks_if_host_returns_double_value);
HYPERLIGHT_REGISTER_FUNCTION("GuestRetrievesStringValue", guest_fn_checks_if_host_returns_string_value);
HYPERLIGHT_REGISTER_FUNCTION("GuestRetrievesBoolValue", guest_fn_checks_if_host_returns_bool_value);
HYPERLIGHT_REGISTER_FUNCTION("Echo", echo);
// HYPERLIGHT_REGISTER_FUNCTION macro does not work for functions that return VecBytes,
// so we use hl_register_function_definition directly
hl_register_function_definition("SetByteArrayToZero", set_byte_array_to_zero, 1, (hl_ParameterType[]){hl_ParameterType_VecBytes}, hl_ReturnType_VecBytes);
HYPERLIGHT_REGISTER_FUNCTION("GuestMethod1", guest_function);
HYPERLIGHT_REGISTER_FUNCTION("PrintOutput", print_output);
HYPERLIGHT_REGISTER_FUNCTION("CheckLibcStubs", check_libc_stubs);
HYPERLIGHT_REGISTER_FUNCTION("StackAllocate", stack_allocate);
HYPERLIGHT_REGISTER_FUNCTION("StackOverflow", stack_overflow);
HYPERLIGHT_REGISTER_FUNCTION("BufferOverrun", buffer_overrun);
HYPERLIGHT_REGISTER_FUNCTION("LargeVar", large_var);
HYPERLIGHT_REGISTER_FUNCTION("SmallVar", small_var);
HYPERLIGHT_REGISTER_FUNCTION("CallMalloc", call_malloc);
HYPERLIGHT_REGISTER_FUNCTION("MallocAndFree", malloc_and_free);
HYPERLIGHT_REGISTER_FUNCTION("PrintTwoArgs", print_two_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintThreeArgs", print_three_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintFourArgs", print_four_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintFiveArgs", print_five_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintSixArgs", print_six_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintSevenArgs", print_seven_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintEightArgs", print_eight_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintNineArgs", print_nine_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintTenArgs", print_ten_args);
HYPERLIGHT_REGISTER_FUNCTION("PrintElevenArgs", print_eleven_args);
HYPERLIGHT_REGISTER_FUNCTION("EchoFloat", echo_float);
HYPERLIGHT_REGISTER_FUNCTION("EchoDouble", echo_double);
HYPERLIGHT_REGISTER_FUNCTION("NextRandom", next_random);
HYPERLIGHT_REGISTER_FUNCTION("NextRandomLong", next_random_long);
HYPERLIGHT_REGISTER_FUNCTION("SetStatic", set_static);
// HYPERLIGHT_REGISTER_FUNCTION macro does not work for functions that return VecBytes,
// so we use hl_register_function_definition directly
hl_register_function_definition("GetSizePrefixedBuffer", get_size_prefixed_buffer, 1, (hl_ParameterType[]){hl_ParameterType_VecBytes}, hl_ReturnType_VecBytes);
HYPERLIGHT_REGISTER_FUNCTION("GuestAbortWithCode", guest_abort_with_code);
HYPERLIGHT_REGISTER_FUNCTION("GuestAbortWithMessage", guest_abort_with_msg);
HYPERLIGHT_REGISTER_FUNCTION("ExecuteOnStack", execute_on_stack);
HYPERLIGHT_REGISTER_FUNCTION("LogMessage", log_message);
// HYPERLIGHT_REGISTER_FUNCTION macro does not work for functions that return VecBytes,
// so we use hl_register_function_definition directly
hl_register_function_definition("24K_in_8K_out", twenty_four_k_in_eight_k_out, 1, (hl_ParameterType[]){hl_ParameterType_VecBytes}, hl_ReturnType_VecBytes);
hl_register_function_definition("RoundTripHostByteChunks", round_trip_host_byte_chunks, 1, (hl_ParameterType[]){hl_ParameterType_ByteChunks}, hl_ReturnType_ByteChunks);
hl_register_function_definition("RetainHostByteChunks", retain_host_byte_chunks, 1, (hl_ParameterType[]){hl_ParameterType_ByteChunks}, hl_ReturnType_Int);
hl_register_function_definition("ReadRetainedHostByteChunks", read_retained_host_byte_chunks, 0, (hl_ParameterType[]){0}, hl_ReturnType_ByteChunks);
hl_register_function_definition("ReleaseHostByteChunks", release_host_byte_chunks, 0, (hl_ParameterType[]){0}, hl_ReturnType_Int);
hl_register_function_definition("ReturnNull", return_null, 0, (hl_ParameterType[]){0}, hl_ReturnType_Void);
hl_register_function_definition("ReturnNullWithError", return_null_with_error, 0, (hl_ParameterType[]){0}, hl_ReturnType_Void);
hl_register_function_definition("ReturnValueWithError", return_value_with_error, 0, (hl_ParameterType[]){0}, hl_ReturnType_VecBytes);
hl_register_function_definition("StashHostReturnAndError", stash_host_return_and_error, 0, (hl_ParameterType[]){0}, hl_ReturnType_Void);
HYPERLIGHT_REGISTER_FUNCTION("ReadStashedHostReturn", read_stashed_host_return);
}
// This dispatch function is only used when the host dispatches a guest function
// call but there is no registered guest function with the given name.
hl_ReturnValue *c_guest_dispatch_function(const hl_FunctionCall *function_call) {
const char *func_name = function_call->function_name;
if (strcmp(func_name, "ThisIsNotARealFunctionButTheNameIsImportant") == 0) {
// TODO DO A LOG HERE
// This is a special case for test `custom_guest_dispatch_is_working`.
return hl_result_from_Int(99);
}
if (strcmp(func_name, "FallbackNullWithError") == 0) {
hl_set_error(hl_ErrorCode_GuestError, "C fallback error");
return NULL;
}
if (strcmp(func_name, "FallbackValueWithError") == 0) {
hl_set_error(hl_ErrorCode_GuestError, "C fallback error");
return hl_result_from_Bytes((const uint8_t *)big_array, 16 * 1024);
}
return NULL;
}