libfaketime modifies the system time for a single application
libfaketime modifies the system time for a single application
The required CI gates cover macOS arm64/arm64e and Linux glibc, musl,
32-bit/time64, ARM64, Fedora, Debian, Rocky Linux, CentOS Stream, openSUSE,
and Arch Linux. The documented C snippets are compiled and run in CI,
including the optional deterministic-random interfaces. The
sanitizer and container jobs are time-bounded and upload their complete test
logs when a run fails. Runtime-specific checks are deliberately optional:
some language runtimes use vDSO or direct system calls and can bypass
LD_PRELOAD/dyld interposition. Such a bypass is reported as a compatibility
limitation rather than treated as proof of a core libfaketime failure.
For a local bounded Linux run, use test/docker_baseline.sh with a compatible
image, for example gcc:13-bookworm. Set DOCKER_PLATFORM=linux/amd64 when
the image architecture must be selected explicitly. Set
LIBFAKETIME_RUN_OPTIONAL_RUNTIME_TESTS=1 only when the optional Rust runtime
probe and its toolchain are available.
Introduction
Compatibility issues
Installation
Usage a) Basics b) Using absolute dates c) Using 'start at' dates d) Using offsets for relative dates e) Advanced features and caveats f) Faking the date and time system-wide g) Using the "faketime" wrapper script h) "Limiting" libfaketime based on elapsed time or number of calls i) "Limiting" libfaketime per process j) Spawning an external process k) Saving timestamps to file, loading them from file l) Replacing random numbers with deterministic number sequences
License
Contact
Introduction
libfaketime intercepts various system calls that programs use to retrieve the current date and time. It then reports modified (faked) dates and times (as specified by you, the user) to these programs. This means you can modify the system time a program sees without having to change the time system-wide.
libfaketime allows you to specify both absolute dates (e.g., 01/01/2004) and relative dates (e.g., 10 days ago).
libfaketime might be used for various purposes, for example
libfaketime ships with a command line wrapper called "faketime" that makes it easier to use, but does not expose all of libfaketime's functionality. If your use case is not covered by the faketime command, make sure to look in this documentation whether it can be achieved by using libfaketime directly.
libfaketime is supposed to work on Linux and macOS. Your mileage may vary; some other *NIXes have been reported to work as well.
libfaketime uses the library preload mechanism of your operating system's linker (which is involved in starting programs) and thus cannot work with statically linked binaries or binaries that have the setuid-flag set (e.g., suidroot programs like "ping" or "passwd"). Please see you system linker's manpage for further details.
libfaketime supports the pthreads environment. A separate library is built (libfaketimeMT.so.1), which contains the pthread synchronization calls. This library also single-threads calls through the time() intercept because several variables are statically cached by the library and could cause issues when accessed without synchronization.
However, the performance penalty for this might be an issue for some applications. If this is the case, you can try using an unsynchronized time() intercept by removing the -DPTHREAD_SINGLETHREADED_TIME from the Makefile and rebuilding libfaketimeMT.so.1
Java-/JVM-based applications work but you need to pass in an extra argument (FAKETIME_DONT_FAKE_MONOTONIC). See usage basics below for details. Without this argument the java command usually hangs.
libfaketime will eventually be bypassed by applications that dynamically load system libraries, such as librt, explicitly themselves instead of relying on the linker to do so at application start time. libfaketime will not work with those applications unless you can modify them.
This apparently happens a lot in complex run-time environments, e.g., for programs written in golang, for some Java Virtual Machine implementations, etc. Since libfaketime is effectively bypassed in such situations, there's nothing we can do about it. Please consider asking the appropriate developers and vendors to implement their runtime environment in a way that supports intercepting selected system calls through LD_PRELOAD.
Applications can explicitly be designed to prevent libfaketime from working, e.g., by checking whether certain environment variables are set or whether libfaketime-specific files are present.
Runtime compatibility: when a language runtime or framework reports real time, first check which clock it uses and whether it dynamically loads its own system libraries. A minimal C reproducer using the same clock API is required before treating an application-specific report as a libfaketime bug. In particular, Go, Python, .NET, Boost.Asio, jemalloc users, Proton/Wine, and JVMs may cache clocks, use vDSO/direct system calls, load libraries after startup, or combine real and faked clock domains. These cases can hang or ignore the preload even when the basic libfaketime test suite passes. Keep preload ordering explicit and use FAKETIME_DONT_FAKE_MONOTONIC when the runtime requires a real monotonic clock. Such limitations are compatibility constraints, not a security boundary.
CLOCK_MONOTONIC test: Running "make test" performs a series of tests after successful compilation of the libfaketime library. On some platforms, the "CLOCK_MONOTONIC test" will apparently hang forever. If and only if this happens on your platform, add the CFLAG -DFORCE_MONOTONIC_FIX to src/Makefile and recompile libfaketime. Do not set FORCE_MONOTONIC_FIX on platforms where the test does not hang.
If you observe hangs on the CLOCK_REALTIME test, add the CFLAG -DFORCE_PTHREAD_NONVER. Also set this FORCE_PTHREAD_NONVER flag in case FORCE_MONOTONIC_FIX alone does not solve the hang on the MONOTONIC_CLOCK test.
If FORCE_MONOTONIC_FIX was not set as a compile-time flag, you can also set an environment variable FAKETIME_FORCE_MONOTONIC_FIX=1 if you want to enable the fix at run-time, or to 0 if you explicitly want to disable it. The fix is automatically enabled if libfaketime was compiled on a system with glibc as the underlying libc implementation, and a glibc version is detected at run-time that is assumed to need this workaround. Please use Github issues at https://github.com/wolfcw/libfaketime/issues to report any observed hangs during CLOCK_MONOTONIC tests and report your CPU architecture, libc implementation (e.g., glibc 2.30) and any other details that might help (e.g., Linux distribution, use within, e.g., Docker containers etc.).
Please try to avoid compiling with FORCE_MONOTONIC_FIX on platforms that do not need it. While it won't make a difference in most cases, depending on the specific FAKETIME settings in use, it would cause certain intercepted functions such as pthread_cond_timedwait() return with a time-out too early or too late, which could break some applications. Try compiling without FORCE_MONOTONIC_FIX first and check whether the tests appear to hang. If they do, you can either set the FAKETIME_FORCE_MONOTONIC_FIX environment variable to 1, or re-compile with FORCE_MONOTONIC_FIX set.
Running "make" compiles both library versions and a test program, which it then also executes.
If the test works fine, you should copy the libfaketime libraries (libfaketime.so.1, and libfaketimeMT.so.1) to the place you want them in. Running "make install" will attempt to place them in /usr/local/lib/faketime and will install the wrapper shell script "faketime" in /usr/local/bin, both of which most likely will require root privileges. However, from a technical point of view, there is no necessity for a system-wide installation, so you can use libfaketime also on machines where you do not have root privileges. You may want to adjust the PREFIX variable in the Makefiles accordingly.
By default, the Makefile compiles/links libfaketime for your default system architecture. If you need to build, e.g., 32-bit files on a 64-bit platform, please see the notes about CFLAGS and LDFLAGS in src/Makefile.
Since version 0.6, system calls to file timestamps are also intercepted, thanks to a contribution by Philipp Hachtmann. This is especially useful in combination with relative time offsets as explained in section 4d) below, if a program writes and reads files whose timestamps also shall be faked. If you do not need this feature or if it confuses the application you want to use FTPL with, define the environment variable NO_FAKE_STAT, and the intercepted stat calls will be passed through unaltered.
On macOS, it is necessary to compile differently, due to the different behavior dyld has. Use the Makefile.OSX file provided to compile libfaketime.1.dylib. Additionally, instead of using LD_PRELOAD, the variable DYLD_INSERT_LIBRARIES should be set to the path to libfaketime.1.dylib, and the variable DYLD_FORCE_FLAT_NAMESPACE should be set (to anything). macOS users should read README.OSX for additional details.
The standard test command builds the platform-specific library and runs the complete functional test suite:
make testOn macOS, the top-level Makefile selects the Darwin makefiles automatically. Linux builds can be tested in disposable Docker containers without writing build artifacts into the checkout. The baseline runner accepts a list of locally available images:
test/docker_baseline.sh gcc:13-bookworm debian:13 alpine:3.20The CI baseline also exercises quay.io/centos/centos:stream9 and
opensuse/tumbleweed:latest on x86_64 runners. These images may require
DOCKER_PLATFORM=linux/amd64 when tested from an ARM64 macOS host.
On an ARM64 macOS host, request the target platform explicitly. The dedicated wrapper also checks each image architecture before starting a container:
DOCKER_PLATFORM=linux/arm64 test/docker_arm64.sh \
gcc:13-bookworm debian:13 alpine:3.20An image tagged for another architecture must be replaced with an ARM64 image; Docker emulation cannot make an image manifest that does not contain ARM64 usable by this runner. Arch Linux can be added once a matching ARM64 image is available:
DOCKER_PLATFORM=linux/arm64 test/docker_arm64.sh archlinux:base-develFor memory-safety checks on Linux, the sanitizer runner builds with AddressSanitizer and UndefinedBehaviorSanitizer and places the sanitizer runtime before libfaketime in LD_PRELOAD:
DOCKER_PLATFORM=linux/arm64 test/docker_sanitizers.sh gcc:13-bookwormThe sanitizer run skips only helper-process checks whose preload arrangements are incompatible with sanitizer instrumentation. Linux-only features are reported as skips on macOS; this keeps the common clock, configuration, process, and lifecycle coverage comparable on both platforms.
Using libfaketime on a program of your choice consists of two steps:
As an example, we want the "date" command to report our faked time. To do so, we could use the following command line on Linux:
user@host> date Tue Nov 23 12:01:05 CEST 2016
user@host> LD_PRELOAD=/usr/local/lib/libfaketime.so.1 FAKETIME="-15d" date Mon Nov 8 12:01:12 CEST 2016
user@host> LD_PRELOAD=/usr/local/lib/libfa
No open issues yet, or sync has not completed.