Skip to content

Commit 4b154bc

Browse files
committed
coverage: Don't convert symbol names to CString for FFI
1 parent e184118 commit 4b154bc

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use rustc_middle::ty::Instance;
2525
use rustc_middle::ty::Ty;
2626

2727
use std::cell::RefCell;
28-
use std::ffi::CString;
2928

3029
pub(crate) mod ffi;
3130
pub(crate) mod map_data;
@@ -332,10 +331,15 @@ fn create_pgo_func_name_var<'ll, 'tcx>(
332331
cx: &CodegenCx<'ll, 'tcx>,
333332
instance: Instance<'tcx>,
334333
) -> &'ll llvm::Value {
335-
let mangled_fn_name = CString::new(cx.tcx.symbol_name(instance).name)
336-
.expect("error converting function name to C string");
334+
let mangled_fn_name: &str = cx.tcx.symbol_name(instance).name;
337335
let llfn = cx.get_fn(instance);
338-
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
336+
unsafe {
337+
llvm::LLVMRustCoverageCreatePGOFuncNameVar(
338+
llfn,
339+
mangled_fn_name.as_ptr().cast(),
340+
mangled_fn_name.len(),
341+
)
342+
}
339343
}
340344

341345
pub(crate) fn write_filenames_section_to_buffer<'a>(

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,11 @@ extern "C" {
17201720
BufferOut: &RustString,
17211721
);
17221722

1723-
pub fn LLVMRustCoverageCreatePGOFuncNameVar(F: &Value, FuncName: *const c_char) -> &Value;
1723+
pub fn LLVMRustCoverageCreatePGOFuncNameVar(
1724+
F: &Value,
1725+
FuncName: *const c_char,
1726+
FuncNameLen: size_t,
1727+
) -> &Value;
17241728
pub fn LLVMRustCoverageHashByteArray(Bytes: *const c_char, NumBytes: size_t) -> u64;
17251729

17261730
#[allow(improper_ctypes)]

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ extern "C" void LLVMRustCoverageWriteMappingToBuffer(
161161
CoverageMappingWriter.write(OS);
162162
}
163163

164-
extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, const char *FuncName) {
165-
StringRef FuncNameRef(FuncName);
164+
extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(
165+
LLVMValueRef F,
166+
const char *FuncName,
167+
size_t FuncNameLen) {
168+
StringRef FuncNameRef(FuncName, FuncNameLen);
166169
return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
167170
}
168171

0 commit comments

Comments
 (0)