Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type casting of rbpf #578

Closed
shenghaoyuan opened this issue Jun 11, 2024 · 1 comment
Closed

type casting of rbpf #578

shenghaoyuan opened this issue Jun 11, 2024 · 1 comment

Comments

@shenghaoyuan
Copy link

shenghaoyuan commented Jun 11, 2024

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

  • ebpf::ADD32_IMM considers dst and imm as i32 firstly, then as u64, while
  • ebpf::OR32_IMM considers dst and imm as u32 first, then as u64
    // BPF_ALU class
    ebpf::ADD32_IMM  => self.reg[dst] = (self.reg[dst] as i32).wrapping_add(insn.imm as i32)      as u64,
    ebpf::ADD32_REG  => self.reg[dst] = (self.reg[dst] as i32).wrapping_add(self.reg[src] as i32) as u64,
    ebpf::SUB32_IMM  => if self.executable.get_sbpf_version().swap_sub_reg_imm_operands() {
                        self.reg[dst] = (insn.imm as i32).wrapping_sub(self.reg[dst] as i32)      as u64
    } else {
                        self.reg[dst] = (self.reg[dst] as i32).wrapping_sub(insn.imm as i32)      as u64
    },
    ebpf::SUB32_REG  => self.reg[dst] = (self.reg[dst] as i32).wrapping_sub(self.reg[src] as i32) as u64,
    ...
    ebpf::OR32_IMM   => self.reg[dst] = (self.reg[dst] as u32             | insn.imm as u32)      as u64,

Does Solana rbpf perform some specific operations on e.g. eBPF ADD ...? As self.reg[dst] = (self.reg[dst] as i32).wrapping_add(insn.imm as i32) as u64 and self.reg[dst] = (self.reg[dst] as u32).wrapping_add(insn.imm as u32) as u64 may have different results.

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
Copy link

Lichtso commented Jul 3, 2024

Known issue:
#548
solana-labs/solana#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;

@Lichtso Lichtso closed this as completed Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants