-
Notifications
You must be signed in to change notification settings - Fork 60
[BEAM] Unsynchronized access to a shared static mut
variable
#113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Unsynchronized access to a shared `static mut` variable | ||
|
||
Sometimes interrupt handlers need to share state. This is done using a static | ||
variable. We claim that *no* synchronization is needed to access the shared | ||
static variable if the interrupt handlers run at the same priority. | ||
|
||
Note that: | ||
|
||
- The programs in this document target the [Basic Embedded Abstract Machine | ||
(BEAM)][beam]. Please become familiar with the linked specification before you | ||
read the rest of this document. | ||
|
||
[beam]: https://github.com/rust-lang/unsafe-code-guidelines/pull/111 | ||
|
||
- In these programs we assume that [rust-lang/rfcs#2585][rfc2585] has been | ||
accepted and implemented. | ||
|
||
[rfc2585]: https://github.com/rust-lang/rfcs/pull/2585 | ||
|
||
## Example program | ||
|
||
Consider this program | ||
|
||
``` rust | ||
#![no_std] | ||
|
||
#[no_mangle] | ||
unsafe fn main() -> ! { | ||
unsafe { | ||
asm!("ENABLE_INTERRUPTS" : : : : "volatile"); | ||
} | ||
|
||
loop { | ||
// .. any safe code .. | ||
} | ||
} | ||
|
||
static mut X: u128 = 0; | ||
|
||
#[no_mangle] | ||
unsafe fn INTERRUPT0() { | ||
let x: &mut u128 = unsafe { &mut X }; | ||
|
||
// .. any safe code .. | ||
} | ||
|
||
#[no_mangle] | ||
unsafe fn INTERRUPT1() { | ||
let x: &mut u128 = unsafe { &mut X }; | ||
|
||
// .. any safe code .. | ||
} | ||
``` | ||
|
||
Note that "any safe code" can *not* call `main`, `INTERRUPT0` or `INTERRUPT1` | ||
(because they are `unsafe` functions), use `asm!` or access registers. | ||
|
||
**Claim**: this program is well-defined / sound. Rust aliasing rules are | ||
preserved because no preemption is possible between `INTERRUPT0` and | ||
`INTERRUPT1`. | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In fact, I would say that this program (ignoring `main`) is equivalent to this | ||
code, which is sound (and reminds me of cooperative scheduling): | ||
|
||
``` rust | ||
let mut X: u128 = 0; | ||
|
||
loop { | ||
sleep(random()); | ||
|
||
if random() { | ||
INTERRUPT0(&mut X); | ||
} else if random() { | ||
INTERRUPT1(&mut X); | ||
} | ||
} | ||
``` | ||
|
||
## Questions | ||
|
||
- Can this program be misoptimized given that the compiler has *no* information | ||
about `INTERRUPT0` and `INTERRUPT1` executing "cooperatively"? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The crucial aliasing point here is: the moment So, safety relies on the assumption that the safe code cannot somehow leak the #[no_mangle]
unsafe fn INTERRUPT1() {
let x: &mut u128 = unsafe { &mut X };
inner(x);
// Crucially, inner is generic in the lifetime and hence
// cannot leak the reference.
fn inner(x: &mut u128) {
// .. any safe code ..
}
} Besides this point, I agree that the program is fine as far as Stacked Borrows is concerned. I can only hope LLVM agrees with that. ;) The compiler cannot know that the functions cooperate, but when it doubt it has to assume that they do. |
Uh oh!
There was an error while loading. Please reload this page.