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

Failed to unprotect memory #51

Closed
houxinlin opened this issue Dec 11, 2023 · 5 comments
Closed

Failed to unprotect memory #51

houxinlin opened this issue Dec 11, 2023 · 5 comments

Comments

@houxinlin
Copy link

#include <funchook.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static int  (*getpid_func)();

static int getpid_hook() {
    printf("origin getpid==%d\n",getpid_func());
    return 0;
}

int main() {
    funchook_t *funchook = funchook_create();

    getpid_func = getpid;
    funchook_prepare(funchook, (void**)&getpid_func, getpid_hook);

    if (funchook_install(funchook, 0) != FUNCHOOK_ERROR_SUCCESS) {
        printf("%s\n", funchook_error_message(funchook));
    }

    printf("%d\n",getpid());
    return 0;
}

output

Failed to unprotect memory 0xffff876d7000 (size=4096, prot=read,write,exec) <- 0xffff876d76b0 (size=8, error=Invalid argument)
3358211
@kubo
Copy link
Owner

kubo commented Dec 11, 2023

@houxinlin
Thanks for the report with sample code. Could you post environment information also?
Your code works fine on my Linux box (Ubuntu 22.04 x86_64 on physical machine).

@houxinlin
Copy link
Author

@houxinlin Thanks for the report with sample code. Could you post environment information also? Your code works fine on my Linux box (Ubuntu 22.04 x86_64 on physical machine).

Thank you for getting back to me. I noticed there hasn't been much activity on your GitHub lately, so I thought you might not be maintaining this repository anymore. I'm running on an arm64 architecture with CentOS as the operating system, and I encountered this error. It works fine on x86 systems, though.

@kubo
Copy link
Owner

kubo commented Dec 12, 2023

Could you check the page size by running the following command?

$ getconf PAGE_SIZE
4096

If the output isn't 4096, could you replace PAGE_SIZE at this line with the value?

The page size of ARM64 CPU seems 4KB or 64KB according to https://www.kernel.org/doc/html/next/arch/arm64/memory.html.
It is all 4KB in my ARM64 machines. It may not be 4KB in yours.

@houxinlin
Copy link
Author

Could you check the page size by running the following command?

$ getconf PAGE_SIZE
4096

If the output isn't 4096, could you replace PAGE_SIZE at this line with the value?

The page size of ARM64 CPU seems 4KB or 64KB according to https://www.kernel.org/doc/html/next/arch/arm64/memory.html. It is all 4KB in my ARM64 machines. It may not be 4KB in yours.

After my modifications, it started functioning properly, but issues persist when the system has different PAGE_SIZE values. I attempted replacing all instances of 'page_size' with 'sysconf(_SC_PAGE_SIZE)' to dynamically fetch the size during runtime. After testing, it ran without errors, but could this approach lead to any other unexpected behaviors?

@kubo kubo closed this as completed in d5faee5 Dec 16, 2023
@kubo
Copy link
Owner

kubo commented Dec 16, 2023

but could this approach lead to any other unexpected behaviors?

No. It is okay.

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

2 participants