aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorvan Hauser <vh@thc.org>2020-05-30 06:51:51 +0200
committervan Hauser <vh@thc.org>2020-05-30 06:51:51 +0200
commit8726d7b0a351fcb16587acd80a4b42b521264692 (patch)
tree9d140d93854b3b3575656719a4334c7d928e93fa /src
parent408ef5298bd2f233649dd6799757369e963b0a24 (diff)
downloadafl++-8726d7b0a351fcb16587acd80a4b42b521264692.tar.gz
simplified read_timed
Diffstat (limited to 'src')
-rw-r--r--src/afl-common.c48
1 files changed, 23 insertions, 25 deletions
diff --git a/src/afl-common.c b/src/afl-common.c
index 9fd4bf03..d428c9c5 100644
--- a/src/afl-common.c
+++ b/src/afl-common.c
@@ -869,53 +869,51 @@ u8 *u_stringify_time_diff(u8 *buf, u64 cur_ms, u64 event_ms) {
}
-/* Wrapper for select() and read(), reading exactly len bytes.
+/* Wrapper for select() and read(), reading len bytes.
+ Assumes that all bytes are available on read!
Returns the time passed to read.
If the wait times out, returns timeout_ms + 1;
Returns 0 if an error occurred (fd closed, signal, ...); */
u32 read_timed(s32 fd, void *buf, size_t len, u32 timeout_ms,
volatile u8 *stop_soon_p) {
- struct timeval timeout;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
+ struct timeval timeout;
timeout.tv_sec = (timeout_ms / 1000);
timeout.tv_usec = (timeout_ms % 1000) * 1000;
+#if !defined(__linux__)
+ u64 read_start = get_cur_time_us();
+#endif
- size_t read_total = 0;
- ssize_t len_read = 0;
-
- while (read_total < len) {
-
- /* set exceptfds as well to return when a child exited/closed the pipe. */
- int sret = select(fd + 1, &readfds, NULL, NULL, &timeout);
-
- if (!sret) {
-
- // printf("Timeout in sret.");
- return timeout_ms + 1;
+ /* set exceptfds as well to return when a child exited/closed the pipe. */
+ int sret = select(fd + 1, &readfds, NULL, NULL, &timeout);
- } else if (sret < 0) {
+ if (!sret) {
- /* Retry select for all signals other than than ctrl+c */
- if (errno == EINTR && !*stop_soon_p) { continue; }
- return 0;
+ return timeout_ms + 1;
- }
+ } else if (sret < 0) {
- len_read = read(fd, ((u8 *)buf) + read_total, len - read_total);
- if (len_read <= 0) { return 0; }
- read_total += len_read;
+ return 0;
}
- s32 exec_ms =
+ ssize_t len_read = read(fd, ((u8 *)buf), len);
+ if (len_read < len) { return 0; }
+
+#if defined(__linux__)
+ u32 exec_ms =
MIN(timeout_ms,
((u64)timeout_ms - (timeout.tv_sec * 1000 + timeout.tv_usec / 1000)));
- return exec_ms > 0 ? exec_ms
- : 1; // at least 1 milli must have passed (0 is an error)
+#else
+ u32 exec_ms = get_cur_time_us() - read_start;
+#endif
+
+ // ensure to report 1 ms has passed (0 is an error)
+ return exec_ms > 0 ? exec_ms : 1;
}