Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
[LibOS] Handle 0 being returned from DkStreamRead
Browse files Browse the repository at this point in the history
Signed-off-by: Borys Popławski <[email protected]>
  • Loading branch information
boryspoplawski committed Feb 22, 2021
1 parent ccb0c77 commit 24b0d0c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
9 changes: 6 additions & 3 deletions LibOS/shim/include/shim_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static inline int wait_event(AEVENTTYPE* e) {
do {
char byte;
PAL_NUM ret = DkStreamRead(e->event, 0, 1, &byte, NULL, 0);
err = ret == PAL_STREAM_ERROR ? PAL_ERRNO() : 0;
err = ret == PAL_STREAM_ERROR ? PAL_ERRNO() : (ret == 0 ? ENODATA : 0);
} while (err == EINTR || err == EAGAIN || err == EWOULDBLOCK);

return -err;
Expand All @@ -273,7 +273,7 @@ static inline int clear_event(AEVENTTYPE* e) {
int err = PAL_ERRNO();
if (err == EINTR) {
continue;
} else if (!err || err == EAGAIN || err == EWOULDBLOCK) {
} else if (err == EAGAIN || err == EWOULDBLOCK) {
break;
}
return -err;
Expand All @@ -290,10 +290,13 @@ static inline int clear_event(AEVENTTYPE* e) {
if (err == EINTR) {
continue;
} else if (err == EAGAIN || err == EWOULDBLOCK) {
/* This should not happen, since we polled above ... */
/* This should not happen, since we polled above... */
break;
}
return -err;
} else if (n == 0) {
/* This should not happen, since we polled above... */
return -ENODATA;
}
}

Expand Down
7 changes: 4 additions & 3 deletions LibOS/shim/src/ipc/shim_ipc_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,15 @@ static int receive_ipc_message(struct shim_ipc_port* port) {
DkStreamRead(port->pal_handle, /*offset=*/0, expected_size - bytes + readahead,
(void*)msg + bytes, NULL, 0);

if (read == PAL_STREAM_ERROR) {
if (PAL_ERRNO() == EINTR || PAL_ERRNO() == EAGAIN || PAL_ERRNO() == EWOULDBLOCK)
if (!read || read == PAL_STREAM_ERROR) {
int err = !read ? ENODATA : PAL_ERRNO();
if (err == EINTR || err == EAGAIN || err == EWOULDBLOCK)
continue;

debug("Port %p (handle %p) closed while receiving IPC message\n", port,
port->pal_handle);
del_ipc_port_fini(port);
ret = -PAL_ERRNO();
ret = -err;
goto out;
}

Expand Down
2 changes: 2 additions & 0 deletions LibOS/shim/src/shim_checkpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ static int read_exact(PAL_HANDLE handle, void* buf, size_t size) {
continue;
}
return -err;
} else if (x == 0) {
return -ENODATA;
}

bytes += x;
Expand Down
13 changes: 11 additions & 2 deletions LibOS/shim/src/shim_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,17 @@ noreturn void* shim_init(int argc, void* args) {
struct checkpoint_hdr hdr;

PAL_NUM ret = DkStreamRead(PAL_CB(parent_process), 0, sizeof(hdr), &hdr, NULL, 0);
if (ret == PAL_STREAM_ERROR || ret != sizeof(hdr))
DkProcessExit(PAL_ERRNO());
int err = 0;
if (ret == PAL_STREAM_ERROR) {
/* PAL_ERRNO() == 0 should not be possible now, but old code allowed for such case.
* This will be removed soon anyway. */
err = PAL_ERRNO() ?: ENOTRECOVERABLE;
} else if (ret != sizeof(hdr)) {
err = ENODATA;
}
if (err) {
DkProcessExit(err);
}

assert(hdr.size);
RUN_INIT(receive_checkpoint_and_restore, &hdr);
Expand Down
8 changes: 7 additions & 1 deletion LibOS/shim/test/regression/tcp_msg_peek.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ static void client(void) {

/* we specify dummy MSG_DONTWAIT and MSG_WAITALL just to test these flags */
printf("[client] receiving with MSG_PEEK: ");
count = client_recv(server_socket, buffer, sizeof(buffer), MSG_WAITALL | MSG_DONTWAIT | MSG_PEEK);
count = client_recv(server_socket, buffer, sizeof(buffer),
MSG_WAITALL | MSG_DONTWAIT | MSG_PEEK);
fwrite(buffer, count, 1, stdout);

printf("[client] receiving with MSG_PEEK again: ");
count = client_recv(server_socket, buffer, sizeof(buffer),
MSG_WAITALL | MSG_DONTWAIT | MSG_PEEK);
fwrite(buffer, count, 1, stdout);

printf("[client] receiving without MSG_PEEK: ");
Expand Down
1 change: 1 addition & 0 deletions LibOS/shim/test/regression/test_libos.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ def test_200_socket_udp(self):
def test_300_socket_tcp_msg_peek(self):
stdout, _ = self.run_binary(['tcp_msg_peek'], timeout=50)
self.assertIn('[client] receiving with MSG_PEEK: Hello from server!', stdout)
self.assertIn('[client] receiving with MSG_PEEK again: Hello from server!', stdout)
self.assertIn('[client] receiving without MSG_PEEK: Hello from server!', stdout)
self.assertIn('[client] checking how many bytes are left unread: 0', stdout)
self.assertIn('[client] done', stdout)
Expand Down

0 comments on commit 24b0d0c

Please sign in to comment.