Skip to content

Commit 6cc19b5

Browse files
committed
Moves second half of emit_sanitized_load_immediate(stack_slot_of_value_to_store, constant) into ANCHOR_TRANSLATE_MEMORY_ADDRESS.
1 parent 5980c15 commit 6cc19b5

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/jit.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
memory_management::{
3131
allocate_pages, free_pages, get_system_page_size, protect_pages, round_to_page_size,
3232
},
33-
memory_region::{AccessType, MemoryMapping},
33+
memory_region::MemoryMapping,
3434
program::BuiltinFunction,
3535
vm::{get_runtime_environment_key, Config, ContextObject, EbpfVm},
3636
x86::*,
@@ -200,7 +200,7 @@ const ANCHOR_INTERNAL_FUNCTION_CALL_PROLOGUE: usize = 12;
200200
const ANCHOR_INTERNAL_FUNCTION_CALL_REG: usize = 13;
201201
const ANCHOR_CALL_REG_UNSUPPORTED_INSTRUCTION: usize = 14;
202202
const ANCHOR_TRANSLATE_MEMORY_ADDRESS: usize = 21;
203-
const ANCHOR_COUNT: usize = 30; // Update me when adding or removing anchors
203+
const ANCHOR_COUNT: usize = 34; // Update me when adding or removing anchors
204204

205205
const REGISTER_MAP: [u8; 11] = [
206206
CALLER_SAVED_REGISTERS[0], // RAX
@@ -1155,11 +1155,10 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
11551155
self.emit_ins(X86Instruction::store(OperandSize::S64, reg, RSP, stack_slot_of_value_to_store));
11561156
}
11571157
Some(Value::Constant64(constant, user_provided)) => {
1158-
if user_provided && self.should_sanitize_constant(constant) {
1159-
self.emit_sanitized_load_immediate(REGISTER_SCRATCH, constant);
1160-
} else {
1161-
self.emit_ins(X86Instruction::load_immediate(REGISTER_SCRATCH, constant));
1162-
}
1158+
debug_assert!(user_provided);
1159+
// First half of emit_sanitized_load_immediate()
1160+
let lower_key = self.immediate_value_key as i32 as i64;
1161+
self.emit_ins(X86Instruction::load_immediate(REGISTER_SCRATCH, constant.wrapping_sub(lower_key)));
11631162
self.emit_ins(X86Instruction::store(OperandSize::S64, REGISTER_SCRATCH, RSP, stack_slot_of_value_to_store));
11641163
}
11651164
_ => {}
@@ -1185,8 +1184,12 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
11851184
}
11861185

11871186
if self.config.enable_address_translation {
1188-
let access_type = if value.is_none() { AccessType::Load } else { AccessType::Store };
1189-
let anchor = ANCHOR_TRANSLATE_MEMORY_ADDRESS + len.trailing_zeros() as usize + 4 * (access_type as usize);
1187+
let access_type = match value {
1188+
Some(Value::Register(_reg)) => 4,
1189+
Some(Value::Constant64(_constant, _user_provided)) => 8,
1190+
_ => 0,
1191+
};
1192+
let anchor = ANCHOR_TRANSLATE_MEMORY_ADDRESS + access_type + len.trailing_zeros() as usize;
11901193
self.emit_ins(X86Instruction::push_immediate(OperandSize::S64, self.pc as i32));
11911194
self.emit_ins(X86Instruction::call_immediate(self.relative_to_anchor(anchor, 5)));
11921195
if let Some(dst) = dst {
@@ -1597,21 +1600,16 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
15971600
// Translates a vm memory address to a host memory address
15981601
let lower_key = self.immediate_value_key as i32 as i64;
15991602
for (access_type, len) in &[
1600-
(AccessType::Load, 1i32),
1601-
(AccessType::Load, 2i32),
1602-
(AccessType::Load, 4i32),
1603-
(AccessType::Load, 8i32),
1604-
(AccessType::Store, 1i32),
1605-
(AccessType::Store, 2i32),
1606-
(AccessType::Store, 4i32),
1607-
(AccessType::Store, 8i32),
1603+
(0, 1i32), (0, 2i32), (0, 4i32), (0, 8i32),
1604+
(4, 1i32), (4, 2i32), (4, 4i32), (4, 8i32),
1605+
(8, 1i32), (8, 2i32), (8, 4i32), (8, 8i32),
16081606
] {
1609-
let target_offset = len.trailing_zeros() as usize + 4 * (*access_type as usize);
1607+
let target_offset = *access_type + len.trailing_zeros() as usize;
16101608
self.set_anchor(ANCHOR_TRANSLATE_MEMORY_ADDRESS + target_offset);
16111609
// Second half of emit_sanitized_load_immediate(REGISTER_SCRATCH, vm_addr)
16121610
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, REGISTER_SCRATCH, lower_key, None));
16131611
// call MemoryMapping::(load|store) storing the result in RuntimeEnvironmentSlot::ProgramResult
1614-
if *access_type == AccessType::Load {
1612+
if *access_type == 0 { // AccessType::Load
16151613
let load = match len {
16161614
1 => MemoryMapping::load::<u8> as *const u8 as i64,
16171615
2 => MemoryMapping::load::<u16> as *const u8 as i64,
@@ -1625,7 +1623,11 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
16251623
Argument { index: 1, value: Value::RegisterPlusConstant32(REGISTER_PTR_TO_VM, self.slot_in_vm(RuntimeEnvironmentSlot::MemoryMapping), false) },
16261624
Argument { index: 0, value: Value::RegisterPlusConstant32(REGISTER_PTR_TO_VM, self.slot_in_vm(RuntimeEnvironmentSlot::ProgramResult), false) },
16271625
], None);
1628-
} else {
1626+
} else { // AccessType::Store
1627+
if *access_type == 8 {
1628+
// Second half of emit_sanitized_load_immediate()
1629+
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, RSP, lower_key, Some(X86IndirectAccess::OffsetIndexShift(-96, RSP, 0))));
1630+
}
16291631
let store = match len {
16301632
1 => MemoryMapping::store::<u8> as *const u8 as i64,
16311633
2 => MemoryMapping::store::<u16> as *const u8 as i64,
@@ -1648,7 +1650,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
16481650
self.emit_ins(X86Instruction::xchg(OperandSize::S64, REGISTER_SCRATCH, RSP, Some(X86IndirectAccess::OffsetIndexShift(0, RSP, 0)))); // Swap return address and self.pc
16491651
self.emit_ins(X86Instruction::conditional_jump_immediate(0x85, self.relative_to_anchor(ANCHOR_THROW_EXCEPTION, 6)));
16501652

1651-
if *access_type == AccessType::Load {
1653+
if *access_type == 0 { // AccessType::Load
16521654
// unwrap() the result into REGISTER_SCRATCH
16531655
self.emit_ins(X86Instruction::load(OperandSize::S64, REGISTER_PTR_TO_VM, REGISTER_SCRATCH, X86IndirectAccess::Offset(self.slot_in_vm(RuntimeEnvironmentSlot::ProgramResult) + std::mem::size_of::<u64>() as i32)));
16541656
}

0 commit comments

Comments
 (0)