You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

A consistent locking policy guarantees that multiple threads cannot simultaneously access or modify shared data. When two or more operations must be performed as a single atomic operation, a consistent locking policy must be implemented using some form of locking, such as a mutex. In the absence of such a policy, the code is susceptible to race conditions.

When presented with a set of operations, where each is guaranteed to be atomic, it is tempting to assume that a single operation consisting of individually-atomic operations is guaranteed to be collectively atomic without additional locking. A grouping of calls to such methods requires additional synchronization for the group.

Compound operations on shared variables are also non-atomic. See rule CON42-C. Ensure that compound operations on shared variables are atomic for more information.

Noncompliant Code Example

This noncompliant code example stores two integers atomically. It also provides atomic methods to obtain their sum and product. All methods are locked with the same mutex to provide their atomicity.

static int a = 0;
static int b = 0;
mtx_t flag_mutex;
 
int result;
if ((result = mtx_init(&flag_mutex, mtx_plain)) == thrd_error) {
  /* handle error */
}


void set_values(int new_a, int new_b) {
  if ((result = mtx_lock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
  a = new_a;
  b = new_b;
  if ((result = mtx_unlock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
}

int get_sum() {
  if ((result = mtx_lock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
  int sum = a + b;
  if ((result = mtx_unlock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
  return sum;
}
  
int get_product() {
  if ((result = mtx_lock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
  int product = a * b;
  if ((result = mtx_unlock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
  return product;
}


/* Can be called by multiple threads */
void multiply_monomials(int x1, int x2) {
  printf("(x + %d)(x + %d)\n", x1, x2);
  set_values( x1, x2);
  printf("= x^2 + %dx + %d\n", get_sum(), get_product());
}

Unfortunately, the multiply_monomials() function is still subject to race conditions, despite relying exclusively on atomic function calls. It is quite possible for get_sum() and get_product() to work with tdifferent numbers than the ones that were set by set_values(). It is even possible for get_sum() to operate with different numbers than get_product().

Compliant Solution

This compliant solution locks the multiply_monomials() function with the same mutex lock that is used by the other functions.

 

/* Can be called by multiple threads */
void multiply_monomials(int x1, int x2) {
  if ((result = mtx_lock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }
  set_values( x1, x2);
  int sum = get_sum();
  int product = get_product();
  if ((result = mtx_unlock(&flag_mutex)) == thrd_error) {
    /* handle error */
  }

  printf("(x + %d)(x + %d)\n", x1, x2);
  printf("= x^2 + %dx + %d\n", sum, product);
}

 

Risk Assessment

Failure to ensure the atomicity of two or more operations that must be performed as a single atomic operation can result in race conditions in multithreaded applications.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

CON43-C

low

probable

medium

P4

L3

Related Guidelines

MITRE CWE

CWE-362. Concurrent execution using shared resource with improper synchronization ("race condition")

 

CWE-366. Race condition within a thread

 

CWE-662. Improper synchronization

CERT Java

VNA03-J. Do not assume that a group of calls to independently atomic methods is atomic

CERT JavaVNA04-J. Ensure that calls to chained methods are atomic

Bibliography

 

 

 

            

  • No labels