Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detours crash on DetourAttach #145

Open
luca2125 opened this issue Sep 14, 2020 · 8 comments
Open

detours crash on DetourAttach #145

luca2125 opened this issue Sep 14, 2020 · 8 comments
Labels
bug Something isn't working

Comments

@luca2125
Copy link

Hi,

I have used detours on 2 projects. In the first project work fine.

The second project crash always when I use DetourAttach:

// This row cause crash
DetourAttach(&(LPVOID&)AddressOfHookDamageFunction, &HookDamageSub);

The second project is a 90% copy of the first project.

AddressOfHookDamageFunction = 0x72DD81 (this is stored in (ffback.ini)

Becouse is very hard understand why this happen. I attach the entere project:
BattleZone CC.zip

just open:

dllmain.cpp

goto "Init_Detour" and you find the line with DetourAttach.

To be honest I have no idea about this situation and I not sure if this is a bug,

Can you please verify ?

@luca2125 luca2125 added the bug Something isn't working label Sep 14, 2020
@luca2125
Copy link
Author

luca2125 commented Sep 15, 2020

HI,

I have solved the problem.

I have added:

Sleep(1000);

before call: DetourTransactionBegin();

However I suggest to manage the error (ex.. "Target Application not ready") becouse when a application crash is very hard to find the cause becouse can depend by too much factors.

@sonyps5201314
Copy link
Contributor

This problem will be automatically fixed, when PR 144 is merged, because it provides a new extension API:
DetourTransactionBeginEx (BOOL fWait), it supports waiting for other Detour transactions to complete, but currently in order to maintain compatibility, DetourTransactionBegin only calls DetourTransactionBeginEx with the FALSE parameter , as below code:

LONG WINAPI DetourTransactionBegin()
{
     return DetourTransactionBeginEx(FALSE);
}

Perhaps the official can consider adding a fWait parameter to DetourTransactionBegin directly, as shown below:
LONG WINAPI DetourTransactionBegin(_In_opt_ BOOL fWait) .
Due to the suggestion in pull_request_template.md:

These changes introduce no known API breaks (changing the public facing
functions return types, function parameters, renaming functions, etc.).

I didn't do that, but the official can really consider adding a parameter for stability.

@luca2125
Copy link
Author

Thank you !

I have another question:

Sometime when detour call the funciton crash and now always is easy to find the cause.

I suspect that IDA not always return the correct number of parameters or types, but the crash can also depend by other factiors.

My question is there is a way to add some managaed error exception ? For example eneble some log file that write the error ?

If no, can be a good idea if is possible do it in the future.

Thanks !

@sonyps5201314
Copy link
Contributor

sonyps5201314 commented Jan 28, 2021

You can define the DETOUR_DEBUG macro to enable DETOUR_TRACE and DETOUR_BREAK to indicate exceptions of Detours Internals. In the future, DETOUR_ASSERT/Detour_Assert can indicate more exceptions of Detours Internals, regardless of whether the DETOUR_DEBUG macro is defined.

@luca2125
Copy link
Author

thank you ! there is a sample sorcce code that use DETOUR_DEBUG / DETOUR_TRACE ? I like to see it.

@sonyps5201314
Copy link
Contributor

sonyps5201314 commented Jan 28, 2021

create a c++ source file named DTRS.cpp with below code:

#define DETOUR_DEBUG

#undef _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE
typedef long NTSTATUS;
#include <ntstatus.h>
#define WIN32_NO_STATUS
#define DETOURS_INTERNAL
#include "detours.h"
#include "detours.cpp"
#include "disasm.cpp"
#include "image.cpp"
#include "modules.cpp"
#include "creatwth.cpp"

include this file to your Detours sample project without precompiled header (.pch) file for this source file, then
you will see your want.

@luca2125
Copy link
Author

thanks ! if I do it what happen when there is an detour error ?

@sonyps5201314
Copy link
Contributor

Output a message to visual studio debug output window or break at the place of fatal error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants