Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - Sanitization of memory accesses in JIT #6

Merged
merged 4 commits into from
Dec 20, 2024

Conversation

Lichtso
Copy link
Collaborator

@Lichtso Lichtso commented Dec 20, 2024

No description provided.

@Lichtso Lichtso requested a review from LucasSte December 20, 2024 16:30
@@ -200,7 +200,7 @@ const ANCHOR_INTERNAL_FUNCTION_CALL_PROLOGUE: usize = 12;
const ANCHOR_INTERNAL_FUNCTION_CALL_REG: usize = 13;
const ANCHOR_CALL_REG_UNSUPPORTED_INSTRUCTION: usize = 14;
const ANCHOR_TRANSLATE_MEMORY_ADDRESS: usize = 21;
const ANCHOR_COUNT: usize = 30; // Update me when adding or removing anchors
const ANCHOR_COUNT: usize = 34; // Update me when adding or removing anchors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't added or removed anchors, have you?

Copy link
Collaborator Author

@Lichtso Lichtso Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The store immediate ones have their own ANCHOR_TRANSLATE_MEMORY_ADDRESS now.

src/jit.rs Outdated
} else if destination != REGISTER_SCRATCH {
self.emit_ins(X86Instruction::load_immediate(destination, value.wrapping_sub(self.immediate_value_key)));
self.emit_ins(X86Instruction::load_immediate(REGISTER_SCRATCH, self.immediate_value_key));
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x01, REGISTER_SCRATCH, destination, 0, None));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the comments you add for clarity next to instructions. One could be useful here for signaling an addition.

src/jit.rs Outdated
@@ -1190,8 +1184,12 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
}

if self.config.enable_address_translation {
let access_type = if value.is_none() { AccessType::Load } else { AccessType::Store };
let anchor = ANCHOR_TRANSLATE_MEMORY_ADDRESS + len.trailing_zeros() as usize + 4 * (access_type as usize);
let access_type = match value {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this access_type may be confusing with AccessType. What about altering the enum to represent load, store reg and store imm?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AccessType comes from the memory mapping and that does not know about store immediate.
So I renamed access_type to anchor_base.

} else { // AccessType::Store
if *access_type == 8 {
// Second half of emit_sanitized_load_immediate()
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, RSP, lower_key, Some(X86IndirectAccess::OffsetIndexShift(-96, RSP, 0))));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a comment about the -96 offset? Does it come from stack_slot_of_value_to_store?

src/jit.rs Outdated
@@ -1650,8 +1650,10 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
self.emit_ins(X86Instruction::xchg(OperandSize::S64, REGISTER_SCRATCH, RSP, Some(X86IndirectAccess::OffsetIndexShift(0, RSP, 0)))); // Swap return address and self.pc
self.emit_ins(X86Instruction::conditional_jump_immediate(0x85, self.relative_to_anchor(ANCHOR_THROW_EXCEPTION, 6)));

// unwrap() the result into REGISTER_SCRATCH
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)));
if *access_type == 0 { // AccessType::Load
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this restricted to load?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only load instructions do return a value, store instructions don't.

@Lichtso Lichtso force-pushed the refactor/sanitization_in_memory_access branch from 6cc19b5 to 1132ec9 Compare December 20, 2024 20:05
@Lichtso Lichtso merged commit cad781a into main Dec 20, 2024
11 checks passed
@Lichtso Lichtso deleted the refactor/sanitization_in_memory_access branch December 20, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants