Skip to content

Commit

Permalink
Fix #557 (#567)
Browse files Browse the repository at this point in the history
* Fixes "Sanitizes pc in emit_validate_instruction_count() and emit_profile_instruction_count(). (#557)"

d2419e5

* Adds test_far_jumps()
  • Loading branch information
Lichtso committed May 20, 2024
1 parent 86fde9a commit 899cd91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,13 +832,13 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
#[inline]
fn emit_sanitized_alu(&mut self, size: OperandSize, opcode: u8, opcode_extension: u8, destination: u8, immediate: i64) {
if self.should_sanitize_constant(immediate) {
self.emit_sanitized_load_immediate(size, REGISTER_SCRATCH, immediate);
self.emit_ins(X86Instruction::alu(size, opcode, REGISTER_SCRATCH, destination, 0, None));
self.emit_sanitized_load_immediate(size, REGISTER_OTHER_SCRATCH, immediate);
self.emit_ins(X86Instruction::alu(size, opcode, REGISTER_OTHER_SCRATCH, destination, 0, None));
} else if immediate >= i32::MIN as i64 && immediate <= i32::MAX as i64 {
self.emit_ins(X86Instruction::alu(size, 0x81, opcode_extension, destination, immediate, None));
} else {
self.emit_ins(X86Instruction::load_immediate(size, REGISTER_SCRATCH, immediate));
self.emit_ins(X86Instruction::alu(size, opcode, REGISTER_SCRATCH, destination, 0, None));
self.emit_ins(X86Instruction::load_immediate(size, REGISTER_OTHER_SCRATCH, immediate));
self.emit_ins(X86Instruction::alu(size, opcode, REGISTER_OTHER_SCRATCH, destination, 0, None));
}
}

Expand Down Expand Up @@ -882,7 +882,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
fn emit_profile_instruction_count(&mut self, target_pc: Option<usize>) {
match target_pc {
Some(target_pc) => {
self.emit_sanitized_alu(OperandSize::S32, 0x81, 0, REGISTER_INSTRUCTION_METER, target_pc as i64 - self.pc as i64 - 1);
self.emit_sanitized_alu(OperandSize::S64, 0x01, 0, REGISTER_INSTRUCTION_METER, target_pc as i64 - self.pc as i64 - 1);
},
None => {
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 5, REGISTER_INSTRUCTION_METER, self.pc as i64 + 1, None)); // instruction_meter -= self.pc + 1;
Expand Down
23 changes: 23 additions & 0 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,29 @@ fn test_err_exit_capped() {
);
}

#[test]
fn test_far_jumps() {
test_interpreter_and_jit_asm!(
"
call function_c
exit
function_a:
exit
function_b:
.fill 1024, 0x0F
exit
function_c:
mov32 r1, 0x00000010
hor64 r1, 0x00000001
callx r1
exit",
[],
(),
TestContextObject::new(7),
ProgramResult::Ok(0),
);
}

// Symbols and Relocation

#[test]
Expand Down

0 comments on commit 899cd91

Please sign in to comment.