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 {...@@ -121,28 +121,26 @@ const Value = extern struct {
121121
122 pub fn format(122 pub fn format(
123 value: Value,123 value: Value,
124 bw: *std.io.BufferedWriter,
124 comptime fmt: []const u8,125 comptime fmt: []const u8,
125 _: std.fmt.FormatOptions,126 ) anyerror!usize {
126 writer: anytype,
127 ) !void {
128 comptime assert(fmt.len == 0);127 comptime assert(fmt.len == 0);
129128
130 // Work around x86_64 backend limitation.129 // Work around x86_64 backend limitation.
131 if (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag == .windows) {130 if (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag == .windows) {
132 try writer.writeAll("(unknown)");131 return bw.writeAllCount("(unknown)");
133 return;
134 }132 }
135133
136 switch (value.td.kind) {134 switch (value.td.kind) {
137 .integer => {135 .integer => {
138 if (value.td.isSigned()) {136 if (value.td.isSigned()) {
139 try writer.print("{}", .{value.getSignedInteger()});137 return bw.printCount("{d}", .{value.getSignedInteger()});
140 } else {138 } else {
141 try writer.print("{}", .{value.getUnsignedInteger()});139 return bw.printCount("{d}", .{value.getUnsignedInteger()});
142 }140 }
143 },141 },
144 .float => try writer.print("{}", .{value.getFloat()}),142 .float => return bw.printCount("{d}", .{value.getFloat()}),
145 .unknown => try writer.writeAll("(unknown)"),143 .unknown => return bw.writeAllCount("(unknown)"),
146 }144 }
147 }145 }
148};146};
...@@ -174,8 +172,8 @@ fn overflowHandler(...@@ -174,8 +172,8 @@ fn overflowHandler(
174 const rhs: Value = .{ .handle = rhs_handle, .td = data.td };172 const rhs: Value = .{ .handle = rhs_handle, .td = data.td };
175173
176 const is_signed = data.td.isSigned();174 const is_signed = data.td.isSigned();
177 const fmt = "{s} integer overflow: " ++ "{} " ++175 const fmt = "{s} integer overflow: " ++ "{f} " ++
178 operator ++ " {} cannot be represented in type {s}";176 operator ++ " {f} cannot be represented in type {s}";
179177
180 panic(@returnAddress(), fmt, .{178 panic(@returnAddress(), fmt, .{
181 if (is_signed) "signed" else "unsigned",179 if (is_signed) "signed" else "unsigned",
...@@ -203,7 +201,7 @@ fn negationHandler(...@@ -203,7 +201,7 @@ fn negationHandler(
203 const value: Value = .{ .handle = value_handle, .td = data.td };201 const value: Value = .{ .handle = value_handle, .td = data.td };
204 panic(202 panic(
205 @returnAddress(),203 @returnAddress(),
206 "negation of {} cannot be represented in type {s}",204 "negation of {f} cannot be represented in type {s}",
207 .{ value, data.td.getName() },205 .{ value, data.td.getName() },
208 );206 );
209}207}
...@@ -227,7 +225,7 @@ fn divRemHandler(...@@ -227,7 +225,7 @@ fn divRemHandler(
227 if (rhs.isMinusOne()) {225 if (rhs.isMinusOne()) {
228 panic(226 panic(
229 @returnAddress(),227 @returnAddress(),
230 "division of {} by -1 cannot be represented in type {s}",228 "division of {f} by -1 cannot be represented in type {s}",
231 .{ lhs, data.td.getName() },229 .{ lhs, data.td.getName() },
232 );230 );
233 } else panic(@returnAddress(), "division by zero", .{});231 } else panic(@returnAddress(), "division by zero", .{});
...@@ -269,8 +267,8 @@ fn alignmentAssumptionHandler(...@@ -269,8 +267,8 @@ fn alignmentAssumptionHandler(
269 if (maybe_offset) |offset| {267 if (maybe_offset) |offset| {
270 panic(268 panic(
271 @returnAddress(),269 @returnAddress(),
272 "assumption of {} byte alignment (with offset of {} byte) for pointer of type {s} failed\n" ++270 "assumption of {f} byte alignment (with offset of {d} byte) for pointer of type {s} failed\n" ++
273 "offset address is {} aligned, misalignment offset is {} bytes",271 "offset address is {d} aligned, misalignment offset is {d} bytes",
274 .{272 .{
275 alignment,273 alignment,
276 @intFromPtr(offset),274 @intFromPtr(offset),
...@@ -282,8 +280,8 @@ fn alignmentAssumptionHandler(...@@ -282,8 +280,8 @@ fn alignmentAssumptionHandler(
282 } else {280 } else {
283 panic(281 panic(
284 @returnAddress(),282 @returnAddress(),
285 "assumption of {} byte alignment for pointer of type {s} failed\n" ++283 "assumption of {f} byte alignment for pointer of type {s} failed\n" ++
286 "address is {} aligned, misalignment offset is {} bytes",284 "address is {d} aligned, misalignment offset is {d} bytes",
287 .{285 .{
288 alignment,286 alignment,
289 data.td.getName(),287 data.td.getName(),
...@@ -320,21 +318,21 @@ fn shiftOob(...@@ -320,21 +318,21 @@ fn shiftOob(
320 rhs.getPositiveInteger() >= data.lhs_type.getIntegerSize())318 rhs.getPositiveInteger() >= data.lhs_type.getIntegerSize())
321 {319 {
322 if (rhs.isNegative()) {320 if (rhs.isNegative()) {
323 panic(@returnAddress(), "shift exponent {} is negative", .{rhs});321 panic(@returnAddress(), "shift exponent {f} is negative", .{rhs});
324 } else {322 } else {
325 panic(323 panic(
326 @returnAddress(),324 @returnAddress(),
327 "shift exponent {} is too large for {}-bit type {s}",325 "shift exponent {f} is too large for {d}-bit type {s}",
328 .{ rhs, data.lhs_type.getIntegerSize(), data.lhs_type.getName() },326 .{ rhs, data.lhs_type.getIntegerSize(), data.lhs_type.getName() },
329 );327 );
330 }328 }
331 } else {329 } else {
332 if (lhs.isNegative()) {330 if (lhs.isNegative()) {
333 panic(@returnAddress(), "left shift of negative value {}", .{lhs});331 panic(@returnAddress(), "left shift of negative value {f}", .{lhs});
334 } else {332 } else {
335 panic(333 panic(
336 @returnAddress(),334 @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}",
338 .{ lhs, rhs, data.lhs_type.getName() },336 .{ lhs, rhs, data.lhs_type.getName() },
339 );337 );
340 }338 }
...@@ -361,7 +359,7 @@ fn outOfBounds(...@@ -361,7 +359,7 @@ fn outOfBounds(
361 const index: Value = .{ .handle = index_handle, .td = data.index_type };359 const index: Value = .{ .handle = index_handle, .td = data.index_type };
362 panic(360 panic(
363 @returnAddress(),361 @returnAddress(),
364 "index {} out of bounds for type {s}",362 "index {f} out of bounds for type {s}",
365 .{ index, data.array_type.getName() },363 .{ index, data.array_type.getName() },
366 );364 );
367}365}
...@@ -387,7 +385,7 @@ fn pointerOverflow(...@@ -387,7 +385,7 @@ fn pointerOverflow(
387 if (result == 0) {385 if (result == 0) {
388 panic(@returnAddress(), "applying zero offset to null pointer", .{});386 panic(@returnAddress(), "applying zero offset to null pointer", .{});
389 } else {387 } else {
390 panic(@returnAddress(), "applying non-zero offset {} to null pointer", .{result});388 panic(@returnAddress(), "applying non-zero offset {d} to null pointer", .{result});
391 }389 }
392 } else {390 } else {
393 if (result == 0) {391 if (result == 0) {
...@@ -483,7 +481,7 @@ fn typeMismatch(...@@ -483,7 +481,7 @@ fn typeMismatch(
483 } else if (!std.mem.isAligned(handle, alignment)) {481 } else if (!std.mem.isAligned(handle, alignment)) {
484 panic(482 panic(
485 @returnAddress(),483 @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",
487 .{ data.kind.getName(), handle, data.td.getName(), alignment },485 .{ data.kind.getName(), handle, data.td.getName(), alignment },
488 );486 );
489 } else {487 } else {
...@@ -531,7 +529,7 @@ fn nonNullArgAbort(data: *const NonNullArgData) callconv(.c) noreturn {...@@ -531,7 +529,7 @@ fn nonNullArgAbort(data: *const NonNullArgData) callconv(.c) noreturn {
531fn nonNullArg(data: *const NonNullArgData) callconv(.c) noreturn {529fn nonNullArg(data: *const NonNullArgData) callconv(.c) noreturn {
532 panic(530 panic(
533 @returnAddress(),531 @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",
535 .{data.arg_index},533 .{data.arg_index},
536 );534 );
537}535}
...@@ -555,7 +553,7 @@ fn loadInvalidValue(...@@ -555,7 +553,7 @@ fn loadInvalidValue(
555 const value: Value = .{ .handle = value_handle, .td = data.td };553 const value: Value = .{ .handle = value_handle, .td = data.td };
556 panic(554 panic(
557 @returnAddress(),555 @returnAddress(),
558 "load of value {}, which is not valid for type {s}",556 "load of value {f}, which is not valid for type {s}",
559 .{ value, data.td.getName() },557 .{ value, data.td.getName() },
560 );558 );
561}559}
...@@ -598,7 +596,7 @@ fn vlaBoundNotPositive(...@@ -598,7 +596,7 @@ fn vlaBoundNotPositive(
598 const bound: Value = .{ .handle = bound_handle, .td = data.td };596 const bound: Value = .{ .handle = bound_handle, .td = data.td };
599 panic(597 panic(
600 @returnAddress(),598 @returnAddress(),
601 "variable length array bound evaluates to non-positive value {}",599 "variable length array bound evaluates to non-positive value {f}",
602 .{bound},600 .{bound},
603 );601 );
604}602}
...@@ -631,13 +629,13 @@ fn floatCastOverflow(...@@ -631,13 +629,13 @@ fn floatCastOverflow(
631 if (@as(u16, ptr[0]) + @as(u16, ptr[1]) < 2 or ptr[0] == 0xFF or ptr[1] == 0xFF) {629 if (@as(u16, ptr[0]) + @as(u16, ptr[1]) < 2 or ptr[0] == 0xFF or ptr[1] == 0xFF) {
632 const data: *const FloatCastOverflowData = @ptrCast(data_handle);630 const data: *const FloatCastOverflowData = @ptrCast(data_handle);
633 const from_value: Value = .{ .handle = from_handle, .td = data.from };631 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}", .{
635 from_value, data.to.getName(),633 from_value, data.to.getName(),
636 });634 });
637 } else {635 } else {
638 const data: *const FloatCastOverflowDataV2 = @ptrCast(data_handle);636 const data: *const FloatCastOverflowDataV2 = @ptrCast(data_handle);
639 const from_value: Value = .{ .handle = from_handle, .td = data.from };637 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}", .{
641 from_value, data.to.getName(),639 from_value, data.to.getName(),
642 });640 });
643 }641 }