Skip to content

Commit 0a8da9b

Browse files
committed
Document some safety constraints and use more safe wrappers
1 parent 0a374b0 commit 0a8da9b

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
@@ -1038,24 +1038,18 @@ unsafe fn embed_bitcode(
10381038
{
10391039
// We don't need custom section flags, create LLVM globals.
10401040
let llconst = common::bytes_in_context(llcx, bitcode);
1041-
let llglobal = llvm::LLVMAddGlobal(
1042-
llmod,
1043-
common::val_ty(llconst),
1044-
c"rustc.embedded.module".as_ptr(),
1045-
);
1046-
llvm::LLVMSetInitializer(llglobal, llconst);
1041+
let llglobal =
1042+
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1043+
llvm::set_initializer(llglobal, llconst);
10471044

10481045
llvm::set_section(llglobal, bitcode_section_name(cgcx));
10491046
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
10501047
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
10511048

10521049
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1053-
let llglobal = llvm::LLVMAddGlobal(
1054-
llmod,
1055-
common::val_ty(llconst),
1056-
c"rustc.embedded.cmdline".as_ptr(),
1057-
);
1058-
llvm::LLVMSetInitializer(llglobal, llconst);
1050+
let llglobal =
1051+
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1052+
llvm::set_initializer(llglobal, llconst);
10591053
let section = if cgcx.target_is_like_osx {
10601054
c"__LLVM,__cmdline"
10611055
} else if cgcx.target_is_like_aix {
@@ -1095,31 +1089,29 @@ fn create_msvc_imps(
10951089
// underscores added in front).
10961090
let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" };
10971091

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

1118-
for (imp_name, val) in globals {
1119-
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr());
1120-
llvm::LLVMSetInitializer(imp, val);
1121-
llvm::set_linkage(imp, llvm::Linkage::ExternalLinkage);
1122-
}
1110+
for (imp_name, val) in globals {
1111+
let imp = llvm::add_global(llmod, ptr_ty, &imp_name);
1112+
1113+
llvm::set_initializer(imp, val);
1114+
llvm::set_linkage(imp, llvm::Linkage::ExternalLinkage);
11231115
}
11241116

11251117
// 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)
@@ -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);

Diff for: 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 {

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
@@ -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
}

Diff for: 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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ unsafe extern "C" {
22462246
);
22472247
pub fn LLVMRustWriteOutputFile<'a>(
22482248
T: &'a TargetMachine,
2249-
PM: &PassManager<'a>,
2249+
PM: *mut PassManager<'a>,
22502250
M: &'a Module,
22512251
Output: *const c_char,
22522252
DwoOutput: *const c_char,

Diff for: 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)