Skip to content

Commit

Permalink
Add rust test case
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Aug 2, 2023
1 parent 70f38cc commit c2697a5
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
2 changes: 2 additions & 0 deletions imports/wasi_snapshot_preview1/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func pollOneoffFn(_ context.Context, mod api.Module, params []uint64) sys.Errno
// We check these fds once using poll.
n, errno := pollFileEventsOnce(blockingSubs, outBuf[nevents*32:])
if errno != 0 {
println("ABOUT TO ERROR OUT (1)", errno)
return errno
}
nevents += n
Expand All @@ -187,6 +188,7 @@ func pollOneoffFn(_ context.Context, mod api.Module, params []uint64) sys.Errno
// or any File.Poll() returns true ("ready"); otherwise we are done.
if n == 0 && timeout > 0 {
n, errno = pollFileEventsUntil(sysCtx, timeout, blockingSubs, outBuf[nevents*32:])
println("ABOUT TO ERROR OUT", errno)
if errno != 0 {
return errno
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ path = "wasi.rs"

[dependencies]
libc = "0.2"
mio = {version="0.8", features=["os-poll", "net"]}
100 changes: 100 additions & 0 deletions imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use std::net::{TcpListener};
use std::os::wasi::io::FromRawFd;
use std::process::exit;
use std::str::from_utf8;
use std::error::Error;

use std::collections::HashMap;
use std::time::Duration;

// Until NotADirectory is implemented, read the underlying error raised by
// wasi-libc. See https://github.com/rust-lang/rust/issues/86442
Expand All @@ -23,6 +27,7 @@ fn main() {
}
"stat" => main_stat(),
"sock" => main_sock(),
"mixed" => main_mixed().unwrap(),
_ => {
writeln!(io::stderr(), "unknown command: {}", args[1]).unwrap();
exit(1);
Expand Down Expand Up @@ -87,3 +92,98 @@ fn main_sock() {
}
}
}

fn main_mixed() -> Result<(), Box<dyn Error>> {

use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token};

// Some tokens to allow us to identify which event is for which socket.
const SERVER: Token = Token(0);
const STDIN: Token = Token(1);

// Create a poll instance.
let mut poll = Poll::new()?;
// Create storage for events.
let mut events = Events::with_capacity(128);

let mut server = unsafe { TcpListener::from_raw_fd(3) };
let mut stdin = unsafe { TcpStream::from_raw_fd(0) };


// Start listening for incoming connections.
poll.registry()
.register(&mut server, SERVER, Interest::READABLE)?;

// Keep track of incoming connections.
let mut m: HashMap<Token, TcpStream> = HashMap::new();

let mut count = 2;

// Start an event loop.
loop {
// Poll Mio for events, blocking until we get an event.
poll.poll(&mut events, Some(Duration::from_nanos(0)))?;

// Process each event.
for event in events.iter() {
// We can use the token we previously provided to `register` to
// determine for which socket the event is.
match event.token() {
SERVER => {
// If this is an event for the server, it means a connection
// is ready to be accepted.
//
// Accept the connection and add it to the map.
match server.accept() {
Ok((mut connection, _addr)) => {
let tok = Token(count);
_ = poll.registry()
.register(&mut connection, tok, Interest::READABLE);
m.insert(tok, connection);
// drop(connection);
count+=1;
},
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// ignore
},
Err(err) => panic!("ERROR! {}", err),
}
},

STDIN => {
// There is for reading on one of our connections, read it and echo.
let mut buf = [0u8; 32];
match stdin.read(&mut buf) {
Ok(n) if n>0 =>
println!("{}", String::from_utf8_lossy(&buf[0..n])),
_ => {} // ignore error.
}
},


conn_id => {
// There is for reading on one of our connections, read it and echo.
let mut buf = [0u8; 32];
let mut el = m.get(&conn_id).unwrap();
match el.read(&mut buf) {
Ok(n) if n>0 => {
let s = String::from_utf8_lossy(&buf[0..n]);
println!("{}", s);
if s.contains("wazero") {
return Ok(());
}
},

_ => {} // ignore error.
}
}
// We don't expect any events with tokens other than those we provided.
_ => unreachable!(),
}
}

}
}


Binary file modified imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions imports/wasi_snapshot_preview1/wasi_stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ func testLargeStdout(t *testing.T, tname string, bin []byte) {

func Test_Mixed(t *testing.T) {
toolchains := map[string][]byte{
// TODO: "cargo-wasi": wasmCargoWasi,
"zig-cc": wasmZigCc,
"cargo-wasi": wasmCargoWasi,
"zig-cc": wasmZigCc,
}
if wasmGotip != nil {
toolchains["gotip"] = wasmGotip
Expand Down
3 changes: 3 additions & 0 deletions internal/sysfs/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ func poll(fd uintptr, flag sys.Pflag, timeoutMillis int32) (ready bool, errno sy
}
fds := []pollFd{newPollFd(fd, _POLLIN, 0)}
count, errno := _poll(fds, timeoutMillis)
if errno == sys.EINTR {
return false, 0
}
return count > 0, errno
}

0 comments on commit c2697a5

Please sign in to comment.