Skip to content

Commit b45d296

Browse files
committed
Add some test cases for the previous commits
1 parent 97f36d9 commit b45d296

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

test/runtime_safety.zig

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
11
const tests = @import("tests.zig");
22

33
pub fn addCases(cases: *tests.CompareOutputContext) void {
4+
{
5+
const check_panic_msg =
6+
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
7+
\\ if (std.mem.eql(u8, message, "reached unreachable code")) {
8+
\\ std.process.exit(126); // good
9+
\\ }
10+
\\ std.process.exit(0); // test failed
11+
\\}
12+
;
13+
14+
cases.addRuntimeSafety("switch on corrupted enum value",
15+
\\const std = @import("std");
16+
++ check_panic_msg ++
17+
\\const E = enum(u32) {
18+
\\ X = 1,
19+
\\};
20+
\\pub fn main() void {
21+
\\ var e: E = undefined;
22+
\\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
23+
\\ switch (e) {
24+
\\ .X => @breakpoint(),
25+
\\ }
26+
\\}
27+
);
28+
29+
cases.addRuntimeSafety("switch on corrupted union value",
30+
\\const std = @import("std");
31+
++ check_panic_msg ++
32+
\\const U = union(enum(u32)) {
33+
\\ X: u8,
34+
\\};
35+
\\pub fn main() void {
36+
\\ var u: U = undefined;
37+
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
38+
\\ switch (u) {
39+
\\ .X => @breakpoint(),
40+
\\ }
41+
\\}
42+
);
43+
}
44+
45+
{
46+
const check_panic_msg =
47+
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
48+
\\ if (std.mem.eql(u8, message, "invalid enum value")) {
49+
\\ std.process.exit(126); // good
50+
\\ }
51+
\\ std.process.exit(0); // test failed
52+
\\}
53+
;
54+
55+
cases.addRuntimeSafety("@tagName on corrupted enum value",
56+
\\const std = @import("std");
57+
++ check_panic_msg ++
58+
\\const E = enum(u32) {
59+
\\ X = 1,
60+
\\};
61+
\\pub fn main() void {
62+
\\ var e: E = undefined;
63+
\\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
64+
\\ var n = @tagName(e);
65+
\\}
66+
);
67+
68+
cases.addRuntimeSafety("@tagName on corrupted union value",
69+
\\const std = @import("std");
70+
++ check_panic_msg ++
71+
\\const U = union(enum(u32)) {
72+
\\ X: u8,
73+
\\};
74+
\\pub fn main() void {
75+
\\ var u: U = undefined;
76+
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
77+
\\ var t: @TagType(U) = u;
78+
\\ var n = @tagName(t);
79+
\\}
80+
);
81+
}
82+
483
{
584
const check_panic_msg =
685
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {

0 commit comments

Comments
 (0)