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
use prelude::*;
use core::ptr;
use core::convert::TryInto;
use shim::{syscalls, config};
use {sync, fail};
static BRK_MUTEX: Mutex<BrkState> = Mutex::new(BrkState {
current_brk: None,
});
struct BrkState {
current_brk: Option<Pointer<u8>>,
}
pub struct BrkLock {
state: sync::MutexGuard<'static, BrkState>,
}
impl BrkLock {
unsafe fn sbrk(&mut self, size: isize) -> Result<Pointer<u8>, ()> {
log!(NOTE, "Incrementing the program break by {} bytes.", size);
let expected_brk = self.current_brk().offset(size);
let old_brk = Pointer::new(syscalls::brk(*expected_brk as *const u8) as *mut u8);
log!(INTERNAL, "Program break set.");
if expected_brk == old_brk {
self.state.current_brk = Some(expected_brk.clone());
Ok(old_brk)
} else {
Err(())
}
}
#[allow(cast_possible_wrap)]
pub fn release(&mut self, block: Block) -> Result<(), Block> {
if self.current_brk() == Pointer::from(block.empty_right()) {
log!(DEBUG, "Releasing {:?} to the OS.", block);
let res = unsafe {
self.sbrk(-(block.size() as isize))
};
debug_assert!(res.is_ok(), "Failed to set the program break back.");
Ok(())
} else {
log!(DEBUG, "Unable to release {:?} to the OS.", block);
Err(block)
}
}
fn current_brk(&mut self) -> Pointer<u8> {
if let Some(ref cur) = self.state.current_brk {
let res = cur.clone();
debug_assert!(res == current_brk(), "The cached program break is out of sync with the \
actual program break. Are you interfering with BRK? If so, prefer the \
provided 'sbrk' instead, then.");
return res;
}
let cur = current_brk();
self.state.current_brk = Some(cur.clone());
cur
}
pub fn canonical_brk(&mut self, size: usize, align: usize) -> (Block, Block, Block) {
let brk_size = size + config::extra_brk(size) + align;
let (alignment_block, rest) = unsafe {
Block::from_raw_parts(
self.sbrk(brk_size.try_into().unwrap()).unwrap_or_else(|()| fail::oom()),
brk_size,
)
}.align(align).unwrap();
let (res, excessive) = rest.split(size);
debug_assert!(res.aligned_to(align), "Alignment failed.");
debug_assert!(res.size() + alignment_block.size() + excessive.size() == brk_size, "BRK memory leak.");
(alignment_block, res, excessive)
}
}
pub fn lock() -> BrkLock {
BrkLock {
state: BRK_MUTEX.lock(),
}
}
pub unsafe extern fn sbrk(size: isize) -> *mut u8 {
*lock().sbrk(size).unwrap_or_else(|()| Pointer::new(!0 as *mut u8))
}
fn current_brk() -> Pointer<u8> {
unsafe {
Pointer::new(syscalls::brk(ptr::null()) as *mut u8)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ordered() {
let brk = lock().canonical_brk(20, 1);
assert!(brk.0 <= brk.1);
assert!(brk.1 <= brk.2);
}
#[test]
fn test_brk_grow_up() {
unsafe {
let brk1 = lock().sbrk(5).unwrap();
let brk2 = lock().sbrk(100).unwrap();
assert!(*brk1 < *brk2);
}
}
}