summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2021-02-24 10:33:43 -0500
committerDaniel Kahn Gillmor <dkg@fifthhorseman.net>2021-02-24 11:15:31 -0500
commit2ca0b719e38fc83a6c3a3709f671e69e53a0b6be (patch)
treed342a9a1afe96a9cae269679ebf18652c9e1eb65 /test
parentf6ddc32695fb4602ed8b8e1e53b0636b28fef170 (diff)
downloadlibfaketime-2ca0b719e38fc83a6c3a3709f671e69e53a0b6be.tar.gz
test getpid() against library with constructor that calls it
This is an attempt to ensure that an external library invocation of getpid doesn't trigger a crash (e.g. #295) or an infinite loop (e.g. #297).
Diffstat (limited to 'test')
-rw-r--r--test/Makefile5
-rw-r--r--test/libgetpid.c13
-rw-r--r--test/libgetpid.h6
-rwxr-xr-xtest/pidtest.sh7
-rw-r--r--test/use_lib_getpid.c12
5 files changed, 42 insertions, 1 deletions
diff --git a/test/Makefile b/test/Makefile
index d193bef..8f56489 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -28,6 +28,9 @@ functest:
randomtest: getrandom_test use_lib_random librandom.so
./randomtest.sh
+getpidtest: use_lib_getpid libgetpid.so
+ ./pidtest.sh
+
lib%.o: lib%.c
${CC} -c -o $@ -fpic ${CFLAGS} $<
@@ -38,7 +41,7 @@ use_lib_%: use_lib_%.c lib%.so
${CC} -L. -o $@ ${CFLAGS} $< -l$*
clean:
- @rm -f ${OBJ} timetest getrandom_test lib*.o lib*.so use_lib_random
+ @rm -f ${OBJ} timetest getrandom_test lib*.o lib*.so use_lib_random use_lib_getpid
distclean: clean
@echo
diff --git a/test/libgetpid.c b/test/libgetpid.c
new file mode 100644
index 0000000..1fb84c5
--- /dev/null
+++ b/test/libgetpid.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+void getpid_func() {
+ fprintf(stderr, " called getpid_func()\n");
+}
+
+
+static __attribute__((constructor)) void getpid_init() {
+ pid_t pid = getpid();
+ fprintf(stderr, " getpid() yielded %d\n", pid);
+}
diff --git a/test/libgetpid.h b/test/libgetpid.h
new file mode 100644
index 0000000..e18c61d
--- /dev/null
+++ b/test/libgetpid.h
@@ -0,0 +1,6 @@
+#ifndef __LIBGETPID_H__
+#define __LIBGETPID_H__
+
+extern void getpid_func();
+
+#endif
diff --git a/test/pidtest.sh b/test/pidtest.sh
index 617bad8..83e0771 100755
--- a/test/pidtest.sh
+++ b/test/pidtest.sh
@@ -17,3 +17,10 @@ if [ $output != 13 ]; then
printf >&2 'Failed to enforce a rigid response to getpid()\n'
exit 2
fi
+
+printf 'testing shared object with getpid() in library constructor\n'
+LD_LIBRARY_PATH=. ./use_lib_getpid
+printf 'now with LD_PRELOAD and FAKETIME_FAKEPID\n'
+FAKETIME_FAKEPID=25 LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_getpid
+printf 'now with LD_PRELOAD without FAKETIME_FAKEPID\n'
+LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_getpid
diff --git a/test/use_lib_getpid.c b/test/use_lib_getpid.c
new file mode 100644
index 0000000..c9f3deb
--- /dev/null
+++ b/test/use_lib_getpid.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "libgetpid.h"
+
+int main() {
+ pid_t pid;
+ getpid_func();
+ pid = getpid();
+ fprintf(stderr, " getpid() -> %d\n", pid);
+ return 0;
+}