authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-05 22:24:25+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-07-05 22:24:25+00:00
logdcca5cf1a9241e7274ae03a587b7e3313d673f3e
tree6b4ca8bd860d267f3c75ba37e78d0acd9bc49753
parent289eab9177443bdfadfe750afda8f7f32f43be0f
parentb8553b4813e971e50dcae7b8181e5f6771f3dbff
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5797 from xackus/intcast-runtime-safety

stage1: `@intcast` runtime safety for unsigned -> signed of same bit count

5 files changed, 19 insertions(+), 7 deletions(-)

lib/std/special/compiler_rt/clzsi2_test.zig+1-1
......@@ -4,7 +4,7 @@ const testing = @import("std").testing;
44fn test__clzsi2(a: u32, expected: i32) void {
55 var nakedClzsi2 = clzsi2.__clzsi2;
66 var actualClzsi2 = @ptrCast(fn (a: i32) callconv(.C) i32, nakedClzsi2);
7 var x = @intCast(i32, a);
7 var x = @bitCast(i32, a);
88 var result = actualClzsi2(x);
99 testing.expectEqual(expected, result);
1010}
lib/std/special/compiler_rt/int.zig+1-1
......@@ -244,7 +244,7 @@ pub fn __udivsi3(n: u32, d: u32) callconv(.C) u32 {
244244 // r.all -= d.all;
245245 // carry = 1;
246246 // }
247 const s = @intCast(i32, d -% r -% 1) >> @intCast(u5, n_uword_bits - 1);
247 const s = @bitCast(i32, d -% r -% 1) >> @intCast(u5, n_uword_bits - 1);
248248 carry = @intCast(u32, s & 1);
249249 r -= d & @bitCast(u32, s);
250250 }
lib/std/special/compiler_rt/udivmod.zig+1-1
......@@ -184,7 +184,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
184184 // carry = 1;
185185 // }
186186 r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
187 const s: SignedDoubleInt = @intCast(SignedDoubleInt, b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
187 const s: SignedDoubleInt = @bitCast(SignedDoubleInt, b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
188188 carry = @intCast(u32, s & 1);
189189 r_all -= b & @bitCast(DoubleInt, s);
190190 r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421
src/codegen.cpp+6-4
......@@ -1535,9 +1535,11 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
15351535 zig_unreachable();
15361536 }
15371537
1538 if (actual_type->id == ZigTypeIdInt &&
1539 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
1540 want_runtime_safety)
1538 if (actual_type->id == ZigTypeIdInt && want_runtime_safety && (
1539 // negative to unsigned
1540 (!wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed) ||
1541 // unsigned would become negative
1542 (wanted_type->data.integral.is_signed && !actual_type->data.integral.is_signed && actual_bits == wanted_bits)))
15411543 {
15421544 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, actual_type));
15431545 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
......@@ -1547,7 +1549,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
15471549 LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
15481550
15491551 LLVMPositionBuilderAtEnd(g->builder, fail_block);
1550 gen_safety_crash(g, PanicMsgIdCastNegativeToUnsigned);
1552 gen_safety_crash(g, actual_type->data.integral.is_signed ? PanicMsgIdCastNegativeToUnsigned : PanicMsgIdCastTruncatedData);
15511553
15521554 LLVMPositionBuilderAtEnd(g->builder, ok_block);
15531555 }
test/runtime_safety.zig+10
......@@ -757,6 +757,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
757757 \\}
758758 );
759759
760 cases.addRuntimeSafety("unsigned integer not fitting in cast to signed integer - same bit count",
761 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
762 \\ @import("std").os.exit(126);
763 \\}
764 \\pub fn main() void {
765 \\ var value: u8 = 245;
766 \\ var casted = @intCast(i8, value);
767 \\}
768 );
769
760770 cases.addRuntimeSafety("unwrap error",
761771 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
762772 \\ if (@import("std").mem.eql(u8, message, "attempt to unwrap error: Whatever")) {