1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 19:37:34 +00:00

LibDebug: Store 64-bit numbers in AttributeValue

This helps us avoid weird truncation issues and fixes a bug on Clang
builds where truncation while reading caused the DIE offsets following
large LEB128 numbers to be incorrect. This removes the need for the
separate `LongUnsignedNumber` type.
This commit is contained in:
Daniel Bertalan 2021-07-27 11:03:24 +02:00 committed by Andreas Kling
parent efd1aea969
commit 7396e4aedc
6 changed files with 32 additions and 31 deletions

View file

@ -13,9 +13,12 @@
TEST_CASE(basic_scenario) TEST_CASE(basic_scenario)
{ {
auto location = SourceLocation::current(); auto location = SourceLocation::current();
EXPECT_EQ(StringView(__FILE__), location.filename());
EXPECT_EQ(StringView(__FUNCTION__), location.function_name()); EXPECT_EQ(StringView(__FUNCTION__), location.function_name());
EXPECT_EQ(__LINE__ - 3u, location.line_number()); EXPECT_EQ(__LINE__ - 2u, location.line_number());
// FIXME: On Clang, __FILE__ is a relative path, while location.path() is absolute
#ifndef __clang__
EXPECT_EQ(StringView(__FILE__), location.filename());
#endif
} }
static StringView test_default_arg(const SourceLocation& loc = SourceLocation::current()) static StringView test_default_arg(const SourceLocation& loc = SourceLocation::current())

View file

@ -193,7 +193,7 @@ static Optional<Dwarf::DIE> parse_variable_type_die(Dwarf::DIE const& variable_d
VERIFY(type_die_offset.value().type == Dwarf::AttributeValue::Type::DieReference); VERIFY(type_die_offset.value().type == Dwarf::AttributeValue::Type::DieReference);
auto type_die = variable_die.compilation_unit().get_die_at_offset(type_die_offset.value().data.as_u32); auto type_die = variable_die.compilation_unit().get_die_at_offset(type_die_offset.value().data.as_unsigned);
auto type_name = type_die.get_attribute(Dwarf::Attribute::Name); auto type_name = type_die.get_attribute(Dwarf::Attribute::Name);
if (type_name.has_value()) { if (type_name.has_value()) {
variable_info.type_name = type_name.value().data.as_string; variable_info.type_name = type_name.value().data.as_string;
@ -258,10 +258,10 @@ OwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(Dwarf::DIE const
VERIFY(constant.has_value()); VERIFY(constant.has_value());
switch (constant.value().type) { switch (constant.value().type) {
case Dwarf::AttributeValue::Type::UnsignedNumber: case Dwarf::AttributeValue::Type::UnsignedNumber:
variable_info->constant_data.as_u32 = constant.value().data.as_u32; variable_info->constant_data.as_u32 = constant.value().data.as_unsigned;
break; break;
case Dwarf::AttributeValue::Type::SignedNumber: case Dwarf::AttributeValue::Type::SignedNumber:
variable_info->constant_data.as_i32 = constant.value().data.as_i32; variable_info->constant_data.as_i32 = constant.value().data.as_signed;
break; break;
case Dwarf::AttributeValue::Type::String: case Dwarf::AttributeValue::Type::String:
variable_info->constant_data.as_string = constant.value().data.as_string; variable_info->constant_data.as_string = constant.value().data.as_string;
@ -298,7 +298,7 @@ void DebugInfo::add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegi
if (is_array_type && member.tag() == Dwarf::EntryTag::SubRangeType) { if (is_array_type && member.tag() == Dwarf::EntryTag::SubRangeType) {
auto upper_bound = member.get_attribute(Dwarf::Attribute::UpperBound); auto upper_bound = member.get_attribute(Dwarf::Attribute::UpperBound);
VERIFY(upper_bound.has_value()); VERIFY(upper_bound.has_value());
auto size = upper_bound.value().data.as_u32 + 1; auto size = upper_bound.value().data.as_unsigned + 1;
type_info->dimension_sizes.append(size); type_info->dimension_sizes.append(size);
return; return;
} }
@ -433,11 +433,11 @@ Optional<Dwarf::LineProgram::DirectoryAndFile> DebugInfo::get_source_path_of_inl
u32 file_index = 0; u32 file_index = 0;
if (caller_file->type == Dwarf::AttributeValue::Type::UnsignedNumber) { if (caller_file->type == Dwarf::AttributeValue::Type::UnsignedNumber) {
file_index = caller_file->data.as_u32; file_index = caller_file->data.as_unsigned;
} else if (caller_file->type == Dwarf::AttributeValue::Type::SignedNumber) { } else if (caller_file->type == Dwarf::AttributeValue::Type::SignedNumber) {
// For some reason, the file_index is sometimes stored as a signed number. // For some reason, the file_index is sometimes stored as a signed number.
VERIFY(caller_file->data.as_i32 >= 0); VERIFY(caller_file->data.as_signed >= 0);
file_index = (u32)caller_file->data.as_i32; file_index = (u32)caller_file->data.as_signed;
} else { } else {
return {}; return {};
} }
@ -455,7 +455,7 @@ Optional<uint32_t> DebugInfo::get_line_of_inline(Dwarf::DIE const& die) const
if (caller_line->type != Dwarf::AttributeValue::Type::UnsignedNumber) if (caller_line->type != Dwarf::AttributeValue::Type::UnsignedNumber)
return {}; return {};
return caller_line.value().data.as_u32; return caller_line.value().data.as_unsigned;
} }
} }

View file

@ -15,7 +15,6 @@ struct AttributeValue {
enum class Type : u8 { enum class Type : u8 {
UnsignedNumber, UnsignedNumber,
SignedNumber, SignedNumber,
LongUnsignedNumber,
String, String,
DieReference, // Reference to another DIE in the same compilation unit DieReference, // Reference to another DIE in the same compilation unit
Boolean, Boolean,
@ -26,9 +25,8 @@ struct AttributeValue {
union { union {
FlatPtr as_addr; FlatPtr as_addr;
u32 as_u32; u64 as_unsigned;
i32 as_i32; i64 as_signed;
u64 as_u64;
const char* as_string; // points to bytes in the memory mapped elf image const char* as_string; // points to bytes in the memory mapped elf image
bool as_bool; bool as_bool;
struct { struct {

View file

@ -75,7 +75,7 @@ void DIE::for_each_child(Function<void(DIE const& child)> callback) const
auto sibling = current_child->get_attribute(Attribute::Sibling); auto sibling = current_child->get_attribute(Attribute::Sibling);
u32 sibling_offset = 0; u32 sibling_offset = 0;
if (sibling.has_value()) { if (sibling.has_value()) {
sibling_offset = sibling.value().data.as_u32; sibling_offset = sibling.value().data.as_unsigned;
} }
if (!sibling.has_value()) { if (!sibling.has_value()) {

View file

@ -90,7 +90,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
debug_info_stream >> data; debug_info_stream >> data;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::UnsignedNumber; value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data; value.data.as_unsigned = data;
break; break;
} }
case AttributeDataForm::Data2: { case AttributeDataForm::Data2: {
@ -98,7 +98,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
debug_info_stream >> data; debug_info_stream >> data;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::UnsignedNumber; value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data; value.data.as_signed = data;
break; break;
} }
case AttributeDataForm::Addr: { case AttributeDataForm::Addr: {
@ -110,19 +110,19 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
break; break;
} }
case AttributeDataForm::SData: { case AttributeDataForm::SData: {
ssize_t data; i64 data;
debug_info_stream.read_LEB128_signed(data); debug_info_stream.read_LEB128_signed(data);
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::SignedNumber; value.type = AttributeValue::Type::SignedNumber;
value.data.as_i32 = data; value.data.as_signed = data;
break; break;
} }
case AttributeDataForm::UData: { case AttributeDataForm::UData: {
size_t data; u64 data;
debug_info_stream.read_LEB128_unsigned(data); debug_info_stream.read_LEB128_unsigned(data);
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::UnsignedNumber; value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data; value.data.as_unsigned = data;
break; break;
} }
case AttributeDataForm::SecOffset: { case AttributeDataForm::SecOffset: {
@ -130,7 +130,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
debug_info_stream >> data; debug_info_stream >> data;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::SecOffset; value.type = AttributeValue::Type::SecOffset;
value.data.as_u32 = data; value.data.as_unsigned = data;
break; break;
} }
case AttributeDataForm::Data4: { case AttributeDataForm::Data4: {
@ -138,15 +138,15 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
debug_info_stream >> data; debug_info_stream >> data;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::UnsignedNumber; value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data; value.data.as_unsigned = data;
break; break;
} }
case AttributeDataForm::Data8: { case AttributeDataForm::Data8: {
u64 data; u64 data;
debug_info_stream >> data; debug_info_stream >> data;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::LongUnsignedNumber; value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u64 = data; value.data.as_unsigned = data;
break; break;
} }
case AttributeDataForm::Ref4: { case AttributeDataForm::Ref4: {
@ -155,7 +155,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.type = AttributeValue::Type::DieReference; value.type = AttributeValue::Type::DieReference;
VERIFY(unit); VERIFY(unit);
value.data.as_u32 = data + unit->offset(); value.data.as_unsigned = data + unit->offset();
break; break;
} }
case AttributeDataForm::FlagPresent: { case AttributeDataForm::FlagPresent: {
@ -225,7 +225,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
case AttributeDataForm::ImplicitConst: { case AttributeDataForm::ImplicitConst: {
/* Value is part of the abbreviation record. */ /* Value is part of the abbreviation record. */
value.type = AttributeValue::Type::SignedNumber; value.type = AttributeValue::Type::SignedNumber;
value.data.as_i32 = implicit_const_value; value.data.as_signed = implicit_const_value;
break; break;
} }
default: default:
@ -250,7 +250,7 @@ void DwarfInfo::build_cached_dies() const
if (!start.has_value() || !end.has_value()) if (!start.has_value() || !end.has_value())
return {}; return {};
VERIFY(start->type == Dwarf::AttributeValue::Type::UnsignedNumber); VERIFY(start->form == Dwarf::AttributeDataForm::Addr);
// DW_AT_high_pc attribute can have different meanings depending on the attribute form. // DW_AT_high_pc attribute can have different meanings depending on the attribute form.
// (Dwarf version 5, section 2.17.2). // (Dwarf version 5, section 2.17.2).
@ -259,9 +259,9 @@ void DwarfInfo::build_cached_dies() const
if (end->form == Dwarf::AttributeDataForm::Addr) if (end->form == Dwarf::AttributeDataForm::Addr)
range_end = end->data.as_addr; range_end = end->data.as_addr;
else else
range_end = start->data.as_u32 + end->data.as_u32; range_end = start->data.as_unsigned + end->data.as_unsigned;
return { DIERange { start.value().data.as_u32, range_end } }; return { DIERange { (FlatPtr)start.value().data.as_addr, range_end } };
}; };
// If we simply use a lambda, type deduction fails because it's used recursively. // If we simply use a lambda, type deduction fails because it's used recursively.

View file

@ -63,7 +63,7 @@ void LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback,
entry.path = value.data.as_string; entry.path = value.data.as_string;
break; break;
case ContentType::DirectoryIndex: case ContentType::DirectoryIndex:
entry.directory_index = value.data.as_u32; entry.directory_index = value.data.as_unsigned;
break; break;
default: default:
dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", (int)format_description.type); dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", (int)format_description.type);