Counter-Example Guided Fence Insertion under TSO

Parosh Aziz Abdulla, Mohamed Faouzi Atig, Yu-Fang Chen, Carl Leonardsson, Ahmed RezineTACAS 2012

Introduction

In concurrent programming, developers typically reason about code execution using Sequential Consistency (SC)—the intuitive model where operations occur one after another in the order they appear in the source code. However, modern hardware architectures, such as x86 and SPARC, utilize the Total Store Ordering (TSO) memory model to optimize performance. TSO allows a processor to buffer write operations and proceed to subsequent reads, creating a "write-to-read" relaxation. While beneficial for speed, this behavior can break fundamental synchronization protocols like Dekker’s or Peterson’s algorithms.

To restore correctness, programmers must insert memory fences (barriers) that force the CPU to flush its buffers. The challenge lies in the trade-off: too few fences lead to subtle concurrency bugs, while too many fences negate the performance benefits of the TSO model. This paper presents the first sound and complete method for automatically inserting the minimal number of memory fences required to ensure a concurrent program remains correct under TSO.

Technical Approach

The authors' methodology is built upon three technical pillars designed to transform a complex, infinite-state verification problem into a manageable one.

1. Single-Buffer (SB) Semantics

Standard TSO models typically employ one unbounded FIFO buffer per process, which is notoriously difficult to analyze. The authors introduce a novel, equivalent model called Single-Buffer (SB) semantics. In this model, all processes share a single global buffer, and each process maintains a "pointer" to its specific view of memory. The authors prove that reachability (the ability to reach a "bad" or unsafe state) in the SB model is identical to reachability in the standard TSO model. This transformation is crucial for the symbolic analysis that follows.

2. Symbolic Backward Reachability

Because buffers can theoretically grow to an infinite size, the state space of these programs is infinite. To address this, the authors utilize SB-automata—a specialized form of finite-state automata—to represent infinite sets of configurations symbolically. By demonstrating that the SB model is "monotonic" (meaning that adding elements to the buffer does not transition a safe state into an unsafe one), they leverage the theory of well-quasi-ordered systems. This ensures that the backward search for error states is guaranteed to terminate.

3. Counter-Example Guided Fence Insertion

The synthesis of fence locations follows a refinement loop:

  1. Initial Check: The algorithm begins with a version of the program containing no fences.
  2. Verification: The symbolic reachability engine searches for a counter-example (a trace leading to a bug).
  3. Analysis: If a bug is found, the tool identifies the specific read-after-write dependency that caused the violation.
  4. Refinement: The tool suggests a set of potential fence locations to block that specific trace. This process iterates until no further counter-examples exist, ensuring the final set of fences is both sufficient and minimal.

Experimental Evaluation

The authors evaluated their tool on a suite of 17 classic concurrent benchmarks, including mutual exclusion protocols (Dekker, Peterson, Lamport’s Bakery) and synchronization primitives (CLH Queue Lock, Sense Reversing Barriers).

Key Results

  • Optimality: The tool successfully identified all minimal fence sets for the benchmarks. For instance, in Lamport’s Bakery algorithm, the tool identified four distinct minimal sets of fences that guarantee correctness.
  • Efficiency: Verification was remarkably fast; most benchmarks were processed in under a second. Even complex Producer-Consumer variants were resolved in less than 10 minutes on standard consumer hardware.
  • Robustness: Unlike previous tools that required programs to be "data-race-free" (a circular requirement for many low-level primitives), this method handles intentional data races natively. It also successfully verified the "Increasing Sequence" example, a known edge case where abstraction-based tools often fail due to the importance of message ordering in the buffer.

Conclusion and Future Work

This research provides a rigorous framework for bridging the gap between high-level programming assumptions and low-level hardware reality. By introducing Single-Buffer semantics and a monotonic transition relation, the authors moved fence insertion from a manual, error-prone task to an automated, provably optimal process.

While currently limited to finite-state programs with a fixed number of processes, the authors suggest that this framework could be extended to even weaker memory models, such as Partial Store Ordering (PSO), which allows for write-to-write reordering. Future iterations may also incorporate predicate abstraction to handle programs with more complex, unbounded data structures.

Summary updated

BibTeX
@inproceedings{abdulla2012counter,
  author = {Parosh Aziz Abdulla and Mohamed Faouzi Atig and Yu-Fang Chen and Carl Leonardsson and Ahmed Rezine},
  title = {{Counter-Example Guided Fence Insertion under TSO}},
  booktitle = {TACAS},
  year = {2012},
  doi = {10.1007/978-3-642-28756-5\_15},
}
Figure