Skip to content

Commit

Permalink
Add double modem init protection
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Jun 18, 2024
1 parent 82837d7 commit 4fba64c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.4.3 (2024-06-18)

- Added extra check that prevents initializing the modem multiple times
- Updated embassy-sync
- Stopped using `cortex_m::interrupt::free` in favour of `critical_section`

## 0.4.2 (2024-06-17)

- Fixed a memory ownership issue in `nrf_modem_init`. The `nrf_modem_init_params` pointer given to the init must
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nrf-modem"
version = "0.4.2"
version = "0.4.3"
edition = "2021"
rust-version = "1.64"
license = "MIT OR Apache-2.0"
Expand All @@ -24,7 +24,7 @@ arrayvec = { version = "0.7", default-features = false }
at-commands = "0.5.2"
no-std-net = "0.6.0"
critical-section = "1.1"
embassy-sync = "0.3.0"
embassy-sync = "0.6.0"
grounded = "0.2.0"

[features]
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum Error {
InternalRuntimeMutexLocked,
/// The given memory layout falls outside of the acceptable range
BadMemoryLayout,
ModemAlreadyInitialized,
}

pub trait ErrorSource {
Expand Down
4 changes: 2 additions & 2 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub extern "C" fn nrfx_ipc_receive_event_disable(event_index: u8) {
unsafe fn generic_alloc(num_bytes_requested: usize, heap: &crate::WrappedHeap) -> *mut u8 {
let sizeof_usize = core::mem::size_of::<usize>();
let mut result = core::ptr::null_mut();
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
let num_bytes_allocated = num_bytes_requested + sizeof_usize;
let layout =
core::alloc::Layout::from_size_align_unchecked(num_bytes_allocated, sizeof_usize);
Expand Down Expand Up @@ -317,7 +317,7 @@ unsafe fn generic_alloc(num_bytes_requested: usize, heap: &crate::WrappedHeap) -
/// This function is safe to call from an ISR.
unsafe fn generic_free(ptr: *mut u8, heap: &crate::WrappedHeap) {
let sizeof_usize = core::mem::size_of::<usize>() as isize;
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Fetch the size from the previous four bytes
let real_ptr = ptr.offset(-sizeof_usize);
let num_bytes_allocated = core::ptr::read_volatile::<usize>(real_ptr as *const usize);
Expand Down
3 changes: 2 additions & 1 deletion src/gnss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ impl GnssData {
)
.into_result()?;

let data = core::mem::transmute(data.assume_init().nmea_str); // Make data be u8
let data =
core::mem::transmute::<[i8; 83], [u8; 83]>(data.assume_init().nmea_str); // Make data be u8
let mut string_data = ArrayString::from_byte_string(&data)?;
string_data.truncate(
string_data
Expand Down
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{
ops::Range,
sync::atomic::{AtomicBool, Ordering},
};
use cortex_m::interrupt::Mutex;
use critical_section::Mutex;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use linked_list_allocator::Heap;

Expand Down Expand Up @@ -67,6 +67,7 @@ static LIBRARY_ALLOCATOR: WrappedHeap = Mutex::new(RefCell::new(None));
static TX_ALLOCATOR: WrappedHeap = Mutex::new(RefCell::new(None));

pub(crate) static MODEM_RUNTIME_STATE: RuntimeState = RuntimeState::new();
static INITIALIZED: AtomicBool = AtomicBool::new(false);

/// Start the NRF Modem library
pub async fn init(mode: SystemMode) -> Result<(), Error> {
Expand All @@ -78,6 +79,10 @@ pub async fn init_with_custom_layout(
mode: SystemMode,
memory_layout: MemoryLayout,
) -> Result<(), Error> {
if INITIALIZED.fetch_or(true, Ordering::SeqCst) {
return Err(Error::ModemAlreadyInitialized);
}

const SHARED_MEMORY_RANGE: Range<u32> = 0x2000_0000..0x2002_0000;

if !SHARED_MEMORY_RANGE.contains(&memory_layout.base_address) {
Expand Down Expand Up @@ -107,7 +112,7 @@ pub async fn init_with_custom_layout(
static mut HEAP_MEMORY: [u32; 1024] = [0u32; 1024];
let heap_start = HEAP_MEMORY.as_ptr() as *mut u8;
let heap_size = HEAP_MEMORY.len() * core::mem::size_of::<u32>();
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
*LIBRARY_ALLOCATOR.borrow(cs).borrow_mut() = Some(Heap::new(heap_start, heap_size))
});
}
Expand Down Expand Up @@ -148,7 +153,7 @@ pub async fn init_with_custom_layout(

unsafe {
// Use the same TX memory region as above
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
*TX_ALLOCATOR.borrow(cs).borrow_mut() = Some(Heap::new(
params.shmem.tx.base as usize as *mut u8,
params.shmem.tx.size as usize,
Expand Down

0 comments on commit 4fba64c

Please sign in to comment.