make StubbedPointer an explicit C++ type

This commit is contained in:
Jake Hillion 2023-09-21 08:57:08 -07:00 committed by Jake Hillion
parent 9359757fb0
commit 5632738d97
9 changed files with 60 additions and 24 deletions

View File

@ -143,3 +143,5 @@ struct validate_offset {
static constexpr bool value = true; static constexpr bool value = true;
static_assert(ExpectedOffset == ActualOffset); static_assert(ExpectedOffset == ActualOffset);
}; };
enum class StubbedPointer : uintptr_t {};

View File

@ -464,13 +464,13 @@ static drgn_type* getPtrUnderlyingType(drgn_type* type) {
Type& DrgnParser::enumeratePointer(struct drgn_type* type) { Type& DrgnParser::enumeratePointer(struct drgn_type* type) {
if (!chasePointer()) { if (!chasePointer()) {
return makeType<Primitive>(type, Primitive::Kind::UIntPtr); return makeType<Primitive>(type, Primitive::Kind::StubbedPointer);
} }
struct drgn_type* pointeeType = drgn_type_type(type).type; struct drgn_type* pointeeType = drgn_type_type(type).type;
if (drgn_type_kind(getPtrUnderlyingType(type)) == DRGN_TYPE_FUNCTION) { if (drgn_type_kind(getPtrUnderlyingType(type)) == DRGN_TYPE_FUNCTION) {
return makeType<Primitive>(type, Primitive::Kind::UIntPtr); return makeType<Primitive>(type, Primitive::Kind::StubbedPointer);
} }
Type& t = enumerateType(pointeeType); Type& t = enumerateType(pointeeType);

View File

@ -59,9 +59,9 @@ Primitive& TypeGraph::makeType<Primitive>(Primitive::Kind kind) {
case Primitive::Kind::Bool: case Primitive::Kind::Bool:
static Primitive pBool{kind}; static Primitive pBool{kind};
return pBool; return pBool;
case Primitive::Kind::UIntPtr: case Primitive::Kind::StubbedPointer:
static Primitive pUIntPtr{kind}; static Primitive pStubbedPointer{kind};
return pUIntPtr; return pStubbedPointer;
case Primitive::Kind::Void: case Primitive::Kind::Void:
static Primitive pVoid{kind}; static Primitive pVoid{kind};
return pVoid; return pVoid;

View File

@ -57,14 +57,21 @@ std::string Primitive::getName(Kind kind) {
return "long double"; return "long double";
case Kind::Bool: case Kind::Bool:
return "bool"; return "bool";
case Kind::UIntPtr: case Kind::StubbedPointer:
return "uintptr_t"; return "StubbedPointer";
case Kind::Void: case Kind::Void:
case Kind::Incomplete: case Kind::Incomplete:
return "void"; return "void";
} }
} }
std::string_view Primitive::inputName() const {
static const std::string kStubbedPointer = "uintptr_t (stubbed)";
if (kind_ == Kind::StubbedPointer)
return kStubbedPointer;
return name_;
}
std::size_t Primitive::size() const { std::size_t Primitive::size() const {
switch (kind_) { switch (kind_) {
case Kind::Int8: case Kind::Int8:
@ -93,7 +100,7 @@ std::size_t Primitive::size() const {
return 16; return 16;
case Kind::Bool: case Kind::Bool:
return 1; return 1;
case Kind::UIntPtr: case Kind::StubbedPointer:
return sizeof(uintptr_t); return sizeof(uintptr_t);
case Kind::Void: case Kind::Void:
case Kind::Incomplete: case Kind::Incomplete:

View File

@ -485,8 +485,7 @@ class Primitive : public Type {
Float128, // TODO can we generate this? Float128, // TODO can we generate this?
Bool, Bool,
UIntPtr, // Really an alias, but useful to have as its own primitive StubbedPointer,
Void, Void,
Incomplete, // Behaves the same as Void, but alerts us that the type was Incomplete, // Behaves the same as Void, but alerts us that the type was
// stubbed out due to incomplete DWARF // stubbed out due to incomplete DWARF
@ -502,9 +501,7 @@ class Primitive : public Type {
virtual const std::string& name() const override { virtual const std::string& name() const override {
return name_; return name_;
} }
virtual std::string_view inputName() const override { virtual std::string_view inputName() const override;
return name_;
}
virtual size_t size() const override; virtual size_t size() const override;
virtual uint64_t align() const override { virtual uint64_t align() const override {
return size(); return size();

View File

@ -45,8 +45,8 @@ Primitive::Kind getKind(std::string_view kindStr) {
return Primitive::Kind::Float128; return Primitive::Kind::Float128;
if (kindStr == "bool") if (kindStr == "bool")
return Primitive::Kind::Bool; return Primitive::Kind::Bool;
if (kindStr == "uintptr_t") if (kindStr == "StubbedPointer")
return Primitive::Kind::UIntPtr; return Primitive::Kind::StubbedPointer;
if (kindStr == "void") if (kindStr == "void")
return Primitive::Kind::Void; return Primitive::Kind::Void;
if (kindStr == "void (incomplete)") if (kindStr == "void (incomplete)")

View File

@ -1,7 +1,6 @@
includes = ["vector", "utility", "cstdint"] includes = ["vector", "utility", "cstdint"]
[cases] [cases]
[cases.uint64_uint64] [cases.uint64_uint64]
oil_skip = 'tests need updating for treebuilder v2' # https://github.com/facebookexperimental/object-introspection/issues/310
param_types = ["std::pair<std::uint64_t, std::uint64_t>&"] param_types = ["std::pair<std::uint64_t, std::uint64_t>&"]
setup = "return {{0, 1}};" setup = "return {{0, 1}};"
expect_json = ''' expect_json = '''
@ -14,8 +13,13 @@ includes = ["vector", "utility", "cstdint"]
} }
] ]
''' '''
expect_json_v2 = '''[
{"staticSize": 16, "exclusiveSize": 0, "members": [
{"typeNames": ["uint64_t"], "staticSize": 8, "exclusiveSize": 8},
{"typeNames": ["uint64_t"], "staticSize": 8, "exclusiveSize": 8}
]}
]'''
[cases.uint64_uint32] [cases.uint64_uint32]
oil_skip = 'tests need updating for treebuilder v2' # https://github.com/facebookexperimental/object-introspection/issues/310
param_types = ["std::pair<std::uint64_t, std::uint32_t>&"] param_types = ["std::pair<std::uint64_t, std::uint32_t>&"]
setup = "return {{0, 1}};" setup = "return {{0, 1}};"
# Should still have static size of 16 due to padding # Should still have static size of 16 due to padding
@ -29,8 +33,28 @@ includes = ["vector", "utility", "cstdint"]
} }
] ]
''' '''
expect_json_v2 = '''[
{"staticSize": 16, "exclusiveSize": 4, "members": [
{"typeNames": ["uint64_t"], "staticSize": 8, "exclusiveSize": 8},
{"typeNames": ["uint32_t"], "staticSize": 4, "exclusiveSize": 4}
]}
]'''
[cases.uint64_uint64_ptr]
# Stubbed pointers were previously generated as a uintptr_t. Now they're
# generated as a special StubbedPointer type. This previously caused
# codegen problems as uintptr_t is a typedef of uint64_t and they'd both
# be specialised on a template.
param_types = ["std::pair<uint64_t, uint64_t*>&"]
setup = "return {{0, nullptr}};"
expect_json_v2 = '''[
{"staticSize": 16, "exclusiveSize": 0, "members": [
{"typeNames": ["uint64_t"], "staticSize": 8, "exclusiveSize": 8},
{"typeNames": ["uintptr_t (stubbed)"], "staticSize": 8, "exclusiveSize": 8}
]}
]'''
[cases.vector_vector] [cases.vector_vector]
oil_skip = 'tests need updating for treebuilder v2' # https://github.com/facebookexperimental/object-introspection/issues/310
param_types = ["std::pair<std::vector<std::uint64_t>, std::vector<std::uint64_t>>&"] param_types = ["std::pair<std::vector<std::uint64_t>, std::vector<std::uint64_t>>&"]
setup = "return {{std::initializer_list<std::uint64_t>({0,1,2}), std::initializer_list<std::uint64_t>({3,4,5,6})}};" setup = "return {{std::initializer_list<std::uint64_t>({0,1,2}), std::initializer_list<std::uint64_t>({3,4,5,6})}};"
expect_json = ''' expect_json = '''
@ -57,3 +81,9 @@ includes = ["vector", "utility", "cstdint"]
} }
] ]
''' '''
expect_json_v2 = '''[
{"staticSize": 48, "exclusiveSize": 0, "members": [
{"typeNames": ["std::vector<uint64_t, std::allocator<uint64_t>>"], "staticSize": 24, "exclusiveSize": 24},
{"typeNames": ["std::vector<uint64_t, std::allocator<uint64_t>>"], "staticSize": 24, "exclusiveSize": 24}
]}
]'''

View File

@ -70,7 +70,7 @@ TEST_F(AddChildrenTest, InheritancePolymorphic) {
[1] Pointer [1] Pointer
[0] Class: A (size: 16) [0] Class: A (size: 16)
Member: _vptr$A (offset: 0) Member: _vptr$A (offset: 0)
Primitive: uintptr_t Primitive: StubbedPointer
Member: int_a (offset: 8) Member: int_a (offset: 8)
Primitive: int32_t Primitive: int32_t
Function: ~A (virtual) Function: ~A (virtual)
@ -124,7 +124,7 @@ TEST_F(AddChildrenTest, InheritancePolymorphic) {
[1] Pointer [1] Pointer
[0] Class: A (size: 16) [0] Class: A (size: 16)
Member: _vptr.A (offset: 0) Member: _vptr.A (offset: 0)
Primitive: uintptr_t Primitive: StubbedPointer
Member: int_a (offset: 8) Member: int_a (offset: 8)
Primitive: int32_t Primitive: int32_t
Function: operator= Function: operator=

View File

@ -356,9 +356,9 @@ TEST_F(DrgnParserTest, PointerNoFollow) {
Member: a (offset: 0) Member: a (offset: 0)
Primitive: int32_t Primitive: int32_t
Member: b (offset: 8) Member: b (offset: 8)
Primitive: uintptr_t Primitive: StubbedPointer
Member: c (offset: 16) Member: c (offset: 16)
Primitive: uintptr_t Primitive: StubbedPointer
)", )",
options); options);
} }
@ -586,7 +586,7 @@ TEST_F(DrgnParserTest, VirtualFunctions) {
[1] Pointer [1] Pointer
[0] Class: A (size: 16) [0] Class: A (size: 16)
Member: _vptr$A (offset: 0) Member: _vptr$A (offset: 0)
Primitive: uintptr_t Primitive: StubbedPointer
Member: int_a (offset: 8) Member: int_a (offset: 8)
Primitive: int32_t Primitive: int32_t
Function: ~A (virtual) Function: ~A (virtual)
@ -598,7 +598,7 @@ TEST_F(DrgnParserTest, VirtualFunctions) {
[1] Pointer [1] Pointer
[0] Class: A (size: 16) [0] Class: A (size: 16)
Member: _vptr.A (offset: 0) Member: _vptr.A (offset: 0)
Primitive: uintptr_t Primitive: StubbedPointer
Member: int_a (offset: 8) Member: int_a (offset: 8)
Primitive: int32_t Primitive: int32_t
Function: operator= Function: operator=