Skip to content

Commit 58387a0

Browse files
committed
Use a dedicated safe wrapper for LLVMRustGetHostCPUName
1 parent 915bee9 commit 58387a0

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ unsafe extern "C" {
22042204
Desc: &mut *const c_char,
22052205
);
22062206

2207-
pub fn LLVMRustGetHostCPUName(len: *mut usize) -> *const c_char;
2207+
pub fn LLVMRustGetHostCPUName(LenOut: &mut size_t) -> *const u8;
22082208

22092209
// This function makes copies of pointed to data, so the data's lifetime may end after this
22102210
// function returns.

compiler/rustc_codegen_llvm/src/llvm_util.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -476,23 +476,31 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut String, sess: &Session) {
476476
}
477477
}
478478

479-
fn handle_native(name: &str) -> &str {
480-
if name != "native" {
481-
return name;
482-
}
483-
484-
unsafe {
485-
let mut len = 0;
479+
/// Returns the host CPU name, according to LLVM.
480+
fn get_host_cpu_name() -> &'static str {
481+
let mut len = 0;
482+
// SAFETY: The underlying C++ global function returns a `StringRef` that
483+
// isn't tied to any particular backing buffer, so it must be 'static.
484+
let slice: &'static [u8] = unsafe {
486485
let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
487-
str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
486+
assert!(!ptr.is_null());
487+
slice::from_raw_parts(ptr, len)
488+
};
489+
str::from_utf8(slice).expect("host CPU name should be UTF-8")
490+
}
491+
492+
/// If the given string is `"native"`, returns the host CPU name according to
493+
/// LLVM. Otherwise, the string is returned as-is.
494+
fn handle_native(cpu_name: &str) -> &str {
495+
match cpu_name {
496+
"native" => get_host_cpu_name(),
497+
_ => cpu_name,
488498
}
489499
}
490500

491501
pub(crate) fn target_cpu(sess: &Session) -> &str {
492-
match sess.opts.cg.target_cpu {
493-
Some(ref name) => handle_native(name),
494-
None => handle_native(sess.target.cpu.as_ref()),
495-
}
502+
let cpu_name = sess.opts.cg.target_cpu.as_deref().unwrap_or_else(|| &sess.target.cpu);
503+
handle_native(cpu_name)
496504
}
497505

498506
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ extern "C" void LLVMRustGetTargetFeature(LLVMTargetMachineRef TM, size_t Index,
382382
*Desc = Feat.Desc;
383383
}
384384

385-
extern "C" const char *LLVMRustGetHostCPUName(size_t *len) {
385+
extern "C" const char *LLVMRustGetHostCPUName(size_t *OutLen) {
386386
StringRef Name = sys::getHostCPUName();
387-
*len = Name.size();
387+
*OutLen = Name.size();
388388
return Name.data();
389389
}
390390

0 commit comments

Comments
 (0)