Support to discard valgrind translation cache #20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)