Skip to content

Commit 81ed7dc

Browse files
committed
Document some safety constraints and use more safe wrappers
1 parent d776ef6 commit 81ed7dc

File tree

11 files changed

+50
-59
lines changed

11 files changed

+50
-59
lines changed

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 {

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())

compiler/rustc_codegen_llvm/src/back/write.rs

+28-36
Original file line numberDiff line numberDiff line change
@@ -1041,24 +1041,18 @@ unsafe fn embed_bitcode(
10411041
{
10421042
// We don't need custom section flags, create LLVM globals.
10431043
let llconst = common::bytes_in_context(llcx, bitcode);
1044-
let llglobal = llvm::LLVMAddGlobal(
1045-
llmod,
1046-
common::val_ty(llconst),
1047-
c"rustc.embedded.module".as_ptr(),
1048-
);
1049-
llvm::LLVMSetInitializer(llglobal, llconst);
1044+
let llglobal =
1045+
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1046+
llvm::set_initializer(llglobal, llconst);
10501047

10511048
llvm::set_section(llglobal, bitcode_section_name(cgcx));
10521049
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
10531050
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
10541051

10551052
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1056-
let llglobal = llvm::LLVMAddGlobal(
1057-
llmod,
1058-
common::val_ty(llconst),
1059-
c"rustc.embedded.cmdline".as_ptr(),
1060-
);
1061-
llvm::LLVMSetInitializer(llglobal, llconst);
1053+
let llglobal =
1054+
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1055+
llvm::set_initializer(llglobal, llconst);
10621056
let section = if cgcx.target_is_like_osx {
10631057
c"__LLVM,__cmdline"
10641058
} else if cgcx.target_is_like_aix {
@@ -1098,31 +1092,29 @@ fn create_msvc_imps(
10981092
// underscores added in front).
10991093
let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" };
11001094

1101-
unsafe {
1102-
let ptr_ty = Type::ptr_llcx(llcx);
1103-
let globals = base::iter_globals(llmod)
1104-
.filter(|&val| {
1105-
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage
1106-
&& llvm::LLVMIsDeclaration(val) == 0
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<_>>();
1095+
let ptr_ty = Type::ptr_llcx(llcx);
1096+
let globals = base::iter_globals(llmod)
1097+
.filter(|&val| {
1098+
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage && llvm::is_declaration(val)
1099+
})
1100+
.filter_map(|val| {
1101+
// Exclude some symbols that we know are not Rust symbols.
1102+
let name = llvm::get_value_name(val);
1103+
if ignored(name) { None } else { Some((val, name)) }
1104+
})
1105+
.map(move |(val, name)| {
1106+
let mut imp_name = prefix.as_bytes().to_vec();
1107+
imp_name.extend(name);
1108+
let imp_name = CString::new(imp_name).unwrap();
1109+
(imp_name, val)
1110+
})
1111+
.collect::<Vec<_>>();
11201112

1121-
for (imp_name, val) in globals {
1122-
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr());
1123-
llvm::LLVMSetInitializer(imp, val);
1124-
llvm::set_linkage(imp, llvm::Linkage::ExternalLinkage);
1125-
}
1113+
for (imp_name, val) in globals {
1114+
let imp = llvm::add_global(llmod, ptr_ty, &imp_name);
1115+
1116+
llvm::set_initializer(imp, val);
1117+
llvm::set_linkage(imp, llvm::Linkage::ExternalLinkage);
11261118
}
11271119

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

compiler/rustc_codegen_llvm/src/common.rs

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

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)
@@ -227,7 +227,7 @@ impl<'ll> CodegenCx<'ll, '_> {
227227
}
228228
_ => self.define_private_global(self.val_ty(cv)),
229229
};
230-
unsafe { llvm::LLVMSetInitializer(gv, cv) };
230+
llvm::set_initializer(gv, cv);
231231
set_global_alignment(self, gv, align);
232232
llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
233233
gv
@@ -416,7 +416,7 @@ impl<'ll> CodegenCx<'ll, '_> {
416416
new_g
417417
};
418418
set_global_alignment(self, g, alloc.align);
419-
llvm::LLVMSetInitializer(g, v);
419+
llvm::set_initializer(g, v);
420420

421421
if self.should_assume_dso_local(g, true) {
422422
llvm::LLVMRustSetDSOLocal(g, true);

compiler/rustc_codegen_llvm/src/context.rs

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

596-
unsafe {
597-
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
598-
llvm::LLVMSetInitializer(g, array);
599-
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
600-
llvm::set_section(g, c"llvm.metadata");
601-
}
596+
let g = llvm::add_global(self.llmod, self.val_ty(array), name);
597+
llvm::set_initializer(g, array);
598+
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
599+
llvm::set_section(g, c"llvm.metadata");
602600
}
603601

604602
pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {

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);

compiler/rustc_codegen_llvm/src/declare.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
217217
/// name.
218218
pub(crate) fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
219219
self.get_declared_value(name).and_then(|val| {
220-
let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
220+
let declaration = llvm::is_declaration(val);
221221
if !declaration { Some(val) } else { None }
222222
})
223223
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

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

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

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ unsafe extern "C" {
22302230
);
22312231
pub fn LLVMRustWriteOutputFile<'a>(
22322232
T: &'a TargetMachine,
2233-
PM: &PassManager<'a>,
2233+
PM: *mut PassManager<'a>,
22342234
M: &'a Module,
22352235
Output: *const c_char,
22362236
DwoOutput: *const c_char,

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
245245
}
246246
}
247247

248+
pub fn is_declaration(llglobal: &Value) -> bool {
249+
unsafe { LLVMIsDeclaration(llglobal) == ffi::True }
250+
}
251+
248252
pub fn get_visibility(llglobal: &Value) -> Visibility {
249253
unsafe { LLVMGetVisibility(llglobal) }.to_rust()
250254
}

0 commit comments

Comments
 (0)