solana-labs / rbpf

Rust virtual machine and JIT compiler for eBPF programs
Apache License 2.0
272 stars 163 forks source link

type casting of rbpf #578

Closed shenghaoyuan closed 2 months ago

shenghaoyuan commented 3 months ago

Hello, It looks like there are some inconsistency when Solana rbpf deals with the type-casting using i32 and u32. e.g.

When Linux eBPF performs type-casting, it always follows a consistent way

#define ALU(OPCODE, OP)                 \
    ALU64_##OPCODE##_X:             \
        DST = DST OP SRC;           \
        CONT;                   \
    ALU_##OPCODE##_X:               \
        DST = (u32) DST OP (u32) SRC;       \
        CONT;                   \
    ALU64_##OPCODE##_K:             \
        DST = DST OP IMM;           \
        CONT;                   \
    ALU_##OPCODE##_K:               \
        DST = (u32) DST OP (u32) IMM;       \
Lichtso commented 2 months ago

Known issue: https://github.com/solana-labs/rbpf/pull/548 https://github.com/solana-labs/solana/issues/32924

The inconsistency looks like it comes from the input side:

let a = (x as i32).wrapping_add(y as i32) as u64;
let b = (x as u32).wrapping_add(y as u32) as u64;

But it actually happens on the output side:

let c = (x as u32).wrapping_add(y as u32) as i32 as u64;
let d = (x as u32).wrapping_add(y as u32) as u32 as u64;