Skip to content

Support to discard valgrind translation cache #20

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

Merged
merged 2 commits into from
Jan 3, 2024

Conversation

qdkevinkou
Copy link
Contributor

The original function can't reenter after stub.reset when running the compiled elf with valgrind, the reason is valgrind has translated the function code after stub and saved to its code translation cache, and valgrind didn't re-translate the code after stub.reset. This code change is to support discarding valgrind code translate cache by valgrind api VALGRIND_DISCARD_TRANSLATIONS and re-translate the code after calling stub.reset, and we added self-defined macro VALGRIND to enable this feature when needed(need to provide valgrind-devel to include valgrind/valgrind.h).

e.g.

void foo()
{
printf("I am foo\n");
}

void foo_stub1()
{
printf("I am foo_stub1\n");
}

Stub stub;

void test_foo()
{
foo();
}

int main()
{
stub.set(foo, foo_stub1);
test_foo();
stub.reset(foo);
test_foo();
}

You will get below output which shows foo_stub1 still called after "stub.reset(foo);".

[root@build-vm-01 test]# /root/github/valgrind/bin/valgrind --vgdb=no ./test_valgrind_discard_translation_linux
==25196== Memcheck, a memory error detector
==25196== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25196== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==25196== Command: ./test_valgrind_discard_translation_linux
==25196==
I am foo_stub1
I am foo_stub1
==25196==
==25196== HEAP SUMMARY:
==25196== in use at exit: 0 bytes in 0 blocks
==25196== total heap usage: 2 allocs, 2 frees, 44 bytes allocated
==25196==
==25196== All heap blocks were freed -- no leaks are possible
==25196==
==25196== For lists of detected and suppressed errors, rerun with: -s
==25196== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

@qdkevinkou qdkevinkou marked this pull request as ready for review December 16, 2021 07:30
@coolxv
Copy link
Owner

coolxv commented Dec 20, 2021

@qdkevinkou
Why is the set function not modified?

@qdkevinkou
Copy link
Contributor Author

qdkevinkou commented Dec 21, 2021

@coolxv , set function also needs this operation, and I add the unified Macro VALGRIND_CACHE_FLUSH, and reworks the solution. Valgrind supports ARM/ARM64, X86/X86-64, MIPS/MIPS64 platforms, so I added VALGRIND_CACHE_FLUSH to REPLACE_FAR for these #if branches.

@qdkevinkou
Copy link
Contributor Author

Hi @coolxv , Any other comment?

@coolxv coolxv merged commit 35280a2 into coolxv:master Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants