Skip to content

Commit 2fc568b

Browse files
committed
Take dependency on zerocopy for safe transmutation
1 parent fbd1802 commit 2fc568b

7 files changed

Lines changed: 311 additions & 66 deletions

File tree

perf-event-open-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ name = "perf_event_open_sys"
2525
[dependencies]
2626
libc = "0.2"
2727
memoffset = "0.9.1"
28+
zerocopy = { version = "0.8.26", features = ["derive"] }

perf-event-open-sys/regenerate.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ function gen_bindings {
9292
BINDGEN_ARGS=(
9393
--impl-debug
9494
--with-derive-default
95+
--with-derive-custom ".*=zerocopy::FromBytes"
96+
--with-derive-custom ".*=zerocopy::Immutable"
97+
--with-derive-custom ".*=zerocopy::KnownLayout"
9598
--no-prepend-enum-name
9699
)
97100

@@ -120,6 +123,14 @@ function gen_bindings {
120123
# field if they really need it.
121124
sed -i "$bindings" -e 's/\( *\)pub \(fn new_bitfield_[0-9]*\)/\1#[allow(dead_code)]\n\1pub(crate) \2/g'
122125

126+
# Add zerocopy derives to __BindgenBitfieldUnit and __IncompleteArrayField.
127+
# Unfortunately bindgen does not have a way to add custom derives to these structs today.
128+
# See: https://github.com/rust-lang/rust-bindgen/issues/2168 and https://github.com/rust-lang/rust-bindgen/issues/3196.
129+
# Rust-VMM has the same problem:
130+
# https://github.com/rust-vmm/kvm/blob/bd3260e132706a07e1422be5a0b02e49736329bb/kvm-bindings/CONTRIBUTING.md
131+
sed -i "$bindings" -e 's/\(pub struct __BindgenBitfieldUnit.*\)/#[derive(zerocopy::FromBytes, zerocopy::Immutable, zerocopy::KnownLayout)]\n\1/g'
132+
sed -i "$bindings" -e 's/\(pub struct __IncompleteArrayField.*\)/#[derive(zerocopy::FromBytes, zerocopy::Immutable, zerocopy::KnownLayout)]\n\1/g'
133+
123134
cat src/bindings_header.rs \
124135
"$bindings" \
125136
> "src/bindings_$arch.rs"

perf-event-open-sys/src/bindings_aarch64.rs

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,23 @@
2121
#![allow(deref_nullptr)] // `bindgen_test_layout` tests use bogus code
2222
#![allow(clippy::all)]
2323

24-
/* automatically generated by rust-bindgen 0.69.4 */
24+
/* automatically generated by rust-bindgen 0.69.1 */
2525

2626
#[repr(C)]
27-
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
27+
#[derive(
28+
Copy,
29+
Clone,
30+
Debug,
31+
Default,
32+
Eq,
33+
Hash,
34+
Ord,
35+
PartialEq,
36+
PartialOrd,
37+
zerocopy::FromBytes,
38+
zerocopy::Immutable,
39+
zerocopy::KnownLayout,
40+
)]
2841
pub struct __BindgenBitfieldUnit<Storage> {
2942
storage: Storage,
3043
}
@@ -104,7 +117,7 @@ where
104117
}
105118
}
106119
#[repr(C)]
107-
#[derive(Default)]
120+
#[derive(Default, zerocopy::FromBytes, zerocopy::Immutable, zerocopy::KnownLayout)]
108121
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
109122
impl<T> __IncompleteArrayField<T> {
110123
#[inline]
@@ -603,7 +616,15 @@ pub type __u32 = ::std::os::raw::c_uint;
603616
pub type __s64 = ::std::os::raw::c_longlong;
604617
pub type __u64 = ::std::os::raw::c_ulonglong;
605618
#[repr(C)]
606-
#[derive(Debug, Default, Copy, Clone)]
619+
#[derive(
620+
Debug,
621+
Default,
622+
Copy,
623+
Clone,
624+
zerocopy :: FromBytes,
625+
zerocopy :: Immutable,
626+
zerocopy :: KnownLayout,
627+
)]
607628
pub struct __kernel_fd_set {
608629
pub fds_bits: [::std::os::raw::c_ulong; 16usize],
609630
}
@@ -655,7 +676,15 @@ pub type __kernel_size_t = __kernel_ulong_t;
655676
pub type __kernel_ssize_t = __kernel_long_t;
656677
pub type __kernel_ptrdiff_t = __kernel_long_t;
657678
#[repr(C)]
658-
#[derive(Debug, Default, Copy, Clone)]
679+
#[derive(
680+
Debug,
681+
Default,
682+
Copy,
683+
Clone,
684+
zerocopy :: FromBytes,
685+
zerocopy :: Immutable,
686+
zerocopy :: KnownLayout,
687+
)]
659688
pub struct __kernel_fsid_t {
660689
pub val: [::std::os::raw::c_int; 2usize],
661690
}
@@ -892,7 +921,7 @@ pub const PERF_FORMAT_LOST: perf_event_read_format = 16;
892921
pub const PERF_FORMAT_MAX: perf_event_read_format = 32;
893922
pub type perf_event_read_format = ::std::os::raw::c_uint;
894923
#[repr(C)]
895-
#[derive(Copy, Clone)]
924+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
896925
#[non_exhaustive]
897926
pub struct perf_event_attr {
898927
pub type_: __u32,
@@ -921,7 +950,7 @@ pub struct perf_event_attr {
921950
pub config3: __u64,
922951
}
923952
#[repr(C)]
924-
#[derive(Copy, Clone)]
953+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
925954
pub union perf_event_attr__bindgen_ty_1 {
926955
pub sample_period: __u64,
927956
pub sample_freq: __u64,
@@ -977,7 +1006,7 @@ impl ::std::fmt::Debug for perf_event_attr__bindgen_ty_1 {
9771006
}
9781007
}
9791008
#[repr(C)]
980-
#[derive(Copy, Clone)]
1009+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
9811010
pub union perf_event_attr__bindgen_ty_2 {
9821011
pub wakeup_events: __u32,
9831012
pub wakeup_watermark: __u32,
@@ -1033,7 +1062,7 @@ impl ::std::fmt::Debug for perf_event_attr__bindgen_ty_2 {
10331062
}
10341063
}
10351064
#[repr(C)]
1036-
#[derive(Copy, Clone)]
1065+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
10371066
pub union perf_event_attr__bindgen_ty_3 {
10381067
pub bp_addr: __u64,
10391068
pub kprobe_func: __u64,
@@ -1111,7 +1140,7 @@ impl ::std::fmt::Debug for perf_event_attr__bindgen_ty_3 {
11111140
}
11121141
}
11131142
#[repr(C)]
1114-
#[derive(Copy, Clone)]
1143+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
11151144
pub union perf_event_attr__bindgen_ty_4 {
11161145
pub bp_len: __u64,
11171146
pub kprobe_addr: __u64,
@@ -2017,7 +2046,7 @@ impl perf_event_attr {
20172046
}
20182047
}
20192048
#[repr(C)]
2020-
#[derive(Debug, Default)]
2049+
#[derive(Debug, Default, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
20212050
pub struct perf_event_query_bpf {
20222051
pub ids_len: __u32,
20232052
pub prog_cnt: __u32,
@@ -2071,7 +2100,7 @@ fn bindgen_test_layout_perf_event_query_bpf() {
20712100
pub const PERF_IOC_FLAG_GROUP: perf_event_ioc_flags = 1;
20722101
pub type perf_event_ioc_flags = ::std::os::raw::c_uint;
20732102
#[repr(C)]
2074-
#[derive(Copy, Clone)]
2103+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
20752104
#[non_exhaustive]
20762105
pub struct perf_event_mmap_page {
20772106
pub version: __u32,
@@ -2102,13 +2131,22 @@ pub struct perf_event_mmap_page {
21022131
pub aux_size: __u64,
21032132
}
21042133
#[repr(C)]
2105-
#[derive(Copy, Clone)]
2134+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
21062135
pub union perf_event_mmap_page__bindgen_ty_1 {
21072136
pub capabilities: __u64,
21082137
pub __bindgen_anon_1: perf_event_mmap_page__bindgen_ty_1__bindgen_ty_1,
21092138
}
21102139
#[repr(C)]
2111-
#[derive(Debug, Default, Copy, Clone)]
2140+
#[repr(align(8))]
2141+
#[derive(
2142+
Debug,
2143+
Default,
2144+
Copy,
2145+
Clone,
2146+
zerocopy :: FromBytes,
2147+
zerocopy :: Immutable,
2148+
zerocopy :: KnownLayout,
2149+
)]
21122150
#[non_exhaustive]
21132151
pub struct perf_event_mmap_page__bindgen_ty_1__bindgen_ty_1 {
21142152
pub _bitfield_align_1: [u64; 0],
@@ -2578,7 +2616,15 @@ impl ::std::fmt::Debug for perf_event_mmap_page {
25782616
}
25792617
}
25802618
#[repr(C)]
2581-
#[derive(Debug, Default, Copy, Clone)]
2619+
#[derive(
2620+
Debug,
2621+
Default,
2622+
Copy,
2623+
Clone,
2624+
zerocopy :: FromBytes,
2625+
zerocopy :: Immutable,
2626+
zerocopy :: KnownLayout,
2627+
)]
25822628
pub struct perf_event_header {
25832629
pub type_: __u32,
25842630
pub misc: __u16,
@@ -2630,7 +2676,15 @@ fn bindgen_test_layout_perf_event_header() {
26302676
);
26312677
}
26322678
#[repr(C)]
2633-
#[derive(Debug, Default, Copy, Clone)]
2679+
#[derive(
2680+
Debug,
2681+
Default,
2682+
Copy,
2683+
Clone,
2684+
zerocopy :: FromBytes,
2685+
zerocopy :: Immutable,
2686+
zerocopy :: KnownLayout,
2687+
)]
26342688
pub struct perf_ns_link_info {
26352689
pub dev: __u64,
26362690
pub ino: __u64,
@@ -2721,14 +2775,22 @@ pub const PERF_CONTEXT_GUEST_USER: perf_callchain_context = 18446744073709549056
27212775
pub const PERF_CONTEXT_MAX: perf_callchain_context = 18446744073709547521;
27222776
pub type perf_callchain_context = ::std::os::raw::c_ulong;
27232777
#[repr(C)]
2724-
#[derive(Copy, Clone)]
2778+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
27252779
pub union perf_mem_data_src {
27262780
pub val: __u64,
27272781
pub __bindgen_anon_1: perf_mem_data_src__bindgen_ty_1,
27282782
}
27292783
#[repr(C)]
27302784
#[repr(align(8))]
2731-
#[derive(Debug, Default, Copy, Clone)]
2785+
#[derive(
2786+
Debug,
2787+
Default,
2788+
Copy,
2789+
Clone,
2790+
zerocopy :: FromBytes,
2791+
zerocopy :: Immutable,
2792+
zerocopy :: KnownLayout,
2793+
)]
27322794
pub struct perf_mem_data_src__bindgen_ty_1 {
27332795
pub _bitfield_align_1: [u32; 0],
27342796
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 8usize]>,
@@ -2971,7 +3033,15 @@ impl ::std::fmt::Debug for perf_mem_data_src {
29713033
}
29723034
}
29733035
#[repr(C)]
2974-
#[derive(Debug, Default, Copy, Clone)]
3036+
#[derive(
3037+
Debug,
3038+
Default,
3039+
Copy,
3040+
Clone,
3041+
zerocopy :: FromBytes,
3042+
zerocopy :: Immutable,
3043+
zerocopy :: KnownLayout,
3044+
)]
29753045
pub struct perf_branch_entry {
29763046
pub from: __u64,
29773047
pub to: __u64,
@@ -3183,13 +3253,21 @@ impl perf_branch_entry {
31833253
}
31843254
}
31853255
#[repr(C)]
3186-
#[derive(Copy, Clone)]
3256+
#[derive(Copy, Clone, zerocopy :: FromBytes, zerocopy :: Immutable, zerocopy :: KnownLayout)]
31873257
pub union perf_sample_weight {
31883258
pub full: __u64,
31893259
pub __bindgen_anon_1: perf_sample_weight__bindgen_ty_1,
31903260
}
31913261
#[repr(C)]
3192-
#[derive(Debug, Default, Copy, Clone)]
3262+
#[derive(
3263+
Debug,
3264+
Default,
3265+
Copy,
3266+
Clone,
3267+
zerocopy :: FromBytes,
3268+
zerocopy :: Immutable,
3269+
zerocopy :: KnownLayout,
3270+
)]
31933271
pub struct perf_sample_weight__bindgen_ty_1 {
31943272
pub var1_dw: __u32,
31953273
pub var2_w: __u16,

0 commit comments

Comments
 (0)