ticktimer: only allow RecalculateSleep from ourselves

Rewrite the RecalculateSleep opcode handler so that it will only be
processed if sent by our own process.

As part of this rewrite, use the new `let else` pattern.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-01-29 18:02:36 +08:00
parent 3b7b49b4da
commit 511f77f897

View file

@ -655,8 +655,17 @@ fn main() -> ! {
} }
} }
api::Opcode::RecalculateSleep => { api::Opcode::RecalculateSleep => {
// let timeout_queue = timeout_heap.entry(msg.sender.pid()).or_default(); if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32
if let Some(args) = msg.body.scalar_message() { != xous::process::id()
{
recalculate_sleep(&mut ticktimer, &mut sleep_heap, None);
continue;
}
let Some(args) = msg.body.scalar_message() else {
continue
};
// If this is a Timeout message that fired, remove it from the Notification list // If this is a Timeout message that fired, remove it from the Notification list
let sender = args.arg1; let sender = args.arg1;
let request_kind = args.arg2; let request_kind = args.arg2;
@ -665,32 +674,40 @@ fn main() -> ! {
// If we're being asked to recalculate due to a timeout expiring, drop the sent // If we're being asked to recalculate due to a timeout expiring, drop the sent
// message from the `entries` list. // message from the `entries` list.
// the first check confirms that the origin of the RecalculateSleep message is the Ticktimer, if (request_kind == RequestKind::Timeout as usize) && (sender > 0) {
// to prevent third-party servers from issuing the command and thus distorting the sleep let mut expired_values = 0;
// calculations (since this is a public API, anything could happen). let awaiting = notify_hash
if (msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32)
== xous::process::id()
&& (request_kind == RequestKind::Timeout as usize)
&& (sender > 0)
{
let entries = notify_hash
.entry(sender_pid) .entry(sender_pid)
.or_default() .or_default()
.entry(condvar) .entry(condvar)
.or_default(); .or_default();
let mut idx = None;
for (i, val) in entries.iter().enumerate() { awaiting.retain(|e| {
if val.to_usize() == sender { if e.to_usize() == sender {
idx = Some(i); expired_values += 1;
break; false
} else {
true
}
});
// Remove each entry in the timeout set
for (_msecs, timer_request) in sleep_heap.iter() {
if timer_request.sender.to_usize() == sender {
log::error!("we were just notified of PID {:?}/condvar {:08x} expiring, yet it's still in the sleep heap with sender {:08x}", sender_pid, condvar, timer_request.sender.to_usize());
} }
} }
if let Some(idx) = idx {
entries.remove(idx); // log::info!(
} // "removed {} entries for PID {:?}/condvar {:08x}, {} to start, {} remain",
// expired_values,
// sender_pid,
// condvar,
// len_before,
// awaiting.len(),
// );
// log::trace!("new entries for PID {:?}/condvar {:08x}: {:?}", sender_pid, condvar, notify_hash.get(&sender_pid).unwrap().get(&condvar)); // log::trace!("new entries for PID {:?}/condvar {:08x}: {:?}", sender_pid, condvar, notify_hash.get(&sender_pid).unwrap().get(&condvar));
} }
}
recalculate_sleep(&mut ticktimer, &mut sleep_heap, None); recalculate_sleep(&mut ticktimer, &mut sleep_heap, None);
} }
api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, { api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, {