Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-272 - Gnu Demangler - fixed demangler failure related to namespace elements that have multiple function parenetheses

dragonmacher0e6c7a685968
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java
index 8737cd9da4..ef3430330a 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java
@@ -450,8 +450,9 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {
BigInteger value = context.getValue(isaModeSwitchRegister, false);
if (value != null && program.getListing().getInstructionAt(addr) == null) {
try {
- program.getProgramContext().setValue(isaModeRegister, addr, addr,
- value);
+ program.getProgramContext()
+ .setValue(isaModeRegister, addr, addr,
+ value);
}
catch (ContextChangeException e) {
// ignore
@@ -641,7 +642,6 @@ public class CreateThunkFunctionCmd extends BackgroundCommand {
return null;
}
try {
- Msg.debug(CreateFunctionCmd.class, "Creating external function symbol: " + s.getName());
ExternalManager extMgr = program.getExternalManager();
ExternalLocation extLoc =
extMgr.addExtFunction(Library.UNKNOWN, s.getName(), null, s.getSource());
diff --git a/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java b/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java
index 7f475e471f..416083804f 100644
--- a/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java
+++ b/Ghidra/Features/GnuDemangler/src/main/java/ghidra/app/util/demangler/gnu/GnuDemanglerParser.java
@@ -605,7 +605,14 @@ public class GnuDemanglerParser {
}
}
- i = getFunctionPointerCloseParen(parameterString, i);
+ //
+ // we wish to move past two sets of parens for function pointers; however, sometimes
+ // we have code with only one set of parens; for example:
+ // unsigned long (*)(long const &)
+ // or
+ // iterator<boost::function<void ()>
+ //
+ i = findBalancedEnd(parameterString, i, '(', ')');
}
}
if (startIndex < parameterString.length()) {
@@ -615,40 +622,6 @@ public class GnuDemanglerParser {
return parameters;
}
- private int getFunctionPointerCloseParen(String parameterString, int currentIndex) {
- int firstCloseParen = parameterString.indexOf(')', currentIndex);
- if (firstCloseParen == -1) {
- throw new DemanglerParseException(
- "Unable to find closing paren for parameter string: " + parameterString);
- }
-
- //
- // we wish to move past two sets of parens for function pointers; however, sometimes
- // we have code with only one set of parens; for example:
- // unsigned long (*)(long const &)
- // or
- // iterator<boost::function<void ()>
- //
- boolean foundNextStart = false;
- int length = parameterString.length();
- for (int i = currentIndex; i < length; i++) {
- char ch = parameterString.charAt(i);
- if (ch == ')') {
- return i;
- }
- else if (ch == '(') {
- foundNextStart = true;
- }
- else if (ch == ',') {
- if (!foundNextStart) {
- return firstCloseParen;// no new set of parens found
- }
- }
- }
-
- return firstCloseParen;
- }
-
/**
* This method converts each parameter string into
* actual DemangledDataType objects.
@@ -868,26 +841,31 @@ public class GnuDemanglerParser {
}
/**
- * Scans the given string from the given offset looking for a template and reporting the
- * index of the closing template character '>' or -1 if no templates are found
+ * Scans the given string from the given offset looking for a balanced {@code close}
+ * character. This algorithm will not report a match for the end character until the
+ * {@code open} character has first been found. This allows clients to scan from anywhere
+ * in a string to find an open and start character combination, including at or before the
+ * desired opening character.
*
* @param string the input string
* @param start the start position within the string
- * @return the template end index; -1 if no templates found
+ * @param open the open character (e.g, '(' or '<')
+ * @param close the close character (e.g, ')' or '>')
+ * @return the end index; -1 if no templates found
*/
- private int findTemplateEnd(String string, int start) {
+ private int findBalancedEnd(String string, int start, char open, char close) {
boolean found = false;
int depth = 0;
for (int i = start; i < string.length(); i++) {
- switch (string.charAt(i)) {
- case '<':
- depth++;
- found = true;
- break;
- case '>':
- depth--;
- break;
+
+ char c = string.charAt(i);
+ if (c == open) {
+ depth++;
+ found = true;
+ }
+ else if (c == close) {
+ depth--;
}
if (found && depth == 0) {
@@ -898,29 +876,50 @@ public class GnuDemanglerParser {
return -1;
}
- // assumption: the given index is in a template
- // Walk backwards to find the template start
- private int findMatchingTemplateStart(String string, int templateEnd) {
+ /**
+ * Scans the given string from the given offset looking for a balanced {@code open}
+ * character. This algorithm will not report a match for the open character until the
+ * {@code end} character has first been found. This allows clients to scan from anywhere
+ * in a string to find an open and start character combination, including at or before the
+ * desired opening character.
+ *
+ * @param string the input string
+ * @param start the start position within the string
+ * @param open the open character (e.g, '(' or '<')
+ * @param close the close character (e.g, ')' or '>')
+ * @return the end index; -1 if no templates found
+ */
+ private int findBalancedStart(String string, int start, char open, char close) {
+
+ boolean found = false;
+ int depth = 0;
+ for (int i = start; i >= 0; i--) {
- int depth = 1;
- for (int i = templateEnd - 1; i >= 0; i--) {
- switch (string.charAt(i)) {
- case '<':
- depth--;
- break;
- case '>':
- depth++;
- break;
+ char c = string.charAt(i);
+ if (c == open) {
+ depth--;
+ }
+ else if (c == close) {
+ depth++;
+ found = true;
}
- if (depth == 0) {
- return i;// found our opening tag
+ if (found && depth == 0) {
+ return i;
}
}
return -1;
}
+ private int findTemplateEnd(String string, int start) {
+ return findBalancedEnd(string, start, '<', '>');
+ }
+
+ private int findTemplateStart(String string, int templateEnd) {
+ return findBalancedStart(string, templateEnd, '<', '>');
+ }
+
private DemangledDataType createTypeInNamespace(String name) {
List<String> names = SymbolPathParser.parse(name, false);
DemangledType namespace = null;
@@ -1477,7 +1476,7 @@ public class GnuDemanglerParser {
int templateEnd = findTemplateEnd(text, 0);
int templateStart = -1;
if (templateEnd != -1) {
- templateStart = findMatchingTemplateStart(text, templateEnd);
+ templateStart = findTemplateStart(text, templateEnd);
}
if (paramStart > templateStart && paramStart < templateEnd) {
// ignore parentheses inside of templates (they are cast operators)
diff --git a/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java b/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java
index 2214691b66..b3ee889a3a 100644
--- a/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java
+++ b/Ghidra/Features/GnuDemangler/src/test/java/ghidra/app/util/demangler/GnuDemanglerParserTest.java
@@ -1479,6 +1479,29 @@ public class GnuDemanglerParserTest extends AbstractGenericTest {
signature);
}
+ @Test
+ public void testNamespaceElementWithMultipleFunctionParentheses() throws Exception {
+
+ // __ZN7brigand13for_each_argsIZN7WebCore11JSConverterINS1_8IDLUnionIJNS1_7IDLNullENS1_12IDLDOMStringENS1_21IDLUnrestrictedDoubleEEEEE7convertERN3JSC9ExecStateERNS1_17JSDOMGlobalObjectERKN3WTF7VariantIJDnNSE_6StringEdEEEEUlOT_E_JNS_5type_INSt3__117integral_constantIlLl0EEEEENSN_INSP_IlLl1EEEEENSN_INSP_IlLl2EEEEEEEESK_SK_DpOT0_
+
+ DemangledObject object = parser.parse(
+ "__ZN7brigand13for_each_argsIZN7WebCore11JSConverterINS1_8IDLUnionIJNS1_7IDLNullENS1_12IDLDOMStringENS1_21IDLUnrestrictedDoubleEEEEE7convertERN3JSC9ExecStateERNS1_17JSDOMGlobalObjectERKN3WTF7VariantIJDnNSE_6StringEdEEEEUlOT_E_JNS_5type_INSt3__117integral_constantIlLl0EEEEENSN_INSP_IlLl1EEEEENSN_INSP_IlLl2EEEEEEEESK_SK_DpOT0_",
+ "WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1} brigand::for_each_args<WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1}, brigand::type_<std::__1::integral_constant<long, 0l> >, WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1}<std::__1<long, 1l> >, WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1}<std::__1<long, 2l> > >(WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1}, brigand::type_<std::__1::integral_constant<long, 0l> >&&, WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1}<std::__1<long, 1l> >&&, WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull, WebCore::IDLDOMString, WebCore::IDLUnrestrictedDouble> >::convert(JSC::ExecState&, WebCore::JSDOMGlobalObject&, WTF::Variant<decltype(nullptr), WTF::String, double> const&)::{lambda(auto:1&&)#1}<std::__1<long, 2l> >&&)");
+
+ assertNotNull(object);
+ assertType(object, DemangledFunction.class);
+
+ String name =
+ "for_each_args<WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1},brigand::type_<std::__1::integral_constant<long,0l>>,WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1}<std::__1<long,1l>>,WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1}<std::__1<long,2l>>>";
+ assertName(object, name,
+ "brigand");
+
+ String signature = object.getSignature(false);
+ assertEquals(
+ "WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1} brigand::for_each_args<WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1},brigand::type_<std::__1::integral_constant<long,0l>>,WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1}<std::__1<long,1l>>,WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1}<std::__1<long,2l>>>(WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1},brigand::type_<std::__1::integral_constant<long,0l>> &&,WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1}<std::__1<long,1l>> &&,WebCore::JSConverter<WebCore::IDLUnion<WebCore::IDLNull,WebCore::IDLDOMString,WebCore::IDLUnrestrictedDouble>>::convert(JSC::ExecState&,WebCore::JSDOMGlobalObject&,WTF::Variant<decltype(nullptr),WTF::String,double>const&)::{lambda(auto:1&&)#1}<std::__1<long,2l>> &&)",
+ signature);
+ }
+
@Test
public void testOperator_Equals_ExcessivelyTemplated() throws Exception {
@@ -1495,7 +1518,7 @@ public class GnuDemanglerParserTest extends AbstractGenericTest {
String signature = object.getSignature(false);
assertEquals(
- "WTF::Function<void ()> & WTF::Function<void()>::operator=<WebCore::IDBClient::TransactionOperationImpl<WebCore::IDBObjectStoreInfo_const&>::TransactionOperationImpl(WebCore::IDBTransaction&,void(WebCore::IDBTransaction::*)(WebCore::IDBResultData_const &,void ()(WebCore::IDBClient::TransactionOperation &,WebCore::IDBObjectStoreInfo_const &),WebCore::IDBObjectStoreInfo_const&)::{lambda()#1},void>(WebCore::IDBClient::TransactionOperationImpl<WebCore::IDBObjectStoreInfo const&>::TransactionOperationImpl(WebCore::IDBTransaction&, void (WebCore::IDBTransaction::*)(WebCore::IDBResultData const &,void ()(WebCore::IDBClient::TransactionOperation &,WebCore::IDBObjectStoreInfo const &),WebCore::IDBObjectStoreInfo const&)::{lambda()#1} &&)",
+ "WTF::Function<void ()> & WTF::Function<void()>::operator=<WebCore::IDBClient::TransactionOperationImpl<WebCore::IDBObjectStoreInfo_const&>::TransactionOperationImpl(WebCore::IDBTransaction&,void(WebCore::IDBTransaction::*)(WebCore::IDBResultData_const&),void(WebCore::IDBTransaction::*)(WebCore::IDBClient::TransactionOperation&,WebCore::IDBObjectStoreInfo_const&),WebCore::IDBObjectStoreInfo_const&)::{lambda()#1},void>(WebCore::IDBClient::TransactionOperationImpl<WebCore::IDBObjectStoreInfo_const&>::TransactionOperationImpl(WebCore::IDBTransaction&,void(WebCore::IDBTransaction::*)(WebCore::IDBResultData_const&),void(WebCore::IDBTransaction::*)(WebCore::IDBClient::TransactionOperation&,WebCore::IDBObjectStoreInfo_const&),WebCore::IDBObjectStoreInfo_const&)::{lambda()#1} &&)",
signature);
}