0

Why does the following instruction produce a bus error on armv8?

sturb wzr, [sp, #0]

Bus error (core dumped)

Minimal example:

.text
.global _start

_start:
    sub sp, sp, #1
    sturb wzr, [sp, #0]

    mov x0, #0
    mov x8, #93
    svc 0
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • you have confirmed it is the sturb and not the unaligned stack pointer? – old_timer Jun 18 '21 at 18:01
  • Yes, but why does it fail with x31 but not with, say, x1 – Moss Richardson Jun 18 '21 at 18:05
  • 5
    ` sub sp, sp, #1` - this misaligns the stack. The processor rules are that the bottom two bits of `sp` are "reserved must be zero". You have broken the rules and UNPREDICTABLE behavior is your punishment. – Raymond Chen Jun 18 '21 at 18:18
  • 1
    That makes sense! Thanks! – Moss Richardson Jun 18 '21 at 18:22
  • 2
    @RaymondChen: I'm not seeing that as an architectural requirement on ARM64 (could it be from ARM32 instead?), but what is true is that the OS can enable SP alignment checking, where the bottom *four* bits of `sp` must be zero on any `sp`-relative load or store. And AFAIK most OSes enable this. So 16-byte alignment is required. The result of a violation isn't UNPREDICTABLE but rather an exception, triggering the OS to deliver SIGBUS. – Nate Eldredge Jun 18 '21 at 19:00
  • @NateEldredge Hi, Nate! You're right, I was looking at the wrong manual (armv8m = arm32). On arm32, sp[0:1] is marked as res0. – Raymond Chen Jun 18 '21 at 20:09

0 Answers0