On Fri, 9 Feb 2024 at 11:14, Alistair Francis <[email protected]> wrote:
>
> From: Daniel Henrique Barboza <[email protected]>
>
> There is no need to keep both 'vlen' and 'vlenb'. All existing code
> that requires 'vlen' is retrieving it via 'vlenb << 3'.
>
> Signed-off-by: Daniel Henrique Barboza <[email protected]>
> Reviewed-by: Richard Henderson <[email protected]>
> Reviewed-by: Alistair Francis <[email protected]>
> Message-ID: <[email protected]>
> Signed-off-by: Alistair Francis <[email protected]>

Hi; I was looking at this code while investigating a Coverity
issue (CID 1467672) and noticed something a little odd:

> @@ -1816,22 +1815,21 @@ static void prop_vlen_set(Object *obj, Visitor *v, 
> const char *name,
>          return;
>      }

This property setter forbids values that aren't a power of 2:
    if (!is_power_of_2(value)) {
        error_setg(errp, "Vector extension VLEN must be power of 2");
        return;
    }
and that implicitly includes ruling out 0. But...

> -    if (value != cpu->cfg.vlen && riscv_cpu_is_vendor(obj)) {
> +    if (value != cpu->cfg.vlenb && riscv_cpu_is_vendor(obj)) {
>          cpu_set_prop_err(cpu, name, errp);
>          error_append_hint(errp, "Current '%s' val: %u\n",
> -                          name, cpu->cfg.vlen);
> +                          name, cpu->cfg.vlenb << 3);
>          return;
>      }
>
>      cpu_option_add_user_setting(name, value);
> -    cpu->cfg.vlen = value;
>      cpu->cfg.vlenb = value >> 3;

...here we shift the user-provided value right by 3 without checking
that it's still non-zero. So the user can pass '2', for example, and
get past the validity check and set cfg.vlenb to 0 anyway.

> diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c
> index b580b83f9a..5618769a00 100644
> --- a/target/riscv/tcg/tcg-cpu.c
> +++ b/target/riscv/tcg/tcg-cpu.c
> @@ -298,7 +298,9 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, 
> Error **errp)
>  static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg,
>                                   Error **errp)
>  {
> -    if (cfg->vlen > RV_VLEN_MAX || cfg->vlen < 128) {
> +    uint32_t vlen = cfg->vlenb << 3;
> +
> +    if (vlen > RV_VLEN_MAX || vlen < 128) {
>          error_setg(errp,
>                     "Vector extension implementation only supports VLEN "
>                     "in the range [128, %d]", RV_VLEN_MAX);

Here it looks like we will catch the 0 later, but it seems inconsistent
to do half the property validity check in the setter function and
then half of it later on in realize. Plus the property and its setter
are present in the base class, but we only do the realize-time validity
checks for TCG.

(The Coverity issue is in code that implicitly is relying on
cfg->vlenb being non-zero, because it passes it to ctzl() to get
a shift count, so vlenb == 0 would be UB. I'm going to mark it
a false positive because we do still sanitize vlenb in the
TCG realize function.)

thanks
-- PMM

Reply via email to