diff options
| author | bors <bors@rust-lang.org> | 2023-11-30 09:45:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-30 09:45:59 +0000 |
| commit | 1670ff64bf1ccb2ad71068254b53725631c55864 (patch) | |
| tree | e89b8defac72beddae35d61e21ce13ad8e8eae67 /compiler/rustc_codegen_llvm/src | |
| parent | c52b8763bf36027f24baabe1f97cab3d3571c9e5 (diff) | |
| parent | 640a43178ebc3239d85c597b0a71566b67ab8e60 (diff) | |
| download | rust-1670ff64bf1ccb2ad71068254b53725631c55864.tar.gz rust-1670ff64bf1ccb2ad71068254b53725631c55864.zip | |
Auto merge of #118473 - matthiaskrgr:rollup-q96bm3u, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #118452 (rustdoc-search: allow spaces around `::` in path query) - #118453 (Tweak message on ADT with private fields building) - #118456 (rustc_span: Remove unused symbols.) - #118458 (rustdoc: remove small from `small-section-header`) - #118464 (Dispose llvm::TargetMachines prior to llvm::Context being disposed) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
| -rw-r--r-- | compiler/rustc_codegen_llvm/src/back/lto.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_codegen_llvm/src/lib.rs | 21 |
2 files changed, 18 insertions, 6 deletions
diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 8655aeec13d..db297425b03 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -25,6 +25,7 @@ use std::ffi::{CStr, CString}; use std::fs::File; use std::io; use std::iter; +use std::mem::ManuallyDrop; use std::path::Path; use std::slice; use std::sync::Arc; @@ -734,7 +735,7 @@ pub unsafe fn optimize_thin_module( let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names); let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &diag_handler)? as *const _; let mut module = ModuleCodegen { - module_llvm: ModuleLlvm { llmod_raw, llcx, tm }, + module_llvm: ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) }, name: thin_module.name().to_string(), kind: ModuleKind::Regular, }; diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index f8a0423e9b1..915cf31de08 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -52,6 +52,7 @@ use rustc_span::symbol::Symbol; use std::any::Any; use std::ffi::CStr; use std::io::Write; +use std::mem::ManuallyDrop; mod back { pub mod archive; @@ -407,8 +408,9 @@ pub struct ModuleLlvm { llcx: &'static mut llvm::Context, llmod_raw: *const llvm::Module, - // independent from llcx and llmod_raw, resources get disposed by drop impl - tm: OwnedTargetMachine, + // This field is `ManuallyDrop` because it is important that the `TargetMachine` + // is disposed prior to the `Context` being disposed otherwise UAFs can occur. + tm: ManuallyDrop<OwnedTargetMachine>, } unsafe impl Send for ModuleLlvm {} @@ -419,7 +421,11 @@ impl ModuleLlvm { unsafe { let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names()); let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _; - ModuleLlvm { llmod_raw, llcx, tm: create_target_machine(tcx, mod_name) } + ModuleLlvm { + llmod_raw, + llcx, + tm: ManuallyDrop::new(create_target_machine(tcx, mod_name)), + } } } @@ -427,7 +433,11 @@ impl ModuleLlvm { unsafe { let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names()); let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _; - ModuleLlvm { llmod_raw, llcx, tm: create_informational_target_machine(tcx.sess) } + ModuleLlvm { + llmod_raw, + llcx, + tm: ManuallyDrop::new(create_informational_target_machine(tcx.sess)), + } } } @@ -448,7 +458,7 @@ impl ModuleLlvm { } }; - Ok(ModuleLlvm { llmod_raw, llcx, tm }) + Ok(ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) }) } } @@ -460,6 +470,7 @@ impl ModuleLlvm { impl Drop for ModuleLlvm { fn drop(&mut self) { unsafe { + ManuallyDrop::drop(&mut self.tm); llvm::LLVMContextDispose(&mut *(self.llcx as *mut _)); } } |
