C and C++


CONCURRENCY.DATARACE : データ競合

要旨

複数のスレッドが共有データにアクセスする場合で、別々にアクセスするための明示的な同期操作無しに、少なくとも1つのスレッドが共有データの値を変更しました。

CodeSonar はミューテックスロックの取得による、共有メモリアクセスの同期モデルを仮定して解析を行います。 (生産者/消費者モデルの様な)他の同期パターンを使うようなアプリケーションでは、 CodeSonar はデータ競合の誤検出を発生する可能性があります。

プロパティ

クラス名 Data Race
日本語クラス名 データ競合
クラス分類 信頼性 (reliability)
ニーモニック CONCURRENCY.DATARACE
カテゴリー
MisraC2023 MisraC2023:D.5.1 There shall be no data races between threads
Misra2012 Misra2012:D.5.1 There shall be no data races between threads
CWE CWE:362 Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
  CWE:364 Signal Handler Race Condition
  CWE:366 Race Condition within a Thread
  CWE:413 Improper Resource Locking
  CWE:567 Unsynchronized Access to Shared Data in a Multithreaded Context
CERT-C CERT-C:CON07-C Ensure that compound operations on shared variables are atomic
  CERT-C:CON32-C Prevent data races when accessing bit-fields from multiple threads
  CERT-C:CON43-C Do not allow data races in multithreaded code
  CERT-C:POS49-C When data must be accessed by multiple threads, provide a mutex and guarantee no adjacent data is also accessed
  CERT-C:SIG31-C Do not access shared objects in signal handlers
CERT-CPP CERT-CPP:CON52-CPP Prevent data races when accessing bit-fields from multiple threads
DISA-6r1 DISA-6r1:V-222567 The application must not be vulnerable to race conditions.
DISA-5r3 DISA-5r3:V-70185 The application must not be vulnerable to race conditions.
DISA-4r3 DISA-4r3:V-70185 The application must not be vulnerable to race conditions.
DISA-3r10 DISA-3r10:V-16815 The designer will ensure the application is not vulnerable to race conditions.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Data Race"
また、このワーニングクラスを有効にした場合は TIME_LIMIT_INTER_CLASSIFYTIME_LIMIT_INTRA_CLASSIFY の値を、それぞれ無効の場合の2倍の値程度に設定することを推奨します。

スレッドエントリーポイント

以下のどちらかに該当する場合、関数 f() をスレッドエントリーポイントと認識します。

[オブジェクト指向のマルチスレッド API を使用するプログラムの場合のみ]
o がスレッドエントリーポイントを期待する箇所で使用され、m() が設定ファイルの THREAD_ENTRY_METHOD_NAMES と一致する場合に、オブジェクトメソッド o.m() をスレッドエントリーポイントとみなします。

#include <pthread.h>

int count;

void update_count(){
    int tmp = count;                                             /* read without locking */
    tmp = tmp+1;
    count = tmp;       /* 'Data Race' warning issued here */     /* write without locking */
}

void *watch_Y(void *arg){
    while (1){
        /* If see a yellow object, then... */
        update_count();
        /* Do yellow-specific tasks. */
    }
}

void *watch_R(void *arg){
    while (1){
        /* If see a red object, then... */
        update_count();
        /* Do red-specific tasks. */
    }
}

int main(void) {
    pthread_t Y_pth, R_pth;
    int y_res, r_res = 0;
    int retval = 0;
    y_res = pthread_create(&Y_pth, NULL, watch_Y, "yellow");
    r_res = pthread_create(&R_pth, NULL, watch_R, "red");
    /* ... */
    if (!y_res)
        pthread_detach(Y_pth);
    if (!r_res)
        pthread_detach(R_pth);
    return y_res + r_res;
}

上記は update_count() 内でデータ競合が発生する可能性があります。

Data Race example diagram

注釈

このワーニングクラスが有効の場合でも、 以下の箇所が存在しない場合はチェックしません。

設定パラメータ FORCE_THREAD_ENTRY_NAMES もしくは THREAD_ENTRY_METHOD_NAMES にて指定された関数は上記に含まれないので注意してください。

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。