Skip to content

Commit 32f9539

Browse files
committed
Mark all optimize methods and the codegen method as safe
There is no safety contract and I don't think any of them can actually cause UB in more ways than passing malicious source code to rustc can. While LtoModuleCodegen::optimize says that the returned ModuleCodegen points into the LTO module, the LTO module has already been dropped by the time this function returns, so if the returned ModuleCodegen indeed points into the LTO module, we would have seen crashes on every LTO compilation, which we don't. As such the comment is outdated.
1 parent bfb8cf7 commit 32f9539

File tree

7 files changed

+25
-34
lines changed

7 files changed

+25
-34
lines changed

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl WriteBackendMethods for GccCodegenBackend {
391391
unimplemented!()
392392
}
393393

394-
unsafe fn optimize(
394+
fn optimize(
395395
_cgcx: &CodegenContext<Self>,
396396
_dcx: DiagCtxtHandle<'_>,
397397
module: &mut ModuleCodegen<Self::Module>,
@@ -409,14 +409,14 @@ impl WriteBackendMethods for GccCodegenBackend {
409409
Ok(())
410410
}
411411

412-
unsafe fn optimize_thin(
412+
fn optimize_thin(
413413
cgcx: &CodegenContext<Self>,
414414
thin: ThinModule<Self>,
415415
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
416416
back::lto::optimize_thin_module(thin, cgcx)
417417
}
418418

419-
unsafe fn codegen(
419+
fn codegen(
420420
cgcx: &CodegenContext<Self>,
421421
dcx: DiagCtxtHandle<'_>,
422422
module: ModuleCodegen<Self::Module>,

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl Drop for ThinBuffer {
799799
}
800800
}
801801

802-
pub(crate) unsafe fn optimize_thin_module(
802+
pub(crate) fn optimize_thin_module(
803803
thin_module: ThinModule<LlvmCodegenBackend>,
804804
cgcx: &CodegenContext<LlvmCodegenBackend>,
805805
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub(crate) unsafe fn llvm_optimize(
704704
}
705705

706706
// Unsafe due to LLVM calls.
707-
pub(crate) unsafe fn optimize(
707+
pub(crate) fn optimize(
708708
cgcx: &CodegenContext<LlvmCodegenBackend>,
709709
dcx: DiagCtxtHandle<'_>,
710710
module: &mut ModuleCodegen<ModuleLlvm>,
@@ -815,7 +815,7 @@ pub(crate) fn link(
815815
Ok(modules.remove(0))
816816
}
817817

818-
pub(crate) unsafe fn codegen(
818+
pub(crate) fn codegen(
819819
cgcx: &CodegenContext<LlvmCodegenBackend>,
820820
dcx: DiagCtxtHandle<'_>,
821821
module: ModuleCodegen<ModuleLlvm>,

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ impl WriteBackendMethods for LlvmCodegenBackend {
189189
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
190190
back::lto::run_thin(cgcx, modules, cached_modules)
191191
}
192-
unsafe fn optimize(
192+
fn optimize(
193193
cgcx: &CodegenContext<Self>,
194194
dcx: DiagCtxtHandle<'_>,
195195
module: &mut ModuleCodegen<Self::Module>,
196196
config: &ModuleConfig,
197197
) -> Result<(), FatalError> {
198-
unsafe { back::write::optimize(cgcx, dcx, module, config) }
198+
back::write::optimize(cgcx, dcx, module, config)
199199
}
200200
fn optimize_fat(
201201
cgcx: &CodegenContext<Self>,
@@ -205,19 +205,19 @@ impl WriteBackendMethods for LlvmCodegenBackend {
205205
let dcx = dcx.handle();
206206
back::lto::run_pass_manager(cgcx, dcx, module, false)
207207
}
208-
unsafe fn optimize_thin(
208+
fn optimize_thin(
209209
cgcx: &CodegenContext<Self>,
210210
thin: ThinModule<Self>,
211211
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
212-
unsafe { back::lto::optimize_thin_module(thin, cgcx) }
212+
back::lto::optimize_thin_module(thin, cgcx)
213213
}
214-
unsafe fn codegen(
214+
fn codegen(
215215
cgcx: &CodegenContext<Self>,
216216
dcx: DiagCtxtHandle<'_>,
217217
module: ModuleCodegen<Self::Module>,
218218
config: &ModuleConfig,
219219
) -> Result<CompiledModule, FatalError> {
220-
unsafe { back::write::codegen(cgcx, dcx, module, config) }
220+
back::write::codegen(cgcx, dcx, module, config)
221221
}
222222
fn prepare_thin(
223223
module: ModuleCodegen<Self::Module>,

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
5656
}
5757

5858
/// Optimize this module within the given codegen context.
59-
///
60-
/// This function is unsafe as it'll return a `ModuleCodegen` still
61-
/// points to LLVM data structures owned by this `LtoModuleCodegen`.
62-
/// It's intended that the module returned is immediately code generated and
63-
/// dropped, and then this LTO module is dropped.
64-
pub unsafe fn optimize(
59+
pub fn optimize(
6560
self,
6661
cgcx: &CodegenContext<B>,
6762
) -> Result<ModuleCodegen<B::Module>, FatalError> {
@@ -70,7 +65,7 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
7065
B::optimize_fat(cgcx, &mut module)?;
7166
Ok(module)
7267
}
73-
LtoModuleCodegen::Thin(thin) => unsafe { B::optimize_thin(cgcx, thin) },
68+
LtoModuleCodegen::Thin(thin) => B::optimize_thin(cgcx, thin),
7469
}
7570
}
7671

@@ -85,7 +80,7 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
8580
}
8681

8782
/// Run autodiff on Fat LTO module
88-
pub unsafe fn autodiff(
83+
pub fn autodiff(
8984
self,
9085
cgcx: &CodegenContext<B>,
9186
diff_fncs: Vec<AutoDiffItem>,

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ fn generate_lto_work<B: ExtraBackendMethods>(
416416
B::run_fat_lto(cgcx, needs_fat_lto, import_only_modules).unwrap_or_else(|e| e.raise());
417417
if cgcx.lto == Lto::Fat && !autodiff.is_empty() {
418418
let config = cgcx.config(ModuleKind::Regular);
419-
module =
420-
unsafe { module.autodiff(cgcx, autodiff, config).unwrap_or_else(|e| e.raise()) };
419+
module = module.autodiff(cgcx, autodiff, config).unwrap_or_else(|e| e.raise());
421420
}
422421
// We are adding a single work item, so the cost doesn't matter.
423422
vec![(WorkItem::LTO(module), 0)]
@@ -887,9 +886,7 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
887886
let dcx = cgcx.create_dcx();
888887
let dcx = dcx.handle();
889888

890-
unsafe {
891-
B::optimize(cgcx, dcx, &mut module, module_config)?;
892-
}
889+
B::optimize(cgcx, dcx, &mut module, module_config)?;
893890

894891
// After we've done the initial round of optimizations we need to
895892
// decide whether to synchronously codegen this module or ship it
@@ -1020,7 +1017,7 @@ fn execute_lto_work_item<B: ExtraBackendMethods>(
10201017
module: lto::LtoModuleCodegen<B>,
10211018
module_config: &ModuleConfig,
10221019
) -> Result<WorkItemResult<B>, FatalError> {
1023-
let module = unsafe { module.optimize(cgcx)? };
1020+
let module = module.optimize(cgcx)?;
10241021
finish_intra_module_work(cgcx, module, module_config)
10251022
}
10261023

@@ -1036,7 +1033,7 @@ fn finish_intra_module_work<B: ExtraBackendMethods>(
10361033
|| module.kind == ModuleKind::Metadata
10371034
|| module.kind == ModuleKind::Allocator
10381035
{
1039-
let module = unsafe { B::codegen(cgcx, dcx, module, module_config)? };
1036+
let module = B::codegen(cgcx, dcx, module, module_config)?;
10401037
Ok(WorkItemResult::Finished(module))
10411038
} else {
10421039
Ok(WorkItemResult::NeedsLink(module))
@@ -1725,9 +1722,8 @@ fn start_executing_work<B: ExtraBackendMethods>(
17251722
let dcx = cgcx.create_dcx();
17261723
let dcx = dcx.handle();
17271724
let module = B::run_link(&cgcx, dcx, needs_link).map_err(|_| ())?;
1728-
let module = unsafe {
1729-
B::codegen(&cgcx, dcx, module, cgcx.config(ModuleKind::Regular)).map_err(|_| ())?
1730-
};
1725+
let module =
1726+
B::codegen(&cgcx, dcx, module, cgcx.config(ModuleKind::Regular)).map_err(|_| ())?;
17311727
compiled_modules.push(module);
17321728
}
17331729

compiler/rustc_codegen_ssa/src/traits/write.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
66
use crate::back::write::{CodegenContext, FatLtoInput, ModuleConfig};
77
use crate::{CompiledModule, ModuleCodegen};
88

9-
pub trait WriteBackendMethods: 'static + Sized + Clone {
9+
pub trait WriteBackendMethods: Clone + 'static {
1010
type Module: Send + Sync;
1111
type TargetMachine;
1212
type TargetMachineError;
@@ -37,7 +37,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
3737
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError>;
3838
fn print_pass_timings(&self);
3939
fn print_statistics(&self);
40-
unsafe fn optimize(
40+
fn optimize(
4141
cgcx: &CodegenContext<Self>,
4242
dcx: DiagCtxtHandle<'_>,
4343
module: &mut ModuleCodegen<Self::Module>,
@@ -47,11 +47,11 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
4747
cgcx: &CodegenContext<Self>,
4848
llmod: &mut ModuleCodegen<Self::Module>,
4949
) -> Result<(), FatalError>;
50-
unsafe fn optimize_thin(
50+
fn optimize_thin(
5151
cgcx: &CodegenContext<Self>,
5252
thin: ThinModule<Self>,
5353
) -> Result<ModuleCodegen<Self::Module>, FatalError>;
54-
unsafe fn codegen(
54+
fn codegen(
5555
cgcx: &CodegenContext<Self>,
5656
dcx: DiagCtxtHandle<'_>,
5757
module: ModuleCodegen<Self::Module>,

0 commit comments

Comments
 (0)