From 1b36c27a51e14cb97e592c533045d81e5f257b71 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Thu, 15 Apr 2021 09:37:10 +0200 Subject: [PATCH 1/2] don't export core --- src/lib.rs | 2 +- src/util.rs | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9e71c0b..b60f20a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ #![cfg_attr(all(test, feature = "unstable"), feature(test))] #[cfg(all(test, feature = "unstable"))] extern crate test; -#[cfg(any(test, feature="std"))] pub extern crate core; +#[cfg(any(test, feature="std"))] extern crate core; #[cfg(feature="serde")] pub extern crate serde; #[cfg(all(test,feature="serde"))] extern crate serde_test; diff --git a/src/util.rs b/src/util.rs index e6d4422..80d109b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -29,8 +29,8 @@ macro_rules! hex_fmt_impl( hex_fmt_impl!($imp, $ty, ); ); ($imp:ident, $ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::fmt::$imp for $ty<$($gen),*> { - fn fmt(&self, f: &mut $crate::core::fmt::Formatter) -> $crate::core::fmt::Result { + impl<$($gen: $gent),*> ::core::fmt::$imp for $ty<$($gen),*> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use $crate::hex::{format_hex, format_hex_reverse}; if $ty::<$($gen),*>::DISPLAY_BACKWARD { format_hex_reverse(&self.0, f) @@ -49,37 +49,37 @@ macro_rules! index_impl( index_impl!($ty, ); ); ($ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::ops::Index for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index for $ty<$($gen),*> { type Output = u8; fn index(&self, index: usize) -> &u8 { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::Range> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::Range> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::Range) -> &[u8] { + fn index(&self, index: ::core::ops::Range) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFrom> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFrom> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeFrom) -> &[u8] { + fn index(&self, index: ::core::ops::RangeFrom) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeTo> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeTo> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeTo) -> &[u8] { + fn index(&self, index: ::core::ops::RangeTo) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFull> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFull> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeFull) -> &[u8] { + fn index(&self, index: ::core::ops::RangeFull) -> &[u8] { &self.0[index] } } @@ -93,19 +93,19 @@ macro_rules! borrow_slice_impl( borrow_slice_impl!($ty, ); ); ($ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::borrow::Borrow<[u8]> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::borrow::Borrow<[u8]> for $ty<$($gen),*> { fn borrow(&self) -> &[u8] { &self[..] } } - impl<$($gen: $gent),*> $crate::core::convert::AsRef<[u8]> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::convert::AsRef<[u8]> for $ty<$($gen),*> { fn as_ref(&self) -> &[u8] { &self[..] } } - impl<$($gen: $gent),*> $crate::core::ops::Deref for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Deref for $ty<$($gen),*> { type Target = [u8]; fn deref(&self) -> &Self::Target { From 9d08cbdbb1a9d9c82aecb65da068bbcf22f1ea74 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Thu, 15 Apr 2021 09:36:14 +0200 Subject: [PATCH 2/2] add no_std embedded test in CI --- .github/workflows/rust.yml | 22 ++++++++++++++++ .gitignore | 3 +++ embedded/Cargo.toml | 24 +++++++++++++++++ embedded/memory.x | 5 ++++ embedded/src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 embedded/Cargo.toml create mode 100644 embedded/memory.x create mode 100644 embedded/src/main.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 578a499..82d30b0 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -80,3 +80,25 @@ jobs: DO_FEATURE_MATRIX: true run: ./contrib/test.sh + Embedded: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Set up QEMU + run: sudo apt update && sudo apt install qemu-system-arm + - name: Checkout Toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly + override: true + components: rust-src + target: thumbv7m-none-eabi + - name: Run + env: + RUSTFLAGS: "-C link-arg=-Tlink.x" + CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel" + run: cd embedded && cargo run --target thumbv7m-none-eabi + + diff --git a/.gitignore b/.gitignore index 03eb4d0..84d3dcb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ Cargo.lock fuzz/hfuzz_target fuzz/hfuzz_workspace +#embedded +embedded/.cargo + *~ diff --git a/embedded/Cargo.toml b/embedded/Cargo.toml new file mode 100644 index 0000000..f753bf8 --- /dev/null +++ b/embedded/Cargo.toml @@ -0,0 +1,24 @@ +[package] +authors = ["Riccardo Casatta "] +edition = "2018" +readme = "README.md" +name = "embedded" +version = "0.1.0" + +[dependencies] +cortex-m = "0.6.0" +cortex-m-rt = "0.6.10" +cortex-m-semihosting = "0.3.3" +panic-halt = "0.2.0" +alloc-cortex-m = "0.4.1" +bitcoin_hashes = { path="../", default-features = false } + +[[bin]] +name = "embedded" +test = false +bench = false + +[profile.release] +codegen-units = 1 # better optimizations +debug = true # symbols are nice and they don't increase the size on Flash +lto = true # better optimizations diff --git a/embedded/memory.x b/embedded/memory.x new file mode 100644 index 0000000..95de161 --- /dev/null +++ b/embedded/memory.x @@ -0,0 +1,5 @@ +MEMORY +{ + FLASH : ORIGIN = 0x00000000, LENGTH = 256K + RAM : ORIGIN = 0x20000000, LENGTH = 64K +} diff --git a/embedded/src/main.rs b/embedded/src/main.rs new file mode 100644 index 0000000..6b5940a --- /dev/null +++ b/embedded/src/main.rs @@ -0,0 +1,54 @@ +#![feature(alloc_error_handler)] +#![no_std] +#![no_main] + +#[macro_use] +extern crate bitcoin_hashes; + +extern crate alloc; + +use alloc_cortex_m::CortexMHeap; +use bitcoin_hashes::{sha256, Hash, HashEngine}; +use core::alloc::Layout; +use core::str::FromStr; +use cortex_m::asm; +use cortex_m_rt::entry; +use cortex_m_semihosting::{debug, hprintln}; +use panic_halt as _; + +hash_newtype!(TestType, sha256::Hash, 32, doc = "test"); + +// this is the allocator the application will use +#[global_allocator] +static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); + +const HEAP_SIZE: usize = 1024; // in bytes + +#[entry] +fn main() -> ! { + unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) } + + let mut engine = TestType::engine(); + engine.input(b"abc"); + let hash = TestType::from_engine(engine); + + let hash_check = + TestType::from_str("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") + .unwrap(); + hprintln!("hash:{} hash_check:{}", hash, hash_check).unwrap(); + if hash == hash_check { + debug::exit(debug::EXIT_SUCCESS); + } else { + debug::exit(debug::EXIT_FAILURE); + } + + loop {} +} + +// define what happens in an Out Of Memory (OOM) condition +#[alloc_error_handler] +fn alloc_error(_layout: Layout) -> ! { + asm::bkpt(); + + loop {} +}