メインコンテンツ

CERT C: Rule CON30-C

Clean up thread-specific storage

説明

このチェッカーは、既定の Polyspace® as You Code 解析では非アクティブにされますPolyspace as You Code 解析で非アクティブにされるチェッカー (Polyspace Access)を参照してください

ルール定義

スレッド固有のストレージをクリーンアップします。1

Polyspace 実装

ルール チェッカーは、"スレッド固有のメモリ リーク" をチェックします。

すべて展開する

問題

スレッド固有のメモリ リークは、動的に割り当てられたスレッド固有のメモリをスレッドの終了前に開放していない場合に発生します。

スレッド固有のストレージを作成するには、通常、以下の手順を行います。

  1. スレッド固有のストレージのキーを作成する。

  2. スレッドを作成する。

  3. 各スレッドで、ストレージを動的に割り当ててから、このストレージとキーを関連付ける。

    関連付けたら、格納されたデータはキーを使用して後から読み取ることができます。

  4. スレッドの終了前に、キーを使用してスレッド固有のメモリを解放する。

チェッカーは、最後の手順が不足しているスレッドの実行パスにフラグを設定します。

チェッカーは、以下の関数のファミリに対して機能します。

  • tss_get および tss_set (C11)

  • pthread_getspecific および pthread_setspecific (POSIX)

リスク

メモリに格納されたデータは、スレッドの終了後も他のプロセスから利用できます (メモリ リーク)。メモリ リークはセキュリティの脆弱性になるだけでなく、使用可能なメモリの量を縮小し、パフォーマンスを低下させる可能性があります。

修正方法

スレッドの終了前に動的に割り当てられたメモリを解放します。

動的に割り当てられたメモリは、free などの関数を使用して明示的に開放できます。

または、キーを作成するときにデストラクター関数とキーを関連付けることができます。デストラクター関数は、キー値を引数に指定してスレッドの最後に呼び出されます。デストラクター関数の本体で、キーに関連付けられたメモリを解放することができます。この方法を使用している場合でも、Bug Finder は欠陥にフラグを設定します。適切なコメントを使用してこの欠陥は無視します。詳細は、以下を参照してください。

例 - スレッドの終了時にメモリが未解放
#include <threads.h>
#include <stdlib.h>
 
/* Global key to the thread-specific storage */
tss_t key;
enum { MAX_THREADS = 3 };
 

int add_data(void) {
  int *data = (int *)malloc(2 * sizeof(int));
  if (data == NULL) {
    return -1;  /* Report error  */
  }
  data[0] = 0;
  data[1] = 1;
 
  if (thrd_success != tss_set(key, (void *)data)) {
    /* Handle error */
  }
  return 0;
}
 
void print_data(void) {
  /* Get this thread's global data from key */
  int *data = tss_get(key);
 
  if (data != NULL) {
    /* Print data */
  }
}
 
int func(void *dummy) {
  if (add_data() != 0) {
    return -1;  /* Report error */ //Noncompliant
  }
  print_data();
  return 0; //Noncompliant
}
 
int main(void) {
  thrd_t thread_id[MAX_THREADS];
 
  /* Create the key before creating the threads */
  if (thrd_success != tss_create(&key, NULL)) {
    /* Handle error */
  }
 
  /* Create threads that would store specific storage */
  for (size_t i = 0; i < MAX_THREADS; i++) {
    if (thrd_success != thrd_create(&thread_id[i], func, NULL)) {
      /* Handle error */
    }
  }
 
  for (size_t i = 0; i < MAX_THREADS; i++) {
    if (thrd_success != thrd_join(thread_id[i], NULL)) {
      /* Handle error */
    }
  }
 
  tss_delete(key);
  return 0;
}

この例では、各スレッドの開始関数 func で次の 2 つの関数を呼び出しています。

  • add_data: この関数はストレージを動的に割り当て、関数 tss_set を使用してそのストレージとキーを関連付けます。

  • print_data: この関数は関数 tss_get を使用して、格納されているデータを読み取ります。

func が戻った時点では、動的に割り当てられたストレージが解放されていません。

修正 — 動的に割り当てられたメモリを明示的に開放

1 つの修正方法として、スレッドの開始関数が終わる前に、動的に割り当てられたメモリを明示的に開放します。訂正されたバージョンでは変更が強調表示されています。

この訂正されたバージョンでも、func のエラー処理セクションの return ステートメントには相変わらず欠陥が表示されます。エラー処理セクションに入るのは動的なメモリ割り当てが失敗した場合に限られるため、この欠陥は実際には発生しません。適切なコメントを使用してこの残りの欠陥は無視します。詳細は、以下を参照してください。

#include <threads.h>
#include <stdlib.h>
 
/* Global key to the thread-specific storage */
tss_t key;
enum { MAX_THREADS = 3 };
 

int add_data(void) {
  int *data = (int *)malloc(2 * sizeof(int));
  if (data == NULL) {
    return -1;  /* Report error  */
  }
  data[0] = 0;
  data[1] = 1;
 
  if (thrd_success != tss_set(key, (void *)data)) {
    /* Handle error */
  }
  return 0;
}
 
void print_data(void) {
  /* Get this thread's global data from key */
  int *data = tss_get(key);
 
  if (data != NULL) {
    /* Print data */
  }
}
 
int func(void *dummy) {
  if (add_data() != 0) {
    return -1;  /* Report error */ //Noncompliant
  }
  print_data();
  free(tss_get(key));
  return 0;
}
 
int main(void) {
  thrd_t thread_id[MAX_THREADS];
 
  /* Create the key before creating the threads */
  if (thrd_success != tss_create(&key, NULL)) {
    /* Handle error */
  }
 
  /* Create threads that would store specific storage */
  for (size_t i = 0; i < MAX_THREADS; i++) {
    if (thrd_success != thrd_create(&thread_id[i], func, NULL)) {
      /* Handle error */
    }
  }
 
  for (size_t i = 0; i < MAX_THREADS; i++) {
    if (thrd_success != thrd_join(thread_id[i], NULL)) {
      /* Handle error */
    }
  }
 
  tss_delete(key);
  return 0;
}

チェック情報

グループ: Rule 14.同時実行 (CON)

バージョン履歴

R2019a で導入


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.