[PATCH v2 9/9] tests: Additional IPC checks
Eugene Syromyatnikov
evgsyr at gmail.com
Thu Sep 8 14:07:45 UTC 2016
* tests/ipc_msg.c: Additional msgget (parameter format), msgctl
(parameter format, decoding of struct msqid_ds in IPC_SET/IPC_STAT
commands) checks.
* tests/ipc_sem.c: Additional semget, semctl checks.
* tests/ipc_shm.c: Additional shmget, shmctl checks.
* tests/semop.c: Additional semop checks. Added checks for semtimedop.
* tests/semop.test: Add explicit -e parameter in order to trace both
semop and semtimedop.
* tests/shmxt.c: Additional shmat/shmdt tests.
---
tests/ipc_msg.c | 50 ++++++++++++++++++++++++++++++++++++-----
tests/ipc_sem.c | 29 ++++++++++++++++++++++++
tests/ipc_shm.c | 37 +++++++++++++++++++++++++++++++
tests/semop.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/semop.test | 2 +-
tests/shmxt.c | 11 +++++++++
6 files changed, 187 insertions(+), 7 deletions(-)
diff --git a/tests/ipc_msg.c b/tests/ipc_msg.c
index 2fd7d4f..7de1bf7 100644
--- a/tests/ipc_msg.c
+++ b/tests/ipc_msg.c
@@ -27,10 +27,15 @@
*/
#include "tests.h"
+#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/msg.h>
+#include <sys/types.h>
+
+#include "xlat.h"
+#include "xlat/resource_flags.h"
static int id = -1;
@@ -48,18 +53,42 @@ main(void)
int rc;
struct msqid_ds ds;
+ static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
+ static const int bogus_msgid = 0xfdb97531;
+ static const int bogus_cmd = 0xdeadbeef;
+ static void * const bogus_addr = (void *) -1L;
+ static const int bogus_flags = 0xface1e55 & ~IPC_CREAT;
+
+ assert(msgget(bogus_key, bogus_flags) == -1);
+ printf("msgget\\(%#llx, %s%s%s%#x\\|%#04o\\) += %s\n",
+ zero_extend_signed_to_ull(bogus_key),
+ IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
+ IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
+ IPC_NOWAIT & bogus_flags ? "IPC_NOWAIT\\|" : "",
+ bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | IPC_NOWAIT),
+ bogus_flags & 0777,
+ sprintrc_grep(-1));
+
id = msgget(IPC_PRIVATE, 0600);
if (id < 0)
perror_msg_and_skip("msgget");
printf("msgget\\(IPC_PRIVATE, 0600\\) += %d\n", id);
atexit(cleanup);
+ assert(msgctl(bogus_msgid, bogus_cmd, NULL) == -1);
+ printf("msgctl\\(%d, (IPC_64\\|)?%#x /\\* MSG_\\?\\?\\? \\*/, NULL\\) "
+ "+= %s\n", bogus_msgid, bogus_cmd, sprintrc_grep(-1));
+
+ assert(msgctl(bogus_msgid, IPC_SET, bogus_addr) == -1);
+ printf("msgctl\\(%d, (IPC_64\\|)?IPC_SET, %p\\) += %s\n",
+ bogus_msgid, bogus_addr, sprintrc_grep(-1));
+
if (msgctl(id, IPC_STAT, &ds))
perror_msg_and_skip("msgctl IPC_STAT");
- printf("msgctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{msg_perm=\\{uid=%u, gid=%u, "
- "mode=%#o, key=%u, cuid=%u, cgid=%u\\}, msg_stime=%u, msg_rtime=%u, "
- "msg_ctime=%u, msg_qnum=%u, msg_qbytes=%u, msg_lspid=%u, "
- "msg_lrpid=%u\\}\\) += 0\n",
+ printf("msgctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{msg_perm=\\{uid=%u, "
+ "gid=%u, mode=%#o, key=%u, cuid=%u, cgid=%u\\}, msg_stime=%u, "
+ "msg_rtime=%u, msg_ctime=%u, msg_qnum=%u, msg_qbytes=%u, "
+ "msg_lspid=%u, msg_lrpid=%u\\}\\) += 0\n",
id, (unsigned) ds.msg_perm.uid, (unsigned) ds.msg_perm.gid,
(unsigned) ds.msg_perm.mode, (unsigned) ds.msg_perm.__key,
(unsigned) ds.msg_perm.cuid, (unsigned) ds.msg_perm.cgid,
@@ -68,6 +97,13 @@ main(void)
(unsigned) ds.msg_qbytes, (unsigned) ds.msg_lspid,
(unsigned) ds.msg_lrpid);
+ if (msgctl(id, IPC_SET, &ds))
+ perror_msg_and_skip("msgctl IPC_SET");
+ printf("msgctl\\(%d, (IPC_64\\|)?IPC_SET, \\{msg_perm=\\{uid=%u, "
+ "gid=%u, mode=%#o\\}, ...\\}\\) += 0\n",
+ id, (unsigned) ds.msg_perm.uid, (unsigned) ds.msg_perm.gid,
+ (unsigned) ds.msg_perm.mode);
+
int max = msgctl(0, MSG_INFO, &ds);
if (max < 0)
perror_msg_and_skip("msgctl MSG_INFO");
@@ -81,9 +117,11 @@ main(void)
*/
if (-1 != rc || EINVAL != errno)
perror_msg_and_skip("msgctl MSG_STAT");
- printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += -1 EINVAL \\(%m\\)\n", id, &ds);
+ printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += "
+ "-1 EINVAL \\(%m\\)\n", id, &ds);
} else {
- printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += %d\n", id, &ds, id);
+ printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += %d\n",
+ id, &ds, id);
}
return 0;
diff --git a/tests/ipc_sem.c b/tests/ipc_sem.c
index afe74d2..74d0fbd 100644
--- a/tests/ipc_sem.c
+++ b/tests/ipc_sem.c
@@ -27,11 +27,15 @@
*/
#include "tests.h"
+#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
+#include "xlat.h"
+#include "xlat/resource_flags.h"
+
union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
@@ -53,17 +57,42 @@ cleanup(void)
int
main(void)
{
+ static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
+ static const int bogus_semid = 0xfdb97531;
+ static const int bogus_semnum = 0xeca86420;
+ static const int bogus_size = 0xdec0ded1;
+ static const int bogus_flags = 0xface1e55;
+ static const int bogus_cmd = 0xdeadbeef;
+ static const unsigned long bogus_arg =
+ (unsigned long) 0xbadc0dedfffffaceULL;
+
int rc;
union semun un;
struct semid_ds ds;
struct seminfo info;
+ assert(semget(bogus_key, bogus_size, bogus_flags) == -1);
+ printf("semget\\(%#llx, %d, %s%s%s%#x\\|%#04o\\) += %s\n",
+ zero_extend_signed_to_ull(bogus_key), bogus_size,
+ IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
+ IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
+ IPC_NOWAIT & bogus_flags ? "IPC_NOWAIT\\|" : "",
+ bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | IPC_NOWAIT),
+ bogus_flags & 0777,
+ sprintrc_grep(-1));
+
id = semget(IPC_PRIVATE, 1, 0600);
if (id < 0)
perror_msg_and_skip("semget");
printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
atexit(cleanup);
+ assert(semctl(bogus_semid, bogus_semnum, bogus_cmd, bogus_arg) == -1);
+ printf("semctl\\(%d, %d, (IPC_64\\|)?%#x /\\* SEM_\\?\\?\\? \\*/, "
+ "\\[?%#lx\\]?\\) += %s\n",
+ bogus_semid, bogus_semnum, bogus_cmd, bogus_arg,
+ sprintrc_grep(-1));
+
un.buf = &ds;
if (semctl(id, 0, IPC_STAT, un))
perror_msg_and_skip("semctl IPC_STAT");
diff --git a/tests/ipc_shm.c b/tests/ipc_shm.c
index 54723e2..f339d0e 100644
--- a/tests/ipc_shm.c
+++ b/tests/ipc_shm.c
@@ -27,11 +27,15 @@
*/
#include "tests.h"
+#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
+#include "xlat.h"
+#include "xlat/shm_resource_flags.h"
+
static int id = -1;
static void
@@ -45,15 +49,41 @@ cleanup(void)
int
main(void)
{
+ static const key_t bogus_key = (key_t)0xeca86420fdb97531ULL;
+ static const int bogus_id = 0xdefaced1;
+ static const int bogus_cmd = 0xdefaced2;
+ static void * const bogus_addr = (void *) -1L;
+ static const size_t bogus_size = (size_t) 0xdec0ded1dec0ded2ULL;
+ static const int bogus_flags = 0xface1e55;
+
int rc;
struct shmid_ds ds;
+ assert(shmget(bogus_key, bogus_size, bogus_flags) == -1);
+ printf("shmget\\(%#llx, %zu, %s%s%s%#x\\|%#04o\\) += %s\n",
+ zero_extend_signed_to_ull(bogus_key), bogus_size,
+ IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
+ IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
+ SHM_HUGETLB & bogus_flags ? "SHM_HUGETLB\\|" : "",
+ bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | SHM_HUGETLB),
+ bogus_flags & 0777,
+ sprintrc_grep(-1));
+
id = shmget(IPC_PRIVATE, 1, 0600);
if (id < 0)
perror_msg_and_skip("shmget");
printf("shmget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
atexit(cleanup);
+ assert(shmctl(bogus_id, bogus_cmd, NULL) == -1);
+ printf("shmctl\\(%d, (IPC_64\\|)?%#x /\\* SHM_\\?\\?\\? \\*/, "
+ "NULL\\) += %s\n",
+ bogus_id, bogus_cmd, sprintrc_grep(-1));
+
+ assert(shmctl(bogus_id, IPC_STAT, bogus_addr) == -1);
+ printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, %p\\) += %s\n",
+ bogus_id, bogus_addr, sprintrc_grep(-1));
+
if (shmctl(id, IPC_STAT, &ds))
perror_msg_and_skip("shmctl IPC_STAT");
printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{shm_perm=\\{uid=%u, gid=%u, "
@@ -68,6 +98,13 @@ main(void)
(unsigned) ds.shm_atime, (unsigned) ds.shm_dtime,
(unsigned) ds. shm_ctime);
+ if (shmctl(id, IPC_SET, &ds))
+ perror_msg_and_skip("shmctl IPC_SET");
+ printf("shmctl\\(%d, (IPC_64\\|)?IPC_SET, \\{shm_perm=\\{uid=%u, gid=%u, "
+ "mode=%#o\\}, ...\\}\\) += 0\n",
+ id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid,
+ (unsigned) ds.shm_perm.mode);
+
int max = shmctl(0, SHM_INFO, &ds);
if (max < 0)
perror_msg_and_skip("shmctl SHM_INFO");
diff --git a/tests/semop.c b/tests/semop.c
index 214ce32..1d1f38d 100644
--- a/tests/semop.c
+++ b/tests/semop.c
@@ -1,11 +1,16 @@
#include "tests.h"
+#include <assert.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include "xlat.h"
+#include "xlat/semop_flags.h"
+
union semun
{
int val;
@@ -26,6 +31,14 @@ cleanup(void)
int
main(void)
{
+ static const int bogus_semid = 0xfdb97531;
+ static void * const bogus_sops = (void *) -1L;
+ static const unsigned bogus_nsops = 0xdeadbeef;
+
+ static const struct timespec ts_data = { 1, 123456789 };
+
+ struct timespec *ts = tail_memdup(&ts_data, sizeof(*ts));
+
id = semget(IPC_PRIVATE, 1, 0600);
if (id < 0)
perror_msg_and_skip("semget");
@@ -36,10 +49,32 @@ main(void)
perror_msg_and_skip("semctl");
struct sembuf *const sem_b = tail_alloc(sizeof(*sem_b));
+ struct sembuf *const sem_b2 = tail_alloc(sizeof(*sem_b2));
+
+ assert(semop(bogus_semid, NULL, bogus_nsops) == -1);
+ printf("semop(%d, NULL, %u) = %s\n",
+ bogus_semid, bogus_nsops, sprintrc(-1));
+
+ assert(semop(bogus_semid, bogus_sops, 1) == -1);
+ printf("semop(%d, %p, %u) = %s\n",
+ bogus_semid, bogus_sops, 1, sprintrc(-1));
+
sem_b->sem_num = 0;
sem_b->sem_op = 1;
sem_b->sem_flg = SEM_UNDO;
+ sem_b2->sem_num = 0xface;
+ sem_b2->sem_op = 0xf00d;
+ sem_b2->sem_flg = 0xbeef;
+
+ assert(semop(bogus_semid, sem_b2, 2) == -1);
+ printf("semop(%d, [{%hu, %hd, %s%s%#hx}, %p], %u) = %s\n",
+ bogus_semid, sem_b2->sem_num, sem_b2->sem_op,
+ sem_b2->sem_flg & SEM_UNDO ? "SEM_UNDO|" : "",
+ sem_b2->sem_flg & IPC_NOWAIT ? "IPC_NOWAIT|" : "",
+ sem_b2->sem_flg & ~(SEM_UNDO | IPC_NOWAIT),
+ sem_b2 + 1, 2, sprintrc(-1));
+
if (semop(id, sem_b, 1))
perror_msg_and_skip("semop, 1");
printf("semop(%d, [{0, 1, SEM_UNDO}], 1) = 0\n", id);
@@ -49,6 +84,36 @@ main(void)
perror_msg_and_skip("semop, -1");
printf("semop(%d, [{0, -1, SEM_UNDO}], 1) = 0\n", id);
+ assert(semtimedop(bogus_semid, NULL, bogus_nsops, NULL) == -1);
+ printf("semtimedop(%d, NULL, %u, NULL) = %s\n",
+ bogus_semid, bogus_nsops, sprintrc(-1));
+
+ assert(semtimedop(id, sem_b + 1, 1, ts + 1) == -1);
+ printf("semtimedop(%d, %p, 1, %p) = %s\n",
+ id, sem_b + 1, ts + 1, sprintrc(-1));
+
+ assert(semtimedop(bogus_semid, sem_b2, 2, ts) == -1);
+ printf("semtimedop(%d, [{%hu, %hd, %s%s%#hx}, %p], %u, {%jd, %jd}) = "
+ "%s\n",
+ bogus_semid, sem_b2->sem_num, sem_b2->sem_op,
+ sem_b2->sem_flg & SEM_UNDO ? "SEM_UNDO|" : "",
+ sem_b2->sem_flg & IPC_NOWAIT ? "IPC_NOWAIT|" : "",
+ sem_b2->sem_flg & ~(SEM_UNDO | IPC_NOWAIT),
+ sem_b2 + 1, 2,
+ (intmax_t) ts->tv_sec, (intmax_t) ts->tv_nsec,
+ sprintrc(-1));
+
+ sem_b->sem_op = 1;
+ if (semtimedop(id, sem_b, 1, NULL))
+ perror_msg_and_skip("semtimedop, 1");
+ printf("semtimedop(%d, [{0, 1, SEM_UNDO}], 1, NULL) = 0\n", id);
+
+ sem_b->sem_op = -1;
+ if (semtimedop(id, sem_b, 1, ts))
+ perror_msg_and_skip("semtimedop, -1");
+ printf("semtimedop(%d, [{0, -1, SEM_UNDO}], 1, {%jd, %jd}) = 0\n", id,
+ (intmax_t) ts->tv_sec, (intmax_t) ts->tv_nsec);
+
puts("+++ exited with 0 +++");
return 0;
}
diff --git a/tests/semop.test b/tests/semop.test
index 7e8f32c..3e77d2f 100755
--- a/tests/semop.test
+++ b/tests/semop.test
@@ -3,4 +3,4 @@
# Check semop syscall decoding.
. "${srcdir=.}/init.sh"
-run_strace_match_diff -a32
+run_strace_match_diff -a32 -e trace=semop,semtimedop
diff --git a/tests/shmxt.c b/tests/shmxt.c
index 8e087f3..4f073d7 100644
--- a/tests/shmxt.c
+++ b/tests/shmxt.c
@@ -1,4 +1,5 @@
#include "tests.h"
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
@@ -26,6 +27,13 @@ main(void)
perror_msg_and_skip("shmget");
atexit(cleanup);
+ assert(shmat(0xfdb97531, (void *) (unsigned long) 0xdec0ded1dec0ded2ULL,
+ (int)0xffffffff) == (void *)-1);
+ printf("%s(%d, %p, SHM_REMAP|SHM_RDONLY|SHM_RND|0xffff8fff) = %s\n",
+ SHMAT, 0xfdb97531,
+ (void *) (unsigned long) 0xdec0ded1dec0ded2ULL,
+ sprintrc(-1));
+
shmat(id, NULL, SHM_REMAP);
printf("%s(%d, NULL, SHM_REMAP) = -1 %s (%m)\n",
SHMAT, id, errno2name());
@@ -35,6 +43,9 @@ main(void)
perror_msg_and_skip("shmat SHM_RDONLY");
printf("%s(%d, NULL, SHM_RDONLY) = %p\n", SHMAT, id, shmaddr);
+ assert(shmdt(NULL) == -1);
+ printf("shmdt(NULL) = %s\n", sprintrc(-1));
+
if (shmdt(shmaddr))
perror_msg_and_skip("shmdt");
printf("shmdt(%p) = 0\n", shmaddr);
--
1.7.10.4
More information about the Strace-devel
mailing list