@@ -423,7 +423,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
423
423
424
424
match insn. opc {
425
425
ebpf:: LD_DW_IMM if !self . executable . get_sbpf_version ( ) . disable_lddw ( ) => {
426
- self . emit_validate_and_profile_instruction_count ( false , Some ( self . pc + 2 ) ) ;
426
+ self . emit_validate_and_profile_instruction_count ( Some ( self . pc + 2 ) ) ;
427
427
self . pc += 1 ;
428
428
self . result . pc_section [ self . pc ] = self . anchors [ ANCHOR_CALL_UNSUPPORTED_INSTRUCTION ] as usize ;
429
429
ebpf:: augment_lddw_unchecked ( self . program , & mut insn) ;
@@ -702,7 +702,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
702
702
703
703
// BPF_JMP class
704
704
ebpf:: JA => {
705
- self . emit_validate_and_profile_instruction_count ( true , Some ( target_pc) ) ;
705
+ self . emit_validate_and_profile_instruction_count ( Some ( target_pc) ) ;
706
706
self . emit_ins ( X86Instruction :: load_immediate ( REGISTER_SCRATCH , target_pc as i64 ) ) ;
707
707
let jump_offset = self . relative_to_target_pc ( target_pc, 5 ) ;
708
708
self . emit_ins ( X86Instruction :: jump_immediate ( jump_offset) ) ;
@@ -788,7 +788,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
788
788
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x81 , 5 , REGISTER_PTR_TO_VM , 1 , Some ( call_depth_access) ) ) ; // env.call_depth -= 1;
789
789
790
790
// and return
791
- self . emit_profile_instruction_count ( false , Some ( 0 ) ) ;
791
+ self . emit_profile_instruction_count ( Some ( 0 ) ) ;
792
792
self . emit_ins ( X86Instruction :: return_near ( ) ) ;
793
793
} ,
794
794
@@ -802,7 +802,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
802
802
if self . offset_in_text_section + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION * 2 >= self . result . text_section . len ( ) {
803
803
return Err ( EbpfError :: ExhaustedTextSegment ( self . pc ) ) ;
804
804
}
805
- self . emit_validate_and_profile_instruction_count ( false , Some ( self . pc + 1 ) ) ;
805
+ self . emit_validate_and_profile_instruction_count ( Some ( self . pc + 1 ) ) ;
806
806
self . emit_ins ( X86Instruction :: load_immediate ( REGISTER_SCRATCH , self . pc as i64 ) ) ; // Save pc
807
807
self . emit_set_exception_kind ( EbpfError :: ExecutionOverrun ) ;
808
808
self . emit_ins ( X86Instruction :: jump_immediate ( self . relative_to_anchor ( ANCHOR_THROW_EXCEPTION , 5 ) ) ) ;
@@ -951,39 +951,27 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
951
951
}
952
952
953
953
#[ inline]
954
- fn emit_profile_instruction_count ( & mut self , user_provided : bool , target_pc : Option < usize > ) {
954
+ fn emit_profile_instruction_count ( & mut self , target_pc : Option < usize > ) {
955
955
if !self . config . enable_instruction_meter {
956
956
return ;
957
957
}
958
958
match target_pc {
959
959
Some ( target_pc) => {
960
- // instruction_meter += target_pc - (self.pc + 1);
961
- let immediate = target_pc as i64 - self . pc as i64 - 1 ;
962
- if user_provided {
963
- self . emit_sanitized_alu ( OperandSize :: S64 , 0x01 , 0 , REGISTER_INSTRUCTION_METER , immediate) ;
964
- } else {
965
- self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x81 , 0 , REGISTER_INSTRUCTION_METER , immediate, None ) ) ;
966
- }
960
+ self . emit_sanitized_alu ( OperandSize :: S64 , 0x01 , 0 , REGISTER_INSTRUCTION_METER , target_pc as i64 - self . pc as i64 - 1 ) ; // instruction_meter += target_pc - (self.pc + 1);
967
961
} ,
968
962
None => {
969
- self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x81 , 5 , REGISTER_INSTRUCTION_METER , self . pc as i64 + 1 , None ) ) ; // instruction_meter -= self.pc + 1;
970
963
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x01 , REGISTER_SCRATCH , REGISTER_INSTRUCTION_METER , 0 , None ) ) ; // instruction_meter += target_pc;
964
+ self . emit_sanitized_alu ( OperandSize :: S64 , 0x81 , 5 , REGISTER_INSTRUCTION_METER , self . pc as i64 + 1 ) ; // instruction_meter -= self.pc + 1;
971
965
} ,
972
966
}
973
967
}
974
968
975
- #[ inline]
976
- fn emit_validate_and_profile_instruction_count ( & mut self , user_provided : bool , target_pc : Option < usize > ) {
977
- self . emit_validate_instruction_count ( Some ( self . pc ) ) ;
978
- self . emit_profile_instruction_count ( user_provided, target_pc) ;
979
- }
980
-
981
969
#[ inline]
982
970
fn emit_undo_profile_instruction_count ( & mut self , target_pc : Value ) {
983
971
if self . config . enable_instruction_meter {
984
972
match target_pc {
985
973
Value :: Constant64 ( target_pc, _) => {
986
- self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x81 , 0 , REGISTER_INSTRUCTION_METER , self . pc as i64 + 1 - target_pc, None ) ) ; // instruction_meter += (self.pc + 1) - target_pc;
974
+ self . emit_sanitized_alu ( OperandSize :: S64 , 0x01 , 0 , REGISTER_INSTRUCTION_METER , self . pc as i64 + 1 - target_pc) ; // instruction_meter += (self.pc + 1) - target_pc;
987
975
}
988
976
Value :: Register ( target_pc) => {
989
977
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x29 , target_pc, REGISTER_INSTRUCTION_METER , 0 , None ) ) ; // instruction_meter -= guest_target_pc
@@ -995,6 +983,12 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
995
983
}
996
984
}
997
985
986
+ #[ inline]
987
+ fn emit_validate_and_profile_instruction_count ( & mut self , target_pc : Option < usize > ) {
988
+ self . emit_validate_instruction_count ( Some ( self . pc ) ) ;
989
+ self . emit_profile_instruction_count ( target_pc) ;
990
+ }
991
+
998
992
fn emit_rust_call ( & mut self , target : Value , arguments : & [ Argument ] , result_reg : Option < u8 > ) {
999
993
let mut saved_registers = CALLER_SAVED_REGISTERS . to_vec ( ) ;
1000
994
if let Some ( reg) = result_reg {
@@ -1123,7 +1117,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1123
1117
} ,
1124
1118
Value :: Constant64 ( target_pc, user_provided) => {
1125
1119
debug_assert ! ( user_provided) ;
1126
- self . emit_profile_instruction_count ( user_provided , Some ( target_pc as usize ) ) ;
1120
+ self . emit_profile_instruction_count ( Some ( target_pc as usize ) ) ;
1127
1121
if user_provided && self . should_sanitize_constant ( target_pc) {
1128
1122
self . emit_sanitized_load_immediate ( REGISTER_SCRATCH , target_pc) ;
1129
1123
} else {
@@ -1149,7 +1143,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1149
1143
1150
1144
#[ inline]
1151
1145
fn emit_syscall_dispatch ( & mut self , function : BuiltinFunction < C > ) {
1152
- self . emit_validate_and_profile_instruction_count ( false , Some ( 0 ) ) ;
1146
+ self . emit_validate_and_profile_instruction_count ( Some ( 0 ) ) ;
1153
1147
self . emit_ins ( X86Instruction :: load_immediate ( REGISTER_SCRATCH , function as usize as i64 ) ) ;
1154
1148
self . emit_ins ( X86Instruction :: call_immediate ( self . relative_to_anchor ( ANCHOR_EXTERNAL_FUNCTION_CALL , 5 ) ) ) ;
1155
1149
self . emit_undo_profile_instruction_count ( Value :: Constant64 ( 0 , false ) ) ;
@@ -1228,7 +1222,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1228
1222
1229
1223
#[ inline]
1230
1224
fn emit_conditional_branch_reg ( & mut self , op : u8 , bitwise : bool , first_operand : u8 , second_operand : u8 , target_pc : usize ) {
1231
- self . emit_validate_and_profile_instruction_count ( true , Some ( target_pc) ) ;
1225
+ self . emit_validate_and_profile_instruction_count ( Some ( target_pc) ) ;
1232
1226
if bitwise { // Logical
1233
1227
self . emit_ins ( X86Instruction :: test ( OperandSize :: S64 , first_operand, second_operand, None ) ) ;
1234
1228
} else { // Arithmetic
@@ -1242,7 +1236,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1242
1236
1243
1237
#[ inline]
1244
1238
fn emit_conditional_branch_imm ( & mut self , op : u8 , bitwise : bool , immediate : i64 , second_operand : u8 , target_pc : usize ) {
1245
- self . emit_validate_and_profile_instruction_count ( true , Some ( target_pc) ) ;
1239
+ self . emit_validate_and_profile_instruction_count ( Some ( target_pc) ) ;
1246
1240
if self . should_sanitize_constant ( immediate) {
1247
1241
self . emit_sanitized_load_immediate ( REGISTER_SCRATCH , immediate) ;
1248
1242
if bitwise { // Logical
@@ -1578,7 +1572,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1578
1572
let number_of_instructions = self . result . pc_section . len ( ) ;
1579
1573
self . emit_ins ( X86Instruction :: cmp_immediate ( OperandSize :: S64 , REGISTER_SCRATCH , ( number_of_instructions * INSN_SIZE ) as i64 , None ) ) ; // guest_target_address.cmp(number_of_instructions * INSN_SIZE)
1580
1574
self . emit_ins ( X86Instruction :: conditional_jump_immediate ( 0x83 , self . relative_to_anchor ( ANCHOR_CALL_OUTSIDE_TEXT_SEGMENT , 6 ) ) ) ;
1581
- // First half of self.emit_profile_instruction_count(false, None);
1575
+ // First half of self.emit_profile_instruction_count(None);
1582
1576
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x2b , REGISTER_INSTRUCTION_METER , RSP , 0 , Some ( X86IndirectAccess :: OffsetIndexShift ( -8 , RSP , 0 ) ) ) ) ; // instruction_meter -= guest_current_pc;
1583
1577
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x81 , 5 , REGISTER_INSTRUCTION_METER , 1 , None ) ) ; // instruction_meter -= 1;
1584
1578
// Load host target_address from self.result.pc_section
@@ -1591,7 +1585,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1591
1585
let shift_amount = INSN_SIZE . trailing_zeros ( ) ;
1592
1586
debug_assert_eq ! ( INSN_SIZE , 1 << shift_amount) ;
1593
1587
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0xc1 , 5 , REGISTER_SCRATCH , shift_amount as i64 , None ) ) ; // guest_target_pc /= INSN_SIZE;
1594
- // Second half of self.emit_profile_instruction_count(false, None);
1588
+ // Second half of self.emit_profile_instruction_count(None);
1595
1589
self . emit_ins ( X86Instruction :: alu ( OperandSize :: S64 , 0x01 , REGISTER_SCRATCH , REGISTER_INSTRUCTION_METER , 0 , None ) ) ; // instruction_meter += guest_target_pc;
1596
1590
// Restore the clobbered REGISTER_MAP[0]
1597
1591
self . emit_ins ( X86Instruction :: xchg ( OperandSize :: S64 , REGISTER_MAP [ 0 ] , RSP , Some ( X86IndirectAccess :: OffsetIndexShift ( 0 , RSP , 0 ) ) ) ) ; // Swap REGISTER_MAP[0] and host_target_address
0 commit comments