aboutsummaryrefslogtreecommitdiff
path: root/test/runtime_safety.zig
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-12-05 19:16:36 +0100
committerLemonBoy <thatlemon@gmail.com>2020-12-05 20:14:04 +0100
commitb45d2968e51cd22650b019f668056333337513e9 (patch)
treece0ddfbcd55e911dd1d12833fbeef06fcbd1879d /test/runtime_safety.zig
parent97f36d93f4766a8d225d19665309e825cdef72bf (diff)
downloadzig-b45d2968e51cd22650b019f668056333337513e9.tar.gz
zig-b45d2968e51cd22650b019f668056333337513e9.zip
Add some test cases for the previous commits
Diffstat (limited to 'test/runtime_safety.zig')
-rw-r--r--test/runtime_safety.zig79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/runtime_safety.zig b/test/runtime_safety.zig
index f4bcfa6f9f..2ab728b580 100644
--- a/test/runtime_safety.zig
+++ b/test/runtime_safety.zig
@@ -4,6 +4,85 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
{
const check_panic_msg =
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
+ \\ if (std.mem.eql(u8, message, "reached unreachable code")) {
+ \\ std.process.exit(126); // good
+ \\ }
+ \\ std.process.exit(0); // test failed
+ \\}
+ ;
+
+ cases.addRuntimeSafety("switch on corrupted enum value",
+ \\const std = @import("std");
+ ++ check_panic_msg ++
+ \\const E = enum(u32) {
+ \\ X = 1,
+ \\};
+ \\pub fn main() void {
+ \\ var e: E = undefined;
+ \\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
+ \\ switch (e) {
+ \\ .X => @breakpoint(),
+ \\ }
+ \\}
+ );
+
+ cases.addRuntimeSafety("switch on corrupted union value",
+ \\const std = @import("std");
+ ++ check_panic_msg ++
+ \\const U = union(enum(u32)) {
+ \\ X: u8,
+ \\};
+ \\pub fn main() void {
+ \\ var u: U = undefined;
+ \\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
+ \\ switch (u) {
+ \\ .X => @breakpoint(),
+ \\ }
+ \\}
+ );
+ }
+
+ {
+ const check_panic_msg =
+ \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
+ \\ if (std.mem.eql(u8, message, "invalid enum value")) {
+ \\ std.process.exit(126); // good
+ \\ }
+ \\ std.process.exit(0); // test failed
+ \\}
+ ;
+
+ cases.addRuntimeSafety("@tagName on corrupted enum value",
+ \\const std = @import("std");
+ ++ check_panic_msg ++
+ \\const E = enum(u32) {
+ \\ X = 1,
+ \\};
+ \\pub fn main() void {
+ \\ var e: E = undefined;
+ \\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
+ \\ var n = @tagName(e);
+ \\}
+ );
+
+ cases.addRuntimeSafety("@tagName on corrupted union value",
+ \\const std = @import("std");
+ ++ check_panic_msg ++
+ \\const U = union(enum(u32)) {
+ \\ X: u8,
+ \\};
+ \\pub fn main() void {
+ \\ var u: U = undefined;
+ \\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
+ \\ var t: @TagType(U) = u;
+ \\ var n = @tagName(t);
+ \\}
+ );
+ }
+
+ {
+ const check_panic_msg =
+ \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
\\ if (std.mem.eql(u8, message, "index out of bounds")) {
\\ std.process.exit(126); // good
\\ }