Skip to content

Commit

Permalink
tests::verify: Assert initial_sp to be in static RAM
Browse files Browse the repository at this point in the history
  • Loading branch information
Urhengulas committed Jun 15, 2021
1 parent 36f282f commit 52f513f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion tests/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ fn should_verify_memory_layout() -> anyhow::Result<()> {
let (bss, data, uninit, vector_table) = elf::get_sections(&object)?;
// compute the initial stack-pointer from `.vector_table`
let initial_sp = elf::compute_initial_sp(&vector_table)?;
// get the bounds of 'static RAM'
let bounds = elf::get_bounds(&[data, bss, uninit])?;

// Is the initial stack-pointer inside 'static RAM'?
assert!(bounds.contains(&initial_sp));
}

// ---
Expand Down Expand Up @@ -76,7 +81,7 @@ mod cargo {
}

mod elf {
use std::convert::TryInto;
use std::{convert::TryInto, ops::RangeInclusive};

use anyhow::anyhow;
use object::{File, Object, ObjectSection, Section};
Expand All @@ -93,6 +98,21 @@ mod elf {
Ok(sp as u64)
}

/// Get [`RangeInclusive`] from lowest to highest address of all sections
pub fn get_bounds(sections: &[Section]) -> anyhow::Result<RangeInclusive<u64>> {
// get beginning and end of all sections
let addresses = sections
.iter()
.flat_map(|sec| vec![sec.address(), sec.address() + sec.size()])
.collect::<Vec<_>>();

// get highest and lowest address of all sections
let min = *addresses.iter().min().ok_or(anyhow!("empty iterator"))?;
let max = *addresses.iter().max().ok_or(anyhow!("empty iterator"))?;

Ok(min..=max)
}

/// Get the following sections from the elf-file:
/// * `.bss`
/// * `.data`
Expand Down

0 comments on commit 52f513f

Please sign in to comment.