summaryrefslogtreecommitdiff
path: root/tests/basic_jmp.c
blob: d65b340431bb19f1235ab208503e2743068bb920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * @file basic_jmp.c
 * @brief cdl86 asm jmp patch unit test
 *
 * cdl86
 * Experimental Linux/Windows x86/x86_64 detours library.
 * Author: Dylan Müller
 *
 * +---------------------------------------+
 * |     .-.       .-.       .-.           |
 * |    /   \     /   \     /   \     +    |
 * |         \   /     \   /     \   /     |
 * |          "_"       "_"       "_"      |
 * |                                       |
 * |  _   _   _ _  _   _   ___   ___ _  _  |
 * | | | | | | | \| | /_\ | _ \ / __| || | |
 * | | |_| |_| | .` |/ _ \|   /_\__ \ __ | |
 * | |____\___/|_|\_/_/ \_\_|_(_)___/_||_| |
 * |                                       |
 * |                                       |
 * | Lunar RF Labs                         |
 * | https://lunar.sh                      |
 * |                                       |
 * | Research Laboratories                 |
 * | OpenAlias (BTC, XMR): lunar.sh        |
 * | Copyright (C) 2022-2024               |
 * +---------------------------------------+
 */

#include <stdio.h>

#include "cdl.h"

typedef int add_t(
    __in int x,
    __in int y
);

add_t* addo = NULL;

int add(
    __in int x,
    __in int y
)
{
    printf("Inside original function\n");
    return x + y;
}

int add_detour(
    __in int x,
    __in int y
)
{
    printf("Inside detour function\n");
    return addo(5,5);
}

int main(
    __in void
)
{
    struct cdl_jmp_patch jmp_patch = {};
    addo = (add_t*)add;

    printf("Before attach: \n");
    printf("add(1,1) = %i\n\n", add(1,1));

    jmp_patch = cdl_jmp_attach((void**)&addo, add_detour);
    if(jmp_patch.active)
    {
        printf("After attach: \n");
        printf("add(1,1) = %i\n\n", add(1,1));
        printf("== DEBUG INFO ==\n");
        cdl_jmp_dbg(&jmp_patch);
    }

    cdl_jmp_detach(&jmp_patch);
    printf("\nAfter detach: \n");
    printf("add(1,1) = %i\n\n", add(1,1));

    return 0;
}