GCC Code Coverage Report


Directory: src/gate/
File: src/gate/functions.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 105 134 78.4%
Functions: 7 7 100.0%
Branches: 43 84 51.2%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28 #include "gate/functions.h"
29 #include "gate/results.h"
30 #include "gate/memalloc.h"
31 #include "gate/callstacks.h"
32 #include "gate/debugging.h"
33
34 #if defined(GATE_COMPILER_MSVC)
35 # if defined(GATE_SYS_EFI)
36 # define GATE_FUNCTION_GUARD_NOGUARD 1
37 # else
38 # define GATE_FUNCTION_GUARD_WINSEH 1
39 # endif
40 #elif defined(GATE_SYS_WASM_WASI)
41 # define GATE_FUNCTION_GUARD_NOGUARD 1
42 #elif defined(GATE_SYS_ARDUINO)
43 # define GATE_FUNCTION_GUARD_NOGUARD 1
44 #else
45 # define GATE_FUNCTION_GUARD_SIGNALS 1
46 #endif
47
48 #define GATE_FUNCTIONS_GENERIC_IMPL 1
49
50 #if defined(GATE_FUNCTIONS_GENERIC_IMPL)
51
52 #if defined(GATE_COMPILER_WATCOM)
53 typedef short fparam_t;
54 #else
55 typedef void* fparam_t;
56 #endif
57
58 typedef gate_uint64_t(*gate_function_generic_t)(
59 fparam_t a000, fparam_t a001, fparam_t a002, fparam_t a003, fparam_t a004, fparam_t a005, fparam_t a006, fparam_t a007, fparam_t a008, fparam_t a009,
60 fparam_t a010, fparam_t a011, fparam_t a012, fparam_t a013, fparam_t a014, fparam_t a015, fparam_t a016, fparam_t a017, fparam_t a018, fparam_t a019,
61 fparam_t a020, fparam_t a021, fparam_t a022, fparam_t a023, fparam_t a024, fparam_t a025, fparam_t a026, fparam_t a027, fparam_t a028, fparam_t a029,
62 fparam_t a030, fparam_t a031, fparam_t a032, fparam_t a033, fparam_t a034, fparam_t a035, fparam_t a036, fparam_t a037, fparam_t a038, fparam_t a039,
63 fparam_t a040, fparam_t a041, fparam_t a042, fparam_t a043, fparam_t a044, fparam_t a045, fparam_t a046, fparam_t a047, fparam_t a048, fparam_t a049,
64 fparam_t a050, fparam_t a051, fparam_t a052, fparam_t a053, fparam_t a054, fparam_t a055, fparam_t a056, fparam_t a057, fparam_t a058, fparam_t a059,
65 fparam_t a060, fparam_t a061, fparam_t a062, fparam_t a063
66 );
67
68 #define REGISTER_TYPE_CONVERTER(type, arg_type_name) \
69 typedef union { \
70 type native_value; \
71 fparam_t generic_value[(sizeof(fparam_t) + sizeof(type) - 1) / sizeof(fparam_t)]; \
72 } arg_type_name ## _converter_t
73
74 REGISTER_TYPE_CONVERTER(unsigned char, unsigned_char);
75 REGISTER_TYPE_CONVERTER(signed char, signed_char);
76 REGISTER_TYPE_CONVERTER(unsigned short, unsigned_short);
77 REGISTER_TYPE_CONVERTER(signed short, signed_short);
78 REGISTER_TYPE_CONVERTER(unsigned int, unsigned_int);
79 REGISTER_TYPE_CONVERTER(signed int, signed_int);
80 REGISTER_TYPE_CONVERTER(unsigned long, unsigned_long);
81 REGISTER_TYPE_CONVERTER(signed long, signed_long);
82 REGISTER_TYPE_CONVERTER(gate_uint64_t, unsigned_longlong);
83 REGISTER_TYPE_CONVERTER(gate_int64_t, signed_longlong);
84 REGISTER_TYPE_CONVERTER(float, float);
85 REGISTER_TYPE_CONVERTER(double, double);
86 REGISTER_TYPE_CONVERTER(long double, longdouble);
87 REGISTER_TYPE_CONVERTER(void*, pointer);
88
89 #define CONVERT_TYPE_TO_GENERIC(arg_type_name, input_value, output_ptr, output_count, output_max) do { \
90 arg_type_name ## _converter_t converter = GATE_INIT_EMPTY; \
91 if(sizeof(input_value) > (sizeof(fparam_t) * output_max)) \
92 { \
93 *output_count = 0; \
94 } \
95 else \
96 { \
97 converter.native_value = input_value; \
98 *output_count = sizeof(converter.generic_value) / sizeof(fparam_t); \
99 gate_mem_copy(output_ptr, &converter.generic_value[0], sizeof(converter.generic_value)); \
100 } \
101 } while(0)
102
103 #if defined(GATE_SYS_WIN)
104 #else
105 # define GATE_FUNCTION_ARGS_MAX_ALIGNMENT
106 #endif
107
108 19 static unsigned gate_function_load_argument(gate_function_argument_t* arg, fparam_t* target, unsigned target_count)
109 {
110 19 unsigned target_used = 0;
111
112
12/16
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
19 switch (arg->arg_type)
113 {
114 2 case GATE_FUNCTION_ARG_UCHAR:
115 {
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 CONVERT_TYPE_TO_GENERIC(unsigned_char, arg->arg_value.u_chr, target, &target_used, target_count);
117 2 break;
118 }
119 1 case GATE_FUNCTION_ARG_SCHAR:
120 {
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CONVERT_TYPE_TO_GENERIC(signed_char, arg->arg_value.s_chr, target, &target_used, target_count);
122 1 break;
123 }
124 2 case GATE_FUNCTION_ARG_USHORT:
125 {
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 CONVERT_TYPE_TO_GENERIC(unsigned_short, arg->arg_value.u_short, target, &target_used, target_count);
127 2 break;
128 }
129 1 case GATE_FUNCTION_ARG_SSHORT:
130 {
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CONVERT_TYPE_TO_GENERIC(signed_short, arg->arg_value.s_short, target, &target_used, target_count);
132 1 break;
133 }
134 1 case GATE_FUNCTION_ARG_UINT:
135 {
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CONVERT_TYPE_TO_GENERIC(unsigned_int, arg->arg_value.u_int, target, &target_used, target_count);
137 1 break;
138 }
139 1 case GATE_FUNCTION_ARG_SINT:
140 {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CONVERT_TYPE_TO_GENERIC(signed_int, arg->arg_value.s_int, target, &target_used, target_count);
142 1 break;
143 }
144 2 case GATE_FUNCTION_ARG_ULONG:
145 {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 CONVERT_TYPE_TO_GENERIC(unsigned_long, arg->arg_value.u_long, target, &target_used, target_count);
147 2 break;
148 }
149 1 case GATE_FUNCTION_ARG_SLONG:
150 {
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CONVERT_TYPE_TO_GENERIC(signed_long, arg->arg_value.s_long, target, &target_used, target_count);
152 1 break;
153 }
154 2 case GATE_FUNCTION_ARG_ULONGLONG:
155 {
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 CONVERT_TYPE_TO_GENERIC(unsigned_longlong, arg->arg_value.u_long_long, target, &target_used, target_count);
157 2 break;
158 }
159 1 case GATE_FUNCTION_ARG_SLONGLONG:
160 {
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 CONVERT_TYPE_TO_GENERIC(signed_longlong, arg->arg_value.s_long_long, target, &target_used, target_count);
162 1 break;
163 }
164 ✗ case GATE_FUNCTION_ARG_FLOAT:
165 {
166 ✗ CONVERT_TYPE_TO_GENERIC(float, arg->arg_value.flt, target, &target_used, target_count);
167 ✗ break;
168 }
169 ✗ case GATE_FUNCTION_ARG_DOUBLE:
170 {
171 ✗ CONVERT_TYPE_TO_GENERIC(double, arg->arg_value.dbl, target, &target_used, target_count);
172 ✗ break;
173 }
174 ✗ case GATE_FUNCTION_ARG_LONGDOUBLE:
175 {
176 ✗ CONVERT_TYPE_TO_GENERIC(longdouble, arg->arg_value.ldbl, target, &target_used, target_count);
177 ✗ break;
178 }
179 4 case GATE_FUNCTION_ARG_POINTER:
180 {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 CONVERT_TYPE_TO_GENERIC(pointer, arg->arg_value.ptr, target, &target_used, target_count);
182 4 break;
183 }
184 1 case GATE_FUNCTION_ARG_BLOB:
185 {
186 1 target_used = (arg->arg_value.blob.blob_size + sizeof(fparam_t) - 1) / sizeof(fparam_t);
187 1 gate_mem_copy(target, arg->arg_value.blob.blob_ptr, arg->arg_value.blob.blob_size);
188 1 break;
189 }
190 }
191 19 return target_used;
192 }
193
194 5 gate_result_t gate_function_invoke_generic(gate_funcptr_t func_ptr, gate_enumint_t flags,
195 gate_function_argument_t* args, gate_size_t arg_count,
196 gate_function_argument_t* retval)
197 {
198 5 fparam_t v[64] = GATE_INIT_EMPTY;
199 5 fparam_t* ptr_v = &v[0];
200 5 unsigned count = sizeof(v) / sizeof(v[0]);
201 5 gate_uint64_t r = 0;
202 5 gate_function_generic_t func = (gate_function_generic_t)func_ptr;
203
204 GATE_UNUSED_ARG(flags);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (args == NULL)
206 {
207 ✗ return GATE_RESULT_NULLPOINTER;
208 }
209
210
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 5 times.
24 while (arg_count-- != 0)
211 {
212 unsigned used, n;
213 fparam_t tmp[16];
214 19 gate_mem_clear(tmp, sizeof(tmp));
215 19 used = gate_function_load_argument(args, tmp, sizeof(tmp) / sizeof(tmp[0]));
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (used > count)
217 {
218 ✗ return GATE_RESULT_OUTOFMEMORY;
219 }
220 #if defined(GATE_FUNCTION_ARGS_MAX_ALIGNMENT)
221 /* type was greater than pointer, add padding words to extend alignment */
222
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
19 if (used > 1)
223 {
224 unsigned mod;
225 1 mod = count % used;
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (mod > 0)
227 {
228 ✗ unsigned padd = used - mod;
229 ✗ if (padd > count)
230 {
231 ✗ return GATE_RESULT_OUTOFMEMORY;
232 }
233 ✗ for (n = 0; n != padd; ++n)
234 {
235 ✗ ptr_v[n] = ((fparam_t)0);
236 }
237 ✗ ptr_v += padd;
238 ✗ count -= padd;
239 }
240 }
241 #else
242 /* we are pointer-aligned -> nothing to do */
243 #endif
244
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 19 times.
39 for (n = 0; n != used; ++n)
245 {
246 20 ptr_v[n] = tmp[n];
247 }
248 19 ptr_v += used;
249 19 count -= used;
250 19 ++args;
251 }
252
253 5 r = func(
254 v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9],
255 v[10], v[11], v[12], v[13], v[14], v[15], v[16], v[17], v[18], v[19],
256 v[20], v[21], v[22], v[23], v[24], v[25], v[26], v[27], v[28], v[29],
257 v[30], v[31], v[32], v[33], v[34], v[35], v[36], v[37], v[38], v[39],
258 v[40], v[41], v[42], v[43], v[44], v[45], v[46], v[47], v[48], v[49],
259 v[50], v[51], v[52], v[53], v[54], v[55], v[56], v[57], v[58], v[59],
260 v[60], v[61], v[62], v[63]
261 );
262
263
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (retval)
264 {
265 5 retval->arg_value.u_long_long = r;
266 5 retval->arg_type = GATE_FUNCTION_ARG_ULONGLONG;
267 }
268
269 5 return GATE_RESULT_OK;
270 }
271
272 #endif
273
274
275 #if defined(GATE_FUNCTIONS_X86_IMPL)
276
277 typedef union x86_arguments
278 {
279 void* function_ptr;
280 unsigned char buffer[1024];
281 } x86_arguments_t;
282
283 typedef gate_uint64_t(*x86_c_func_t)(x86_arguments_t args);
284
285 gate_result_t gate_function_invoke_generic(void* func_ptr, gate_enumint_t flags,
286 gate_function_argument_t* args, gate_size_t arg_count, gate_function_argument_t* retval)
287 {
288 gate_result_t ret = GATE_RESULT_OK;
289 x86_c_func_t func = (x86_c_func_t)func_ptr;
290 gate_uint64_t x86_ret = NULL;
291 x86_arguments_t x86_args = GATE_INIT_EMPTY;
292 gate_size_t n;
293
294 for (n = 0; n != sizeof(x86_args.buffer); ++n)
295 {
296 x86_args.buffer[n] = (unsigned char)(n % 256);
297 }
298
299 x86_ret = func(x86_args);
300 if (retval)
301 {
302 retval->arg_value.u_long_long = x86_ret;
303 retval->arg_type = GATE_FUNCTION_ARG_ULONGLONG;
304 }
305 return ret;
306 }
307
308
309 #endif /* GATE_FUNCTIONS_X86_IMPL */
310
311 #if defined(GATE_FUNCTIONS_X86X64_IMPL)
312
313 typedef void* (*x64_c_func_t)(
314 void* a1, void* a2, void* a3, void* a4, void* a5, void* a6, void* a7, void* a8,
315 void* b1, void* b2, void* b3, void* b4, void* b5, void* b6, void* b7, void* b8,
316 void* c1, void* c2, void* c3, void* c4, void* c5, void* c6, void* c7, void* c8,
317 void* d1, void* d2, void* d3, void* d4, void* d5, void* d6, void* d7, void* d8,
318 void* e1, void* e2, void* e3, void* e4, void* e5, void* e6, void* e7, void* e8,
319 void* f1, void* f2, void* f3, void* f4, void* f5, void* f6, void* f7, void* f8
320 );
321
322 gate_result_t gate_function_invoke_generic(void* func_ptr, gate_enumint_t flags,
323 gate_function_argument_t* args, gate_size_t arg_count, gate_function_argument_t* retval)
324 {
325 gate_result_t ret = GATE_RESULT_OK;
326 x64_c_func_t func = (x64_c_func_t)func_ptr;
327 void* x64_ret = NULL;
328 gate_size_t n, i;
329 void* a[8];
330 void* b[8];
331 void* c[8];
332 void* d[8];
333 void* e[8];
334 void* f[8];
335
336 i = 0;
337 for (n = 0; n != 8; ++n) a[n] = (unsigned char)(i++ % 256);
338 for (n = 0; n != 8; ++n) b[n] = (unsigned char)(i++ % 256);
339 for (n = 0; n != 8; ++n) c[n] = (unsigned char)(i++ % 256);
340 for (n = 0; n != 8; ++n) d[n] = (unsigned char)(i++ % 256);
341 for (n = 0; n != 8; ++n) e[n] = (unsigned char)(i++ % 256);
342 for (n = 0; n != 8; ++n) f[n] = (unsigned char)(i++ % 256);
343
344 x64_ret = func(
345 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
346 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
347 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7],
348 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
349 e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7],
350 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]
351 );
352 if (retval)
353 {
354 retval->arg_value.u_long_long = x64_ret;
355 retval->arg_type = GATE_FUNCTION_ARG_ULONGLONG;
356 }
357
358 return ret;
359 }
360
361 #endif /* GATE_FUNCTIONS_X86X64_IMPL */
362
363
364
365 #if defined(GATE_FUNCTION_GUARD_WINSEH)
366
367 #include "gate/platforms.h"
368
369 static int gate_win32_seh_filter(unsigned int seh_code,
370 struct _EXCEPTION_POINTERS* xcpt_ptrs,
371 gate_function_fault_handler_t handler, void* param)
372 {
373 unsigned fault_code;
374 gate_result_t result;
375
376 GATE_UNUSED_ARG(xcpt_ptrs);
377
378 switch (seh_code)
379 {
380 case EXCEPTION_FLT_DENORMAL_OPERAND:
381 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
382 case EXCEPTION_FLT_INEXACT_RESULT:
383 case EXCEPTION_FLT_INVALID_OPERATION:
384 case EXCEPTION_FLT_OVERFLOW:
385 case EXCEPTION_FLT_STACK_CHECK:
386 case EXCEPTION_FLT_UNDERFLOW:
387 fault_code = GATE_FUNCTION_FAULT_FLOAT;
388 break;
389 case EXCEPTION_ACCESS_VIOLATION:
390 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
391 case EXCEPTION_GUARD_PAGE:
392 case EXCEPTION_IN_PAGE_ERROR:
393 fault_code = GATE_FUNCTION_FAULT_ADDRESS;
394 break;
395 case EXCEPTION_ILLEGAL_INSTRUCTION:
396 fault_code = GATE_FUNCTION_FAULT_INSTRUCTION;
397 break;
398 case EXCEPTION_SINGLE_STEP:
399 case EXCEPTION_BREAKPOINT:
400 fault_code = GATE_FUNCTION_FAULT_BREAKPOINT;
401 break;
402 case EXCEPTION_DATATYPE_MISALIGNMENT:
403 fault_code = GATE_FUNCTION_FAULT_ALIGNMENT;
404 break;
405 case EXCEPTION_INT_DIVIDE_BY_ZERO:
406 fault_code = GATE_FUNCTION_FAULT_ARITHMETIC;
407 break;
408 case EXCEPTION_INT_OVERFLOW:
409 fault_code = GATE_FUNCTION_FAULT_OVERFLOW;
410 break;
411 case EXCEPTION_INVALID_HANDLE:
412 fault_code = GATE_FUNCTION_FAULT_SYSTEM;
413 break;
414 case EXCEPTION_PRIV_INSTRUCTION:
415 fault_code = GATE_FUNCTION_FAULT_PRIVILEGE;
416 break;
417 case EXCEPTION_STACK_OVERFLOW:
418 fault_code = GATE_FUNCTION_FAULT_STACK;
419 break;
420 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
421 case EXCEPTION_INVALID_DISPOSITION:
422 default:
423 fault_code = GATE_FUNCTION_FAULT_UNKNOWN;
424 break;
425 }
426
427 if (handler)
428 {
429 result = handler(fault_code, param);
430 if (GATE_FAILED(result))
431 {
432 return EXCEPTION_CONTINUE_SEARCH;
433 }
434 }
435 return EXCEPTION_EXECUTE_HANDLER;
436 }
437
438
439 gate_result_t gate_function_invoke_guarded(gate_entrypoint_t entry_point, void* entry_param,
440 gate_function_fault_handler_t fault_handler, void* fault_param)
441 {
442 gate_result_t result = GATE_RESULT_OK;
443 __try
444 {
445 if (entry_point)
446 {
447 result = entry_point(entry_param);
448 }
449 }
450 __except (gate_win32_seh_filter(GetExceptionCode(), GetExceptionInformation(), fault_handler, fault_param))
451 {
452 result = GATE_RESULT_BADINSTRUCTION;
453 }
454 return result;
455 }
456
457 #endif /* GATE_FUNCTION_GUARD_WINSEH */
458
459
460
461 #if defined(GATE_FUNCTION_GUARD_SIGNALS)
462
463 #include "gate/atomics.h"
464 #include "gate/platforms.h"
465 #include "gate/callstacks.h"
466 #include "gate/threading.h"
467 #include <setjmp.h>
468 #include <signal.h>
469
470
471 static gate_thread_storage_t global_function_guard_registry;
472 static gate_atomic_flag_t global_function_guard_registry_initialized = GATE_ATOMIC_FLAG_INIT;
473
474 typedef struct gate_function_anchor_class
475 {
476 gate_callstack_jmpbuf_t context;
477 gate_function_fault_handler_t fault_handler;
478 void* fault_param;
479 } gate_function_anchor_t;
480
481
482 static void gate_function_guard_signal_handler(int signum);
483
484 6 static gate_result_t gate_function_guard_init()
485 {
486 6 gate_result_t ret = GATE_RESULT_OK;
487
488
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if (false == gate_atomic_flag_set(&global_function_guard_registry_initialized))
489 {
490 2 ret = gate_thread_storage_alloc(&global_function_guard_registry);
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(ret))
492 {
493 ✗ gate_atomic_flag_clear(&global_function_guard_registry_initialized);
494 ✗ return ret;
495 }
496 }
497
498 6 gate_platform_set_signal_handler(SIGABRT, &gate_function_guard_signal_handler, NULL);
499 #ifdef SIGBUS
500 6 gate_platform_set_signal_handler(SIGBUS, &gate_function_guard_signal_handler, NULL);
501 #endif
502 6 gate_platform_set_signal_handler(SIGFPE, &gate_function_guard_signal_handler, NULL);
503 6 gate_platform_set_signal_handler(SIGILL, &gate_function_guard_signal_handler, NULL);
504 6 gate_platform_set_signal_handler(SIGSEGV, &gate_function_guard_signal_handler, NULL);
505 #ifdef SIGSYS
506 6 gate_platform_set_signal_handler(SIGSYS, &gate_function_guard_signal_handler, NULL);
507 #endif
508 #ifdef SIGTRAP
509 6 gate_platform_set_signal_handler(SIGTRAP, &gate_function_guard_signal_handler, NULL);
510 #endif
511 6 return ret;
512 }
513
514 2 static unsigned translate_signal(int sig)
515 {
516
2/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2 switch (sig)
517 {
518 1 case SIGSEGV: return GATE_FUNCTION_FAULT_ADDRESS;
519 #ifdef SIGBUS
520 ✗ case SIGBUS: return GATE_FUNCTION_FAULT_ADDRESS;
521 #endif
522 ✗ case SIGABRT: return GATE_FUNCTION_FAULT_ADDRESS;
523
524 ✗ case SIGILL: return GATE_FUNCTION_FAULT_INSTRUCTION;
525 1 case SIGFPE: return GATE_FUNCTION_FAULT_ARITHMETIC;
526
527 #ifdef SIGSYS
528 ✗ case SIGSYS: return GATE_FUNCTION_FAULT_SYSTEM;
529 #endif
530 #ifdef SIGTRAP
531 ✗ case SIGTRAP: return GATE_FUNCTION_FAULT_BREAKPOINT;
532 #endif
533 }
534 ✗ return GATE_FUNCTION_FAULT_UNKNOWN;
535 }
536
537 2 static void gate_function_guard_signal_handler(int signum)
538 {
539 2 gate_function_anchor_t* ptr_anchor = NULL;
540 2 gate_result_t result = gate_thread_storage_get(&global_function_guard_registry, (void**)&ptr_anchor);
541
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (GATE_SUCCEEDED(result) && (ptr_anchor != NULL))
542 {
543 2 unsigned const fault_code = translate_signal(signum);
544 2 result = ptr_anchor->fault_handler(fault_code, ptr_anchor->fault_param);
545
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(result))
546 {
547 2 gate_callstack_jump(&ptr_anchor->context);
548 }
549 }
550 ✗ }
551
552 2 static gate_result_t GATE_CALL gate_function_guard_error_branch(void* param)
553 {
554 (void)param;
555 2 return GATE_RESULT_BADINSTRUCTION;
556 }
557
558 6 gate_result_t gate_function_invoke_guarded(gate_entrypoint_t entry_point, void* entry_param,
559 gate_function_fault_handler_t fault_handler, void* fault_param)
560 {
561 6 volatile gate_result_t ret = GATE_RESULT_FAILED;
562 gate_function_anchor_t new_anchor;
563 6 gate_function_anchor_t* ptr_old_anchor = NULL;
564
565 6 ret = gate_function_guard_init();
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (GATE_FAILED(ret))
567 {
568 ✗ return ret;
569 }
570
571 6 gate_thread_storage_get(&global_function_guard_registry, (void**)&ptr_old_anchor);
572 6 new_anchor.fault_handler = fault_handler;
573 6 new_anchor.fault_param = fault_param;
574
575 6 ret = gate_thread_storage_set(&global_function_guard_registry, &new_anchor);
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (GATE_FAILED(ret))
577 {
578 ✗ return ret;
579 }
580
581 6 ret = gate_callstack_fork(&new_anchor.context, entry_point, entry_param, &gate_function_guard_error_branch, NULL);
582
583 6 gate_thread_storage_set(&global_function_guard_registry, ptr_old_anchor);
584 6 return ret;
585 }
586
587 #endif /* GATE_FUNCTION_GUARD_SIGNALS */
588
589 #if defined(GATE_FUNCTION_GUARD_NOGUARD)
590
591 gate_result_t gate_function_invoke_guarded(gate_entrypoint_t entry_point, void* entry_param,
592 gate_function_fault_handler_t fault_handler, void* fault_param)
593 {
594 gate_result_t ret;
595
596 ret = entry_point(entry_param);
597 return ret;
598 }
599
600 #endif /* GATE_FUNCTION_GUARD_NOGUARD */
601
602