Skip to content

Commit

Permalink
Correct increased memory usage (#7)
Browse files Browse the repository at this point in the history
Refactor: decrease deserialization memory overhead

With this commit the memory overhead on deserialization and data use
should be much smaller, while the execution performance should improve.

- Use references directly for the incoming data using serde-bytes
- Reduce clone use
- Rework command routing
- Decrease PIN size to 32 bytes for now
- Build USB/IP example in the CI calls (leave out cargo test for now)
- Remove git_version dependency
- The UDP Simulation is still not working.

Fixes #7

---------

Co-authored-by: sosthene-nitrokey <109070476+sosthene-nitrokey@users.noreply.github.com>
  • Loading branch information
szszszsz and sosthene-nitrokey authored Jul 31, 2023
1 parent 3213d94 commit 9ec3d56
Show file tree
Hide file tree
Showing 18 changed files with 686 additions and 581 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ repos:
rev: v1.0
hooks:
- id: fmt
- id: cargo-check
- id: clippy
# - id: cargo-check
# - id: clippy
41 changes: 11 additions & 30 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ heapless-bytes = { version = "0.3.0", features = ["cbor"] }
heapless = "0.7"
serde = { version = "1.0", default-features = false }
serde-indexed = "0.1.0"
serde_bytes = { version = "0.11.10", default-features = false, features=["alloc"] }
generic-array = "0.14.3"
ctap-types = "0.1"
ctaphid-dispatch = "0.1"
apdu-dispatch = "0.1"

trussed = "0.1.0"
pretty_env_logger = { version = "0.4.0", optional = true }
git-version = "0.3.5"
trussed-rsa-alloc = { version = "0.1.0", optional = true }
trussed-staging = { version = "0.1.0"}

Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ setup-fedora:

ci:
$(MAKE) check
cargo test
cargo build --example usbip --features $(FEATURES)
-timeout 2 $(MAKE) usbip
# TODO: run cargo test

check:
cargo fmt --check
Expand Down
9 changes: 7 additions & 2 deletions examples/udp_sim/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[macro_use]
extern crate delog;

use delog::log;
use heapless_bytes::{Bytes, Bytes32};

use webcrypt::{RequestDetails, RequestSource, Webcrypt};
use webcrypt::{Options, RequestDetails, RequestSource, Webcrypt};

use crate::udp_server::UDPServer;

Expand All @@ -13,6 +14,7 @@ mod udp_server;

#[cfg(feature = "enable-logs")]
use pretty_env_logger::env_logger;
use trussed::types::Location;

mod virt;

Expand All @@ -21,7 +23,10 @@ fn main() {
env_logger::init();

virt::with_ram_client("webcrypt", |client| {
let mut w = Webcrypt::new(client);
let mut w = webcrypt::Webcrypt::new_with_options(
client,
Options::new(Location::External, *b"1234", 10000),
);

let mut server = UDPServer::new();
loop {
Expand Down
5 changes: 2 additions & 3 deletions examples/udp_sim/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod dispatch {
platform::Platform,
serde_extensions::{ExtensionDispatch, ExtensionId, ExtensionImpl},
service::ServiceResources,
types::{Bytes, Context, Location},
types::{Bytes, Context},
};
use trussed_auth::{AuthBackend, AuthContext, AuthExtension, MAX_HW_KEY_LEN};

Expand Down Expand Up @@ -144,11 +144,10 @@ pub mod dispatch {
}
}

use std::path::PathBuf;
use trussed::types::Location;
use trussed::{
types::Bytes,
virt::{self, Client, Filesystem, Ram, StoreProvider},
virt::{self, Client, Ram, StoreProvider},
};

/// Client type using a dispatcher with the backends required by opcard
Expand Down
24 changes: 14 additions & 10 deletions examples/usbip/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ mod dispatch {
use trussed_staging::StagingBackend;
use trussed_staging::StagingContext;

use trussed::serde_extensions::ExtensionImpl;
use trussed::{
api::{reply, request, Reply, Request},
backend::{Backend as _, BackendId},
error::Error,
platform::Platform,
serde_extensions::{ExtensionDispatch, ExtensionId, ExtensionImpl as _},
serde_extensions::{ExtensionDispatch, ExtensionId, ExtensionImpl},
service::ServiceResources,
types::{Bytes, Context, Location},
types::{Bytes, Context},
};
use trussed_auth::{AuthBackend, AuthContext, AuthExtension, MAX_HW_KEY_LEN};

Expand Down Expand Up @@ -95,6 +94,12 @@ mod dispatch {
}
}

impl Default for Dispatch {
fn default() -> Self {
Self::new()
}
}

impl ExtensionDispatch for Dispatch {
type BackendId = Backend;
type Context = DispatchContext;
Expand Down Expand Up @@ -180,12 +185,11 @@ use clap::Parser;
use clap_num::maybe_hex;
use trussed::backend::BackendId;
use trussed::platform::{consent, reboot, ui};
use trussed::serde_extensions::ExtensionId;

use trussed::types::Location;
use trussed::{virt, ClientImplementation, Platform};
use trussed_usbip::ClientBuilder;

use crate::dispatch::Extension;
use usbd_ctaphid::constants::MESSAGE_SIZE;
use webcrypt::{debug, info, try_debug, try_info, try_warn, warn};
use webcrypt::{Options, PeekingBypass};
Expand Down Expand Up @@ -311,11 +315,11 @@ impl trussed::platform::UserInterface for UserInterface {
fn set_status(&mut self, status: ui::Status) {
debug!("Set status: {:?}", status);
if let ui::Status::Custom(s) = status {
let cs: CustomStatus = CustomStatus::try_from(s).unwrap_or_else(|_| {
let _cs: CustomStatus = CustomStatus::try_from(s).unwrap_or_else(|_| {
warn!("Unsupported status value: {:?}", status);
CustomStatus::Unknown
});
info!("Set status: [{}] {:?}", s, cs);
info!("Set status: [{}] {:?}", s, _cs);
}

if status == ui::Status::WaitingForUserPresence {
Expand All @@ -332,8 +336,8 @@ impl trussed::platform::UserInterface for UserInterface {
self.start_time.elapsed()
}

fn reboot(&mut self, to: reboot::To) -> ! {
info!("Restart! ({:?})", to);
fn reboot(&mut self, _to: reboot::To) -> ! {
info!("Restart! ({:?})", _to);
std::process::exit(25);
}
}
Expand Down Expand Up @@ -395,7 +399,7 @@ struct Apps {

const MAX_RESIDENT_CREDENTIAL_COUNT: u32 = 50;

impl<'a> trussed_usbip::Apps<'static, VirtClient, dispatch::Dispatch> for Apps {
impl trussed_usbip::Apps<'static, VirtClient, dispatch::Dispatch> for Apps {
type Data = ();
fn new<B: ClientBuilder<VirtClient, dispatch::Dispatch>>(builder: &B, _data: ()) -> Self {
let fido = fido_authenticator::Authenticator::new(
Expand Down
Loading

0 comments on commit 9ec3d56

Please sign in to comment.