Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,17 +785,19 @@ PBYTE CDetourDis::CopyVexEvexCommon(BYTE m, PBYTE pbDst, PBYTE pbSrc, BYTE p, BY

// see https://software.intel.com/content/www/us/en/develop/download/intel-avx512-fp16-architecture-specification.html
// EVEX maps (with FP16 and APX mmm-bit extension):
// mmm=001 (MAP1) and mmm=101 (MAP5) share the legacy 0F opcode structure,
// so per-opcode sizing must come from s_rceCopyTable0F (e.g. MAP5 C2 /r ib = VCMPPH/VCMPSH).
// mmm=010 (MAP2) and mmm=110 (MAP6) share the legacy 0F 38 structure (ModR/M, no imm).
// mmm=001 (MAP1) shares the legacy 0F opcode structure,
// so per-opcode sizing must come from s_rceCopyTable0F.
// mmm=010 (MAP2) shares the legacy 0F 38 structure (ModR/M, no imm).
// mmm=101 (MAP5) and mmm=110 (MAP6) are FP16 maps where every opcode is
// ModR/M with no imm (VCMPPH/VCMPSH live in MAP3, not MAP5).
// mmm=011 (MAP3) has the 0F 3A structure (ModR/M + imm8).
// mmm=100 (MAP4, APX) mirrors legacy MAP0 opcode structure (per-opcode sizing).
switch (m | fp16) {
default: return Invalid(&ceInvalid, pbDst, pbSrc);
case 5: // MAP5 (FP16) - opcode structure mirrors 0F (MAP1).
case 1: pEntry = &s_rceCopyTable0F[pbSrc[0]];
return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
case 6: // MAP6 (FP16) - opcode structure mirrors 0F 38 (MAP2).
case 5: // MAP5 (FP16) - ModR/M, no immediate.
case 6: // MAP6 (FP16) - ModR/M, no immediate.
case 2: return CopyBytes(&ceF38, pbDst, pbSrc);
case 3: return CopyBytes(&ceF3A, pbDst, pbSrc);
case 4: // MAP4 (APX) - promoted legacy MAP0 instructions.
Expand Down
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ dirs:
$(OBJD)\main.obj : main.cpp
$(OBJD)\test_module_api.obj : test_module_api.cpp
$(OBJD)\test_image_api.obj : test_image_api.cpp
$(OBJD)\test_disasm_api.obj : test_disasm_api.cpp
$(OBJD)\corruptor.obj : corruptor.cpp
$(OBJD)\process_helpers.obj : process_helpers.cpp
$(OBJD)\payload.obj : payload.cpp

$(BIND)\unittests.exe : $(OBJD)\main.obj \
$(OBJD)\test_module_api.obj \
$(OBJD)\test_image_api.obj \
$(OBJD)\test_disasm_api.obj \
$(OBJD)\corruptor.obj \
$(OBJD)\process_helpers.obj \
$(OBJD)\payload.obj $(DEPS)
cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb \
$(OBJD)\main.obj \
$(OBJD)\test_module_api.obj \
$(OBJD)\test_image_api.obj \
$(OBJD)\test_disasm_api.obj \
$(OBJD)\corruptor.obj \
$(OBJD)\process_helpers.obj \
$(OBJD)\payload.obj \
Expand Down
86 changes: 86 additions & 0 deletions tests/test_disasm_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//////////////////////////////////////////////////////////////////////////////
//
// Unit Tests for Detours Disassembly API (test_disasm_api.cpp of unittests.exe)
//
// Microsoft Research Detours Package
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
#include "catch.hpp"
#include "windows.h"

#define DETOURS_INTERNAL

#include "detours.h"

#include <string.h>

#if defined(DETOURS_X86) || defined(DETOURS_X64)

// Copy an instruction into a padded source buffer and verify the length
// the disassembler reports and the bytes it produces.
//
static void
CheckInstructionCopy(
_In_reads_bytes_(cbCode) const BYTE *pbCode,
_In_ DWORD cbCode,
_In_ DWORD cbExpected
)
{
BYTE pbSrc[32];
BYTE pbDst[32];
PVOID pTarget = NULL;
LONG lExtra = 0;

memset(pbSrc, 0x90, sizeof(pbSrc));
memset(pbDst, 0xCC, sizeof(pbDst));
memcpy(pbSrc, pbCode, cbCode);

PVOID pNext = DetourCopyInstruction(pbDst, NULL, pbSrc, &pTarget, &lExtra);

REQUIRE( pNext == pbSrc + cbExpected );
REQUIRE( memcmp(pbDst, pbCode, cbExpected) == 0 );
REQUIRE( lExtra == 0 );
}

TEST_CASE("DetourCopyInstruction", "[disasm]")
{
SECTION("VCVTUDQ2PH (EVEX MAP5 F2 7A)")
{
static const BYTE rbCode[] = { 0x62, 0xF5, 0x7F, 0x48, 0x7A, 0xC0 };
CheckInstructionCopy(rbCode, (DWORD)sizeof(rbCode), 6);
}

SECTION("VCVTPH2QQ (EVEX MAP5 66 7B)")
{
static const BYTE rbCode[] = { 0x62, 0xF5, 0x7D, 0x48, 0x7B, 0xC0 };
CheckInstructionCopy(rbCode, (DWORD)sizeof(rbCode), 6);
}

SECTION("VCVTTPH2UQQ (EVEX MAP5 66 78)")
{
static const BYTE rbCode[] = { 0x62, 0xF5, 0x7D, 0x48, 0x78, 0xC1 };
CheckInstructionCopy(rbCode, (DWORD)sizeof(rbCode), 6);
}

SECTION("VADDPH (EVEX MAP5 58)")
{
static const BYTE rbCode[] = { 0x62, 0xF5, 0x7C, 0x48, 0x58, 0xC1 };
CheckInstructionCopy(rbCode, (DWORD)sizeof(rbCode), 6);
}

SECTION("VCVTUDQ2PH with disp32 (EVEX MAP5 F2 7A)")
{
static const BYTE rbCode[] = { 0x62, 0xF5, 0x7F, 0x48, 0x7A, 0x80,
0x01, 0x02, 0x03, 0x04 };
CheckInstructionCopy(rbCode, (DWORD)sizeof(rbCode), 10);
}

SECTION("VCMPPH (EVEX MAP3 C2 ib)")
{
static const BYTE rbCode[] = { 0x62, 0xF3, 0x7C, 0x48, 0xC2, 0xC1, 0x05 };
CheckInstructionCopy(rbCode, (DWORD)sizeof(rbCode), 7);
}
}

#endif // DETOURS_X86 || DETOURS_X64