GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tests/gatecore_cpp_special_test/test_fault_guard.c
Date: 2026-09-21 06:26:40
Exec Total Coverage
Lines: 40 49 81.6%
Functions: 7 7 100.0%
Branches: 5 10 50.0%

Line Branch Exec Source
1 #include "gate/functions.h"
2 #include "gate/results.h"
3 #include "gate/console.h"
4 #include "gate/debugging.h"
5
6 1 static gate_result_t GATE_CALL no_fault_entry(void* arg)
7 {
8 GATE_UNUSED_ARG(arg);
9 1 return GATE_RESULT_OK;
10 }
11
12 1 static gate_result_t GATE_CALL seg_fault_entry(void* arg)
13 {
14 1 int volatile* volatile ptr = (int volatile*)arg;
15 1 volatile int n = *ptr;
16 1 *ptr = n;
17 1 return GATE_RESULT_OK;
18 }
19 static volatile int zero = 0;
20 1 static gate_result_t GATE_CALL div_fault_entry(void* arg)
21 {
22 1 int volatile value = 42;
23 int volatile result;
24
25 (void)arg;
26 1 result = value / zero; // cppcheck-suppress zerodiv
27 1 value = result * zero; // cppcheck-suppress unreadVariable
28 1 return GATE_RESULT_OK;
29 }
30
31 #if defined(GATE_SYS_WIN)
32 static volatile float zerof = 0.0f;
33 static gate_result_t GATE_CALL float_fault_entry(void* arg)
34 {
35 float value = 42.0f;
36 float result;
37
38 (void)arg;
39 result = value / zerof;
40 value = result * zerof; // cppcheck-suppress unreadVariable
41 return GATE_RESULT_OK;
42 }
43 #endif
44
45 2 static gate_result_t GATE_CALL fault_handler(unsigned fault_code, void* fault_arg)
46 {
47
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (fault_arg)
48 {
49 2 unsigned* volatile ptr = (unsigned* volatile)fault_arg; // cppcheck-suppress unreadVariable
50 2 *ptr = fault_code; // cppcheck-suppress uninitvar
51 }
52 else
53 {
54 GATE_DEBUG_TRACE("Missing fault_arg");
55 }
56 2 return GATE_RESULT_OK;
57 }
58
59 1 static gate_bool_t test_function_fault_succeed(gate_entrypoint_t entry, void* arg)
60 {
61 1 gate_result_t ret = true;
62 gate_result_t result;
63 1 unsigned fault_code = 0;
64
65 1 result = gate_function_invoke_guarded(entry, arg, &fault_handler, &fault_code);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(result))
67 {
68 GATE_DEBUG_TRACE("correct function failed to executed with fault guard");
69 ret = false;
70 }
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (fault_code != 0)
72 {
73 GATE_DEBUG_TRACE_MSG_VALUE("unexpected fault code", fault_code);
74 ret = false;
75 }
76 1 return ret;
77 }
78
79 2 static gate_bool_t test_function_fault_fail(gate_entrypoint_t entry, void* arg, unsigned expected_fault_code)
80 {
81 2 gate_bool_t ret = true;
82 gate_result_t result;
83 2 unsigned fault_code = 0;
84
85 2 result = gate_function_invoke_guarded(entry, arg, &fault_handler, &fault_code);
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != GATE_RESULT_BADINSTRUCTION)
87 {
88 GATE_DEBUG_TRACE_MSG_VALUE("unexpected result of bad-instruction function", result);
89 ret = false;
90 }
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (fault_code != expected_fault_code)
93 {
94 GATE_DEBUG_TRACE_MSG_VALUE("unexpected result of bad-instruction function", fault_code);
95 ret = false;
96 }
97
98 2 return ret;
99 }
100
101
102 1 gate_bool_t GATE_CALL test_fault_guard(void)
103 {
104 1 gate_bool_t succeeded = true;
105 #if !defined(GATE_SYS_BSD)
106 1 GATE_DEBUG_TRACE("Test correct function with fault guard");
107 1 succeeded &= test_function_fault_succeed(&no_fault_entry, NULL);
108 #if !defined(GATE_SYS_DOS)
109 1 GATE_DEBUG_TRACE("Test bad segfault function with fault guard");
110 1 succeeded &= test_function_fault_fail(&seg_fault_entry, NULL, GATE_FUNCTION_FAULT_ADDRESS);
111 1 GATE_DEBUG_TRACE("Test bad integer-error function with fault guard");
112 1 succeeded &= test_function_fault_fail(&div_fault_entry, NULL, GATE_FUNCTION_FAULT_ARITHMETIC);
113 #endif
114
115 #if defined(GATE_SYS_WIN)
116 // floating-point errors are not raised on posix platforms
117 GATE_DEBUG_TRACE("Test bad floating-point-error function with fault guard");
118 succeeded &= test_function_fault_fail(&float_fault_entry, NULL, GATE_FUNCTION_FAULT_FLOAT);
119 #endif
120
121 #endif
122 1 return succeeded;
123 }
124