From 70b5129914c6665b00bc4fb5471a6a5e1256c877 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 8 May 2023 14:35:16 -0400 Subject: [PATCH 1/2] Make clang thread safety analysis configurable. Depending on CLANG version used, thread safety analysis may not be available. Found this when OSS-FUZZ tests were failing. This CL adds a `chip_enable_thread_safety_checks` flag to turn these checks on/off and defaults them to on when the compiler is clang. --- src/system/BUILD.gn | 10 ++++++++++ src/system/SystemMutex.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/system/BUILD.gn b/src/system/BUILD.gn index 581ed2e23f0bb0..bbce0eb2810933 100644 --- a/src/system/BUILD.gn +++ b/src/system/BUILD.gn @@ -17,6 +17,7 @@ import("//build_overrides/chip.gni") import("//build_overrides/nlassert.gni") import("//build_overrides/nlfaultinjection.gni") +import("${build_root}/config/compiler/compiler.gni") import("${chip_root}/build/chip/buildconfig_header.gni") import("${chip_root}/build/chip/tests.gni") import("${chip_root}/src/platform/device.gni") @@ -32,6 +33,12 @@ declare_args() { # Extra include dirs for project configs. chip_project_config_include_dirs = [] + + # enable clang thread safety analysis in SystemMutex.h, + # specifically attributes such as capability, guarded_by, acquire, ... + # + # see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html + chip_enable_thread_safety_checks = is_clang } if (chip_project_config_include_dirs == [] && @@ -117,6 +124,9 @@ buildconfig_header("system_buildconfig") { "SYSTEM_PLATFORM_CONFIG_INCLUDE=${chip_system_platform_config_include}", ] } + if (chip_enable_thread_safety_checks) { + defines += ["SYSTEM_ENABLE_CLANG_THREAD_SAFETY_ANALYSIS=1"] + } if (chip_system_layer_impl_config_file != "") { defines += [ "CHIP_SYSTEM_LAYER_IMPL_CONFIG_FILE=${chip_system_layer_impl_config_file}" ] diff --git a/src/system/SystemMutex.h b/src/system/SystemMutex.h index bb0d67fd7d91c4..4a1f135f5e9efd 100644 --- a/src/system/SystemMutex.h +++ b/src/system/SystemMutex.h @@ -64,7 +64,7 @@ namespace chip { namespace System { // Enable thread safety attributes only with clang. -#if defined(__clang__) && (!defined(SWIG)) +#if defined(SYSTEM_ENABLE_CLANG_THREAD_SAFETY_ANALYSIS) && (!defined(SWIG)) #define CHIP_TSA_ATTRIBUTE__(x) __attribute__((x)) #else #define CHIP_TSA_ATTRIBUTE__(x) From 2a89692ab62c8275a49e876e79cc957e0fe9310e Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 8 May 2023 14:39:09 -0400 Subject: [PATCH 2/2] Restyle --- src/system/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/system/BUILD.gn b/src/system/BUILD.gn index bbce0eb2810933..acc7f790e3471d 100644 --- a/src/system/BUILD.gn +++ b/src/system/BUILD.gn @@ -125,8 +125,8 @@ buildconfig_header("system_buildconfig") { ] } if (chip_enable_thread_safety_checks) { - defines += ["SYSTEM_ENABLE_CLANG_THREAD_SAFETY_ANALYSIS=1"] - } + defines += [ "SYSTEM_ENABLE_CLANG_THREAD_SAFETY_ANALYSIS=1" ] + } if (chip_system_layer_impl_config_file != "") { defines += [ "CHIP_SYSTEM_LAYER_IMPL_CONFIG_FILE=${chip_system_layer_impl_config_file}" ]