Skip to content

Commit

Permalink
uplink: add panic extern
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Leegwater Simões committed Sep 22, 2023
1 parent d99a077 commit 4f7e662
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
5 changes: 5 additions & 0 deletions piecrust-uplink/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add call to `panic` in panic handler [#271]
- Add `panic` extern [#271]

### Removed

- Remove call to `hdebug` on panic [#271]
Expand Down
36 changes: 36 additions & 0 deletions piecrust-uplink/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::fmt::{self, Write};

mod allocator;

mod handlers;
Expand All @@ -19,3 +21,37 @@ pub use state::*;
mod debug;
#[cfg(feature = "debug")]
pub use debug::*;

/// A small struct that can `fmt::Write` to the argument buffer.
///
/// It is just an offset to the argument buffer, representing how much has been
/// written to it.
#[derive(Default)]
pub struct ArgbufWriter(usize);

impl ArgbufWriter {
pub fn ofs(&self) -> usize {
self.0
}
}

impl Write for ArgbufWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();
let bytes_len = bytes.len();

let new_ofs = self.0 + bytes_len;

if new_ofs > crate::ARGBUF_LEN {
return Err(fmt::Error);
}

state::with_arg_buf(|buf| {
buf[self.0..new_ofs].copy_from_slice(bytes);
});

self.0 = new_ofs;

Ok(())
}
}
39 changes: 0 additions & 39 deletions piecrust-uplink/src/abi/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,10 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::fmt::{self, Write};

use crate::abi::state;
use crate::ARGBUF_LEN;

extern "C" {
pub fn hdebug(arg_len: u32);
}

/// A small struct that can `fmt::Write` to the argument buffer.
///
/// It is just an offset to the argument buffer, representing how much has been
/// written to it.
#[derive(Default)]
pub struct ArgbufWriter(usize);

impl ArgbufWriter {
pub fn ofs(&self) -> usize {
self.0
}
}

impl Write for ArgbufWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();
let bytes_len = bytes.len();

let new_ofs = self.0 + bytes_len;

if new_ofs > ARGBUF_LEN {
return Err(fmt::Error);
}

state::with_arg_buf(|buf| {
buf[self.0..new_ofs].copy_from_slice(bytes);
});

self.0 = new_ofs;

Ok(())
}
}

/// Macro to format and send debug output to the host
#[macro_export]
macro_rules! debug {
Expand Down
19 changes: 18 additions & 1 deletion piecrust-uplink/src/abi/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::fmt::Write;
use core::panic::PanicInfo;

extern "C" {
pub fn panic(arg_len: u32);
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
unsafe fn handle_panic(info: &PanicInfo) -> ! {
let mut w = crate::ArgbufWriter::default();

// If we fail in writing to the argument buffer, we just call `panic` after
// writing a standard message instead.
if let Some(args) = info.message() {
if w.write_fmt(*args).is_err() {
w = crate::ArgbufWriter::default();
let _ = write!(w, "PANIC INFO TOO LONG");
}
}

panic(w.ofs() as u32);
unreachable!()
}

Expand Down

0 comments on commit 4f7e662

Please sign in to comment.