Skip to content

Commit e714b87

Browse files
committed
Fix struct sigevent decoding for musl
Do not rely on "struct sigevent.__pad" being located at the same address as "struct sigevent.sigev_notify_thread_id", it's not the case with musl libc. Do not rely on struct sigevent definition at all to access sigev_notify_thread_id. * configure.ac (AC_CHECK_MEMBERS): Remove struct sigevent._sigev_un._pad and struct sigevent.__pad. * sigevent.h: New file. * Makefile.am (strace_SOURCES): Add it. * print_sigevent.c: Include it. * print_sigevent.c (struct_sigevent): Remove. (print_sigevent): Update all struct_sigevent users. * tests/timer_create.c: Include "sigevent.h". (main): Use struct_sigevent instead of struct sigevent, all struct sigevent clients changed.
1 parent 2a6ac94 commit e714b87

File tree

5 files changed

+73
-58
lines changed

5 files changed

+73
-58
lines changed

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automake input for strace.
22
#
33
# Copyright (c) 2002-2009 Roland McGrath <[email protected]>
4-
# Copyright (c) 2006-2015 Dmitry V. Levin <[email protected]>
4+
# Copyright (c) 2006-2016 Dmitry V. Levin <[email protected]>
55
# Copyright (c) 2008-2015 Mike Frysinger <[email protected]>
66
# Copyright (c) 2015 Elvira Khabirova <[email protected]>
77
# All rights reserved.
@@ -172,6 +172,7 @@ strace_SOURCES = \
172172
seccomp_fprog.h \
173173
sendfile.c \
174174
sigaltstack.c \
175+
sigevent.h \
175176
signal.c \
176177
signalfd.c \
177178
sigreturn.c \

configure.ac

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,7 @@ AC_CHECK_TYPES([struct pt_all_user_regs, struct ia64_fpreg, struct ptrace_peeksi
378378

379379
AC_CHECK_MEMBERS([struct utsname.domainname],,, [#include <sys/utsname.h>])
380380

381-
AC_CHECK_MEMBERS([struct sigevent._sigev_un._pad,
382-
struct sigevent.__pad,
383-
siginfo_t.si_syscall,
381+
AC_CHECK_MEMBERS([siginfo_t.si_syscall,
384382
siginfo_t.si_timerid,
385383
siginfo_t.si_overrun],,, [#include <signal.h>])
386384

print_sigevent.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2003, 2004 Ulrich Drepper <[email protected]>
3-
* Copyright (c) 2005-2015 Dmitry V. Levin <[email protected]>
3+
* Copyright (c) 2005-2016 Dmitry V. Levin <[email protected]>
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -29,12 +29,10 @@
2929
#include "defs.h"
3030

3131
#include DEF_MPERS_TYPE(struct_sigevent)
32-
33-
#include <signal.h>
34-
typedef struct sigevent struct_sigevent;
35-
32+
#include "sigevent.h"
3633
#include MPERS_DEFS
3734

35+
#include <signal.h>
3836
#include "xlat/sigev_value.h"
3937

4038
MPERS_PRINTER_DECL(void, print_sigevent)(struct tcb *tcp, const long addr)
@@ -66,30 +64,13 @@ MPERS_PRINTER_DECL(void, print_sigevent)(struct tcb *tcp, const long addr)
6664

6765
switch (sev.sigev_notify) {
6866
case SIGEV_THREAD_ID:
69-
#ifndef sigev_notify_thread_id
70-
/*
71-
* _pad[0] is the _tid field which might not be
72-
* present in the userlevel definition of the struct.
73-
*/
74-
# if defined HAVE_STRUCT_SIGEVENT__SIGEV_UN__PAD
75-
# define sigev_notify_thread_id _sigev_un._pad[0]
76-
# elif defined HAVE_STRUCT_SIGEVENT___PAD
77-
# define sigev_notify_thread_id __pad[0]
78-
# endif
79-
#endif
80-
81-
#ifdef sigev_notify_thread_id
82-
tprintf(", sigev_notify_thread_id=%d",
83-
sev.sigev_notify_thread_id);
84-
#else
85-
# warning unfamiliar struct sigevent => incomplete SIGEV_THREAD_ID decoding
86-
#endif
67+
tprintf(", sigev_notify_thread_id=%d", sev.sigev_un.tid);
8768
break;
8869
case SIGEV_THREAD:
8970
tprints(", sigev_notify_function=");
90-
printaddr((unsigned long) sev.sigev_notify_function);
71+
printaddr(sev.sigev_un.sigev_thread.function);
9172
tprints(", sigev_notify_attributes=");
92-
printaddr((unsigned long) sev.sigev_notify_attributes);
73+
printaddr(sev.sigev_un.sigev_thread.attribute);
9374
break;
9475
}
9576
tprints("}");

sigevent.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2016 Dmitry V. Levin <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. The name of the author may not be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#ifndef SIGEVENT_H_
29+
#define SIGEVENT_H_
30+
31+
typedef struct {
32+
union {
33+
int sival_int;
34+
unsigned long sival_ptr;
35+
} sigev_value;
36+
int sigev_signo;
37+
int sigev_notify;
38+
union {
39+
int tid;
40+
struct {
41+
unsigned long function;
42+
unsigned long attribute;
43+
} sigev_thread;
44+
} sigev_un;
45+
} struct_sigevent;
46+
47+
#endif

tests/timer_create.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@
3434
# include <signal.h>
3535
# include <time.h>
3636
# include <unistd.h>
37+
# include "sigevent.h"
3738

3839
int
3940
main(void)
4041
{
4142
int tid[4] = {};
42-
struct sigevent sev = {
43+
struct_sigevent sev = {
4344
.sigev_notify = SIGEV_NONE,
4445
.sigev_signo = 0xfacefeed,
45-
.sigev_value.sival_ptr =
46-
(void *) (unsigned long) 0xdeadbeefbadc0ded
46+
.sigev_value.sival_ptr = (unsigned long) 0xdeadbeefbadc0ded
4747
};
4848

4949
if (syscall(__NR_timer_create, CLOCK_REALTIME, &sev, &tid[0]))
5050
perror_msg_and_skip("timer_create CLOCK_REALTIME");
51-
printf("timer_create(CLOCK_REALTIME, {sigev_value={int=%d, ptr=%p}"
51+
printf("timer_create(CLOCK_REALTIME, {sigev_value={int=%d, ptr=%#lx}"
5252
", sigev_signo=%u, sigev_notify=SIGEV_NONE}"
5353
", [%d]) = 0\n",
5454
sev.sigev_value.sival_int,
@@ -59,54 +59,42 @@ main(void)
5959
sev.sigev_signo = SIGALRM;
6060
if (syscall(__NR_timer_create, CLOCK_MONOTONIC, &sev, &tid[1]))
6161
perror_msg_and_skip("timer_create CLOCK_MONOTONIC");
62-
printf("timer_create(CLOCK_MONOTONIC, {sigev_value={int=%d, ptr=%p}"
62+
printf("timer_create(CLOCK_MONOTONIC, {sigev_value={int=%d, ptr=%#lx}"
6363
", sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}"
6464
", [%d]) = 0\n",
6565
sev.sigev_value.sival_int,
6666
sev.sigev_value.sival_ptr, tid[1]);
6767

6868
sev.sigev_notify = SIGEV_THREAD;
69-
sev.sigev_notify_function =
70-
(void *) (unsigned long) 0xdeadbeefbadc0ded;
71-
sev.sigev_notify_attributes =
72-
(void *) (unsigned long) 0xcafef00dfacefeed;
69+
sev.sigev_un.sigev_thread.function = (unsigned long) 0xdeadbeefbadc0ded;
70+
sev.sigev_un.sigev_thread.attribute = (unsigned long) 0xcafef00dfacefeed;
7371
if (syscall(__NR_timer_create, CLOCK_REALTIME, &sev, &tid[2]))
7472
perror_msg_and_skip("timer_create CLOCK_REALTIME");
75-
printf("timer_create(CLOCK_REALTIME, {sigev_value={int=%d, ptr=%p}"
73+
printf("timer_create(CLOCK_REALTIME, {sigev_value={int=%d, ptr=%#lx}"
7674
", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD"
77-
", sigev_notify_function=%p, sigev_notify_attributes=%p}"
75+
", sigev_notify_function=%#lx, sigev_notify_attributes=%#lx}"
7876
", [%d]) = 0\n",
7977
sev.sigev_value.sival_int,
8078
sev.sigev_value.sival_ptr,
81-
sev.sigev_notify_function,
82-
sev.sigev_notify_attributes,
79+
sev.sigev_un.sigev_thread.function,
80+
sev.sigev_un.sigev_thread.attribute,
8381
tid[2]);
8482

85-
#ifndef sigev_notify_thread_id
86-
# if defined HAVE_STRUCT_SIGEVENT__SIGEV_UN__PAD
87-
# define sigev_notify_thread_id _sigev_un._pad[0]
88-
# elif defined HAVE_STRUCT_SIGEVENT___PAD
89-
# define sigev_notify_thread_id __pad[0]
90-
# endif
91-
#endif /* !sigev_notify_thread_id */
92-
93-
#ifdef sigev_notify_thread_id
94-
# ifndef SIGEV_THREAD_ID
95-
# define SIGEV_THREAD_ID 4
96-
# endif
83+
#ifndef SIGEV_THREAD_ID
84+
# define SIGEV_THREAD_ID 4
85+
#endif
9786
sev.sigev_notify = SIGEV_THREAD_ID;
98-
sev.sigev_notify_thread_id = getpid();
87+
sev.sigev_un.tid = getpid();
9988
if (syscall(__NR_timer_create, CLOCK_MONOTONIC, &sev, &tid[3]))
10089
perror_msg_and_skip("timer_create CLOCK_MONOTONIC");
101-
printf("timer_create(CLOCK_MONOTONIC, {sigev_value={int=%d, ptr=%p}"
90+
printf("timer_create(CLOCK_MONOTONIC, {sigev_value={int=%d, ptr=%#lx}"
10291
", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD_ID"
10392
", sigev_notify_thread_id=%d}"
10493
", [%d]) = 0\n",
10594
sev.sigev_value.sival_int,
10695
sev.sigev_value.sival_ptr,
107-
sev.sigev_notify_thread_id,
96+
sev.sigev_un.tid,
10897
tid[3]);
109-
#endif /* sigev_notify_thread_id */
11098

11199
puts("+++ exited with 0 +++");
112100
return 0;

0 commit comments

Comments
 (0)