Add examples for panic_test and oom_test.

This commit is contained in:
Guillaume Endignoux
2020-07-10 10:41:55 +02:00
parent 58e2f1211c
commit e60e10e777
4 changed files with 85 additions and 9 deletions

View File

@@ -894,8 +894,22 @@ if __name__ == "__main__":
dest="application", dest="application",
action="store_const", action="store_const",
const="crypto_bench", 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.")) "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"]) main_parser.set_defaults(features=["with_ctap1"])

View File

@@ -17,24 +17,26 @@
#[macro_use] #[macro_use]
extern crate alloc; extern crate alloc;
extern crate crypto; extern crate crypto;
extern crate libtock; extern crate lang_items;
extern crate libtock_drivers;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::fmt::Write; use core::fmt::Write;
use crypto::{ use crypto::{
aes256, cbc, ecdsa, rng256, sha256, Decrypt16BytesBlock, Encrypt16BytesBlock, Hash256, aes256, cbc, ecdsa, rng256, sha256, Decrypt16BytesBlock, Encrypt16BytesBlock, Hash256,
}; };
use libtock::console::Console; use libtock_drivers::console::Console;
use libtock::timer; use libtock_drivers::result::FlexUnwrap;
use libtock::timer::Timer; use libtock_drivers::timer;
use libtock::timer::Timestamp; use libtock_drivers::timer::Timer;
use libtock_drivers::timer::Timestamp;
fn main() { fn main() {
let mut console = Console::new(); let mut console = Console::new();
// Setup the timer with a dummy callback (we only care about reading the current time, but the // 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). // API forces us to set an alarm callback too).
let mut with_callback = timer::with_callback(|_, _| {}); 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 {}; let mut rng = rng256::TockRng256 {};
@@ -158,11 +160,11 @@ where
writeln!(console, "----------------------------------------").unwrap(); writeln!(console, "----------------------------------------").unwrap();
let mut count = 1; let mut count = 1;
for _ in 0..30 { for _ in 0..30 {
let start = Timestamp::<f64>::from_clock_value(timer.get_current_clock()); let start = Timestamp::<f64>::from_clock_value(timer.get_current_clock().flex_unwrap());
for _ in 0..count { for _ in 0..count {
f(); f();
} }
let end = Timestamp::<f64>::from_clock_value(timer.get_current_clock()); let end = Timestamp::<f64>::from_clock_value(timer.get_current_clock().flex_unwrap());
let elapsed = (end - start).ms(); let elapsed = (end - start).ms();
writeln!( writeln!(
console, console,
@@ -172,6 +174,7 @@ where
elapsed / (count as f64) elapsed / (count as f64)
) )
.unwrap(); .unwrap();
console.flush();
if elapsed > 1000.0 { if elapsed > 1000.0 {
break; break;
} }

35
examples/oom_test.rs Normal file
View File

@@ -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<u8> = Vec::with_capacity(1 << i);
writeln!(Console::new(), "Allocated!").unwrap();
drop(x);
writeln!(Console::new(), "Dropped!").unwrap();
}
}

24
examples/panic_test.rs Normal file
View File

@@ -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!")
}