From 135d24dd039306daab7157c4c47963986c091d26 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Sat, 19 Mar 2016 02:18:33 +0000 Subject: [PATCH] Fix volatile stores of fat pointers --- src/librustc_trans/trans/intrinsic.rs | 21 +++++++++++++-------- src/test/run-pass/volatile-fat-ptr.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/test/run-pass/volatile-fat-ptr.rs diff --git a/src/librustc_trans/trans/intrinsic.rs b/src/librustc_trans/trans/intrinsic.rs index 43976f8233b..3c0329e0b7c 100644 --- a/src/librustc_trans/trans/intrinsic.rs +++ b/src/librustc_trans/trans/intrinsic.rs @@ -589,15 +589,20 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, }, (_, "volatile_store") => { let tp_ty = *substs.types.get(FnSpace, 0); - let val = if fn_ty.args[1].is_indirect() { - Load(bcx, llargs[1]) + if type_is_fat_ptr(bcx.tcx(), tp_ty) { + VolatileStore(bcx, llargs[1], expr::get_dataptr(bcx, llargs[0])); + VolatileStore(bcx, llargs[2], expr::get_meta(bcx, llargs[0])); } else { - from_immediate(bcx, llargs[1]) - }; - let ptr = PointerCast(bcx, llargs[0], val_ty(val).ptr_to()); - let store = VolatileStore(bcx, val, ptr); - unsafe { - llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty)); + let val = if fn_ty.args[1].is_indirect() { + Load(bcx, llargs[1]) + } else { + from_immediate(bcx, llargs[1]) + }; + let ptr = PointerCast(bcx, llargs[0], val_ty(val).ptr_to()); + let store = VolatileStore(bcx, val, ptr); + unsafe { + llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty)); + } } C_nil(ccx) }, diff --git a/src/test/run-pass/volatile-fat-ptr.rs b/src/test/run-pass/volatile-fat-ptr.rs new file mode 100644 index 00000000000..03ba5587fce --- /dev/null +++ b/src/test/run-pass/volatile-fat-ptr.rs @@ -0,0 +1,22 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(volatile)] +use std::ptr::{read_volatile, write_volatile}; + +fn main() { + let mut x: &'static str = "test"; + unsafe { + let a = read_volatile(&x); + assert_eq!(a, "test"); + write_volatile(&mut x, "foo"); + assert_eq!(x, "foo"); + } +}