Commit 353610de authored by David Brown's avatar David Brown Committed by David Brown
Browse files

sim: Prevent reentrancy on simulation

Lock the simulation with a mutex to prevent concurrent access.  The C
code being tested uses globals, and can only be run in one context at a
time.  The Rust test framework may run tests concurrently, so use the
mutex to prevent this.

Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent 541860c2
Loading
Loading
Loading
Loading
+1
Changes for sim/Cargo.lock: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -157,6 +157,7 @@ name = "mcuboot-sys"
version = "0.1.0"
dependencies = [
 "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)",
 "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "libc 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "simflash 0.1.0",
+1
Changes for sim/mcuboot-sys/Cargo.toml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ overwrite-only = []
gcc = "0.3.51"

[dependencies]
lazy_static = "0.2"
libc = "0.2.0"
log = "0.3"
simflash = { path = "../simflash" }
+9
Changes for sim/mcuboot-sys/src/c.rs: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,9 +4,18 @@ use area::AreaDesc;
use simflash::Flash;
use libc;
use api;
use std::sync::Mutex;

lazy_static! {
    /// Mutex to lock the simulation.  The C code for the bootloader uses
    /// global variables, and is therefore non-reentrant.
    static ref BOOT_LOCK: Mutex<()> = Mutex::new(());
}

/// Invoke the bootloader on this flash device.
pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc, counter: Option<&mut i32>, align: u8) -> i32 {
    let _lock = BOOT_LOCK.lock().unwrap();

    unsafe {
        api::set_flash(flash);
        raw::sim_flash_align = align;
+1
Changes for sim/mcuboot-sys/src/lib.rs: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
#[macro_use] extern crate lazy_static;
extern crate libc;
#[macro_use] extern crate log;
extern crate simflash;