Skip to content

Commit bead241

Browse files
committed
Sanitize emit_validate_instruction_count() as well.
1 parent 372ccb4 commit bead241

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/jit.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838

3939
const MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH: usize = 4096;
4040
const MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION: usize = 110;
41-
const MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT: usize = 13;
41+
const MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT: usize = 23;
4242
const MAX_START_PADDING_LENGTH: usize = 256;
4343

4444
pub struct JitProgram {
@@ -941,12 +941,10 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
941941
// Update `MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT` if you change the code generation here
942942
if let Some(pc) = pc {
943943
self.last_instruction_meter_validation_pc = pc;
944-
// instruction_meter >= self.pc
945-
self.emit_ins(X86Instruction::cmp_immediate(OperandSize::S64, REGISTER_INSTRUCTION_METER, pc as i64, None));
946-
} else {
947-
// instruction_meter >= scratch_register
948-
self.emit_ins(X86Instruction::cmp(OperandSize::S64, REGISTER_SCRATCH, REGISTER_INSTRUCTION_METER, None));
944+
self.emit_sanitized_load_immediate(REGISTER_SCRATCH, pc as i64);
949945
}
946+
// If instruction_meter >= pc, throw ExceededMaxInstructions
947+
self.emit_ins(X86Instruction::cmp(OperandSize::S64, REGISTER_SCRATCH, REGISTER_INSTRUCTION_METER, None));
950948
self.emit_ins(X86Instruction::conditional_jump_immediate(0x86, self.relative_to_anchor(ANCHOR_THROW_EXCEEDED_MAX_INSTRUCTIONS, 6)));
951949
}
952950

@@ -1835,9 +1833,9 @@ mod tests {
18351833
let instruction_meter_checkpoint_machine_code_length =
18361834
instruction_meter_checkpoint_machine_code_length[0]
18371835
- instruction_meter_checkpoint_machine_code_length[1];
1838-
assert_eq!(
1839-
instruction_meter_checkpoint_machine_code_length,
1840-
MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT
1836+
assert!(
1837+
instruction_meter_checkpoint_machine_code_length
1838+
<= MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT
18411839
);
18421840

18431841
for sbpf_version in [SBPFVersion::V0, SBPFVersion::V3] {

0 commit comments

Comments
 (0)