Skip to content

Commit

Permalink
Merge pull request #155 from LedgerHQ/feat/heap
Browse files Browse the repository at this point in the history
Heap and global allocator
  • Loading branch information
yogh333 authored Jun 10, 2024
2 parents 4d094bb + eb2ca5e commit 696d503
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ runner = "speculos -m nanox --display=headless"
runner = "speculos -m nanosp --display=headless"

[target.stax]
runner = "speculos --model stax"
runner = "speculos --model stax --display=headless"

[target.flex]
runner = "speculos --model flex"
runner = "speculos --model flex --display=headless"

[unstable]
build-std = ["core"]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_with_current_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest
strategy:
matrix:
device: ["nanos", "nanox", "nanosplus"]
device: ["nanos", "nanox", "nanosplus", "stax", "flex"]
steps:
- name: update nightly toolchain
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools:latest
strategy:
matrix:
target: ["nanos", "nanox", "nanosplus"]
target: ["nanos", "nanox", "nanosplus", "stax", "flex"]
package: [include_gif, testmacro, ledger_secure_sdk_sys, ledger_device_sdk]
steps:
- name: Print Environment variables
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools:latest
strategy:
matrix:
target: ["nanos", "nanox", "nanosplus"]
target: ["nanos", "nanox", "nanosplus", "stax", "flex"]
steps:
- name: Clone
uses: actions/checkout@v4
Expand All @@ -91,7 +91,7 @@ jobs:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools:latest
strategy:
matrix:
target: ["nanos", "nanox", "nanosplus"]
target: ["nanos", "nanox", "nanosplus", "stax", "flex"]
steps:
- name: Force speculos update
run: |
Expand Down
36 changes: 30 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ledger_device_sdk"
version = "1.9.3"
authors = ["yhql", "yogh333", "agrojean-ledger"]
version = "1.10.0"
authors = ["yhql", "yogh333", "agrojean-ledger", "kingofpayne"]
edition = "2021"
license.workspace = true
repository.workspace = true
Expand All @@ -25,7 +25,5 @@ const-zero = "0.1.1"

[features]
speculos = []
pre1_54 = []
lib_bagl = []
nbgl = []
ccid = []
heap = ["ledger_secure_sdk_sys/heap"]
3 changes: 3 additions & 0 deletions ledger_device_sdk/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(target_os = "nanox")]
use crate::ble;
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_secure_sdk_sys::buttons::{get_button_event, ButtonEvent, ButtonsState};
use ledger_secure_sdk_sys::seph as sys_seph;
use ledger_secure_sdk_sys::*;
Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct Comm {
pub rx: usize,
pub tx: usize,
pub event_pending: bool,
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
buttons: ButtonsState,
/// Expected value for the APDU CLA byte.
/// If defined, [`Comm`] will automatically reply with [`StatusWords::BadCla`] when an APDU
Expand Down Expand Up @@ -139,6 +141,7 @@ impl Comm {
rx: 0,
tx: 0,
event_pending: false,
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
buttons: ButtonsState::new(),
expected_cla: None,
}
Expand Down
5 changes: 0 additions & 5 deletions ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,3 @@ extern "C" fn io_seproxyhal_play_tune(tune_index: u8) {
return;
}
}

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
exit_app(1);
}
76 changes: 0 additions & 76 deletions ledger_device_sdk/tests/ecc_tests.rs

This file was deleted.

9 changes: 8 additions & 1 deletion ledger_secure_sdk_sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledger_secure_sdk_sys"
version = "1.3.1"
version = "1.4.0"
authors = ["yhql", "agrojean-ledger"]
edition = "2021"
license.workspace = true
Expand All @@ -11,3 +11,10 @@ description = "Bindings to Ledger C SDK"
bindgen = "0.65.1"
cc = "1.0.73"
glob = "0.3.1"

[dependencies]
embedded-alloc = { version = "0.5.1", optional = true }
critical-section = { version = "1.1.2", optional = true }

[features]
heap = ["dep:embedded-alloc", "dep:critical-section"]
2 changes: 2 additions & 0 deletions ledger_secure_sdk_sys/src/c/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#endif

extern void sample_main();
extern void heap_init();

struct SectionSrc;
struct SectionDst;
Expand Down Expand Up @@ -329,6 +330,7 @@ int c_main(void) {
check_audited_app();
#endif // !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)

heap_init();
sample_main();
}
CATCH(EXCEPTION_IO_RESET) {
Expand Down
39 changes: 39 additions & 0 deletions ledger_secure_sdk_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#![allow(non_snake_case)]

use core::ffi::c_void;
#[cfg(feature = "heap")]
use core::mem::MaybeUninit;

pub mod buttons;
mod infos;
Expand Down Expand Up @@ -33,4 +35,41 @@ pub fn pic_rs_mut<T>(x: &mut T) -> &mut T {
unsafe { &mut *ptr }
}

#[cfg(feature = "heap")]
use critical_section::RawRestoreState;
#[cfg(feature = "heap")]
use embedded_alloc::Heap;

#[cfg(feature = "heap")]
#[global_allocator]
static HEAP: Heap = Heap::empty();

#[cfg(feature = "heap")]
struct CriticalSection;
#[cfg(feature = "heap")]
critical_section::set_impl!(CriticalSection);

/// Default empty implementation as we don't have concurrency.
#[cfg(feature = "heap")]
unsafe impl critical_section::Impl for CriticalSection {
unsafe fn acquire() -> RawRestoreState {}
unsafe fn release(_restore_state: RawRestoreState) {}
}

/// Initializes the heap memory for the global allocator.
///
/// The heap is stored in the stack, and has a fixed size.
/// This method is called just before [sample_main].
#[no_mangle]
#[cfg(feature = "heap")]
extern "C" fn heap_init() {
const HEAP_SIZE: usize = 8192;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}

#[no_mangle]
#[cfg(not(feature = "heap"))]
extern "C" fn heap_init() {}

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 comments on commit 696d503

Please sign in to comment.