LibDebug+LibCoredump: Use ByteReader to do unaligned reads

The previous solution of "lol whats a UB" was not nice and tripped over
itself when it was run under UBSAN, fix this by doing explicit
byte-by-byte reads where needed.
This commit is contained in:
Ali Mohammad Pur 2022-01-27 13:22:45 +03:30 committed by Linus Groh
parent 6d64b13a1b
commit da3c4e5df5
Notes: sideshowbarker 2024-07-17 20:03:54 +09:00
3 changed files with 18 additions and 11 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteReader.h>
#include <AK/HashTable.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
@ -139,8 +140,10 @@ Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
return {};
FlatPtr offset_in_region = address - region->region_start;
const char* region_data = image().program_header(region->program_header_index).raw_data();
return *(const FlatPtr*)(&region_data[offset_in_region]);
auto* region_data = bit_cast<const u8*>(image().program_header(region->program_header_index).raw_data());
FlatPtr value { 0 };
ByteReader::load(region_data + offset_in_region, value);
return value;
}
const JsonObject Reader::process_info() const

View file

@ -6,6 +6,7 @@
#include "CompilationUnit.h"
#include "DIE.h"
#include <AK/ByteReader.h>
namespace Debug::Dwarf {
@ -94,9 +95,11 @@ FlatPtr CompilationUnit::get_address(size_t index) const
auto base = address_table_base();
auto debug_addr_data = dwarf_info().debug_addr_data();
VERIFY(base < debug_addr_data.size());
auto addresses = reinterpret_cast<FlatPtr const*>(debug_addr_data.offset(base));
VERIFY(base + index * sizeof(FlatPtr) < debug_addr_data.size());
return addresses[index];
auto addresses = debug_addr_data.slice(base);
VERIFY(index * sizeof(FlatPtr) < addresses.size());
FlatPtr value { 0 };
ByteReader::load<FlatPtr>(addresses.offset_pointer(index * sizeof(FlatPtr)), value);
return value;
}
char const* CompilationUnit::get_string(size_t index) const
@ -105,9 +108,9 @@ char const* CompilationUnit::get_string(size_t index) const
auto debug_str_offsets_data = dwarf_info().debug_str_offsets_data();
VERIFY(base < debug_str_offsets_data.size());
// FIXME: This assumes DWARF32
auto offsets = reinterpret_cast<u32 const*>(debug_str_offsets_data.offset(base));
VERIFY(base + index * sizeof(u32) < debug_str_offsets_data.size());
auto offset = offsets[index];
return reinterpret_cast<char const*>(dwarf_info().debug_strings_data().offset(offset));
auto offsets = debug_str_offsets_data.slice(base);
VERIFY(index * sizeof(u32) < offsets.size());
auto offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32)));
return bit_cast<char const*>(dwarf_info().debug_strings_data().offset(offset));
}
}

View file

@ -9,6 +9,7 @@
#include "AttributeValue.h"
#include "CompilationUnit.h"
#include <AK/ByteReader.h>
#include <AK/MemoryStream.h>
#include <LibDebug/DebugInfo.h>
@ -344,8 +345,8 @@ void DwarfInfo::build_cached_dies() const
auto index = ranges->as_unsigned();
auto base = die.compilation_unit().range_lists_base();
// FIXME: This assumes that the format is DWARf32
auto offsets = reinterpret_cast<u32 const*>(debug_range_lists_data().offset(base));
offset = offsets[index] + base;
auto offsets = debug_range_lists_data().slice(base);
offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32))) + base;
}
Vector<DIERange> entries;