Re: [PATCH v5] rust: alloc: satisfy POSIX alignment requirement

From: Tamir Duberstein
Date: Wed Feb 12 2025 - 16:24:53 EST


On Wed, Feb 12, 2025 at 3:58 PM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
>
> On Wed, Feb 12, 2025 at 03:47:11PM -0500, Tamir Duberstein wrote:
> > Looks like I wasn't the only one to fall into the trap (rust/kernel/io.rs):
> >
> > #[inline]
> > const fn io_addr_assert<U>(&self, offset: usize) -> usize {
> > build_assert!(Self::offset_valid::<U>(offset, SIZE));
> >
> > self.addr() + offset
> > }
> >
> > since offset isn't known at compile time, this can easily be misused?
>
> Well, that's intentional.
>
> iomem.readb(0x0) // succeeds if SIZE >=1
> iomem.readb(foo) // fails if foo is not known at compile time

By "fails" here you mean fail to link, right?

> iomem.try_readb(foo) // succeeds if self.maxsize() >= 1

Apologies for being dense throughout this discussion. Could you check
my understanding?

The trick is that `build_error` is marked `#[export_name =
"rust_build_error"]` which isn't exported unless
CONFIG_RUST_BUILD_ASSERT_ALLOW is defined, causing linking to it to
fail. This even works for doctests, but not for #[test] in the kernel
crate because they are built as part of the crate. The only to way
make that work correctly is to put `build_error` in a crate all by
itself.