diff options
author | 2022-08-05 11:28:02 +0200 | |
---|---|---|
committer | 2022-08-05 11:37:16 +0200 | |
commit | 46a3f2befd6fd821e5747ce9db112c550bc989f3 (patch) | |
tree | 384667a40772d821e9d3e95a73293254170adbb5 /macros/src/codegen/dispatchers.rs | |
parent | b48a95e87930fa51ef6fb47ad08a95d3159d9bac (diff) | |
download | rtic-46a3f2befd6fd821e5747ce9db112c550bc989f3.tar.gz rtic-46a3f2befd6fd821e5747ce9db112c550bc989f3.tar.zst rtic-46a3f2befd6fd821e5747ce9db112c550bc989f3.zip |
Fix UB in the access of `Priority` for asyc executors
The `Priority` was generated on the stack in the dispatcher
which caused it to be dropped after usage. This is now fixed
by having the `Priority` being a static variable for executors
Diffstat (limited to 'macros/src/codegen/dispatchers.rs')
-rw-r--r-- | macros/src/codegen/dispatchers.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index cc994402..62df4293 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -10,17 +10,23 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream let interrupts = &analysis.interrupts; - // Generate executor definition in global scope + // Generate executor definition and priority in global scope for (name, task) in app.software_tasks.iter() { if task.is_async { let type_name = util::internal_task_ident(name, "F"); let exec_name = util::internal_task_ident(name, "EXEC"); + let prio_name = util::internal_task_ident(name, "PRIORITY"); items.push(quote!( type #type_name = impl core::future::Future + 'static; static #exec_name: rtic::RacyCell<rtic::export::executor::AsyncTaskExecutor<#type_name>> = rtic::RacyCell::new(rtic::export::executor::AsyncTaskExecutor::new()); + + // The executors priority, this can be any value - we will overwrite it when we + // start a task + static #prio_name: rtic::RacyCell<rtic::export::Priority> = + unsafe { rtic::RacyCell::new(rtic::export::Priority::new(0)) }; )); } } @@ -92,6 +98,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream let inputs = util::inputs_ident(name); let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs); let exec_name = util::internal_task_ident(name, "EXEC"); + let prio_name = util::internal_task_ident(name, "PRIORITY"); if task.is_async { let executor_run_ident = util::executor_run_ident(name); @@ -108,7 +115,10 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream .read(); (&mut *#fq.get_mut()).split().0.enqueue_unchecked(index); - let priority = &rtic::export::Priority::new(PRIORITY); + // The async executor needs a static priority + #prio_name.get_mut().write(rtic::export::Priority::new(PRIORITY)); + let priority: &'static _ = &*#prio_name.get(); + (&mut *#exec_name.get_mut()).spawn(#name(#name::Context::new(priority) #(,#pats)*)); #executor_run_ident.store(true, core::sync::atomic::Ordering::Relaxed); } else { |