Aarno Labs Logo

PTSan: Memory Safety for Legacy C/C++

Memory safety protections for your legacy C/C++ code with only a recompilation. PTSan is an open-source LLVM sanitizer pass designed for low overhead and high compatibility with existing applications.

Overview

Organizations depend on mature C/C++ systems, yet a single memory error can expose data or give an attacker control. Rewriting these systems is still extremely costly, and patching cannot keep pace with every vulnerability as AI accelerates exploitation. PTSan adds protection through recompilation, helping secure existing code before individual bugs are found and fixed.

Earlier research systems have demonstrated compiler-enforced memory safety, but high overhead and compatibility restrictions have limited practical use. PTSan brings together robust protection, compatibility with complex applications, and low overhead in an LLVM sanitizer pass. This combination depend on analyses and optimizations we developed to eliminate unnecessary checks and repeated work while preserving protection. Fully open source and ready to use, PTSan lets you retain your existing code, with Aarno Labs available to support integration and further optimization for your workload. The design, security analysis, and evaluation are described in our PTSan paper.

C/C++ source and libraries pass through PTSan LLVM to a protected production binary. Optional CodeHawk analysis reduces overhead; runtime checks block memory-corruption attacks.
Recompile your application and its libraries with PTSan LLVM. The protected binary carries memory-safety checks into production, blocking invalid accesses before they can corrupt other allocations. Additional analysis can further reduce overhead.

Memory safety with a recompilation

Add protection to mature C/C++ software through your build. Keep your existing source code and avoid the cost and disruption of a rewrite.

Recover from memory violations

In the context of our DRIFT system, recovery mechanisms connect PTSan to existing application error handling, allowing bad inputs to be rejected without a separate patch for every vulnerability.

Compatibility and performance

Keep familiar LLVM build workflows and existing C/C++ applications. Choose the lower-overhead configuration that fits your workload, with targeted optimization available for demanding deployments.

Technical Design

PTSan gives each pointer authority to access a particular allocation. A tag carried in the pointer identifies that allocation, and the runtime records its bounds and lifetime. Before a memory operation, PTSan checks that the entire access stays within the object the pointer is allowed to use. An attacker cannot evade this check simply by redirecting the access into another live object. Freeing an object invalidates its metadata so stale pointers fail the check until the identifier is reused. The compiler inserts the protection as ordinary LLVM operations, allowing its analyses to combine, move, or remove checks when it can establish that the accesses are safe.

Pointer representations for different workloads

PTSan offers multiple pointer representations optimized for different workloads. Both keep pointers at their normal 64-bit size, but balance runtime cost against the number of allocations an application can keep live at once. Choose the representation that fits your application's memory use and performance requirements.

ID16: lower runtime cost
16-bit ID48-bit native address

ID16 is normally the lower-overhead choice when the application's peak live allocations fit within roughly 65,000 IDs. Keeping the native address in the pointer makes memory accesses cheaper.

ID32: greater allocation capacity
32-bit ID32-bit encoded position

ID32 makes billions of IDs available for applications with much larger numbers of live allocations. Its compact address encoding requires additional work on memory accesses, trading some runtime cost for greater capacity.

Both representations recycle identifiers in randomized order, which makes it hard for an attacker to steer a freed object's identifier onto a chosen new object. ID32 adds a second layer: each allocation receives a fresh random origin for its encoded positions, so a stale pointer must also match the new object's origin before it can pass a check.

Compiler analyses reduce repeated work

PTSan includes aggressive static analysis that seeks to remove unnecessary tag and check operations to reduce overhead. PTSan hoists ID extraction and bounds loads where lifetime rules permit, merges checks for overlapping or contained access ranges, and recovers loop-wide ranges before unrolling and vectorization obscure them. Moving pointer-tag stripping out of repeated accesses also reduces address calculations and register pressure. These transformations preserve both bounds and pointer authority.

See the PTSan security analysis for the full security model, assumptions, and protection guarantees.

Protection against AI-assisted Attacks

We gave an AI agent known vulnerabilities in Redis, rsync, and ImageMagick and asked it to exploit each application, first without PTSan and then with it. The agent was OpenAI's GPT-5.6 Sol (gpt-5.6-sol), accessed through OpenAI's Daybreak Blue program and run in the Codex agent. It had full knowledge of PTSan and the protected application's binary.

The goal started as remote code execution on every target. The agent achieved it quickly against unprotected Redis. For rsync and ImageMagick, remote code execution proved too hard to reach, so we lowered the bar to the step such an exploit depends on: an out-of-bounds write that corrupts another allocation. With PTSan enabled on those two applications, the goal was simply to get that overflow past PTSan. The agent quickly reached its goal on every unprotected application and reached none of them with PTSan enabled.

Each PTSan run continued until the agent exhausted its allotted budget. The protection did not depend on hiding PTSan's design or keeping the application binary secret. PTSan stopped the memory operations the attacks needed, giving defenders a practical way to raise the cost of exploiting legacy code.

Leading Results in DARPA Evaluations

PTSan was developed under DARPA's E-BOSS program, as part of Aarno Labs' DRIFT project. E-BOSS challenged teams to detect and remediate vulnerabilities in deployed software, focusing on the build system. Independent evaluators tested whether defenses stopped attacks while preserving normal application behavior. PTSan helped our team deliver leading results.

100% Vulnerabilities remediated

PTSan captured and remediated all evaluated vulnerabilities in E-BOSS Evaluation-2 in the shortest amount of time.

#1 Most vulnerabilities remediated

At the April 2026 E-BOSS hackathon, Aarno's PTSan-based automated workflow remediated more vulnerabilities than any other participating team.

DRIFT Demo: Block Attacks and Keep Running

See PTSan in action within DRIFT, Aarno Labs' integrated workflow for assessing and responding to software vulnerabilities. The demonstration follows a vulnerability from disclosure through reachability analysis, attack validation, and automated recovery. Watch PTSan block an attack, then see DRIFT connect that protection to the application's existing error handling. The protected application rejects the malicious input, keeps running, and successfully processes valid requests. These recovery hooks avoid the need to develop a separate fix for each underlying vulnerability.

Watch the DRIFT demonstration on YouTube.

Performance and Compatibility

PTSan delivers the lowest average overhead when compared to published memory-safety sanitizers with similarly strong safety guarantees. ID16 generally delivers lower overhead when the application's live allocations fit within its roughly 65,000-ID budget. ID32 handles much larger workloads, trading additional pointer-decoding work for billions of available IDs.

Added runtime relative to uninstrumented musl
Benchmark suiteStrict ID16Strict ID32
LLVM MultiSource26.63%127 timed workloads31.13%144 timed workloads
SPEC CPU201771.31%9 timed workloads80.18%14 timed workloads
Fleetbench21.61%8 timed workloads58.38%17 timed workloads

Geometric-mean runtime overhead. Each mode covers a different set of completed workloads.

Broad C/C++ compatibility

PTSan supports the vast majority of C/C++ semantics and has been demonstrated on large, real-world workloads chosen by independent evaluation teams. ID32 has handled 22.3 million live allocations without exhausting its ID budget.

A modest memory footprint

ID32's median added peak physical memory was 4.66 MiB on SPEC, 1.53 MiB on MultiSource, and 11.59 MiB on Fleetbench. Geometric-mean peak-memory ratios were 1.13×, 2.05×, and 1.18× native, respectively.

Cut overhead further with workload-specific optimization.

PTSan's LLVM analyses automatically reduce redundant checks and pointer conversions. Aarno Labs can go further with customized analysis of the code that dominates your application's runtime. For loops that account for much of the overhead, we use CodeHawk to verify that a set of entry conditions implies memory safety for the accesses whose checks we remove. Runtime guards test those conditions before entering the specialized loop. When they hold, the loop runs without those repeated checks; otherwise, the fully checked version runs. This connects the performance improvement to a verified safety argument. For example, on SPEC's 544.nab_r molecular simulation benchmark, this approach cut PTSan's overhead from about 80% to 18%, removing 77% of the added runtime.

Deployment

PTSan fits into your existing build process. Deployment centers on rebuilding your code and libraries, selecting the right pointer representation, and deciding how the application should respond to a violation.

  1. Choose ID16 or ID32. ID16 normally offers lower overhead when peak live allocations fit within roughly 65,000 IDs. Use ID32 for workloads that need greater allocation capacity.
  2. Rebuild the application and all its libraries with PTSan's LLVM toolchain. Use a consistent PTSan configuration for your source, third-party dependencies, and C/C++ runtime libraries, then validate the rebuilt system with representative inputs.
  3. Set a recovery policy. Decide whether a violation should stop or restart the service, or connect PTSan to existing error handling so the application can reject a bad input and continue.
  4. Optimize the workflows that matter most. Measure performance on your real workloads. Aarno Labs can use profiling, CodeHawk analysis, and specialized compiler optimizations to reduce overhead in the code that dominates runtime.

PTSan is fully open source. The build and dependency guide covers the toolchain and library setup; the repository includes the license inventory.

Memory Safety Modernization Services

Aarno Labs helps teams modernize the memory safety of existing systems. We combine PTSan with source and binary analysis, targeted patching, testing, and migration to memory-safe languages. The starting point is your exposure, compatibility requirements, and operating budget.

Integration and qualification

Build a compatible toolchain and dependency set, review native interfaces and allocation behavior, and validate protection against representative workloads.

Workload-specific optimization

Measure hot paths and apply customized analysis, guard specialization, and compiler optimization to reduce PTSan's runtime cost.

Transition support

Design deployment, monitoring, and recovery around your application. Combine runtime protection with a practical plan for longer-term modernization.

Start with an application and a performance budget.

Tell us what the system does, which platforms and libraries it relies on, and what latency or throughput it needs to maintain. We can help determine where PTSan fits and what it will take to deploy it.

Artifacts and Links

Research Funding

  • DRIFT (DARPA E-BOSS): Enhancing SBOMs to solve vulnerability discovery, reachability and remediation.