Skip to content

Commit

Permalink
Address code reviews
Browse files Browse the repository at this point in the history
- Changes return type of futex() to InterpResult<'tcx> and removes every ok() calls
- Add documentation and link to `_umtx_op` man pages
- Move comments to parameters and remove pointless assignments
- Replace `OpTy`s usages to `Pointer`s
- Run `deref_operand` on `read_scalar` output in Futex wait operation
- Replace `to_u64` to `to_machine_usize`

Signed-off-by: InfRandomness <infrandomness@gmail.com>
  • Loading branch information
b-ncMN committed Jul 6, 2022
1 parent 9efdbd4 commit 4eacb0f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/shims/unix/freebsd/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_middle::mir;
use rustc_span::Symbol;
use rustc_target::spec::abi::Abi;

use crate::shims::unix::freebsd::futex::futex;
use crate::shims::unix::freebsd::sync::futex;
use crate::*;
use shims::foreign_items::EmulateByNameResult;

Expand Down
67 changes: 0 additions & 67 deletions src/shims/unix/freebsd/futex.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/shims/unix/freebsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod dlsym;
pub mod foreign_items;
pub mod futex;
pub mod sync;
81 changes: 81 additions & 0 deletions src/shims/unix/freebsd/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::helpers::EvalContextExt as HEvalContext;
use crate::sync::EvalContextExt as SEvalContext;
use crate::thread::EvalContextExt as TEvalContext;
use crate::InterpResult;
use crate::OpTy;
use crate::{MiriEvalContext, Tag};
use std::fmt::Pointer;

enum OpType {
UmtxOpWait = 2,
//TODO: other types
}

impl TryFrom<i32> for OpType {
type Error = &'static str;

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
2 => Ok(OpType::UmtxOpWait),
_ => Err("Unsupported Futex operation"),
}
}
}

struct Futex<'mir, 'tcx> {}

impl<'mir, 'tcx: 'mir> Futex<'mir, 'tcx> {
/// Function that performs a specific action on a futex thread
/// https://www.freebsd.org/cgi/man.cgi?query=_umtx_op&sektion=2&n=1
///
/// # Arguments
///
/// * `this`: Context of the evaluation
/// * `obj`: Pointer to a variable of type long
/// * `op`: Futex operation to perform on `obj`
/// * `val`: Depends on the operation performed (see man pages)
/// * `uaddr`: Depends on the operation performed (see man pages)
/// * `uaddr2`: Depends on the operation performed (see man pages)
///
/// returns: InterpResult<'tcx>
pub fn futex<'tcx>(
this: &mut MiriEvalContext<'_, 'tcx>,
// Object to operate on
obj: &OpTy<'tcx, Tag>,
// Operation type
op: i32,
// Current value pointed to by `obj`
val: u64,
// Pointer that's purpose depends on the op_type
//uaddr: &OpTy<'tcx, Tag>,
uaddr: Pointer<Option<Tag>>,
// Pointer that's purpose depends on the op_type
uaddr2: Pointer<<Option<Tag>>,
) -> InterpResult<'tcx> {
match OpType::try_from(op) {
Ok(op) =>
match op {
OpType::UmtxOpWait =>
if this.deref_operand(this.read_scalar(obj)?.to_machine_usize()?) == val {
this.futex_wait(
this.read_scalar(obj)?.to_machine_usize()?,
this.get_active_thread(),
u32::MAX,
);
Ok(())
} else {
// The `val` value is invalid. Double check this against the manual.
let einval = this.eval_libc("EINVAL")?;
this.set_last_error(einval)?;
Ok(())
},
},
Err(_) => {
// The `op` value is invalid.
let einval = this.eval_libc("EINVAL")?;
this.set_last_error(einval)?;
Ok(())
}
}
}
}

0 comments on commit 4eacb0f

Please sign in to comment.