I am currently working on a large project which has recently had many changes, most prominently the addition of threading support.
While going through the code I have identified sections that may potentially cause race conditions, if not now then sometime in the future. In order to prevent such a regression, I would like to write a test that can reliably detect a race condition in that particular region so as to ensure that no future commits cause this fault.
The code isn't littered with sleep() statements, but is a potential deadlock and race minefield and I want to ensure robustness.
This project is written completely in C. So, is there anyway for me to write unit tests to prevent race conditions?
Testing for Race Conditions The best way to test for race condition vulnerabilities is to have access to source code, in what is known as a “white box” assessment.
RacerX This flow-sensitive static analysis tool is used for detecting races and deadlocks.
A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable.
Race conditions are inherently a result of non-determinism. If you cannot ensure that the calling sequence is secure, then introduce a number of run-time checks that verify the protocol invariants are honoured. Then, at least you will have evidence of a fault whenever they occur.
While this won't solve your problem, it at least gives you a tool to quantify the extent of the problem.
If any of the races are triggered from events outside the scope of the application, then any static analysis would require this to also be modeled to be able to detect the conditions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With