-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathbasic_mem.rs
148 lines (139 loc) · 3.98 KB
/
basic_mem.rs
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
#![feature(test)]
#![no_std]
#![no_main]
#![test_runner(common::test_case_runner)]
#![feature(custom_test_frameworks)]
#![reexport_test_harness_main = "test_main"]
extern crate alloc;
mod common;
use alloc::vec::Vec;
use core::mem::size_of;
const PATTERN: u8 = 0xab;
/// Mainly test if memcpy works as expected. Also somewhat tests memcmp
/// Works with u8, u16, u32, u64, u128, i16, i32, i64 and i128
/// Probably not a super good test
fn mem<T>()
where
T: core::fmt::Debug,
T: num_traits::int::PrimInt,
{
unsafe extern "C" {
fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
}
let vec_size: u32 = 10000;
let pre_dest_vec_size: u32 = 1;
let post_dest_vec_size: u32 = 1;
let t_base_pattern: T = T::from(PATTERN).unwrap();
let mut pattern: T = t_base_pattern;
// Fill pattern of type T with size_of<T> times the byte pattern
// The "pre" and "post part of the destination vector are later filled with this pattern
for _i in 1..size_of::<T>() {
pattern = pattern.shl(8) + t_base_pattern;
}
let pattern = pattern; // remove mut
let a: Vec<T> = {
//Vec containing 0..min(vec_size, T::max_value()) as pattern for vec_size elements
let mut a: Vec<T> = Vec::with_capacity(vec_size as usize);
let max = {
// the max value in a is the minimum of (vec_size -1) and T::max
let tmax = T::max_value();
if T::from(vec_size).is_none() {
tmax.to_u64().unwrap() // If vec_size can't be represented in T, then tmax must fit in u64
} else {
u64::from(vec_size - 1)
}
};
// ToDo - This loop should be rewritten in a nicer way
while a.len() < vec_size as usize {
for i in 0..=max {
a.push(T::from(i).unwrap());
if a.len() == vec_size as usize {
break;
};
}
}
a
};
assert_eq!(a.len(), vec_size as usize);
let mut b: Vec<T> =
Vec::with_capacity((vec_size + pre_dest_vec_size + post_dest_vec_size) as usize);
// Fill pre and post section with `pattern`
for i in 0..pre_dest_vec_size {
b.spare_capacity_mut()[i as usize].write(pattern);
}
for i in 0..post_dest_vec_size {
b.spare_capacity_mut()[(pre_dest_vec_size + vec_size + i) as usize].write(pattern);
}
// Manually set length, since we manually filled the vector
unsafe {
b.set_len((vec_size + pre_dest_vec_size + post_dest_vec_size) as usize);
}
// Copy the actual vector
unsafe {
memcpy(
b.as_mut_ptr().add(pre_dest_vec_size as usize).cast::<u8>(),
a.as_ptr().cast::<u8>(),
((size_of::<T>() as u32) * vec_size) as usize,
);
}
// Assert that `pattern` in pre section was not changed by memcpy
for i in 0..pre_dest_vec_size {
assert_eq!(b[i as usize], pattern);
}
// Assert that `a` was correctly copied to `b`
{
let mut i = 0; // a[i] should match b[pre_dest_vec_size + i]
let mut j: T = T::from(0).unwrap();
while i < vec_size {
assert_eq!(b[(pre_dest_vec_size + i) as usize], j);
i += 1;
j = if j == T::max_value() {
T::from(0).unwrap()
} else {
j + T::from(1).unwrap()
}
}
}
// Assert that `pattern` in post section was not changed
for i in 0..post_dest_vec_size {
assert_eq!(b[(pre_dest_vec_size + vec_size + i) as usize], pattern);
}
// Do the assertions again, but this time using `memcmp`
unsafe {
assert_eq!(
memcmp(
b.as_ptr().add(pre_dest_vec_size as usize).cast::<u8>(),
a.as_ptr().cast::<u8>(),
size_of::<T>() * vec_size as usize,
),
0
);
// pattern is larger, a[0] is 0
assert!(memcmp(b.as_ptr().cast::<u8>(), a.as_ptr().cast::<u8>(), 1) > 0);
assert!(memcmp(a.as_ptr().cast::<u8>(), b.as_ptr().cast::<u8>(), 1) < 0);
assert!(
memcmp(
b.as_ptr()
.add((vec_size + pre_dest_vec_size) as usize)
.cast::<u8>(),
a.as_ptr().cast::<u8>(),
1,
) > 0
);
}
}
#[test_case]
fn test_mem() {
mem::<u8>();
mem::<u16>();
mem::<u32>();
mem::<u64>();
mem::<u128>();
mem::<usize>();
}
#[unsafe(no_mangle)]
extern "C" fn runtime_entry(_argc: i32, _argv: *const *const u8, _env: *const *const u8) -> ! {
test_main();
common::exit(false)
}