https://github.com/jetsetilly/critsec
Proof-of concept for a method of creating and identifying critical sections such that they can be statically analysed for violations
https://github.com/jetsetilly/critsec
go golang
Last synced: about 1 month ago
JSON representation
Proof-of concept for a method of creating and identifying critical sections such that they can be statically analysed for violations
- Host: GitHub
- URL: https://github.com/jetsetilly/critsec
- Owner: JetSetIlly
- Created: 2024-04-24T16:55:28.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-24T16:56:12.000Z (about 1 year ago)
- Last Synced: 2024-04-30T11:43:28.551Z (about 1 year ago)
- Topics: go, golang
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# critsec
Proof-of concept for a method of treating critical sections such that they can
be statically analysed for violations.### Critical Sectioning
In this scheme critical sections are defined by embedding the `crit.Section`
type into a new struct type.```
type exampleCritSectioning struct {
crit.Section
a int
b bool
}
```Any fields in this new struct type can then only be accessed if the critical
section has been `leased````
var A exampleCritSectioning_ = A.Lease(func() error {
A.a = 10
A.b = true
return nil
})
```### Limitations
For simplicity and for the purposes of the proof-of-concept there are two
significant restrictions on `crit.Section` usage:- instances of types derived from `crit.Section` cannot be passed as arguments
to functions
- only one instance of each `crit.Section` derived type can be declaredTo be clear these limitations are enforced by the static analysis and the
`critcheck` driver. They only exist to make the job of Lease enforcment easier
and with more sophisticated parsing of the AST the limitations can most probably
be lifted.### Static Analysis
The project provides a [static
analyser](https://pkg.go.dev/golang.org/x/[email protected]/go/analysis) to report critical section violations.A `critcheck` command is also provided. This is a standalone driver for the
analysis package and will be used for the following demonstration. The
demonstration uses the `example/example.go` program for input.#### Example output
When run without arguments, as in the example below, the static analysis issues
single line reports with a brief description of the violation.```
> critcheck example.go
/home/steve/critsec/example/example.go:27:1: crit.Section types cannot be passed to a function
/home/steve/critsec/example/example.go:28:2: assignment to crit.Section without Lease
/home/steve/critsec/example/example.go:47:4: assignment to crit.Section without Lease
/home/steve/critsec/example/example.go:55:2: assignment to crit.Section without Lease
/home/steve/critsec/example/example.go:56:6: access of crit.Section without Lease
/home/steve/critsec/example/example.go:63:6: multiple instance of a crit.Section derived type
````critcheck` accepts the standard command line arguments for Go analysis drivers.
For example, the `-c` option instructs the program to print the line of source
that caused the violation and additional lines to provide context.```
> critcheck -c 1 example.go
/home/steve/critsec/example/example.go:27:1: crit.Section types cannot be passed to a function
26 // use of a crit.Section derived type as a parameter
27 func used(c *critSectionExample) {
28 c.value = -1
/home/steve/critsec/example/example.go:28:2: assignment to crit.Section without Lease
27 func used(c *critSectionExample) {
28 c.value = -1
29 }
/home/steve/critsec/example/example.go:47:4: assignment to crit.Section without Lease
46 for i := 0; i < 1000; i++ {
47 C.value = 2
48 }
/home/steve/critsec/example/example.go:55:2: assignment to crit.Section without Lease
54 // deliberate critical section violations
55 C.value = 4
56 _ = C.value
/home/steve/critsec/example/example.go:56:6: access of crit.Section without Lease
55 C.value = 4
56 _ = C.value
57
/home/steve/critsec/example/example.go:63:6: multiple instance of a crit.Section derived type
62 func subtask() {
63 var D critSectionExample
64
```