Skip to content

Commit

Permalink
Merge #119
Browse files Browse the repository at this point in the history
119: Don't allocate when forwarding stdout r=stlankes a=mkroening

This avoids heap allocations and allows writing non-UTF-8-data to stdout.

Co-authored-by: Martin Kröning <mkroening@posteo.net>
  • Loading branch information
bors[bot] and mkroening committed Jun 14, 2021
2 parents 11f00e6 + b20a7fb commit 5f4a8de
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/linux/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl VirtualCPU for UhyveCPU {
return Ok(None);
}
UHYVE_UART_PORT => {
self.uart(String::from_utf8_lossy(&addr).to_string())?;
self.uart(&addr).unwrap();
}
UHYVE_PORT_CMDSIZE => {
let data_addr: usize =
Expand Down
4 changes: 1 addition & 3 deletions src/macos/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,8 @@ impl VirtualCPU for UhyveCPU {
}
UHYVE_UART_PORT => {
let al = (self.vcpu.read_register(&x86Reg::RAX)? & 0xFF) as u8;
let mut msg = vec![];
msg.push(al);

self.uart(std::str::from_utf8(&msg).unwrap().to_string())?;
self.uart(&[al]).unwrap();
self.vcpu.write_register(&x86Reg::RIP, rip + len)?;
}
UHYVE_PORT_CMDSIZE => {
Expand Down
10 changes: 4 additions & 6 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use log::{debug, error, warn};
use nix::errno::errno;
use raw_cpuid::CpuId;
use std::convert::TryInto;
use std::fs;
use std::io::Write;
use std::net::Ipv4Addr;
use std::ptr::write;
use std::time::{Duration, Instant, SystemTime};
use std::{fmt, mem, slice};
use std::{fs, io};

use crate::consts::*;
use crate::debug_manager::DebugManager;
Expand Down Expand Up @@ -446,11 +447,8 @@ pub trait VirtualCPU {
Ok(())
}

fn uart(&self, message: String) -> Result<()> {
print!("{}", message);
//io::stdout().flush().ok().expect("Could not flush stdout");

Ok(())
fn uart(&self, buf: &[u8]) -> io::Result<()> {
io::stdout().write_all(buf)
}
}

Expand Down

0 comments on commit 5f4a8de

Please sign in to comment.