+25
.tangled/workflows/checks.yaml
+25
.tangled/workflows/checks.yaml
···
1
+
when:
2
+
- event: ["manual", "push"]
3
+
branch: ["main"]
4
+
5
+
engine: "nixery"
6
+
7
+
# default clone
8
+
9
+
dependencies:
10
+
nixpkgs:
11
+
- rustup
12
+
- cargo-mutants
13
+
# need a linker
14
+
- gcc
15
+
16
+
steps:
17
+
- name: "install rust with miri"
18
+
command: "rustup toolchain install nightly -c miri,rust-src --profile minimal"
19
+
- name: "run regular tests"
20
+
command: "cargo test"
21
+
- name: "run mutants"
22
+
command: 'cargo mutants -E "$(cat known_mutants_regex.txt)"'
23
+
- name: "run miri tests"
24
+
command: "cargo +nightly miri test"
25
+
+6
-1
CHANGELOG.md
+6
-1
CHANGELOG.md
···
3
3
This file is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
4
4
This project follows semver and every release is checked by cargo-semver-checks.
5
5
6
-
## [Unreleased] (not breaking)
6
+
## [0.2.3] - 2025-10-01
7
+
8
+
### Fixed
9
+
- detect when a guard was forgotten to avoid UB
10
+
11
+
## [0.2.2] - 2025-08-09
7
12
8
13
### Changed
9
14
- remove '#[inline]' annotations from public functions (mtomsoop)
+2
-2
Cargo.toml
+2
-2
Cargo.toml
···
1
1
[package]
2
2
name = "simple-left-right"
3
-
version = "0.2.1"
3
+
version = "0.2.3"
4
4
edition = "2021"
5
5
rust-version = "1.82"
6
6
readme = "README.md"
···
10
10
license = "MIT OR Apache-2.0"
11
11
repository = "https://tangled.sh/did:plc:54jgbo4psy24qu2bk4njtpc4/simple-left-right/"
12
12
description = "Lockfree, realtime safe and copy-free Synchronisation"
13
-
# workspace = "../"
13
+
exclude = ["known_mutants_regex.txt", ".tangled", "mutants.out", "mutants.out.old"]
14
14
15
15
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+10
README.md
+10
README.md
···
13
13
as the write locking spins forever.
14
14
15
15
PRs should keep this state as much as possible.
16
+
17
+
## std::mem::forget
18
+
Forgetting a Read/Write guard could lead to UB, so i detect that and panic if it happens. In order to make
19
+
the detection cheap it doesn't differentiate between cases where a forget would lead to UB and cases where
20
+
it doesn't. Just don't forget the guards and it won't panic.
21
+
22
+
## Git
23
+
This project is hosted on [tangled](https://tangled.org/did:plc:54jgbo4psy24qu2bk4njtpc4/simple-left-right/),
24
+
[github](https://github.com/luca3s/simple-left-right) and [codeberg](https://codeberg.org/increasing/simple-left-right).
25
+
You can create issues and PRs on any platform you like.
+1
known_mutants_regex.txt
+1
known_mutants_regex.txt
···
1
+
(replace \| with \^ in State::with_read|replace Shared<T>::release_read_lock with \(\)|replace State::can_write -> bool with false|replace Writer<T, O>::try_lock -> Option<WriteGuard<'_, T, O>> with None|replace <impl Drop for ReadGuard<'_, T>>::drop with \(\))
+29
-25
src/lib.rs
+29
-25
src/lib.rs
···
36
36
#[derive(Debug)]
37
37
pub struct Reader<T> {
38
38
shared: NonNull<Shared<T>>,
39
+
locked: bool,
39
40
/// for drop check
40
41
_own: PhantomData<Shared<T>>,
41
42
}
···
49
50
50
51
/// this function never blocks. (`fetch_update` loop doesn't count)
51
52
pub fn lock(&mut self) -> ReadGuard<'_, T> {
52
-
let shared_ref = self.shared_ref();
53
-
53
+
if self.locked {
54
+
self.locked = false;
55
+
panic!("ReadGuard was forgotten");
56
+
}
57
+
self.locked = true;
58
+
// SAFETY: value just locked
59
+
let value = unsafe { &*self.shared_ref().lock_read().get() };
54
60
ReadGuard {
55
-
shared: shared_ref,
56
-
value: shared_ref.lock_read(),
57
-
reader: PhantomData,
61
+
value,
62
+
reader: self,
58
63
}
59
64
}
60
65
}
···
66
71
fn drop(&mut self) {
67
72
// SAFETY: self.shared is valid and not used after this.
68
73
unsafe { Shared::drop(self.shared) };
74
+
assert!(!self.locked, "ReadGuard was forgotten");
69
75
}
70
76
}
71
77
···
75
81
/// Doesn't implement Clone as that would require refcounting to know when to unlock.
76
82
#[derive(Debug)]
77
83
pub struct ReadGuard<'a, T> {
78
-
shared: &'a Shared<T>,
79
-
value: Ptr,
80
-
/// `PhantomData` makes the borrow checker prove that there only ever is one `ReadGuard`.
81
-
/// This allows resetting the readstate without some kind of counter
82
-
reader: PhantomData<&'a mut Reader<T>>,
84
+
reader: &'a mut Reader<T>,
85
+
value: &'a T,
83
86
}
84
87
85
88
impl<T> Deref for ReadGuard<'_, T> {
86
89
type Target = T;
87
90
88
91
fn deref(&self) -> &Self::Target {
89
-
// SAFETY: ReadGuard was created, so the Writer knows not to write in this spot
90
-
unsafe { self.shared.get_value_ref(self.value) }
92
+
self.value
91
93
}
92
94
}
93
95
···
101
103
}
102
104
}
103
105
104
-
// /// SAFETY: behaves like a ref to T. https://doc.rust-lang.org/std/marker/trait.Sync.html
105
-
// unsafe impl<T: Sync> Send for ReadGuard<'_, T> {}
106
-
// /// SAFETY: behaves like a ref to T. https://doc.rust-lang.org/std/marker/trait.Sync.html
107
-
// unsafe impl<T: Sync> Sync for ReadGuard<'_, T> {}
108
-
109
106
impl<T> Drop for ReadGuard<'_, T> {
110
107
fn drop(&mut self) {
111
108
// release the read lock
112
-
self.shared.release_read_lock();
109
+
self.reader.shared_ref().release_read_lock();
110
+
self.reader.locked = false;
113
111
}
114
112
}
115
113
···
122
120
write_ptr: Ptr,
123
121
// buffer is pushed at the back and popped at the front.
124
122
op_buffer: VecDeque<O>,
123
+
locked: bool,
125
124
// needed for drop_check
126
125
_own: PhantomData<Shared<T>>,
127
126
}
···
162
161
Reader {
163
162
shared: self.shared,
164
163
_own: PhantomData,
164
+
locked: false,
165
165
}
166
166
})
167
167
}
···
171
171
impl<T: Absorb<O>, O> Writer<T, O> {
172
172
/// doesn't block. Returns None if the Reader has a `ReadGuard` pointing to the old value.
173
173
pub fn try_lock(&mut self) -> Option<WriteGuard<'_, T, O>> {
174
+
if self.locked {
175
+
self.locked = false;
176
+
panic!("WriteGuard was forgotten");
177
+
}
174
178
self.shared_ref()
175
179
.lock_write(self.write_ptr)
176
180
.ok()
177
181
// locking was successful
178
182
.map(|()| {
179
-
// WriteGuard::new(self)
183
+
self.locked = true;
180
184
let mut guard = WriteGuard { writer: self };
181
185
while let Some(operation) = guard.writer.op_buffer.pop_front() {
182
186
guard.get_data_mut().absorb(operation);
···
196
200
write_ptr,
197
201
op_buffer: VecDeque::new(),
198
202
_own: PhantomData,
203
+
locked: false,
199
204
}
200
205
}
201
206
}
···
211
216
write_ptr,
212
217
op_buffer: VecDeque::new(),
213
218
_own: PhantomData,
219
+
locked: false,
214
220
}
215
221
}
216
222
}
···
239
245
fn drop(&mut self) {
240
246
// SAFETY: self.shared is valid and not used after this.
241
247
unsafe { Shared::drop(self.shared) };
248
+
assert!(!self.locked, "WriteGuard was forgotten");
242
249
}
243
250
}
244
251
···
250
257
/// Dropping this makes all changes available to the Reader.
251
258
#[derive(Debug)]
252
259
pub struct WriteGuard<'a, T, O> {
260
+
// can't hold a mut ref to T, as then it wouldn't be possible to write to both at the same time,
261
+
// which is an optimization i want to keep.
253
262
writer: &'a mut Writer<T, O>,
254
263
}
255
264
···
298
307
}
299
308
}
300
309
301
-
// /// SAFETY: behaves like a &mut T and &mut Vec<O>. https://doc.rust-lang.org/stable/std/marker/trait.Sync.html
302
-
// unsafe impl<T: Send, O: Send> Send for WriteGuard<'_, T, O> {}
303
-
304
-
// /// Safety: can only create shared refs to T, not to O. https://doc.rust-lang.org/stable/std/marker/trait.Sync.html
305
-
// unsafe impl<T: Sync, O> Sync for WriteGuard<'_, T, O> {}
306
-
307
310
impl<T, O> Drop for WriteGuard<'_, T, O> {
308
311
fn drop(&mut self) {
309
312
self.writer.swap();
313
+
self.writer.locked = false;
310
314
}
311
315
}
312
316
+37
tests/tests.rs
+37
tests/tests.rs
···
110
110
}
111
111
112
112
#[test]
113
+
fn assert_writeguard_none() {
114
+
let mut writer = Writer::new(0);
115
+
let mut reader = writer.build_reader().unwrap();
116
+
let read_lock = reader.lock();
117
+
let mut write_lock = writer.try_lock().unwrap();
118
+
write_lock.apply_op(CounterAddOp(1));
119
+
assert_eq!(*read_lock, 0);
120
+
drop(write_lock);
121
+
// read lock is still being held, so can't lock
122
+
assert!(writer.try_lock().is_none());
123
+
drop(read_lock);
124
+
// now do it all again, to also trigger code paths for the second value
125
+
let read_lock = reader.lock();
126
+
let mut write_lock = writer.try_lock().unwrap();
127
+
write_lock.apply_op(CounterAddOp(1));
128
+
assert_eq!(*read_lock, 1);
129
+
drop(write_lock);
130
+
// read lock is still being held, so can't lock
131
+
assert!(writer.try_lock().is_none());
132
+
}
133
+
134
+
#[test]
113
135
fn block() {
114
136
let mut writer = Writer::new(0);
115
137
let mut reader = writer.build_reader().unwrap();
···
302
324
drop(reader);
303
325
writer.apply_op(SetToNone);
304
326
}
327
+
}
328
+
329
+
#[test]
330
+
#[should_panic]
331
+
fn forget_lock() {
332
+
let mut writer: Writer<i32, CounterAddOp> = Writer::new(0);
333
+
let mut reader = writer.build_reader().unwrap();
334
+
335
+
let write = writer.try_lock().unwrap();
336
+
core::mem::forget(write);
337
+
let _ = writer.try_lock().unwrap();
338
+
339
+
let read = reader.lock();
340
+
core::mem::forget(read);
341
+
reader.lock();
305
342
}
306
343
}