Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/analysis/passes/critsec: a new checker for control flow in critical sections #59978

Open
adonovan opened this issue May 4, 2023 · 0 comments
Assignees
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@adonovan
Copy link
Member

adonovan commented May 4, 2023

A common programming mistake in Go is to add an early return statement in a critical section and then to forget to release the mutex. Usually the unlock operation can be deferred so that it runs automatically, but there are times when the critical section does not extend to the end of the function so defer cannot be used. It would be interesting to see how many mistakes a checker in the analysis framework might catch. (A quick experiment in x/tools caught only the one instance that I was training it on.)

The notion of "critical section" could be generalized to other control flow problems. For example, some long functions (example) are structured to have a single return statement and a comment saying "don't return before the end!". This region can be thought of as a "critical section of no return" that is "unlocked" immediately before the final return statement. Similarly, a function that opens a file and then closes it before returning should probably close it on all paths, making a "critical section of an open file". Ideally a checker would allow the user to define pairs of lock/unlock operation using some kind of annotation mechanism.

@adonovan adonovan self-assigned this May 4, 2023
@adonovan adonovan added Analysis Issues related to static analysis (vet, x/tools/go/analysis) Tools This label describes issues relating to any tools in the x/tools repository. labels May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

1 participant