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
264
265
266
267
268
#![deny(missing_docs)]
#[macro_use]
extern crate bitflags;
extern crate libc;
pub use error::{Error, Result};
pub use lock::{lock, unlock, LockGuard};
pub use protect::{protect, protect_with_handle, ProtectGuard, Protection};
mod error;
mod lock;
mod os;
pub mod page;
mod protect;
#[derive(Debug, Clone, Copy)]
pub struct Region {
pub base: *const u8,
pub guarded: bool,
pub protection: Protection,
pub shared: bool,
pub size: usize,
}
impl Region {
pub fn lower(&self) -> usize {
self.base as usize
}
pub fn upper(&self) -> usize {
self.lower() + self.size
}
}
unsafe impl Send for Region {}
unsafe impl Sync for Region {}
pub fn query(address: *const u8) -> Result<Region> {
if address.is_null() {
return Err(Error::NullAddress);
}
os::get_region(page::floor(address as usize) as *const u8)
}
pub fn query_range(address: *const u8, size: usize) -> Result<Vec<Region>> {
if size == 0 {
return Err(Error::EmptyRange);
}
let mut result = Vec::new();
let mut base = page::floor(address as usize);
let limit = address as usize + size;
loop {
let region = query(base as *const u8)?;
result.push(region);
base = region.upper();
if limit <= region.upper() {
break;
}
}
Ok(result)
}
#[cfg(test)]
mod tests {
extern crate memmap;
use self::memmap::MmapMut;
use super::*;
pub fn alloc_pages(prots: &[Protection]) -> MmapMut {
let pz = page::size();
let map = MmapMut::map_anon(pz * prots.len()).unwrap();
let mut base = map.as_ptr();
for protection in prots {
unsafe {
protect(base, pz, *protection).unwrap();
base = base.offset(pz as isize);
}
}
map
}
#[test]
fn query_null() {
assert!(query(::std::ptr::null()).is_err());
}
#[test]
fn query_code() {
let region = query(query_code as *const () as *const u8).unwrap();
assert_eq!(region.guarded, false);
assert_eq!(region.shared, cfg!(windows));
}
#[test]
fn query_alloc() {
let size = page::size() * 2;
let mut map = alloc_pages(&[Protection::READ_EXECUTE, Protection::READ_EXECUTE]);
let region = query(map.as_ptr()).unwrap();
assert_eq!(region.guarded, false);
assert_eq!(region.protection, Protection::READ_EXECUTE);
assert!(!region.base.is_null() && region.base <= map.as_mut_ptr());
assert!(region.size >= size);
}
#[test]
fn query_area_zero() {
assert!(query_range(&query_area_zero as *const _ as *const u8, 0).is_err());
}
#[test]
fn query_area_overlap() {
let pz = page::size();
let prots = [Protection::READ_EXECUTE, Protection::READ_WRITE];
let map = alloc_pages(&prots);
let address = unsafe { map.as_ptr().offset(pz as isize - 1) };
let result = query_range(address, 2).unwrap();
assert_eq!(result.len(), prots.len());
for i in 0..prots.len() {
assert_eq!(result[i].protection, prots[i]);
}
}
#[test]
fn query_area_alloc() {
let pz = page::size();
let prots = [
Protection::READ,
Protection::READ_WRITE,
Protection::READ_EXECUTE,
];
let map = alloc_pages(&prots);
let result = query_range(map.as_ptr(), pz).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].protection, prots[0]);
let result = query_range(map.as_ptr(), pz * prots.len()).unwrap();
assert_eq!(result.len(), prots.len());
assert_eq!(result[1].size, pz);
for i in 0..prots.len() {
assert_eq!(result[i].protection, prots[i]);
}
}
}