From 0a786e90197568207593ca15928325db0c6434e4 Mon Sep 17 00:00:00 2001 From: Daiva Naudziuniene Date: Mon, 19 Jun 2017 08:33:53 -0700 Subject: [PATCH] [ThreadSafety] Do not report if all accesses are unprotected. Reviewed By: jberdine Differential Revision: D5256183 fbshipit-source-id: f2f2c57 --- infer/src/checkers/ThreadSafety.ml | 11 +++++-- .../cpp/threadsafety/basics_with_mutex.cpp | 32 ------------------- .../codetoanalyze/cpp/threadsafety/issues.exp | 4 --- 3 files changed, 8 insertions(+), 39 deletions(-) delete mode 100644 infer/tests/codetoanalyze/cpp/threadsafety/basics_with_mutex.cpp diff --git a/infer/src/checkers/ThreadSafety.ml b/infer/src/checkers/ThreadSafety.ml index 873e972f9..aa3178885 100644 --- a/infer/src/checkers/ThreadSafety.ml +++ b/infer/src/checkers/ThreadSafety.ml @@ -1215,11 +1215,16 @@ let report_unsafe_accesses aggregated_access_map = (* protected write, do nothing *) reported_acc | Access.Read, AccessPrecondition.Unprotected _ -> - (* unprotected read. report all writes as conflicts *) + (* unprotected read. report all writes as conflicts for java *) + (* for c++ filter out unprotected writes *) + let is_cpp_protected_write pre = match pre with + | AccessPrecondition.Unprotected _ -> Typ.Procname.is_java pname + | AccessPrecondition.Protected _ -> true in let all_writes = List.filter - ~f:(fun (other_access, _, other_threaded, _, _) -> - TraceElem.is_write other_access && not (threaded && other_threaded)) + ~f:(fun (other_access, pre, other_threaded, _, _) -> + TraceElem.is_write other_access && not (threaded && other_threaded) + && is_cpp_protected_write pre) accesses in if List.is_empty all_writes then diff --git a/infer/tests/codetoanalyze/cpp/threadsafety/basics_with_mutex.cpp b/infer/tests/codetoanalyze/cpp/threadsafety/basics_with_mutex.cpp deleted file mode 100644 index 8ac513e97..000000000 --- a/infer/tests/codetoanalyze/cpp/threadsafety/basics_with_mutex.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2017 - present Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -#include - -namespace basics { - -class BasicsWithHeader { - public: - BasicsWithHeader(){}; - - void set(int new_value) { - field_1 = new_value; - field_2 = new_value; - field_3 = new_value; - }; - int get1() { return field_1; }; - int get2() { return field_2; }; - - private: - int field_1; - int field_2; - int field_3; - std::mutex mutex_; -}; -} diff --git a/infer/tests/codetoanalyze/cpp/threadsafety/issues.exp b/infer/tests/codetoanalyze/cpp/threadsafety/issues.exp index 35b2f7bf2..50f349744 100644 --- a/infer/tests/codetoanalyze/cpp/threadsafety/issues.exp +++ b/infer/tests/codetoanalyze/cpp/threadsafety/issues.exp @@ -1,10 +1,6 @@ codetoanalyze/cpp/threadsafety/basics.cpp, basics::Basic_get2, 3, THREAD_SAFETY_VIOLATION, [,access to `suspiciously_written`,,access to `suspiciously_written`] -codetoanalyze/cpp/threadsafety/basics.cpp, basics::Basic_get3, 0, THREAD_SAFETY_VIOLATION, [,access to `not_guarded`,,access to `not_guarded`] codetoanalyze/cpp/threadsafety/basics.cpp, basics::Basic_get4, 0, THREAD_SAFETY_VIOLATION, [,access to `suspiciously_read`,,access to `suspiciously_read`] -codetoanalyze/cpp/threadsafety/basics_with_mutex.cpp, basics::BasicsWithHeader_get1, 0, THREAD_SAFETY_VIOLATION, [,access to `field_1`,,access to `field_1`] -codetoanalyze/cpp/threadsafety/basics_with_mutex.cpp, basics::BasicsWithHeader_get2, 0, THREAD_SAFETY_VIOLATION, [,access to `field_2`,,access to `field_2`] codetoanalyze/cpp/threadsafety/lock_guard.cpp, basics::LockGuard_get2, 3, THREAD_SAFETY_VIOLATION, [,access to `suspiciously_written`,,access to `suspiciously_written`] -codetoanalyze/cpp/threadsafety/lock_guard.cpp, basics::LockGuard_get3, 0, THREAD_SAFETY_VIOLATION, [,access to `not_guarded`,,access to `not_guarded`] codetoanalyze/cpp/threadsafety/lock_guard.cpp, basics::LockGuard_get4, 0, THREAD_SAFETY_VIOLATION, [,access to `suspiciously_read`,,access to `suspiciously_read`] codetoanalyze/cpp/threadsafety/lock_guard.cpp, basics::LockGuard_test1, 2, THREAD_SAFETY_VIOLATION, [,access to `suspiciously_read`,,access to `suspiciously_read`] codetoanalyze/cpp/threadsafety/lock_guard_with_scope.cpp, basics::LockGuardWithScope_get3, 0, THREAD_SAFETY_VIOLATION, [,access to `not_guarded`,,access to `not_guarded`]