Skip to content

Commit

Permalink
Fix volatile stores of fat pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Mar 19, 2016
1 parent 235d774 commit 135d24d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/librustc_trans/trans/intrinsic.rs
Expand Up @@ -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)
},
Expand Down
22 changes: 22 additions & 0 deletions 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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");
}
}

0 comments on commit 135d24d

Please sign in to comment.