Skip to content

Commit 1106579

Browse files
committed
librustc_codegen_llvm: Use slices instead of 0-terminated strings
Changed functions: * LLVMRustGetOrInsertFunction * LLVMRustGetNamedValue * LLVMRustBuildCall (removed unused name argument) * LLVMRustInlineAsm * LLVMRustInlineAsmVerify * LLVMRustAppendModuleInlineAsm
1 parent 3dbade6 commit 1106579

File tree

6 files changed

+62
-56
lines changed

6 files changed

+62
-56
lines changed

src/librustc_codegen_llvm/allocator.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ffi::CString;
2-
31
use crate::attributes;
42
use libc::c_uint;
53
use rustc::bug;
@@ -50,8 +48,8 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: Alloc
5048
args.len() as c_uint,
5149
False,
5250
);
53-
let name = CString::new(format!("__rust_{}", method.name)).unwrap();
54-
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr(), ty);
51+
let name = format!("__rust_{}", method.name);
52+
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
5553

5654
if tcx.sess.target.target.options.default_hidden_visibility {
5755
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
@@ -60,8 +58,9 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: Alloc
6058
attributes::emit_uwtable(llfn, true);
6159
}
6260

63-
let callee = CString::new(kind.fn_name(method.name)).unwrap();
64-
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr(), ty);
61+
let callee = kind.fn_name(method.name);
62+
let callee =
63+
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
6564
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
6665

6766
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
@@ -73,14 +72,8 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: Alloc
7372
.enumerate()
7473
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
7574
.collect::<Vec<_>>();
76-
let ret = llvm::LLVMRustBuildCall(
77-
llbuilder,
78-
callee,
79-
args.as_ptr(),
80-
args.len() as c_uint,
81-
None,
82-
"\0".as_ptr().cast(),
83-
);
75+
let ret =
76+
llvm::LLVMRustBuildCall(llbuilder, callee, args.as_ptr(), args.len() as c_uint, None);
8477
llvm::LLVMSetTailCall(ret, True);
8578
if output.is_some() {
8679
llvm::LLVMBuildRet(llbuilder, ret);

src/librustc_codegen_llvm/asm.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_span::Span;
1212

1313
use libc::{c_char, c_uint};
1414
use log::debug;
15-
use std::ffi::{CStr, CString};
1615

1716
impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
1817
fn codegen_inline_asm(
@@ -74,12 +73,11 @@ impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
7473
_ => self.type_struct(&output_types, false),
7574
};
7675

77-
let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
78-
let constraint_cstr = CString::new(all_constraints).unwrap();
76+
let asm = ia.asm.as_str();
7977
let r = inline_asm_call(
8078
self,
8179
&asm,
82-
&constraint_cstr,
80+
&all_constraints,
8381
&inputs,
8482
output_type,
8583
ia.volatile,
@@ -119,17 +117,17 @@ impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
119117

120118
impl AsmMethods for CodegenCx<'ll, 'tcx> {
121119
fn codegen_global_asm(&self, ga: &hir::GlobalAsm) {
122-
let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
120+
let asm = ga.asm.as_str();
123121
unsafe {
124-
llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr());
122+
llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr().cast(), asm.len());
125123
}
126124
}
127125
}
128126

129127
fn inline_asm_call(
130128
bx: &mut Builder<'a, 'll, 'tcx>,
131-
asm: &CStr,
132-
cons: &CStr,
129+
asm: &str,
130+
cons: &str,
133131
inputs: &[&'ll Value],
134132
output: &'ll llvm::Type,
135133
volatile: bool,
@@ -151,13 +149,15 @@ fn inline_asm_call(
151149
let fty = bx.cx.type_func(&argtys[..], output);
152150
unsafe {
153151
// Ask LLVM to verify that the constraints are well-formed.
154-
let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr());
152+
let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr().cast(), cons.len());
155153
debug!("constraint verification result: {:?}", constraints_ok);
156154
if constraints_ok {
157155
let v = llvm::LLVMRustInlineAsm(
158156
fty,
159-
asm.as_ptr(),
160-
cons.as_ptr(),
157+
asm.as_ptr().cast(),
158+
asm.len(),
159+
cons.as_ptr().cast(),
160+
cons.len(),
161161
volatile,
162162
alignstack,
163163
llvm::AsmDialect::from_generic(dia),

src/librustc_codegen_llvm/builder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10161016
args.as_ptr() as *const &llvm::Value,
10171017
args.len() as c_uint,
10181018
bundle,
1019-
UNNAMED,
10201019
)
10211020
}
10221021
}

src/librustc_codegen_llvm/declare.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::value::Value;
2121
use log::debug;
2222
use rustc::ty::Ty;
2323
use rustc_codegen_ssa::traits::*;
24-
use rustc_data_structures::small_c_str::SmallCStr;
2524

2625
/// Declare a function.
2726
///
@@ -34,8 +33,9 @@ fn declare_raw_fn(
3433
ty: &'ll Type,
3534
) -> &'ll Value {
3635
debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
37-
let namebuf = SmallCStr::new(name);
38-
let llfn = unsafe { llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty) };
36+
let llfn = unsafe {
37+
llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_ptr().cast(), name.len(), ty)
38+
};
3939

4040
llvm::SetFunctionCallConv(llfn, callconv);
4141
// Function addresses in Rust are never significant, allowing functions to
@@ -83,8 +83,7 @@ impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
8383

8484
fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
8585
debug!("get_declared_value(name={:?})", name);
86-
let namebuf = SmallCStr::new(name);
87-
unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
86+
unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
8887
}
8988

9089
fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {

src/librustc_codegen_llvm/llvm/ffi.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ extern "C" {
732732

733733
/// See Module::setModuleInlineAsm.
734734
pub fn LLVMSetModuleInlineAsm(M: &Module, Asm: *const c_char);
735-
pub fn LLVMRustAppendModuleInlineAsm(M: &Module, Asm: *const c_char);
735+
pub fn LLVMRustAppendModuleInlineAsm(M: &Module, Asm: *const c_char, AsmLen: size_t);
736736

737737
/// See llvm::LLVMTypeKind::getTypeID.
738738
pub fn LLVMRustGetTypeKind(Ty: &Type) -> TypeKind;
@@ -879,13 +879,18 @@ extern "C" {
879879
pub fn LLVMSetThreadLocalMode(GlobalVar: &Value, Mode: ThreadLocalMode);
880880
pub fn LLVMIsGlobalConstant(GlobalVar: &Value) -> Bool;
881881
pub fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
882-
pub fn LLVMRustGetNamedValue(M: &Module, Name: *const c_char) -> Option<&Value>;
882+
pub fn LLVMRustGetNamedValue(
883+
M: &Module,
884+
Name: *const c_char,
885+
NameLen: size_t,
886+
) -> Option<&Value>;
883887
pub fn LLVMSetTailCall(CallInst: &Value, IsTailCall: Bool);
884888

885889
// Operations on functions
886890
pub fn LLVMRustGetOrInsertFunction(
887891
M: &'a Module,
888892
Name: *const c_char,
893+
NameLen: size_t,
889894
FunctionTy: &'a Type,
890895
) -> &'a Value;
891896
pub fn LLVMSetFunctionCallConv(Fn: &Value, CC: c_uint);
@@ -1332,7 +1337,6 @@ extern "C" {
13321337
Args: *const &'a Value,
13331338
NumArgs: c_uint,
13341339
Bundle: Option<&OperandBundleDef<'a>>,
1335-
Name: *const c_char,
13361340
) -> &'a Value;
13371341
pub fn LLVMRustBuildMemCpy(
13381342
B: &Builder<'a>,
@@ -1581,12 +1585,18 @@ extern "C" {
15811585
pub fn LLVMRustInlineAsm(
15821586
Ty: &Type,
15831587
AsmString: *const c_char,
1588+
AsmStringLen: size_t,
15841589
Constraints: *const c_char,
1590+
ConstraintsLen: size_t,
15851591
SideEffects: Bool,
15861592
AlignStack: Bool,
15871593
Dialect: AsmDialect,
15881594
) -> &Value;
1589-
pub fn LLVMRustInlineAsmVerify(Ty: &Type, Constraints: *const c_char) -> bool;
1595+
pub fn LLVMRustInlineAsmVerify(
1596+
Ty: &Type,
1597+
Constraints: *const c_char,
1598+
ConstraintsLen: size_t,
1599+
) -> bool;
15901600

15911601
pub fn LLVMRustDebugMetadataVersion() -> u32;
15921602
pub fn LLVMRustVersionMajor() -> u32;

src/rustllvm/RustWrapper.cpp

+26-21
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,22 @@ extern "C" void LLVMRustPrintPassTimings() {
112112
TimerGroup::printAll(OS);
113113
}
114114

115-
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M,
116-
const char *Name) {
117-
return wrap(unwrap(M)->getNamedValue(Name));
115+
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,
116+
size_t NameLen) {
117+
return wrap(unwrap(M)->getNamedValue(StringRef(Name, NameLen)));
118118
}
119119

120120
extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
121121
const char *Name,
122+
size_t NameLen,
122123
LLVMTypeRef FunctionTy) {
123-
return wrap(
124-
unwrap(M)->getOrInsertFunction(Name, unwrap<FunctionType>(FunctionTy))
124+
return wrap(unwrap(M)
125+
->getOrInsertFunction(StringRef(Name, NameLen),
126+
unwrap<FunctionType>(FunctionTy))
125127
#if LLVM_VERSION_GE(9, 0)
126-
.getCallee()
128+
.getCallee()
127129
#endif
128-
);
130+
);
129131
}
130132

131133
extern "C" LLVMValueRef
@@ -395,22 +397,26 @@ static InlineAsm::AsmDialect fromRust(LLVMRustAsmDialect Dialect) {
395397
}
396398
}
397399

398-
extern "C" LLVMValueRef LLVMRustInlineAsm(LLVMTypeRef Ty, char *AsmString,
399-
char *Constraints,
400-
LLVMBool HasSideEffects,
401-
LLVMBool IsAlignStack,
402-
LLVMRustAsmDialect Dialect) {
403-
return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString, Constraints,
400+
extern "C" LLVMValueRef
401+
LLVMRustInlineAsm(LLVMTypeRef Ty, char *AsmString, size_t AsmStringLen,
402+
char *Constraints, size_t ConstraintsLen,
403+
LLVMBool HasSideEffects, LLVMBool IsAlignStack,
404+
LLVMRustAsmDialect Dialect) {
405+
return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
406+
StringRef(AsmString, AsmStringLen),
407+
StringRef(Constraints, ConstraintsLen),
404408
HasSideEffects, IsAlignStack, fromRust(Dialect)));
405409
}
406410

407-
extern "C" bool LLVMRustInlineAsmVerify(LLVMTypeRef Ty,
408-
char *Constraints) {
409-
return InlineAsm::Verify(unwrap<FunctionType>(Ty), Constraints);
411+
extern "C" bool LLVMRustInlineAsmVerify(LLVMTypeRef Ty, char *Constraints,
412+
size_t ConstraintsLen) {
413+
return InlineAsm::Verify(unwrap<FunctionType>(Ty),
414+
StringRef(Constraints, ConstraintsLen));
410415
}
411416

412-
extern "C" void LLVMRustAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
413-
unwrap(M)->appendModuleInlineAsm(StringRef(Asm));
417+
extern "C" void LLVMRustAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm,
418+
size_t AsmLen) {
419+
unwrap(M)->appendModuleInlineAsm(StringRef(Asm, AsmLen));
414420
}
415421

416422
typedef DIBuilder *LLVMRustDIBuilderRef;
@@ -1282,12 +1288,11 @@ extern "C" void LLVMRustFreeOperandBundleDef(OperandBundleDef *Bundle) {
12821288

12831289
extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
12841290
LLVMValueRef *Args, unsigned NumArgs,
1285-
OperandBundleDef *Bundle,
1286-
const char *Name) {
1291+
OperandBundleDef *Bundle) {
12871292
unsigned Len = Bundle ? 1 : 0;
12881293
ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
12891294
return wrap(unwrap(B)->CreateCall(
1290-
unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Bundles, Name));
1295+
unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Bundles));
12911296
}
12921297

12931298
extern "C" LLVMValueRef LLVMRustBuildMemCpy(LLVMBuilderRef B,

0 commit comments

Comments
 (0)