forked from rust-lang/libc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhierarchy.out.rs
More file actions
263 lines (231 loc) · 9.93 KB
/
Copy pathhierarchy.out.rs
File metadata and controls
263 lines (231 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* This file was autogenerated by ctest; do not modify directly */
/// As this file is sometimes built using rustc, crate level attributes
/// are not allowed at the top-level, so we hack around this by keeping it
/// inside of a module.
mod generated_tests {
#![allow(non_snake_case)]
#![deny(improper_ctypes_definitions)]
#[allow(unused_imports)]
use std::ffi::{CStr, c_int, c_char, c_uint};
use std::fmt::{Debug, LowerHex};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[allow(unused_imports)]
use std::{mem, ptr, slice};
#[allow(unused_imports)]
use std::mem::{MaybeUninit, offset_of};
use super::*;
pub static FAILED: AtomicBool = AtomicBool::new(false);
pub static NTESTS: AtomicUsize = AtomicUsize::new(0);
/// Check that the value returned from the Rust and C side in a certain test is equivalent.
///
/// Internally it will remember which checks failed and how many tests have been run.
fn check_same<T: PartialEq + Debug>(rust: T, c: T, attr: &str) {
if rust != c {
eprintln!("bad {attr}: rust: {rust:?} != c {c:?}");
FAILED.store(true, Ordering::Relaxed);
} else {
NTESTS.fetch_add(1, Ordering::Relaxed);
}
}
/// Check that the value returned from the Rust and C side in a certain test is equivalent.
///
/// Internally it will remember which checks failed and how many tests have been run. This
/// method is the same as `check_same` but prints errors in bytes in hex.
fn check_same_hex<T: PartialEq + LowerHex + Debug>(rust: T, c: T, attr: &str) {
if rust != c {
eprintln!("bad {attr}: rust: {rust:?} ({rust:#x}) != c {c:?} ({c:#x})");
FAILED.store(true, Ordering::Relaxed);
} else {
NTESTS.fetch_add(1, Ordering::Relaxed);
}
}
// Test that the value of the constant is the same in both Rust and C.
// This performs a byte by byte comparision of the constant value.
pub fn ctest_const_ON() {
type T = bool;
extern "C" {
fn ctest_const__ON() -> *const T;
}
/* HACK: The slices may contian uninitialized data! We do this because
* there isn't a good way to recursively iterate all fields. */
let r_val: T = ON;
let r_bytes = unsafe {
slice::from_raw_parts(ptr::from_ref(&r_val).cast::<u8>(), size_of::<T>())
};
let c_bytes = unsafe {
let c_ptr: *const T = ctest_const__ON();
slice::from_raw_parts(c_ptr.cast::<u8>(), size_of::<T>())
};
for (i, (&b1, &b2)) in r_bytes.iter().zip(c_bytes.iter()).enumerate() {
check_same_hex(b1, b2, &format!("ON value at byte {}", i));
}
}
/// Compare the size and alignment of the type in Rust and C, making sure they are the same.
pub fn ctest_size_align_in6_addr() {
extern "C" {
fn ctest_size_of__in6_addr() -> u64;
fn ctest_align_of__in6_addr() -> u64;
}
let rust_size = size_of::<in6_addr>() as u64;
let c_size = unsafe { ctest_size_of__in6_addr() };
let rust_align = align_of::<in6_addr>() as u64;
let c_align = unsafe { ctest_align_of__in6_addr() };
check_same(rust_size, c_size, "in6_addr size");
check_same(rust_align, c_align, "in6_addr align");
}
/// Make sure that the signededness of a type alias in Rust and C is the same.
///
/// This is done by casting 0 to that type and flipping all of its bits. For unsigned types,
/// this would result in a value larger than zero. For signed types, this results in a value
/// smaller than 0.
pub fn ctest_signededness_in6_addr() {
extern "C" {
fn ctest_signededness_of__in6_addr() -> u32;
}
let all_ones = !(0 as in6_addr);
let all_zeros = 0 as in6_addr;
let c_is_signed = unsafe { ctest_signededness_of__in6_addr() };
check_same((all_ones < all_zeros) as u32, c_is_signed, "in6_addr signed");
}
/// Generates a padding map for a specific type.
///
/// Essentially, it returns a list of bytes, whose length is equal to the size of the type in
/// bytes. Each element corresponds to a byte and has two values. `true` if the byte is padding,
/// and `false` if the byte is not padding.
///
/// For aliases we assume that there are no padding bytes, for structs and unions,
/// if there are no fields, then everything is padding, if there are fields, then we have to
/// go through each field and figure out the padding.
fn roundtrip_padding__in6_addr() -> Vec<bool> {
if 0 == 0 {
// FIXME(ctest): What if it's an alias to a struct/union?
return vec![!true; size_of::<in6_addr>()]
}
// If there are no fields, v and bar become unused.
#[allow(unused_mut)]
let mut v = Vec::<(usize, usize)>::new();
#[allow(unused_variables)]
let bar = MaybeUninit::<in6_addr>::zeroed();
#[allow(unused_variables)]
let bar = bar.as_ptr();
// This vector contains `true` if the byte is padding and `false` if the byte is not
// padding. Initialize all bytes as:
// - padding if we have fields, this means that only the fields will be checked
// - no-padding if we have a type alias: if this causes problems the type alias should
// be skipped
let mut is_padding_byte = vec![true; size_of::<in6_addr>()];
for (off, size) in &v {
for i in 0..*size {
is_padding_byte[off + i] = false;
}
}
is_padding_byte
}
/// Tests whether a type alias when passed to C and back to Rust remains unchanged.
///
/// It checks if the size is the same as well as if the padding bytes are all in the
/// correct place. For this test to be sound, `T` must be valid for any bitpattern.
pub fn ctest_roundtrip_in6_addr() {
type U = in6_addr;
extern "C" {
fn ctest_size_of__in6_addr() -> u64;
fn ctest_roundtrip__in6_addr(
input: MaybeUninit<U>, is_padding_byte: *const bool, value_bytes: *mut u8
) -> U;
}
const SIZE: usize = size_of::<U>();
let is_padding_byte = roundtrip_padding__in6_addr();
let mut expected = vec![0u8; SIZE];
let mut input = MaybeUninit::<U>::zeroed();
let input_ptr = input.as_mut_ptr().cast::<u8>();
// Fill the unitialized memory with a deterministic pattern.
// From Rust to C: every byte will be labelled from 1 to 255, with 0 turning into 42.
// From C to Rust: every byte will be inverted from before (254 -> 1), but 0 is still 42.
for i in 0..SIZE {
let c: u8 = (i % 256) as u8;
let c = if c == 0 { 42 } else { c };
let d: u8 = 255_u8 - (i % 256) as u8;
let d = if d == 0 { 42 } else { d };
unsafe {
input_ptr.add(i).write_volatile(c);
expected[i] = d;
}
}
let c_size = unsafe { ctest_size_of__in6_addr() } as usize;
if SIZE != c_size {
FAILED.store(true, Ordering::Relaxed);
eprintln!(
"size of in6_addr is {c_size} in C and {SIZE} in Rust\n",
);
return;
}
let mut c_value_bytes = vec![0; size_of::<in6_addr>()];
let r: U = unsafe {
ctest_roundtrip__in6_addr(input, is_padding_byte.as_ptr(), c_value_bytes.as_mut_ptr())
};
// Check that the value bytes as read from C match the byte we sent from Rust.
for (i, is_padding_byte) in is_padding_byte.iter().enumerate() {
if *is_padding_byte { continue; }
let rust = unsafe { *input_ptr.add(i) };
let c = c_value_bytes[i];
if rust != c {
eprintln!("rust[{}] = {} != {} (C): Rust \"in6_addr\" -> C", i, rust, c);
FAILED.store(true, Ordering::Relaxed);
}
}
// Check that value returned from C contains the bytes we expect.
for (i, is_padding_byte) in is_padding_byte.iter().enumerate() {
if *is_padding_byte { continue; }
let rust = expected[i] as usize;
let c = unsafe { (&raw const r).cast::<u8>().add(i).read_volatile() as usize };
if rust != c {
eprintln!(
"rust [{i}] = {rust} != {c} (C): C \"in6_addr\" -> Rust",
);
FAILED.store(true, Ordering::Relaxed);
}
}
}
/// Check if the Rust and C side function pointers point to the same underlying function.
pub fn ctest_foreign_fn_malloc() {
extern "C" {
fn ctest_foreign_fn__malloc() -> unsafe extern "C" fn();
}
let actual = unsafe { ctest_foreign_fn__malloc() } as u64;
let expected = malloc as u64;
check_same(actual, expected, "malloc function pointer");
}
// Tests if the pointer to the static variable matches in both Rust and C.
pub fn ctest_static_in6addr_any() {
extern "C" {
fn ctest_static__in6addr_any() -> *const in6_addr;
}
let actual = (&raw const in6addr_any).addr();
let expected = unsafe {
ctest_static__in6addr_any().addr()
};
check_same(actual, expected, "in6addr_any static");
}
}
use generated_tests::*;
fn main() {
println!("RUNNING ALL TESTS");
run_all();
if FAILED.load(std::sync::atomic::Ordering::Relaxed) {
panic!("some tests failed");
} else {
println!(
"PASSED {} tests",
NTESTS.load(std::sync::atomic::Ordering::Relaxed)
);
}
}
// Run all tests by calling the functions that define them.
fn run_all() {
ctest_const_ON();
ctest_size_align_in6_addr();
ctest_signededness_in6_addr();
ctest_roundtrip_in6_addr();
ctest_foreign_fn_malloc();
ctest_static_in6addr_any();
}