authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-22 08:47:04+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-22 08:50:01+01:00
log406c85f9ba056e10899feed18dae91e20942dc55
treefbc6b204723691dfad10851137c7df67858d679b
parent062ddb693f3b060a59bc3881cbc6cea2cc8e2855

macho+elf: fix integer overflow in allocateAtom

If there is a big atom available for re-use in the free list, and it's the last atom in section, it's ideal capacity might span the entire section in which case we do not want to calculate the actual end VM addr of the symbol since it may overflow. Instead, we just take the max capacity available as end VM addr estimate. In this case, the max capacity equals `std.math.maxInt(u64)`.

2 files changed, 2 insertions(+), 2 deletions(-)

src/link/Elf.zig+1-1
...@@ -2118,7 +2118,7 @@ fn allocateTextBlock(self: *Elf, block_list: *TextBlockList, text_block: *TextBl...@@ -2118,7 +2118,7 @@ fn allocateTextBlock(self: *Elf, block_list: *TextBlockList, text_block: *TextBl
2118 const sym = self.local_symbols.items[big_block.local_sym_index];2118 const sym = self.local_symbols.items[big_block.local_sym_index];
2119 const capacity = big_block.capacity(self.*);2119 const capacity = big_block.capacity(self.*);
2120 const ideal_capacity = padToIdeal(capacity);2120 const ideal_capacity = padToIdeal(capacity);
2121 const ideal_capacity_end_vaddr = sym.st_value + ideal_capacity;2121 const ideal_capacity_end_vaddr = std.math.add(u64, sym.st_value, ideal_capacity) catch ideal_capacity;
2122 const capacity_end_vaddr = sym.st_value + capacity;2122 const capacity_end_vaddr = sym.st_value + capacity;
2123 const new_start_vaddr_unaligned = capacity_end_vaddr - new_block_ideal_capacity;2123 const new_start_vaddr_unaligned = capacity_end_vaddr - new_block_ideal_capacity;
2124 const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment);2124 const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment);
src/link/MachO.zig+1-1
...@@ -5064,7 +5064,7 @@ fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, m...@@ -5064,7 +5064,7 @@ fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, m
5064 const sym = self.locals.items[big_atom.local_sym_index];5064 const sym = self.locals.items[big_atom.local_sym_index];
5065 const capacity = big_atom.capacity(self.*);5065 const capacity = big_atom.capacity(self.*);
5066 const ideal_capacity = if (needs_padding) padToIdeal(capacity) else capacity;5066 const ideal_capacity = if (needs_padding) padToIdeal(capacity) else capacity;
5067 const ideal_capacity_end_vaddr = sym.n_value + ideal_capacity;5067 const ideal_capacity_end_vaddr = math.add(u64, sym.n_value, ideal_capacity) catch ideal_capacity;
5068 const capacity_end_vaddr = sym.n_value + capacity;5068 const capacity_end_vaddr = sym.n_value + capacity;
5069 const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;5069 const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;
5070 const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment);5070 const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment);