summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUli Schlachter <psychon@znc.in>2022-05-04 14:51:35 +0200
committerUli Schlachter <psychon@znc.in>2022-05-04 14:51:35 +0200
commitfff49b23fca69721dca669ff4fb3289bc7c24660 (patch)
tree588134218ed9f49403c9c2946ce2bfe4a1011f62
parent6e0f978079e2792c4f910ab8b2cc8e966350d2ab (diff)
downloadlibfaketime-fff49b23fca69721dca669ff4fb3289bc7c24660.tar.gz
Add FAIL_PRE_INIT_CALLS define
This commit adds a new define FAIL_PRE_INIT_CALLS. When that define is set, calls to clock_gettime() that occur before ftpl_init() was called (due to being marked with __attribute__((constructor))) will just fail and return -1. After this commit, the test case added in the previous commit no longer hangs. To make this actually work, this new define is enabled by default. Fixes: https://github.com/wolfcw/libfaketime/issues/365 Signed-off-by: Uli Schlachter <psychon@znc.in>
-rw-r--r--src/Makefile7
-rw-r--r--src/libfaketime.c11
2 files changed, 17 insertions, 1 deletions
diff --git a/src/Makefile b/src/Makefile
index 62e924c..df72d47 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -27,6 +27,11 @@
# without this, but the performance impact may require you to
# try it unsynchronized.
#
+# FAIL_PRE_INIT_CALLS
+# - If the time is queried before the library was initialised, let the
+# call fail instead of trying to initialise on-the-fly. This fixes /
+# works around hangs that were seen with address sanitizer.
+#
# * Compilation Defines that are unset by default:
#
# FAKE_FILE_TIMESTAMPS, FAKE_UTIME
@@ -110,7 +115,7 @@ PREFIX ?= /usr/local
LIBDIRNAME ?= /lib/faketime
PLATFORM ?=$(shell uname)
-CFLAGS += -std=gnu99 -Wall -Wextra -Werror -Wno-nonnull-compare -DFAKE_PTHREAD -DFAKE_STAT -DFAKE_UTIME -DFAKE_SLEEP -DFAKE_TIMERS -DFAKE_INTERNAL_CALLS -fPIC -DPREFIX='"'$(PREFIX)'"' -DLIBDIRNAME='"'$(LIBDIRNAME)'"' $(FAKETIME_COMPILE_CFLAGS)
+CFLAGS += -std=gnu99 -Wall -Wextra -Werror -Wno-nonnull-compare -DFAKE_PTHREAD -DFAKE_STAT -DFAKE_UTIME -DFAKE_SLEEP -DFAKE_TIMERS -DFAKE_INTERNAL_CALLS -DFAIL_PRE_INIT_CALLS -fPIC -DPREFIX='"'$(PREFIX)'"' -DLIBDIRNAME='"'$(LIBDIRNAME)'"' $(FAKETIME_COMPILE_CFLAGS)
ifeq ($(PLATFORM),SunOS)
CFLAGS += -D__EXTENSIONS__ -D_XOPEN_SOURCE=600
endif
diff --git a/src/libfaketime.c b/src/libfaketime.c
index f92ecf8..ec80ec8 100644
--- a/src/libfaketime.c
+++ b/src/libfaketime.c
@@ -2282,6 +2282,16 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp)
if (!initialized)
{
recursion_depth++;
+#ifdef FAIL_PRE_INIT_CALLS
+ fprintf(stderr, "libfaketime: clock_gettime() was called before initialization.\n");
+ fprintf(stderr, "libfaketime: Returning -1 on clock_gettime().\n");
+ if (tp != NULL)
+ {
+ tp->tv_sec = 0;
+ tp->tv_nsec = 0;
+ }
+ return -1;
+#else
if (recursion_depth == 2)
{
fprintf(stderr, "libfaketime: Unexpected recursive calls to clock_gettime() without proper initialization. Trying alternative.\n");
@@ -2302,6 +2312,7 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp)
else {
ftpl_init();
}
+#endif
recursion_depth--;
}
/* sanity check */