Cache Coherence (MESI)

Models a 4-core MESI cache coherence protocol with two patterns:

Properties declared: coherence, exclusiveIsolation, liveness.

Run#

Shell
saple analyze examples/cache_coherence.spl

Today's report holds one finding, dataRace (CRITICAL).

Known limitation
In expressions, Cache(i).state is read as Cache.state: the instance argument is dropped (see Expressions). The coherence properties of the model below therefore do not distinguish the caches today. Its schema invariants are not checked at all: saple analyze does not check schema invariants (see Schemas).

Complete model#

examples/cache_coherence.spl, as shipped:

SPL cache_coherence.spl
// MESI Cache Coherence Protocol — Behavioral Equivalence
//
// Models the MESI (Modified/Exclusive/Shared/Invalid) cache coherence
// protocol used in multi-core processors. Demonstrates behavioral
// equivalence checking: the protocol implementation should be
// equivalent to a sequential memory model.
//
// Key SAPLE features: parallel composition of cache agents,
// environment invariants for coherence, behavioral equations
// with guards, and equivalence properties.
//
// What the analysis should find: the dataRace pattern (CRITICAL) matches;
// staleRead does not, and no property is reported violated.

// State of a cache line (MESI) and the operation on the shared bus.
type CacheState = { MODIFIED, EXCLUSIVE, SHARED, INVALID };
type BusOp = { READ, READX, UPGRADE, FLUSH };

// ─── Actions ───
// Decomposed into fine-grained atomic transitions.

// The core reads a valid line: no state change.
transition cpuReadHit(core : Int) {
    guard:  Cache(core).state != INVALID ;
    effect: skip ;
}

// The core reads an invalid line: it is loaded as Shared.
transition cpuReadMiss(core : Int) {
    guard:  Cache(core).state == INVALID ;
    effect: Cache(core).state := SHARED ;
}

// Broadcast a read request on the bus.
transition issueBusRead(addr : Int) {
    guard:  true ;
    effect: busOp := READ ;
}

// The core writes an invalid line: it becomes Modified.
transition cpuWriteInvalid(core : Int, val : Int) {
    guard:  Cache(core).state == INVALID ;
    effect: Cache(core).state := MODIFIED ;
}

// The core writes a Shared line: it becomes Modified.
transition cpuWriteShared(core : Int, val : Int) {
    guard:  Cache(core).state == SHARED ;
    effect: Cache(core).state := MODIFIED ;
}

// The core writes an Exclusive line: it becomes Modified without bus traffic.
transition cpuWriteExclusive(core : Int) {
    guard:  Cache(core).state == EXCLUSIVE ;
    effect: Cache(core).state := MODIFIED ;
}

// Broadcast an upgrade (invalidate the other copies).
transition issueBusUpgrade() {
    guard:  true ;
    effect: busOp := UPGRADE ;
}

// Broadcast a read-for-ownership.
transition issueBusReadX() {
    guard:  true ;
    effect: busOp := READX ;
}

// Snooping another core's write: drop a Shared copy.
transition snoopInvalidate(core : Int) {
    guard:  Cache(core).state == SHARED ;
    effect: Cache(core).state := INVALID ;
}

// Snooping another core's read: write back a Modified line and share it.
transition snoopFlush(core : Int) {
    guard:  Cache(core).state == MODIFIED ;
    effect: Cache(core).state := SHARED ;
}

// Snooping another core's read: an Exclusive line becomes Shared.
transition snoopShareExclusive(core : Int) {
    guard:  Cache(core).state == EXCLUSIVE ;
    effect: Cache(core).state := SHARED ;
}

// Evict a clean (Shared or Exclusive) line.
transition evictClean(core : Int) {
    guard:  Cache(core).state == SHARED || Cache(core).state == EXCLUSIVE ;
    effect: Cache(core).state := INVALID ;
}

// Evict a Modified line (it must be written back).
transition evictDirty(core : Int) {
    guard:  Cache(core).state == MODIFIED ;
    effect: Cache(core).state := INVALID ;
}

// Broadcast a write-back.
transition flushBus() {
    guard:  true ;
    effect: busOp := FLUSH ;
}

// ─── Agents ───

// One core's cache controller.
agent Cache(id : Int) {
    attributes {
        state : CacheState = INVALID ;   // MESI state of the line
        data : Int = 0 ;                 // cached value
        addr : Int = 0 ;                 // cached address
    }
    process {
        // Each cache controller cycles through operations
        Idle    = [state == INVALID] Miss + [state != INVALID] Hit ;
        // Miss: read or write an invalid line, then answer snoops.
        Miss    = cpuReadMiss(id) . issueBusRead(addr) . SnoopReply
                + cpuWriteInvalid(id, data) . issueBusReadX() . SnoopReply ;
        // Hit: read, write, or evict a valid line.
        Hit     = cpuReadHit(id) . Idle
                + cpuWriteShared(id, data) . issueBusUpgrade() . Idle
                + cpuWriteExclusive(id) . Idle
                + evictClean(id) . Idle
                + evictDirty(id) . flushBus() . Idle ;
        // SnoopReply: react to the other cores' bus traffic, or not at all.
        SnoopReply = snoopInvalidate(id) . Idle
                   + snoopFlush(id) . Idle
                   + snoopShareExclusive(id) . Idle
                   + Idle ;
    }
}

// Reference model: sequential memory (for equivalence checking)
agent SequentialMemory() {
    attributes {
        lastWriter : Int = 0 ;
        lastValue : Int = 0 ;
    }
    process {
        // Reads and writes in sequence, one at a time.
        Ready = cpuReadHit(core) . Ready
              + cpuWriteShared(core, val) . Ready
              + done ;
    }
}

// ─── Environment ───

// Shared state: number of cores and the current bus operation.
schema CacheEnv {
    attributes {
        numCores : Int = 4 ;
        busOp : BusOp = FLUSH ;
        busAddr : Int = 0 ;
    }
    invariants {
        // MESI invariant: at most one cache in Modified state
        forall (i : Int) in [0..numCores) .
            forall (j : Int) in [0..numCores) .
                (i != j) =>
                    !(Cache(i).state == MODIFIED && Cache(j).state == MODIFIED) ;

        // If any cache is Exclusive, no other cache has it Shared or Modified
        forall (i : Int) in [0..numCores) .
            Cache(i).state == EXCLUSIVE =>
                forall (j : Int) in [0..numCores) .
                    (i != j) => Cache(j).state == INVALID ;
    }
}

// ─── Vulnerability Patterns ───

// Data race: two cores write without proper invalidation
pattern dataRace {
    signature {
        cpuWriteShared(core1, val1) .
        cpuWriteShared(core2, val2) . done
    }
    constraints { core1 != core2 }
    severity: CRITICAL ;
}

// Stale read: reading from a cache line modified elsewhere
pattern staleRead {
    signature {
        cpuWriteInvalid(writer, val) .
        cpuReadHit(reader) . done
    }
    constraints { writer != reader }
    severity: HIGH ;
}

// ─── System ───

// Four caches, all starting Invalid.
system MESIProtocol {
    schema: CacheEnv ;
    agents {
        Cache(i) * 4 ;
    }
    init {
        numCores == 4 &&
        forall (i : Int) in [0..numCores) .
            Cache(i).state == INVALID ;
    }
    properties {
        // Coherence: no two caches hold Modified simultaneously
        coherence : forall (i : Int) in [0..numCores) .
            forall (j : Int) in [0..numCores) .
                (i != j) =>
                    !(Cache(i).state == MODIFIED &&
                      Cache(j).state == MODIFIED) ;

        // Exclusive isolation: Exclusive implies all others Invalid
        exclusiveIsolation : forall (i : Int) in [0..numCores) .
            Cache(i).state == EXCLUSIVE =>
                forall (j : Int) in [0..numCores) .
                    (i != j) => Cache(j).state == INVALID ;

        // Liveness: Invalid cache can transition to valid state
        liveness : forall (i : Int) in [0..numCores) .
            Cache(i).state == INVALID =>
                Cache(i).state == EXCLUSIVE ||
                Cache(i).state == SHARED ;
    }
}
↑ ↓ to moveEnter to openEsc to close