Skip to content

Commit c45dcd0

Browse files
authored
Merge pull request #22488 from Rexicon226/ubsan-rt
implement a ubsan runtime for better error messages
2 parents e0a955a + ca83f52 commit c45dcd0

File tree

23 files changed

+958
-28
lines changed

23 files changed

+958
-28
lines changed

lib/std/Build/Step/Compile.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ compress_debug_sections: enum { none, zlib, zstd } = .none,
4040
verbose_link: bool,
4141
verbose_cc: bool,
4242
bundle_compiler_rt: ?bool = null,
43+
bundle_ubsan_rt: ?bool = null,
4344
rdynamic: bool,
4445
import_memory: bool = false,
4546
export_memory: bool = false,
@@ -1563,6 +1564,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
15631564
}
15641565

15651566
try addFlag(&zig_args, "compiler-rt", compile.bundle_compiler_rt);
1567+
try addFlag(&zig_args, "ubsan-rt", compile.bundle_ubsan_rt);
15661568
try addFlag(&zig_args, "dll-export-fns", compile.dll_export_fns);
15671569
if (compile.rdynamic) {
15681570
try zig_args.append("-rdynamic");

lib/std/heap.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ pub var next_mmap_addr_hint: ?[*]align(page_size_min) u8 = null;
4242
///
4343
/// On many systems, the actual page size can only be determined at runtime
4444
/// with `pageSize`.
45-
pub const page_size_min: usize = std.options.page_size_min orelse (page_size_min_default orelse if (builtin.os.tag == .freestanding or builtin.os.tag == .other)
46-
@compileError("freestanding/other page_size_min must provided with std.options.page_size_min")
47-
else
48-
@compileError(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " has unknown page_size_min; populate std.options.page_size_min"));
45+
pub const page_size_min: usize = std.options.page_size_min orelse page_size_min_default orelse
46+
@compileError(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " has unknown page_size_min; populate std.options.page_size_min");
4947

5048
/// comptime-known maximum page size of the target.
5149
///
@@ -831,8 +829,10 @@ const page_size_min_default: ?usize = switch (builtin.os.tag) {
831829
.xtensa => 4 << 10,
832830
else => null,
833831
},
834-
.freestanding => switch (builtin.cpu.arch) {
832+
.freestanding, .other => switch (builtin.cpu.arch) {
835833
.wasm32, .wasm64 => 64 << 10,
834+
.x86, .x86_64 => 4 << 10,
835+
.aarch64, .aarch64_be => 4 << 10,
836836
else => null,
837837
},
838838
else => null,

lib/std/mem.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,12 +1098,12 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co
10981098
// as we don't read into a new page. This should be the case for most architectures
10991099
// which use paged memory, however should be confirmed before adding a new arch below.
11001100
.aarch64, .x86, .x86_64 => if (std.simd.suggestVectorLength(T)) |block_len| {
1101-
const page_size = std.heap.pageSize();
1101+
const page_size = std.heap.page_size_min;
11021102
const block_size = @sizeOf(T) * block_len;
11031103
const Block = @Vector(block_len, T);
11041104
const mask: Block = @splat(sentinel);
11051105

1106-
comptime assert(std.heap.page_size_max % @sizeOf(Block) == 0);
1106+
comptime assert(std.heap.page_size_min % @sizeOf(Block) == 0);
11071107
assert(page_size % @sizeOf(Block) == 0);
11081108

11091109
// First block may be unaligned
@@ -1119,6 +1119,7 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co
11191119

11201120
i += @divExact(std.mem.alignForward(usize, start_addr, block_size) - start_addr, @sizeOf(T));
11211121
} else {
1122+
@branchHint(.unlikely);
11221123
// Would read over a page boundary. Per-byte at a time until aligned or found.
11231124
// 0.39% chance this branch is taken for 4K pages at 16b block length.
11241125
//
@@ -1152,7 +1153,7 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co
11521153
test "indexOfSentinel vector paths" {
11531154
const Types = [_]type{ u8, u16, u32, u64 };
11541155
const allocator = std.testing.allocator;
1155-
const page_size = std.heap.pageSize();
1156+
const page_size = std.heap.page_size_min;
11561157

11571158
inline for (Types) |T| {
11581159
const block_len = std.simd.suggestVectorLength(T) orelse continue;

0 commit comments

Comments
 (0)