solana-labs / rbpf

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

Interperter CUs Computation questions #595

Closed shenghaoyuan closed 1 month ago

shenghaoyuan commented 1 month ago

Hello, I have two questions about CUs computation of the Solana interpreter, could someone please give me some suggestions?

  1. The Solana interpreter says the CALL_REG spends an additional CU by self.vm.due_insn_count += 1;, why?
  2. The interpreter does self.vm.due_insn_count = self.vm.previous_instruction_meter - self.vm.due_insn_count; and after invoking function, does self.vm.due_insn_count = 0;, why? Could we remove those two lines from the interpreter (and also modify the program.rs )?
    // https://github.com/solana-labs/rbpf/blob/main/src/interpreter.rs#L477
                        self.vm.due_insn_count = self.vm.previous_instruction_meter - self.vm.due_insn_count; // removing 
                        self.vm.registers[0..6].copy_from_slice(&self.reg[0..6]);
                        self.vm.invoke_function(function);
                        self.vm.due_insn_count = 0; // removing 

https://github.com/solana-labs/rbpf/blob/main/src/program.rs#L331 vm.context_object_pointer.consume(vm.previous_instruction_meter - vm.due_insn_count); // vm.previous_instruction_meter - vm.due_insn_count -> vm.due_insn_count ... vm.previous_instruction_meter = vm.context_object_pointer.get_remaining(); // removing

shenghaoyuan commented 1 month ago

The first question can be omitted as due_insn_count +1 is in a throw_error branch.

Lichtso commented 1 month ago

The second is so that it is interface compatible with JIT compiled programs upon entry in a syscall. The syscall helper wrapper in program.rs does expect the CU metering to have been flushed.