From 1891a16f8fb06f0583b831f98fd764eb08ad3b08 Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Thu, 12 Jan 2017 11:45:27 -0800 Subject: [PATCH] [thread-safety] suppress warnings on methods annotated with @OnEvent Reviewed By: peterogithub Differential Revision: D4407224 fbshipit-source-id: 511aad4 --- infer/src/checkers/ThreadSafety.ml | 10 ++++------ infer/src/checkers/annotations.ml | 4 ++++ infer/src/checkers/annotations.mli | 1 + .../java/threadsafety/Annotations.java | 13 ++++++++++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/infer/src/checkers/ThreadSafety.ml b/infer/src/checkers/ThreadSafety.ml index 1ddd14746..7124edb13 100644 --- a/infer/src/checkers/ThreadSafety.ml +++ b/infer/src/checkers/ThreadSafety.ml @@ -300,15 +300,13 @@ let is_thread_confined_method tenv pname = (* we don't want to warn on methods that run on the UI thread because they should always be single-threaded *) let runs_on_ui_thread proc_desc = - (* assume that methods named onClick always run on the UI thread *) - let is_on_click pdesc = match Procdesc.get_proc_name pdesc with - | Procname.Java java_pname -> Procname.java_get_method java_pname = "onClick" - | _ -> false in + (* assume that methods annotated with @UiThread or @OnEvent(SomeEvent.class) always run on the UI + thread *) let is_annotated pdesc = let annotated_signature = Annotations.get_annotated_signature (Procdesc.get_attributes pdesc) in let ret_annotation, _ = annotated_signature.Annotations.ret in - Annotations.ia_is_ui_thread ret_annotation in - is_on_click proc_desc || is_annotated proc_desc + Annotations.ia_is_ui_thread ret_annotation || Annotations.ia_is_on_event ret_annotation in + is_annotated proc_desc (* return true if we should compute a summary for the procedure. if this returns false, we won't analyze the procedure or report any warnings on it *) diff --git a/infer/src/checkers/annotations.ml b/infer/src/checkers/annotations.ml index 8fe684ce1..ad099abd0 100644 --- a/infer/src/checkers/annotations.ml +++ b/infer/src/checkers/annotations.ml @@ -102,6 +102,7 @@ let mutable_ = "Mutable" let nullable = "Nullable" let nonnull = "Nonnull" let on_bind = "OnBind" +let on_event = "OnEvent" let camel_nonnull = "NonNull" let notnull = "NotNull" let present = "Present" @@ -208,6 +209,9 @@ let ia_is_ignore_allocations ia = let ia_is_suppress_lint ia = ia_ends_with ia suppress_lint +let ia_is_on_event ia = + ia_ends_with ia on_event + let ia_is_privacy_source ia = ia_ends_with ia privacy_source diff --git a/infer/src/checkers/annotations.mli b/infer/src/checkers/annotations.mli index a17d47a05..0002e13a4 100644 --- a/infer/src/checkers/annotations.mli +++ b/infer/src/checkers/annotations.mli @@ -93,6 +93,7 @@ val ia_is_performance_critical : Annot.Item.t -> bool val ia_is_no_allocation : Annot.Item.t -> bool val ia_is_ignore_allocations : Annot.Item.t -> bool val ia_is_suppress_lint : Annot.Item.t -> bool +val ia_is_on_event : Annot.Item.t -> bool val ia_is_privacy_source : Annot.Item.t -> bool val ia_is_privacy_sink : Annot.Item.t -> bool val ia_is_integrity_source : Annot.Item.t -> bool diff --git a/infer/tests/codetoanalyze/java/threadsafety/Annotations.java b/infer/tests/codetoanalyze/java/threadsafety/Annotations.java index 434c1bb6c..0f330d44b 100644 --- a/infer/tests/codetoanalyze/java/threadsafety/Annotations.java +++ b/infer/tests/codetoanalyze/java/threadsafety/Annotations.java @@ -9,12 +9,22 @@ package codetoanalyze.java.checkers; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + import android.support.annotation.UiThread; import com.facebook.infer.annotation.ThreadConfined; /** tests for classes and method annotations that are meaningful w.r.t thread-safety */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.CLASS) +@interface OnEvent { +} + @ThreadSafe class Annotations { Object f; @@ -32,7 +42,8 @@ class Annotations { this.f = new Object(); } - // we model onClick as running on the UI thread, should not warn + // anything annotated with OnEvent is modeled as running on the UI thread, should not warn + @OnEvent public void onClick() { this.f = new Object(); }