Skip to content

Commit dd9241d

Browse files
committed
c_variadic: use Clone instead of LLVM va_copy
1 parent 158ae9e commit dd9241d

9 files changed

Lines changed: 101 additions & 85 deletions

File tree

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
15061506
}
15071507

15081508
// FIXME implement variadics in cranelift
1509-
sym::va_copy | sym::va_arg | sym::va_end => {
1509+
sym::va_arg | sym::va_end => {
15101510
fx.tcx.dcx().span_fatal(
15111511
source_info.span,
15121512
"Defining variadic functions is not yet supported by Cranelift",

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,6 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
391391
sym::breakpoint => {
392392
unimplemented!();
393393
}
394-
sym::va_copy => {
395-
unimplemented!();
396-
}
397394
sym::va_arg => {
398395
unimplemented!();
399396
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,6 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
269269
return Ok(());
270270
}
271271
sym::breakpoint => self.call_intrinsic("llvm.debugtrap", &[], &[]),
272-
sym::va_copy => {
273-
let dest = args[0].immediate();
274-
self.call_intrinsic(
275-
"llvm.va_copy",
276-
&[self.val_ty(dest)],
277-
&[dest, args[1].immediate()],
278-
)
279-
}
280272
sym::va_arg => {
281273
match result.layout.backend_repr {
282274
BackendRepr::Scalar(scalar) => {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
216216
| sym::type_name
217217
| sym::type_of
218218
| sym::ub_checks
219+
| sym::va_copy
219220
| sym::variant_count
220221
| sym::vtable_for
221222
| sym::wrapping_add
@@ -629,14 +630,13 @@ pub(crate) fn check_intrinsic_type(
629630
)
630631
}
631632

632-
sym::va_start | sym::va_end => {
633-
(0, 0, vec![mk_va_list_ty(hir::Mutability::Mut).0], tcx.types.unit)
634-
}
635-
636633
sym::va_copy => {
637634
let (va_list_ref_ty, va_list_ty) = mk_va_list_ty(hir::Mutability::Not);
638-
let va_list_ptr_ty = Ty::new_mut_ptr(tcx, va_list_ty);
639-
(0, 0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.types.unit)
635+
(0, 0, vec![va_list_ref_ty], va_list_ty)
636+
}
637+
638+
sym::va_start | sym::va_end => {
639+
(0, 0, vec![mk_va_list_ty(hir::Mutability::Mut).0], tcx.types.unit)
640640
}
641641

642642
sym::va_arg => (1, 0, vec![mk_va_list_ty(hir::Mutability::Mut).0], param(0)),

library/core/src/ffi/va_list.rs

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ use crate::marker::PhantomCovariantLifetime;
3434
//
3535
// The Clang `BuiltinVaListKind` enumerates the `va_list` variations that Clang supports,
3636
// and we mirror these here.
37+
//
38+
// For all current LLVM targets, `va_copy` lowers to `memcpy`. Hence the inner structs below all
39+
// derive `Copy`. However, in the future we might want to support a target where `va_copy`
40+
// allocates, or otherwise violates the requirements of `Copy`. Therefore `VaList` is only `Clone`.
3741
crate::cfg_select! {
3842
all(
3943
target_arch = "aarch64",
@@ -45,10 +49,12 @@ crate::cfg_select! {
4549
///
4650
/// See the [AArch64 Procedure Call Standard] for more details.
4751
///
52+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/5aee01a3df011e660f26660bc30a8c94a1651d8e/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp#L12682-L12700>
53+
///
4854
/// [AArch64 Procedure Call Standard]:
4955
/// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055b/IHI0055B_aapcs64.pdf
5056
#[repr(C)]
51-
#[derive(Debug)]
57+
#[derive(Debug, Clone, Copy)]
5258
struct VaListInner {
5359
stack: *const c_void,
5460
gr_top: *const c_void,
@@ -62,11 +68,13 @@ crate::cfg_select! {
6268
///
6369
/// See the [LLVM source] and [GCC header] for more details.
6470
///
71+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/5aee01a3df011e660f26660bc30a8c94a1651d8e/llvm/lib/Target/PowerPC/PPCISelLowering.cpp#L3755-L3764>
72+
///
6573
/// [LLVM source]:
6674
/// https://github.com/llvm/llvm-project/blob/af9a4263a1a209953a1d339ef781a954e31268ff/llvm/lib/Target/PowerPC/PPCISelLowering.cpp#L4089-L4111
6775
/// [GCC header]: https://web.mit.edu/darwin/src/modules/gcc/gcc/ginclude/va-ppc.h
6876
#[repr(C)]
69-
#[derive(Debug)]
77+
#[derive(Debug, Clone, Copy)]
7078
#[rustc_pass_indirectly_in_non_rustic_abis]
7179
struct VaListInner {
7280
gpr: u8,
@@ -81,10 +89,12 @@ crate::cfg_select! {
8189
///
8290
/// See the [S/390x ELF Application Binary Interface Supplement] for more details.
8391
///
92+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/5aee01a3df011e660f26660bc30a8c94a1651d8e/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp#L4457-L4472>
93+
///
8494
/// [S/390x ELF Application Binary Interface Supplement]:
8595
/// https://docs.google.com/gview?embedded=true&url=https://github.com/IBM/s390x-abi/releases/download/v1.7/lzsabi_s390x.pdf
8696
#[repr(C)]
87-
#[derive(Debug)]
97+
#[derive(Debug, Clone, Copy)]
8898
#[rustc_pass_indirectly_in_non_rustic_abis]
8999
struct VaListInner {
90100
gpr: i64,
@@ -98,10 +108,13 @@ crate::cfg_select! {
98108
///
99109
/// See the [System V AMD64 ABI] for more details.
100110
///
111+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/5aee01a3df011e660f26660bc30a8c94a1651d8e/llvm/lib/Target/X86/X86ISelLowering.cpp#26319>
112+
/// (github won't render that file, look for `SDValue LowerVACOPY`)
113+
///
101114
/// [System V AMD64 ABI]:
102115
/// https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf
103116
#[repr(C)]
104-
#[derive(Debug)]
117+
#[derive(Debug, Clone, Copy)]
105118
#[rustc_pass_indirectly_in_non_rustic_abis]
106119
struct VaListInner {
107120
gp_offset: i32,
@@ -115,10 +128,12 @@ crate::cfg_select! {
115128
///
116129
/// See the [LLVM source] for more details.
117130
///
131+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/5aee01a3df011e660f26660bc30a8c94a1651d8e/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp#L1260>
132+
///
118133
/// [LLVM source]:
119134
/// https://github.com/llvm/llvm-project/blob/af9a4263a1a209953a1d339ef781a954e31268ff/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp#L1211-L1215
120135
#[repr(C)]
121-
#[derive(Debug)]
136+
#[derive(Debug, Clone, Copy)]
122137
#[rustc_pass_indirectly_in_non_rustic_abis]
123138
struct VaListInner {
124139
stk: *const i32,
@@ -132,10 +147,12 @@ crate::cfg_select! {
132147
///
133148
/// See the [LLVM source] for more details. On bare metal Hexagon uses an opaque pointer.
134149
///
150+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/5aee01a3df011e660f26660bc30a8c94a1651d8e/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp#L1087-L1102>
151+
///
135152
/// [LLVM source]:
136153
/// https://github.com/llvm/llvm-project/blob/0cdc1b6dd4a870fc41d4b15ad97e0001882aba58/clang/lib/CodeGen/Targets/Hexagon.cpp#L407-L417
137154
#[repr(C)]
138-
#[derive(Debug)]
155+
#[derive(Debug, Clone, Copy)]
139156
#[rustc_pass_indirectly_in_non_rustic_abis]
140157
struct VaListInner {
141158
__current_saved_reg_area_pointer: *const c_void,
@@ -156,8 +173,10 @@ crate::cfg_select! {
156173
// That pointer is probably just the next variadic argument on the caller's stack.
157174
_ => {
158175
/// Basic implementation of a `va_list`.
176+
///
177+
/// `va_copy` is `memcpy`: <https://github.com/llvm/llvm-project/blob/87e8e7d8f0db53060ef2f6ef4ab612fc0f2b4490/llvm/lib/Transforms/IPO/ExpandVariadics.cpp#L127-L129>
159178
#[repr(transparent)]
160-
#[derive(Debug)]
179+
#[derive(Debug, Clone, Copy)]
161180
struct VaListInner {
162181
ptr: *const c_void,
163182
}
@@ -179,6 +198,36 @@ impl fmt::Debug for VaList<'_> {
179198
}
180199
}
181200

201+
impl VaList<'_> {
202+
// Helper used in the implementation of the `va_copy` intrinsic.
203+
pub(crate) fn duplicate(&self) -> Self {
204+
Self { inner: self.inner.clone(), _marker: self._marker }
205+
}
206+
}
207+
208+
impl Clone for VaList<'_> {
209+
#[inline]
210+
fn clone(&self) -> Self {
211+
// We only implement Clone and not Copy because some future target might not be able to
212+
// implement Copy (e.g. because it allocates). For the same reason we use an intrinsic
213+
// to do the copying: the fact that on all current targets, this is just `memcpy`, is an implementation
214+
// detail. The intrinsic lets Miri catch UB from code incorrectly relying on that implementation detail.
215+
va_copy(self)
216+
}
217+
}
218+
219+
impl Drop for VaList<'_> {
220+
fn drop(&mut self) {
221+
// For all current LLVM targets `va_end` is a no-op.
222+
//
223+
// We implement `Drop` here because some future target might need to actually run
224+
// destructors (e.g. to deallocate).
225+
//
226+
// Rust requires that not calling `va_end` on a `va_list` does not cause undefined
227+
// behaviour: it is safe to leak values.
228+
}
229+
}
230+
182231
mod sealed {
183232
pub trait Sealed {}
184233

@@ -253,26 +302,6 @@ impl<'f> VaList<'f> {
253302
}
254303
}
255304

256-
impl<'f> Clone for VaList<'f> {
257-
#[inline]
258-
fn clone(&self) -> Self {
259-
let mut dest = crate::mem::MaybeUninit::uninit();
260-
// SAFETY: we write to the `MaybeUninit`, thus it is initialized and `assume_init` is legal.
261-
unsafe {
262-
va_copy(dest.as_mut_ptr(), self);
263-
dest.assume_init()
264-
}
265-
}
266-
}
267-
268-
impl<'f> Drop for VaList<'f> {
269-
fn drop(&mut self) {
270-
// Rust requires that not calling `va_end` on a `va_list` does not cause undefined behaviour
271-
// (as it is safe to leak values). As `va_end` is a no-op on all current LLVM targets, this
272-
// destructor is empty.
273-
}
274-
}
275-
276305
// Checks (via an assert in `compiler/rustc_ty_utils/src/abi.rs`) that the C ABI for the current
277306
// target correctly implements `rustc_pass_indirectly_in_non_rustic_abis`.
278307
const _: () = {

library/core/src/intrinsics/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,19 +3451,6 @@ pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize
34513451
)
34523452
}
34533453

3454-
/// Copies the current location of arglist `src` to the arglist `dst`.
3455-
///
3456-
/// # Safety
3457-
///
3458-
/// You must check the following invariants before you call this function:
3459-
///
3460-
/// - `dest` must be non-null and point to valid, writable memory.
3461-
/// - `dest` must not alias `src`.
3462-
///
3463-
#[rustc_intrinsic]
3464-
#[rustc_nounwind]
3465-
pub unsafe fn va_copy<'f>(dest: *mut VaList<'f>, src: &VaList<'f>);
3466-
34673454
/// Loads an argument of type `T` from the `va_list` `ap` and increment the
34683455
/// argument `ap` points to.
34693456
///
@@ -3482,6 +3469,20 @@ pub unsafe fn va_copy<'f>(dest: *mut VaList<'f>, src: &VaList<'f>);
34823469
#[rustc_nounwind]
34833470
pub unsafe fn va_arg<T: VaArgSafe>(ap: &mut VaList<'_>) -> T;
34843471

3472+
/// Duplicates a variable argument list. The returned list is initially at the same position as
3473+
/// the one in `src`, but can be advanced independently.
3474+
///
3475+
/// Codegen backends should not have custom behavior for this intrinsic, they should always use
3476+
/// this fallback implementation. This intrinsic *does not* map to the LLVM `va_copy` intrinsic.
3477+
///
3478+
/// This intrinsic exists only as a hook for Miri and constant evaluation, and is used to detect UB
3479+
/// when a variable argument list is used incorrectly.
3480+
#[rustc_intrinsic]
3481+
#[rustc_nounwind]
3482+
pub fn va_copy<'f>(src: &VaList<'f>) -> VaList<'f> {
3483+
src.duplicate()
3484+
}
3485+
34853486
/// Destroy the arglist `ap` after initialization with `va_start` or `va_copy`.
34863487
///
34873488
/// # Safety

tests/codegen-llvm/cffi/c-variadic-copy.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/codegen-llvm/cffi/c-variadic-opt.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,3 @@ pub unsafe extern "C" fn c_variadic_no_use(fmt: *const i8, mut ap: ...) -> i32 {
1717
vprintf(fmt, ap)
1818
// CHECK: call void @llvm.va_end
1919
}
20-
21-
// Check that `VaList::clone` gets inlined into a direct call to `llvm.va_copy`
22-
#[no_mangle]
23-
pub unsafe extern "C" fn c_variadic_clone(fmt: *const i8, mut ap: ...) -> i32 {
24-
// CHECK: call void @llvm.va_start
25-
let mut ap2 = ap.clone();
26-
// CHECK: call void @llvm.va_copy
27-
let res = vprintf(fmt, ap2);
28-
res
29-
// CHECK: call void @llvm.va_end
30-
}

tests/ui/c-variadic/copy.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ run-pass
2+
//@ ignore-backends: gcc
3+
#![feature(c_variadic)]
4+
5+
// Test the behavior of `VaList::clone`. In C a `va_list` is duplicated using `va_copy`, but the
6+
// rust api just uses `Clone`. This should create a completely independent cursor into the
7+
// variable argument list: advancing the original has no effect on the copy and vice versa.
8+
9+
fn main() {
10+
unsafe { variadic(1, 2, 3) }
11+
}
12+
13+
unsafe extern "C" fn variadic(mut ap1: ...) {
14+
let mut ap2 = ap1.clone();
15+
16+
assert_eq!(ap1.arg::<i32>(), 1);
17+
assert_eq!(ap2.arg::<i32>(), 1);
18+
19+
assert_eq!(ap2.arg::<i32>(), 2);
20+
assert_eq!(ap1.arg::<i32>(), 2);
21+
22+
drop(ap1);
23+
assert_eq!(ap2.arg::<i32>(), 3);
24+
}

0 commit comments

Comments
 (0)