Download Annotation-based property checking for systems software
Transcript
typedef struct _LIST_ENTRY{
struct _LIST_ENTRY *Flink, *Blink;
} LIST_ENTRY, *PLIST_ENTRY;
typedef struct _NODEA{
PERESOURCE Resource;
LIST_ENTRY NodeBQueue;
...
} NODEA, *PNODEA;
typedef struct _NODEB{
PNODEA
ParentA;
ULONG State;
LIST_ENTRY NodeALinks;
...
} NODEB, *PNODEB;
#define __resrA_held(x)\
__resource(‘‘NODEA_RESOURCE’’,x) > 0
VOID CompClearChildState(PNODEA NodeA,
ULONG StateMask) {
CompAcquireNodeAExcl(NodeA);
PNODEB NodeB;
CompFindChildState(NodeA, StateMask, &NodeB);
#define CONTAINING_RECORD(addr, type, field)\
((type *)((PCHAR)(addr) -\
(PCHAR)(&((type *)0)->field)))
//helper macros
#define ENCL_NODEA(x)\
CONTAINING_RECORD(x, NODEA, NodeBQueue)
#define ENCL_NODEB(x)\
CONTAINING_RECORD(x, NODEB, NodeALinks)
Figure 1. Data structures and macros used in
the example.
is a pointer to a ERESOURCE structure that implements a
reader-writer lock. The lock not only protects accesses to
the fields in the NODEA structure but additionally also protects the NodeALinks, ParentA and State fields in
any of its NODEB children.
Figure 2 describes three procedures that manipulate the
NODEA and NODEB objects. For this example, we indicate the lines containing an annotation using an empty
comment “/**/” at the start of the line. The procedure
CompClearChildState takes a NODEA object NodeA
and clears a mask StateMask from the State field of
any NODEB child that matches this mask. It uses the
procedure CompFindChildState in a loop to find all
the children that match the StateMask and then calls
CompClearState on the child to actually clear the mask.
The procedure CompFindChildState iterates over the
children for a NODEA object and returns either the first child
that matches the mask, or NULL if no such child exists.
To encode the data race freedom property on the fields
of NODEA and NODEB objects, we introduce assertions
that each access (read or write) to a field is guarded
by the Resource lock in the appropriate NODEA object.
The three procedures clearly satisfy data race
freedom since the lock on the NODEA object is acquired by a call to CompAcquireNodeAExcl inside
CompClearChildState before any of the operations.
/**/__loop_invariant(NodeB != NULL ==>
/**/
NodeB->ParentA == NodeA)
while (NodeB != NULL) {
CompClearState(NodeB, StateMask);
CompFindChildState(NodeA, StateMask, &NodeB);
}
CompReleaseNodeA(NodeA);
}
/**/__requires(__resrA_held(NodeA))
/**/__ensures (*PNodeB != NULL ==>
/**/
(*PNodeB)->ParentA == NodeA)
VOID CompFindChildState(PNODEA NodeA,
ULONG StateMask,
PNODEB* PNodeB) {
PLIST_ENTRY Entry = NodeA->NodeBQueue.Flink;
/**/__loop_invariant(
/**/ Entry != &NodeA->NodeBQueue ==>
/**/ ENCL_NODEB(Entry)->ParentA == NodeA
/**/ )
while (Entry != &NodeA->NodeBQueue) {
PNODEB NodeB = ENCL_NODEB(Entry);
if (NodeB->State & StateMask != 0) {
*PNodeB = NodeB; return;
}
Entry = Entry->FLink;
}
*PNodeB = NULL; return;
}
/**/__requires(__resrA_held(NodeB->ParentA))
VOID CompClearState(PNODEB NodeB,
ULONG StateMask) {
NodeB->State &= ˜StateMask;
}
Figure 2. Procedures and annotations for
data race freedom.