|
| 1 | +use crate::helpers::EvalContextExt as HEvalContext; |
| 2 | +use crate::sync::EvalContextExt as SEvalContext; |
| 3 | +use crate::thread::EvalContextExt as TEvalContext; |
| 4 | +use crate::InterpResult; |
| 5 | +use crate::OpTy; |
| 6 | +use crate::{MiriEvalContext, Tag}; |
| 7 | + |
| 8 | +enum OpType { |
| 9 | + UmtxOpWait = 2, |
| 10 | + //TODO: other types |
| 11 | +} |
| 12 | + |
| 13 | +impl TryFrom<i32> for OpType { |
| 14 | + type Error = &'static str; |
| 15 | + |
| 16 | + fn try_from(value: i32) -> Result<Self, Self::Error> { |
| 17 | + match value { |
| 18 | + 2 => Ok(OpType::UmtxOpWait), |
| 19 | + _ => Err("Unsupported Futex operation"), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +pub fn futex<'tcx>( |
| 25 | + this: &mut MiriEvalContext<'_, 'tcx>, |
| 26 | + obj: &OpTy<'tcx, Tag>, |
| 27 | + op: i32, |
| 28 | + val: u64, |
| 29 | + uaddr: &OpTy<'tcx, Tag>, |
| 30 | + uaddr2: &OpTy<'tcx, Tag>, |
| 31 | +) -> Option<InterpResult<'tcx>> { |
| 32 | + // Object to operate on |
| 33 | + let obj = obj; |
| 34 | + // Operation type |
| 35 | + let _op_type = op; |
| 36 | + // Current value pointed to by `obj` |
| 37 | + let val = val; |
| 38 | + // Pointer that's purpose depends on the op_type |
| 39 | + let _uaddr = this.read_pointer(uaddr).ok()?; |
| 40 | + // Pointer that's purpose depends on the op_type |
| 41 | + let _uaddr2 = this.read_pointer(uaddr2).ok()?; |
| 42 | + match OpType::try_from(op) { |
| 43 | + Ok(op) => |
| 44 | + match op { |
| 45 | + OpType::UmtxOpWait => |
| 46 | + if this.read_scalar(obj).ok()?.to_u64().ok()? == val { |
| 47 | + this.futex_wait( |
| 48 | + this.read_scalar(obj).ok()?.to_u64().ok()?, |
| 49 | + this.get_active_thread(), |
| 50 | + u32::MAX, |
| 51 | + ); |
| 52 | + None |
| 53 | + } else { |
| 54 | + // The `val` value is invalid. Double check this against the manual. |
| 55 | + let einval = this.eval_libc("EINVAL").ok()?; |
| 56 | + this.set_last_error(einval).ok()?; |
| 57 | + None |
| 58 | + }, |
| 59 | + }, |
| 60 | + Err(_) => { |
| 61 | + // The `op` value is invalid. |
| 62 | + let einval = this.eval_libc("EINVAL").ok()?; |
| 63 | + this.set_last_error(einval).ok()?; |
| 64 | + None |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments