Skip to content

Commit 5c93db1

Browse files
icmccormobraunsdorf
authored andcommitted
Switched to using dedicated alloc ID values for provenance.
1 parent 6061efe commit 5c93db1

File tree

1 file changed

+23
-17
lines changed
  • src/tools/bsan/bsan-rt/src

1 file changed

+23
-17
lines changed

src/tools/bsan/bsan-rt/src/lib.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,24 @@ impl AllocId {
7979
pub fn get(&self) -> usize {
8080
self.0
8181
}
82-
/// The minimum representable tag
83-
pub const fn zero() -> Self {
82+
/// An invalid allocation
83+
pub const fn null() -> Self {
8484
AllocId(0)
8585
}
86+
87+
/// Represents any valid allocation
88+
pub const fn wildcard() -> Self {
89+
AllocId(1)
90+
}
91+
92+
/// A global or stack allocation, which cannot be manually freed
93+
pub const fn sticky() -> Self {
94+
AllocId(2)
95+
}
96+
97+
pub const fn min() -> Self {
98+
AllocId(3)
99+
}
86100
}
87101

88102
impl fmt::Debug for AllocId {
@@ -97,19 +111,12 @@ impl fmt::Debug for AllocId {
97111
pub struct BorTag(usize);
98112

99113
impl BorTag {
100-
pub fn new(i: usize) -> Self {
114+
pub const fn new(i: usize) -> Self {
101115
BorTag(i)
102116
}
103117
pub fn get(&self) -> usize {
104118
self.0
105119
}
106-
/// The minimum representable tag
107-
pub const fn zero() -> Self {
108-
BorTag(0)
109-
}
110-
pub const fn one() -> Self {
111-
BorTag(1)
112-
}
113120
}
114121

115122
impl fmt::Debug for BorTag {
@@ -139,19 +146,18 @@ impl Provenance {
139146
/// pointers.
140147
const fn null() -> Self {
141148
Provenance {
142-
alloc_id: AllocId::zero(),
143-
bor_tag: BorTag::zero(),
149+
alloc_id: AllocId::null(),
150+
bor_tag: BorTag::new(0),
144151
alloc_info: core::ptr::null_mut(),
145152
}
146153
}
147154

148155
/// Pointers cast from integers receive a "wildcard" provenance value, which permits
149-
/// any access. A provenance value with an `alloc_id` of zero and any non-zero `bor_tag`
150-
/// is treated as a wildcard provenance value.
156+
/// any access.
151157
const fn wildcard() -> Self {
152158
Provenance {
153-
alloc_id: AllocId::zero(),
154-
bor_tag: BorTag::one(),
159+
alloc_id: AllocId::wildcard(),
160+
bor_tag: BorTag::new(0),
155161
alloc_info: core::ptr::null_mut(),
156162
}
157163
}
@@ -175,7 +181,7 @@ impl AllocInfo {
175181
/// When we deallocate an allocation, we need to invalidate its metadata.
176182
/// so that any uses-after-free are detectable.
177183
fn dealloc(&mut self) {
178-
self.alloc_id = AllocId::zero();
184+
self.alloc_id = AllocId::null();
179185
self.base_addr = 0;
180186
self.size = 0;
181187
self.align = 1;

0 commit comments

Comments
 (0)