102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
binary.c File Reference
#include "dttr_test_support.h"
#include <dttr_path.h>
#include <dttr_sigscan.h>
#include <Zydis/Utils.h>
#include <physfs.h>
#include <xxhash.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define DTTR_TEST_BINARY_SUPPORT

Functions

static void dttr_test_pe_free_image (DTTR_TestPEImage *image)
static bool dttr_test_pe_fixture_hash_matches (const DTTR_TestBinaryFixture *fixture, const DTTR_TestPEImage *image)
static const uint8_t * pe_rva_bytes (const DTTR_TestPEImage *image, uintptr_t rva, size_t size)
static sds dttr_test_join_path (const char *dir, const char *file)
static bool mount_fixture_dir (const char *fixture_dir, bool *out_initialized)
static void unmount_fixture_dir (const char *fixture_dir, bool initialized)
static bool read_fixture_file (const char *filename, uint8_t **out, size_t *out_size)
bool dttr_test_range_valid (size_t offset, size_t size, size_t total)
static uintptr_t signed_offset_magnitude (int32_t offset)
bool dttr_test_signed_range_valid (uintptr_t base, int32_t offset, size_t size, size_t total)
uintptr_t dttr_test_offset_site (uintptr_t base, int32_t offset)
static bool dttr_test_bytes_match_mask (const uint8_t *actual, const uint8_t *expected, const char *mask, size_t size)
bool dttr_test_case_equal (const char *a, const char *b)
bool dttr_test_fixture_required (DTTR_TestFixtureMask required, size_t fixture_index)
bool dttr_test_fixtures_available (const DTTR_TestBinaryFixture *fixtures, size_t fixture_count, const char *fixture_dir)
static bool pe_section_table (const DTTR_TestPEImage *image, size_t pe_offset, const IMAGE_NT_HEADERS32 *nt, const IMAGE_SECTION_HEADER **out_sections)
static bool pe_copy_section (DTTR_TestPEImage *image, const IMAGE_SECTION_HEADER *section)
static bool load_pe_image (uint8_t *file, size_t file_size, DTTR_TestPEImage *image)
static bool dttr_test_pe_load_fixture (const DTTR_TestBinaryFixture *fixtures, size_t fixture_count, size_t fixture_index, const char *fixture_dir, sds *out_path, DTTR_TestPEImage *out_image)
bool dttr_test_pe_for_each_fixture (const DTTR_TestBinaryFixture *fixtures, size_t fixture_count, const char *fixture_dir, DTTR_TestPEFixtureVisitor visitor, void *userdata)
static size_t masked_sigscan_count (const uint8_t *bytes, size_t size, const uint8_t *sig, const char *mask)
size_t DTTR_TestPE_SigscanCount (const DTTR_TestPEImage *image, const uint8_t *sig, const char *mask)
uintptr_t DTTR_TestPE_Sigscan (const DTTR_TestPEImage *image, const uint8_t *sig, const char *mask)
static uint64_t dttr_test_pe_file_hash (const DTTR_TestPEImage *image)
static const char * dttr_test_pe_cstr (const DTTR_TestPEImage *image, uintptr_t rva)
size_t DTTR_TestPE_CollectImports (const DTTR_TestPEImage *image, DTTR_TestImportEntry *imports, size_t imports_cap)
static ZydisDecoder * zydis_decoder32 ()
static bool dttr_test_zydis_decode32 (const uint8_t *bytes, size_t size, DTTR_TestDecodedInstruction *out)
bool dttr_test_zydis_decode32_at (const DTTR_TestPEImage *image, uintptr_t rva, DTTR_TestDecodedInstruction *out)
static bool dttr_test_zydis_decode32_prefix (const DTTR_TestPEImage *image, uintptr_t rva, size_t required_size, size_t *out_size)
static bool dttr_test_zydis_absolute_operand (const DTTR_TestDecodedInstruction *decoded, size_t operand_index, uintptr_t runtime_address, uintptr_t *out_address)
static void assert_pointer_ff25_e8_target (const DTTR_TestBinaryFixture *fixture, const DTTR_TestTargetExpectation *target, const DTTR_TestPEImage *image, uintptr_t match)
static void assert_pointer_u32_at_match_plus_2 (const DTTR_TestPEImage *image, uintptr_t match)
static void assert_byte_patch_site (const DTTR_TestBinaryFixture *fixture, const DTTR_TestTargetExpectation *target, const DTTR_TestPEImage *image, uintptr_t match)
void dttr_test_assert_target_resolved (const DTTR_TestBinaryFixture *fixture, const DTTR_TestTargetExpectation *target, const DTTR_TestPEImage *image)

Macro Definition Documentation

◆ DTTR_TEST_BINARY_SUPPORT

#define DTTR_TEST_BINARY_SUPPORT

Definition at line 1 of file binary.c.

Function Documentation

◆ assert_byte_patch_site()

void assert_byte_patch_site ( const DTTR_TestBinaryFixture * fixture,
const DTTR_TestTargetExpectation * target,
const DTTR_TestPEImage * image,
uintptr_t match )
static

◆ assert_pointer_ff25_e8_target()

void assert_pointer_ff25_e8_target ( const DTTR_TestBinaryFixture * fixture,
const DTTR_TestTargetExpectation * target,
const DTTR_TestPEImage * image,
uintptr_t match )
static

◆ assert_pointer_u32_at_match_plus_2()

void assert_pointer_u32_at_match_plus_2 ( const DTTR_TestPEImage * image,
uintptr_t match )
static

◆ dttr_test_assert_target_resolved()

void dttr_test_assert_target_resolved ( const DTTR_TestBinaryFixture * fixture,
const DTTR_TestTargetExpectation * target,
const DTTR_TestPEImage * image )

◆ dttr_test_bytes_match_mask()

bool dttr_test_bytes_match_mask ( const uint8_t * actual,
const uint8_t * expected,
const char * mask,
size_t size )
static

Definition at line 164 of file binary.c.

References size.

Referenced by assert_byte_patch_site().

◆ dttr_test_case_equal()

bool dttr_test_case_equal ( const char * a,
const char * b )

Definition at line 183 of file binary.c.

Referenced by assert_imports_for_fixture().

◆ dttr_test_fixture_required()

bool dttr_test_fixture_required ( DTTR_TestFixtureMask required,
size_t fixture_index )

◆ dttr_test_fixtures_available()

bool dttr_test_fixtures_available ( const DTTR_TestBinaryFixture * fixtures,
size_t fixture_count,
const char * fixture_dir )

Definition at line 191 of file binary.c.

References mount_fixture_dir(), and unmount_fixture_dir().

Referenced by pcdogs_fixtures_available().

◆ dttr_test_join_path()

sds dttr_test_join_path ( const char * dir,
const char * file )
static

Definition at line 34 of file binary.c.

References DTTR_Path_AppendSegment(), and NULL.

Referenced by dttr_test_pe_load_fixture().

◆ dttr_test_offset_site()

uintptr_t dttr_test_offset_site ( uintptr_t base,
int32_t offset )

Definition at line 159 of file binary.c.

References signed_offset_magnitude().

Referenced by assert_byte_patch_site(), and blueprint_function_site().

◆ dttr_test_pe_cstr()

const char * dttr_test_pe_cstr ( const DTTR_TestPEImage * image,
uintptr_t rva )
static

Definition at line 557 of file binary.c.

References NULL, and pe_rva_bytes().

Referenced by DTTR_TestPE_CollectImports().

◆ dttr_test_pe_file_hash()

uint64_t dttr_test_pe_file_hash ( const DTTR_TestPEImage * image)
static

Definition at line 537 of file binary.c.

Referenced by dttr_test_pe_fixture_hash_matches().

◆ dttr_test_pe_fixture_hash_matches()

bool dttr_test_pe_fixture_hash_matches ( const DTTR_TestBinaryFixture * fixture,
const DTTR_TestPEImage * image )
static

Definition at line 545 of file binary.c.

References dttr_test_pe_file_hash().

Referenced by dttr_test_pe_load_fixture().

◆ dttr_test_pe_for_each_fixture()

bool dttr_test_pe_for_each_fixture ( const DTTR_TestBinaryFixture * fixtures,
size_t fixture_count,
const char * fixture_dir,
DTTR_TestPEFixtureVisitor visitor,
void * userdata )

Definition at line 405 of file binary.c.

References dttr_test_pe_free_image(), dttr_test_pe_load_fixture(), and NULL.

Referenced by pcdogs_for_each_fixture().

◆ dttr_test_pe_free_image()

void dttr_test_pe_free_image ( DTTR_TestPEImage * image)
static

Definition at line 444 of file binary.c.

References free.

Referenced by dttr_test_pe_for_each_fixture(), dttr_test_pe_load_fixture(), and load_pe_image().

◆ dttr_test_pe_load_fixture()

bool dttr_test_pe_load_fixture ( const DTTR_TestBinaryFixture * fixtures,
size_t fixture_count,
size_t fixture_index,
const char * fixture_dir,
sds * out_path,
DTTR_TestPEImage * out_image )
static

◆ dttr_test_range_valid()

bool dttr_test_range_valid ( size_t offset,
size_t size,
size_t total )

◆ dttr_test_signed_range_valid()

bool dttr_test_signed_range_valid ( uintptr_t base,
int32_t offset,
size_t size,
size_t total )

◆ dttr_test_zydis_absolute_operand()

bool dttr_test_zydis_absolute_operand ( const DTTR_TestDecodedInstruction * decoded,
size_t operand_index,
uintptr_t runtime_address,
uintptr_t * out_address )
static

Definition at line 721 of file binary.c.

Referenced by assert_pointer_ff25_e8_target(), and assert_pointer_u32_at_match_plus_2().

◆ dttr_test_zydis_decode32()

bool dttr_test_zydis_decode32 ( const uint8_t * bytes,
size_t size,
DTTR_TestDecodedInstruction * out )
static

Definition at line 658 of file binary.c.

References decoder, size, and zydis_decoder32().

Referenced by dttr_test_zydis_decode32_at().

◆ dttr_test_zydis_decode32_at()

bool dttr_test_zydis_decode32_at ( const DTTR_TestPEImage * image,
uintptr_t rva,
DTTR_TestDecodedInstruction * out )

◆ dttr_test_zydis_decode32_prefix()

bool dttr_test_zydis_decode32_prefix ( const DTTR_TestPEImage * image,
uintptr_t rva,
size_t required_size,
size_t * out_size )
static

Definition at line 691 of file binary.c.

References dttr_test_zydis_decode32_at().

Referenced by dttr_test_assert_target_resolved().

◆ DTTR_TestPE_CollectImports()

size_t DTTR_TestPE_CollectImports ( const DTTR_TestPEImage * image,
DTTR_TestImportEntry * imports,
size_t imports_cap )

Definition at line 567 of file binary.c.

References count, desc, dttr_test_pe_cstr(), and pe_rva_bytes().

Referenced by pcdogs_collect_imports().

◆ DTTR_TestPE_Sigscan()

uintptr_t DTTR_TestPE_Sigscan ( const DTTR_TestPEImage * image,
const uint8_t * sig,
const char * mask )

Definition at line 524 of file binary.c.

References DTTR_Sigscan_Bytes().

Referenced by dttr_test_assert_target_resolved(), and pcdogs_sigscan().

◆ DTTR_TestPE_SigscanCount()

size_t DTTR_TestPE_SigscanCount ( const DTTR_TestPEImage * image,
const uint8_t * sig,
const char * mask )

Definition at line 512 of file binary.c.

References masked_sigscan_count().

Referenced by dttr_test_assert_target_resolved(), and pcdogs_sigscan_count().

◆ load_pe_image()

bool load_pe_image ( uint8_t * file,
size_t file_size,
DTTR_TestPEImage * image )
static

◆ masked_sigscan_count()

size_t masked_sigscan_count ( const uint8_t * bytes,
size_t size,
const uint8_t * sig,
const char * mask )
static

Definition at line 454 of file binary.c.

References size.

Referenced by DTTR_TestPE_SigscanCount().

◆ mount_fixture_dir()

bool mount_fixture_dir ( const char * fixture_dir,
bool * out_initialized )
static

Definition at line 50 of file binary.c.

References NULL.

Referenced by dttr_test_fixtures_available(), and dttr_test_pe_load_fixture().

◆ pe_copy_section()

bool pe_copy_section ( DTTR_TestPEImage * image,
const IMAGE_SECTION_HEADER * section )
static

Definition at line 259 of file binary.c.

References dttr_test_range_valid().

Referenced by load_pe_image().

◆ pe_rva_bytes()

const uint8_t * pe_rva_bytes ( const DTTR_TestPEImage * image,
uintptr_t rva,
size_t size )
static

Definition at line 22 of file binary.c.

References dttr_test_range_valid(), NULL, and size.

Referenced by dttr_test_pe_cstr(), and DTTR_TestPE_CollectImports().

◆ pe_section_table()

bool pe_section_table ( const DTTR_TestPEImage * image,
size_t pe_offset,
const IMAGE_NT_HEADERS32 * nt,
const IMAGE_SECTION_HEADER ** out_sections )
static

Definition at line 223 of file binary.c.

References dttr_test_range_valid().

Referenced by load_pe_image().

◆ read_fixture_file()

bool read_fixture_file ( const char * filename,
uint8_t ** out,
size_t * out_size )
static

Definition at line 86 of file binary.c.

References data, free, and size.

Referenced by dttr_test_pe_load_fixture().

◆ signed_offset_magnitude()

uintptr_t signed_offset_magnitude ( int32_t offset)
static

Definition at line 130 of file binary.c.

Referenced by dttr_test_offset_site(), and dttr_test_signed_range_valid().

◆ unmount_fixture_dir()

void unmount_fixture_dir ( const char * fixture_dir,
bool initialized )
static

Definition at line 75 of file binary.c.

Referenced by dttr_test_fixtures_available(), and dttr_test_pe_load_fixture().

◆ zydis_decoder32()

ZydisDecoder * zydis_decoder32 ( )
static

Definition at line 639 of file binary.c.

References decoder, and NULL.

Referenced by dttr_test_zydis_decode32().