问题
My mips32 application run against Valgrind fails in mmap function. It works fine if I run separately but when I run it against valgrind it fails with EINVAL each time.
void * mem = (uint32_t *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, <fd>,
mmap_size);
回答1:
When the client application runs against the Valgrind, the Valgrind intercepts the mmap call made by the client. It then invokes the kernel's mmap function by setting the MAP_FIXED flag and also specifies the memory location to map. The Kernel will then try to map to this advised memory. When the advised location is not available for mapping, the Kernel returns failure (EINVAL) because the MAP_FIXED flag is set. The above function call will get translated into,
void * mem = (uint32_t *)mmap( advised_memory, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, <fd>,
mmap_size);
You can modify the Valgrind's mmap function to fix this behaviour. Remove the MAP_FIXED flag and retry mmap without specifying the advised memory address.
--- .coregrind/m_syswrap/syswrap-mips32-linux.c 2014-09-08 13:28:45.000000000 +0100
+++ coregrind/m_syswrap/syswrap-mips32-linux.c 2014-11-19 12:12:43.000000000 +0000
@@ -452,10 +452,11 @@
/* Otherwise we're OK (so far). Install aspacem's choice of
address, and let the mmap go through. */
sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3,
- arg4 | VKI_MAP_FIXED,
+ arg4,
arg5, arg6);
- /* A refinement: it may be that the kernel refused aspacem's choice
+
+ /* A refinement: it may be that the kernel refused aspacem's choice
of address. If we were originally asked for a hinted mapping,
there is still a last chance: try again at any address.
Hence: */
@@ -470,10 +471,20 @@
}
/* and try again with the kernel */
sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3,
- arg4 | VKI_MAP_FIXED,
+ arg4,
+ arg5, arg6);
+
+ if( sr_isError(sres) )
+ sres = VG_(am_do_mmap_NO_NOTIFY)(NULL, arg2, arg3,
+ arg4,
arg5, arg6);
}
+ if( sr_isError(sres) )
+ sres = VG_(am_do_mmap_NO_NOTIFY)(NULL, arg2, arg3,
+ arg4,
+ arg5, arg6);
+
if (!sr_isError(sres)) {
ULong di_handle;
/* Notify aspacem. */
来源:https://stackoverflow.com/questions/27382653/mmap-return-einval-when-run-with-valgrind