From ba006440eeae07cb6d6285a6b64f4374754d2300 Mon Sep 17 00:00:00 2001 From: Irina Popa Date: Tue, 17 Jul 2018 16:43:49 +0300 Subject: [PATCH] rustc_codegen_llvm: use safe references for ThinLTOData. --- src/librustc_codegen_llvm/back/lto.rs | 13 ++++++------- src/librustc_codegen_llvm/llvm/ffi.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs index ce56be734d77..daa2fb052806 100644 --- a/src/librustc_codegen_llvm/back/lto.rs +++ b/src/librustc_codegen_llvm/back/lto.rs @@ -423,11 +423,10 @@ fn thin_lto(diag_handler: &Handler, thin_modules.len() as u32, symbol_white_list.as_ptr(), symbol_white_list.len() as u32, - ); - if data.is_null() { - let msg = "failed to prepare thin LTO context".to_string(); - return Err(write::llvm_err(&diag_handler, msg)) - } + ).ok_or_else(|| { + write::llvm_err(&diag_handler, "failed to prepare thin LTO context".to_string()) + })?; + let data = ThinData(data); info!("thin LTO data created"); timeline.record("data"); @@ -566,7 +565,7 @@ struct ThinShared { module_names: Vec, } -struct ThinData(*mut llvm::ThinLTOData); +struct ThinData(&'static mut llvm::ThinLTOData); unsafe impl Send for ThinData {} unsafe impl Sync for ThinData {} @@ -574,7 +573,7 @@ unsafe impl Sync for ThinData {} impl Drop for ThinData { fn drop(&mut self) { unsafe { - llvm::LLVMRustFreeThinLTOData(self.0); + llvm::LLVMRustFreeThinLTOData(&mut *(self.0 as *mut _)); } } } diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index ce10a98938ec..ba37eaa4608b 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -1580,24 +1580,24 @@ extern "C" { NumModules: c_uint, PreservedSymbols: *const *const c_char, PreservedSymbolsLen: c_uint, - ) -> *mut ThinLTOData; + ) -> Option<&'static mut ThinLTOData>; pub fn LLVMRustPrepareThinLTORename( - Data: *const ThinLTOData, + Data: &ThinLTOData, Module: &Module, ) -> bool; pub fn LLVMRustPrepareThinLTOResolveWeak( - Data: *const ThinLTOData, + Data: &ThinLTOData, Module: &Module, ) -> bool; pub fn LLVMRustPrepareThinLTOInternalize( - Data: *const ThinLTOData, + Data: &ThinLTOData, Module: &Module, ) -> bool; pub fn LLVMRustPrepareThinLTOImport( - Data: *const ThinLTOData, + Data: &ThinLTOData, Module: &Module, ) -> bool; - pub fn LLVMRustFreeThinLTOData(Data: *mut ThinLTOData); + pub fn LLVMRustFreeThinLTOData(Data: &'static mut ThinLTOData); pub fn LLVMRustParseBitcodeForThinLTO( Context: &Context, Data: *const u8,