Re: [PATCH v4 10/20] rust: add `kernel` crate

From: Miguel Ojeda
Date: Tue Feb 22 2022 - 07:48:34 EST


Hi Petr,

On Tue, Feb 22, 2022 at 10:06 AM Petr Mladek <pmladek@xxxxxxxx> wrote:
>
> What exactly should we keep in sync, please?
>
> I see only handling of KERN_* prefix in print.rs. I do not see there
> any counter part of LOG_LINE_MAX, CONSOLE_LOG_MAX, or PREFIX_MAX.

Good catch! We had a buffer on the Rust side in the past, but that is
not the case anymore since commit 9e8bd679ecf2 ("Support Rust
`core::fmt::Argument` in vsprintf") on our side, so we will remove the
comment.

> I am sorry but I am not familiar with rust. What are these limits
> 2 and 10 used for, please?
>
> I guess that 2 is the size of a single KERN_* identifier.
> But what is 10?
>
> Note that printk() format prefix is typically just a single KERN_*
> identifier. But there might be more. Well, in practice only the
> following combination makes sense: KERN_CONT + KERN_<LEVEL>.

What we are doing here is generating compile-time format strings that
are then used by the `pr_*!` macros (which call the C side `printk()`
with one of the strings).

In other words, this is not parsing arbitrary `printk()` format
strings (which I am guessing something like that is your concern --
please let me know if I got it wrong).

To clarify a bit what the code does and what the constants are:

- `generate()` is called at compile-time.

- The size of these generated strings is always 10.

- We compose those strings by using the first 2 bytes of `KERN_*`.
However, we also take the chance to check (at compile-time) they have
the contents we expect, including the third zero byte.

- Then other 8 bytes are used to complete the 10: either "%pA" plus
0 padding (`CONT` level) or "%s: %pA" (otherwise).

> Finally, is there any way to test whether any change in the printk
> code breaks the rust support?

One way is to compile the code, e.g. the `assert!`s in the `generate`
function run at compile-time, thus they provide a first layer of
defense.

Another way is to use `samples/rust/print.rs` which we run in the CI
as a black box test.

Is that what you had in mind? Or something like unit tests or self tests?

Thanks for taking a look!

Cheers,
Miguel