Граф коммитов

73 Коммитов

Автор SHA1 Сообщение Дата
Brian Gianforcaro 2de2babb25
Tests: Add initial set of unit tests for Detours (#137)
- Import the Catch2 self-contained C++ test framework.
  It's used by many Microsoft OSS projects:
  - https://github.com/microsoft/cppwinrt/tree/master/test
  - https://github.com/microsoft/wil/tree/master/tests
  As well as many OSS projects in general.

  When the CMake PR is merged, we can remove this as
  a checked in development dependency, and can instead
  download it using CMake.

- Start basic set of unit tests to validate failure modes of

- Hook the execution into the existing NMake build system.

- Hook test execution into CI pipeline
2020-12-01 16:16:13 -08:00
Yogesh B d8b8144c54
Fixed#116 via changing to signed for negative diff (#146)
Addressed : Issue #116 [syelogd] Negative timing difference between successive events with option /d (delta time)
2020-09-30 15:15:06 -07:00
Brian Gianforcaro a3bdeaf006
README.md link to License file 2020-09-07 00:27:30 +00:00
sonyps5201314 73e9a0a34c
add vs solution, it is safety because it only call nmake command, and it is friendly for edit, modify and debug code in Detours (#138) 2020-09-07 00:26:00 +00:00
Jay Krell 5c919c80d2
Remove opcode fields from COPYENTRY as it isn't used, except (#17)
These are only used for a self-check, and aren't needed.
2020-09-06 22:32:02 +00:00
sonyps5201314 a9e6186a88
Fix createwith exe with path containing special characters or unicode languages (#142)
Currently we can not use createwith API to start exes which path contain special characters or unicode languages like Chinese.
This change fixes that by formatting the string path correctly.
2020-09-06 18:04:40 +00:00
sonyps5201314 969e3701c8
merge code from Detours 3.0 Build_343 (#139)
Detours 4.0.1 looks like it's Detours 3.0 build_339 changed its name by comparing the code and looking at git commit logs, so the code in follow version need to be merged.
2020-09-05 11:13:16 -07:00
lironzua 9fec98628f
Replace DWORDXX with IMAGE_THUNK_DATAXX. (#112)
Since both of the types have the same capacity in the same arch, they both work. 
But IMAGE_THUNK_DATAXX is the right struct for this code and I believe to make the code more readable and descriptive.
2020-09-05 00:25:47 +00:00
Frerich Raabe 81e6a5f796
Improved Detours logic for detection of 32bit processes (#104)
This patch improves the logic for detecting whether the process to be
patched is a 32bit or a 64bit process.

The old logic would first enumerate the modules in the process and see
if: 
    1. There is a 32bit executable module
    2. There is a 64bit DLL module

In case 1.) is true and 2.) is false, i.e. a 32bit executable but no
64bit DLL, the process was deemed to be a 32bit process.

This seems plausible, but I encountered a case in which it is not true:
I launched an IL-only .NET application (a Windows Forms GUI application) in
Windows 10. Right after the CreateProcess call, there were just two
modules in the process

- A 32bit executable
- A 32bit ntdll.dll library

I.e. the .NET runtime was not loaded yet. Hence, because there *is* a
32bit executable but there is *not* a 64bit DLL, bIs32BitProcess was set
to TRUE. However, when resuming the process and inspecting with Process
Explorer, it appears that the process executed in 64bit mode!

I suppose it would be possible to replicate the behavior of the Windows
loader and be a bit smarter about looking for 32bit executables: instead
of just looking at the 'machine' flag, also look for a potential
IMAGE_COR20_HEADER (which basically acts as the PE header for .NET
executables) and see if that requires 32bit. However, I think there is
an easier way to check if the process is 32bit or not.

The new logic performs two steps:

1. Detect whether the operating system is 64bit. If the code is compiled
as 64bit, then the OS is trivially 64bit. If the code does not have
_WIN64 defined, i.e. it is 32bit, but it is running under WOW64, then
the OS is 64bit, too.

2. Detect if the process to be patched is 32bit. If the OS is *not*
64bit, the process can't possibly be 64bit. So it must be 32bit. If the
OS *is* 64bit, we can identify 32bit processes by calling
IsWow64Process() again.
2020-09-03 20:58:03 +00:00
Jay Krell 5f674df62c
Add test case for mov reg, imm64 but it already worked, bug fixed many years ago. (#117) 2020-09-02 04:17:44 +00:00
Brian Gianforcaro 244b243d82
Build: Catch incorrect format specifiers with /we4777 and fix issues. (#129)
* Build: Catch incorrect format specifiers with /we4777 and fix issues.

Most of these are using %d for a DWORD when it should be %ld.

* More compilation fixes
2020-08-31 18:33:59 -07:00
Brian Gianforcaro 06d3cc8828
CodeQL: Enable more cpp analysis rules for Detours (#136) 2020-08-31 18:12:31 -07:00
Brian Gianforcaro 08ac104547
DetourProcessViaHelperDllsW should not call ResumeThread twice (#130)
Fixes #97 found by @svark
2020-08-31 16:16:16 -07:00
Brian Gianforcaro ea5361c377
Build: Enable /analyze in the CI build (#135) 2020-08-30 17:49:53 -07:00
Brian Gianforcaro 2c8d0ecc9b
Samples: Fix findfunc + symtest samples using incorrect dll name. (#131)
@GladYouLikeIt described the bug in the issue:

> Extend.cpp is built into extend64.dll for x64, and extend86.dll for x86,
> but the code in samples\findfunc\extend.cpp references the dll without
> specifying the DETOURS_BITS in the dll name. DetourFindFunction fails
> at runtime because of this.

@ohuseyinoglu noted the same bug in SymTest.cpp as well.

Fixes #2
2020-08-30 19:45:53 +00:00
Brian Gianforcaro 7b0f5fa57b
Add initial Issue and PR Templates to the repository (#133) 2020-08-30 19:43:52 +00:00
Brian Gianforcaro 28ad0932f2
Add CLA details to the README.md (#134) 2020-08-30 19:43:32 +00:00
Brian Gianforcaro d059c022a4
Build: Detect DETOURS_TARGET_PROCESSOR from VS Developer Command Prompt (#128)
The VS Developer command prompts sets two variables based on the configuration
which was launched. One is the architecture of the host compiler, the other is
the architecture that the compiler is targeting.

    ```
    C:\> set | rg VSCMD_ARG_
    VSCMD_ARG_HOST_ARCH=x64
    VSCMD_ARG_TGT_ARCH=x86
```

$(VSCMD_ARG_TGT_ARCH) is a direct mapping to what the user is expected to set
the $(DETOURS_TARGET_PROCESSOR) environment variable too. For cross compilation
and normal compilation the variable is always set to the value that is expected.

This change uses this to our advantage so that users won't have to manually set
the $(DETOURS_TARGET_PROCESSOR) variable in order to compile.
2020-08-30 19:42:58 +00:00
Dani Kamanovsky 8842fe68ea
Members initialized in initialization list instead of ctor body (#132)
Members should be initialized in the MIL
2020-08-28 16:40:49 +00:00
Michael Schmidt 9546ddb349
dynamic_alloc: Replace implicit type conversion with incorrect semantics by proper return value (#99)
The dynamic_alloc example fails to compile on Visual Studio 2015 due to an C4800 / C2220 compiler error (LONG status is used as return value). Apart from the performance warning, which is treated as error because of the /WX flag, the semantics of this operation seem to be wrong. At the end of the function, we find return status == NO_ERROR. That means that in line 70 a successful execution results in a return value of true and an unsuccessful execution results in false. However, if the function is left in line 52, the return code is true (because the test status != NO_ERROR was positive).

Fix this, and force /we4800 to be enabled to catch issues in the future. 

Co-authored-by: Brian Gianforcaro <bgianf@microsoft.com>
2020-08-28 07:49:13 +00:00
lironzua 25982e4919
Add documentation (#113)
Add an explanation on why we need to deal with a missing IAT.

Co-authored-by: Brian Gianforcaro <bgianf@microsoft.com>
2020-08-28 07:27:46 +00:00
lironzua 9bd7d719fc
Update createwth.cpp with enum values. (#111)
Replace CLR flags magic numbers with enum values instead.
2020-08-28 07:21:50 +00:00
pilao 277ceea11c
Add IsWow64ProcessHelper function to enable use on older Windows SDK's (#118)
I needed to compile Detours with VS 2005, which doesn't include IsWow64Process in
its Windows SDK, so added a small wrapper function around it using GetModuleHandleW 
and GetProcAddress. The wrapper will continue to call IsWow64Process directly if not X86.

Used the example on MSDN as a reference.
See: https://docs.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-iswow64process#examples
2020-08-27 06:24:23 +00:00
Brian Gianforcaro e9da64c72f
CI: Enable build of ARM / ARM64 in CI and upload artifacts (#123)
We need to set DETOURS_TARGET_PROCESSOR to tell the makefile's what
architecture we are targeting. It seems like we should eventually
fix system.mak to sniff target architecture using %VSCMD_ARG_TGT_ARCH%.

Also add support for uploading the artifacts from the pipeline so
they are available for download / testing and consumption.
2020-08-22 20:21:24 +00:00
Dani Kamanovsky 24e79fa0c3
Early return when detour_alloc_region_from_hi and lo fail (#122)
Early return in case the VirtualAlloc fails with ERROR_DYNAMIC_CODE_BLOCKED error
(aka The operation was blocked as the process prohibits dynamic code generation).
2020-08-22 03:30:42 -07:00
Jay Krell 5584029e3b
Define `_KERNEL32_` and `_USER32_` for the sake of users (#119)
providing kernel32/user32 functionality.

Mainstream users will suffer acceptably, an instruction on
some calls, which LTCG optimizes away.
2020-08-21 05:28:53 +00:00
Brian Gianforcaro c89449a124
CI: Add initial CI workflow for Detours (#121)
* Build: Fix of traceapi on Windows SDK >= 10.0.19041.0

In the 10.0.19041.0 SDK, the definitions for GetThreadLocale
and SetThreadLocale were moved under a #if(WINVER >= 0x0500)
check, where there was no such check before.

Adjust the detours build to have the same check, to avoid
consuming undeclared functions

* Build: Suppress logo (/nologo) in places where it was missed

* CI: Add initial CI workflow for Detours project

Add a simple workflow that builds x86 and x64 build architecture on push to master,
or to validate pull requests before merge.

* CI: Add codeql analysis to the build
2020-08-19 16:43:37 -07:00
Jay Krell 6449a9036a
Fix dynamic_alloc to build if UNICODE is defined. (#96)
People often copy Detours to other build environments, that
might define UNICODE by default.
2020-08-05 11:02:13 -07:00
Arjun G 40108dd085
Fix typo “Window Store apps” (#114)
There is a typo that reads “ It cannot be used by Window Store apps”. Fixed to read “...Windows Store apps”
2020-08-05 10:58:23 -07:00
Dustin Spicuzza 10ce1f4fb8
Define __in_ecount for compilers that don't have it (#107) 2020-06-30 09:32:18 -07:00
NancyLi1013 b6d9e41311
Add vcpkg installation instructions (#102) 2020-06-30 09:27:38 -07:00
zeffy 64ec135a50 Fix inconsistencies in declarations/macros and incorrect usage of ZeroMemory (#75)
* Fix incorrect usage of ZeroMemory

* Fix inconsistent declaration of DetourFindFunction

* Move DETOURS_STRINGIFY macros, and fix inconsistent reference to them

* Fix macros in detver.h
2019-12-09 10:20:48 -08:00
David Tarditi a6cf2fcbaa
Improve samples directory README.TXT based on feedback. (#61)
- Describe how to set up the build open environment (Visual Studio).
- Clarify that you should build the tests in a Developer Command
Prompt for Visual Studio. Explain that there are several different
ones.
- Note that you can run all the tests from the sample directory
using "nmake test".
2019-12-07 13:00:55 -08:00
Pedro Miguel Justo a4849d23cf Replace ARM64's patch jmp from a 16 byte encoding to a 12 byte encoding, as some functions in Windows are only 12 bytes long and have no padding. (#57)
* Replace ARM64's patch jmp from a 16 byte encoding to a 12 byte encoding, as some functions in Windows are only 12 bytes long and have no padding.

* NIB: Update comments and constants.

* Remove redundant field in _DETOUR_TRAMPOLINE. pbDetour can also be used as the island valus for theindirect jump.

* Update the _DETOUR_TRAMPOLINE's comment to describe the new encoding size and shape correctly.
2019-12-07 12:59:31 -08:00
Assaf Nativ 9d5283d939 Added .gitignore, trust me it's good for you (#66) 2019-12-07 12:52:13 -08:00
Indy Ray b60e485ae5 Fix makefile error message, using the wrong environment variable. (#84) 2019-12-07 12:50:10 -08:00
tokikuch edc8b07ae7 Add a new sample dynamic_alloc to test a new API function (#55)
Add a new sample dynamic_alloc to test the new API function DetourAllocateRegionWithinJumpBounds.
2019-06-20 00:13:15 -07:00
Jay Krell 404c153ff3 Move repetitive content to detours.h undef #ifdef DETOURS_INTERNAL. (#6) 2019-05-15 15:14:16 -07:00
Andreas Rosenberg b63f80aa2c Fix link to licence. (#52) 2019-02-22 11:45:16 -08:00
tokikuch d319d527a7 Add DetourAllocateRegionWithinJumpBounds API (#27)
This patch exports the ability to allocate a region within the jump bounds
from the given address, that was implemented as an internal function
`detour_alloc_trampoline`.  This ability would be useful for Detours' consumer.
2019-02-22 11:36:03 -08:00
Jay Krell 7e86ff5688 Remove dead code and add a comment. (#12) 2019-01-25 14:22:53 -08:00
Jay Krell d19ba159a0 ARM64 does not like addresses below 4GB -- rebase tests higher. (#9)
* ARM64 does not like addresses below 4GB -- rebase tests higher.

Port of these changes:

Change 114680 by NTDEV\jaykrell@JAYKRELL100-4 on 2017/10/20 22:24:17
        ARM64 does not like addresses below 4GB.
        Disable managed test on ARM64 until/unless anyone constructs an adequate environment.
        It is disabled on ARM32 also.
Affected files ...
... //depot/969/private/jaykrell/3.0/samples/einst/Makefile#5 edit
... //depot/969/private/jaykrell/3.0/samples/findfunc/Makefile#4 edit
... //depot/969/private/jaykrell/3.0/samples/Makefile#4 edit

Change 114788 by NTDEV\jaykrell@JAYKRELL100-4 on 2017/11/16 01:00:07
        fix copy/pasto that breaks all except arm64
Affected files ...
... //depot/969/private/jaykrell/3.0/samples/findfunc/Makefile#5 edit

* PR: Use separate variables for each .dll base address and set them each conditionally based on ARM64.
2019-01-25 14:21:29 -08:00
Jay Krell d272794f93 Support Intel EVEX (AVX512) and AMD XOP. (#15)
Port support for Intel EVEX (AVX512) and AMD XOP.

Port support for Intel EVEX (AVX512) and AMD XOP.    EVEX appeared in shipping hardware in 2017 e.g. SkyLake Xeon and has been seen in binaries.   AMD has stopped implementing XOP but the instructions are latent in binaries.
2019-01-23 14:26:49 -08:00
asmichi 3d9580164d Use DetourCreateProcessWithDllExA/W so that the traceapi sample can detour a child processe with different bitness. (#43)
This commit fixes #18.
2019-01-23 13:34:02 -08:00
NoshBar bbd7d71cd3 Minor sample fixes: (#39)
* added missing "va_end" calls in cping.cpp
* used array form of delete on array in slept.cpp
2019-01-23 13:26:57 -08:00
zeffy 89a9723fa2 Move C_ASSERT outside of struct (#31)
I moved the C_ASSERT outside of the `DETOUR_EXE_RESTORE` struct definition because it expands into a typedef (which I believe isn't valid C) and causes this compilation error if you include `detours.h` in a C program or library:

```
detours.h(390): error C2071: '__C_ASSERT__': illegal storage class
```
2019-01-23 13:24:52 -08:00
Sanghyeok Nam 523bf1324c Fix C4819 warning in non-english build environment (#41)
The '±' character can not be correctly represented in some code pages.  This causes the build to fail with a C4919 warning.  This commit replaces the '±' character in the comments with '+/-'.
2019-01-23 13:22:59 -08:00
Jay Krell c0c0ef9bff Fix ARM64 detour_skip_jmp. (#8)
Change 114679 by NTDEV\jaykrell@JAYKRELL100-4 on 2017/10/20 21:27:42
        Fix ARM64 detour_skip_jmp.
2018-08-02 18:04:27 -07:00
Jay Krell c5cb6c3af5 Expand ARM64 rbCode (and scratch) to 128 and document why. (#10)
This is a little high, but 64 seemed low.

Change 114686 by NTDEV\jaykrell@JAYKRELL100-4 on 2017/10/23 19:06:28
        Expand ARM64 rbCode (and scratch) to 128 and document why.
        This is a little high, but 64 seemed low.
2018-08-02 18:03:58 -07:00
Jay Krell dfa375637e Correct ARM64 SIZE_OF_JMP from 8 to 16 and spell out 2*4 as 8. (#11)
This is a lot of bytes to overwrite, and it precludes
patching a number of small-ish functions, but it is how many.

For example, automatic following imports, has lead to system service stubs,
that were smaller than this (and aligned less than this).
2018-08-02 17:35:58 -07:00