Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write tests that check for race conditions?

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?

like image 543
darnir Avatar asked Sep 12 '13 18:09

darnir


People also ask

How do you test 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.

Which tool will help us detecting possible race conditions in a program?

RacerX This flow-sensitive static analysis tool is used for detecting races and deadlocks.

What is a race condition How will you find and solve race condition?

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.


1 Answers

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.

like image 152
Pekka Avatar answered Nov 11 '22 18:11

Pekka