Skip to content

Commit 9bb6663

Browse files
committed
[eddyb] rustc_codegen_ssa: handle LLVM unsafety correctly.
1 parent bf7f8cd commit 9bb6663

File tree

7 files changed

+29
-22
lines changed

7 files changed

+29
-22
lines changed

src/librustc_codegen_llvm/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
8484
let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
8585
let constraint_cstr = CString::new(all_constraints).unwrap();
8686
let r = self.inline_asm_call(
87-
asm.as_ptr(),
88-
constraint_cstr.as_ptr(),
87+
&asm,
88+
&constraint_cstr,
8989
&inputs,
9090
output_type,
9191
ia.volatile,

src/librustc_codegen_llvm/base.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ pub fn compile_codegen_unit<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
194194

195195
// Run replace-all-uses-with for statics that need it
196196
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
197-
cx.static_replace_all_uses(old_g, new_g)
197+
unsafe {
198+
cx.static_replace_all_uses(old_g, new_g)
199+
}
198200
}
199201

200202
// Create the llvm.used variable

src/librustc_codegen_llvm/builder.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_codegen_ssa::base::to_immediate;
2828
use rustc_codegen_ssa::mir::operand::{OperandValue, OperandRef};
2929
use rustc_codegen_ssa::mir::place::PlaceRef;
3030
use std::borrow::Cow;
31+
use std::ffi::CStr;
3132
use std::ops::Range;
3233
use std::ptr;
3334

@@ -838,7 +839,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
838839
}
839840
}
840841

841-
fn inline_asm_call(&mut self, asm: *const c_char, cons: *const c_char,
842+
fn inline_asm_call(&mut self, asm: &CStr, cons: &CStr,
842843
inputs: &[&'ll Value], output: &'ll Type,
843844
volatile: bool, alignstack: bool,
844845
dia: syntax::ast::AsmDialect) -> Option<&'ll Value> {
@@ -858,11 +859,17 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
858859
let fty = self.cx().type_func(&argtys[..], output);
859860
unsafe {
860861
// Ask LLVM to verify that the constraints are well-formed.
861-
let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons);
862+
let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr());
862863
debug!("Constraint verification result: {:?}", constraints_ok);
863864
if constraints_ok {
864865
let v = llvm::LLVMRustInlineAsm(
865-
fty, asm, cons, volatile, alignstack, AsmDialect::from_generic(dia));
866+
fty,
867+
asm.as_ptr(),
868+
cons.as_ptr(),
869+
volatile,
870+
alignstack,
871+
AsmDialect::from_generic(dia),
872+
);
866873
Some(self.call(v, inputs, None))
867874
} else {
868875
// LLVM has detected an issue with our constraints, bail out
@@ -1400,10 +1407,8 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
14001407
self.cx
14011408
}
14021409

1403-
fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
1404-
unsafe {
1405-
llvm::LLVMDeleteBasicBlock(bb);
1406-
}
1410+
unsafe fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
1411+
llvm::LLVMDeleteBasicBlock(bb);
14071412
}
14081413

14091414
fn do_not_inline(&mut self, llret: &'ll Value) {

src/librustc_codegen_llvm/consts.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,9 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> {
498498
}
499499
}
500500
}
501-
fn static_replace_all_uses(&self, old_g: &'ll Value, new_g: &'ll Value) {
502-
unsafe {
503-
let bitcast = llvm::LLVMConstPointerCast(new_g, self.val_ty(old_g));
504-
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
505-
llvm::LLVMDeleteGlobal(old_g);
506-
}
501+
unsafe fn static_replace_all_uses(&self, old_g: &'ll Value, new_g: &'ll Value) {
502+
let bitcast = llvm::LLVMConstPointerCast(new_g, self.val_ty(old_g));
503+
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
504+
llvm::LLVMDeleteGlobal(old_g);
507505
}
508506
}

src/librustc_codegen_ssa/interfaces/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::intrinsic::IntrinsicCallMethods;
1515
use super::type_::ArgTypeMethods;
1616
use super::HasCodegen;
1717
use common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope};
18-
use libc::c_char;
18+
use std::ffi::CStr;
1919
use mir::operand::OperandRef;
2020
use mir::place::PlaceRef;
2121
use rustc::ty::layout::{Align, Size};
@@ -162,8 +162,8 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
162162
) -> Self::Value;
163163
fn inline_asm_call(
164164
&mut self,
165-
asm: *const c_char,
166-
cons: *const c_char,
165+
asm: &CStr,
166+
cons: &CStr,
167167
inputs: &[Self::Value],
168168
output: Self::Type,
169169
volatile: bool,
@@ -318,6 +318,6 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
318318
) -> Self::Value;
319319
fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
320320

321-
fn delete_basic_block(&mut self, bb: Self::BasicBlock);
321+
unsafe fn delete_basic_block(&mut self, bb: Self::BasicBlock);
322322
fn do_not_inline(&mut self, llret: Self::Value);
323323
}

src/librustc_codegen_ssa/interfaces/statics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ pub trait StaticMethods<'tcx>: Backend<'tcx> {
1919
fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
2020
fn get_static(&self, def_id: DefId) -> Self::Value;
2121
fn codegen_static(&self, def_id: DefId, is_mutable: bool);
22-
fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value);
22+
unsafe fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value);
2323
}

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ pub fn codegen_mir<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
359359
// Unreachable block
360360
if !visited.contains(bb.index()) {
361361
debug!("codegen_mir: block {:?} was not visited", bb);
362-
bx.delete_basic_block(fx.blocks[bb]);
362+
unsafe {
363+
bx.delete_basic_block(fx.blocks[bb]);
364+
}
363365
}
364366
}
365367
}

0 commit comments

Comments
 (0)