Search code examples
valgrindarm64

Valgrind reports "invalid write" at "X bytes below stack pointer"


I'm running some code under Valgrind, compiled with gcc 7.5 targeting an aarch64 (ARM 64 bits) architecture, with optimizations enabled.

I get the following error:

==3580== Invalid write of size 8
==3580==    at 0x38865C: ??? (in ...)
==3580==  Address 0x1ffeffdb70 is on thread 1's stack
==3580==  16 bytes below stack pointer

This is the assembly dump in the vicinity of the offending code:

  388640:       a9bd7bfd        stp     x29, x30, [sp, #-48]!
  388644:       f9000bfc        str     x28, [sp, #16]
  388648:       a9024ff4        stp     x20, x19, [sp, #32]
  38864c:       910003fd        mov     x29, sp
  388650:       d1400bff        sub     sp, sp, #0x2, lsl #12
  388654:       90fff3f4        adrp    x20, 204000 <_IO_stdin_used-0x4f0>
  388658:       3dc2a280        ldr     q0, [x20, #2688]
  38865c:       3c9f0fe0        str     q0, [sp, #-16]!

I'm trying to ascertain whether this is a possible bug in my code (note that I've thoroughly reviewed my code and I'm fairly confident it's correct), or whether Valgrind will blindly report any writes below the stack pointer as an error.

Assuming the latter, it looks like a Valgrind bug since the offending instruction at 0x38865c uses the pre-decrement addressing mode, so it's not actually writing below the stack pointer.

Furthermore, at address 0x388640 a similar access (and again with pre-decrement addressing mode) is performed, yet this isn't reported by Valgrind; the main difference being the use of an x register at address 0x388640 versus a q register at address 38865c.

I'd also like to draw attention to the large stack pointer subtraction at 0x388650, which may or may not have anything to do with the issue (note this subtraction makes sense, given that the offending C code declares a large array on the stack).

So, will anyone help me make sense of this, and whether I should worry about my code?


Solution

  • The code looks fine, and the write is certainly not below the stack pointer. The message seems to be a valgrind bug, possibly #432552, which is marked as fixed. OP confirms that the message is not produced after upgrading valgrind to 3.17.0.