Skip to content

spinlock alignement issue in aarch64 causing so3 to crash #141

@AndreCostaaa

Description

@AndreCostaaa

Encountered this bug when using the virt64 config where so3 would suddenly crash when starting a new process.

After some digging, I found that the crash happens when we try to lock a mutex. Basically, whenever the new process would call printf, that causes a mutex lock which in turn tries to lock a spinlock that is misaligned :

In user space:

printf -> vprintf -> FLOCK -> __lockfile -> mutex_lock -> sys_mutex_lock(1)

When we get to kernel space:

do_mutex_lock -> mutex_lock -> spin_lock_irqsave -> spin_trylock

And then so3 would crash:

Image

Taking a look at what ESR 0x96000061 :

Image

We see that the problem is with the alignment of a memory write which must mean the call "stxr %w0, %2, [%1]\n" here:

static inline int spin_trylock(spinlock_t *lock)
{
uint32_t tmp;
__asm__ __volatile__(" ldaxr %w0, [%1]\n"
" tbnz %w0, #0, 1f\n"
" stxr %w0, %2, [%1]\n"
"1:"
: "=&r"(tmp)
: "r"(&lock->lock), "r"(1)
: "cc");

So after adding a test to check the memory alignment of the spinlock inside the mutex_init function i found this :

Image

So basically, there's a memory misalignment with the new spinlocks created when creating a new process. I stopped investigating here and i'm still not sure why this is happening considering the fact that we're telling the compiler to align the spinlocks:

/*
* On Aarch64, the field has to be 64-bit aligned apparently.
*/
typedef struct {
__attribute__((aligned(8))) volatile uint32_t lock;
} spinlock_t;

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions