Formalizing and Checking Thread Refinement for Data-Race-Free Execution Models (Extended Version)

Daniel Poetzl, Daniel Kroening

Introduction

In the realm of concurrent programming, compiler optimizations present a unique challenge. When a compiler optimizes a thread, it must ensure that the resulting code is a valid "refinement" of the original. In a concurrent context, this means the optimized thread must not introduce new, undefined behaviors—such as data races—when executed alongside other threads. Traditionally, verifying this refinement has relied on event-based models that track every individual memory access. However, these methods are often computationally prohibitive and overly restrictive.

This paper introduces a novel, state-based theory for verifying thread refinement under the Sequential Consistency for Data-Race-Free (SC-for-DRF) memory model. By shifting the focus from individual event sequences to thread states at synchronization points, the authors provide a framework that is both more permissive of valid optimizations and significantly faster to verify.

The Limitations of Event-Based Verification

Historically, refinement has been checked by analyzing execution traces—sequences of read and write events—and applying a set of valid syntactic transformations. For example, a tool might check if a trace can be transformed by reordering two non-conflicting reads. This approach suffers from two primary drawbacks:

  1. Computational Complexity: Searching for a valid sequence of transformations to prove that an optimized trace matches an original trace is a search-heavy problem that scales poorly.
  2. Over-Restrictedness: Event-based theories often fail to validate safe optimizations that move memory accesses across synchronization boundaries. While "roach motel" optimizations (moving accesses into a critical section) are sometimes supported, "inverse roach motel" optimizations (moving accesses out of a critical section) are typically rejected, even when they do not introduce data races.

A State-Based Theory of Refinement

The authors propose a paradigm shift: instead of tracking every read and write, refinement can be verified by comparing the state of the thread at specific checkpoints. Under the SC-for-DRF model, the most critical checkpoints are synchronization operations, specifically lock and unlock primitives.

In this state-based approach, a thread is viewed as executing uninterrupted between synchronization points. To prove that an optimized thread refines an original thread, the authors define a match predicate based on three conditions:

  • State at Locks: Both the original and optimized traces must observe the same values from shared memory when acquiring a lock.
  • State at Unlocks: Both traces must write the same values to shared memory before releasing a lock.
  • Race Constraints: The set of memory locations accessed by the optimized thread must be a subset of those accessed by the original thread. This ensures the compiler has not introduced new memory accesses that could trigger a data race in a concurrent environment.

This abstraction transforms the verification process from a complex combinatorial search into a linear-time ($O(N)$) set-matching problem.

Implementation and Evaluation

To test this theory, the authors developed tracecheck, a tool designed to automate refinement checking. They evaluated the tool using a massive dataset of 40,000 random C threads generated via csmith-sync, a modified version of the csmith tool that incorporates pthread_mutex_lock operations.

Execution traces were collected by compiling these programs at different optimization levels (-O0 and -O3) and capturing the memory events using pin-interceptor, a binary instrumentation tool. This allowed the researchers to compare the performance and accuracy of tracecheck against the state-of-the-art event-based checker, cmmtest-check.

Key Results

The experimental results demonstrate a significant leap in performance and flexibility:

  • Massive Speedup: tracecheck performed the refinement checks an average of 210x faster than cmmtest-check.
  • Linear Scalability: Unlike event-based tools that struggle with long traces, tracecheck scales linearly with the number of events, handling traces of up to 4,000 events with ease.
  • Robustness to Lock Density: Event-based checkers often rely on frequent locks to act as "barriers" to limit their search space; when locks are sparse, their performance degrades. In contrast, tracecheck maintained consistent performance regardless of lock density.
  • Support for Complex Reorderings: The state-based model successfully validated both "roach motel" and "inverse roach motel" optimizations, provided they remained data-race-free. This represents a significant improvement in the breadth of optimizations that can be formally verified.

Conclusion

By moving from event-based sequences to state-based synchronization checkpoints, the authors have provided a more efficient and flexible way to verify compiler optimizations for concurrent code. While the current theory is tailored to the SC-for-DRF model and lock-based synchronization, it establishes a powerful foundation for future work. Extending this framework to handle relaxed hardware memory models (such as TSO or ARM) and non-lock primitives (like C11 atomics) remains a promising avenue for ensuring the correctness of high-performance concurrent systems.

Summary updated

BibTeX
@inproceedings{poetzl2016formalizing,
  author = {Daniel Poetzl and Daniel Kroening},
  title = {{Formalizing and Checking Thread Refinement for Data-Race-Free Execution Models (Extended Version)}},
  booktitle = {TACAS},
  year = {2016},
  doi = {10.1007/978-3-662-49674-9\_30},
}
Figure