tests/utils: add fmt::Write implementations for miri's native stdout/stderr

This commit is contained in:
Ralf Jung 2024-04-17 09:40:53 +02:00
parent d10f61313f
commit d7f79cc2b2
10 changed files with 64 additions and 59 deletions

View file

@ -7,15 +7,14 @@
extern crate alloc;
use alloc::alloc::*;
use core::fmt::Write;
extern "Rust" {
fn miri_write_to_stderr(bytes: &[u8]);
}
#[path = "../../utils/mod.no_std.rs"]
mod utils;
#[alloc_error_handler]
fn alloc_error_handler(_: Layout) -> ! {
let msg = "custom alloc error handler called!\n";
unsafe { miri_write_to_stderr(msg.as_bytes()) };
fn alloc_error_handler(layout: Layout) -> ! {
let _ = writeln!(utils::MiriStderr, "custom alloc error handler: {layout:?}");
core::intrinsics::abort(); //~ERROR: aborted
}
@ -25,9 +24,7 @@ mod plumbing {
#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
let msg = "custom panic handler called!\n";
unsafe { miri_write_to_stderr(msg.as_bytes()) };
core::intrinsics::abort();
loop {}
}
struct NoAlloc;

View file

@ -1,4 +1,4 @@
custom alloc error handler called!
custom alloc error handler: Layout { size: 4, align: 4 (1 << 2) }
error: abnormal termination: the program aborted execution
--> $DIR/alloc_error_handler_custom.rs:LL:CC
|
@ -12,8 +12,8 @@ note: inside `_::__rg_oom`
|
LL | #[alloc_error_handler]
| ---------------------- in this procedural macro expansion
LL | fn alloc_error_handler(_: Layout) -> ! {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | fn alloc_error_handler(layout: Layout) -> ! {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
note: inside `start`

View file

@ -7,17 +7,17 @@
extern crate alloc;
use alloc::alloc::*;
use core::fmt::Write;
extern "Rust" {
fn miri_write_to_stderr(bytes: &[u8]);
}
#[path = "../../utils/mod.no_std.rs"]
mod utils;
// The default no_std alloc_error_handler is a panic.
#[panic_handler]
fn panic_handler(_panic_info: &core::panic::PanicInfo) -> ! {
let msg = "custom panic handler called!\n";
unsafe { miri_write_to_stderr(msg.as_bytes()) };
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
let _ = writeln!(utils::MiriStderr, "custom panic handler called!");
let _ = writeln!(utils::MiriStderr, "{panic_info}");
core::intrinsics::abort(); //~ERROR: aborted
}

View file

@ -1,4 +1,6 @@
custom panic handler called!
panicked at RUSTLIB/alloc/src/alloc.rs:LL:CC:
memory allocation of 4 bytes failed
error: abnormal termination: the program aborted execution
--> $DIR/alloc_error_handler_no_std.rs:LL:CC
|

View file

@ -1,27 +1,11 @@
//@compile-flags: -Cpanic=abort
#![feature(start, core_intrinsics)]
#![no_std]
//@compile-flags: -Cpanic=abort
// Plumbing to let us use `writeln!` to host stderr:
extern "Rust" {
fn miri_write_to_stderr(bytes: &[u8]);
}
struct HostErr;
use core::fmt::Write;
impl Write for HostErr {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
unsafe {
miri_write_to_stderr(s.as_bytes());
}
Ok(())
}
}
// Aaaand the test:
#[path = "../../utils/mod.no_std.rs"]
mod utils;
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
@ -30,6 +14,6 @@ fn start(_: isize, _: *const *const u8) -> isize {
#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! {
writeln!(HostErr, "{panic_info}").ok();
writeln!(utils::MiriStderr, "{panic_info}").ok();
core::intrinsics::abort(); //~ ERROR: the program aborted execution
}

View file

@ -2,30 +2,14 @@
#![feature(start)]
#![no_std]
// Plumbing to let us use `writeln!` to host stdout:
extern "Rust" {
fn miri_write_to_stdout(bytes: &[u8]);
}
struct Host;
use core::fmt::Write;
impl Write for Host {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
unsafe {
miri_write_to_stdout(s.as_bytes());
}
Ok(())
}
}
// Aaaand the test:
#[path = "../utils/mod.no_std.rs"]
mod utils;
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
writeln!(Host, "hello, world!").unwrap();
writeln!(utils::MiriStdout, "hello, world!").unwrap();
0
}

View file

@ -0,0 +1,25 @@
use core::fmt::{self, Write};
use super::miri_extern;
pub struct MiriStderr;
impl Write for MiriStderr {
fn write_str(&mut self, s: &str) -> fmt::Result {
unsafe {
miri_extern::miri_write_to_stderr(s.as_bytes());
}
Ok(())
}
}
pub struct MiriStdout;
impl Write for MiriStdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
unsafe {
miri_extern::miri_write_to_stdout(s.as_bytes());
}
Ok(())
}
}

View file

@ -133,8 +133,8 @@ extern "Rust" {
/// with a null terminator.
/// Returns 0 if the `out` buffer was large enough, and the required size otherwise.
pub fn miri_host_to_target_path(
path: *const std::ffi::c_char,
out: *mut std::ffi::c_char,
path: *const core::ffi::c_char,
out: *mut core::ffi::c_char,
out_size: usize,
) -> usize;

View file

@ -0,0 +1,11 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#[macro_use]
mod macros;
mod io;
mod miri_extern;
pub use self::io::*;
pub use self::miri_extern::*;

View file

@ -5,9 +5,11 @@
mod macros;
mod fs;
mod io;
mod miri_extern;
pub use self::fs::*;
pub use self::io::*;
pub use self::miri_extern::*;
pub fn run_provenance_gc() {