From 9bb66634314323e92eb1f33fed0a059f46b0aa4c Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 16 Nov 2018 13:33:28 +0200 Subject: [PATCH] [eddyb] rustc_codegen_ssa: handle LLVM unsafety correctly. --- src/librustc_codegen_llvm/asm.rs | 4 ++-- src/librustc_codegen_llvm/base.rs | 4 +++- src/librustc_codegen_llvm/builder.rs | 19 ++++++++++++------- src/librustc_codegen_llvm/consts.rs | 10 ++++------ .../interfaces/builder.rs | 8 ++++---- .../interfaces/statics.rs | 2 +- src/librustc_codegen_ssa/mir/mod.rs | 4 +++- 7 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/librustc_codegen_llvm/asm.rs b/src/librustc_codegen_llvm/asm.rs index 8bb88ba5a83..7f0e18e1aeb 100644 --- a/src/librustc_codegen_llvm/asm.rs +++ b/src/librustc_codegen_llvm/asm.rs @@ -84,8 +84,8 @@ impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> { let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap(); let constraint_cstr = CString::new(all_constraints).unwrap(); let r = self.inline_asm_call( - asm.as_ptr(), - constraint_cstr.as_ptr(), + &asm, + &constraint_cstr, &inputs, output_type, ia.volatile, diff --git a/src/librustc_codegen_llvm/base.rs b/src/librustc_codegen_llvm/base.rs index 529639bf033..102fc95529d 100644 --- a/src/librustc_codegen_llvm/base.rs +++ b/src/librustc_codegen_llvm/base.rs @@ -194,7 +194,9 @@ pub fn compile_codegen_unit<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>, // Run replace-all-uses-with for statics that need it for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() { - cx.static_replace_all_uses(old_g, new_g) + unsafe { + cx.static_replace_all_uses(old_g, new_g) + } } // Create the llvm.used variable diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs index c10e98c5546..b8df95e3183 100644 --- a/src/librustc_codegen_llvm/builder.rs +++ b/src/librustc_codegen_llvm/builder.rs @@ -28,6 +28,7 @@ use rustc_codegen_ssa::base::to_immediate; use rustc_codegen_ssa::mir::operand::{OperandValue, OperandRef}; use rustc_codegen_ssa::mir::place::PlaceRef; use std::borrow::Cow; +use std::ffi::CStr; use std::ops::Range; use std::ptr; @@ -838,7 +839,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { } } - fn inline_asm_call(&mut self, asm: *const c_char, cons: *const c_char, + fn inline_asm_call(&mut self, asm: &CStr, cons: &CStr, inputs: &[&'ll Value], output: &'ll Type, volatile: bool, alignstack: bool, dia: syntax::ast::AsmDialect) -> Option<&'ll Value> { @@ -858,11 +859,17 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { let fty = self.cx().type_func(&argtys[..], output); unsafe { // Ask LLVM to verify that the constraints are well-formed. - let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons); + let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr()); debug!("Constraint verification result: {:?}", constraints_ok); if constraints_ok { let v = llvm::LLVMRustInlineAsm( - fty, asm, cons, volatile, alignstack, AsmDialect::from_generic(dia)); + fty, + asm.as_ptr(), + cons.as_ptr(), + volatile, + alignstack, + AsmDialect::from_generic(dia), + ); Some(self.call(v, inputs, None)) } else { // LLVM has detected an issue with our constraints, bail out @@ -1400,10 +1407,8 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { self.cx } - fn delete_basic_block(&mut self, bb: &'ll BasicBlock) { - unsafe { - llvm::LLVMDeleteBasicBlock(bb); - } + unsafe fn delete_basic_block(&mut self, bb: &'ll BasicBlock) { + llvm::LLVMDeleteBasicBlock(bb); } fn do_not_inline(&mut self, llret: &'ll Value) { diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index f8876207b2e..5e45a4f68ae 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -498,11 +498,9 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> { } } } - fn static_replace_all_uses(&self, old_g: &'ll Value, new_g: &'ll Value) { - unsafe { - let bitcast = llvm::LLVMConstPointerCast(new_g, self.val_ty(old_g)); - llvm::LLVMReplaceAllUsesWith(old_g, bitcast); - llvm::LLVMDeleteGlobal(old_g); - } + unsafe fn static_replace_all_uses(&self, old_g: &'ll Value, new_g: &'ll Value) { + let bitcast = llvm::LLVMConstPointerCast(new_g, self.val_ty(old_g)); + llvm::LLVMReplaceAllUsesWith(old_g, bitcast); + llvm::LLVMDeleteGlobal(old_g); } } diff --git a/src/librustc_codegen_ssa/interfaces/builder.rs b/src/librustc_codegen_ssa/interfaces/builder.rs index c80eb271911..3757c514d2c 100644 --- a/src/librustc_codegen_ssa/interfaces/builder.rs +++ b/src/librustc_codegen_ssa/interfaces/builder.rs @@ -15,7 +15,7 @@ use super::intrinsic::IntrinsicCallMethods; use super::type_::ArgTypeMethods; use super::HasCodegen; use common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope}; -use libc::c_char; +use std::ffi::CStr; use mir::operand::OperandRef; use mir::place::PlaceRef; use rustc::ty::layout::{Align, Size}; @@ -162,8 +162,8 @@ pub trait BuilderMethods<'a, 'tcx: 'a>: ) -> Self::Value; fn inline_asm_call( &mut self, - asm: *const c_char, - cons: *const c_char, + asm: &CStr, + cons: &CStr, inputs: &[Self::Value], output: Self::Type, volatile: bool, @@ -318,6 +318,6 @@ pub trait BuilderMethods<'a, 'tcx: 'a>: ) -> Self::Value; fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value; - fn delete_basic_block(&mut self, bb: Self::BasicBlock); + unsafe fn delete_basic_block(&mut self, bb: Self::BasicBlock); fn do_not_inline(&mut self, llret: Self::Value); } diff --git a/src/librustc_codegen_ssa/interfaces/statics.rs b/src/librustc_codegen_ssa/interfaces/statics.rs index 27748a8dd42..172c48f8a85 100644 --- a/src/librustc_codegen_ssa/interfaces/statics.rs +++ b/src/librustc_codegen_ssa/interfaces/statics.rs @@ -19,5 +19,5 @@ pub trait StaticMethods<'tcx>: Backend<'tcx> { fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value; fn get_static(&self, def_id: DefId) -> Self::Value; fn codegen_static(&self, def_id: DefId, is_mutable: bool); - fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value); + unsafe fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value); } diff --git a/src/librustc_codegen_ssa/mir/mod.rs b/src/librustc_codegen_ssa/mir/mod.rs index 9722e2f03b9..873c3ee367c 100644 --- a/src/librustc_codegen_ssa/mir/mod.rs +++ b/src/librustc_codegen_ssa/mir/mod.rs @@ -359,7 +359,9 @@ pub fn codegen_mir<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>( // Unreachable block if !visited.contains(bb.index()) { debug!("codegen_mir: block {:?} was not visited", bb); - bx.delete_basic_block(fx.blocks[bb]); + unsafe { + bx.delete_basic_block(fx.blocks[bb]); + } } } }