Class CSpecPrototypeTestUtil

java.lang.Object
ghidra.test.compilers.support.CSpecPrototypeTestUtil

public class CSpecPrototypeTestUtil extends Object
Utility for testing prototype models defined in cspec files.
  • 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 inspect
      piece - basic elements of a parameter
      addrSpace - the address space where the stack resides
      stackReg - the register acting as the stack pointer
      langCompPair - the language/compiler specification pair
      dataConverter - 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 inspect
      address - Address of memory to read
      size - 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 inspect
      stackReg - the register acting as the stack pointer
      addrSpace - the address space where the stack resides
      offset - the byte offset from the stack pointer
      size - the number of bytes to read from the stack
      dataConverter - 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 value
      dt - the source data type (e.g., FloatDataType, DoubleDataType, or Structure)
      joinPiece - representing the target storage, used to determine target size
      dataConverter - the converter used to determine endianness
      logger - 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

      public static List<DataType> 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. 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

      public static Function getFirstCall(Function function)
      Returns the function that calls the given function first.
      Parameters:
      function - the callee which is searched for
      Returns:
      Function the caller which is first
    • getAdjustedCalleeName

      public static String getAdjustedCalleeName(String calleeName)
      Adjusts the name of a function to remove characters prepended by compilers.
      Parameters:
      calleeName - the name of the function
      Returns:
      String the name of the function after it has been adjusted
    • 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 convention
      langComp - 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/loaded
      LanguageNotFoundException - 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.