Skip to content

Commit f807360

Browse files
committed
rust: update toolchain to 1.84.0 and update bindgen/cbindgen
This reduces the binary size by about 3kB. New now are warnings (which we interpret as errors) about shared mut refs: ``` = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives = note: `-D static-mut-refs` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(static_mut_refs)]` error: creating a shared reference to mutable static is discouraged ``` The commit includes `UnsafeSyncRefCell` and uses it in some unit tests to remove the warning. In `src/rust/bitbox02-rust-c/src/workflow.rs` we disable the warning altogether as it's hard to fix it there, so we delay it there for a future PR.
1 parent 160e154 commit f807360

File tree

1,150 files changed

+402741
-190986
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,150 files changed

+402741
-190986
lines changed

.containerversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
43
1+
44

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ RUN rustup target add thumbv7em-none-eabi
150150
RUN rustup component add rustfmt
151151
RUN rustup component add clippy
152152
RUN rustup component add rust-src
153-
RUN CARGO_HOME=/opt/cargo cargo install cbindgen --version 0.26.0 --locked
154-
RUN CARGO_HOME=/opt/cargo cargo install bindgen-cli --version 0.69.4 --locked
153+
RUN CARGO_HOME=/opt/cargo cargo install cbindgen --version 0.27.0 --locked
154+
RUN CARGO_HOME=/opt/cargo cargo install bindgen-cli --version 0.71.1 --locked
155155

156156
# Until cargo vendor supports vendoring dependencies of the rust std libs we
157157
# need a copy of this file next to the toml file. It also has to be world
158158
# writable so that invocations of `cargo vendor` can update it. Below is the
159159
# tracking issue for `cargo vendor` to support rust std libs.
160160
# https://github.com/rust-lang/wg-cargo-std-aware/issues/23
161-
RUN cp "$(rustc --print=sysroot)/lib/rustlib/src/rust/Cargo.lock" "$(rustc --print=sysroot)/lib/rustlib/src/rust/library/test/"
161+
RUN cp "$(rustc --print=sysroot)/lib/rustlib/src/rust/library/Cargo.lock" "$(rustc --print=sysroot)/lib/rustlib/src/rust/library/test/"
162162
RUN chmod 777 $(rustc --print=sysroot)/lib/rustlib/src/rust/library/test/Cargo.lock
163163

164164
COPY tools/prost-build-proto prost-build-proto

src/rust/bitbox02-rust-c/src/workflow.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
//! usb message proessing is not ported to Rust. If that happens, the `async_usb` module can be
1717
//! used and this can be deleted.
1818
19+
// TODO: figure out how to deal with the static muts below.
20+
// https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html
21+
#![allow(static_mut_refs)]
22+
1923
extern crate alloc;
2024

2125
use alloc::boxed::Box;

src/rust/bitbox02-rust/src/hww.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,23 @@ mod tests {
185185
};
186186
if verification_required {
187187
// Verify pairing code.
188-
static mut EXPECTED_PAIRING_CODE: Option<String> = None;
188+
static EXPECTED_PAIRING_CODE: bitbox02::testing::UnsafeSyncRefCell<Option<String>> =
189+
bitbox02::testing::UnsafeSyncRefCell::new(None);
189190

190191
// Handshake hash as computed by the host. Should be the same as computed on the device. The
191192
// pairing code is derived from that.
192193
let handshake_hash: bitbox02_noise::HandshakeHash =
193194
host_noise.get_hash().try_into().unwrap();
194-
unsafe {
195-
EXPECTED_PAIRING_CODE =
196-
Some(crate::workflow::pairing::format_hash(&handshake_hash));
197-
}
195+
*EXPECTED_PAIRING_CODE.borrow_mut() =
196+
Some(crate::workflow::pairing::format_hash(&handshake_hash));
198197
static mut PAIRING_CONFIRMED: bool = false;
199198
mock(Data {
200199
ui_confirm_create: Some(Box::new(|params| {
201200
assert_eq!(params.title, "Pairing code");
202-
assert_eq!(params.body, unsafe {
203-
EXPECTED_PAIRING_CODE.as_ref().unwrap().as_str()
204-
});
201+
assert_eq!(
202+
params.body,
203+
EXPECTED_PAIRING_CODE.borrow().as_ref().unwrap().as_str()
204+
);
205205
unsafe {
206206
PAIRING_CONFIRMED = true;
207207
}

src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ struct WalletPolicyPkTranslator<'a> {
149149
address_index: u32,
150150
}
151151

152-
impl<'a> miniscript::Translator<String, bitcoin::PublicKey, Error>
153-
for WalletPolicyPkTranslator<'a>
154-
{
152+
impl miniscript::Translator<String, bitcoin::PublicKey, Error> for WalletPolicyPkTranslator<'_> {
155153
fn pk(&mut self, pk: &String) -> Result<bitcoin::PublicKey, Error> {
156154
let (key_index, multipath_index_left, multipath_index_right) =
157155
parse_wallet_policy_pk(pk).or(Err(Error::InvalidInput))?;
@@ -273,7 +271,7 @@ pub struct ParsedPolicy<'a> {
273271
pub descriptor: Descriptor<String>,
274272
}
275273

276-
impl<'a> ParsedPolicy<'a> {
274+
impl ParsedPolicy<'_> {
277275
/// Iterates over the placeholder keys in this descriptor. For tr() descriptors, this covers the
278276
/// internal key and every key in every leaf script.
279277
/// This iterates the keys "left-to-right" in the descriptor.

src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction/cbor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a, U: Update> HashedWriter<'a, U> {
3535
}
3636
}
3737

38-
impl<'a, U: Update> Write for HashedWriter<'a, U> {
38+
impl<U: Update> Write for HashedWriter<'_, U> {
3939
type Error = ();
4040
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
4141
self.0.update(buf);

src/rust/bitbox02-rust/src/hww/api/ethereum/amount.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Amount<'a> {
2222
pub value: BigUint,
2323
}
2424

25-
impl<'a> Amount<'a> {
25+
impl Amount<'_> {
2626
/// Formats the amount with the right number of decimal places, suffixed with the unit. If the
2727
/// value (without the unit suffix) is too long to fit on the screen, it will be truncated and
2828
/// ellipsis ('...') are appended.

src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum Transaction<'a> {
3535
Eip1559(&'a pb::EthSignEip1559Request),
3636
}
3737

38-
impl<'a> Transaction<'a> {
38+
impl Transaction<'_> {
3939
fn nonce(&self) -> &[u8] {
4040
match self {
4141
Transaction::Legacy(legacy) => &legacy.nonce,

src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ mod tests {
639639
Struct(Vec<Object<'a>>),
640640
}
641641

642-
impl<'a> Object<'a> {
642+
impl Object<'_> {
643643
fn encode(&self) -> Vec<u8> {
644644
match self {
645645
Object::String(s) => s.as_bytes().to_vec(),
@@ -680,7 +680,7 @@ mod tests {
680680
message: Object<'a>,
681681
}
682682

683-
impl<'a> TypedMessage<'a> {
683+
impl TypedMessage<'_> {
684684
/// The host is asked for a value at a member of an object. This handles this request and
685685
/// responds with value.
686686
fn handle_host_response(

src/rust/bitbox02-rust/src/xpubcache.rs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,22 @@ mod tests {
127127
#[derive(Clone)]
128128
struct MockXpub(Vec<u32>);
129129

130-
static mut CHILD_DERIVATIONS: u32 = 0;
131-
static mut ROOT_DERIVATIONS: u32 = 0;
130+
static CHILD_DERIVATIONS: bitbox02::testing::UnsafeSyncRefCell<u32> =
131+
bitbox02::testing::UnsafeSyncRefCell::new(0);
132+
static ROOT_DERIVATIONS: bitbox02::testing::UnsafeSyncRefCell<u32> =
133+
bitbox02::testing::UnsafeSyncRefCell::new(0);
132134

133135
impl Xpub for MockXpub {
134136
fn derive(&self, keypath: &[u32]) -> Result<Self, ()> {
135137
let mut kp = Vec::new();
136138
kp.extend_from_slice(&self.0);
137139
kp.extend_from_slice(keypath);
138-
unsafe {
139-
CHILD_DERIVATIONS += keypath.len() as u32;
140-
}
140+
*CHILD_DERIVATIONS.borrow_mut() += keypath.len() as u32;
141141
Ok(MockXpub(kp))
142142
}
143143

144144
fn from_keypath(keypath: &[u32]) -> Result<Self, ()> {
145-
unsafe {
146-
ROOT_DERIVATIONS += 1;
147-
}
145+
*ROOT_DERIVATIONS.borrow_mut() += 1;
148146
Ok(MockXpub(keypath.to_vec()))
149147
}
150148
}
@@ -154,18 +152,14 @@ mod tests {
154152
let mut cache = MockCache::new();
155153

156154
assert_eq!(cache.get_xpub(&[]).unwrap().0.as_slice(), &[]);
157-
unsafe {
158-
assert_eq!(CHILD_DERIVATIONS, 0u32);
159-
assert_eq!(ROOT_DERIVATIONS, 1u32);
160-
ROOT_DERIVATIONS = 0;
161-
}
155+
assert_eq!(*CHILD_DERIVATIONS.borrow(), 0u32);
156+
assert_eq!(*ROOT_DERIVATIONS.borrow(), 1u32);
157+
*ROOT_DERIVATIONS.borrow_mut() = 0;
162158

163159
assert_eq!(cache.get_xpub(&[1, 2, 3]).unwrap().0.as_slice(), &[1, 2, 3]);
164-
unsafe {
165-
assert_eq!(CHILD_DERIVATIONS, 0u32);
166-
assert_eq!(ROOT_DERIVATIONS, 1u32);
167-
ROOT_DERIVATIONS = 0;
168-
}
160+
assert_eq!(*CHILD_DERIVATIONS.borrow(), 0u32);
161+
assert_eq!(*ROOT_DERIVATIONS.borrow(), 1u32);
162+
*ROOT_DERIVATIONS.borrow_mut() = 0;
169163

170164
// Cache some keypaths.
171165
cache.add_keypath(&[84 + HARDENED, 0 + HARDENED, 0 + HARDENED]);
@@ -179,15 +173,13 @@ mod tests {
179173
.as_slice(),
180174
&[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 1, 2]
181175
);
182-
unsafe {
183-
// Two child derivations:
184-
// 1: m/84'/0'/0' -> m/84'/0'/0'/1
185-
// 2: m/84'/0'/0'/1 -> m/84'/0'/0'/1/2
186-
assert_eq!(CHILD_DERIVATIONS, 2u32);
187-
assert_eq!(ROOT_DERIVATIONS, 1u32);
188-
CHILD_DERIVATIONS = 0;
189-
ROOT_DERIVATIONS = 0;
190-
}
176+
// Two child derivations:
177+
// 1: m/84'/0'/0' -> m/84'/0'/0'/1
178+
// 2: m/84'/0'/0'/1 -> m/84'/0'/0'/1/2
179+
assert_eq!(*CHILD_DERIVATIONS.borrow(), 2u32);
180+
*CHILD_DERIVATIONS.borrow_mut() = 0;
181+
assert_eq!(*ROOT_DERIVATIONS.borrow(), 1u32);
182+
*ROOT_DERIVATIONS.borrow_mut() = 0;
191183

192184
// Same keypath again is a cache hit at m/84'/0'/0'/1 with one child derivation.
193185
assert_eq!(
@@ -198,11 +190,9 @@ mod tests {
198190
.as_slice(),
199191
&[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 1, 2]
200192
);
201-
unsafe {
202-
assert_eq!(CHILD_DERIVATIONS, 1u32);
203-
assert_eq!(ROOT_DERIVATIONS, 0u32);
204-
CHILD_DERIVATIONS = 0;
205-
}
193+
assert_eq!(*CHILD_DERIVATIONS.borrow(), 1u32);
194+
*CHILD_DERIVATIONS.borrow_mut() = 0;
195+
assert_eq!(*ROOT_DERIVATIONS.borrow(), 0u32);
206196

207197
// m/84'/0'/0'/0/0 is a cache hit at m/84'/0'/0', which was cached because of the above we
208198
// call using m/84'/0'/0'/1/2.
@@ -214,10 +204,8 @@ mod tests {
214204
.as_slice(),
215205
&[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0]
216206
);
217-
unsafe {
218-
assert_eq!(CHILD_DERIVATIONS, 2u32);
219-
assert_eq!(ROOT_DERIVATIONS, 0u32);
220-
}
207+
assert_eq!(*CHILD_DERIVATIONS.borrow(), 2u32);
208+
assert_eq!(*ROOT_DERIVATIONS.borrow(), 0u32);
221209
}
222210

223211
#[test]

0 commit comments

Comments
 (0)