authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-04-10 01:04:49-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:26-07:00
log5d22c588e27e264e16c15fb1639335465e6139e2
treed96e204772e0eb191b0cd29c5d526793db3e9b2f
parentd76c8d0bd2305ff5c45653702dd92505e3fb8f6a

ubsan_rt: port to new `std.io.BufferedWriter` API


1 files changed, 27 insertions(+), 29 deletions(-)

lib/ubsan_rt.zig+27-29
......@@ -121,28 +121,26 @@ const Value = extern struct {
121121
122122 pub fn format(
123123 value: Value,
124 bw: *std.io.BufferedWriter,
124125 comptime fmt: []const u8,
125 _: std.fmt.FormatOptions,
126 writer: anytype,
127 ) !void {
126 ) anyerror!usize {
128127 comptime assert(fmt.len == 0);
129128
130129 // Work around x86_64 backend limitation.
131130 if (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag == .windows) {
132 try writer.writeAll("(unknown)");
133 return;
131 return bw.writeAllCount("(unknown)");
134132 }
135133
136134 switch (value.td.kind) {
137135 .integer => {
138136 if (value.td.isSigned()) {
139 try writer.print("{}", .{value.getSignedInteger()});
137 return bw.printCount("{d}", .{value.getSignedInteger()});
140138 } else {
141 try writer.print("{}", .{value.getUnsignedInteger()});
139 return bw.printCount("{d}", .{value.getUnsignedInteger()});
142140 }
143141 },
144 .float => try writer.print("{}", .{value.getFloat()}),
145 .unknown => try writer.writeAll("(unknown)"),
142 .float => return bw.printCount("{d}", .{value.getFloat()}),
143 .unknown => return bw.writeAllCount("(unknown)"),
146144 }
147145 }
148146};
......@@ -174,8 +172,8 @@ fn overflowHandler(
174172 const rhs: Value = .{ .handle = rhs_handle, .td = data.td };
175173
176174 const is_signed = data.td.isSigned();
177 const fmt = "{s} integer overflow: " ++ "{} " ++
178 operator ++ " {} cannot be represented in type {s}";
175 const fmt = "{s} integer overflow: " ++ "{f} " ++
176 operator ++ " {f} cannot be represented in type {s}";
179177
180178 panic(@returnAddress(), fmt, .{
181179 if (is_signed) "signed" else "unsigned",
......@@ -203,7 +201,7 @@ fn negationHandler(
203201 const value: Value = .{ .handle = value_handle, .td = data.td };
204202 panic(
205203 @returnAddress(),
206 "negation of {} cannot be represented in type {s}",
204 "negation of {f} cannot be represented in type {s}",
207205 .{ value, data.td.getName() },
208206 );
209207}
......@@ -227,7 +225,7 @@ fn divRemHandler(
227225 if (rhs.isMinusOne()) {
228226 panic(
229227 @returnAddress(),
230 "division of {} by -1 cannot be represented in type {s}",
228 "division of {f} by -1 cannot be represented in type {s}",
231229 .{ lhs, data.td.getName() },
232230 );
233231 } else panic(@returnAddress(), "division by zero", .{});
......@@ -269,8 +267,8 @@ fn alignmentAssumptionHandler(
269267 if (maybe_offset) |offset| {
270268 panic(
271269 @returnAddress(),
272 "assumption of {} byte alignment (with offset of {} byte) for pointer of type {s} failed\n" ++
273 "offset address is {} aligned, misalignment offset is {} bytes",
270 "assumption of {f} byte alignment (with offset of {d} byte) for pointer of type {s} failed\n" ++
271 "offset address is {d} aligned, misalignment offset is {d} bytes",
274272 .{
275273 alignment,
276274 @intFromPtr(offset),
......@@ -282,8 +280,8 @@ fn alignmentAssumptionHandler(
282280 } else {
283281 panic(
284282 @returnAddress(),
285 "assumption of {} byte alignment for pointer of type {s} failed\n" ++
286 "address is {} aligned, misalignment offset is {} bytes",
283 "assumption of {f} byte alignment for pointer of type {s} failed\n" ++
284 "address is {d} aligned, misalignment offset is {d} bytes",
287285 .{
288286 alignment,
289287 data.td.getName(),
......@@ -320,21 +318,21 @@ fn shiftOob(
320318 rhs.getPositiveInteger() >= data.lhs_type.getIntegerSize())
321319 {
322320 if (rhs.isNegative()) {
323 panic(@returnAddress(), "shift exponent {} is negative", .{rhs});
321 panic(@returnAddress(), "shift exponent {f} is negative", .{rhs});
324322 } else {
325323 panic(
326324 @returnAddress(),
327 "shift exponent {} is too large for {}-bit type {s}",
325 "shift exponent {f} is too large for {d}-bit type {s}",
328326 .{ rhs, data.lhs_type.getIntegerSize(), data.lhs_type.getName() },
329327 );
330328 }
331329 } else {
332330 if (lhs.isNegative()) {
333 panic(@returnAddress(), "left shift of negative value {}", .{lhs});
331 panic(@returnAddress(), "left shift of negative value {f}", .{lhs});
334332 } else {
335333 panic(
336334 @returnAddress(),
337 "left shift of {} by {} places cannot be represented in type {s}",
335 "left shift of {f} by {f} places cannot be represented in type {s}",
338336 .{ lhs, rhs, data.lhs_type.getName() },
339337 );
340338 }
......@@ -361,7 +359,7 @@ fn outOfBounds(
361359 const index: Value = .{ .handle = index_handle, .td = data.index_type };
362360 panic(
363361 @returnAddress(),
364 "index {} out of bounds for type {s}",
362 "index {f} out of bounds for type {s}",
365363 .{ index, data.array_type.getName() },
366364 );
367365}
......@@ -387,7 +385,7 @@ fn pointerOverflow(
387385 if (result == 0) {
388386 panic(@returnAddress(), "applying zero offset to null pointer", .{});
389387 } else {
390 panic(@returnAddress(), "applying non-zero offset {} to null pointer", .{result});
388 panic(@returnAddress(), "applying non-zero offset {d} to null pointer", .{result});
391389 }
392390 } else {
393391 if (result == 0) {
......@@ -483,7 +481,7 @@ fn typeMismatch(
483481 } else if (!std.mem.isAligned(handle, alignment)) {
484482 panic(
485483 @returnAddress(),
486 "{s} misaligned address 0x{x} for type {s}, which requires {} byte alignment",
484 "{s} misaligned address 0x{x} for type {s}, which requires {d} byte alignment",
487485 .{ data.kind.getName(), handle, data.td.getName(), alignment },
488486 );
489487 } else {
......@@ -531,7 +529,7 @@ fn nonNullArgAbort(data: *const NonNullArgData) callconv(.c) noreturn {
531529fn nonNullArg(data: *const NonNullArgData) callconv(.c) noreturn {
532530 panic(
533531 @returnAddress(),
534 "null pointer passed as argument {}, which is declared to never be null",
532 "null pointer passed as argument {d}, which is declared to never be null",
535533 .{data.arg_index},
536534 );
537535}
......@@ -555,7 +553,7 @@ fn loadInvalidValue(
555553 const value: Value = .{ .handle = value_handle, .td = data.td };
556554 panic(
557555 @returnAddress(),
558 "load of value {}, which is not valid for type {s}",
556 "load of value {f}, which is not valid for type {s}",
559557 .{ value, data.td.getName() },
560558 );
561559}
......@@ -598,7 +596,7 @@ fn vlaBoundNotPositive(
598596 const bound: Value = .{ .handle = bound_handle, .td = data.td };
599597 panic(
600598 @returnAddress(),
601 "variable length array bound evaluates to non-positive value {}",
599 "variable length array bound evaluates to non-positive value {f}",
602600 .{bound},
603601 );
604602}
......@@ -631,13 +629,13 @@ fn floatCastOverflow(
631629 if (@as(u16, ptr[0]) + @as(u16, ptr[1]) < 2 or ptr[0] == 0xFF or ptr[1] == 0xFF) {
632630 const data: *const FloatCastOverflowData = @ptrCast(data_handle);
633631 const from_value: Value = .{ .handle = from_handle, .td = data.from };
634 panic(@returnAddress(), "{} is outside the range of representable values of type {s}", .{
632 panic(@returnAddress(), "{f} is outside the range of representable values of type {s}", .{
635633 from_value, data.to.getName(),
636634 });
637635 } else {
638636 const data: *const FloatCastOverflowDataV2 = @ptrCast(data_handle);
639637 const from_value: Value = .{ .handle = from_handle, .td = data.from };
640 panic(@returnAddress(), "{} is outside the range of representable values of type {s}", .{
638 panic(@returnAddress(), "{f} is outside the range of representable values of type {s}", .{
641639 from_value, data.to.getName(),
642640 });
643641 }