Ghidra is a software reverse engineering (SRE) framework
Class loading fixes
dragonmacher5b0295e0d155
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/nav/LocationMemento.java b/Ghidra/Features/Base/src/main/java/ghidra/app/nav/LocationMemento.javaindex 7b0c9210e7..72c5b7b1b2 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/nav/LocationMemento.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/nav/LocationMemento.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -15,6 +15,9 @@*/package ghidra.app.nav;+import java.lang.reflect.Constructor;+import java.lang.reflect.InvocationTargetException;+import ghidra.framework.options.SaveState;import ghidra.program.model.listing.Program;import ghidra.program.util.AddressFieldLocation;@@ -22,9 +25,6 @@ import ghidra.program.util.ProgramLocation;import ghidra.util.Msg;import ghidra.util.SystemUtilities;-import java.lang.reflect.Constructor;-import java.lang.reflect.InvocationTargetException;-public class LocationMemento {private static final String PROGRAM_PATH = "PROGRAM_PATH_";private static final String PROGRAM_ID = "PROGRAM_ID";@@ -86,7 +86,8 @@ public class LocationMemento {return false;}LocationMemento other = (LocationMemento) obj;- return (program == other.program && compareLocations(programLocation, other.programLocation));+ return (program == other.program &&+ compareLocations(programLocation, other.programLocation));}@Override@@ -142,7 +143,6 @@ public class LocationMemento {programLocation.saveState(saveState);}- @SuppressWarnings("unchecked")// we saved the class, it should be the right typepublic static LocationMemento getLocationMemento(SaveState saveState, Program[] programs) {String className = saveState.getString(MEMENTO_CLASS, null);@@ -150,10 +150,15 @@ public class LocationMemento {return null;}+ ClassLoader loader = LocationMemento.class.getClassLoader();try {- Class<? extends LocationMemento> mementoClass =- (Class<? extends LocationMemento>) Class.forName(className);+ Class<?> clazz = Class.forName(className, false, loader);+ if (!LocationMemento.class.isAssignableFrom(clazz)) {+ Msg.error(LocationMemento.class, "Class is not a LocationMemento: " + clazz);+ return null;+ }+ Class<? extends LocationMemento> mementoClass = clazz.asSubclass(LocationMemento.class);Constructor<? extends LocationMemento> constructor =mementoClass.getConstructor(SaveState.class, Program[].class);return constructor.newInstance(saveState, programs);@@ -182,7 +187,8 @@ public class LocationMemento {// Cause could be null here, so protect.String message = cause == null ? "" : cause.getMessage();- throw new IllegalArgumentException("Unexpected exception restoring memento: " + message);+ throw new IllegalArgumentException(+ "Unexpected exception restoring memento: " + message);}return null;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/ProgramLocation.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/ProgramLocation.javaindex b4cfcfe7dd..5d22182486 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/ProgramLocation.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/ProgramLocation.java@@ -278,8 +278,10 @@ public class ProgramLocation implements Cloneable, Comparable<ProgramLocation> {return null;}+ ClassLoader loader = ProgramLocation.class.getClassLoader();try {- Class<?> locationClass = Class.forName(className);++ Class<?> locationClass = Class.forName(className, false, loader);if (locationClass.isInterface()) {// This check is needed due to a refactoring that has changed a class into an// interface. The class name may have been saved into the tool. Upon restoring we@@ -288,6 +290,12 @@ public class ProgramLocation implements Cloneable, Comparable<ProgramLocation> {return null;}+ if (!ProgramLocation.class.isAssignableFrom(locationClass)) {+ Msg.error(ProgramLocation.class,+ "Class is not a ProgramLocation: " + locationClass);+ return null;+ }+ProgramLocation loc = (ProgramLocation) locationClass.getConstructor().newInstance();loc.restoreState(program, saveState);if (loc.getAddress() != null) {