Skip to content

Commit

Permalink
Add examples for panic_test and oom_test.
Browse files Browse the repository at this point in the history
  • Loading branch information
gendx committed Aug 7, 2020
1 parent 58e2f12 commit e60e10e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
16 changes: 15 additions & 1 deletion deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,22 @@ def main(args):
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"])

Expand Down
19 changes: 11 additions & 8 deletions examples/crypto_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};

Expand Down Expand Up @@ -158,11 +160,11 @@ where
writeln!(console, "----------------------------------------").unwrap();
let mut count = 1;
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 {
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();
writeln!(
console,
Expand All @@ -172,6 +174,7 @@ where
elapsed / (count as f64)
)
.unwrap();
console.flush();
if elapsed > 1000.0 {
break;
}
Expand Down
35 changes: 35 additions & 0 deletions examples/oom_test.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions examples/panic_test.rs
Original file line number Diff line number Diff line change
@@ -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!")
}

0 comments on commit e60e10e

Please sign in to comment.