Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Fix UB in TokenCell
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Mar 18, 2024
1 parent d072efd commit 001b10e
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions unified-scheduler-logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ mod utils {
/// instances of [`TokenCell<V>`] conceptually owned by the instance of [`Token<V>`] (a
/// particular thread), unless previous borrow is released. After the release, the used
/// singleton token should be free to be reused for reborrows.
pub(super) fn borrow_mut<'t>(&self, _token: &'t mut Token<V>) -> &'t mut V {
///
/// Note that the returned reference's lifetime is restricted to 'self, not 'token to avoid
/// use-after-free undefined behaviors.
// As it's protected by token, it's okay to suppress this clippy lint
#[allow(clippy::mut_from_ref)]
pub(super) fn borrow_mut(&self, _token: &mut Token<V>) -> &mut V {
unsafe { &mut *self.0.get() }
}
}
Expand Down Expand Up @@ -332,10 +337,7 @@ impl TaskInner {
&self.lock_attempts
}

fn blocked_usage_count_mut<'t>(
&self,
token: &'t mut BlockedUsageCountToken,
) -> &'t mut ShortCounter {
fn blocked_usage_count_mut(&self, token: &mut BlockedUsageCountToken) -> &mut ShortCounter {
self.blocked_usage_count.borrow_mut(token)
}

Expand Down Expand Up @@ -369,10 +371,7 @@ impl LockAttempt {
}
}

fn usage_queue_mut<'t>(
&self,
usage_queue_token: &'t mut UsageQueueToken,
) -> &'t mut UsageQueueInner {
fn usage_queue_mut(&self, usage_queue_token: &mut UsageQueueToken) -> &mut UsageQueueInner {
self.usage_queue.0.borrow_mut(usage_queue_token)
}
}
Expand Down Expand Up @@ -1251,11 +1250,12 @@ mod tests {
SchedulingStateMachine::exclusively_initialize_current_thread_for_scheduling()
};
let usage_queue = UsageQueue::default();
let usage_queue_for_lock_attempt = UsageQueue::default();
let _ = SchedulingStateMachine::unlock_usage_queue(
usage_queue
.0
.borrow_mut(&mut state_machine.usage_queue_token),
&LockAttempt::new(usage_queue, RequestedUsage::Writable),
&LockAttempt::new(usage_queue_for_lock_attempt, RequestedUsage::Writable),
);
}

Expand All @@ -1270,11 +1270,12 @@ mod tests {
.0
.borrow_mut(&mut state_machine.usage_queue_token)
.current_usage = Usage::Writable;
let usage_queue_for_lock_attempt = UsageQueue::default();
let _ = SchedulingStateMachine::unlock_usage_queue(
usage_queue
.0
.borrow_mut(&mut state_machine.usage_queue_token),
&LockAttempt::new(usage_queue, RequestedUsage::Readonly),
&LockAttempt::new(usage_queue_for_lock_attempt, RequestedUsage::Readonly),
);
}

Expand All @@ -1289,11 +1290,12 @@ mod tests {
.0
.borrow_mut(&mut state_machine.usage_queue_token)
.current_usage = Usage::Readonly(ShortCounter::one());
let usage_queue_for_lock_attempt = UsageQueue::default();
let _ = SchedulingStateMachine::unlock_usage_queue(
usage_queue
.0
.borrow_mut(&mut state_machine.usage_queue_token),
&LockAttempt::new(usage_queue, RequestedUsage::Writable),
&LockAttempt::new(usage_queue_for_lock_attempt, RequestedUsage::Writable),
);
}
}

0 comments on commit 001b10e

Please sign in to comment.