From e75a23602aed74595cfeeb9d26e5faf3011a9244 Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Fri, 22 Mar 2019 16:57:29 -0700 Subject: [PATCH] wip --- lib/emscripten/src/syscalls/emscripten_vfs.rs | 10 +++++++- lib/emscripten/src/syscalls/unix/select.rs | 23 ++++++++++++++++++- lib/emscripten/src/syscalls/unix/vfs.rs | 11 +++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/emscripten/src/syscalls/emscripten_vfs.rs b/lib/emscripten/src/syscalls/emscripten_vfs.rs index a8c9f54a8..a34a42e68 100644 --- a/lib/emscripten/src/syscalls/emscripten_vfs.rs +++ b/lib/emscripten/src/syscalls/emscripten_vfs.rs @@ -97,7 +97,12 @@ impl EmscriptenVfs { pub fn get_host_socket_fd(&self, vfd: &VirtualFd) -> Option { match self.fd_map.get(&vfd) { - Some(FileHandle::Socket(fd)) => Some(*fd), + Some(FileHandle::Socket(fd)) => { + if *fd < 0 { + panic!() + } + Some(*fd) + }, _ => None, } } @@ -119,6 +124,9 @@ impl EmscriptenVfs { pub fn new_socket_fd(&mut self, host_fd: Fd) -> VirtualFd { let vfd = self.next_lowest_fd(); + if host_fd < 0 { + panic!() + } self.fd_map.insert(vfd.clone(), FileHandle::Socket(host_fd)); vfd } diff --git a/lib/emscripten/src/syscalls/unix/select.rs b/lib/emscripten/src/syscalls/unix/select.rs index 6f77708e8..315154988 100644 --- a/lib/emscripten/src/syscalls/unix/select.rs +++ b/lib/emscripten/src/syscalls/unix/select.rs @@ -194,6 +194,27 @@ pub fn ___syscall142(ctx: &mut Ctx, _which: libc::c_int, mut varargs: VarArgs) - .map(|pair| pair.host_fd) .collect::>() ); + + unsafe { + use libc::FD_ISSET; + let s = 3; + let x = [ + FD_ISSET(s, readfds_set_ptr), + FD_ISSET(s + 1, readfds_set_ptr), + FD_ISSET(s + 2, readfds_set_ptr), + FD_ISSET(s + 3, readfds_set_ptr), + FD_ISSET(s + 4, readfds_set_ptr), + FD_ISSET(s + 5, readfds_set_ptr), + FD_ISSET(s + 6, readfds_set_ptr), + FD_ISSET(s + 7, readfds_set_ptr), + FD_ISSET(s + 8, readfds_set_ptr), + FD_ISSET(s + 9, readfds_set_ptr), + FD_ISSET(s + 10, readfds_set_ptr), + FD_ISSET(s + 11, readfds_set_ptr), + ]; + debug!("BEFORE sets start with fd #{}: {:?}", s, x); + } + let fds_slice = unsafe { slice::from_raw_parts(readfds_set_ptr as *const u8, 4) } as &[u8]; debug!("host read set before: {:?}", fds_slice); let mut result = unsafe { libc::select(sz, readfds_set_ptr, writefds_set_ptr, 0 as _, 0 as _) }; @@ -216,7 +237,7 @@ pub fn ___syscall142(ctx: &mut Ctx, _which: libc::c_int, mut varargs: VarArgs) - FD_ISSET(s + 10, readfds_set_ptr), FD_ISSET(s + 11, readfds_set_ptr), ]; - debug!("sets (start with fd #{}: {:?}", s, x); + debug!("AFTER sets (start with fd #{}: {:?}", s, x); } if result == -1 { diff --git a/lib/emscripten/src/syscalls/unix/vfs.rs b/lib/emscripten/src/syscalls/unix/vfs.rs index 9f1bf729c..d00cc5879 100644 --- a/lib/emscripten/src/syscalls/unix/vfs.rs +++ b/lib/emscripten/src/syscalls/unix/vfs.rs @@ -264,7 +264,7 @@ pub fn ___syscall63(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int Some(FileHandle::Socket(src_host_fd)) => unsafe { // get a dst file descriptor, or just use the underlying dup syscall let dst_host_fd = libc::dup(*src_host_fd); - if dst_host_fd == -1 { + if dst_host_fd < 0 { panic!() } FileHandle::Socket(dst_host_fd) @@ -440,6 +440,10 @@ pub fn ___syscall102(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in let new_accept_host_fd = unsafe { libc::accept(host_socket_fd, address, address_len_addr) }; + if new_accept_host_fd < 0 { + panic!("accept file descriptor should not be negative."); + } + unsafe { let address_linux = emscripten_memory_pointer!(ctx.memory(0), address_addr) as *mut LinuxSockAddr; @@ -548,6 +552,9 @@ pub fn ___syscall102(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in "recvfrom: socket: {}, flags: {}, len: {}, result: {}", socket, flags, len, recv_result ); + if recv_result < 0 { + panic!("recvfrom result was less than zero. Errno: {}", errno::errno()); + } recv_result } 14 => { @@ -691,7 +698,7 @@ pub fn ___syscall146(ctx: &mut Ctx, _which: i32, mut varargs: VarArgs) -> i32 { Some(FileHandle::Socket(host_fd)) => unsafe { let count = libc::write(*host_fd, iov_buf_ptr, count); if count < 0 { - panic!() + panic!("the count from write was less than zero. errno: {}", errno::errno()); } count as usize },