| 1 |
""" |
| 2 |
Test the BoundsSafety instrumentation plugin |
| 3 |
""" |
| 4 |
|
| 5 |
import lldb |
| 6 |
from lldbsuite.test.lldbtest import * |
| 7 |
from lldbsuite.test.decorators import * |
| 8 |
|
| 9 |
|
| 10 |
STOP_REASON_MAX_LEN = 100 |
| 11 |
SOFT_TRAP_FUNC_MINIMAL = "__bounds_safety_soft_trap" |
| 12 |
SOFT_TRAP_FUNC_WITH_STR = "__bounds_safety_soft_trap_s" |
| 13 |
|
| 14 |
|
| 15 |
class BoundsSafetyTestSoftTrapPlugin(TestBase): |
| 16 |
def _check_stop_reason_impl( |
| 17 |
self, |
| 18 |
expected_soft_trap_func: str, |
| 19 |
expected_stop_reason: str, |
| 20 |
expected_func_name: str, |
| 21 |
expected_file_name: str, |
| 22 |
expected_line_num: int, |
| 23 |
): |
| 24 |
process = self.test_target.process |
| 25 |
thread = process.GetSelectedThread() |
| 26 |
self.assertEqual( |
| 27 |
thread.GetStopReason(), |
| 28 |
lldb.eStopReasonInstrumentation, |
| 29 |
) |
| 30 |
|
| 31 |
stop_reason = thread.GetStopDescription(STOP_REASON_MAX_LEN) |
| 32 |
self.assertEqual(stop_reason, expected_stop_reason) |
| 33 |
|
| 34 |
soft_trap_func_frame = thread.GetFrameAtIndex(0) |
| 35 |
self.assertEqual(soft_trap_func_frame.name, expected_soft_trap_func) |
| 36 |
|
| 37 |
stop_frame = thread.GetSelectedFrame() |
| 38 |
self.assertEqual(stop_frame.name, expected_func_name) |
| 39 |
# The stop frame isn't frame 1 because that frame is the artificial |
| 40 |
# frame containing the trap reason. |
| 41 |
self.assertEqual(stop_frame.idx, 2) |
| 42 |
file_name = stop_frame.GetLineEntry().GetFileSpec().basename |
| 43 |
self.assertEqual(file_name, expected_file_name) |
| 44 |
line = stop_frame.GetLineEntry().line |
| 45 |
self.assertEqual(line, expected_line_num) |
| 46 |
|
| 47 |
def check_state_soft_trap_minimal( |
| 48 |
self, stop_reason: str, func_name: str, file_name: str, line_num: int |
| 49 |
): |
| 50 |
""" |
| 51 |
Check the program state is as expected when hitting |
| 52 |
a soft trap from -fbounds-safety-soft-traps=call-minimal |
| 53 |
""" |
| 54 |
self._check_stop_reason_impl( |
| 55 |
SOFT_TRAP_FUNC_MINIMAL, |
| 56 |
expected_stop_reason=stop_reason, |
| 57 |
expected_func_name=func_name, |
| 58 |
expected_file_name=file_name, |
| 59 |
expected_line_num=line_num, |
| 60 |
) |
| 61 |
|
| 62 |
def check_state_soft_trap_with_str( |
| 63 |
self, stop_reason: str, func_name: str, file_name: str, line_num: int |
| 64 |
): |
| 65 |
""" |
| 66 |
Check the program state is as expected when hitting |
| 67 |
a soft trap from -fbounds-safety-soft-traps=call-with_str |
| 68 |
""" |
| 69 |
self._check_stop_reason_impl( |
| 70 |
SOFT_TRAP_FUNC_WITH_STR, |
| 71 |
expected_stop_reason=stop_reason, |
| 72 |
expected_func_name=func_name, |
| 73 |
expected_file_name=file_name, |
| 74 |
expected_line_num=line_num, |
| 75 |
) |
| 76 |
|
| 77 |
# Skip the tests on Windows because they fail due to the stop reason |
| 78 |
# being `eStopReasonNon` instead of the expected |
| 79 |
# `eStopReasonInstrumentation`. |
| 80 |
@skipIfWindows |
| 81 |
@skipUnlessBoundsSafety |
| 82 |
def test_call_minimal(self): |
| 83 |
""" |
| 84 |
Test the plugin on code built with |
| 85 |
-fbounds-safety-soft-traps=call-minimal |
| 86 |
""" |
| 87 |
self.build(make_targets=["soft-trap-test-minimal"]) |
| 88 |
self.test_target = self.createTestTarget() |
| 89 |
self.runCmd("run") |
| 90 |
|
| 91 |
process = self.test_target.process |
| 92 |
|
| 93 |
# First soft trap hit |
| 94 |
self.check_state_soft_trap_minimal( |
| 95 |
"Soft Bounds check failed: indexing above upper bound in 'buffer[2]'", |
| 96 |
"main", |
| 97 |
"main.c", |
| 98 |
7, |
| 99 |
) |
| 100 |
|
| 101 |
process.Continue() |
| 102 |
|
| 103 |
# Second soft trap hit |
| 104 |
self.check_state_soft_trap_minimal( |
| 105 |
"Soft Bounds check failed: indexing below lower bound in 'buffer[-1]'", |
| 106 |
"main", |
| 107 |
"main.c", |
| 108 |
8, |
| 109 |
) |
| 110 |
|
| 111 |
process.Continue() |
| 112 |
self.assertEqual(process.GetState(), lldb.eStateExited) |
| 113 |
self.assertEqual(process.GetExitStatus(), 0) |
| 114 |
|
| 115 |
@skipIfWindows |
| 116 |
@skipUnlessBoundsSafety |
| 117 |
def test_call_with_str(self): |
| 118 |
""" |
| 119 |
Test the plugin on code built with |
| 120 |
-fbounds-safety-soft-traps=call-with-str |
| 121 |
""" |
| 122 |
self.build(make_targets=["soft-trap-test-with-str"]) |
| 123 |
self.test_target = self.createTestTarget() |
| 124 |
self.runCmd("run") |
| 125 |
|
| 126 |
process = self.test_target.process |
| 127 |
|
| 128 |
# First soft trap hit |
| 129 |
self.check_state_soft_trap_with_str( |
| 130 |
"Soft Bounds check failed: indexing above upper bound in 'buffer[2]'", |
| 131 |
"main", |
| 132 |
"main.c", |
| 133 |
7, |
| 134 |
) |
| 135 |
|
| 136 |
process.Continue() |
| 137 |
|
| 138 |
# Second soft trap hit |
| 139 |
self.check_state_soft_trap_with_str( |
| 140 |
"Soft Bounds check failed: indexing below lower bound in 'buffer[-1]'", |
| 141 |
"main", |
| 142 |
"main.c", |
| 143 |
8, |
| 144 |
) |
| 145 |
|
| 146 |
process.Continue() |
| 147 |
self.assertEqual(process.GetState(), lldb.eStateExited) |
| 148 |
self.assertEqual(process.GetExitStatus(), 0) |