chaos-clock-skew

Test and verification. LD_PRELOAD library that shifts the wall-clock time a process reads, for chaos tests of clock-related behaviour in DDS.

On Linux, chaos-clock-skew builds a shared library (crate-type = ["cdylib", "rlib"], file libchaos_clock_skew.so) that exports clock_gettime and gettimeofday. Both call the libc implementation, found with dlsym(RTLD_NEXT, …), and then add an offset. The offset comes from two environment variables that are read on every call:

  • CHAOS_CLOCK_SKEW_NS: fixed offset in nanoseconds, signed (positive: the clock runs ahead).
  • CHAOS_CLOCK_DRIFT_PPM: rate error in parts per million, signed (100: the clock gains 100 µs per second; -100: it loses 100 µs per second).

Behaviour as implemented:

  • clock_gettime changes only CLOCK_REALTIME; CLOCK_MONOTONIC and all other clocks are passed through unchanged.
  • The offset is CHAOS_CLOCK_SKEW_NS plus a drift term elapsed_ns * ppm / 1_000_000. elapsed_ns is the time since the first intercepted call in the process, measured on the real CLOCK_MONOTONIC.
  • gettimeofday applies the same offset, truncated to microseconds.
  • Calls that do not go through libc (a raw syscall, statically linked binaries) are not intercepted. Rust SystemTime::now() goes through libc.
  • If dlsym does not find the real function, the library panics on the first call.

On other operating systems the crate builds a stub without exported symbols; its only function unsupported() returns "chaos-clock-skew is Linux-only".

Usage from the crate docs:

cargo build -p chaos-clock-skew --release
LD_PRELOAD=$(pwd)/target/release/libchaos_clock_skew.so \
  CHAOS_CLOCK_SKEW_NS=100000000 \
  CHAOS_CLOCK_DRIFT_PPM=100 \
  ./my-process

Consumers

  • No crate in the workspace declares a dependency on it. It is loaded into a process at run time via LD_PRELOAD. The CI job chaos-soak runs with CHAOS_CLOCK_SKEW_NS=50000000.

Status

  • std. Contains unsafe code (FFI: exported functions with #[unsafe(no_mangle)], dlsym, pointer casts); no forbid(unsafe_code). Safety classification in the crate docs: STANDARD (FFI boundary, based on dlsym).
  • No dependencies, no cargo features, no binary.
  • Test functions in the source: 10 in the Linux module (offset arithmetic, drift formula, and calls through the exported functions against the real libc clock), 1 in the stub for other operating systems.

chaos-clock-skew

Test und Verifikation. LD_PRELOAD-Bibliothek, die die Uhrzeit verschiebt, die ein Prozess liest, für Chaos-Tests von uhrzeitabhängigem Verhalten in DDS.

Unter Linux baut chaos-clock-skew eine gemeinsam genutzte Bibliothek (crate-type = ["cdylib", "rlib"], Datei libchaos_clock_skew.so), die clock_gettime und gettimeofday exportiert. Beide rufen die libc-Implementierung auf, die über dlsym(RTLD_NEXT, …) gefunden wird, und addieren danach einen Versatz. Der Versatz kommt aus zwei Umgebungsvariablen, die bei jedem Aufruf gelesen werden:

  • CHAOS_CLOCK_SKEW_NS: fester Versatz in Nanosekunden, mit Vorzeichen (positiv: die Uhr geht vor).
  • CHAOS_CLOCK_DRIFT_PPM: Gangfehler in Millionsteln, mit Vorzeichen (100: die Uhr gewinnt 100 µs je Sekunde; -100: sie verliert 100 µs je Sekunde).

Verhalten laut Implementierung:

  • clock_gettime verändert nur CLOCK_REALTIME; CLOCK_MONOTONIC und alle anderen Uhren werden unverändert durchgereicht.
  • Der Versatz ist CHAOS_CLOCK_SKEW_NS plus ein Driftanteil elapsed_ns * ppm / 1_000_000. elapsed_ns ist die Zeit seit dem ersten abgefangenen Aufruf im Prozess, gemessen an der echten CLOCK_MONOTONIC.
  • gettimeofday wendet denselben Versatz an, auf Mikrosekunden gekürzt.
  • Aufrufe, die nicht über die libc laufen (ein direkter syscall, statisch gelinkte Programme), werden nicht abgefangen. Rust SystemTime::now() läuft über die libc.
  • Findet dlsym die echte Funktion nicht, löst die Bibliothek beim ersten Aufruf eine Panic aus.

Auf anderen Betriebssystemen baut die Crate einen Platzhalter ohne exportierte Symbole; seine einzige Funktion unsupported() liefert "chaos-clock-skew is Linux-only".

Verwendung laut Crate-Dokumentation:

cargo build -p chaos-clock-skew --release
LD_PRELOAD=$(pwd)/target/release/libchaos_clock_skew.so \
  CHAOS_CLOCK_SKEW_NS=100000000 \
  CHAOS_CLOCK_DRIFT_PPM=100 \
  ./my-process

Konsumenten

  • Keine Crate im Workspace deklariert eine Abhängigkeit darauf. Die Bibliothek wird zur Laufzeit über LD_PRELOAD in einen Prozess geladen. Der CI-Job chaos-soak läuft mit CHAOS_CLOCK_SKEW_NS=50000000.

Status

  • std. Enthält unsafe-Code (FFI: exportierte Funktionen mit #[unsafe(no_mangle)], dlsym, Zeiger-Umwandlungen); kein forbid(unsafe_code). Einstufung in der Crate-Dokumentation (safety classification): STANDARD (FFI-Grenze, auf Basis von dlsym).
  • Keine Abhängigkeiten, keine Cargo-Features, kein Binary.
  • Testfunktionen im Quelltext: 10 im Linux-Modul (Versatzrechnung, Driftformel und Aufrufe über die exportierten Funktionen gegen die echte libc-Uhr), 1 im Platzhalter für andere Betriebssysteme.