Skip to content

Commit 8a31aea

Browse files
authored
Add panic-ub test mode (#214)
The `panic` mode is currently used for both describing UB tests (e.g. translate null ptr deref as panic) and for describing translation gaps (panic in libcc2rs for types that don't implement ByteRepr). I renamed the first type as `panic-ub` and the second type as `panic`. So that the `Expectedly Failed Tests` in lit includes the second type, i.e. translation gaps.
1 parent 2d49e8b commit 8a31aea

26 files changed

Lines changed: 37 additions & 26 deletions

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
PTR_RE = re.compile(r"0x[0-9a-fA-F]+")
1919

2020
RE_XFAIL = re.compile(r"//\s*XFAIL:\s*(.*)")
21+
RE_PANIC_UB = re.compile(r"//\s*panic-ub\s*(?::\s*(.*))?$", re.MULTILINE)
2122
RE_PANIC = re.compile(r"//\s*panic\s*(?::\s*(.*))?$", re.MULTILINE)
2223
RE_NOCOMPILE = re.compile(r"//\s*no-compile\s*(?::\s*(.*))?$", re.MULTILINE)
2324
RE_TRANS_FAIL = re.compile(r"//\s*translation-fail\s*(?::\s*(.*))?$", re.MULTILINE)
@@ -27,6 +28,7 @@
2728
@dataclass
2829
class TestExpectations:
2930
should_panic: bool = False
31+
should_panic_ub: bool = False
3032
should_not_compile: bool = False
3133
should_not_translate: bool = False
3234
is_nondet_result: bool = False
@@ -47,6 +49,7 @@ def matches(match):
4749
xfail = xfail_m is not None and model in re.split(r"\s*,\s*", xfail_m.group(1))
4850
return cls(
4951
should_panic=matches(RE_PANIC.search(text)),
52+
should_panic_ub=matches(RE_PANIC_UB.search(text)),
5053
should_not_compile=matches(RE_NOCOMPILE.search(text)),
5154
should_not_translate=matches(RE_TRANS_FAIL.search(text)),
5255
is_nondet_result=matches(RE_NONDET.search(text)),
@@ -254,14 +257,22 @@ def run_rust(self):
254257
return None
255258
self.rust_result = RunResult(*lit.util.executeCommand(str(self.rust_bin)))
256259

257-
if exp.should_panic:
260+
if exp.should_panic_ub:
258261
err = str(self.rust_result.stderr)
259262
if not re.search(
260263
r"thread 'main' \(\d+\) panicked at", err
261264
) or self.rust_result.returncode not in [-6, 101]:
262265
return (exp.fail_code, "expected panic\n" + err)
263266
return self.success_result()
264267

268+
if exp.should_panic:
269+
err = str(self.rust_result.stderr)
270+
if not re.search(
271+
r"thread 'main' \(\d+\) panicked at", err
272+
) or self.rust_result.returncode not in [-6, 101]:
273+
return (exp.fail_code, "expected panic\n" + err)
274+
return (lit.Test.XFAIL, "")
275+
265276
if exp.is_nondet_result:
266277
return self.success_result()
267278
return None

tests/ub/dangling-prvalue-as-lvalue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55

66
#include <vector>
77

tests/ub/enum_out_of_range_cast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// panic
1+
// panic-ub
22

33
enum Color { RED, GREEN, BLUE };
44

tests/ub/enum_out_of_range_increment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// panic
1+
// panic-ub
22

33
enum color { RED, GREEN, BLUE };
44

tests/ub/max.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

44
// ub: unsafe
5-
// panic: refcount
5+
// panic-ub: refcount
66

77
#include <algorithm>
88

tests/ub/ub1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55
// nondet-result: unsafe
66
int &dangling() {
77
int x = 1;

tests/ub/ub10.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55
// nondet-result: unsafe
66
int main() {
77
int *arr = new int[10];

tests/ub/ub11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55
// nondet-result: unsafe
66
int main() {
77
int *element = new int(10);

tests/ub/ub12.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55
// nondet-result: unsafe
66
void escape(int *ptr) { delete ptr; }
77

tests/ub/ub13.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55
// nondet-result: unsafe
66
void escape(int *p) { delete p; }
77

0 commit comments

Comments
 (0)