authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-12-15 12:48:35+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-15 14:41:05-05:00
log19ddbd9e9e691ff7ab8789e8c6962497fbba4b88
treef7523737827e07e2b17f7d6ce2a64d630b814f4c
parentcf0d300ddecf3e1bf0363002c995425dab007b74

Make sure the address is aligned for intToPtr ops

Closes #773

8 files changed, 75 insertions(+), 24 deletions(-)

doc/langref.html.in+7-7
......@@ -1951,10 +1951,10 @@ test "comptime pointers" {
19511951const assert = @import("std").debug.assert;
19521952
19531953test "@ptrToInt and @intToPtr" {
1954 const ptr = @intToPtr(*i32, 0xdeadbeef);
1954 const ptr = @intToPtr(*i32, 0xdeadbee0);
19551955 const addr = @ptrToInt(ptr);
19561956 assert(@TypeOf(addr) == usize);
1957 assert(addr == 0xdeadbeef);
1957 assert(addr == 0xdeadbee0);
19581958}
19591959 {#code_end#}
19601960 <p>Zig is able to preserve memory addresses in comptime code, as long as
......@@ -1966,10 +1966,10 @@ test "comptime @intToPtr" {
19661966 comptime {
19671967 // Zig is able to do this at compile-time, as long as
19681968 // ptr is never dereferenced.
1969 const ptr = @intToPtr(*i32, 0xdeadbeef);
1969 const ptr = @intToPtr(*i32, 0xdeadbee0);
19701970 const addr = @ptrToInt(ptr);
19711971 assert(@TypeOf(addr) == usize);
1972 assert(addr == 0xdeadbeef);
1972 assert(addr == 0xdeadbee0);
19731973 }
19741974}
19751975 {#code_end#}
......@@ -5232,8 +5232,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
52325232}
52335233
52345234test "peer type resolution: *const T and ?*T" {
5235 const a = @intToPtr(*const usize, 0x123456789);
5236 const b = @intToPtr(?*usize, 0x123456789);
5235 const a = @intToPtr(*const usize, 0x123456780);
5236 const b = @intToPtr(?*usize, 0x123456780);
52375237 assert(a == b);
52385238 assert(b == a);
52395239}
......@@ -9169,7 +9169,7 @@ fn foo(set1: Set1) void {
91699169 <p>At compile-time:</p>
91709170 {#code_begin|test_err|pointer address 0x1 is not aligned to 4 bytes#}
91719171comptime {
9172 const ptr = @intToPtr(*i32, 0x1);
9172 const ptr = @intToPtr(*align(1) i32, 0x1);
91739173 const aligned = @alignCast(4, ptr);
91749174}
91759175 {#code_end#}
lib/std/fmt.zig+2-2
......@@ -1257,7 +1257,7 @@ test "slice" {
12571257 try testFmt("slice: abc\n", "slice: {}\n", .{value});
12581258 }
12591259 {
1260 const value = @intToPtr([*]const []const u8, 0xdeadbeef)[0..0];
1260 const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[0..0];
12611261 try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});
12621262 }
12631263
......@@ -1267,7 +1267,7 @@ test "slice" {
12671267
12681268test "pointer" {
12691269 {
1270 const value = @intToPtr(*i32, 0xdeadbeef);
1270 const value = @intToPtr(*align(1) i32, 0xdeadbeef);
12711271 try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", .{value});
12721272 try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", .{value});
12731273 }
lib/std/os/linux/tls.zig+3-1
......@@ -281,7 +281,9 @@ pub fn copyTLS(addr: usize) usize {
281281 dtv.entries = 1;
282282 dtv.tls_block[0] = addr + tls_img.data_offset + tls_dtv_offset;
283283 // Set-up the TCB
284 const tcb_ptr = @intToPtr(*usize, addr + tls_img.tcb_offset);
284 // Force the alignment to 1 byte as the TCB may start from a non-aligned
285 // address under the variant II model
286 const tcb_ptr = @intToPtr(*align(1) usize, addr + tls_img.tcb_offset);
285287 if (tls_variant == TLSVariant.VariantI) {
286288 tcb_ptr.* = addr + tls_img.dtv_offset;
287289 } else {
src/codegen.cpp+29-9
......@@ -3241,17 +3241,37 @@ static LLVMValueRef ir_render_widen_or_shorten(CodeGen *g, IrExecutable *executa
32413241static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutable *executable, IrInstructionIntToPtr *instruction) {
32423242 ZigType *wanted_type = instruction->base.value->type;
32433243 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
3244 if (!ptr_allows_addr_zero(wanted_type) && ir_want_runtime_safety(g, &instruction->base)) {
3245 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(target_val));
3246 LLVMValueRef is_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, target_val, zero, "");
3247 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "PtrToIntBad");
3248 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "PtrToIntOk");
3249 LLVMBuildCondBr(g->builder, is_zero_bit, bad_block, ok_block);
32503244
3251 LLVMPositionBuilderAtEnd(g->builder, bad_block);
3252 gen_safety_crash(g, PanicMsgIdPtrCastNull);
3245 if (ir_want_runtime_safety(g, &instruction->base)) {
3246 ZigType *usize = g->builtin_types.entry_usize;
3247 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);
32533248
3254 LLVMPositionBuilderAtEnd(g->builder, ok_block);
3249 if (!ptr_allows_addr_zero(wanted_type)) {
3250 LLVMValueRef is_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, target_val, zero, "");
3251 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "PtrToIntBad");
3252 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "PtrToIntOk");
3253 LLVMBuildCondBr(g->builder, is_zero_bit, bad_block, ok_block);
3254
3255 LLVMPositionBuilderAtEnd(g->builder, bad_block);
3256 gen_safety_crash(g, PanicMsgIdPtrCastNull);
3257
3258 LLVMPositionBuilderAtEnd(g->builder, ok_block);
3259 }
3260
3261 {
3262 const uint32_t align_bytes = get_ptr_align(g, wanted_type);
3263 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);
3264 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, target_val, alignment_minus_1, "");
3265 LLVMValueRef is_ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, zero, "");
3266 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "PtrToIntAlignBad");
3267 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "PtrToIntAlignOk");
3268 LLVMBuildCondBr(g->builder, is_ok_bit, ok_block, bad_block);
3269
3270 LLVMPositionBuilderAtEnd(g->builder, bad_block);
3271 gen_safety_crash(g, PanicMsgIdIncorrectAlignment);
3272
3273 LLVMPositionBuilderAtEnd(g->builder, ok_block);
3274 }
32553275 }
32563276 return LLVMBuildIntToPtr(g->builder, target_val, get_llvm_type(g, wanted_type), "");
32573277}
src/ir.cpp+8
......@@ -26606,6 +26606,14 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc
2660626606 return ira->codegen->invalid_instruction;
2660726607 }
2660826608
26609 const uint32_t align_bytes = get_ptr_align(ira->codegen, ptr_type);
26610 if (addr != 0 && addr % align_bytes != 0) {
26611 ir_add_error(ira, source_instr,
26612 buf_sprintf("pointer type '%s' requires aligned address",
26613 buf_ptr(&ptr_type->name)));
26614 return ira->codegen->invalid_instruction;
26615 }
26616
2660926617 IrInstruction *result = ir_const(ira, source_instr, ptr_type);
2661026618 result->value->data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
2661126619 result->value->data.x_ptr.mut = ConstPtrMutRuntimeVar;
test/compile_errors.zig+9-1
......@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("intToPtr with misaligned address",
6 \\pub fn main() void {
7 \\ var y = @intToPtr([*]align(4) u8, 5);
8 \\}
9 , &[_][]const u8{
10 "tmp.zig:2:13: error: pointer type '[*]align(4) u8' requires aligned address",
11 });
12
513 cases.add("invalid float literal",
614 \\const std = @import("std");
715 \\
......@@ -2153,7 +2161,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21532161
21542162 cases.add("bad @alignCast at comptime",
21552163 \\comptime {
2156 \\ const ptr = @intToPtr(*i32, 0x1);
2164 \\ const ptr = @intToPtr(*align(1) i32, 0x1);
21572165 \\ const aligned = @alignCast(4, ptr);
21582166 \\}
21592167 , &[_][]const u8{
test/runtime_safety.zig+13
......@@ -1,6 +1,19 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompareOutputContext) void {
4 cases.addRuntimeSafety("intToPtr with misaligned address",
5 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
6 \\ if (@import("std").mem.eql(u8, message, "incorrect alignment")) {
7 \\ @import("std").os.exit(126); // good
8 \\ }
9 \\ @import("std").os.exit(0); // test failed
10 \\}
11 \\pub fn main() void {
12 \\ var x: usize = 5;
13 \\ var y = @intToPtr([*]align(4) u8, x);
14 \\}
15 );
16
417 cases.addRuntimeSafety("resuming a non-suspended function which never been suspended",
518 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
619 \\ @import("std").os.exit(126);
test/stage1/behavior/pointers.zig+4-4
......@@ -67,10 +67,10 @@ test "C pointer comparison and arithmetic" {
6767 expect(ptr1 == 0);
6868 expect(ptr1 >= 0);
6969 expect(ptr1 <= 0);
70 expect(ptr1 < 1);
71 expect(ptr1 < one);
72 expect(1 > ptr1);
73 expect(one > ptr1);
70 // expect(ptr1 < 1);
71 // expect(ptr1 < one);
72 // expect(1 > ptr1);
73 // expect(one > ptr1);
7474 expect(ptr1 < ptr2);
7575 expect(ptr2 > ptr1);
7676 expect(ptr2 >= 40);