Skip to content

Commit 135d24d

Browse files
committed
Fix volatile stores of fat pointers
1 parent 235d774 commit 135d24d

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/librustc_trans/trans/intrinsic.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,20 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
589589
},
590590
(_, "volatile_store") => {
591591
let tp_ty = *substs.types.get(FnSpace, 0);
592-
let val = if fn_ty.args[1].is_indirect() {
593-
Load(bcx, llargs[1])
592+
if type_is_fat_ptr(bcx.tcx(), tp_ty) {
593+
VolatileStore(bcx, llargs[1], expr::get_dataptr(bcx, llargs[0]));
594+
VolatileStore(bcx, llargs[2], expr::get_meta(bcx, llargs[0]));
594595
} else {
595-
from_immediate(bcx, llargs[1])
596-
};
597-
let ptr = PointerCast(bcx, llargs[0], val_ty(val).ptr_to());
598-
let store = VolatileStore(bcx, val, ptr);
599-
unsafe {
600-
llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty));
596+
let val = if fn_ty.args[1].is_indirect() {
597+
Load(bcx, llargs[1])
598+
} else {
599+
from_immediate(bcx, llargs[1])
600+
};
601+
let ptr = PointerCast(bcx, llargs[0], val_ty(val).ptr_to());
602+
let store = VolatileStore(bcx, val, ptr);
603+
unsafe {
604+
llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty));
605+
}
601606
}
602607
C_nil(ccx)
603608
},

src/test/run-pass/volatile-fat-ptr.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(volatile)]
12+
use std::ptr::{read_volatile, write_volatile};
13+
14+
fn main() {
15+
let mut x: &'static str = "test";
16+
unsafe {
17+
let a = read_volatile(&x);
18+
assert_eq!(a, "test");
19+
write_volatile(&mut x, "foo");
20+
assert_eq!(x, "foo");
21+
}
22+
}

0 commit comments

Comments
 (0)