Merge pull request #12286 from ziglang/division-safety

Division safety improvements
This commit is contained in:
Andrew Kelley 2022-07-29 11:03:27 -07:00 committed by GitHub
commit 64dc1b05d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 698 additions and 376 deletions

View File

@ -63,8 +63,7 @@ stage3/bin/zig build test-translate-c -fqemu -fwasmtime -Denable-llvm
stage3/bin/zig build test-run-translated-c -fqemu -fwasmtime -Denable-llvm
stage3/bin/zig build test-standalone -fqemu -fwasmtime -Denable-llvm
stage3/bin/zig build test-cli -fqemu -fwasmtime -Denable-llvm
# https://github.com/ziglang/zig/issues/12144
stage3/bin/zig build test-cases -fqemu -fwasmtime
stage3/bin/zig build test-cases -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
stage3/bin/zig build test-link -fqemu -fwasmtime -Denable-llvm
$STAGE1_ZIG build test-stack-traces -fqemu -fwasmtime

View File

@ -1784,6 +1784,7 @@ pub fn updateSegfaultHandler(act: ?*const os.Sigaction) error{OperationNotSuppor
try os.sigaction(os.SIG.SEGV, act, null);
try os.sigaction(os.SIG.ILL, act, null);
try os.sigaction(os.SIG.BUS, act, null);
try os.sigaction(os.SIG.FPE, act, null);
}
/// Attaches a global SIGSEGV handler which calls @panic("segmentation fault");
@ -1845,6 +1846,7 @@ fn handleSegfaultPosix(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const any
os.SIG.SEGV => stderr.print("Segmentation fault at address 0x{x}\n", .{addr}),
os.SIG.ILL => stderr.print("Illegal instruction at address 0x{x}\n", .{addr}),
os.SIG.BUS => stderr.print("Bus error at address 0x{x}\n", .{addr}),
os.SIG.FPE => stderr.print("Arithmetic exception at address 0x{x}\n", .{addr}),
else => unreachable,
} catch os.abort();
}

View File

@ -111,8 +111,9 @@ pub const Inst = struct {
div_floor,
/// Same as `div_floor` with optimized float mode.
div_floor_optimized,
/// Integer or float division. Guaranteed no remainder.
/// For integers, wrapping is undefined behavior.
/// Integer or float division.
/// If a remainder would be produced, undefined behavior occurs.
/// For integers, overflow is undefined behavior.
/// Both operands are guaranteed to be the same type, and the result type
/// is the same as both operands.
/// Uses the `bin_op` field.

File diff suppressed because it is too large Load Diff

View File

@ -5201,10 +5201,20 @@ pub const Type = extern union {
};
}
// Works for vectors and vectors of integers.
pub fn minInt(ty: Type, arena: Allocator, target: Target) !Value {
const scalar = try minIntScalar(ty.scalarType(), arena, target);
if (ty.zigTypeTag() == .Vector) {
return Value.Tag.repeated.create(arena, scalar);
} else {
return scalar;
}
}
/// Asserts that self.zigTypeTag() == .Int.
pub fn minInt(self: Type, arena: Allocator, target: Target) !Value {
assert(self.zigTypeTag() == .Int);
const info = self.intInfo(target);
pub fn minIntScalar(ty: Type, arena: Allocator, target: Target) !Value {
assert(ty.zigTypeTag() == .Int);
const info = ty.intInfo(target);
if (info.signedness == .unsigned) {
return Value.zero;

View File

@ -3,14 +3,14 @@ pub const List = struct {
allocator: *Allocator,
pub fn init(allocator: *Allocator) List {
return List {
return List{
.len = 0,
.allocator = allocator,
};
}
};
pub var global_allocator = Allocator {
pub var global_allocator = Allocator{
.field = 1234,
};
@ -28,4 +28,4 @@ export fn foo() void {
// target=native
//
// :23:6: error: no field or member function named 'init' in 'tmp.List'
// :1:14: note: struct declared here
// :1:18: note: struct declared here

View File

@ -1,12 +0,0 @@
pub fn main() void {
var i: u32 = 16;
assert(i >> 1, 8);
}
fn assert(a: u32, b: u32) void {
if (a != b) unreachable;
}
// run
// backend=llvm
// target=x86_64-linux,x86_64-macos
//

View File

@ -1,10 +0,0 @@
pub fn main() void {
var i: u32 = 16;
assert(i << 1, 32);
}
fn assert(a: u32, b: u32) void {
if (a != b) unreachable;
}
// run
//