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

jit: correctly load the StableResult discriminant in emit_result_is_err #514

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,9 +1279,9 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

fn emit_result_is_err(&mut self, destination: u8) {
let ok = ProgramResult::Ok(0);
let err_kind = unsafe { *(&ok as *const _ as *const u64).add(1) };
let ok_discriminant = ok.discriminant();
self.emit_ins(X86Instruction::lea(OperandSize::S64, RBP, destination, Some(X86IndirectAccess::Offset(self.slot_on_environment_stack(RuntimeEnvironmentSlot::ProgramResult)))));
self.emit_ins(X86Instruction::cmp_immediate(OperandSize::S64, destination, err_kind as i64, Some(X86IndirectAccess::Offset(0))));
self.emit_ins(X86Instruction::cmp_immediate(OperandSize::S64, destination, ok_discriminant as i64, Some(X86IndirectAccess::Offset(0))));
}

fn emit_subroutines(&mut self) {
Expand Down
16 changes: 14 additions & 2 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ impl<T: Debug, E: Debug> StableResult<T, E> {
Self::Err(error) => error,
}
}

#[cfg_attr(
any(
not(feature = "jit"),
target_os = "windows",
not(target_arch = "x86_64")
),
allow(dead_code)
)]
pub(crate) fn discriminant(&self) -> u64 {
unsafe { *(self as *const _ as *const u64) }
}
}

impl<T, E> From<StableResult<T, E>> for Result<T, E> {
Expand Down Expand Up @@ -558,9 +570,9 @@ mod tests {
#[test]
fn test_program_result_is_stable() {
let ok = ProgramResult::Ok(42);
assert_eq!(unsafe { *(&ok as *const _ as *const u64) }, 0);
assert_eq!(ok.discriminant(), 0);
let err = ProgramResult::Err(Box::new(EbpfError::JitNotCompiled));
assert_eq!(unsafe { *(&err as *const _ as *const u64) }, 1);
assert_eq!(err.discriminant(), 1);
}

#[test]
Expand Down