Description
When I try to use alloc-cortex-m
in the following fashion:
#![no_main]
#![no_std]
#![feature(alloc_error_handler)]
extern crate alloc;
use alloc_cortex_m::CortexMHeap;
use core::alloc::Layout;
use defmt::*;
#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
#[cortex_m_rt::entry]
fn main() -> ! {
defmt::timestamp!("{=usize}", now());
info!("initializing heap");
let start = cortex_m_rt::heap_start() as usize;
let size = 4;
debug!("start = {:#010x}, size = {:#010x}", start, size);
unsafe { ALLOCATOR.init(start, size) }
// other code
}
Then the program panics immediately upon trying to initialize the heap, and the heap start address appears to be wrong (I am using the STM32F411, so SRAM begins at 0x2000 0000
and ends at 0x2002 0000
).
Finished dev [unoptimized + debuginfo] target(s) in 3.78s
Running `probe-run --chip STM32F411VETx target/thumbv7em-none-eabihf/debug/tasks`
(HOST) INFO flashing program (49 pages / 49.00 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
0 INFO initializing heap
└─ tasks::__cortex_m_rt_main::_ @ src/bin/tasks.rs:28
0 DEBUG start = 0x2001fffc, size = 0x00000004
└─ tasks::__cortex_m_rt_main::_ @ src/bin/tasks.rs:33
────────────────────────────────────────────────────────────────────────────────
stack backtrace:
0: HardFaultTrampoline
<exception entry>
1: linked_list_allocator::hole::HoleList::new
at /home/ibiyemi/.cargo/registry/src/github.com-1ecc6299db9ec823/linked_list_allocator-0.8.11/src/hole.rs:50:9
2: linked_list_allocator::Heap::init
at /home/ibiyemi/.cargo/registry/src/github.com-1ecc6299db9ec823/linked_list_allocator-0.8.11/src/lib.rs:73:22
3: alloc_cortex_m::CortexMHeap::init::{{closure}}
at /home/ibiyemi/.cargo/registry/src/github.com-1ecc6299db9ec823/alloc-cortex-m-0.4.1/src/lib.rs:58:13
4: cortex_m::interrupt::free
at /home/ibiyemi/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.3/src/interrupt.rs:64:13
5: alloc_cortex_m::CortexMHeap::init
at /home/ibiyemi/.cargo/registry/src/github.com-1ecc6299db9ec823/alloc-cortex-m-0.4.1/src/lib.rs:57:9
6: tasks::__cortex_m_rt_main
at src/bin/tasks.rs:35:14
7: main
at src/bin/tasks.rs:24:1
8: Reset
(HOST) WARN call stack was corrupted; unwinding could not be completed
(HOST) ERROR the program panicked
If I disable flip-link
by commenting out "-C", "linker=flip-link"
in .cargo/config.toml
, then this panic does not happen, and a more reasonable start address is printed:
Finished dev [unoptimized + debuginfo] target(s) in 52.83s
Running `probe-run --chip STM32F411VETx target/thumbv7em-none-eabihf/debug/tasks`
(HOST) INFO flashing program (49 pages / 49.00 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
0 INFO initializing heap
└─ tasks::__cortex_m_rt_main::_ @ src/bin/tasks.rs:28
0 DEBUG start = 0x20000454, size = 0x00010000
└─ tasks::__cortex_m_rt_main::_ @ src/bin/tasks.rs:33
It has occurred to me that the point of flip-link
is to allow stack overflow errors to be caught by relying on the stack crashing into the RAM boundary. Does this mean that flip-link
is incompatible with the use of a heap, or does the heap need to be placed above the stack in memory? If it's the latter, how would I achieve that?
Activity