Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates HEAP_SIZE to be configurable for embedded alloc #183

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledger_device_sdk"
version = "1.15.1"
version = "1.15.2"
authors = ["yhql", "yogh333", "agrojean-ledger", "kingofpayne"]
edition = "2021"
license.workspace = true
Expand All @@ -21,7 +21,7 @@ rand_core = { version = "0.6.3", default_features = false }
zeroize = { version = "1.6.0", default_features = false }
numtoa = "0.2.4"
const-zero = "0.1.1"
ledger_secure_sdk_sys = { path = "../ledger_secure_sdk_sys", version = "1.4.5" }
ledger_secure_sdk_sys = { path = "../ledger_secure_sdk_sys", version = "1.4.6" }

[features]
speculos = []
Expand Down
2 changes: 1 addition & 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.4.5"
version = "1.4.6"
authors = ["yhql", "agrojean-ledger"]
edition = "2021"
license.workspace = true
Expand Down
16 changes: 16 additions & 0 deletions ledger_secure_sdk_sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate cc;
use glob::glob;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs::File, io::BufRead, io::BufReader, io::Read};
Expand Down Expand Up @@ -581,6 +582,20 @@ impl SDKBuilder {
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings");
}

fn generate_heap_size(&self) {
// Read the HEAP_SIZE environment variable, default to 8192 if not set
let heap_size = env::var("HEAP_SIZE").unwrap_or_else(|_| "8192".to_string());

// Generate the heap_size.rs file with the HEAP_SIZE value
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("heap_size.rs");
fs::write(
&dest_path,
format!("pub const HEAP_SIZE: usize = {};", heap_size),
)
.expect("Unable to write file");
}
}

fn main() {
Expand All @@ -591,6 +606,7 @@ fn main() {
sdk_builder.cxdefines();
sdk_builder.build_c_sdk();
sdk_builder.generate_bindings();
sdk_builder.generate_heap_size();
}

fn finalize_nanos_configuration(command: &mut cc::Build, bolos_sdk: &Path) {
Expand Down
3 changes: 2 additions & 1 deletion ledger_secure_sdk_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ unsafe impl critical_section::Impl for CriticalSection {
#[no_mangle]
#[cfg(all(feature = "heap", not(target_os = "nanos")))]
extern "C" fn heap_init() {
const HEAP_SIZE: usize = 8192;
// HEAP_SIZE comes from heap_size.rs, which is defined via env var and build.rs
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}
Expand All @@ -73,3 +73,4 @@ extern "C" fn heap_init() {
extern "C" fn heap_init() {}

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