diff options
author | Dmitry V. Levin <ldv@altlinux.org> | 2019-11-18 13:31:57 +0000 |
---|---|---|
committer | Dmitry V. Levin <ldv@altlinux.org> | 2019-11-18 13:31:57 +0000 |
commit | 996f6a7d0f7f97d8630bc5fbf629c6532f90cfcb (patch) | |
tree | 17872a6baf079f04bc4f65ffb51b3d58d1e77f22 /tests/semop-common.c | |
parent | 897495915efd7cbfa64b4f2618df8c683a5a41fb (diff) | |
download | strace-996f6a7d0f7f97d8630bc5fbf629c6532f90cfcb.tar.gz |
tests: rewrite semop test
Starting with glibc commit glibc-2.30.9000-169-g765cdd0bff,
semop function in glibc is implemented on top of semtimedop,
which makes it unsuitable for the test of semop decoder.
Resolve this issue by creating two tests: one that checks decoding
of __NR_semop, and another that checks decoding of __NR_ipc SEMOP.
* tests/semop-common.c: New file.
* tests/Makefile.am (EXTRA_DIST): Add semop-common.c.
* tests/semop-indirect.c: New file.
* tests/semop.c: Rewrite using __NR_semop.
* tests/pure_executables.list: Add semop-indirect.
* tests/.gitignore: Likewise.
* tests/gen_tests.in (semop): Update.
(semop-indirect): New entry.
Diffstat (limited to 'tests/semop-common.c')
-rw-r--r-- | tests/semop-common.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/semop-common.c b/tests/semop-common.c new file mode 100644 index 000000000..646a11690 --- /dev/null +++ b/tests/semop-common.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016-2019 The strace developers. + * All rights reserved. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include <sys/ipc.h> +#include <sys/sem.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define XLAT_MACROS_ONLY +#include "xlat/semop_flags.h" +#undef XLAT_MACROS_ONLY + +static const char *errstr; + +static long +k_semop(const unsigned int semid, + const kernel_ulong_t sops, + const unsigned int nsops); + +union semun { + int val; + struct semid_ds *buf; + unsigned short *array; + struct seminfo *__buf; +}; + +static int id = -1; + +static void +cleanup(void) +{ + semctl(id, 0, IPC_RMID, 0); + id = -1; +} + +int +main(void) +{ + static const int bogus_semid = 0xfdb97531; + static kernel_ulong_t bogus_sops = (kernel_ulong_t) -1ULL; + static const unsigned int bogus_nsops = 0xdeadbeefU; + + id = semget(IPC_PRIVATE, 1, 0600); + if (id < 0) + perror_msg_and_skip("semget"); + atexit(cleanup); + + union semun sem_union = { .val = 0 }; + if (semctl(id, 0, SETVAL, sem_union) == -1) + perror_msg_and_skip("semctl"); + + k_semop(bogus_semid, 0, bogus_nsops); + printf("semop(%d, NULL, %u) = %s\n", + bogus_semid, bogus_nsops, errstr); + + k_semop(bogus_semid, bogus_sops, 1); + printf("semop(%d, %#llx, %u) = %s\n", + bogus_semid, (unsigned long long) bogus_sops, 1, errstr); + + TAIL_ALLOC_OBJECT_CONST_PTR(struct sembuf, sem_b2); + sem_b2->sem_num = 0xface; + sem_b2->sem_op = 0xf00d; + sem_b2->sem_flg = 0xbeef; + + k_semop(bogus_semid, (uintptr_t) sem_b2, 2); + 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|" : "", + (short) (sem_b2->sem_flg & ~(SEM_UNDO | IPC_NOWAIT)), + sem_b2 + 1, 2, errstr); + + TAIL_ALLOC_OBJECT_CONST_PTR(struct sembuf, sem_b); + sem_b->sem_num = 0; + sem_b->sem_op = 1; + sem_b->sem_flg = SEM_UNDO; + + k_semop(id, (uintptr_t) sem_b, 1); + printf("semop(%d, [{0, 1, SEM_UNDO}], 1) = %s\n", id, errstr); + + sem_b->sem_op = -1; + k_semop(id, (uintptr_t) sem_b, 1); + printf("semop(%d, [{0, -1, SEM_UNDO}], 1) = %s\n", id, errstr); + + puts("+++ exited with 0 +++"); + return 0; +} |