Skip to content

Runtime Library

Hugo edited this page Feb 26, 2026 · 1 revision

Runtime Library

Instrumented binaries link ct_instrument_runtime (built from src/runtime/). This library implements all __ct_* hooks inserted by LLVM passes.

Module map

File Role
ct_runtime_alloc.cpp allocation tracking, table management, auto-free scan
ct_runtime_bounds.cpp bounds violation checks and reporting
ct_runtime_trace.cpp function entry/exit tracing hooks
ct_runtime_vtable.cpp virtual call and vtable diagnostics
ct_runtime_shadow.cpp shadow metadata tracking
ct_runtime_state.cpp feature bitset + legacy globals
ct_runtime_env.cpp compile-time config globals + env overrides
ct_runtime_logging.cpp logger adapters/helpers
ct_runtime_backtrace.cpp optional backtrace setup

Core exported hooks

Allocation hooks (selection)

  • void* __ct_malloc(size_t, const char*)
  • void* __ct_calloc(size_t, size_t, const char*)
  • void* __ct_realloc(void*, size_t, const char*)
  • void __ct_free(void*)
  • void* __ct_new(size_t, const char*)
  • void __ct_delete(void*)
  • void* __ct_mmap(..., const char*)
  • int __ct_munmap(void*, size_t, const char*)
  • void* __ct_sbrk(size_t, const char*)
  • void* __ct_brk(void*, const char*)

Bounds hook

  • void __ct_check_bounds(const void* base, const void* ptr, size_t access_size, const char* site, int is_write)

Trace hooks

  • void __ct_trace_enter(const char* func)
  • void __ct_trace_exit_void(const char* func)
  • void __ct_trace_exit_i64(const char* func, long long value)
  • void __ct_trace_exit_ptr(const char* func, const void* value)
  • void __ct_trace_exit_f64(const char* func, double value)
  • void __ct_trace_exit_unknown(const char* func)

VTable hooks

  • void __ct_vtable_dump(void* this_ptr, const char* site, const char* static_type)
  • void __ct_vcall_trace(void* this_ptr, void* target, const char* site, const char* static_type)

Runtime feature model

Feature state is stored in a bitset API (ct_is_enabled, ct_set_enabled, ct_get_features). Default enabled bits include trace, alloc, bounds, autofree, and alloc-trace.

At startup, compile-time globals injected by the compiler can disable/enable features:

  • __ct_config_shadow
  • __ct_config_shadow_aggressive
  • __ct_config_bounds_no_abort
  • __ct_config_disable_alloc
  • __ct_config_disable_autofree
  • __ct_config_disable_alloc_trace
  • __ct_config_vtable_diag

Then env vars can override behavior (see below).

Environment overrides

ct_runtime_env.cpp reads:

  • CT_DISABLE_TRACE
  • CT_DISABLE_ALLOC
  • CT_EARLY_TRACE
  • CT_DISABLE_BOUNDS
  • CT_BOUNDS_NO_ABORT
  • CT_SHADOW
  • CT_SHADOW_AGGRESSIVE
  • CT_DISABLE_AUTOFREE
  • CT_DISABLE_ALLOC_TRACE

Auto-free scan env vars are documented in Auto-Free GC.

Notes

  • Allocation/shadow tables use lock-protected open addressing.
  • VTable diagnostics rely on platform symbol/module resolution (dladdr, dyld, phdr fallback).
  • Most diagnostics are emitted through coretrace-logger adapters.

Clone this wiki locally