From 881929f7537d6fd5b85f6c8efc1dc0f3f558591f Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 16 Sep 2019 13:46:37 -0500 Subject: [PATCH] Add align_offset for integers --- src/shims/mod.rs | 49 +++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/shims/mod.rs b/src/shims/mod.rs index a0da364ff0b..f9b89cf553a 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -27,25 +27,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } // There are some more lang items we want to hook that CTFE does not hook (yet). if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) { - - let n = { - let ptr = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?; - let align = this.force_bits( - this.read_scalar(args[1])?.not_undef()?, - this.pointer_size() - )? as usize; - - let stride = this.memory().get(ptr.alloc_id)?.align.bytes() as usize; - // if the allocation alignment is at least the required alignment, we use the - // libcore implementation - if stride >= align { - ((stride + ptr.offset.bytes() as usize) as *const ()) - .align_offset(align) as u128 - } else { - u128::max_value() - } - }; - + let n = this.align_offset(args[0], args[1])?; let dest = dest.unwrap(); let n = this.truncate(n, dest.layout); this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?; @@ -65,4 +47,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // Otherwise, load the MIR. Ok(Some(this.load_mir(instance.def, None)?)) } + + fn align_offset( + &mut self, + ptr_op: OpTy<'tcx, Tag>, + align_op: OpTy<'tcx, Tag> + ) -> InterpResult<'tcx, u128> { + let this = self.eval_context_mut(); + + let req_align = this.force_bits( + this.read_scalar(align_op)?.not_undef()?, + this.pointer_size() + )? as usize; + + let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?; + + if let Scalar::Ptr(ptr) = ptr_scalar { + let cur_align = this.memory().get(ptr.alloc_id)?.align.bytes() as usize; + if cur_align < req_align { + return Ok(u128::max_value()); + } + } + + // if the allocation alignment is at least the required alignment or if the pointer is an + // integer, we use the libcore implementation + Ok( + (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8) + .align_offset(req_align) as u128 + ) + } }