Skip to content

Commit dcf1e4d

Browse files
committed
Document some safety constraints and use more safe wrappers
1 parent 4b83038 commit dcf1e4d

File tree

11 files changed

+50
-59
lines changed

11 files changed

+50
-59
lines changed

Diff for: compiler/rustc_codegen_llvm/src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ pub(crate) unsafe fn codegen(
8181
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
8282
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
8383
let llval = llvm::LLVMConstInt(i8, val as u64, False);
84-
llvm::LLVMSetInitializer(ll_g, llval);
84+
llvm::set_initializer(ll_g, llval);
8585

8686
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
8787
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_c_char_ptr(), name.len(), i8);
8888
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
8989
let llval = llvm::LLVMConstInt(i8, 0, False);
90-
llvm::LLVMSetInitializer(ll_g, llval);
90+
llvm::set_initializer(ll_g, llval);
9191
}
9292

9393
if tcx.sess.opts.debuginfo != DebugInfo::None {

Diff for: compiler/rustc_codegen_llvm/src/back/archive.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_codegen_ssa::back::archive::{
1111
use rustc_session::Session;
1212

1313
use crate::llvm::archive_ro::{ArchiveRO, Child};
14-
use crate::llvm::{self, ArchiveKind};
14+
use crate::llvm::{self, ArchiveKind, last_error};
1515

1616
/// Helper for adding many files to an archive.
1717
#[must_use = "must call build() to finish building the archive"]
@@ -169,6 +169,8 @@ impl<'a> LlvmArchiveBuilder<'a> {
169169
.unwrap_or_else(|kind| self.sess.dcx().emit_fatal(UnknownArchiveKind { kind }));
170170

171171
let mut additions = mem::take(&mut self.additions);
172+
// Values in the `members` list below will contain pointers to the strings allocated here.
173+
// So they need to get dropped after all elements of `members` get freed.
172174
let mut strings = Vec::new();
173175
let mut members = Vec::new();
174176

@@ -229,12 +231,7 @@ impl<'a> LlvmArchiveBuilder<'a> {
229231
self.sess.target.arch == "arm64ec",
230232
);
231233
let ret = if r.into_result().is_err() {
232-
let err = llvm::LLVMRustGetLastError();
233-
let msg = if err.is_null() {
234-
"failed to write archive".into()
235-
} else {
236-
String::from_utf8_lossy(CStr::from_ptr(err).to_bytes())
237-
};
234+
let msg = last_error().unwrap_or_else(|| "failed to write archive".into());
238235
Err(io::Error::new(io::ErrorKind::Other, msg))
239236
} else {
240237
Ok(!members.is_empty())

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+28-36
Original file line numberDiff line numberDiff line change
@@ -1049,24 +1049,18 @@ unsafe fn embed_bitcode(
10491049
{
10501050
// We don't need custom section flags, create LLVM globals.
10511051
let llconst = common::bytes_in_context(llcx, bitcode);
1052-
let llglobal = llvm::LLVMAddGlobal(
1053-
llmod,
1054-
common::val_ty(llconst),
1055-
c"rustc.embedded.module".as_ptr(),
1056-
);
1057-
llvm::LLVMSetInitializer(llglobal, llconst);
1052+
let llglobal =
1053+
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1054+
llvm::set_initializer(llglobal, llconst);
10581055

10591056
llvm::set_section(llglobal, bitcode_section_name(cgcx));
10601057
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
10611058
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
10621059

10631060
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1064-
let llglobal = llvm::LLVMAddGlobal(
1065-
llmod,
1066-
common::val_ty(llconst),
1067-
c"rustc.embedded.cmdline".as_ptr(),
1068-
);
1069-
llvm::LLVMSetInitializer(llglobal, llconst);
1061+
let llglobal =
1062+
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1063+
llvm::set_initializer(llglobal, llconst);
10701064
let section = if cgcx.target_is_like_osx {
10711065
c"__LLVM,__cmdline"
10721066
} else if cgcx.target_is_like_aix {
@@ -1106,31 +1100,29 @@ fn create_msvc_imps(
11061100
// underscores added in front).
11071101
let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" };
11081102

1109-
unsafe {
1110-
let ptr_ty = Type::ptr_llcx(llcx);
1111-
let globals = base::iter_globals(llmod)
1112-
.filter(|&val| {
1113-
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage
1114-
&& llvm::LLVMIsDeclaration(val) == 0
1115-
})
1116-
.filter_map(|val| {
1117-
// Exclude some symbols that we know are not Rust symbols.
1118-
let name = llvm::get_value_name(val);
1119-
if ignored(name) { None } else { Some((val, name)) }
1120-
})
1121-
.map(move |(val, name)| {
1122-
let mut imp_name = prefix.as_bytes().to_vec();
1123-
imp_name.extend(name);
1124-
let imp_name = CString::new(imp_name).unwrap();
1125-
(imp_name, val)
1126-
})
1127-
.collect::<Vec<_>>();
1103+
let ptr_ty = Type::ptr_llcx(llcx);
1104+
let globals = base::iter_globals(llmod)
1105+
.filter(|&val| {
1106+
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage && !llvm::is_declaration(val)
1107+
})
1108+
.filter_map(|val| {
1109+
// Exclude some symbols that we know are not Rust symbols.
1110+
let name = llvm::get_value_name(val);
1111+
if ignored(name) { None } else { Some((val, name)) }
1112+
})
1113+
.map(move |(val, name)| {
1114+
let mut imp_name = prefix.as_bytes().to_vec();
1115+
imp_name.extend(name);
1116+
let imp_name = CString::new(imp_name).unwrap();
1117+
(imp_name, val)
1118+
})
1119+
.collect::<Vec<_>>();
11281120

1129-
for (imp_name, val) in globals {
1130-
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr());
1131-
llvm::LLVMSetInitializer(imp, val);
1132-
llvm::set_linkage(imp, llvm::Linkage::ExternalLinkage);
1133-
}
1121+
for (imp_name, val) in globals {
1122+
let imp = llvm::add_global(llmod, ptr_ty, &imp_name);
1123+
1124+
llvm::set_initializer(imp, val);
1125+
llvm::set_linkage(imp, llvm::Linkage::ExternalLinkage);
11341126
}
11351127

11361128
// Use this function to exclude certain symbols from `__imp` generation.

Diff for: compiler/rustc_codegen_llvm/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
219219
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
220220
bug!("symbol `{}` is already defined", sym);
221221
});
222+
llvm::set_initializer(g, sc);
222223
unsafe {
223-
llvm::LLVMSetInitializer(g, sc);
224224
llvm::LLVMSetGlobalConstant(g, True);
225225
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
226226
}

Diff for: compiler/rustc_codegen_llvm/src/consts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
191191
})
192192
});
193193
llvm::set_linkage(g2, llvm::Linkage::InternalLinkage);
194-
unsafe { llvm::LLVMSetInitializer(g2, g1) };
194+
llvm::set_initializer(g2, g1);
195195
g2
196196
} else if cx.tcx.sess.target.arch == "x86"
197197
&& common::is_mingw_gnu_toolchain(&cx.tcx.sess.target)
@@ -235,7 +235,7 @@ impl<'ll> CodegenCx<'ll, '_> {
235235
}
236236
_ => self.define_private_global(self.val_ty(cv)),
237237
};
238-
unsafe { llvm::LLVMSetInitializer(gv, cv) };
238+
llvm::set_initializer(gv, cv);
239239
set_global_alignment(self, gv, align);
240240
llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
241241
gv
@@ -458,7 +458,7 @@ impl<'ll> CodegenCx<'ll, '_> {
458458
new_g
459459
};
460460
set_global_alignment(self, g, alloc.align);
461-
llvm::LLVMSetInitializer(g, v);
461+
llvm::set_initializer(g, v);
462462

463463
if self.should_assume_dso_local(g, true) {
464464
llvm::LLVMRustSetDSOLocal(g, true);

Diff for: compiler/rustc_codegen_llvm/src/context.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,10 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
616616
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
617617
let array = self.const_array(self.type_ptr(), values);
618618

619-
unsafe {
620-
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
621-
llvm::LLVMSetInitializer(g, array);
622-
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
623-
llvm::set_section(g, c"llvm.metadata");
624-
}
619+
let g = llvm::add_global(self.llmod, self.val_ty(array), name);
620+
llvm::set_initializer(g, array);
621+
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
622+
llvm::set_section(g, c"llvm.metadata");
625623
}
626624
}
627625
impl<'ll> SimpleCx<'ll> {

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
7373
.define_global(section_var_name, llvm_type)
7474
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
7575
llvm::set_section(section_var, c".debug_gdb_scripts");
76-
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
76+
llvm::set_initializer(section_var, cx.const_bytes(section_contents));
7777
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7878
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
7979
llvm::set_linkage(section_var, llvm::Linkage::LinkOnceODRLinkage);

Diff for: compiler/rustc_codegen_llvm/src/declare.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
235235
/// name.
236236
pub(crate) fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
237237
self.get_declared_value(name).and_then(|val| {
238-
let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
238+
let declaration = llvm::is_declaration(val);
239239
if !declaration { Some(val) } else { None }
240240
})
241241
}

Diff for: compiler/rustc_codegen_llvm/src/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ fn codegen_msvc_try<'ll>(
824824
if bx.cx.tcx.sess.target.supports_comdat() {
825825
llvm::SetUniqueComdat(bx.llmod, tydesc);
826826
}
827-
unsafe { llvm::LLVMSetInitializer(tydesc, type_info) };
827+
llvm::set_initializer(tydesc, type_info);
828828

829829
// The flag value of 8 indicates that we are catching the exception by
830830
// reference instead of by value. We can't use catch by value because

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,7 @@ unsafe extern "C" {
23592359
);
23602360
pub fn LLVMRustWriteOutputFile<'a>(
23612361
T: &'a TargetMachine,
2362-
PM: &PassManager<'a>,
2362+
PM: *mut PassManager<'a>,
23632363
M: &'a Module,
23642364
Output: *const c_char,
23652365
DwoOutput: *const c_char,

Diff for: compiler/rustc_codegen_llvm/src/llvm/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
241241
}
242242
}
243243

244+
pub fn is_declaration(llglobal: &Value) -> bool {
245+
unsafe { LLVMIsDeclaration(llglobal) == ffi::True }
246+
}
247+
244248
pub fn get_visibility(llglobal: &Value) -> Visibility {
245249
unsafe { LLVMGetVisibility(llglobal) }.to_rust()
246250
}

0 commit comments

Comments
 (0)