| readReading page · generated from spec/spectec/prose/07-memory-model.spectec, 08-memory-read.spectec, 09-memory-view.spectec
The memory model (§29)
ECMA-262 §29 formalized in SpecTec: candidate executions (Exec), Agent Events Records, happens-before (HB), sequentially consistent memory order (MemoryOrder), valid executions (ValidWith), and races (§29.1–§29.10), with 19 quoted laws, plus how chooseReadsValid reads a candidate execution from the store and appends a shared read.
Candidate executions, relations and validity (§29.1–§29.10)
From spec/spectec/prose/07-memory-model.spectec
A candidate execution as the memory model sees it (Exec): its Agent Events Records, its Chosen Value Records and its events' fields, each event by identity. Over it, with a reads-bytes-from function and the host's host-synchronizes-with: the relations of §29.6, the properties of §29.7 and races, data races and data race freedom (§29.8-29.10). The errata, each at one reading (none of them switchable): MemoryRangeSameBlock at its proposed text (Rbf.Valid, CoherentReads), RacesNotBoth and InitHappensBeforeOverlap at their literal text (Race, HB.init). ValueOfReadEvent and ComposeWriteEventBytes are a least fixpoint (ValueOf, Compose): a cycle of ReadModifyWriteSharedMemory events reading from each other has no value, so it fails valid chosen reads.
MemoryModel.Kind
07-memory-model.spectec:17A Memory event's kind (§29.1, sec-memory-model-fundamentals).
syntax Kindderiving [DecidableEq, Repr]
MemoryModel.Order
07-memory-model.spectec:27A Shared Data Block event's [[Order]] (§29.1, sec-memory-model-fundamentals).
syntax Orderderiving [DecidableEq, Repr]
MemoryModel.EventsRecord
07-memory-model.spectec:35An Agent Events Record (§29.2, sec-agent-event-records): its [[EventList]] and [[AgentSynchronizesWith]].
syntax EventsRecord (E : Type) recordMemoryModel.Exec
07-memory-model.spectec:42A candidate execution (§29.4, sec-candidate-executions) and the fields of its events (§29.1, sec-memory-model-fundamentals), events E by identity.
syntax Exec (M : Model) (E : Type) recordrecords : List(EventsRecord(E));[[ChosenValues]]: each Chosen Value Record's [[Event]] and [[ChosenValue]] (§29.3, sec-chosen-value-records)
chosen : List(E times List(M.Math));kind : E -> Kind;order : E -> Order;noTear : E -> Bool;block : E -> M.Addr;byteIndex : E -> M.Math;elementSize : E -> M.Math;payload : E -> List(M.Math);[[ModifyOp]] called on a List of byte values and [[Payload]]; none where the call has no result
modify : E -> List(M.Math) -> List(M.Math) -> Option(List(M.Math));MemoryModel.Rbf
07-memory-model.spectec:58A reads-bytes-from function (§29.6.2, sec-reads-bytes-from).
syntax Rbf (E : Type) = E -> List(E)MemoryModel.Rel
07-memory-model.spectec:61A Relation on Memory events (§29.1, sec-memory-model-fundamentals; §29.6, sec-relations-of-candidate-executions).
syntax Rel (E : Type) = E -> E -> PropMemoryModel.Plus
07-memory-model.spectec:64The transitive closure of a Relation (§29.6.4, sec-host-synchronizes-with; §29.6.6, sec-happens-before).
relation Plus {E : Type} (r : Rel(E)) : Rel(E)MemoryModel.StrictPartialOrder
07-memory-model.spectec:71A strict partial order on Memory events (§29.6.4, sec-host-synchronizes-with; §29.7.5, sec-valid-executions): irreflexive and transitive.
Proved for happens-before and memory-order across shared reads in Ecma.MemoryModel.snoc_valid.
laws StrictPartialOrder {E : Type} (r : Rel(E))2 laws
execution admits a happens-before Relation that is a strict partial order.
A strict partial order relates no event to itself.
irreflSpecTec
law irrefl : forall a, not r(a, a);A strict partial order is transitive.
transSpecTec
law trans : forall a b c, r(a, b) -> r(b, c) -> r(a, c);Auxiliary definitions (15): MemoryModel.At, MemoryModel.isSdb, MemoryModel.isRead, MemoryModel.isWrite, …
MemoryModel.At
07-memory-model.spectec:83l[k] for a mathematical value k: the element at the integer index k.
def At {M : Model} (l : List(M.Math)) (k : M.Math) (b : M.Math) : Prop := exists (i : Nat), M.eq(M.ofNat(i), k) = true /\ l[i]? = some(b)MemoryModel.isSdb
07-memory-model.spectec:88A Shared Data Block event: a ReadSharedMemory, WriteSharedMemory or ReadModifyWriteSharedMemory.
def isSdb (e : E) : Prop := x.kind(e) = .read \/ x.kind(e) = .write \/ x.kind(e) = .rmwMemoryModel.isRead
07-memory-model.spectec:91A read event: a ReadSharedMemory or a ReadModifyWriteSharedMemory.
def isRead (e : E) : Prop := x.kind(e) = .read \/ x.kind(e) = .rmwMemoryModel.isWrite
07-memory-model.spectec:94A write event: a WriteSharedMemory or a ReadModifyWriteSharedMemory.
def isWrite (e : E) : Prop := x.kind(e) = .write \/ x.kind(e) = .rmwMemoryModel.inEvents
07-memory-model.spectec:97EventSet(execution) contains e (§29.5.1).
def inEvents (e : E) : Prop := exists r mem x.records, e mem r.eventsMemoryModel.inSdb
07-memory-model.spectec:100SharedDataBlockEventSet(execution) contains e (§29.5.2).
def inSdb (e : E) : Prop := inEvents(x, e) /\ isSdb(x, e)MemoryModel.inHost
07-memory-model.spectec:103HostEventSet(execution) contains e (§29.5.3).
def inHost (e : E) : Prop := inEvents(x, e) /\ not inSdb(x, e)MemoryModel.inRange
07-memory-model.spectec:107e has b in its memory range: an integer from [[ByteIndex]] (inclusive) to [[ByteIndex]] + [[ElementSize]] (exclusive) (§29.1).
def inRange (e : E) (b : M.Math) : Prop := M.isInt(b) = true /\ M.lt(b, x.byteIndex(e)) = false /\ M.lt(b, M.add(x.byteIndex(e), x.elementSize(e))) = trueMemoryModel.rangesEqual
07-memory-model.spectec:110Equal memory ranges: the same [[Block]], [[ByteIndex]] and [[ElementSize]] (§29.1).
def rangesEqual (a b : E) : Prop := x.block(a) = x.block(b) /\ M.eq(x.byteIndex(a), x.byteIndex(b)) = true /\ M.eq(x.elementSize(a), x.elementSize(b)) = trueMemoryModel.rangesOverlapping
07-memory-model.spectec:113Overlapping memory ranges: the same [[Block]], not equal, a common byte (§29.1).
def rangesOverlapping (a b : E) : Prop := x.block(a) = x.block(b) /\ not rangesEqual(x, a, b) /\ exists k, inRange(x, a, k) /\ inRange(x, b, k)MemoryModel.rangesDisjoint
07-memory-model.spectec:116Disjoint memory ranges: another [[Block]], or neither equal nor overlapping (§29.1).
def rangesDisjoint (a b : E) : Prop := x.block(a) <> x.block(b) \/ not rangesEqual(x, a, b) /\ not rangesOverlapping(x, a, b)MemoryModel.agentOrder
07-memory-model.spectec:119a is-agent-order-before b (§29.6.1): before it in some Agent Events Record's [[EventList]].
def agentOrder (a b : E) : Prop := exists r mem x.records, a mem r.events /\ b mem r.events /\ exists (i j : Nat), i < j /\ r.events[i]? = some(a) /\ r.events[j]? = some(b)MemoryModel.Rbf.Valid
07-memory-model.spectec:124rbf is a reads-bytes-from function of x (§29.6.2): each read event's List has its [[ElementSize]] write events, the ith of its [[Block]] (MemoryRangeSameBlock) with [[ByteIndex]] + i in its memory range, and not the read event itself ("readEvent is not in writes").
def Rbf.Valid (rbf : Rbf(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> M.eq(M.ofNat(rbf(r).length), x.elementSize(r)) = true /\ (forall w mem rbf(r), inSdb(x, w) /\ isWrite(x, w)) /\ (forall (i : Nat) w, rbf(r)[i]? = some(w) -> x.block(w) = x.block(r) /\ inRange(x, w, M.add(x.byteIndex(r), M.ofNat(i)))) /\ r notmem rbf(r)MemoryModel.readsFrom
07-memory-model.spectec:127r reads-from w (§29.6.3).
def readsFrom (rbf : Rbf(E)) (r w : E) : Prop := inSdb(x, r) /\ inSdb(x, w) /\ w mem rbf(r)MemoryModel.HostSync
07-memory-model.spectec:131hsw is a host-synchronizes-with of x (§29.6.4): a strict partial order on host-specific events with no cycle in its union with is-agent-order-before.
def HostSync (hsw : Rel(E)) : Prop := StrictPartialOrder(hsw) /\ (forall a b, hsw(a, b) -> inHost(x, a) /\ inHost(x, b)) /\ forall a, not Plus(fun a b => hsw(a, b) \/ agentOrder(x, a, b), a, a)MemoryModel.SyncWith
07-memory-model.spectec:134synchronizes-with (§29.6.5, sec-synchronizes-with): the least Relation closed under its three rules.
relation SyncWith (rbf : Rbf(E)) (hsw : Rel(E)) : Rel(E)rule rf {r w : E} : SyncWith(rbf, hsw, w, r) :- readsFrom(x, rbf, r, w), x.order(r) = .seqCst, x.order(w) = .seqCst, rangesEqual(x, r, w);rule agent {a b : E} (rec : EventsRecord(E)) : SyncWith(rbf, hsw, a, b) :- rec mem x.records, (a, b) mem rec.syncs;rule host {a b : E} : SyncWith(rbf, hsw, a, b) :- hsw(a, b);MemoryModel.HB
07-memory-model.spectec:141happens-before (§29.6.6, sec-happens-before): the least Relation closed under its four rules.
relation HB (rbf : Rbf(E)) (hsw : Rel(E)) : Rel(E)rule agent {a b : E} : HB(rbf, hsw, a, b) :- agentOrder(x, a, b);rule sync {a b : E} : HB(rbf, hsw, a, b) :- SyncWith(x, rbf, hsw, a, b);rule init {a b : E} : HB(rbf, hsw, a, b) :- inSdb(x, a), inSdb(x, b), x.order(a) = .init, rangesOverlapping(x, a, b);rule trans {a b c : E} : HB(rbf, hsw, a, b) :- HB(rbf, hsw, a, c), HB(rbf, hsw, c, b);MemoryModel.Compose
07-memory-model.spectec:150ComposeWriteEventBytes(execution, loc, ws) returns bs (§29.5.4, sec-composewriteeventbytes).
relation Compose (rbf : Rbf(E)) : M.Math -> List(E) -> List(M.Math) -> Proprule nil {loc : M.Math} : Compose(rbf, loc, [], []);rule write {loc byte : M.Math} {w : E} {ws : List(E)} {bs : List(M.Math)} : Compose(rbf, loc, w :: ws, byte :: bs) :- inRange(x, w, loc), x.kind(w) = .write, At(x.payload(w), M.sub(loc, x.byteIndex(w)), byte), Compose(rbf, M.add(loc, M.ofNat(1)), ws, bs);rule rmw {loc byte : M.Math} {w : E} {ws : List(E)} {bs bytes mod : List(M.Math)} : Compose(rbf, loc, w :: ws, byte :: bs) :- inRange(x, w, loc), x.kind(w) = .rmw, ValueOf(rbf, w, bytes), x.modify(w, bytes, x.payload(w)) = some(mod), At(mod, M.sub(loc, x.byteIndex(w)), byte), Compose(rbf, M.add(loc, M.ofNat(1)), ws, bs);MemoryModel.ValueOf
07-memory-model.spectec:157ValueOfReadEvent(execution, r) returns v (§29.5.5, sec-valueofreadevent).
relation ValueOf (rbf : Rbf(E)) : E -> List(M.Math) -> Proprule mk {r : E} {v : List(M.Math)} : ValueOf(rbf, r, v) :- forall w mem rbf(r), isWrite(x, w), M.eq(M.ofNat(rbf(r).length), x.elementSize(r)) = true, Compose(rbf, x.byteIndex(r), rbf(r), v);Auxiliary definitions (3): MemoryModel.ValidChosenReads, MemoryModel.CoherentReads, MemoryModel.TearFreeReads
MemoryModel.ValidChosenReads
07-memory-model.spectec:164x has valid chosen reads (§29.7.1): the algorithm returns true. Each read event has one Chosen Value Record, whose [[ChosenValue]] is the value it reads, byte for byte.
def ValidChosenReads (rbf : Rbf(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> (exists (i : Nat) (cv : List(M.Math)), x.chosen[i]? = some((r, cv)) /\ (forall (j : Nat) cv', x.chosen[j]? = some((r, cv')) -> j = i) /\ exists v, ValueOf(x, rbf, r, v) /\ cv.length = v.length /\ forall (k : Nat) a b, cv[k]? = some(a) -> v[k]? = some(b) -> M.eq(a, b) = true)MemoryModel.CoherentReads
07-memory-model.spectec:169x has coherent reads (§29.7.2): no read event reads a byte from a write it happens-before, or from one that another write of the byte, on its [[Block]] (step 1.c.ii; MemoryRangeSameBlock), happens-between.
def CoherentReads (rbf : Rbf(E)) (hsw : Rel(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> (forall (i : Nat) w, rbf(r)[i]? = some(w) -> not HB(x, rbf, hsw, r, w) /\ not exists v, isWrite(x, v) /\ x.block(v) = x.block(r) /\ inRange(x, v, M.add(x.byteIndex(r), M.ofNat(i))) /\ HB(x, rbf, hsw, w, v) /\ HB(x, rbf, hsw, v, r))MemoryModel.TearFreeReads
07-memory-model.spectec:173x has tear free reads (§29.7.3): an aligned read event that cannot tear reads from at most one write that cannot tear with its memory range (step 1.a.ii.1); its Assert holds.
def TearFreeReads (rbf : Rbf(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> x.noTear(r) = true -> M.isInt(M.div(x.byteIndex(r), x.elementSize(r))) = true /\ forall w, readsFrom(x, rbf, r, w) -> x.noTear(w) = true -> not (rangesEqual(x, r, w) /\ exists v, rangesEqual(x, v, w) /\ x.noTear(v) = true /\ w <> v /\ readsFrom(x, rbf, r, v))MemoryModel.MemoryOrder
07-memory-model.spectec:176mo is an is-memory-order-before of x (§29.7.4, sec-memory-order).
Proved preserved across shared reads as part of Ecma.MemoryModel.snoc_valid.
laws MemoryOrder (rbf : Rbf(E)) (hsw : Rel(E)) (mo : Rel(E))6 laws
For a candidate execution execution, is-memory-order-before is a strict total order of all Memory events in EventSet(execution) that satisfies the following.
mo is a strict partial order (StrictPartialOrder).
orderSpecTec
law order : StrictPartialOrder(mo);Any two distinct events in EventSet(execution) (inEvents) are ordered by mo.
mo relates only events in EventSet(execution) (inEvents).
For events eventA and eventB, eventA is-memory-order-before eventB in execution if eventA happens-before eventB in execution.
mo extends happens-before (HB).
For events readEvent and writeEvent such that readEvent reads-from writeEvent in execution, there is no WriteSharedMemory or ReadModifyWriteSharedMemory event value in SharedDataBlockEventSet(execution) such that value.[[Order]] is seq-cst, writeEvent is-memory-order-before value in execution, value is-memory-order-before readEvent in execution, and any of the following conditions are true.
No seq-cst write with equal memory range comes in mo between a read and the write it reads from under any of the three conditions.
scSpecTec
law sc : forall r w, readsFrom(x, rbf, r, w) -> not exists v, inSdb(x, v) /\ isWrite(x, v) /\ x.order(v) = .seqCst /\ mo(w, v) /\ mo(v, r) /\ (SyncWith(x, rbf, hsw, w, r) /\ rangesEqual(x, v, r) \/ HB(x, rbf, hsw, w, r) /\ HB(x, rbf, hsw, v, r) /\ x.order(w) = .seqCst /\ rangesEqual(x, w, v) \/ HB(x, rbf, hsw, w, r) /\ HB(x, rbf, hsw, w, v) /\ x.order(r) = .seqCst /\ rangesEqual(x, v, r));For each WriteSharedMemory or ReadModifyWriteSharedMemory event writeEvent in SharedDataBlockEventSet(execution), if writeEvent.[[Order]] is seq-cst, then it is not the case that there is an infinite number of ReadSharedMemory or ReadModifyWriteSharedMemory events in SharedDataBlockEventSet(execution) with equal memory range that is memory-order before writeEvent.
Every seq-cst write is after only finitely many reads with equal memory range in mo.
Auxiliary definition: MemoryModel.SeqCstAtomics
MemoryModel.SeqCstAtomics
07-memory-model.spectec:205x has sequentially consistent atomics (§29.7.4, sec-memory-order): it admits an is-memory-order-before.
def SeqCstAtomics (rbf : Rbf(E)) (hsw : Rel(E)) : Prop := exists mo, MemoryOrder(x, rbf, hsw, mo)MemoryModel.ValidWith
07-memory-model.spectec:209x is a valid execution (§29.7.5, sec-valid-executions) with the host's host-synchronizes-with hsw and the reads-bytes-from function rbf.
Proved preserved across shared reads by Ecma.MemoryModel.snoc_valid, which discharges Host.MemoryLaws.chooseReadsValid.
laws ValidWith (rbf : Rbf(E)) (hsw : Rel(E))7 laws
The host provides a host-synchronizes-with Relation for execution.
hsw is a host-synchronizes-with Relation for x (HostSync).
For a candidate execution execution, its reads-bytes-from function is a mathematical function mapping Memory events in SharedDataBlockEventSet(execution) to Lists of events in SharedDataBlockEventSet(execution) that satisfies the following conditions.
rbf is a valid reads-bytes-from function for x (Rbf.Valid).
readsBytesFromSpecTec
law readsBytesFrom : Rbf.Valid(x, rbf);execution admits a happens-before Relation that is a strict partial order.
Happens-before (HB) is a strict partial order (StrictPartialOrder).
happensBeforeSpecTec
law happensBefore : StrictPartialOrder(HB(x, rbf, hsw));execution has valid chosen reads.
x has valid chosen reads (ValidChosenReads).
validChosenReadsSpecTec
law validChosenReads : ValidChosenReads(x, rbf);execution has coherent reads.
x has coherent reads (CoherentReads).
coherentReadsSpecTec
law coherentReads : CoherentReads(x, rbf, hsw);execution has tear free reads.
x has tear free reads (TearFreeReads).
tearFreeReadsSpecTec
law tearFreeReads : TearFreeReads(x, rbf);execution has sequentially consistent atomics.
x has sequentially consistent atomics (SeqCstAtomics).
seqCstAtomicsSpecTec
law seqCstAtomics : SeqCstAtomics(x, rbf, hsw);Auxiliary definitions (4): MemoryModel.Valid, MemoryModel.Race, MemoryModel.DataRace, MemoryModel.DataRaceFree
MemoryModel.Valid
07-memory-model.spectec:242x is a valid execution (§29.7.5) with the host's host-synchronizes-with hsw: it admits a reads-bytes-from function with which it is one.
def Valid (hsw : Rel(E)) : Prop := exists rbf, ValidWith(x, rbf, hsw)MemoryModel.Race
07-memory-model.spectec:246a and b are in a race (§29.8; InRace): distinct, not both happens-before each other (RacesNotBoth, literal), and two writes whose ranges are not disjoint or one reading from the other.
def Race (rbf : Rbf(E)) (hsw : Rel(E)) (a b : E) : Prop := a <> b /\ not (HB(x, rbf, hsw, a, b) /\ HB(x, rbf, hsw, b, a)) /\ (isWrite(x, a) /\ isWrite(x, b) /\ not rangesDisjoint(x, a, b) \/ readsFrom(x, rbf, a, b) \/ readsFrom(x, rbf, b, a))MemoryModel.DataRace
07-memory-model.spectec:249a and b are in a data race (§29.9): in a race, and not both seq-cst or overlapping.
def DataRace (rbf : Rbf(E)) (hsw : Rel(E)) (a b : E) : Prop := Race(x, rbf, hsw, a, b) /\ (x.order(a) <> .seqCst \/ x.order(b) <> .seqCst \/ rangesOverlapping(x, a, b))MemoryModel.DataRaceFree
07-memory-model.spectec:252x is data race free (§29.10): no two of its Shared Data Block events are in a data race.
def DataRaceFree (rbf : Rbf(E)) (hsw : Rel(E)) : Prop := forall a b, inSdb(x, a) -> inSdb(x, b) -> not DataRace(x, rbf, hsw, a, b)Adding a shared read to a candidate execution
From spec/spectec/prose/08-memory-read.spectec
A shared read appends its event to an Agent Events Record's [[EventList]] and its Chosen Value Record to [[ChosenValues]] (GetRawBytesFromSharedBlock, GetModifySetValueInBuffer, AtomicCompareExchangeInSharedBlock, CopyDataBlockBytes): Exec.snoc, the new event none and the others some. Over it: the claim of law L2 (ReadsValid).
MemoryModel.NewEvent
08-memory-read.spectec:16The new event of a shared read (§29.1, sec-memory-model-fundamentals; §25.1.3.15, sec-getrawbytesfromsharedblock; §25.1.3.19, sec-getmodifysetvalueinbuffer): its kind and fields.
syntax NewEvent (M : Model) recordMemoryModel.NewEvent.IsRead
08-memory-read.spectec:30A read event of a shared read (§29.1, sec-memory-model-fundamentals; §29.7.3, sec-tear-free-aligned-reads): a ReadSharedMemory or ReadModifyWriteSharedMemory event, not init, its [[ByteIndex]] a non-negative integer, aligned where it cannot tear.
Proved of each shared-read event in Engine.Proofs.Host.MemoryImpl.
laws NewEvent.IsRead (r : NewEvent(M))4 laws
A read event is either a ReadSharedMemory or a ReadModifyWriteSharedMemory.
The new event's kind is .read or .rmw.
[[Order]] seq-cst or unordered
A read event's [[Order]] is not init.
orderSpecTec
law order : r.order <> .init;[[ByteIndex]] a non-negative integer
The event's [[ByteIndex]] is a non-negative integer.
Assert: The remainder of dividing readEvent.[[ByteIndex]] by readEvent.[[ElementSize]] is 0.
When [[NoTear]] is true, [[ByteIndex]] is an integer multiple of [[ElementSize]].
Auxiliary definitions (5): MemoryModel.EventsRecord.snoc, MemoryModel.Exec.snoc, MemoryModel.Rbf.snoc, MemoryModel.Rel.lift, …
MemoryModel.EventsRecord.snoc
08-memory-read.spectec:50The ith Agent Events Record er, the new event none appended where i is k.
def EventsRecord.snoc (k i : Nat) (er : EventsRecord(E)) : EventsRecord(Option(E)) := ctor(er.events.map(some) ++ (if i = k then [none] else []), er.syncs.map(fun p => (some(p.1), some(p.2))))MemoryModel.Exec.snoc
08-memory-read.spectec:54x with the event r (none) appended to the [[EventList]] of its kth Agent Events Record, and the Chosen Value Record of r and cv to its [[ChosenValues]].
SpecTec, 36 lines
def Exec.snoc (x : Exec(M, E)) (k : Nat) (r : NewEvent(M)) (cv : List(M.Math)) : Exec(M, Option(E)) { records := x.records.mapIdx(EventsRecord.snoc(k)); chosen := x.chosen.map(fun c => (some(c.1), c.2)) ++ [(none, cv)]; kind | some(e) => x.kind(e) | none => r.kind end; order | some(e) => x.order(e) | none => r.order end; noTear | some(e) => x.noTear(e) | none => r.noTear end; block | some(e) => x.block(e) | none => r.block end; byteIndex | some(e) => x.byteIndex(e) | none => r.byteIndex end; elementSize | some(e) => x.elementSize(e) | none => r.elementSize end; payload | some(e) => x.payload(e) | none => r.payload end; modify | some(e) => x.modify(e) | none => r.modify end;}
MemoryModel.Rbf.snoc
08-memory-read.spectec:92rbf, the new event reading from ws.
def Rbf.snoc (rbf : Rbf(E)) (ws : List(E)) : Rbf(Option(E)) | some(e) => rbf(e).map(some) | none => ws.map(some)end
MemoryModel.Rel.lift
08-memory-read.spectec:98A Relation on the events but the new one.
def Rel.lift (r : Rel(E)) : Rel(Option(E)) | some(a), some(b) => r(a, b) | _, _ => Falseend
MemoryModel.ReadsValid
08-memory-read.spectec:106Law L2's claim: with the read r appended to the kth Agent Events Record, reading its bytes from ws, its Chosen Value Record the byte values ComposeWriteEventBytes composes of them, x is a valid execution with rbf and hsw.
def ReadsValid (x : Exec(M, E)) (rbf : Rbf(E)) (hsw : Rel(E)) (k : Nat) (r : NewEvent(M)) (ws : List(E)) : Prop := forall cv, (forall c mem cv, exists n, c = M.ofNat(n)) -> Compose(x, rbf, r.byteIndex, ws, cv) -> ValidWith(x.snoc(k, r, cv), rbf.snoc(ws), Rel.lift(hsw))Reading a candidate execution from the store
From spec/spectec/prose/09-memory-view.spectec
A candidate execution Record of the store, as the memory model sees it: its Agent Events Records and Chosen Value Records, read through its Lists, and each event by its address, with the fields of its Record. An event's kind is its Record's sort; [[ModifyOp]] is called by d, which runs an Abstract Closure. Fields, enums and sorts are known only through a Names.
MemoryModel.View.Names
09-memory-view.spectec:19The name lookups of the view (§29.1, sec-memory-model-fundamentals; §29.2, sec-agent-event-records; §29.3, sec-chosen-value-records; §29.4, sec-candidate-executions).
syntax Names recordAuxiliary definitions (15): MemoryModel.View.toList, MemoryModel.View.cell, MemoryModel.View.list, MemoryModel.View.field, …
MemoryModel.View.toList
09-memory-view.spectec:31A sequence's elements, first to last.
def toList {α : Type} (xs : M.Seq(α)) : List(α) := M.seqFoldl(fun acc x => acc ++ [x], [], xs)MemoryModel.View.cell
09-memory-view.spectec:34The store cell a value refers to.
def cell (s : State(M)) : Val(M) -> Option(Cell(M)) | .ref(a) => M.heapGet(s.heap, a) | _ => noneend
MemoryModel.View.list
09-memory-view.spectec:40The elements of the List a value refers to; none where it refers to no List.
def list (s : State(M)) (v : Val(M)) : List(Val(M)) := match cell(s, v) with | some(.list(xs, _)) => toList(xs) | _ => []end
MemoryModel.View.field
09-memory-view.spectec:47A Record's field by name; absent where there is no such Record or field. As Strategy's fieldOf, and unlike readField, it reads no .env cell's fields.
def field (s : State(M)) (v : Val(M)) (f : String) : Val(M) := match cell(s, v), N.field?(f) with | some(.record(_, fs)), some(i) => M.recGet(fs, i).getD(.absent) | _, _ => .absentend
MemoryModel.View.addr
09-memory-view.spectec:53The address a value refers to.
def addr : Val(M) -> Option(M.Addr) | .ref(a) => some(a) | _ => noneend
MemoryModel.View.math
09-memory-view.spectec:59A mathematical value, 0 where the value is none.
def math : Val(M) -> M.Math | .math(m) => m | _ => M.ofNat(0)end
MemoryModel.View.bytes
09-memory-view.spectec:65The byte values of a List of byte values.
def bytes (s : State(M)) (v : Val(M)) : List(M.Math) := list(s, v).filterMap(fun | .math(m) => some(m) | _ => noneend)
MemoryModel.View.pair
09-memory-view.spectec:71An « a, b » pair of events.
def pair (s : State(M)) (v : Val(M)) : Option(M.Addr times M.Addr) := match list(s, v) with | [.ref(a), .ref(b)] => some((a, b)) | _ => noneend
MemoryModel.View.eventsRecord
09-memory-view.spectec:77An Agent Events Record: its [[EventList]]'s events and [[AgentSynchronizesWith]]'s pairs.
def eventsRecord (s : State(M)) (er : Val(M)) : EventsRecord(M.Addr) := ctor(list(s, field(N, s, er, "EventList")).filterMap(addr), list(s, field(N, s, er, "AgentSynchronizesWith")).filterMap(pair(s)))MemoryModel.View.chosenValue
09-memory-view.spectec:80A Chosen Value Record: its [[Event]] and [[ChosenValue]].
def chosenValue (s : State(M)) (cv : Val(M)) : Option(M.Addr times List(M.Math)) := addr(field(N, s, cv, "Event")).map(fun e => (e, bytes(s, field(N, s, cv, "ChosenValue"))))MemoryModel.View.kind
09-memory-view.spectec:83An event's kind: its Record's sort.
def kind (s : State(M)) (e : M.Addr) : Kind := match M.heapGet(s.heap, e) with | some(.record(k, _)) => if N.sort?("ReadSharedMemory") == some(k) then .read else if N.sort?("WriteSharedMemory") == some(k) then .write else if N.sort?("ReadModifyWriteSharedMemory") == some(k) then .rmw else if N.sort?("Synchronize event") == some(k) then .sync else .host | _ => .hostend
MemoryModel.View.order
09-memory-view.spectec:89An event's [[Order]]: unordered where it is neither seq-cst nor init.
def order (s : State(M)) (e : M.Addr) : Order := match field(N, s, .ref(e), "Order") with | .enum(o) => if N.enum?("seq-cst") == some(o) then .seqCst else if N.enum?("init") == some(o) then .init else .unordered | _ => .unorderedend
MemoryModel.View.modify
09-memory-view.spectec:95[[ModifyOp]] of an event called on a List of byte values and a [[Payload]], in the state.
def modify (d : CloSite -> List(Val(M)) -> List(Val(M)) -> Run(M, Val(M))) (s : State(M)) (e : M.Addr) (bs pay : List(M.Math)) : Option(List(M.Math)) := match field(N, s, .ref(e), "ModifyOp") with | .clo(site, caps) => let r : Run(M, Val(M)) := do { let b <- listOf(bs.map(.math)); let p <- listOf(pay.map(.math)); d(site, caps, [b, p]); } in match r(s) with | .ok(.comp(_, v, _), s') | .ok(v, s') => some(bytes(s', v)) | _ => none end | _ => noneend
MemoryModel.View.exec
09-memory-view.spectec:109The candidate execution Record ex of state s as the memory model sees it; an event with no [[Block]] has its own address, no Shared Data Block's.
SpecTec, 15 lines
def exec (d : CloSite -> List(Val(M)) -> List(Val(M)) -> Run(M, Val(M))) (s : State(M)) (ex : Val(M)) : Exec(M, M.Addr) { records := list(s, field(N, s, ex, "EventsRecords")).map(eventsRecord(N, s)); chosen := list(s, field(N, s, ex, "ChosenValues")).filterMap(chosenValue(N, s)); kind := kind(N, s); order := order(N, s); noTear e := match field(N, s, .ref(e), "NoTear") with | .bool(b) => b | _ => false end; block e := addr(field(N, s, .ref(e), "Block")).getD(e); byteIndex e := math(field(N, s, .ref(e), "ByteIndex")); elementSize e := math(field(N, s, .ref(e), "ElementSize")); payload e := bytes(s, field(N, s, .ref(e), "Payload")); modify := modify(N, d, s);}
MemoryModel.View.Formed
09-memory-view.spectec:127The Shared Data Block events of ex are formed: [[Block]] a reference, [[ByteIndex]] and [[ElementSize]] non-negative integers.
def Formed (d : CloSite -> List(Val(M)) -> List(Val(M)) -> Run(M, Val(M))) (s : State(M)) (ex : Val(M)) : Prop := forall e, MemoryModel.inSdb(exec(N, d, s, ex), e) -> (exists b, field(N, s, .ref(e), "Block") = .ref(b)) /\ (exists n, field(N, s, .ref(e), "ByteIndex") = .math(M.ofNat(n))) /\ exists n, field(N, s, .ref(e), "ElementSize") = .math(M.ofNat(n))