I am currently using Valgrind to check for memory leaks and memory corruption on dynamic memory and AddressSanitizer for memory corruption on stack memory.
But those tools are not finding memory corruptions on shared memory. Is there a good tool for this purpose ?
I am programing in C++ and I have control over the compilation process of the executable that is creating the SHM segment and the executables that are reading and writing on it.
There is so many ways you can do different things in C and C++, which may or may not violate some memory allocation, or similar.
There may be a tool out there that can catch X, Y or Z, but not W, V or U or the rest of the alphabet. Someone will add something to catch W or U to the existing tool. But there are still lots of things that it won’t catch.
Valgrind won’t work if you write your own allocator, for example (unless you teach valgrind about your allocator, which is possible but a bit of extra work).
The solution, in general, is to write code defensively. In particular, add asserts or conditional code to check for out of bounds accesses. Yes, this typically means knowing the limit, so you may have to structure your code to have this knowledge in some way (e.g. storing the size along with the pointer to a buffer, etc).
Shared memory can be particularly tricky to diagnose problems with, as it’s entirely plausible that a bug in one component appears in the other – this is really not something you can detect with a tool – since the tool doesn’t actually know what you INTEND to do. The tool won’t know what the "other side" was doing, right? So, be defensive, add asserts or conditions, e.g. if (index >= size) return invalid_value; or if (ptr >= end) exit_with_message("Out of range access…").