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
use hybrid_array::{Array, ArraySize};
use vgzzq::memcheck::{make_mem_defined, make_mem_undefined};
/// Defines valgrind wrappers that allow checking constant-time implementation.
pub trait Classifier {
/// Makes valgrind see underlying memory as addressable but undefined.
///
/// Can be used in combination with valgrind to verify constant-time implementation.
/// NOTE: this wrapper should mimic behaviors of the VALGRIND_MAKE_MEMORY_UNDEFINED macro (see valgrind/memcheck.h).
fn classify(&self);
/// Makes valgrind see underlying memory as addressable and defined.
///
/// Can be used in combination with valgrind to verify constant-time implementation.
/// NOTE: this wrapper should mimic behaviors of the VALGRIND_MAKE_MEMORY_DEFINED macro (see valgrind/memcheck.h).
fn declassify(&self);
}
impl<T> Classifier for [T] {
#[inline(always)]
fn classify(&self) {
unsafe {
make_mem_undefined(self.as_ptr(), self.len());
}
}
#[inline(always)]
fn declassify(&self) {
unsafe {
make_mem_defined(self.as_ptr(), self.len());
}
}
}
impl<T, const N: usize> Classifier for [T; N] {
#[inline(always)]
fn classify(&self) {
self[..].classify()
}
#[inline(always)]
fn declassify(&self) {
self[..].declassify()
}
}
impl<T> Classifier for Vec<T> {
#[inline(always)]
fn classify(&self) {
unsafe {
make_mem_undefined(self.as_ptr(), self.len());
}
}
#[inline(always)]
fn declassify(&self) {
unsafe {
make_mem_defined(self.as_ptr(), self.len());
}
}
}
impl<T, S> Classifier for Array<T, S>
where
S: ArraySize,
{
#[inline(always)]
fn classify(&self) {
unsafe {
make_mem_undefined(self.as_ptr(), self.len());
}
}
#[inline(always)]
fn declassify(&self) {
unsafe {
make_mem_defined(self.as_ptr(), self.len());
}
}
}
impl<T, S> Classifier for &Array<T, S>
where
S: ArraySize,
{
#[inline(always)]
fn classify(&self) {
unsafe {
make_mem_undefined(self.as_ptr(), self.len());
}
}
#[inline(always)]
fn declassify(&self) {
unsafe {
make_mem_defined(self.as_ptr(), self.len());
}
}
}