@@ -30,7 +30,7 @@ use crate::{
30
30
memory_management:: {
31
31
allocate_pages, free_pages, get_system_page_size, protect_pages, round_to_page_size,
32
32
} ,
33
- memory_region:: { AccessType , MemoryMapping } ,
33
+ memory_region:: MemoryMapping ,
34
34
program:: BuiltinFunction ,
35
35
vm:: { get_runtime_environment_key, Config , ContextObject , EbpfVm } ,
36
36
x86:: * ,
@@ -200,7 +200,7 @@ const ANCHOR_INTERNAL_FUNCTION_CALL_PROLOGUE: usize = 12;
200
200
const ANCHOR_INTERNAL_FUNCTION_CALL_REG : usize = 13 ;
201
201
const ANCHOR_CALL_REG_UNSUPPORTED_INSTRUCTION : usize = 14 ;
202
202
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
204
204
205
205
const REGISTER_MAP : [ u8 ; 11 ] = [
206
206
CALLER_SAVED_REGISTERS [ 0 ] , // RAX
@@ -1155,11 +1155,10 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1155
1155
self . emit_ins ( X86Instruction :: store ( OperandSize :: S64 , reg, RSP , stack_slot_of_value_to_store) ) ;
1156
1156
}
1157
1157
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) ) ) ;
1163
1162
self . emit_ins ( X86Instruction :: store ( OperandSize :: S64 , REGISTER_SCRATCH , RSP , stack_slot_of_value_to_store) ) ;
1164
1163
}
1165
1164
_ => { }
@@ -1185,8 +1184,12 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1185
1184
}
1186
1185
1187
1186
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 ;
1190
1193
self . emit_ins ( X86Instruction :: push_immediate ( OperandSize :: S64 , self . pc as i32 ) ) ;
1191
1194
self . emit_ins ( X86Instruction :: call_immediate ( self . relative_to_anchor ( anchor, 5 ) ) ) ;
1192
1195
if let Some ( dst) = dst {
@@ -1597,21 +1600,16 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1597
1600
// Translates a vm memory address to a host memory address
1598
1601
let lower_key = self . immediate_value_key as i32 as i64 ;
1599
1602
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 ) ,
1608
1606
] {
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 ;
1610
1608
self . set_anchor ( ANCHOR_TRANSLATE_MEMORY_ADDRESS + target_offset) ;
1611
1609
// Second half of emit_sanitized_load_immediate(REGISTER_SCRATCH, vm_addr)
1612
1610
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x81 , 0 , REGISTER_SCRATCH , lower_key, None ) ) ;
1613
1611
// call MemoryMapping::(load|store) storing the result in RuntimeEnvironmentSlot::ProgramResult
1614
- if * access_type == AccessType :: Load {
1612
+ if * access_type == 0 { // AccessType::Load
1615
1613
let load = match len {
1616
1614
1 => MemoryMapping :: load :: < u8 > as * const u8 as i64 ,
1617
1615
2 => MemoryMapping :: load :: < u16 > as * const u8 as i64 ,
@@ -1625,7 +1623,11 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1625
1623
Argument { index : 1 , value : Value :: RegisterPlusConstant32 ( REGISTER_PTR_TO_VM , self . slot_in_vm ( RuntimeEnvironmentSlot :: MemoryMapping ) , false ) } ,
1626
1624
Argument { index : 0 , value : Value :: RegisterPlusConstant32 ( REGISTER_PTR_TO_VM , self . slot_in_vm ( RuntimeEnvironmentSlot :: ProgramResult ) , false ) } ,
1627
1625
] , 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
+ }
1629
1631
let store = match len {
1630
1632
1 => MemoryMapping :: store :: < u8 > as * const u8 as i64 ,
1631
1633
2 => MemoryMapping :: store :: < u16 > as * const u8 as i64 ,
@@ -1648,7 +1650,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1648
1650
self . emit_ins ( X86Instruction :: xchg ( OperandSize :: S64 , REGISTER_SCRATCH , RSP , Some ( X86IndirectAccess :: OffsetIndexShift ( 0 , RSP , 0 ) ) ) ) ; // Swap return address and self.pc
1649
1651
self . emit_ins ( X86Instruction :: conditional_jump_immediate ( 0x85 , self . relative_to_anchor ( ANCHOR_THROW_EXCEPTION , 6 ) ) ) ;
1650
1652
1651
- if * access_type == AccessType :: Load {
1653
+ if * access_type == 0 { // AccessType::Load
1652
1654
// unwrap() the result into REGISTER_SCRATCH
1653
1655
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 ) ) ) ;
1654
1656
}
0 commit comments