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

hook the main function #22

Open
milahu opened this issue Sep 29, 2021 · 0 comments
Open

hook the main function #22

milahu opened this issue Sep 29, 2021 · 0 comments

Comments

@milahu
Copy link

milahu commented Sep 29, 2021

this has no effect

hook! {
    unsafe fn main(
        argc: libc::c_int,
        argv: *const *const libc::c_char,
        envp: *const *const libc::c_char
    ) -> libc::c_int => my_main {

        println!("my_main");
        let exit_code = real!(main)(argc, argv, envp);
        return exit_code;
    }
}

this fails to compile. when i remove the hook!, then it compiles, but has no effect

hook! {
    unsafe fn __libc_start_main(
        _main: &dyn Fn(libc::c_int, *const *const libc::c_char, *const *const libc::c_char) -> libc::c_int,
        argc: libc::c_int,
        argv: *const *const libc::c_char,
        init: &dyn Fn(libc::c_int, *const *const libc::c_char, *const *const libc::c_char) -> libc::c_int,
        fini: &dyn Fn() -> libc::c_void,
        rtld_fini: &dyn Fn() -> libc::c_void,
        stack_end: *const libc::c_void
    ) -> libc::c_int => my___libc_start_main {

        println!("my___libc_start_main");
        let exit_code = real!(__libc_start_main)(_main, argc, argv, init, fini, rtld_fini, stack_end);
        return exit_code;
    }
}

error[E0277]: the type `dyn Fn(i32, *const *const i8, *const *const i8) -> i32` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary

error[E0277]: the type `dyn Fn() -> c_void` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary

a working example in C code

taken from abba23/spotify-adblock-linux

typedef int (*main_t)(int, char **, char **);
static main_t real_main;

int wrap_main(int argc, char **argv, char **envp) {
    printf("wrap_main\n");
    int main_res = real_main(argc, argv, envp);
    return main_res;
}

// wrap __libc_start_main: replace real_main with wrap_main
int __libc_start_main(
    main_t main, int argc, char **argv,
    main_t init,
    void (*fini)(void), void (*rtld_fini)(void), void *stack_end
) {
    static int (*real___libc_start_main)() = NULL;
    if (!real___libc_start_main) {
        printf("real___libc_start_main = %p (empty)\n", real___libc_start_main);
        char *error;
        real___libc_start_main = dlsym(RTLD_NEXT, "__libc_start_main");
        printf("real___libc_start_main = %p\n", real___libc_start_main);
        if ((error = dlerror()) != NULL) {
            printf("%s\n", error);
            exit(1);
        }
    }
    real_main = main;
    return real___libc_start_main(wrap_main, argc, argv, init, fini, rtld_fini, stack_end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant