From 6a1999730394ffccee3b9317a2e8ecfd7dc0379f Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Tue, 9 Mar 2021 05:22:45 -0800 Subject: [PATCH] [racerd] fix analysis of class initializers Summary: RacerD needs to analyse the class initialiser in order to establish field properties in its post, such as that certain static fields are synchronized containers. There was a bug where class initializers were not analysed at all, from the time where there was no analysis of field properties in the post. We still don't want to report on the class initialiser since it cannot possibly race with itself (JVM guarantees that) and it cannot race with any of the other methods in its class (because it must finish before any other method can be called). Reviewed By: da319 Differential Revision: D26887151 fbshipit-source-id: 570aff370 --- infer/src/concurrency/RacerDFileAnalysis.ml | 1 + infer/src/concurrency/RacerDModels.ml | 3 +-- infer/tests/codetoanalyze/java/racerd/Containers.java | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/infer/src/concurrency/RacerDFileAnalysis.ml b/infer/src/concurrency/RacerDFileAnalysis.ml index e35e5ddb6..33e171121 100644 --- a/infer/src/concurrency/RacerDFileAnalysis.ml +++ b/infer/src/concurrency/RacerDFileAnalysis.ml @@ -362,6 +362,7 @@ let should_report_on_proc tenv procdesc = requested via @ThreadSafe in java *) RacerDModels.is_thread_safe_method proc_name tenv || (not (PredSymb.equal_access (Procdesc.get_access procdesc) Private)) + && (not (Procname.Java.is_class_initializer java_pname)) && (not (Procname.Java.is_autogen_method java_pname)) && not (Annotations.pdesc_return_annot_ends_with procdesc Annotations.visibleForTesting) | ObjC_Cpp objc_cpp when Procname.ObjC_Cpp.is_cpp_lambda objc_cpp -> diff --git a/infer/src/concurrency/RacerDModels.ml b/infer/src/concurrency/RacerDModels.ml index 84be865a8..263c85099 100644 --- a/infer/src/concurrency/RacerDModels.ml +++ b/infer/src/concurrency/RacerDModels.ml @@ -343,8 +343,7 @@ let should_analyze_proc tenv pn = (not ( match pn with | Procname.Java java_pname -> - Procname.Java.is_class_initializer java_pname - || Typ.Name.Java.is_external (Procname.Java.get_class_type_name java_pname) + Typ.Name.Java.is_external (Procname.Java.get_class_type_name java_pname) (* third party code may be hard to change, not useful to report races there *) | _ -> false )) diff --git a/infer/tests/codetoanalyze/java/racerd/Containers.java b/infer/tests/codetoanalyze/java/racerd/Containers.java index 83ad44da4..d647203b3 100644 --- a/infer/tests/codetoanalyze/java/racerd/Containers.java +++ b/infer/tests/codetoanalyze/java/racerd/Containers.java @@ -386,4 +386,10 @@ class Containers { byte[] raceOnMacDoFinalBad() { return mac.doFinal(); } + + static Set staticSyncSet = new ConcurrentSkipListSet(); + + static void staticAddToSyncIntegerSetOk(int s) { + staticSyncSet.add(s); + } }