Skip to content

Commit 28503d6

Browse files
Fix unsafe_op_in_unsafe_fn in compiler
1 parent 71eb49c commit 28503d6

File tree

15 files changed

+383
-308
lines changed

15 files changed

+383
-308
lines changed

compiler/rustc_abi/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl Step for Size {
627627

628628
#[inline]
629629
unsafe fn forward_unchecked(start: Self, count: usize) -> Self {
630-
Self::from_bytes(u64::forward_unchecked(start.bytes(), count))
630+
Self::from_bytes(unsafe { u64::forward_unchecked(start.bytes(), count) })
631631
}
632632

633633
#[inline]
@@ -642,7 +642,7 @@ impl Step for Size {
642642

643643
#[inline]
644644
unsafe fn backward_unchecked(start: Self, count: usize) -> Self {
645-
Self::from_bytes(u64::backward_unchecked(start.bytes(), count))
645+
Self::from_bytes(unsafe { u64::backward_unchecked(start.bytes(), count) })
646646
}
647647
}
648648

compiler/rustc_codegen_llvm/src/allocator.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ pub(crate) unsafe fn codegen(
2121
) {
2222
let llcx = &*module_llvm.llcx;
2323
let llmod = module_llvm.llmod();
24-
let usize = match tcx.sess.target.pointer_width {
25-
16 => llvm::LLVMInt16TypeInContext(llcx),
26-
32 => llvm::LLVMInt32TypeInContext(llcx),
27-
64 => llvm::LLVMInt64TypeInContext(llcx),
28-
tws => bug!("Unsupported target word size for int: {}", tws),
24+
let usize = unsafe {
25+
match tcx.sess.target.pointer_width {
26+
16 => llvm::LLVMInt16TypeInContext(llcx),
27+
32 => llvm::LLVMInt32TypeInContext(llcx),
28+
64 => llvm::LLVMInt64TypeInContext(llcx),
29+
tws => bug!("Unsupported target word size for int: {}", tws),
30+
}
2931
};
30-
let i8 = llvm::LLVMInt8TypeInContext(llcx);
31-
let i8p = llvm::LLVMPointerTypeInContext(llcx, 0);
32+
let i8 = unsafe { llvm::LLVMInt8TypeInContext(llcx) };
33+
let i8p = unsafe { llvm::LLVMPointerTypeInContext(llcx, 0) };
3234

3335
if kind == AllocatorKind::Default {
3436
for method in ALLOCATOR_METHODS {
@@ -73,23 +75,25 @@ pub(crate) unsafe fn codegen(
7375
true,
7476
);
7577

76-
// __rust_alloc_error_handler_should_panic
77-
let name = OomStrategy::SYMBOL;
78-
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
79-
if tcx.sess.default_hidden_visibility() {
80-
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
81-
}
82-
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
83-
let llval = llvm::LLVMConstInt(i8, val as u64, False);
84-
llvm::LLVMSetInitializer(ll_g, llval);
85-
86-
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
87-
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
88-
if tcx.sess.default_hidden_visibility() {
89-
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
78+
unsafe {
79+
// __rust_alloc_error_handler_should_panic
80+
let name = OomStrategy::SYMBOL;
81+
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
82+
if tcx.sess.default_hidden_visibility() {
83+
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
84+
}
85+
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
86+
let llval = llvm::LLVMConstInt(i8, val as u64, False);
87+
llvm::LLVMSetInitializer(ll_g, llval);
88+
89+
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
90+
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
91+
if tcx.sess.default_hidden_visibility() {
92+
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
93+
}
94+
let llval = llvm::LLVMConstInt(i8, 0, False);
95+
llvm::LLVMSetInitializer(ll_g, llval);
9096
}
91-
let llval = llvm::LLVMConstInt(i8, 0, False);
92-
llvm::LLVMSetInitializer(ll_g, llval);
9397

9498
if tcx.sess.opts.debuginfo != DebugInfo::None {
9599
let dbg_cx = debuginfo::CodegenUnitDebugContext::new(llmod);

compiler/rustc_codegen_llvm/src/back/lto.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ pub unsafe fn optimize_thin_module(
727727
// into that context. One day, however, we may do this for upstream
728728
// crates but for locally codegened modules we may be able to reuse
729729
// that LLVM Context and Module.
730-
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
730+
let llcx = unsafe { llvm::LLVMRustContextCreate(cgcx.fewer_names) };
731731
let llmod_raw = parse_module(llcx, module_name, thin_module.data(), dcx)? as *const _;
732732
let mut module = ModuleCodegen {
733733
module_llvm: ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) },
@@ -750,7 +750,9 @@ pub unsafe fn optimize_thin_module(
750750
{
751751
let _timer =
752752
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_rename", thin_module.name());
753-
if !llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod, target) {
753+
if unsafe {
754+
!llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod, target)
755+
} {
754756
return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
755757
}
756758
save_temp_bitcode(cgcx, &module, "thin-lto-after-rename");
@@ -760,7 +762,8 @@ pub unsafe fn optimize_thin_module(
760762
let _timer = cgcx
761763
.prof
762764
.generic_activity_with_arg("LLVM_thin_lto_resolve_weak", thin_module.name());
763-
if !llvm::LLVMRustPrepareThinLTOResolveWeak(thin_module.shared.data.0, llmod) {
765+
if unsafe { !llvm::LLVMRustPrepareThinLTOResolveWeak(thin_module.shared.data.0, llmod) }
766+
{
764767
return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
765768
}
766769
save_temp_bitcode(cgcx, &module, "thin-lto-after-resolve");
@@ -770,7 +773,8 @@ pub unsafe fn optimize_thin_module(
770773
let _timer = cgcx
771774
.prof
772775
.generic_activity_with_arg("LLVM_thin_lto_internalize", thin_module.name());
773-
if !llvm::LLVMRustPrepareThinLTOInternalize(thin_module.shared.data.0, llmod) {
776+
if unsafe { !llvm::LLVMRustPrepareThinLTOInternalize(thin_module.shared.data.0, llmod) }
777+
{
774778
return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
775779
}
776780
save_temp_bitcode(cgcx, &module, "thin-lto-after-internalize");
@@ -779,7 +783,9 @@ pub unsafe fn optimize_thin_module(
779783
{
780784
let _timer =
781785
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_import", thin_module.name());
782-
if !llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod, target) {
786+
if unsafe {
787+
!llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod, target)
788+
} {
783789
return Err(write::llvm_err(dcx, LlvmError::PrepareThinLtoModule));
784790
}
785791
save_temp_bitcode(cgcx, &module, "thin-lto-after-import");

compiler/rustc_codegen_llvm/src/back/profiling.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ pub unsafe extern "C" fn selfprofile_before_pass_callback(
4646
pass_name: *const c_char,
4747
ir_name: *const c_char,
4848
) {
49-
let llvm_self_profiler = &mut *(llvm_self_profiler as *mut LlvmSelfProfiler<'_>);
50-
let pass_name = CStr::from_ptr(pass_name).to_str().expect("valid UTF-8");
51-
let ir_name = CStr::from_ptr(ir_name).to_str().expect("valid UTF-8");
52-
llvm_self_profiler.before_pass_callback(pass_name, ir_name);
49+
unsafe {
50+
let llvm_self_profiler = &mut *(llvm_self_profiler as *mut LlvmSelfProfiler<'_>);
51+
let pass_name = CStr::from_ptr(pass_name).to_str().expect("valid UTF-8");
52+
let ir_name = CStr::from_ptr(ir_name).to_str().expect("valid UTF-8");
53+
llvm_self_profiler.before_pass_callback(pass_name, ir_name);
54+
}
5355
}
5456

5557
pub unsafe extern "C" fn selfprofile_after_pass_callback(llvm_self_profiler: *mut c_void) {
56-
let llvm_self_profiler = &mut *(llvm_self_profiler as *mut LlvmSelfProfiler<'_>);
58+
let llvm_self_profiler = unsafe { &mut *(llvm_self_profiler as *mut LlvmSelfProfiler<'_>) };
5759
llvm_self_profiler.after_pass_callback();
5860
}

0 commit comments

Comments
 (0)