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

HEAP CORRUPTION DETECTED #779

Open
lauthrul opened this issue May 25, 2023 · 1 comment
Open

HEAP CORRUPTION DETECTED #779

lauthrul opened this issue May 25, 2023 · 1 comment

Comments

@lauthrul
Copy link

System Details

Platform: Windows 11 10.0.22000 64bit
Version: libffi_3.4.4_x64

Problems Description

I'm using 64bit version of libffi, code as below, when run at delete c, there occurated a fatal error:

"HEAP CORRUPTION DETECTED: after Normal block (#1906) at 0x000001BFF6AA5650.
CRT detected that the application wrote to memory after end of heap buffer."

I checked the memory, before ffi_call() peformed, the memory of variable c is: ff ff ff ff, fd fd fd fd, and after that it is: 03 00 00 00, 00 00 00 00. As int is 4 byte, so, it should be expected to 03 00 00 00, fd fd fd fd. It seems like that ffi_call write 8 byte to it. Is this libffi's bug or did I used it in a wrong way?

void test0()
{
    ffi_cif cif;
    ffi_arg ret;
    ffi_type* argTypes[2] = { &ffi_type_sint, &ffi_type_sint };
    int* a = new int(1);
    int* b = new int(2);
    int* c = new int; // "cd cd cd cd, fd fd fd fd"
    int* d = new int(-1);
    void* args[2] = { a, b };

    auto func = lib.resolve("?Add@@YAHHH@Z"); // function is: int Add(int a, int b);

    auto status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_sint, argTypes);
    assert(status == FFI_OK);

    *c = 10; // "0a 00 00 00, fd fd fd fd"
    memcpy(c, d, sizeof(d)); // "ff ff ff ff, fd fd fd fd"

    ffi_call(&cif, FFI_FN(func), c, args);  // "03 00 00 00, 00 00 00 00", HEAP CORRUPTION here!
    assert(*c == 3);

    delete a;
    delete b;
    delete c; // HEAP CORRUPTION DETECTED
    delete d;
}
@atgreen
Copy link
Member

atgreen commented May 25, 2023 via email

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