Skip to content

[5.0] demangler: catch more cases where the demangler crashes on malformed symbols #13832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ NodePointer Demangler::demangleTypeMangling() {
auto TypeMangling = createNode(Node::Kind::TypeMangling);

addChild(TypeMangling, LabelList);
addChild(TypeMangling, Type);
TypeMangling = addChild(TypeMangling, Type);
return TypeMangling;
}

Expand Down Expand Up @@ -908,8 +908,12 @@ NodePointer Demangler::popFunctionParamLabels(NodePointer Type) {
auto LabelList = createNode(Node::Kind::LabelList);
for (unsigned i = 0, n = ParameterType->getIndex(); i != n; ++i) {
auto Label = popNode();
assert(Label && (Label->getKind() == Node::Kind::Identifier ||
Label->getKind() == Node::Kind::FirstElementMarker));
if (!Label)
return nullptr;
if (Label->getKind() != Node::Kind::Identifier &&
Label->getKind() != Node::Kind::FirstElementMarker)
return nullptr;

LabelList->addChild(Label, *this);
}

Expand Down Expand Up @@ -1999,16 +2003,16 @@ NodePointer Demangler::demangleFunctionEntity() {
case None:
break;
case Index:
addChild(Entity, NameOrIndex);
Entity = addChild(Entity, NameOrIndex);
break;
case TypeAndMaybePrivateName:
addChild(Entity, LabelList);
addChild(Entity, ParamType);
Entity = addChild(Entity, ParamType);
addChild(Entity, NameOrIndex);
break;
case TypeAndIndex:
addChild(Entity, NameOrIndex);
addChild(Entity, ParamType);
Entity = addChild(Entity, NameOrIndex);
Entity = addChild(Entity, ParamType);
break;
}
return Entity;
Expand All @@ -2035,9 +2039,9 @@ NodePointer Demangler::demangleSubscript() {
NodePointer Context = popContext();

NodePointer Subscript = createNode(Node::Kind::Subscript);
addChild(Subscript, Context);
Subscript = addChild(Subscript, Context);
addChild(Subscript, LabelList);
addChild(Subscript, Type);
Subscript = addChild(Subscript, Type);
addChild(Subscript, PrivateName);

return demangleAccessor(Subscript);
Expand Down
29 changes: 25 additions & 4 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,27 @@ class NodePrinter {
DemanglerPrinter Printer;
DemangleOptions Options;
bool SpecializationPrefixPrinted = false;
bool isValid = true;

public:
NodePrinter(DemangleOptions options) : Options(options) {}

std::string printRoot(NodePointer root) {
isValid = true;
print(root);
return std::move(Printer).str();
if (isValid)
return std::move(Printer).str();
return "";
}

private:
private:
/// Called when the node tree in valid.
///
/// The demangler already catches most error cases and mostly produces valid
/// node trees. But some cases are difficult to catch in the demangler and
/// instead the NodePrinter bails.
void setInvalid() { isValid = false; }

void printChildren(Node::iterator begin,
Node::iterator end,
const char *sep = nullptr) {
Expand Down Expand Up @@ -553,7 +564,10 @@ class NodePrinter {

void printFunctionParameters(NodePointer LabelList, NodePointer ParameterType,
bool showTypes) {
assert(ParameterType->getKind() == Node::Kind::ArgumentTuple);
if (ParameterType->getKind() != Node::Kind::ArgumentTuple) {
setInvalid();
return;
}

NodePointer Parameters = ParameterType->getFirstChild();
assert(Parameters->getKind() == Node::Kind::Type);
Expand Down Expand Up @@ -609,7 +623,10 @@ class NodePrinter {
}

void printFunctionType(NodePointer LabelList, NodePointer node) {
assert(node->getNumChildren() == 2 || node->getNumChildren() == 3);
if (node->getNumChildren() != 2 && node->getNumChildren() != 3) {
setInvalid();
return;
}
unsigned startIndex = 0;
if (node->getChild(0)->getKind() == Node::Kind::ThrowsAnnotation)
startIndex = 1;
Expand Down Expand Up @@ -1925,6 +1942,10 @@ printEntity(NodePointer Entity, bool asPrefixContext, TypePrinting TypePr,
if (TypePr != TypePrinting::NoType) {
NodePointer type = getChildIf(Entity, Node::Kind::Type);
assert(type && "malformed entity");
if (!type) {
setInvalid();
return nullptr;
}
type = type->getChild(0);
if (TypePr == TypePrinting::FunctionStyle) {
// We expect to see a function type here, but if we don't, use the colon.
Expand Down
6 changes: 6 additions & 0 deletions test/Demangle/Inputs/manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,10 @@ _T0So5GizmoC11doSomethingySQyypGSQySaySSGGFToTembnn_ ---> outlined bridged metho
_T0So5GizmoC12modifyString_10withNumber0D6FoobarSQySSGAF_SiSQyypGtFToTembnnnb_ ---> outlined bridged method (mbnnnb) of @objc __ObjC.Gizmo.modifyString(_: Swift.String!, withNumber: Swift.Int, withFoobar: Any!) -> Swift.String!
_T04test1SVyxGAA1RA2A1ZRzAA1Y2ZZRpzl1A_AhaGPWT ---> {C} associated type witness table accessor for A.ZZ : test.Y in <A where A: test.Z, A.ZZ: test.Y> test.S<A> : test.R in test
_T0s24_UnicodeScalarExceptions33_0E4228093681F6920F0AB2E48B4F1C69LLVACycfC ---> Swift.(_UnicodeScalarExceptions in _0E4228093681F6920F0AB2E48B4F1C69).init() -> Swift.(_UnicodeScalarExceptions in _0E4228093681F6920F0AB2E48B4F1C69)
_T0D ---> _T0D
_T0s18EnumeratedIteratorVyxGs8Sequencess0B8ProtocolRzlsADP5splitSay03SubC0QzGSi9maxSplits_Sb25omittingEmptySubsequencesSb7ElementQzKc14whereSeparatortKFTW ---> _T0s18EnumeratedIteratorVyxGs8Sequencess0B8ProtocolRzlsADP5splitSay03SubC0QzGSi9maxSplits_Sb25omittingEmptySubsequencesSb7ElementQzKc14whereSeparatortKFTW
_T0s3SetVyxGs10CollectiotySivm ---> _T0s3SetVyxGs10CollectiotySivm
_T0s18ReversedCollectionVyxGs04LazyB8ProtocolfC ---> _T0s18ReversedCollectionVyxGs04LazyB8ProtocolfC
_T0iW ---> _T0iW
_T0s5print_9separator10terminatoryypfC ---> _T0s5print_9separator10terminatoryypfC