cpp-stub icon indicating copy to clipboard operation
cpp-stub copied to clipboard

Support to discard valgrind translation cache

Open qdkevinkou opened this issue 3 years ago • 3 comments

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 avatar Dec 16 '21 07:12 qdkevinkou

@qdkevinkou Why is the set function not modified?

coolxv avatar Dec 20 '21 11:12 coolxv

@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 avatar Dec 21 '21 12:12 qdkevinkou

Hi @coolxv , Any other comment?

qdkevinkou avatar Dec 28 '21 15:12 qdkevinkou