From 0e558caa3dd36a27d4b95e5b90cb684c8842e4cf Mon Sep 17 00:00:00 2001 From: Lee Chagolla-Christensen Date: Wed, 21 Aug 2024 15:06:25 -0700 Subject: [PATCH 1/2] Fix null exception in PropagateExternalParametersScript.java --- .../PropagateExternalParametersScript.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java b/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java index 1eecf5918e..9ae737c9bf 100644 --- a/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java +++ b/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java @@ -134,8 +134,8 @@ public class PropagateExternalParametersScript extends GhidraScript { for (Reference extRef : extRefs) { Address refAddr = extRef.getFromAddress(); - String refMnemonic = listing.getCodeUnitAt(refAddr).getMnemonicString(); + Function calledFromFunc = listing.getFunctionContaining(refAddr); if (calledFromFunc == null) { continue; @@ -147,8 +147,14 @@ public class PropagateExternalParametersScript extends GhidraScript { while (tempIter.hasNext()) { Reference thunkRef = tempIter.next(); Address thunkRefAddr = thunkRef.getFromAddress(); - String thunkRefMnemonic = - listing.getCodeUnitAt(thunkRefAddr).getMnemonicString(); + + CodeUnit cu = listing.getCodeUnitAt(thunkRefAddr); + if(cu == null) { + // println("Referenced CodeUnit is null: " + thunkRefAddr); + continue; + } + String thunkRefMnemonic = cu.getMnemonicString(); + Function thunkRefFunc = listing.getFunctionContaining(thunkRefAddr); if ((thunkRefMnemonic.equals(new String("CALL")) && (thunkRefFunc != null))) { CodeUnitIterator cuIt = @@ -297,7 +303,7 @@ public class PropagateExternalParametersScript extends GhidraScript { setEOLComment(cu.getMinAddress(), params[index].getDataType().getDisplayName() + " " + params[index].getName() + " for " + extFuncName); // add the following to the EOL comment to see the value of the optype - // +" " + toHexString(currentProgram.getListing().getInstructionAt(cu.getMinAddress()).getOperandType(0), false, true) + // + " | " + ghidra.program.model.lang.OperandType.toString(currentProgram.getListing().getInstructionAt(cu.getMinAddress()).getOperandType(0)) addResult(params[index].getName(), params[index].getDataType(), cu.getMinAddress(), extFuncName); index++; From ecf5fca01562d2d76b9bf9a8a51897daf1fc8f2d Mon Sep 17 00:00:00 2001 From: ghidra007 Date: Fri, 30 Aug 2024 15:38:32 +0000 Subject: [PATCH 2/2] GP-4883 fix null exception in PropagateExternalParamsScript --- .../PropagateExternalParametersScript.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java b/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java index 9ae737c9bf..2d97e2f76e 100644 --- a/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.java +++ b/Ghidra/Features/Base/ghidra_scripts/PropagateExternalParametersScript.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. @@ -31,6 +31,8 @@ import ghidra.program.model.symbol.*; public class PropagateExternalParametersScript extends GhidraScript { private List results = new ArrayList<>(); + private static final boolean PRINT_OPTYPE = false; + @Override public void run() throws Exception { Listing listing = currentProgram.getListing(); @@ -147,9 +149,9 @@ public class PropagateExternalParametersScript extends GhidraScript { while (tempIter.hasNext()) { Reference thunkRef = tempIter.next(); Address thunkRefAddr = thunkRef.getFromAddress(); - + CodeUnit cu = listing.getCodeUnitAt(thunkRefAddr); - if(cu == null) { + if (cu == null) { // println("Referenced CodeUnit is null: " + thunkRefAddr); continue; } @@ -300,10 +302,18 @@ public class PropagateExternalParametersScript extends GhidraScript { numSkips--; } else { + + // if option is true add the value of the optype to the EOL comment + String opType = ""; + if (PRINT_OPTYPE) { + opType = " " + toHexString(currentProgram.getListing() + .getInstructionAt(cu.getMinAddress()) + .getOperandType(0), + false, true); + } setEOLComment(cu.getMinAddress(), params[index].getDataType().getDisplayName() + - " " + params[index].getName() + " for " + extFuncName); - // add the following to the EOL comment to see the value of the optype - // + " | " + ghidra.program.model.lang.OperandType.toString(currentProgram.getListing().getInstructionAt(cu.getMinAddress()).getOperandType(0)) + " " + params[index].getName() + " for " + extFuncName + opType); + addResult(params[index].getName(), params[index].getDataType(), cu.getMinAddress(), extFuncName); index++;