authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-12-26 03:59:27-08:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-02-25 11:22:33-08:00
loga468929519157cf7a2070a0368357cb73ad80b61
tree0c50a008477c98677b397cb0634a7f933b4dbc71
parent50b95562fde68147c66eb8e8624628094ae7e029

ubsan: resolve the last of the TODOs


1 files changed, 42 insertions(+), 14 deletions(-)

lib/ubsan.zig+42-14
......@@ -213,25 +213,34 @@ fn alignmentAssumptionHandler(
213213 alignment: ValueHandle,
214214 maybe_offset: ?ValueHandle,
215215) callconv(.c) noreturn {
216 _ = pointer;
217 // TODO: add the hint here?
218 // const real_pointer = @intFromPtr(pointer) - @intFromPtr(maybe_offset);
219 // const lsb = @ctz(real_pointer);
220 // const actual_alignment = @as(u64, 1) << @intCast(lsb);
221 // const mask = @intFromPtr(alignment) - 1;
222 // const misalignment_offset = real_pointer & mask;
223 // _ = actual_alignment;
224 // _ = misalignment_offset;
216 const real_pointer = @intFromPtr(pointer) - @intFromPtr(maybe_offset);
217 const lsb = @ctz(real_pointer);
218 const actual_alignment = @as(u64, 1) << @intCast(lsb);
219 const mask = @intFromPtr(alignment) - 1;
220 const misalignment_offset = real_pointer & mask;
225221
226222 if (maybe_offset) |offset| {
227223 logMessage(
228 "assumption of {} byte alignment (with offset of {} byte) for pointer of type {s} failed",
229 .{ alignment.getValue(data), @intFromPtr(offset), data.type_descriptor.getName() },
224 "assumption of {} byte alignment (with offset of {} byte) for pointer of type {s} failed\n" ++
225 "offset address is {} aligned, misalignment offset is {} bytes",
226 .{
227 alignment.getValue(data),
228 @intFromPtr(offset),
229 data.type_descriptor.getName(),
230 actual_alignment,
231 misalignment_offset,
232 },
230233 );
231234 } else {
232235 logMessage(
233 "assumption of {} byte alignment for pointer of type {s} failed",
234 .{ alignment.getValue(data), data.type_descriptor.getName() },
236 "assumption of {} byte alignment for pointer of type {s} failed\n" ++
237 "address is {} aligned, misalignment offset is {} bytes",
238 .{
239 alignment.getValue(data),
240 data.type_descriptor.getName(),
241 actual_alignment,
242 misalignment_offset,
243 },
235244 );
236245 }
237246}
......@@ -309,7 +318,26 @@ fn pointerOverflow(
309318 .{base},
310319 );
311320 } else {
312 @panic("TODO");
321 const signed_base: isize = @bitCast(base);
322 const signed_result: isize = @bitCast(result);
323 if ((signed_base >= 0) == (signed_result >= 0)) {
324 if (base > result) {
325 logMessage(
326 "addition of unsigned offset to 0x{x} overflowed to 0x{x}",
327 .{ base, result },
328 );
329 } else {
330 logMessage(
331 "subtraction of unsigned offset to 0x{x} overflowed to 0x{x}",
332 .{ base, result },
333 );
334 }
335 } else {
336 logMessage(
337 "pointer index expression with base 0x{x} overflowed to 0x{x}",
338 .{ base, result },
339 );
340 }
313341 }
314342 }
315343}