Class CSpecPrototypeTestUtil
java.lang.Object
ghidra.test.compilers.support.CSpecPrototypeTestUtil
Utility for testing prototype models defined in cspec files.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyInfoFromSourceIfNeeded(Program program, PrototypeModel model) Parses function definitions and data types of global variables from the source code and applies them to the binary.static StringgetAdjustedCalleeName(String calleeName) Adjusts the name of a function to remove characters prepended by compilers.static byte[]getExtendedValue(byte[] value, DataType dt, Varnode joinPiece, DataConverter dataConverter, Consumer<String> logger) Extends the byte representation of values from their original DataType to the DataType that is indicated by the joinPiece Varnode's size.static FunctiongetFirstCall(Function function) Returns the function that calls the given function first.static ArrayList<ParameterPieces> getParameterPieces(Function caller, Function callee, PrototypeModel model) Gets the parameters between a caller and callee and organizes them in a PrototypePieces object by prototypeModel.static List<byte[]> getPassedValues(Function func, List<ParameterPieces> pieces, DataConverter dataConverter, Consumer<String> logger) For a given function, and list of parameter pieces, return a list of bytes representing the values of the parameters.static PrototypeModelgetProtoModelToTest(Program program, LanguageCompilerSpecPair langComp) Returns the PrototypeModel from the compiler spec of the given LanguageCompilerSpecPair and calling convention specified in the program's name.getTestResult(Function callee, Function caller, ArrayList<ParameterPieces> pieces, List<byte[]> fromEmulator, List<byte[]> groundTruth) Returns a TestResult record that includes a message detailing the differences between parameter data in the c source code, and parameter data in the Ghidra emulator between a specific caller function and callee function.getVarArgsParamTypes(Function func) All functions in the source code for these Cspec tests are named in such a way that the parameter types of the function are encoded in the name.static byte[]readEmulatorMemory(PcodeThread<byte[]> emulatorThread, Address address, int size) Reads data from emulator's memory at the given Address and for the given size and returns as a byte array.static byte[]readEmulatorStack(PcodeThread<byte[]> emulatorThread, Register stackReg, AddressSpace addrSpace, int offset, int size, DataConverter dataConverter) Reads data from the emulator's stack memory by resolving the current stack pointer and applying a specified offset.static byte[]readParameterPieces(PcodeThread<byte[]> emulatorThread, ParameterPieces piece, AddressSpace addrSpace, Register stackReg, LanguageCompilerSpecPair langCompPair, DataConverter dataConverter) Returns a byte array that represents parameters from the emulator.
-
Constructor Details
-
CSpecPrototypeTestUtil
public CSpecPrototypeTestUtil()
-
-
Method Details
-
readParameterPieces
public static byte[] readParameterPieces(PcodeThread<byte[]> emulatorThread, ParameterPieces piece, AddressSpace addrSpace, Register stackReg, LanguageCompilerSpecPair langCompPair, DataConverter dataConverter) throws Exception Returns a byte array that represents parameters from the emulator. This data is used to compare against the representation of parameters constructed from the binary's source code.- Parameters:
emulatorThread- the active emulator thread to inspectpiece- basic elements of a parameteraddrSpace- the address space where the stack residesstackReg- the register acting as the stack pointerlangCompPair- the language/compiler specification pairdataConverter- used to determine endianness and convert data to byte representation.- Returns:
- byte[] byte array representation of parameter pieces
- Throws:
Exception- if there is a problem reading the emulator stack or getting the correct language.
-
readEmulatorMemory
public static byte[] readEmulatorMemory(PcodeThread<byte[]> emulatorThread, Address address, int size) Reads data from emulator's memory at the given Address and for the given size and returns as a byte array.- Parameters:
emulatorThread- the active emulator thread to inspectaddress- Address of memory to readsize- Size of memory chunk to read- Returns:
- byte[] containing the data read from the emulator's memory
-
readEmulatorStack
public static byte[] readEmulatorStack(PcodeThread<byte[]> emulatorThread, Register stackReg, AddressSpace addrSpace, int offset, int size, DataConverter dataConverter) throws Exception Reads data from the emulator's stack memory by resolving the current stack pointer and applying a specified offset.- Parameters:
emulatorThread- the active emulator thread to inspectstackReg- the register acting as the stack pointeraddrSpace- the address space where the stack residesoffset- the byte offset from the stack pointersize- the number of bytes to read from the stackdataConverter- the converter used to interpret the stack pointer's endianness- Returns:
- byte[] containing the data read from the emulator's memory
- Throws:
Exception- if the register cannot be read or the address is invalid within the state
-
getPassedValues
public static List<byte[]> getPassedValues(Function func, List<ParameterPieces> pieces, DataConverter dataConverter, Consumer<String> logger) throws MemoryAccessException For a given function, and list of parameter pieces, return a list of bytes representing the values of the parameters. This data is produced from the source code of the binary which is used in cspec tests to compare against the list of bytes representing parameter values from the emulator.- Parameters:
func- Function to get the parameter values of.pieces- ParameterPieces representing basic elements of the parameters of the function.dataConverter- used to convert data to bytes and vice versa.logger-Consumer<String>lambda function to print logging information- Returns:
List<byte[]>byte representation of function parameter values- Throws:
MemoryAccessException- if there is a problem accessing the function's program memory.
-
getExtendedValue
public static byte[] getExtendedValue(byte[] value, DataType dt, Varnode joinPiece, DataConverter dataConverter, Consumer<String> logger) Extends the byte representation of values from their original DataType to the DataType that is indicated by the joinPiece Varnode's size.- Parameters:
value- the raw byte array of the original valuedt- the source data type (e.g., FloatDataType, DoubleDataType, or Structure)joinPiece- representing the target storage, used to determine target sizedataConverter- the converter used to determine endiannesslogger-Consumer<String>lambda function to print logging information- Returns:
- byte[] a byte array containing the extended value,
-
getParameterPieces
public static ArrayList<ParameterPieces> getParameterPieces(Function caller, Function callee, PrototypeModel model) Gets the parameters between a caller and callee and organizes them in a PrototypePieces object by prototypeModel. ParameterPieces may be spread between them depending on the calling convention being used.- Parameters:
caller- The function that calls the callee.callee- The function being called by the caller.model- PrototypeModel corresponding to the calling convention.- Returns:
ArrayList<ParameterPieces>
-
getVarArgsParamTypes
All functions in the source code for these Cspec tests are named in such a way that the parameter types of the function are encoded in the name. This function decodes the function names to retrieve the function parameter types as a list.- Parameters:
func- The function to decode into it's parameter's datatypes- Returns:
List<DataType>the datatypes of the parameters of the function.
-
getFirstCall
-
getAdjustedCalleeName
-
getProtoModelToTest
public static PrototypeModel getProtoModelToTest(Program program, LanguageCompilerSpecPair langComp) throws CompilerSpecNotFoundException, LanguageNotFoundException Returns the PrototypeModel from the compiler spec of the given LanguageCompilerSpecPair and calling convention specified in the program's name.- Parameters:
program- Program whose name contains the desired calling conventionlangComp- the language/compiler specification pair to query- Returns:
- PrototypeModel Model corresponding to the extracted calling convention
- Throws:
CompilerSpecNotFoundException- if the compiler specification cannot be found/loadedLanguageNotFoundException- if the specified language is not available
-
applyInfoFromSourceIfNeeded
public static void applyInfoFromSourceIfNeeded(Program program, PrototypeModel model) throws ParseException, IOException, ParseException, CodeUnitInsertionException Parses function definitions and data types of global variables from the source code and applies them to the binary. Also applies the correct signature overrides to calls of variadic functions.- Parameters:
program- Program produced from the source code being parsed.model- The PrototypeModel of the program.- Throws:
ParseException- when the c source code cannot be parsed.IOException- when the c source code cannot be parsed.ParseException- when the c source code cannot be parsed.CodeUnitInsertionException- When code units cannot be created at the given address.
-
getTestResult
public static CSpecPrototypeTestUtil.TestResult getTestResult(Function callee, Function caller, ArrayList<ParameterPieces> pieces, List<byte[]> fromEmulator, List<byte[]> groundTruth) Returns a TestResult record that includes a message detailing the differences between parameter data in the c source code, and parameter data in the Ghidra emulator between a specific caller function and callee function.- Parameters:
callee- The function being called by the caller.caller- The function that calls the callee.pieces-ArrayList<ParameterPieces>representing basic elements of the parameters between callers and callees.fromEmulator- Byte list representing parameter values from the emulator.groundTruth- Byte list representing parameter values from the c source code.- Returns:
- TestResult result record object that contains a message and a boolean hasError.
-