Skip to content

Commit 78eae9b

Browse files
committed
Only allow using the atomic intrinsics on integer types
1 parent 30a3849 commit 78eae9b

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

src/librustc_trans/intrinsic.rs

+43-22
Original file line numberDiff line numberDiff line change
@@ -752,33 +752,47 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
752752

753753
match split[1] {
754754
"cxchg" | "cxchgweak" => {
755-
let cmp = from_immediate(bcx, llargs[1]);
756-
let src = from_immediate(bcx, llargs[2]);
757-
let ptr = PointerCast(bcx, llargs[0], val_ty(src).ptr_to());
758-
let weak = if split[1] == "cxchgweak" { llvm::True } else { llvm::False };
759-
let val = AtomicCmpXchg(bcx, ptr, cmp, src, order, failorder, weak);
760-
let result = ExtractValue(bcx, val, 0);
761-
let success = ZExt(bcx, ExtractValue(bcx, val, 1), Type::bool(bcx.ccx()));
762-
Store(bcx,
763-
result,
764-
PointerCast(bcx, StructGEP(bcx, llresult, 0), val_ty(src).ptr_to()));
765-
Store(bcx, success, StructGEP(bcx, llresult, 1));
755+
let sty = &substs.types.get(FnSpace, 0).sty;
756+
if int_type_width_signed(sty, ccx).is_some() {
757+
let weak = if split[1] == "cxchgweak" { llvm::True } else { llvm::False };
758+
let val = AtomicCmpXchg(bcx, llargs[0], llargs[1], llargs[2],
759+
order, failorder, weak);
760+
let result = ExtractValue(bcx, val, 0);
761+
let success = ZExt(bcx, ExtractValue(bcx, val, 1), Type::bool(bcx.ccx()));
762+
Store(bcx, result, StructGEP(bcx, llresult, 0));
763+
Store(bcx, success, StructGEP(bcx, llresult, 1));
764+
} else {
765+
span_invalid_monomorphization_error(
766+
tcx.sess, span,
767+
&format!("invalid monomorphization of `{}` intrinsic: \
768+
expected basic integer type, found `{}`", name, sty));
769+
}
766770
C_nil(ccx)
767771
}
768772

769773
"load" => {
770-
let tp_ty = *substs.types.get(FnSpace, 0);
771-
let mut ptr = llargs[0];
772-
if let Some(ty) = fn_ty.ret.cast {
773-
ptr = PointerCast(bcx, ptr, ty.ptr_to());
774+
let sty = &substs.types.get(FnSpace, 0).sty;
775+
if int_type_width_signed(sty, ccx).is_some() {
776+
AtomicLoad(bcx, llargs[0], order)
777+
} else {
778+
span_invalid_monomorphization_error(
779+
tcx.sess, span,
780+
&format!("invalid monomorphization of `{}` intrinsic: \
781+
expected basic integer type, found `{}`", name, sty));
782+
C_nil(ccx)
774783
}
775-
to_immediate(bcx, AtomicLoad(bcx, ptr, order), tp_ty)
776784
}
777785

778786
"store" => {
779-
let val = from_immediate(bcx, llargs[1]);
780-
let ptr = PointerCast(bcx, llargs[0], val_ty(val).ptr_to());
781-
AtomicStore(bcx, val, ptr, order);
787+
let sty = &substs.types.get(FnSpace, 0).sty;
788+
if int_type_width_signed(sty, ccx).is_some() {
789+
AtomicStore(bcx, llargs[1], llargs[0], order);
790+
} else {
791+
span_invalid_monomorphization_error(
792+
tcx.sess, span,
793+
&format!("invalid monomorphization of `{}` intrinsic: \
794+
expected basic integer type, found `{}`", name, sty));
795+
}
782796
C_nil(ccx)
783797
}
784798

@@ -809,9 +823,16 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
809823
_ => ccx.sess().fatal("unknown atomic operation")
810824
};
811825

812-
let val = from_immediate(bcx, llargs[1]);
813-
let ptr = PointerCast(bcx, llargs[0], val_ty(val).ptr_to());
814-
AtomicRMW(bcx, atom_op, ptr, val, order)
826+
let sty = &substs.types.get(FnSpace, 0).sty;
827+
if int_type_width_signed(sty, ccx).is_some() {
828+
AtomicRMW(bcx, atom_op, llargs[0], llargs[1], order)
829+
} else {
830+
span_invalid_monomorphization_error(
831+
tcx.sess, span,
832+
&format!("invalid monomorphization of `{}` intrinsic: \
833+
expected basic integer type, found `{}`", name, sty));
834+
C_nil(ccx)
835+
}
815836
}
816837
}
817838

src/test/run-pass/issue-23550.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@ use std::intrinsics;
1616
#[derive(Copy, Clone)]
1717
struct Wrap(i64);
1818

19-
// These volatile and atomic intrinsics used to cause an ICE
19+
// These volatile intrinsics used to cause an ICE
2020

2121
unsafe fn test_bool(p: &mut bool, v: bool) {
2222
intrinsics::volatile_load(p);
2323
intrinsics::volatile_store(p, v);
24-
intrinsics::atomic_load(p);
25-
intrinsics::atomic_cxchg(p, v, v);
26-
intrinsics::atomic_store(p, v);
27-
intrinsics::atomic_xchg(p, v);
2824
}
2925

3026
unsafe fn test_immediate_fca(p: &mut Wrap, v: Wrap) {
3127
intrinsics::volatile_load(p);
3228
intrinsics::volatile_store(p, v);
33-
intrinsics::atomic_load(p);
34-
intrinsics::atomic_cxchg(p, v, v);
35-
intrinsics::atomic_store(p, v);
36-
intrinsics::atomic_xchg(p, v);
3729
}
3830

3931
fn main() {}

0 commit comments

Comments
 (0)