Re: [PATCH] kcsan:fix alignment_fault when read unaligned instrumented memory

From: Marco Elver
Date: Wed Mar 08 2023 - 05:33:51 EST


On Wed, Mar 08, 2023 at 05:41PM +0800, Haibo Li wrote:
[...]
> > > x2 : 0005ff802a0d8d71 x1 : 0000000000000000 x0 : 0000000000000000 Call
> > > trace:
> > > kcsan_setup_watchpoint+0x26c/0x6bc
> > > __tsan_read2+0x1f0/0x234
> > > inflate_fast+0x498/0x750
> >
> > ^^ is it possible that an access in "inflate_fast" is unaligned?
> Here is the instruction for inflate_fast+0x498:
> ffffffc008948980 <inflate_fast>:
> ...
> ffffffc008948e10: e0 03 1c aa mov x0, x28
> ffffffc008948e14: 06 3a e9 97 bl 0xffffffc00839762c <__tsan_unaligned_read2>
> ffffffc008948e18: e0 03 17 aa mov x0, x23
> >ffffffc008948e1c: 9a 27 40 78 ldrh w26, [x28], #2
>
> And the instruction for kcsan_setup_watchpoint+0x26c:
> ffffffc00839ab90 <kcsan_setup_watchpoint>:
> ...
> >ffffffc00839adfc: a8 fe df 48 ldarh w8, [x21]
>
> The instruction is different.READ_ONCE uses ldarh,which requires the access address is aligned.
> As ARM v8 arm said:
> "
> Load-Acquire, Load-AcquirePC and Store-Release, other than Load-Acquire Exclusive Pair and
> Store-Release-Exclusive Pair, access only a single data element. This access is single-copy atomic. The address of the data object must be aligned to the size of the data element being accessed, otherwise the access generates an
> Alignment fault."
>
> while ldrh accepts unaligned address.
> That's why it is ok while disable KCSAN.

I understand now what's going on, thanks for the analysis.

Can you test the below patch, I think it is the correct solution for
this - compared to your approach of opting out unaligned accesses, with
the below there is no loss of functionality.

Thanks,
-- Marco

------ >8 ------