1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:27:43 +00:00

AK: Remove the fallible constructor from FixedMemoryStream

This commit is contained in:
Tim Schumacher 2023-01-30 11:05:43 +01:00 committed by Linus Groh
parent 8b2f23d016
commit 220fbcaa7e
31 changed files with 185 additions and 209 deletions

View file

@ -21,19 +21,19 @@ AbbreviationsMap::AbbreviationsMap(DwarfInfo const& dwarf_info, u32 offset)
ErrorOr<void> AbbreviationsMap::populate_map()
{
auto abbreviation_stream = TRY(FixedMemoryStream::construct(m_dwarf_info.abbreviation_data()));
TRY(abbreviation_stream->discard(m_offset));
FixedMemoryStream abbreviation_stream { m_dwarf_info.abbreviation_data() };
TRY(abbreviation_stream.discard(m_offset));
while (!abbreviation_stream->is_eof()) {
size_t abbreviation_code = TRY(abbreviation_stream->read_value<LEB128<size_t>>());
while (!abbreviation_stream.is_eof()) {
size_t abbreviation_code = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
// An abbreviation code of 0 marks the end of the
// abbreviations for a given compilation unit
if (abbreviation_code == 0)
break;
size_t tag = TRY(abbreviation_stream->read_value<LEB128<size_t>>());
size_t tag = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
auto has_children = TRY(abbreviation_stream->read_value<u8>());
auto has_children = TRY(abbreviation_stream.read_value<u8>());
AbbreviationEntry abbreviation_entry {};
abbreviation_entry.tag = static_cast<EntryTag>(tag);
@ -41,14 +41,14 @@ ErrorOr<void> AbbreviationsMap::populate_map()
AttributeSpecification current_attribute_specification {};
do {
size_t attribute_value = TRY(abbreviation_stream->read_value<LEB128<size_t>>());
size_t form_value = TRY(abbreviation_stream->read_value<LEB128<size_t>>());
size_t attribute_value = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
size_t form_value = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
current_attribute_specification.attribute = static_cast<Attribute>(attribute_value);
current_attribute_specification.form = static_cast<AttributeDataForm>(form_value);
if (current_attribute_specification.form == AttributeDataForm::ImplicitConst) {
ssize_t data_value = TRY(abbreviation_stream->read_value<LEB128<ssize_t>>());
ssize_t data_value = TRY(abbreviation_stream.read_value<LEB128<ssize_t>>());
current_attribute_specification.value = data_value;
}

View file

@ -24,6 +24,7 @@ class AddressRangesV5 {
AK_MAKE_NONMOVABLE(AddressRangesV5);
public:
// FIXME: This should be fine with using a non-owned stream.
AddressRangesV5(NonnullOwnPtr<AK::Stream> range_lists_stream, CompilationUnit const& compilation_unit);
ErrorOr<void> for_each_range(Function<void(Range)>);

View file

@ -23,11 +23,11 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
{
m_offset = offset;
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
FixedMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_offset));
m_abbreviation_code = TRY(stream->read_value<LEB128<size_t>>());
m_data_offset = TRY(stream->tell());
TRY(stream.seek(m_offset));
m_abbreviation_code = TRY(stream.read_value<LEB128<size_t>>());
m_data_offset = TRY(stream.tell());
if (m_abbreviation_code == 0) {
// An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
@ -41,25 +41,25 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
// We iterate the attributes data only to calculate this DIE's size
for (auto& attribute_spec : abbreviation_info->attribute_specifications) {
TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, *stream, &m_compilation_unit));
TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
}
}
m_size = TRY(stream->tell()) - m_offset;
m_size = TRY(stream.tell()) - m_offset;
m_parent_offset = parent_offset;
return {};
}
ErrorOr<Optional<AttributeValue>> DIE::get_attribute(Attribute const& attribute) const
{
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
FixedMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_data_offset));
TRY(stream.seek(m_data_offset));
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
VERIFY(abbreviation_info);
for (auto const& attribute_spec : abbreviation_info->attribute_specifications) {
auto value = TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, *stream, &m_compilation_unit));
auto value = TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
if (attribute_spec.attribute == attribute) {
return value;
}

View file

@ -47,35 +47,35 @@ ErrorOr<void> DwarfInfo::populate_compilation_units()
if (!m_debug_info_data.data())
return {};
auto debug_info_stream = TRY(FixedMemoryStream::construct(m_debug_info_data));
auto line_info_stream = TRY(FixedMemoryStream::construct(m_debug_line_data));
FixedMemoryStream debug_info_stream { m_debug_info_data };
FixedMemoryStream line_info_stream { m_debug_line_data };
while (!debug_info_stream->is_eof()) {
auto unit_offset = TRY(debug_info_stream->tell());
while (!debug_info_stream.is_eof()) {
auto unit_offset = TRY(debug_info_stream.tell());
auto compilation_unit_header = TRY(debug_info_stream->read_value<CompilationUnitHeader>());
auto compilation_unit_header = TRY(debug_info_stream.read_value<CompilationUnitHeader>());
VERIFY(compilation_unit_header.common.version <= 5);
VERIFY(compilation_unit_header.address_size() == sizeof(FlatPtr));
u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
auto line_program = make<LineProgram>(*this, *line_info_stream);
auto line_program = make<LineProgram>(*this, line_info_stream);
// HACK: Clang generates line programs for embedded resource assembly files, but not compile units.
// Meaning that for graphical applications, some line info data would be unread, triggering the assertion below.
// As a fix, we don't create compilation units for line programs that come from resource files.
#if defined(AK_COMPILER_CLANG)
if (line_program->looks_like_embedded_resource()) {
TRY(debug_info_stream->seek(unit_offset));
TRY(debug_info_stream.seek(unit_offset));
} else
#endif
{
m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program)));
TRY(debug_info_stream->discard(length_after_header));
TRY(debug_info_stream.discard(length_after_header));
}
}
VERIFY(line_info_stream->is_eof());
VERIFY(line_info_stream.is_eof());
return {};
}
@ -300,14 +300,14 @@ ErrorOr<void> DwarfInfo::build_cached_dies() const
Vector<DIERange> entries;
if (die.compilation_unit().dwarf_version() == 5) {
auto range_lists_stream = TRY(FixedMemoryStream::construct(debug_range_lists_data()));
auto range_lists_stream = TRY(try_make<FixedMemoryStream>(debug_range_lists_data()));
TRY(range_lists_stream->seek(offset));
AddressRangesV5 address_ranges(move(range_lists_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) {
entries.empend(range.start, range.end);
}));
} else {
auto ranges_stream = TRY(FixedMemoryStream::construct(debug_ranges_data()));
auto ranges_stream = TRY(try_make<FixedMemoryStream>(debug_ranges_data()));
TRY(ranges_stream->seek(offset));
AddressRangesV4 address_ranges(move(ranges_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) {

View file

@ -14,10 +14,10 @@ namespace Debug::Dwarf::Expression {
ErrorOr<Value> evaluate(ReadonlyBytes bytes, [[maybe_unused]] PtraceRegisters const& regs)
{
auto stream = TRY(FixedMemoryStream::construct(bytes));
FixedMemoryStream stream { bytes };
while (!stream->is_eof()) {
auto opcode = TRY(stream->read_value<u8>());
while (!stream.is_eof()) {
auto opcode = TRY(stream.read_value<u8>());
switch (static_cast<Operations>(opcode)) {