Skip to content

Commit b21c438

Browse files
authored
Rollup merge of rust-lang#146635 - Zalathar:llvm-cov, r=SparrowLii
cg_llvm: Stop using `as_c_char_ptr` for coverage-related bindings [As explained by a note in `ffi.rs`](https://github.com/rust-lang/rust/blob/8a1b39995e5b630c5872f5de5079f1f569bd5ac2/compiler/rustc_codegen_llvm/src/llvm/ffi.rs#L4-L11), passing strings and byte slices through FFI is more convenient if we take advantage of the fact that `*const c_uchar` and `*const c_char` have the same ABI. Doing so avoids having to rely on a special helper function, since we can just call `as_ptr` instead. (The same logic applies to every other binding that currently uses the `as_c_char_ptr` helper; I just haven't adjusted all of them yet.) --- As a drive-by change, this PR also marks some coverage-related FFI bindings as `safe`.
2 parents 014e0af + 06a7460 commit b21c438

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22
33
use std::ffi::CString;
44

5-
use crate::common::AsCCharPtr;
65
use crate::coverageinfo::ffi;
76
use crate::llvm;
87

98
pub(crate) fn covmap_var_name() -> CString {
10-
CString::new(llvm::build_byte_buffer(|s| unsafe {
9+
CString::new(llvm::build_byte_buffer(|s| {
1110
llvm::LLVMRustCoverageWriteCovmapVarNameToString(s);
1211
}))
1312
.expect("covmap variable name should not contain NUL")
1413
}
1514

1615
pub(crate) fn covmap_section_name(llmod: &llvm::Module) -> CString {
17-
CString::new(llvm::build_byte_buffer(|s| unsafe {
16+
CString::new(llvm::build_byte_buffer(|s| {
1817
llvm::LLVMRustCoverageWriteCovmapSectionNameToString(llmod, s);
1918
}))
2019
.expect("covmap section name should not contain NUL")
2120
}
2221

2322
pub(crate) fn covfun_section_name(llmod: &llvm::Module) -> CString {
24-
CString::new(llvm::build_byte_buffer(|s| unsafe {
23+
CString::new(llvm::build_byte_buffer(|s| {
2524
llvm::LLVMRustCoverageWriteCovfunSectionNameToString(llmod, s);
2625
}))
2726
.expect("covfun section name should not contain NUL")
@@ -34,7 +33,7 @@ pub(crate) fn create_pgo_func_name_var<'ll>(
3433
unsafe {
3534
llvm::LLVMRustCoverageCreatePGOFuncNameVar(
3635
llfn,
37-
mangled_fn_name.as_c_char_ptr(),
36+
mangled_fn_name.as_ptr(),
3837
mangled_fn_name.len(),
3938
)
4039
}
@@ -44,7 +43,7 @@ pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef<str>]) -> Vec<u8
4443
let (pointers, lengths) = filenames
4544
.into_iter()
4645
.map(AsRef::as_ref)
47-
.map(|s: &str| (s.as_c_char_ptr(), s.len()))
46+
.map(|s: &str| (s.as_ptr(), s.len()))
4847
.unzip::<_, _, Vec<_>, Vec<_>>();
4948

5049
llvm::build_byte_buffer(|buffer| unsafe {
@@ -89,12 +88,12 @@ pub(crate) fn write_function_mappings_to_buffer(
8988
/// Hashes some bytes into a 64-bit hash, via LLVM's `IndexedInstrProf::ComputeHash`,
9089
/// as required for parts of the LLVM coverage mapping format.
9190
pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
92-
unsafe { llvm::LLVMRustCoverageHashBytes(bytes.as_c_char_ptr(), bytes.len()) }
91+
unsafe { llvm::LLVMRustCoverageHashBytes(bytes.as_ptr(), bytes.len()) }
9392
}
9493

9594
/// Returns LLVM's `coverage::CovMapVersion::CurrentVersion` (CoverageMapping.h)
9695
/// as a raw numeric value. For historical reasons, the numeric value is 1 less
9796
/// than the number in the version's name, so `Version7` is actually `6u32`.
9897
pub(crate) fn mapping_version() -> u32 {
99-
unsafe { llvm::LLVMRustCoverageMappingVersion() }
98+
llvm::LLVMRustCoverageMappingVersion()
10099
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,8 +2256,11 @@ unsafe extern "C" {
22562256
ConstraintsLen: size_t,
22572257
) -> bool;
22582258

2259+
/// A list of pointer-length strings is passed as two pointer-length slices,
2260+
/// one slice containing pointers and one slice containing their corresponding
2261+
/// lengths. The implementation will check that both slices have the same length.
22592262
pub(crate) fn LLVMRustCoverageWriteFilenamesToBuffer(
2260-
Filenames: *const *const c_char,
2263+
Filenames: *const *const c_uchar, // See "PTR_LEN_STR".
22612264
FilenamesLen: size_t,
22622265
Lengths: *const size_t,
22632266
LengthsLen: size_t,
@@ -2280,18 +2283,25 @@ unsafe extern "C" {
22802283

22812284
pub(crate) fn LLVMRustCoverageCreatePGOFuncNameVar(
22822285
F: &Value,
2283-
FuncName: *const c_char,
2286+
FuncName: *const c_uchar, // See "PTR_LEN_STR".
22842287
FuncNameLen: size_t,
22852288
) -> &Value;
2286-
pub(crate) fn LLVMRustCoverageHashBytes(Bytes: *const c_char, NumBytes: size_t) -> u64;
2289+
pub(crate) fn LLVMRustCoverageHashBytes(
2290+
Bytes: *const c_uchar, // See "PTR_LEN_STR".
2291+
NumBytes: size_t,
2292+
) -> u64;
22872293

2288-
pub(crate) fn LLVMRustCoverageWriteCovmapSectionNameToString(M: &Module, OutStr: &RustString);
2289-
2290-
pub(crate) fn LLVMRustCoverageWriteCovfunSectionNameToString(M: &Module, OutStr: &RustString);
2291-
2292-
pub(crate) fn LLVMRustCoverageWriteCovmapVarNameToString(OutStr: &RustString);
2294+
pub(crate) safe fn LLVMRustCoverageWriteCovmapSectionNameToString(
2295+
M: &Module,
2296+
OutStr: &RustString,
2297+
);
2298+
pub(crate) safe fn LLVMRustCoverageWriteCovfunSectionNameToString(
2299+
M: &Module,
2300+
OutStr: &RustString,
2301+
);
2302+
pub(crate) safe fn LLVMRustCoverageWriteCovmapVarNameToString(OutStr: &RustString);
22932303

2294-
pub(crate) fn LLVMRustCoverageMappingVersion() -> u32;
2304+
pub(crate) safe fn LLVMRustCoverageMappingVersion() -> u32;
22952305
pub(crate) fn LLVMRustDebugMetadataVersion() -> u32;
22962306
pub(crate) fn LLVMRustVersionMajor() -> u32;
22972307
pub(crate) fn LLVMRustVersionMinor() -> u32;

0 commit comments

Comments
 (0)