aboutsummaryrefslogtreecommitdiff
path: root/test/debug_safety.zig
blob: 58ebf838b0c929b7d6a8af58fbb11ce55e914529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const tests = @import("tests.zig");

pub fn addCases(cases: &tests.CompareOutputContext) {
    cases.addDebugSafety("calling panic",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\pub fn main() -> %void {
        \\    @panic("oh no");
        \\}
    );

    cases.addDebugSafety("out of bounds slice access",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\pub fn main() -> %void {
        \\    const a = []i32{1, 2, 3, 4};
        \\    baz(bar(a));
        \\}
        \\fn bar(a: []const i32) -> i32 {
        \\    return a[4];
        \\}
        \\fn baz(a: i32) { }
    );

    cases.addDebugSafety("integer addition overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = add(65530, 10);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn add(a: u16, b: u16) -> u16 {
        \\    return a + b;
        \\}
    );

    cases.addDebugSafety("integer subtraction overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = sub(10, 20);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn sub(a: u16, b: u16) -> u16 {
        \\    return a - b;
        \\}
    );

    cases.addDebugSafety("integer multiplication overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = mul(300, 6000);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn mul(a: u16, b: u16) -> u16 {
        \\    return a * b;
        \\}
    );

    cases.addDebugSafety("integer negation overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = neg(-32768);
        \\    if (x == 32767) return error.Whatever;
        \\}
        \\fn neg(a: i16) -> i16 {
        \\    return -a;
        \\}
    );

    cases.addDebugSafety("signed integer division overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = div(-32768, -1);
        \\    if (x == 32767) return error.Whatever;
        \\}
        \\fn div(a: i16, b: i16) -> i16 {
        \\    return @divTrunc(a, b);
        \\}
    );

    cases.addDebugSafety("signed shift left overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = shl(-16385, 1);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn shl(a: i16, b: u4) -> i16 {
        \\    return @shlExact(a, b);
        \\}
    );

    cases.addDebugSafety("unsigned shift left overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = shl(0b0010111111111111, 3);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn shl(a: u16, b: u4) -> u16 {
        \\    return @shlExact(a, b);
        \\}
    );

    cases.addDebugSafety("signed shift right overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = shr(-16385, 1);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn shr(a: i16, b: u4) -> i16 {
        \\    return @shrExact(a, b);
        \\}
    );

    cases.addDebugSafety("unsigned shift right overflow",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = shr(0b0010111111111111, 3);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn shr(a: u16, b: u4) -> u16 {
        \\    return @shrExact(a, b);
        \\}
    );

    cases.addDebugSafety("integer division by zero",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = div0(999, 0);
        \\}
        \\fn div0(a: i32, b: i32) -> i32 {
        \\    return @divTrunc(a, b);
        \\}
    );

    cases.addDebugSafety("exact division failure",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = divExact(10, 3);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn divExact(a: i32, b: i32) -> i32 {
        \\    return @divExact(a, b);
        \\}
    );

    cases.addDebugSafety("cast []u8 to bigger slice of wrong size",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = widenSlice([]u8{1, 2, 3, 4, 5});
        \\    if (x.len == 0) return error.Whatever;
        \\}
        \\fn widenSlice(slice: []align(1) const u8) -> []align(1) const i32 {
        \\    return ([]align(1) const i32)(slice);
        \\}
    );

    cases.addDebugSafety("value does not fit in shortening cast",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = shorten_cast(200);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn shorten_cast(x: i32) -> i8 {
        \\    return i8(x);
        \\}
    );

    cases.addDebugSafety("signed integer not fitting in cast to unsigned integer",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    const x = unsigned_cast(-10);
        \\    if (x == 0) return error.Whatever;
        \\}
        \\fn unsigned_cast(x: i32) -> u32 {
        \\    return u32(x);
        \\}
    );

    cases.addDebugSafety("unwrap error",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Whatever;
        \\pub fn main() -> %void {
        \\    %%bar();
        \\}
        \\fn bar() -> %void {
        \\    return error.Whatever;
        \\}
    );

    cases.addDebugSafety("cast integer to error and no code matches",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\pub fn main() -> %void {
        \\    _ = bar(9999);
        \\}
        \\fn bar(x: u32) -> error {
        \\    return error(x);
        \\}
    );

    cases.addDebugSafety("@alignCast misaligned",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\error Wrong;
        \\pub fn main() -> %void {
        \\    var array align(4) = []u32{0x11111111, 0x11111111};
        \\    const bytes = ([]u8)(array[0..]);
        \\    if (foo(bytes) != 0x11111111) return error.Wrong;
        \\}
        \\fn foo(bytes: []u8) -> u32 {
        \\    const slice4 = bytes[1..5];
        \\    const int_slice = ([]u32)(@alignCast(4, slice4));
        \\    return int_slice[0];
        \\}
    );

    cases.addDebugSafety("bad union field access",
        \\pub fn panic(message: []const u8) -> noreturn {
        \\    @import("std").os.exit(126);
        \\}
        \\
        \\const Foo = union {
        \\    float: f32,
        \\    int: u32,
        \\};
        \\
        \\pub fn main() -> %void {
        \\    var f = Foo { .int = 42 };
        \\    bar(&f);
        \\}
        \\
        \\fn bar(f: &Foo) {
        \\    f.float = 12.34;
        \\}
    );
}