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
33793379 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
33803380 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)) {
33833383 ZigType *usize = g->builtin_types.entry_usize;
33843384 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);
33853385
......@@ -3395,7 +3395,7 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable
33953395 LLVMPositionBuilderAtEnd(g->builder, ok_block);
33963396 }
33973397
3398 {
3398 if (align_bytes > 1) {
33993399 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);
34003400 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, target_val, alignment_minus_1, "");
34013401 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 {
1313
1414 cases.addRuntimeSafety("slicing operator with sentinel",
1515 \\const std = @import("std");
16 ++ check_panic_msg ++
16 ++ check_panic_msg ++
1717 \\pub fn main() void {
1818 \\ var buf = [4]u8{'a','b','c',0};
1919 \\ const slice = buf[0..4 :0];
......@@ -21,7 +21,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
2121 );
2222 cases.addRuntimeSafety("slicing operator with sentinel",
2323 \\const std = @import("std");
24 ++ check_panic_msg ++
24 ++ check_panic_msg ++
2525 \\pub fn main() void {
2626 \\ var buf = [4]u8{'a','b','c',0};
2727 \\ const slice = buf[0..:0];
......@@ -29,7 +29,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
2929 );
3030 cases.addRuntimeSafety("slicing operator with sentinel",
3131 \\const std = @import("std");
32 ++ check_panic_msg ++
32 ++ check_panic_msg ++
3333 \\pub fn main() void {
3434 \\ var buf_zero = [0]u8{};
3535 \\ const slice = buf_zero[0..0 :0];
......@@ -37,7 +37,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
3737 );
3838 cases.addRuntimeSafety("slicing operator with sentinel",
3939 \\const std = @import("std");
40 ++ check_panic_msg ++
40 ++ check_panic_msg ++
4141 \\pub fn main() void {
4242 \\ var buf_zero = [0]u8{};
4343 \\ const slice = buf_zero[0..:0];
......@@ -45,7 +45,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
4545 );
4646 cases.addRuntimeSafety("slicing operator with sentinel",
4747 \\const std = @import("std");
48 ++ check_panic_msg ++
48 ++ check_panic_msg ++
4949 \\pub fn main() void {
5050 \\ var buf_sentinel = [2:0]u8{'a','b'};
5151 \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0;
......@@ -54,7 +54,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
5454 );
5555 cases.addRuntimeSafety("slicing operator with sentinel",
5656 \\const std = @import("std");
57 ++ check_panic_msg ++
57 ++ check_panic_msg ++
5858 \\pub fn main() void {
5959 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };
6060 \\ const slice = buf_slice[0..3 :0];
......@@ -62,7 +62,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
6262 );
6363 cases.addRuntimeSafety("slicing operator with sentinel",
6464 \\const std = @import("std");
65 ++ check_panic_msg ++
65 ++ check_panic_msg ++
6666 \\pub fn main() void {
6767 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };
6868 \\ const slice = buf_slice[0.. :0];
......@@ -354,7 +354,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
354354 \\}
355355 );
356356
357 cases.addRuntimeSafety("@ptrToInt address zero to non-optional pointer",
357 cases.addRuntimeSafety("@intToPtr address zero to non-optional pointer",
358358 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
359359 \\ @import("std").os.exit(126);
360360 \\}
......@@ -364,6 +364,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
364364 \\}
365365 );
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
367377 cases.addRuntimeSafety("pointer casting null to non-optional pointer",
368378 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
369379 \\ @import("std").os.exit(126);