GP-4898 - more Demangler changes: interface, individual demanglers, tests

This commit is contained in:
ghizard 2024-09-26 13:58:46 -04:00
parent 9e5c6fecf2
commit c51f65c376
7 changed files with 64 additions and 61 deletions

View File

@ -39,13 +39,6 @@ public class RustDemangler implements Demangler {
return RustUtilities.isRustProgram(program);
}
@Override
@Deprecated(since = "11.3", forRemoval = true)
public DemangledObject demangle(String mangled, DemanglerOptions options) {
MangledContext mangledContext = createMangledContext(mangled, options, null, null);
return demangle(mangledContext);
}
@Override
public DemangledObject demangle(MangledContext context) {
DemanglerOptions options = context.getOptions();

View File

@ -37,40 +37,47 @@ public interface Demangler extends ExtensionPoint {
* default options ({@link #createDefaultOptions()}.
*
* @param mangled the mangled string
* @return the result
* @return the result; {@code null} is possible if the mangled string is not supported
* @throws DemangledException if the string cannot be demangled
*/
public default DemangledObject demangle(String mangled) throws DemangledException {
MangledContext mangledContext = createMangledContext(mangled, null, null, null);
DemangledObject demangledObject = demangle(mangledContext);
if (demangledObject != null && demangledObject.getMangledContext() == null) {
demangledObject.setMangledContext(mangledContext);
}
return demangle(mangledContext);
}
/**
* Deprecated. Use {@link #demangle(String)} or
* {@link #demangle(MangledContext)}.
*
* Attempts to demangle the given string using the given options
*
* @param mangled the mangled string
* @param options the options
* @return the result
* @return the result; {@code null} is possible if the mangled string is not supported
* @throws DemangledException if the string cannot be demangled
* @deprecated see above
* @deprecated Use {@link #demangle(String)} or {@link #demangle(MangledContext)}.
*/
@Deprecated(since = "11.3", forRemoval = true)
public DemangledObject demangle(String mangled, DemanglerOptions options)
throws DemangledException;
public default DemangledObject demangle(String mangled, DemanglerOptions options)
throws DemangledException {
MangledContext mangledContext = createMangledContext(mangled, options, null, null);
DemangledObject demangledObject = demangle(mangledContext);
if (demangledObject != null && demangledObject.getMangledContext() == null) {
demangledObject.setMangledContext(mangledContext);
}
return demangle(mangledContext);
}
/**
* Attempts to demangle the string of the mangled context
* Attempts to demangle the string of the mangled context and sets the mangled context on
* the {@link DemangledObject}
*
* @param context the mangled context
* @return the result
* @return the result; {@code null} is possible if the mangled string is not supported
* @throws DemangledException if the string cannot be demangled
*/
public default DemangledObject demangle(MangledContext context) throws DemangledException {
return demangle(context.getMangled(), context.getOptions());
}
public DemangledObject demangle(MangledContext context) throws DemangledException;
/**
* Creates default options for this particular demangler

View File

@ -65,14 +65,6 @@ public class GnuDemangler implements Demangler {
return false;
}
@Override
@Deprecated(since = "11.3", forRemoval = true)
public DemangledObject demangle(String mangled, DemanglerOptions demanglerOptions)
throws DemangledException {
MangledContext mangledContext = createMangledContext(mangled, demanglerOptions, null, null);
return demangle(mangledContext);
}
@Override
public DemangledObject demangle(MangledContext mangledContext)
throws DemangledException {

View File

@ -23,8 +23,7 @@ import org.junit.Before;
import org.junit.Test;
import generic.test.AbstractGenericTest;
import ghidra.app.util.demangler.DemangledException;
import ghidra.app.util.demangler.DemangledObject;
import ghidra.app.util.demangler.*;
import ghidra.program.database.ProgramDB;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.TerminatedStringDataType;
@ -78,7 +77,9 @@ public class GnuDemanglerTest extends AbstractGenericTest {
GnuDemanglerOptions options = new GnuDemanglerOptions();
options.setDemangleOnlyKnownPatterns(false);
try {
demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
demangler.demangle(mangledContext);
fail("Demangle should have failed attempting to demangle a non-mangled string");
}
catch (DemangledException e) {
@ -104,7 +105,9 @@ public class GnuDemanglerTest extends AbstractGenericTest {
GnuDemanglerOptions options = new GnuDemanglerOptions();
options.setUseStandardReplacements(true);
DemangledObject dobj = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject dobj = demangler.demangle(mangledContext);
assertNotNull(dobj);
String signature = dobj.getSignature();
@ -115,8 +118,8 @@ public class GnuDemanglerTest extends AbstractGenericTest {
//
// Now disable demangled string replacement
//
options.setUseStandardReplacements(false);
dobj = demangler.demangle(mangled, options);
options.setUseStandardReplacements(false); // options are still in context
dobj = demangler.demangle(mangledContext);
assertNotNull(dobj);
String fullSignature = dobj.getSignature();
@ -224,7 +227,9 @@ public class GnuDemanglerTest extends AbstractGenericTest {
demangler.canDemangle(program);// this perform initialization
GnuDemanglerOptions options = new GnuDemanglerOptions(GnuDemanglerFormat.EDG);
DemangledObject result = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject result = demangler.demangle(mangledContext);
assertNull(result);
}
@ -239,7 +244,9 @@ public class GnuDemanglerTest extends AbstractGenericTest {
GnuDemanglerOptions options = new GnuDemanglerOptions(GnuDemanglerFormat.AUTO, true);
options.setDemangleOnlyKnownPatterns(false);
DemangledObject result = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject result = demangler.demangle(mangledContext);
assertNotNull(result);
assertEquals("undefined MyFunction::~MyFunction(void)", result.getSignature(false));
}
@ -257,7 +264,9 @@ public class GnuDemanglerTest extends AbstractGenericTest {
GnuDemanglerOptions options = new GnuDemanglerOptions(GnuDemanglerFormat.AUTO, true);
options.setDemangleOnlyKnownPatterns(false);
DemangledObject result = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject result = demangler.demangle(mangledContext);
assertNotNull(result);
assertEquals("undefined TTextPanel::scroll(unsigned char,short,int)",
result.getSignature(false));

View File

@ -48,14 +48,6 @@ public class MicrosoftDemangler implements Demangler {
executableFormat.indexOf(MSCoffLoader.MSCOFF_NAME) != -1);
}
@Override
@Deprecated(since = "11.3", forRemoval = true)
public DemangledObject demangle(String mangled, DemanglerOptions options)
throws DemangledException {
MangledContext mangledContext = new MangledContext(null, options, mangled, null);
return demangle(mangledContext);
}
@Override
public DemangledObject demangle(MangledContext context) throws DemangledException {
if (!(context instanceof MicrosoftMangledContext mContext)) {

View File

@ -89,22 +89,27 @@ public class SwiftDemangler implements Demangler {
}
@Override
@Deprecated(since = "11.3", forRemoval = true)
public DemangledObject demangle(String mangled, DemanglerOptions op) throws DemangledException {
SwiftDemanglerOptions options = getSwiftDemanglerOptions(op);
public DemangledObject demangle(MangledContext context) throws DemangledException {
SwiftDemanglerOptions options = getSwiftDemanglerOptions(context.getOptions());
String mangled = context.getMangled();
Demangled demangled = getDemangled(mangled, options);
DemangledObject demangledObject;
if (demangled instanceof DemangledFunction func) {
return func;
demangledObject = func;
}
else if (demangled instanceof DemangledLabel label) {
return label;
demangledObject = label;
}
else if (demangled instanceof DemangledUnknown unknown) {
return new DemangledLabel(mangled, unknown.getOriginalDemangled(),
demangledObject = new DemangledLabel(mangled, unknown.getOriginalDemangled(),
options.getUnsupportedPrefix() + unknown.getOriginalDemangled());
}
else {
return null;
}
demangledObject.setMangledContext(context);
return demangledObject;
}
/**
* Get a new {@link Demangled} by demangling the given mangled string

View File

@ -21,8 +21,7 @@ import org.junit.Before;
import org.junit.Test;
import ghidra.app.cmd.label.DemanglerCmd;
import ghidra.app.util.demangler.DemangledException;
import ghidra.app.util.demangler.DemangledObject;
import ghidra.app.util.demangler.*;
import ghidra.program.database.ProgramDB;
import ghidra.program.model.address.Address;
import ghidra.test.AbstractGhidraHeadlessIntegrationTest;
@ -62,7 +61,9 @@ public class GnuDemanglerIntegrationTest extends AbstractGhidraHeadlessIntegrati
GnuDemanglerOptions options = new GnuDemanglerOptions();
options.setDemangleOnlyKnownPatterns(false);
options = options.withDemanglerFormat(GnuDemanglerFormat.AUTO, true);
DemangledObject result = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject result = demangler.demangle(mangledContext);
assertNotNull(result);
assertEquals("undefined MyNamespace::MyFunction($ParamNamespace::paramName *)",
result.getSignature(false));
@ -87,7 +88,9 @@ public class GnuDemanglerIntegrationTest extends AbstractGhidraHeadlessIntegrati
GnuDemanglerOptions options = new GnuDemanglerOptions();
options.setDemangleOnlyKnownPatterns(false);
options = options.withDemanglerFormat(GnuDemanglerFormat.AUTO, true);
DemangledObject result = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject result = demangler.demangle(mangledContext);
assertNotNull(result);
assertEquals("undefined SoloGimbalEKF::{unnamed_type#1}::SoloGimbalEKF(void)",
result.getSignature(false));
@ -117,7 +120,9 @@ public class GnuDemanglerIntegrationTest extends AbstractGhidraHeadlessIntegrati
GnuDemanglerOptions options = new GnuDemanglerOptions();
options.setDemangleOnlyKnownPatterns(false);
options = options.withDemanglerFormat(GnuDemanglerFormat.AUTO, true);
DemangledObject result = demangler.demangle(mangled, options);
MangledContext mangledContext =
demangler.createMangledContext(mangled, options, program, null);
DemangledObject result = demangler.demangle(mangledContext);
assertNotNull(result);
assertEquals(
"int JSC::Structure::add<(JSC::Structure::ShouldPin)1,JSC::JSObject::prepareToPutDirectWithoutTransition(JSC::VM&,JSC::PropertyName,unsigned_int,unsigned_int,JSC::Structure*)::{lambda(JSC::GCSafeConcurrentJSLocker_const&,int,int)#1}>(JSC::VM &,JSC::PropertyName,unsigned int,JSC::JSObject::prepareToPutDirectWithoutTransition(JSC::VM&,JSC::PropertyName,unsigned_int,unsigned_int,JSC::Structure*)::{lambda(JSC::GCSafeConcurrentJSLocker const&, int, int)#1} const &)",