| ... | @@ -5,6 +5,7 @@ const assert = std.debug.assert; | ... | @@ -5,6 +5,7 @@ const assert = std.debug.assert; |
| 5 | const dwarf = std.dwarf; | 5 | const dwarf = std.dwarf; |
| 6 | const leb = std.leb; | 6 | const leb = std.leb; |
| 7 | const log = std.log.scoped(.macho); | 7 | const log = std.log.scoped(.macho); |
| | 8 | const math = std.math; |
| 8 | const mem = std.mem; | 9 | const mem = std.mem; |
| 9 | | 10 | |
| 10 | const Allocator = mem.Allocator; | 11 | const Allocator = mem.Allocator; |
| ... | @@ -32,13 +33,14 @@ const CompileUnitIterator = struct { | ... | @@ -32,13 +33,14 @@ const CompileUnitIterator = struct { |
| 32 | | 33 | |
| 33 | const cuh = try CompileUnit.Header.read(reader); | 34 | const cuh = try CompileUnit.Header.read(reader); |
| 34 | const total_length = cuh.length + @as(u64, if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32)); | 35 | const total_length = cuh.length + @as(u64, if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32)); |
| | 36 | const offset = math.cast(usize, creader.bytes_read) orelse return error.Overflow; |
| 35 | | 37 | |
| 36 | const cu = CompileUnit{ | 38 | const cu = CompileUnit{ |
| 37 | .cuh = cuh, | 39 | .cuh = cuh, |
| 38 | .debug_info_off = creader.bytes_read, | 40 | .debug_info_off = offset, |
| 39 | }; | 41 | }; |
| 40 | | 42 | |
| 41 | self.pos += total_length; | 43 | self.pos += (math.cast(usize, total_length) orelse return error.Overflow); |
| 42 | | 44 | |
| 43 | return cu; | 45 | return cu; |
| 44 | } | 46 | } |
| ... | @@ -102,7 +104,7 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT | ... | @@ -102,7 +104,7 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT |
| 102 | | 104 | |
| 103 | if (kind == 0) break; | 105 | if (kind == 0) break; |
| 104 | | 106 | |
| 105 | const pos = creader.bytes_read; | 107 | const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow; |
| 106 | _ = try leb.readULEB128(u64, reader); // TAG | 108 | _ = try leb.readULEB128(u64, reader); // TAG |
| 107 | _ = try reader.readByte(); // CHILDREN | 109 | _ = try reader.readByte(); // CHILDREN |
| 108 | | 110 | |
| ... | @@ -113,9 +115,11 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT | ... | @@ -113,9 +115,11 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT |
| 113 | if (name == 0 and form == 0) break; | 115 | if (name == 0 and form == 0) break; |
| 114 | } | 116 | } |
| 115 | | 117 | |
| | 118 | const next_pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow; |
| | 119 | |
| 116 | try lookup.putNoClobber(kind, .{ | 120 | try lookup.putNoClobber(kind, .{ |
| 117 | .pos = pos, | 121 | .pos = pos, |
| 118 | .len = creader.bytes_read - pos - 2, | 122 | .len = next_pos - pos - 2, |
| 119 | }); | 123 | }); |
| 120 | } | 124 | } |
| 121 | } | 125 | } |
| ... | @@ -179,7 +183,7 @@ const AbbrevEntryIterator = struct { | ... | @@ -179,7 +183,7 @@ const AbbrevEntryIterator = struct { |
| 179 | const reader = creader.reader(); | 183 | const reader = creader.reader(); |
| 180 | | 184 | |
| 181 | const kind = try leb.readULEB128(u64, reader); | 185 | const kind = try leb.readULEB128(u64, reader); |
| 182 | self.pos += creader.bytes_read; | 186 | self.pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow); |
| 183 | | 187 | |
| 184 | if (kind == 0) { | 188 | if (kind == 0) { |
| 185 | return AbbrevEntry.@"null"(); | 189 | return AbbrevEntry.@"null"(); |
| ... | @@ -325,7 +329,7 @@ const AttributeIterator = struct { | ... | @@ -325,7 +329,7 @@ const AttributeIterator = struct { |
| 325 | const name = try leb.readULEB128(u64, reader); | 329 | const name = try leb.readULEB128(u64, reader); |
| 326 | const form = try leb.readULEB128(u64, reader); | 330 | const form = try leb.readULEB128(u64, reader); |
| 327 | | 331 | |
| 328 | self.debug_abbrev_pos += creader.bytes_read; | 332 | self.debug_abbrev_pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow); |
| 329 | | 333 | |
| 330 | const len = try findFormSize( | 334 | const len = try findFormSize( |
| 331 | self.ctx, | 335 | self.ctx, |
| ... | @@ -366,11 +370,13 @@ fn getAbbrevEntry(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize, | ... | @@ -366,11 +370,13 @@ fn getAbbrevEntry(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize, |
| 366 | else => try reader.readByte(), | 370 | else => try reader.readByte(), |
| 367 | }; | 371 | }; |
| 368 | | 372 | |
| | 373 | const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow; |
| | 374 | |
| 369 | return AbbrevEntry{ | 375 | return AbbrevEntry{ |
| 370 | .tag = tag, | 376 | .tag = tag, |
| 371 | .children = children, | 377 | .children = children, |
| 372 | .debug_abbrev_off = creader.bytes_read + da_off, | 378 | .debug_abbrev_off = pos + da_off, |
| 373 | .debug_abbrev_len = da_len - creader.bytes_read, | 379 | .debug_abbrev_len = da_len - pos, |
| 374 | .debug_info_off = di_off, | 380 | .debug_info_off = di_off, |
| 375 | .debug_info_len = di_len, | 381 | .debug_info_len = di_len, |
| 376 | }; | 382 | }; |
| ... | @@ -392,7 +398,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head | ... | @@ -392,7 +398,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head |
| 392 | while (i < expr_len) : (i += 1) { | 398 | while (i < expr_len) : (i += 1) { |
| 393 | _ = try reader.readByte(); | 399 | _ = try reader.readByte(); |
| 394 | } | 400 | } |
| 395 | return creader.bytes_read; | 401 | return math.cast(usize, creader.bytes_read) orelse error.Overflow; |
| 396 | }, | 402 | }, |
| 397 | dwarf.FORM.flag_present => return 0, | 403 | dwarf.FORM.flag_present => return 0, |
| 398 | | 404 | |
| ... | @@ -402,11 +408,11 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head | ... | @@ -402,11 +408,11 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head |
| 402 | dwarf.FORM.data8 => return @sizeOf(u64), | 408 | dwarf.FORM.data8 => return @sizeOf(u64), |
| 403 | dwarf.FORM.udata => { | 409 | dwarf.FORM.udata => { |
| 404 | _ = try leb.readULEB128(u64, reader); | 410 | _ = try leb.readULEB128(u64, reader); |
| 405 | return creader.bytes_read; | 411 | return math.cast(usize, creader.bytes_read) orelse error.Overflow; |
| 406 | }, | 412 | }, |
| 407 | dwarf.FORM.sdata => { | 413 | dwarf.FORM.sdata => { |
| 408 | _ = try leb.readILEB128(i64, reader); | 414 | _ = try leb.readILEB128(i64, reader); |
| 409 | return creader.bytes_read; | 415 | return math.cast(usize, creader.bytes_read) orelse error.Overflow; |
| 410 | }, | 416 | }, |
| 411 | | 417 | |
| 412 | dwarf.FORM.ref1 => return @sizeOf(u8), | 418 | dwarf.FORM.ref1 => return @sizeOf(u8), |
| ... | @@ -415,7 +421,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head | ... | @@ -415,7 +421,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head |
| 415 | dwarf.FORM.ref8 => return @sizeOf(u64), | 421 | dwarf.FORM.ref8 => return @sizeOf(u64), |
| 416 | dwarf.FORM.ref_udata => { | 422 | dwarf.FORM.ref_udata => { |
| 417 | _ = try leb.readULEB128(u64, reader); | 423 | _ = try leb.readULEB128(u64, reader); |
| 418 | return creader.bytes_read; | 424 | return math.cast(usize, creader.bytes_read) orelse error.Overflow; |
| 419 | }, | 425 | }, |
| 420 | | 426 | |
| 421 | else => return error.ToDo, | 427 | else => return error.ToDo, |
| ... | @@ -457,5 +463,5 @@ fn findAbbrevEntrySize(self: DwarfInfo, da_off: usize, da_len: usize, di_off: us | ... | @@ -457,5 +463,5 @@ fn findAbbrevEntrySize(self: DwarfInfo, da_off: usize, da_len: usize, di_off: us |
| 457 | | 463 | |
| 458 | fn getString(self: DwarfInfo, off: u64) []const u8 { | 464 | fn getString(self: DwarfInfo, off: u64) []const u8 { |
| 459 | assert(off < self.debug_str.len); | 465 | assert(off < self.debug_str.len); |
| 460 | return mem.sliceTo(@ptrCast([*:0]const u8, self.debug_str.ptr + off), 0); | 466 | return mem.sliceTo(@ptrCast([*:0]const u8, self.debug_str.ptr + @intCast(usize, off)), 0); |
| 461 | } | 467 | } |