From d30bdcb096774c1f56d9823fb2fbb78bf5cd3584 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 9 Dec 2017 17:14:51 +0100 Subject: [PATCH] safe `&'static mut` references via init.resources --- examples/safe-static-mut-ref.rs | 32 ++++++++++++++++++++++ macros/Cargo.toml | 3 ++- macros/src/check.rs | 31 +++++++++++++++++++++ macros/src/trans.rs | 30 ++++++++++++++------- tests/cfail/init-resource-share-idle.rs | 31 +++++++++++++++++++++ tests/cfail/init-resource-share-task.rs | 36 +++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 examples/safe-static-mut-ref.rs create mode 100644 tests/cfail/init-resource-share-idle.rs create mode 100644 tests/cfail/init-resource-share-task.rs diff --git a/examples/safe-static-mut-ref.rs b/examples/safe-static-mut-ref.rs new file mode 100644 index 0000000000..bb87212281 --- /dev/null +++ b/examples/safe-static-mut-ref.rs @@ -0,0 +1,32 @@ +//! Safe creation of `&'static mut` references +#![deny(unsafe_code)] +#![deny(warnings)] +#![feature(proc_macro)] +#![no_std] + +extern crate cortex_m_rtfm as rtfm; +extern crate stm32f103xx; + +use rtfm::app; + +app! { + device: stm32f103xx, + + resources: { + static BUFFER: [u8; 16] = [0; 16]; + }, + + init: { + resources: [BUFFER], + }, +} + +fn init(_p: init::Peripherals, r: init::Resources) { + let _buf: &'static mut [u8] = r.BUFFER; +} + +fn idle() -> ! { + loop { + rtfm::wfi(); + } +} diff --git a/macros/Cargo.toml b/macros/Cargo.toml index d51cbc2629..d2e4da5b6b 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -12,7 +12,8 @@ version = "0.2.1" [dependencies] error-chain = "0.10.0" quote = "0.3.15" -rtfm-syntax = "0.2.0" +# rtfm-syntax = "0.2.0" +rtfm-syntax = { git = "https://github.com/japaric/rtfm-syntax", branch = "init-resources" } syn = "0.11.11" [lib] diff --git a/macros/src/check.rs b/macros/src/check.rs index 63cac1fa5e..f6fd9cc609 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -77,7 +77,38 @@ pub fn app(app: check::App) -> Result { } fn resources(app: &App) -> Result<()> { + for name in &app.init.resources { + if let Some(resource) = app.resources.get(name) { + ensure!( + resource.expr.is_some(), + "resource `{}`, allocated to `init`, must have an initial value", + name + ); + } else { + bail!( + "resource `{}`, allocated to `init`, must be a data resource", + name + ); + } + + ensure!( + !app.idle.resources.contains(name), + "resources assigned to `init` can't be shared with `idle`" + ); + + ensure!( + app.tasks + .iter() + .all(|(_, task)| !task.resources.contains(name)), + "resources assigned to `init` can't be shared with tasks" + ) + } + for resource in app.resources.keys() { + if app.init.resources.contains(resource) { + continue; + } + if app.idle.resources.contains(resource) { continue; } diff --git a/macros/src/trans.rs b/macros/src/trans.rs index 1008dfedab..b540fd1d63 100644 --- a/macros/src/trans.rs +++ b/macros/src/trans.rs @@ -249,18 +249,30 @@ fn init(app: &App, main: &mut Vec, root: &mut Vec) { let mut rexprs = vec![]; for (name, resource) in init_resources { - let _name = Ident::new(format!("_{}", name.as_ref())); - lifetime = Some(quote!('a)); - let ty = &resource.ty; - fields.push(quote! { - pub #name: &'a mut #ty, - }); + if app.init.resources.contains(name) { + fields.push(quote! { + pub #name: &'static mut #ty, + }); - rexprs.push(quote! { - #name: &mut ::#_name, - }); + let expr = &resource.expr; + rexprs.push(quote!(#name: { + static mut #name: #ty = #expr; + &mut #name + })); + } else { + let _name = Ident::new(format!("_{}", name.as_ref())); + lifetime = Some(quote!('a)); + + fields.push(quote! { + pub #name: &'a mut #ty, + }); + + rexprs.push(quote! { + #name: &mut ::#_name, + }); + } } root.push(quote! { diff --git a/tests/cfail/init-resource-share-idle.rs b/tests/cfail/init-resource-share-idle.rs new file mode 100644 index 0000000000..d8332469d8 --- /dev/null +++ b/tests/cfail/init-resource-share-idle.rs @@ -0,0 +1,31 @@ +#![deny(warnings)] +#![feature(proc_macro)] +#![no_std] + +extern crate cortex_m_rtfm as rtfm; +extern crate stm32f103xx; + +use rtfm::app; + +app! { //~ proc macro panicked + device: stm32f103xx, + + resources: { + static BUFFER: [u8; 16] = [0; 16]; + }, + + init: { + resources: [BUFFER], + }, + + idle: { + // ERROR resources assigned to `init` can't be shared with `idle` + resources: [BUFFER], + }, +} + +fn init(_p: init::Peripherals, _r: init::Resources) {} + +fn idle(_r: init::Resources) -> ! { + loop {} +} diff --git a/tests/cfail/init-resource-share-task.rs b/tests/cfail/init-resource-share-task.rs new file mode 100644 index 0000000000..8fe688993e --- /dev/null +++ b/tests/cfail/init-resource-share-task.rs @@ -0,0 +1,36 @@ +#![deny(warnings)] +#![feature(proc_macro)] +#![no_std] + +extern crate cortex_m_rtfm as rtfm; +extern crate stm32f103xx; + +use rtfm::app; + +app! { //~ proc macro panicked + device: stm32f103xx, + + resources: { + static BUFFER: [u8; 16] = [0; 16]; + }, + + init: { + resources: [BUFFER], + }, + + tasks: { + SYS_TICK: { + path: sys_tick, + // ERROR resources assigned to `init` can't be shared with tasks + resources: [BUFFER], + }, + }, +} + +fn init(_p: init::Peripherals) {} + +fn idle() -> ! { + loop {} +} + +fn sys_tick() {}