|
From: Nicholas N. <nj...@ca...> - 2003-08-14 09:56:30
|
On Thu, 14 Aug 2003 ps...@ic... wrote:
> I would really appreciate a feature being added to detect
> non-aligned access - i.e. could the valgrind VM halt if it
> reads or writes a 2-byte value from an odd address? or
> a 4-byte value from a non-4-aligned address? (or have an
> option to give a warning?)
>
> This is possibly a good feature for others as well - it
> can ensure portability of your code to the SPARC, for
> example.
It would absolutely have to be an option, since unaligned accesses are
valid on x86. I'm not convinced how useful this would be in general.
[aside: don't feel bad, if we included every feature ever requested into
Valgrind, it would have about 100 options]
However, it's easy to hack Valgrind yourself to abort when this happens.
In memcheck/mc_main.c, look for these functions:
MC_(helperc_LOADV4)()
MC_(helperc_LOADV2)()
MC_(helperc_STOREV4)()
MC_(helperc_STOREV4)()
One of them is called for every 4/2-byte memory read/write. You can
easily add a simple alignment check on the first argument ("a").
Something like this should work, for 2-byte accesses (warning, code not
tested):
if (a & 0x1 != 0) {
VG_(pp_ExeContext)(
VG_(get_ExeContext)(
VG_(get_current_or_recent_tid()
)
);
VG_(printf)("a = %p\n", a);
VG_(core_panic)("unaligned 2-byte access");
}
This code should work on the latest release, 20030725; I'm not sure about
earlier releases as some internal details have changed.
Hope this helps.
N
|