Skip to content

Commit f504977

Browse files
committed
In demanglers selectively reduce confidence under some conditions
1 parent 9f5be8d commit f504977

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

demangler/gnu3/demangled_type_node.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifdef BINARYNINJACORE_LIBRARY
1717
#include "binaryview.h"
1818
#endif
19+
#include <algorithm>
1920
#include <cinttypes>
2021

2122
#ifdef BINARYNINJACORE_LIBRARY
@@ -957,6 +958,26 @@ string DemangledTypeNode::GetTypeAndName(const StringList& name) const
957958
}
958959

959960

961+
bool DemangledTypeNode::HasUndeterminedTopLevelSize() const
962+
{
963+
switch (m_typeClass)
964+
{
965+
case NamedTypeReferenceClass:
966+
return m_widthKind == FixedWidth && m_width == 0;
967+
case ArrayTypeClass:
968+
return m_childType && m_childType->HasUndeterminedTopLevelSize();
969+
default:
970+
return false;
971+
}
972+
}
973+
974+
975+
uint8_t DemangledTypeNode::GetValueConfidence() const
976+
{
977+
return HasUndeterminedTopLevelSize() ? BN_DEFAULT_CONFIDENCE : BN_FULL_CONFIDENCE;
978+
}
979+
980+
960981
Ref<Type> DemangledTypeNode::Finalize(Architecture* arch, Platform* platform) const
961982
{
962983
switch (m_typeClass)
@@ -1043,14 +1064,22 @@ Ref<Type> DemangledTypeNode::Finalize(Architecture* arch, Platform* platform) co
10431064
case FunctionTypeClass:
10441065
{
10451066
Ref<Type> retType = m_childType ? m_childType->Finalize(arch, platform) : Ref<Type>(Type::VoidType());
1067+
uint8_t retTypeConfidence = m_childType ? m_childType->GetValueConfidence() : BN_FULL_CONFIDENCE;
1068+
retTypeConfidence = std::min(retTypeConfidence, m_returnTypeConfidence);
1069+
10461070
vector<FunctionParameter> finalParams;
10471071
finalParams.reserve(m_params.size() + (m_implicitThisParameterType ? 1 : 0));
10481072
if (m_implicitThisParameterType)
1049-
finalParams.push_back({"this", m_implicitThisParameterType->Finalize(arch, platform), true, Variable()});
1073+
{
1074+
Ref<Type> thisType = m_implicitThisParameterType->Finalize(arch, platform);
1075+
finalParams.push_back({"this", thisType->WithConfidence(m_implicitThisParameterType->GetValueConfidence()),
1076+
true, Variable()});
1077+
}
10501078
for (auto& p : m_params)
10511079
{
10521080
Ref<Type> pType = p.type ? p.type->Finalize(arch, platform) : Ref<Type>(Type::VoidType());
1053-
finalParams.push_back({p.name, pType, true, Variable()});
1081+
uint8_t pTypeConfidence = p.type ? p.type->GetValueConfidence() : BN_FULL_CONFIDENCE;
1082+
finalParams.push_back({p.name, pType->WithConfidence(pTypeConfidence), true, Variable()});
10541083
}
10551084
Confidence<Ref<CallingConvention>> callingConvention;
10561085
if (m_callingConvention)
@@ -1061,7 +1090,7 @@ Ref<Type> DemangledTypeNode::Finalize(Architecture* arch, Platform* platform) co
10611090
callingConvention = Confidence<Ref<CallingConvention>>(resolvedCallingConvention, BN_FULL_CONFIDENCE);
10621091
}
10631092
TypeBuilder tb = TypeBuilder::FunctionType(
1064-
retType->WithConfidence(static_cast<uint8_t>(m_returnTypeConfidence)), callingConvention, finalParams,
1093+
retType->WithConfidence(retTypeConfidence), callingConvention, finalParams,
10651094
Confidence<bool>(m_hasVariableArgs, m_hasVariableArgs ? BN_DEFAULT_CONFIDENCE : 0));
10661095
tb.SetConst(m_const);
10671096
tb.SetVolatile(m_volatile);

demangler/gnu3/demangled_type_node.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class DemangledTypeNode
137137
void SetTemplateArguments(_STD_VECTOR<Param> args, bool spaceAfterComma = false);
138138
void SetPointerSuffixBits(uint8_t bits) { m_pointerSuffixBits = bits; }
139139
void AddPointerSuffix(BNPointerSuffix ps) { m_pointerSuffixBits |= PointerSuffixBit(ps); }
140-
void SetReturnTypeConfidence(int8_t c) { m_returnTypeConfidence = c; }
140+
void SetReturnTypeConfidence(uint8_t c) { m_returnTypeConfidence = c; }
141141
void SetCallingConventionName(BNCallingConventionName cc) { m_callingConventionName = cc; }
142142
void SetCallingConvention(BN::Ref<BN::CallingConvention> cc) { m_callingConvention = std::move(cc); }
143143
void SetNTRType(BNNamedTypeReferenceClass cls) { m_ntrClass = cls; }
@@ -155,6 +155,9 @@ class DemangledTypeNode
155155
BN::Ref<BN::Type> Finalize(BN::Architecture* arch = nullptr, BN::Platform* platform = nullptr) const;
156156

157157
private:
158+
bool HasUndeterminedTopLevelSize() const;
159+
uint8_t GetValueConfidence() const;
160+
158161
BNTypeClass m_typeClass;
159162
BNNamedTypeReferenceClass m_ntrClass;
160163
BNReferenceType m_pointerReference;

0 commit comments

Comments
 (0)