AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   [EXTENSION] Midhooks (https://forums.alliedmods.net/showthread.php?t=343973)

Scag 09-19-2023 23:51

[EXTENSION] Midhooks
 
Hi. I wrote an extension that exposes midfunc/inline hooks for SourceMod.

Sometimes, you have a function you want to hook. And sometimes, the logic you want to fiddle with is way far down under deep in there, and it would be really, really hard to work to get what you want with just a hook. Sometimes, you can recreate the function yourself, but that might not work if it's super big and complicated. Sometimes, you might be able to patch over it, but if you want to execute more logic, then that also might not work. In those some times, you may need to deploy a midfunc hook.

By hand, it isn't very fun, you have to patch in a jump that goes somewhere that contains your own assembly, and then that assembly jump back when you're done. That stinks even more if you need to execute some bonus code.

And thus, that's why I wrote this extension. Here's how it works.

How it works and a test case:
Spoiler


SOURCE
RELEASES

fdxx 09-22-2023 11:43

Re: [EXTENSION] Midhooks
 
1 Attachment(s)
Server crashes after trying, what am I doing wrong?

PHP Code:

/*
"Games"
{
    "left4dead2"
    {
        "Addresses"
        {
            "CLunge::IsAbilityReadyToFire"
            {
                "linux"
                {
                    "signature"    "CLunge::IsAbilityReadyToFire"
                    "offset"    "0xC8"
                }
            }
        }

        "Signatures"
        {
            "CLunge::IsAbilityReadyToFire"
            {
                "library"    "server"
                "linux"        "@_ZNK6CLunge20IsAbilityReadyToFireEv"
            }
        }
    }
}
*/

#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>
#include <midhook>

public void OnPluginStart()
{
    
GameData hGameData = new GameData("midhook_test");

    
Address addr hGameData.GetAddress("CLunge::IsAbilityReadyToFire");
    
MidHook midhook = new MidHook(addrOnMidHook);
    if (!
midhook)
        
SetFailState("Failed to create midhook");
    
delete hGameData;
}

void OnMidHook(MidHookRegisters regs)
{
    
PrintToServer("--- OnMidHook ---"); // Server crashes immediately after printing



Scag 09-22-2023 12:03

Re: [EXTENSION] Midhooks
 
Quote:

Originally Posted by fdxx (Post 2810408)
Spoiler

Please provide an Accelerator crash dump if possible. At a glance, it appears that short jumps (the 74 0B) cannot be fixed up in the trampoline of a midhook. Try hooking somewhere that doesn't have a short jump within ~5 bytes of the target address. If that's the problem, then that is a limitation of the API but something I could consider working on in the future.

fdxx 09-22-2023 21:27

Re: [EXTENSION] Midhooks
 
1 Attachment(s)
Quote:

Originally Posted by Scag (Post 2810411)
Please provide an Accelerator crash dump if possible. At a glance, it appears that short jumps (the 74 0B) cannot be fixed up in the trampoline of a midhook. Try hooking somewhere that doesn't have a short jump within ~5 bytes of the target address. If that's the problem, then that is a limitation of the API but something I could consider working on in the future.

https://crash.limetech.org/pw6nmzpo53dp

Scag 09-22-2023 22:38

Re: [EXTENSION] Midhooks
 
Quote:

Originally Posted by fdxx (Post 2810425)

Yes, looks like that's exactly what happened.

Quote:

Originally Posted by Scag
At a glance, it appears that short jumps (the 74 0B) cannot be fixed up in the trampoline of a midhook.

The disassembly in the crash dump provides the tail end bytes of the following movzx instruction. The jz was fortunately short.

Anyways, for now, try to avoid emplacing midhooks where there are short (2-3 byte-long) jmp instructions that the midhook will run over. E8 and E9 jumps should be okay. I can work on getting short jumps to remap/work but that will take some finesse. I'll update the main post to reflect that fact.


All times are GMT -4. The time now is 14:26.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.