| 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 | static volatile float zerof = 0.0f; | ||
| 31 | ✗ | static gate_result_t GATE_CALL float_fault_entry(void* arg) | |
| 32 | { | ||
| 33 | ✗ | float value = 42.0f; | |
| 34 | float result; | ||
| 35 | |||
| 36 | (void)arg; | ||
| 37 | ✗ | result = value / zerof; | |
| 38 | ✗ | value = result * zerof; // cppcheck-suppress unreadVariable | |
| 39 | ✗ | return GATE_RESULT_OK; | |
| 40 | } | ||
| 41 | |||
| 42 | 2 | static gate_result_t GATE_CALL fault_handler(unsigned fault_code, void* fault_arg) | |
| 43 | { | ||
| 44 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (fault_arg) |
| 45 | { | ||
| 46 | 2 | unsigned* volatile ptr = (unsigned* volatile)fault_arg; // cppcheck-suppress unreadVariable | |
| 47 | 2 | *ptr = fault_code; // cppcheck-suppress uninitvar | |
| 48 | } | ||
| 49 | else | ||
| 50 | { | ||
| 51 | ✗ | GATE_DEBUG_TRACE("Missing fault_arg"); | |
| 52 | } | ||
| 53 | 2 | return GATE_RESULT_OK; | |
| 54 | } | ||
| 55 | |||
| 56 | 1 | static gate_bool_t test_function_fault_succeed(gate_entrypoint_t entry, void* arg) | |
| 57 | { | ||
| 58 | 1 | gate_result_t ret = true; | |
| 59 | gate_result_t result; | ||
| 60 | 1 | unsigned fault_code = 0; | |
| 61 | |||
| 62 | 1 | result = gate_function_invoke_guarded(entry, arg, &fault_handler, &fault_code); | |
| 63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (GATE_FAILED(result)) |
| 64 | { | ||
| 65 | ✗ | GATE_DEBUG_TRACE("correct function failed to executed with fault guard"); | |
| 66 | ✗ | ret = false; | |
| 67 | } | ||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (fault_code != 0) |
| 69 | { | ||
| 70 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("unexpected fault code", fault_code); | |
| 71 | ✗ | ret = false; | |
| 72 | } | ||
| 73 | 1 | return ret; | |
| 74 | } | ||
| 75 | |||
| 76 | 2 | static gate_bool_t test_function_fault_fail(gate_entrypoint_t entry, void* arg, unsigned expected_fault_code) | |
| 77 | { | ||
| 78 | 2 | gate_bool_t ret = true; | |
| 79 | gate_result_t result; | ||
| 80 | 2 | unsigned fault_code = 0; | |
| 81 | |||
| 82 | 2 | result = gate_function_invoke_guarded(entry, arg, &fault_handler, &fault_code); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (result != GATE_RESULT_BADINSTRUCTION) |
| 84 | { | ||
| 85 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("unexpected result of bad-instruction function", result); | |
| 86 | ✗ | ret = false; | |
| 87 | } | ||
| 88 | |||
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (fault_code != expected_fault_code) |
| 90 | { | ||
| 91 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("unexpected result of bad-instruction function", fault_code); | |
| 92 | ✗ | ret = false; | |
| 93 | } | ||
| 94 | |||
| 95 | 2 | return ret; | |
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | 1 | gate_bool_t test_fault_guard() | |
| 100 | { | ||
| 101 | 1 | gate_bool_t succeeded = true; | |
| 102 | #if !defined(GATE_SYS_BSD) | ||
| 103 | 1 | GATE_DEBUG_TRACE("Test correct function with fault guard"); | |
| 104 | 1 | succeeded &= test_function_fault_succeed(&no_fault_entry, NULL); | |
| 105 | #if !defined(GATE_SYS_DOS) | ||
| 106 | 1 | GATE_DEBUG_TRACE("Test bad segfault function with fault guard"); | |
| 107 | 1 | succeeded &= test_function_fault_fail(&seg_fault_entry, NULL, GATE_FUNCTION_FAULT_ADDRESS); | |
| 108 | 1 | GATE_DEBUG_TRACE("Test bad integer-error function with fault guard"); | |
| 109 | 1 | succeeded &= test_function_fault_fail(&div_fault_entry, NULL, GATE_FUNCTION_FAULT_ARITHMETIC); | |
| 110 | #endif | ||
| 111 | |||
| 112 | #if defined(GATE_SYS_WIN) | ||
| 113 | // floating-point errors are not raised on | ||
| 114 | GATE_DEBUG_TRACE("Test bad floating-point-error function with fault guard"); | ||
| 115 | succeeded &= test_function_fault_fail(&float_fault_entry, NULL, GATE_FUNCTION_FAULT_FLOAT); | ||
| 116 | #endif | ||
| 117 | |||
| 118 | #endif | ||
| 119 | 1 | return succeeded; | |
| 120 | } | ||
| 121 |