Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

static mut transform allows aliasing &mut references #212

Closed
@jonas-schievink

Description

@jonas-schievink

PoC:

#![feature(asm)]
#![no_std]
#![no_main]

extern crate cortex_m_rt as rt;
extern crate panic_halt;
extern crate cortex_m;
extern crate cortex_m_semihosting as semi;

extern crate stm32l0xx_hal;

use rt::{entry, exception};
use cortex_m::interrupt::{self, Mutex};
use core::cell::RefCell;
use semi::hprintln;

static STORAGE: Mutex<RefCell<Option<&'static mut u8>>> = Mutex::new(RefCell::new(None));

#[exception]
fn SVCall() {
    static mut STORED: bool = false;
    static mut STAT: u8 = 0;

    let stat: &'static mut u8 = STAT;

    if *STORED {
        let alias = interrupt::free(move |cs| {
            STORAGE.borrow(cs).borrow_mut().take().unwrap()
        });

        hprintln!("stat ={:p}", stat).unwrap();
        hprintln!("alias={:p}", alias).unwrap();
        loop {
            unsafe { asm!(""); }
        }
    } else {
        interrupt::free(move |cs| {
            *STORAGE.borrow(cs).borrow_mut() = Some(stat);
        });
        *STORED = true;
    }
}

#[entry]
fn you_died_of_dis_entry() -> ! {
    hprintln!("hello").unwrap();
    loop {
        unsafe { asm!("svc 0"); }
    }
}

Output:

hello
stat =0x20000009
alias=0x20000009

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions