authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-10-13 19:34:20+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-14 00:05:50-04:00
log0570df69b178c99f4d9d679a57d966f507dda484
tree7114064614b422868b1fa73f78d7b74c6b27a33d
parentea45ee54843b10a433bb223caa9706d8253e3222

stage1: Fix missing runtime safety check for intToPtr

Elide the alignment check if the pointer alignment is one, the null check must be preserved as it depends on the pointer type. Fixes #6667

2 files changed, 20 insertions(+), 10 deletions(-)

src/stage1/codegen.cpp+2-2
...@@ -3379,7 +3379,7 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable...@@ -3379,7 +3379,7 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable
3379 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);3379 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
3380 const uint32_t align_bytes = get_ptr_align(g, wanted_type);3380 const uint32_t align_bytes = get_ptr_align(g, wanted_type);
33813381
3382 if (ir_want_runtime_safety(g, &instruction->base) && align_bytes > 1) {3382 if (ir_want_runtime_safety(g, &instruction->base)) {
3383 ZigType *usize = g->builtin_types.entry_usize;3383 ZigType *usize = g->builtin_types.entry_usize;
3384 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);3384 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);
33853385
...@@ -3395,7 +3395,7 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable...@@ -3395,7 +3395,7 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable
3395 LLVMPositionBuilderAtEnd(g->builder, ok_block);3395 LLVMPositionBuilderAtEnd(g->builder, ok_block);
3396 }3396 }
33973397
3398 {3398 if (align_bytes > 1) {
3399 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);3399 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);
3400 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, target_val, alignment_minus_1, "");3400 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, target_val, alignment_minus_1, "");
3401 LLVMValueRef is_ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, zero, "");3401 LLVMValueRef is_ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, zero, "");
test/runtime_safety.zig+18-8
...@@ -13,7 +13,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -13,7 +13,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
1313
14 cases.addRuntimeSafety("slicing operator with sentinel",14 cases.addRuntimeSafety("slicing operator with sentinel",
15 \\const std = @import("std");15 \\const std = @import("std");
16 ++ check_panic_msg ++16 ++ check_panic_msg ++
17 \\pub fn main() void {17 \\pub fn main() void {
18 \\ var buf = [4]u8{'a','b','c',0};18 \\ var buf = [4]u8{'a','b','c',0};
19 \\ const slice = buf[0..4 :0];19 \\ const slice = buf[0..4 :0];
...@@ -21,7 +21,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -21,7 +21,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
21 );21 );
22 cases.addRuntimeSafety("slicing operator with sentinel",22 cases.addRuntimeSafety("slicing operator with sentinel",
23 \\const std = @import("std");23 \\const std = @import("std");
24 ++ check_panic_msg ++24 ++ check_panic_msg ++
25 \\pub fn main() void {25 \\pub fn main() void {
26 \\ var buf = [4]u8{'a','b','c',0};26 \\ var buf = [4]u8{'a','b','c',0};
27 \\ const slice = buf[0..:0];27 \\ const slice = buf[0..:0];
...@@ -29,7 +29,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -29,7 +29,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
29 );29 );
30 cases.addRuntimeSafety("slicing operator with sentinel",30 cases.addRuntimeSafety("slicing operator with sentinel",
31 \\const std = @import("std");31 \\const std = @import("std");
32 ++ check_panic_msg ++32 ++ check_panic_msg ++
33 \\pub fn main() void {33 \\pub fn main() void {
34 \\ var buf_zero = [0]u8{};34 \\ var buf_zero = [0]u8{};
35 \\ const slice = buf_zero[0..0 :0];35 \\ const slice = buf_zero[0..0 :0];
...@@ -37,7 +37,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -37,7 +37,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
37 );37 );
38 cases.addRuntimeSafety("slicing operator with sentinel",38 cases.addRuntimeSafety("slicing operator with sentinel",
39 \\const std = @import("std");39 \\const std = @import("std");
40 ++ check_panic_msg ++40 ++ check_panic_msg ++
41 \\pub fn main() void {41 \\pub fn main() void {
42 \\ var buf_zero = [0]u8{};42 \\ var buf_zero = [0]u8{};
43 \\ const slice = buf_zero[0..:0];43 \\ const slice = buf_zero[0..:0];
...@@ -45,7 +45,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -45,7 +45,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
45 );45 );
46 cases.addRuntimeSafety("slicing operator with sentinel",46 cases.addRuntimeSafety("slicing operator with sentinel",
47 \\const std = @import("std");47 \\const std = @import("std");
48 ++ check_panic_msg ++48 ++ check_panic_msg ++
49 \\pub fn main() void {49 \\pub fn main() void {
50 \\ var buf_sentinel = [2:0]u8{'a','b'};50 \\ var buf_sentinel = [2:0]u8{'a','b'};
51 \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0;51 \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0;
...@@ -54,7 +54,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -54,7 +54,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
54 );54 );
55 cases.addRuntimeSafety("slicing operator with sentinel",55 cases.addRuntimeSafety("slicing operator with sentinel",
56 \\const std = @import("std");56 \\const std = @import("std");
57 ++ check_panic_msg ++57 ++ check_panic_msg ++
58 \\pub fn main() void {58 \\pub fn main() void {
59 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };59 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };
60 \\ const slice = buf_slice[0..3 :0];60 \\ const slice = buf_slice[0..3 :0];
...@@ -62,7 +62,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -62,7 +62,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
62 );62 );
63 cases.addRuntimeSafety("slicing operator with sentinel",63 cases.addRuntimeSafety("slicing operator with sentinel",
64 \\const std = @import("std");64 \\const std = @import("std");
65 ++ check_panic_msg ++65 ++ check_panic_msg ++
66 \\pub fn main() void {66 \\pub fn main() void {
67 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };67 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };
68 \\ const slice = buf_slice[0.. :0];68 \\ const slice = buf_slice[0.. :0];
...@@ -354,7 +354,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -354,7 +354,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
354 \\}354 \\}
355 );355 );
356356
357 cases.addRuntimeSafety("@ptrToInt address zero to non-optional pointer",357 cases.addRuntimeSafety("@intToPtr address zero to non-optional pointer",
358 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {358 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
359 \\ @import("std").os.exit(126);359 \\ @import("std").os.exit(126);
360 \\}360 \\}
...@@ -364,6 +364,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -364,6 +364,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
364 \\}364 \\}
365 );365 );
366366
367 cases.addRuntimeSafety("@intToPtr address zero to non-optional byte-aligned pointer",
368 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
369 \\ @import("std").os.exit(126);
370 \\}
371 \\pub fn main() void {
372 \\ var zero: usize = 0;
373 \\ var b = @intToPtr(*u8, zero);
374 \\}
375 );
376
367 cases.addRuntimeSafety("pointer casting null to non-optional pointer",377 cases.addRuntimeSafety("pointer casting null to non-optional pointer",
368 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {378 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
369 \\ @import("std").os.exit(126);379 \\ @import("std").os.exit(126);