-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathaddress.rs
553 lines (484 loc) · 15.8 KB
/
address.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use atomic_traits::Atomic;
use std::fmt;
use std::mem;
use std::ops::*;
use std::sync::atomic::Ordering;
use crate::mmtk::{MMAPPER, SFT_MAP};
use crate::util::heap::layout::mmapper::Mmapper;
/// size in bytes
pub type ByteSize = usize;
/// offset in byte
pub type ByteOffset = isize;
/// Address represents an arbitrary address. This is designed to represent
/// address and do address arithmetic mostly in a safe way, and to allow
/// mark some operations as unsafe. This type needs to be zero overhead
/// (memory wise and time wise). The idea is from the paper
/// High-level Low-level Programming (VEE09) and JikesRVM.
#[repr(transparent)]
#[derive(Copy, Clone, Eq, Hash, PartialOrd, Ord, PartialEq)]
pub struct Address(usize);
/// Address + ByteSize (positive)
impl Add<ByteSize> for Address {
type Output = Address;
fn add(self, offset: ByteSize) -> Address {
Address(self.0 + offset)
}
}
/// Address += ByteSize (positive)
impl AddAssign<ByteSize> for Address {
fn add_assign(&mut self, offset: ByteSize) {
self.0 += offset;
}
}
/// Address + ByteOffset (positive or negative)
impl Add<ByteOffset> for Address {
type Output = Address;
fn add(self, offset: ByteOffset) -> Address {
Address((self.0 as isize + offset) as usize)
}
}
/// Address += ByteOffset (positive or negative)
impl AddAssign<ByteOffset> for Address {
fn add_assign(&mut self, offset: ByteOffset) {
self.0 = (self.0 as isize + offset) as usize
}
}
/// Address - ByteSize (positive)
impl Sub<ByteSize> for Address {
type Output = Address;
fn sub(self, offset: ByteSize) -> Address {
Address(self.0 - offset)
}
}
/// Address -= ByteSize (positive)
impl SubAssign<ByteSize> for Address {
fn sub_assign(&mut self, offset: ByteSize) {
self.0 -= offset;
}
}
/// Address - Address (the first address must be higher)
impl Sub<Address> for Address {
type Output = ByteSize;
fn sub(self, other: Address) -> ByteSize {
debug_assert!(
self.0 >= other.0,
"for (addr_a - addr_b), a({}) needs to be larger than b({})",
self,
other
);
self.0 - other.0
}
}
/// Address & mask
impl BitAnd<usize> for Address {
type Output = usize;
fn bitand(self, other: usize) -> usize {
self.0 & other
}
}
// Be careful about the return type here. Address & u8 = u8
// This is different from Address | u8 = usize
impl BitAnd<u8> for Address {
type Output = u8;
fn bitand(self, other: u8) -> u8 {
(self.0 as u8) & other
}
}
/// Address | mask
impl BitOr<usize> for Address {
type Output = usize;
fn bitor(self, other: usize) -> usize {
self.0 | other
}
}
// Be careful about the return type here. Address | u8 = size
// This is different from Address & u8 = u8
impl BitOr<u8> for Address {
type Output = usize;
fn bitor(self, other: u8) -> usize {
self.0 | (other as usize)
}
}
/// Address >> shift (get an index)
impl Shr<usize> for Address {
type Output = usize;
fn shr(self, shift: usize) -> usize {
self.0 >> shift
}
}
/// Address << shift (get an index)
impl Shl<usize> for Address {
type Output = usize;
fn shl(self, shift: usize) -> usize {
self.0 << shift
}
}
impl Address {
pub const ZERO: Self = Address(0);
pub const MAX: Self = Address(usize::max_value());
/// creates Address from a pointer
#[inline(always)]
pub fn from_ptr<T>(ptr: *const T) -> Address {
Address(ptr as usize)
}
#[inline(always)]
pub fn from_ref<T>(r: &T) -> Address {
Address(r as *const T as usize)
}
/// creates Address from a mutable pointer
#[inline(always)]
pub fn from_mut_ptr<T>(ptr: *mut T) -> Address {
Address(ptr as usize)
}
/// creates a null Address (0)
/// # Safety
/// It is unsafe and the user needs to be aware that they are creating an invalid address.
/// The zero address should only be used as unininitialized or sentinel values in performance critical code (where you dont want to use Option<Address>).
#[inline(always)]
pub const unsafe fn zero() -> Address {
Address(0)
}
/// creates an Address of (usize::MAX)
/// # Safety
/// It is unsafe and the user needs to be aware that they are creating an invalid address.
/// The max address should only be used as unininitialized or sentinel values in performance critical code (where you dont want to use Option<Address>).
#[inline(always)]
pub unsafe fn max() -> Address {
use std::usize;
Address(usize::MAX)
}
/// creates an arbitrary Address
/// # Safety
/// It is unsafe and the user needs to be aware that they may create an invalid address.
/// This creates arbitrary addresses which may not be valid. This should only be used for hard-coded addresses. Any other uses of this function could be
/// replaced with more proper alternatives.
#[inline(always)]
pub const unsafe fn from_usize(raw: usize) -> Address {
Address(raw)
}
/// shifts the address by N T-typed objects (returns addr + N * size_of(T))
#[inline(always)]
pub fn shift<T>(self, offset: isize) -> Self {
self + mem::size_of::<T>() as isize * offset
}
// These const functions are duplicated with the operator traits. But we need them,
// as we need them to declare constants.
#[inline(always)]
pub const fn get_extent(self, other: Address) -> ByteSize {
self.0 - other.0
}
#[inline(always)]
pub const fn get_offset(self, other: Address) -> ByteOffset {
self.0 as isize - other.0 as isize
}
// We implemented the Add trait but we still keep this add function.
// The add() function is const fn, and we can use it to declare Address constants.
// The Add trait function cannot be const.
#[allow(clippy::should_implement_trait)]
#[inline(always)]
pub const fn add(self, size: usize) -> Address {
Address(self.0 + size)
}
// We implemented the Sub trait but we still keep this sub function.
// The sub() function is const fn, and we can use it to declare Address constants.
// The Sub trait function cannot be const.
#[allow(clippy::should_implement_trait)]
#[inline(always)]
pub const fn sub(self, size: usize) -> Address {
Address(self.0 - size)
}
// Perform a saturating subtract on the Address
pub const fn saturating_sub(self, size: usize) -> Address {
Address(self.0.saturating_sub(size))
}
/// loads a value of type T from the address
/// # Safety
/// This could throw a segment fault if the address is invalid
#[inline(always)]
pub unsafe fn load<T: Copy>(self) -> T {
*(self.0 as *mut T)
}
/// stores a value of type T to the address
/// # Safety
/// This could throw a segment fault if the address is invalid
#[inline(always)]
pub unsafe fn store<T>(self, value: T) {
*(self.0 as *mut T) = value;
}
/// atomic operation: load
/// # Safety
/// This could throw a segment fault if the address is invalid
#[inline(always)]
pub unsafe fn atomic_load<T: Atomic>(self, order: Ordering) -> T::Type {
let loc = &*(self.0 as *const T);
loc.load(order)
}
/// atomic operation: store
/// # Safety
/// This could throw a segment fault if the address is invalid
#[inline(always)]
pub unsafe fn atomic_store<T: Atomic>(self, val: T::Type, order: Ordering) {
let loc = &*(self.0 as *const T);
loc.store(val, order)
}
/// atomic operation: compare and exchange usize
/// # Safety
/// This could throw a segment fault if the address is invalid
#[inline(always)]
pub unsafe fn compare_exchange<T: Atomic>(
self,
old: T::Type,
new: T::Type,
success: Ordering,
failure: Ordering,
) -> Result<T::Type, T::Type> {
let loc = &*(self.0 as *const T);
loc.compare_exchange(old, new, success, failure)
}
/// is this address zero?
#[inline(always)]
pub fn is_zero(self) -> bool {
self.0 == 0
}
/// aligns up the address to the given alignment
#[inline(always)]
pub const fn align_up(self, align: ByteSize) -> Address {
use crate::util::conversions;
Address(conversions::raw_align_up(self.0, align))
}
/// aligns down the address to the given alignment
#[inline(always)]
pub const fn align_down(self, align: ByteSize) -> Address {
use crate::util::conversions;
Address(conversions::raw_align_down(self.0, align))
}
/// is this address aligned to the given alignment
pub fn is_aligned_to(self, align: usize) -> bool {
use crate::util::conversions;
conversions::raw_is_aligned(self.0, align)
}
/// converts the Address into an ObjectReference
/// # Safety
/// We would expect ObjectReferences point to valid objects,
/// but an arbitrary Address may not reside an object. This conversion is unsafe,
/// and it is the user's responsibility to ensure the safety.
#[inline(always)]
pub unsafe fn to_object_reference(self) -> ObjectReference {
mem::transmute(self.0)
}
/// converts the Address to a pointer
#[inline(always)]
pub fn to_ptr<T>(self) -> *const T {
self.0 as *const T
}
/// converts the Address to a mutable pointer
#[inline(always)]
pub fn to_mut_ptr<T>(self) -> *mut T {
self.0 as *mut T
}
/// converts the Address to a Rust reference
///
/// # Safety
/// The caller must guarantee the address actually points to a Rust object.
#[inline(always)]
pub unsafe fn as_ref<'a, T>(self) -> &'a T {
&*self.to_mut_ptr()
}
/// converts the Address to a pointer-sized integer
#[inline(always)]
pub const fn as_usize(self) -> usize {
self.0
}
/// returns the chunk index for this address
#[inline(always)]
pub fn chunk_index(self) -> usize {
use crate::util::conversions;
conversions::address_to_chunk_index(self)
}
/// return true if the referenced memory is mapped
pub fn is_mapped(self) -> bool {
if self.0 == 0 {
false
} else {
MMAPPER.is_mapped_address(self)
}
}
}
/// allows print Address as upper-case hex value
impl fmt::UpperHex for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:X}", self.0)
}
}
/// allows print Address as lower-case hex value
impl fmt::LowerHex for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}
/// allows Display format the Address (as upper-case hex value with 0x prefix)
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}
/// allows Debug format the Address (as upper-case hex value with 0x prefix)
impl fmt::Debug for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}
#[cfg(test)]
mod tests {
use crate::util::Address;
#[test]
fn align_up() {
unsafe {
assert_eq!(
Address::from_usize(0x10).align_up(0x10),
Address::from_usize(0x10)
);
assert_eq!(
Address::from_usize(0x11).align_up(0x10),
Address::from_usize(0x20)
);
assert_eq!(
Address::from_usize(0x20).align_up(0x10),
Address::from_usize(0x20)
);
}
}
#[test]
fn align_down() {
unsafe {
assert_eq!(
Address::from_usize(0x10).align_down(0x10),
Address::from_usize(0x10)
);
assert_eq!(
Address::from_usize(0x11).align_down(0x10),
Address::from_usize(0x10)
);
assert_eq!(
Address::from_usize(0x20).align_down(0x10),
Address::from_usize(0x20)
);
}
}
#[test]
fn is_aligned_to() {
unsafe {
assert!(Address::from_usize(0x10).is_aligned_to(0x10));
assert!(!Address::from_usize(0x11).is_aligned_to(0x10));
assert!(Address::from_usize(0x10).is_aligned_to(0x8));
assert!(!Address::from_usize(0x10).is_aligned_to(0x20));
}
}
#[test]
fn bit_and() {
unsafe {
assert_eq!(
Address::from_usize(0b1111_1111_1100usize) & 0b1010u8,
0b1000u8
);
assert_eq!(
Address::from_usize(0b1111_1111_1100usize) & 0b1000_0000_1010usize,
0b1000_0000_1000usize
);
}
}
#[test]
fn bit_or() {
unsafe {
assert_eq!(
Address::from_usize(0b1111_1111_1100usize) | 0b1010u8,
0b1111_1111_1110usize
);
assert_eq!(
Address::from_usize(0b1111_1111_1100usize) | 0b1000_0000_1010usize,
0b1111_1111_1110usize
);
}
}
}
/// ObjectReference represents address for an object. Compared with Address,
/// operations allowed on ObjectReference are very limited. No address arithmetics
/// are allowed for ObjectReference. The idea is from the paper
/// High-level Low-level Programming (VEE09) and JikesRVM.
#[repr(transparent)]
#[derive(Copy, Clone, Eq, Hash, PartialOrd, PartialEq)]
pub struct ObjectReference(usize);
impl ObjectReference {
pub const NULL: ObjectReference = ObjectReference(0);
/// converts the ObjectReference to an Address
#[inline(always)]
pub fn to_address(self) -> Address {
Address(self.0)
}
/// is this object reference null reference?
#[inline(always)]
pub fn is_null(self) -> bool {
self.0 == 0
}
/// returns the ObjectReference
pub fn value(self) -> usize {
self.0
}
/// Is the object reachable, determined by the policy?
/// Note: Objects in ImmortalSpace may have `is_live = true` but are actually unreachable.
#[inline(always)]
pub fn is_reachable(self) -> bool {
if self.is_null() {
false
} else {
SFT_MAP.get(Address(self.0)).is_reachable(self)
}
}
/// Is the object live, determined by the policy?
pub fn is_live(self) -> bool {
if self.0 == 0 {
false
} else {
SFT_MAP.get(Address(self.0)).is_live(self)
}
}
pub fn is_movable(self) -> bool {
SFT_MAP.get(Address(self.0)).is_movable()
}
/// Get forwarding pointer if the object is forwarded.
#[inline(always)]
pub fn get_forwarded_object(self) -> Option<Self> {
SFT_MAP.get(Address(self.0)).get_forwarded_object(self)
}
pub fn is_in_any_space(self) -> bool {
SFT_MAP.is_in_any_space(self)
}
#[cfg(feature = "sanity")]
pub fn is_sane(self) -> bool {
SFT_MAP.get(Address(self.0)).is_sane()
}
}
/// allows print Address as upper-case hex value
impl fmt::UpperHex for ObjectReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:X}", self.0)
}
}
/// allows print Address as lower-case hex value
impl fmt::LowerHex for ObjectReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}
/// allows Display format the Address (as upper-case hex value with 0x prefix)
impl fmt::Display for ObjectReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}
/// allows Debug format the Address (as upper-case hex value with 0x prefix)
impl fmt::Debug for ObjectReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}