Skip to content

Demangler: Save state to allow reentrant demangling. #25856

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
Jun 28, 2019
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
16 changes: 15 additions & 1 deletion include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,21 @@ class Demangler : public NodeFactory {
return popNode();
}

void init(StringRef MangledName);
/// This class handles preparing the initial state for a demangle job in a reentrant way, pushing the
/// existing state back when a demangle job is completed.
class DemangleInitRAII {
Demangler &Dem;
Vector<NodePointer> NodeStack;
Vector<NodePointer> Substitutions;
int NumWords;
StringRef Text;
size_t Pos;

public:
DemangleInitRAII(Demangler &Dem, StringRef MangledName);
~DemangleInitRAII();
};
friend DemangleInitRAII;

void addSubstitution(NodePointer Nd) {
if (Nd)
Expand Down
34 changes: 25 additions & 9 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,16 +492,32 @@ void Demangler::clear() {
NodeFactory::clear();
}

void Demangler::init(StringRef MangledName) {
NodeStack.init(*this, 16);
Substitutions.init(*this, 16);
NumWords = 0;
Text = MangledName;
Pos = 0;
Demangler::DemangleInitRAII::DemangleInitRAII(Demangler &Dem,
StringRef MangledName)
// Save the current demangler state so we can restore it.
: Dem(Dem),
NodeStack(Dem.NodeStack), Substitutions(Dem.Substitutions),
NumWords(Dem.NumWords), Text(Dem.Text), Pos(Dem.Pos)
{
// Reset the demangler state for a nested job.
Dem.NodeStack.init(Dem, 16);
Dem.Substitutions.init(Dem, 16);
Dem.NumWords = 0;
Dem.Text = MangledName;
Dem.Pos = 0;
}

Demangler::DemangleInitRAII::~DemangleInitRAII() {
// Restore the saved state.
Dem.NodeStack = NodeStack;
Dem.Substitutions = Substitutions;
Dem.NumWords = NumWords;
Dem.Text = Text;
Dem.Pos = Pos;
}

NodePointer Demangler::demangleSymbol(StringRef MangledName) {
init(MangledName);
DemangleInitRAII state(*this, MangledName);

// Demangle old-style class and protocol names, which are still used in the
// ObjC metadata.
Expand Down Expand Up @@ -546,7 +562,7 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {
}

NodePointer Demangler::demangleType(StringRef MangledName) {
init(MangledName);
DemangleInitRAII state(*this, MangledName);

parseAndPushNodes();

Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Utils/SpecializationMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AttributeDemangler : public Demangle::Demangler {
public:
void demangleAndAddAsChildren(StringRef MangledSpecialization,
NodePointer Parent) {
init(MangledSpecialization);
DemangleInitRAII state(*this, MangledSpecialization);
if (!parseAndPushNodes()) {
llvm::errs() << "Can't demangle: " << MangledSpecialization << '\n';
abort();
Expand Down
8 changes: 8 additions & 0 deletions test/Reflection/Inputs/ConcreteTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ public struct References {
public unowned var unownedRef: C
public unowned(unsafe) var unownedUnsafeRef: C
}

private struct PrivateStructField {
var x: Int16
}

public struct HasArrayOfPrivateStructField {
private var x: [PrivateStructField]
}
15 changes: 11 additions & 4 deletions test/Reflection/typeref_decoding.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// REQUIRES: no_asan
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/Inputs/ConcreteTypes.swift %S/Inputs/GenericTypes.swift %S/Inputs/Protocols.swift %S/Inputs/Extensions.swift %S/Inputs/Closures.swift -parse-as-library -emit-module -emit-library -module-name TypesToReflect -o %t/%target-library-name(TypesToReflect)
// RUN: %target-build-swift -Xfrontend -enable-anonymous-context-mangled-names %S/Inputs/ConcreteTypes.swift %S/Inputs/GenericTypes.swift %S/Inputs/Protocols.swift %S/Inputs/Extensions.swift %S/Inputs/Closures.swift -parse-as-library -emit-module -emit-library -module-name TypesToReflect -o %t/%target-library-name(TypesToReflect)
// RUN: %target-swift-reflection-dump -binary-filename %t/%target-library-name(TypesToReflect) | %FileCheck %s

// CHECK: FIELDS:
Expand Down Expand Up @@ -201,6 +201,13 @@
// CHECK: (unmanaged_storage
// CHECK: (class TypesToReflect.C))

// CHECK: TypesToReflect.(PrivateStructField
// CHECK: ----------------------------------

// CHECK: TypesToReflect.HasArrayOfPrivateStructField
// CHECK: -------------------------------------------
// CHECK: x: Swift.Array<TypesToReflect.(PrivateStructField{{.*}})>

// CHECK: TypesToReflect.C1
// CHECK: -----------------
// CHECK: aClass: TypesToReflect.C1<A>
Expand Down Expand Up @@ -652,14 +659,14 @@
// CHECK: TypesToReflect.ClassBoundP
// CHECK: --------------------------

// CHECK: TypesToReflect.(FileprivateProtocol in _{{[0-9a-fA-F]+}})
// CHECK: TypesToReflect.(FileprivateProtocol in {{.*}})
// CHECK: -------------------------------------------------------------------------

// CHECK: TypesToReflect.HasFileprivateProtocol
// CHECK: -------------------------------------
// CHECK: x: TypesToReflect.(FileprivateProtocol in ${{[0-9a-fA-F]+}})
// CHECK: x: TypesToReflect.(FileprivateProtocol in {{.*}})
// CHECK: (protocol_composition
// CHECK-NEXT: (protocol TypesToReflect.(FileprivateProtocol in ${{[0-9a-fA-F]+}})))
// CHECK-NEXT: (protocol TypesToReflect.(FileprivateProtocol in {{.*}})))

// CHECK: ASSOCIATED TYPES:
// CHECK: =================
Expand Down