From a203ff13df251ac5a172997c2210e0b4a3322e44 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Thu, 24 Sep 2020 10:06:03 +0200 Subject: [PATCH] Create new store and add storage API --- .github/workflows/cargo_fmt.yml | 6 ++ .github/workflows/persistent_store_test.yml | 26 ++++++ libraries/persistent_store/Cargo.toml | 11 +++ libraries/persistent_store/src/lib.rs | 19 +++++ libraries/persistent_store/src/storage.rs | 95 +++++++++++++++++++++ run_desktop_tests.sh | 9 ++ 6 files changed, 166 insertions(+) create mode 100644 .github/workflows/persistent_store_test.yml create mode 100644 libraries/persistent_store/Cargo.toml create mode 100644 libraries/persistent_store/src/lib.rs create mode 100644 libraries/persistent_store/src/storage.rs diff --git a/.github/workflows/cargo_fmt.yml b/.github/workflows/cargo_fmt.yml index a3cbd70..6e38687 100644 --- a/.github/workflows/cargo_fmt.yml +++ b/.github/workflows/cargo_fmt.yml @@ -62,6 +62,12 @@ jobs: command: fmt args: --manifest-path libraries/crypto/Cargo.toml --all -- --check + - name: Cargo format libraries/persistent_store + uses: actions-rs/cargo@v1 + with: + command: fmt + args: --manifest-path libraries/persistent_store/Cargo.toml --all -- --check + - name: Cargo format tools/heapviz uses: actions-rs/cargo@v1 with: diff --git a/.github/workflows/persistent_store_test.yml b/.github/workflows/persistent_store_test.yml new file mode 100644 index 0000000..1a1d942 --- /dev/null +++ b/.github/workflows/persistent_store_test.yml @@ -0,0 +1,26 @@ +--- +name: Persistent store tests +on: + push: + paths: + - 'libraries/peristent_store/**' + pull_request: + types: [opened, synchronize, reopened] + +jobs: + persistent_store_test: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + + - name: Unit testing of Persistent store library (release mode) + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path libraries/persistent_store/Cargo.toml --release --features std + + - name: Unit testing of Persistent store library (debug mode) + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path libraries/persistent_store/Cargo.toml --features std diff --git a/libraries/persistent_store/Cargo.toml b/libraries/persistent_store/Cargo.toml new file mode 100644 index 0000000..5fe7c9f --- /dev/null +++ b/libraries/persistent_store/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "persistent_store" +version = "0.1.0" +authors = ["Julien Cretin "] +license = "Apache-2.0" +edition = "2018" + +[dependencies] + +[features] +std = [] diff --git a/libraries/persistent_store/src/lib.rs b/libraries/persistent_store/src/lib.rs new file mode 100644 index 0000000..05425c3 --- /dev/null +++ b/libraries/persistent_store/src/lib.rs @@ -0,0 +1,19 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![cfg_attr(not(feature = "std"), no_std)] + +mod storage; + +pub use self::storage::{Storage, StorageError, StorageIndex, StorageResult}; diff --git a/libraries/persistent_store/src/storage.rs b/libraries/persistent_store/src/storage.rs new file mode 100644 index 0000000..c5d55c5 --- /dev/null +++ b/libraries/persistent_store/src/storage.rs @@ -0,0 +1,95 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Represents a byte position in a storage +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct StorageIndex { + pub page: usize, + pub byte: usize, +} + +/// Represents a possible storage error +#[derive(Debug, PartialEq, Eq)] +pub enum StorageError { + /// Arguments are not correctly aligned + NotAligned, + + /// Arguments are out of bounds + OutOfBounds, + + /// Implementation-specific error + CustomError, +} + +pub type StorageResult = Result; + +/// Abstracts a flash storage +pub trait Storage { + /// The size of a word in bytes + fn word_size(&self) -> usize; + + /// The size of a page in bytes + fn page_size(&self) -> usize; + + /// The number of pages in the storage + fn num_pages(&self) -> usize; + + /// Maximum number of times a word can be written between page erasures + fn max_word_writes(&self) -> usize; + + /// Maximum number of times a page can be erased + fn max_page_erases(&self) -> usize; + + /// Reads a byte slice from the storage + /// + /// The `index` must designate `length` bytes in the storage. + fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]>; + + /// Writes a word slice to the storage + /// + /// The following pre-conditions must hold: + /// - The `index` must designate `value.len()` bytes in the storage. + /// - Both `index` and `value.len()` must be word-aligned. + /// - The written words should not have been written too many times since last page erasure. + fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()>; + + /// Erases a page of the storage + /// + /// The `page` must be in the storage. + fn erase_page(&mut self, page: usize) -> StorageResult<()>; +} + +impl StorageIndex { + /// Whether a slice fits in a storage page + fn is_valid(self, length: usize, storage: &impl Storage) -> bool { + let page_size = storage.page_size(); + self.page < storage.num_pages() && length <= page_size && self.byte <= page_size - length + } + + /// Returns the range of a valid slice + /// + /// The range starts at `self` with `length` bytes. + pub fn range( + self, + length: usize, + storage: &impl Storage, + ) -> StorageResult> { + if self.is_valid(length, storage) { + let start = self.page * storage.page_size() + self.byte; + Ok(start..start + length) + } else { + Err(StorageError::OutOfBounds) + } + } +} diff --git a/run_desktop_tests.sh b/run_desktop_tests.sh index 703e6e6..f2dff08 100755 --- a/run_desktop_tests.sh +++ b/run_desktop_tests.sh @@ -23,6 +23,9 @@ cd ../.. cd libraries/crypto cargo fmt --all -- --check cd ../.. +cd libraries/persistent_store +cargo fmt --all -- --check +cd ../.. cd tools/heapviz cargo fmt --all -- --check cd ../.. @@ -84,6 +87,9 @@ then cd libraries/crypto RUSTFLAGS='-C target-feature=+aes' cargo test --release --features std,derive_debug cd ../.. + cd libraries/persistent_store + cargo test --release --features std + cd ../.. cargo test --release --features std echo "Running unit tests on the desktop (debug mode)..." @@ -93,6 +99,9 @@ then cd libraries/crypto RUSTFLAGS='-C target-feature=+aes' cargo test --features std,derive_debug cd ../.. + cd libraries/persistent_store + cargo test --features std + cd ../.. cargo test --features std echo "Running unit tests on the desktop (release mode + CTAP1)..."