tests: add a test for select syscall decoding

Convert test/select.c into a regular test.

* test/Makefile (PROGS): Remove select.
* test/.gitignore: Likewise.
* test/select.c: Rewrite to ...
* tests/select.c: ... new file.
* tests/select.awk: New file.
* tests/select.test: New test.
* tests/Makefile.am (check_PROGRAMS): Add select.
(TESTS): Add select.test.
(EXTRA_DIST): Add select.awk.
* tests/.gitignore: Add select.
This commit is contained in:
Дмитрий Левин 2015-02-04 02:09:52 +00:00
parent 495f525d68
commit e837b14a53
8 changed files with 97 additions and 36 deletions

1
test/.gitignore vendored
View File

@ -10,5 +10,4 @@ wait_must_be_interruptible
threaded_execve
mtd
ubi
select
sigreturn

View File

@ -3,7 +3,7 @@ CFLAGS += -Wall
PROGS = \
vfork fork sig skodic clone leaderkill childthread \
sigkill_rain wait_must_be_interruptible threaded_execve \
mtd ubi select sigreturn
mtd ubi sigreturn
all: $(PROGS)

View File

@ -1,34 +0,0 @@
/* dave@treblig.org */
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char buffer[1024*1024*2];
int main()
{
fd_set rds;
struct timeval timeout;
FD_ZERO(&rds);
FD_SET(2, &rds);
/* Start with a nice simple select */
select(3, &rds, &rds, &rds, NULL);
/* Now the crash case that trinity found, negative nfds
* but with a pointer to a large chunk of valid memory.
*/
FD_ZERO((fd_set*)buffer);
FD_SET(2,(fd_set*)buffer);
select(-1, (fd_set *)buffer, NULL, NULL, NULL);
/* Another variant, with nfds exceeding allowed limit. */
timeout.tv_sec = 0;
timeout.tv_usec = 100;
select(FD_SETSIZE + 1, (fd_set *)buffer, NULL, NULL, &timeout);
return 0;
}

1
tests/.gitignore vendored
View File

@ -10,6 +10,7 @@ net-accept-connect
netlink_inet_diag
netlink_unix_diag
scm_rights
select
set_ptracer_any
sigaction
stack-fcall

View File

@ -15,6 +15,7 @@ check_PROGRAMS = \
netlink_inet_diag \
netlink_unix_diag \
scm_rights \
select \
set_ptracer_any \
sigaction \
stack-fcall \
@ -46,6 +47,7 @@ TESTS = \
ipc_shm.test \
ipc_sem.test \
scm_rights-fd.test \
select.test \
sigaction.test \
stat.test \
stat32-v.test \
@ -78,6 +80,7 @@ EXTRA_DIST = init.sh run.sh \
mmsg.expected \
net-yy-accept.awk \
net-yy-connect.awk \
select.awk \
sigaction.awk \
uid.awk \
unix-yy-accept.awk \

26
tests/select.awk Normal file
View File

@ -0,0 +1,26 @@
BEGIN {
r[1] = "^select\\(2, \\[0 1\\], \\[0 1\\], \\[0 1\\], NULL\\) += 1 \\(\\)$"
r[2] = "^select\\(-1, NULL, 0x[0-9a-f]+, NULL, NULL\\) += -1 "
r[3] = "^select\\(1025, \\[0\\], \\[\\], NULL, \\{0, 100\\}\\) += 0 \\(Timeout\\)$"
r[4] = "^\\+\\+\\+ exited with 0 \\+\\+\\+$"
lines = 4
fail = 0
}
NR > lines { exit 1 }
{
if (match($0, r[NR]))
next
print "Line " NR " does not match."
fail = 1
}
END {
if (fail == 0 && NR != lines) {
fail = 1
print "Expected " lines " lines, found " NR " line(s)."
}
exit fail
}

43
tests/select.c Normal file
View File

@ -0,0 +1,43 @@
/*
* Based on test by Dr. David Alan Gilbert <dave@treblig.org>
*/
#include <assert.h>
#include <unistd.h>
#include <sys/select.h>
static fd_set set[0x1000000 / sizeof(fd_set)];
int main()
{
int fds[2];
struct timeval timeout = { .tv_sec = 0, .tv_usec = 100 };
(void) close(0);
(void) close(1);
assert(pipe(fds) == 0);
/*
* Start with a nice simple select.
*/
FD_ZERO(set);
FD_SET(0, set);
FD_SET(1, set);
assert(select(2, set, set, set, NULL) == 1);
/*
* Now the crash case that trinity found, negative nfds
* but with a pointer to a large chunk of valid memory.
*/
FD_ZERO(set);
FD_SET(1,set);
assert(select(-1, NULL, set, NULL, NULL) == -1);
/*
* Another variant, with nfds exceeding FD_SETSIZE limit.
*/
FD_ZERO(set);
FD_SET(0,set);
assert(select(FD_SETSIZE + 1, set, set + 1, NULL, &timeout) == 0);
return 0;
}

23
tests/select.test Executable file
View File

@ -0,0 +1,23 @@
#!/bin/sh
# Check select syscall decoding.
. "${srcdir=.}/init.sh"
check_prog awk
./select ||
framework_skip_ 'select syscall does not behave as expected'
args="-eselect ./select"
$STRACE -o "$LOG" $args || {
cat "$LOG"
fail_ "$STRACE $args failed"
}
awk -f "$srcdir"/select.awk "$LOG" || {
cat "$LOG"
fail_ 'unexpected output'
}
exit 0