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

no_std support, keeping MSRV #603

Merged
merged 2 commits into from Jul 20, 2021

Conversation

devrandom
Copy link
Contributor

@devrandom devrandom commented May 5, 2021

TODO:

  • needs release of bitcoin_hashes
  • needs release of rust-bech32

Based on the original work by Justin Moon in #528

MSRV unchanged from 1.29.0.

When std is off the no-std feature must be turned on, which turns on the alloc crate, which requires the user define a global allocator. It also pulls in the hashbrown crate and activates no_std and alloc support in a couple of dependencies.

  • Import from core and alloc instead of std
  • alloc only used if std is off
  • Create std feature
  • Create no-std feature which adds a core2 dependency to polyfill std::io features. This is an experimental feature and should be used with caution.
  • CI runs tests no_std code
  • MSRV of no_std is 1.51 or so

Fixes #409

@devrandom devrandom changed the title WIP: no_std support, keeping MSRV Draft: no_std support, keeping MSRV May 5, 2021
@devrandom devrandom marked this pull request as draft May 5, 2021 13:09
Cargo.toml Outdated

base64-compat = { version = "1.0.0", optional = true }
bitcoinconsensus = { version = "0.19.0-1", optional = true }
serde = { version = "1", features = [ "derive" ], optional = true }
hashbrown = "0.11.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we feature-guard hashbrown somehow? We can probably just not export chunks of the library that rely on a HashMap, most of the library is still useful without it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a couple of commits to do this, let me know what you think of the approach

src/io.rs Outdated Show resolved Hide resolved
@devrandom devrandom force-pushed the no_std_devrandom branch 2 times, most recently from 55e2a03 to 9f79ab9 Compare May 5, 2021 16:09
@devrandom
Copy link
Contributor Author

Need CI use approval.

@elichai
Copy link
Member

elichai commented May 5, 2021

Need CI use approval.

I Approved the CI to run

@TheBlueMatt
Copy link
Member

I Approved the CI to run

We have to do it every time any new push happens. I have zero clue what this is even intended to solve, but its super obnoxious. Obnoxious enough that I'd start thinking about something other than GH actions at this point for CI.

@devrandom
Copy link
Contributor Author

CI should be fixed with latest push. Sorry for the hassle.

@devrandom
Copy link
Contributor Author

And one more...

@devrandom
Copy link
Contributor Author

OK, looks like CI passes, but there is some strangeness in the display with three additional line items showing as "expected".

@devrandom
Copy link
Contributor Author

devrandom commented May 7, 2021

What do we think about bumping the MSRV to edition 2018 / 1.31.0?

This would affect rust-lightning too, since it is at 1.30.0.

@sgeisler
Copy link
Contributor

sgeisler commented May 7, 2021

What do we think about bumping the MSRV to edition 2018 / 1.31.0?

I think it's planned in the next few months (#510 (comment)).

src/io.rs Outdated Show resolved Hide resolved
Copy link
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good. I think we should wait for/work on rust-bitcoin/bitcoin_hashes#126 or rust-bitcoin/bitcoin_hashes#127 first and then use that here.

@devrandom
Copy link
Contributor Author

In the meantime, I added an embedded test, similar to the one in bitcoin_hashes. 512KB minimum is required because of the amount of stack that secp256k1 requires to build a context, even in lowmemory mode.

@devrandom
Copy link
Contributor Author

devrandom commented May 25, 2021

switch from take() to CappedRead as discussed in rust-bitcoin/bitcoin_hashes#127 (comment)

@devrandom devrandom force-pushed the no_std_devrandom branch 3 times, most recently from 4e267b6 to c715916 Compare May 26, 2021 16:19
@devrandom
Copy link
Contributor Author

So HashEngine from bitcoin_hashes doesn't implement core2::io::Write yet. I assume the plan is to implement that? Should I go ahead and open a PR?

Copy link
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High level looks good. Can you squash up the commits and fix the FIXMEs? Lets land this!

fuzz/fuzz_targets/deserialize_address.rs Outdated Show resolved Hide resolved
@TheBlueMatt
Copy link
Member

So HashEngine from bitcoin_hashes doesn't implement core2::io::Write yet. I assume the plan is to implement that? Should I go ahead and open a PR?

Yea, probably go ahead and open a PR there or chat with @RCasatta about doing it. Maybe we can simplify this PR to export fewer things for now and land it quicker, then we can expand the things exposed in no_std over time?

@devrandom
Copy link
Contributor Author

Opened rust-bitcoin/bitcoin_hashes#128 and adjusted this one to point to that

@sgeisler
Copy link
Contributor

Ah, sorry for another remark, I was just going through my notifications and saw that there is a non-alpha release of core2 now (bbqsrc/core2#4) which we should use here imo.

@devrandom
Copy link
Contributor Author

Ah, sorry for another remark, I was just going through my notifications and saw that there is a non-alpha release of core2 now (bbqsrc/core2#4) which we should use here imo.

Ah, great. But we'll have to upgrade and release bitcoin_hashes first, otherwise there's a semver version conflict.

@TheBlueMatt
Copy link
Member

An updated version of bitcoin_hashes has been published. Can we use the latest core2 now?

@devrandom
Copy link
Contributor Author

updated deps, rebased and squashed

Copy link
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question, two tiny comments, and I'm otherwise happy :)

contrib/test.sh Outdated
alias cargo="cargo +$TOOLCHAIN"
fi
echo "********* Testing std *************"
# Test without any features first. std is required for tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're just gonna require std for tests, I'm a bit confused why a bunch of #[cfg(test)] blocks have std/no-std feature checks. Is it just that we intend to support no_std tests but don't yet and its WIP to be done in a followup?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment was wrong, and we do indeed test with no-std below. Fixed.

src/util/merkleblock.rs Outdated Show resolved Hide resolved
src/util/merkleblock.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
TheBlueMatt
TheBlueMatt previously approved these changes Jul 14, 2021
Copy link
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@TheBlueMatt
Copy link
Member

Note that this now requires a major version bump as its "breaking" to update the bitcoin_hashes dependency. I think that's fine and don't really care about the merkle block deprecated functions, just wanted to flag it.

src/util/mod.rs Outdated Show resolved Hide resolved
Based on the original work by Justin Moon.

*MSRV unchanged from 1.29.0.*

When `std` is off, `no-std` must be on, and we use the [`alloc`](https://doc.rust-lang.org/alloc/) and core2 crates. The `alloc` crate requires the user define a global allocator.

* Import from `core` and `alloc` instead of `std`
* `alloc` only used if `no-std` is on
* Create `std` feature
* Create `no-std` feature which adds a core2 dependency to polyfill `std::io` features. This is an experimental feature and should be
used with caution.
* CI runs tests `no-std`
* MSRV for `no-std` is 1.51 or so
@devrandom
Copy link
Contributor Author

Note that this now requires a major version bump as its "breaking" to update the bitcoin_hashes dependency. I think that's fine and don't really care about the merkle block deprecated functions, just wanted to flag it.

@TheBlueMatt to clarify, are you saying that we could remove the deprecated funcs but you don't feel strongly about it? (I don't feel strongly either)

Copy link
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheBlueMatt to clarify, are you saying that we could remove the deprecated funcs but you don't feel strongly about it? (I don't feel strongly either)

Yes.

Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK bba57d7

Copy link
Collaborator

@RCasatta RCasatta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a couple of tests limiting memory with the embedded test:

  • with 64KB of memory the process hangs forever with the CPU looping
  • with 128KB it panics with panic Some([libsecp256k1] illegal argument. rustsecp256k1_v0_4_0_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx))
  • with 256KB works correctly (secp buf size 66240 bytes)

embedded/scripts/env.sh Show resolved Hide resolved
@@ -0,0 +1,2 @@
export RUSTFLAGS="-C link-arg=-Tlink.x"
export CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER="qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -m 1G -nographic -semihosting-config enable=on,target=native -kernel"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-m 1G on my host complain saying maximum memory for this machine is 16Mib, removing the parameter seems to work fine

qemu-system-arm: Invalid RAM size, should be 16 MiB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious what is your qemu and OS version? does it work with -m 16m?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of changing this to -m 1m, but it would be good to know the answer to the above to make sure it works in your env too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, sorry.

On macOS Catalina with QEMU emulator version 5.2.0 it works only with -m 16m or without the option.

It doesn't work with -m 1m or -m 1G

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Ubuntu 20.04 has qemu 4.2 and it also works without the flag. So I think I'll remove the flag if there are any other changes, or wait to a followup PR.

#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();

const HEAP_SIZE: usize = 1024 * 512; // 512 KB
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all memory available is 512Kb according to memory.x so the heap should be less, eg 256KB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix

Copy link
Collaborator

@RCasatta RCasatta Jul 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not worth dismissing approvals only for this

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack bba57d7

@apoelstra
Copy link
Member

Gonna merge this. We can address @RCasatta comment in a folllowup PR (and maybe make other changes to work with tiny amounts of RAM)

@apoelstra apoelstra merged commit 5e6b56a into rust-bitcoin:master Jul 20, 2021
@devrandom devrandom mentioned this pull request Aug 2, 2021
@dr-orlovsky dr-orlovsky mentioned this pull request Aug 9, 2021
@mcroad mcroad mentioned this pull request Oct 13, 2021
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

no_std support
10 participants