From e60e10e7774099823b87b8ae5c5ad445ba8ac1fe Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Fri, 10 Jul 2020 10:41:55 +0200 Subject: [PATCH] Add examples for panic_test and oom_test. --- deploy.py | 16 +++++++++++++++- examples/crypto_bench.rs | 19 +++++++++++-------- examples/oom_test.rs | 35 +++++++++++++++++++++++++++++++++++ examples/panic_test.rs | 24 ++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 examples/oom_test.rs create mode 100644 examples/panic_test.rs diff --git a/deploy.py b/deploy.py index d959a71..d956911 100755 --- a/deploy.py +++ b/deploy.py @@ -894,8 +894,22 @@ if __name__ == "__main__": dest="application", action="store_const", const="crypto_bench", - help=("Compiles and installs the crypto_bench example that tests " + help=("Compiles and installs the crypto_bench example that benchmarks " "the performance of the cryptographic algorithms on the board.")) + apps_group.add_argument( + "--panic_test", + dest="application", + action="store_const", + const="panic_test", + help=("Compiles and installs the panic_test example that immediately " + "triggers a panic.")) + apps_group.add_argument( + "--oom_test", + dest="application", + action="store_const", + const="oom_test", + help=("Compiles and installs the oom_test example that tests the " + "allocator until an out-of-memory error occurs.")) main_parser.set_defaults(features=["with_ctap1"]) diff --git a/examples/crypto_bench.rs b/examples/crypto_bench.rs index 95a82d2..ff92ba2 100644 --- a/examples/crypto_bench.rs +++ b/examples/crypto_bench.rs @@ -17,24 +17,26 @@ #[macro_use] extern crate alloc; extern crate crypto; -extern crate libtock; +extern crate lang_items; +extern crate libtock_drivers; use alloc::vec::Vec; use core::fmt::Write; use crypto::{ aes256, cbc, ecdsa, rng256, sha256, Decrypt16BytesBlock, Encrypt16BytesBlock, Hash256, }; -use libtock::console::Console; -use libtock::timer; -use libtock::timer::Timer; -use libtock::timer::Timestamp; +use libtock_drivers::console::Console; +use libtock_drivers::result::FlexUnwrap; +use libtock_drivers::timer; +use libtock_drivers::timer::Timer; +use libtock_drivers::timer::Timestamp; fn main() { let mut console = Console::new(); // Setup the timer with a dummy callback (we only care about reading the current time, but the // API forces us to set an alarm callback too). let mut with_callback = timer::with_callback(|_, _| {}); - let timer = with_callback.init().unwrap(); + let timer = with_callback.init().flex_unwrap(); let mut rng = rng256::TockRng256 {}; @@ -158,11 +160,11 @@ where writeln!(console, "----------------------------------------").unwrap(); let mut count = 1; for _ in 0..30 { - let start = Timestamp::::from_clock_value(timer.get_current_clock()); + let start = Timestamp::::from_clock_value(timer.get_current_clock().flex_unwrap()); for _ in 0..count { f(); } - let end = Timestamp::::from_clock_value(timer.get_current_clock()); + let end = Timestamp::::from_clock_value(timer.get_current_clock().flex_unwrap()); let elapsed = (end - start).ms(); writeln!( console, @@ -172,6 +174,7 @@ where elapsed / (count as f64) ) .unwrap(); + console.flush(); if elapsed > 1000.0 { break; } diff --git a/examples/oom_test.rs b/examples/oom_test.rs new file mode 100644 index 0000000..665993b --- /dev/null +++ b/examples/oom_test.rs @@ -0,0 +1,35 @@ +// Copyright 2020 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. + +#![no_std] + +extern crate alloc; +extern crate crypto; +extern crate lang_items; +extern crate libtock_drivers; + +use alloc::vec::Vec; +use core::fmt::Write; +use libtock_drivers::console::Console; + +fn main() { + writeln!(Console::new(), "****************************************").unwrap(); + for i in 0.. { + writeln!(Console::new(), "Allocating {} bytes...", 1 << i).unwrap(); + let x: Vec = Vec::with_capacity(1 << i); + writeln!(Console::new(), "Allocated!").unwrap(); + drop(x); + writeln!(Console::new(), "Dropped!").unwrap(); + } +} diff --git a/examples/panic_test.rs b/examples/panic_test.rs new file mode 100644 index 0000000..71c70b0 --- /dev/null +++ b/examples/panic_test.rs @@ -0,0 +1,24 @@ +// Copyright 2020 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. + +#![no_std] + +extern crate alloc; +extern crate crypto; +extern crate lang_items; +extern crate libtock_drivers; + +fn main() { + panic!("Bye world!") +}