sustrik / libdill

Structured concurrency in C
MIT License
1.68k stars 156 forks source link

Force alignment in ARM architectures #207

Closed jcarrano closed 3 years ago

jcarrano commented 3 years ago

Description

The "opaque but sized" structures (dill_*_storage) may cause data (in particular pointers) not to be aligned. This is not a problem in x86_64, but in ARM it causes loads and stores to be unpredictable (i.e. store wrong data). Then the code will try to invoke a function from a wrong pointer and jumps to nowhere, causing a segfault.

Longer description

The struct dill_suffix_storage suffix_mem; member in dill_http_sock is not aligned. Thus, the dill_hvfs in the suffix socket is not aligned either. dill_suffix_attach_mem will try to set self->hvfs.query and close but because these words are not aligned, the pointers have garbage written (and read). Then dill_hquery will try to invoke this pointers and jump to nowhere.

The size increase caused by alignment is compensated by the smaller pointer sizes in ARM, so the DILL_CHECK_STORAGE() still pass without modifying the sizes.

Testing

Please tell me if you need a short test program that shows the crash.

raveslave commented 3 years ago

what mcu are you running?

jcarrano commented 3 years ago

@raveslave I'm on a AT91SAM9260, that's ARM926EJ. It is somewhat old but still in production.