[threadsafety] don't warn on methods from classes annotated ThreadConfined

Reviewed By: sblackshear

Differential Revision: D4384861

fbshipit-source-id: 9b7c999
master
Peter O'Hearn 8 years ago committed by Facebook Github Bot
parent 68c0705f26
commit 7bcc7e421d

@ -0,0 +1,21 @@
/*
* Copyright (c) 2004 - 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.
*/
package com.facebook.infer.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface ThreadConfined {
}

@ -245,6 +245,16 @@ let is_initializer tenv proc_name =
Procname.is_class_initializer proc_name || Procname.is_class_initializer proc_name ||
FbThreadSafety.is_custom_init tenv proc_name FbThreadSafety.is_custom_init tenv proc_name
(* Methods in @ThreadConfined classes are assumed to all run on the same thread.
For the moment we won't warn on accesses resulting from use of such methods at all.
In future we should account for races between these methods and methods from completely
different classes that don't necessarily run on the same thread as the confined object.
*)
let is_thread_confined_method tenv pname =
PatternMatch.check_current_class_attributes
Annotations.ia_is_thread_confined tenv pname
(* we don't want to warn on methods that run on the UI thread because they should always be (* we don't want to warn on methods that run on the UI thread because they should always be
single-threaded *) single-threaded *)
let runs_on_ui_thread proc_desc = let runs_on_ui_thread proc_desc =
@ -268,7 +278,8 @@ let should_analyze_proc pdesc tenv =
not (FbThreadSafety.is_logging_method pn) && not (FbThreadSafety.is_logging_method pn) &&
not (is_call_to_builder_class_method pn) && not (is_call_to_builder_class_method pn) &&
not (is_call_to_immutable_collection_method tenv pn) && not (is_call_to_immutable_collection_method tenv pn) &&
not (runs_on_ui_thread pdesc) not (runs_on_ui_thread pdesc) &&
not (is_thread_confined_method tenv pn)
(* return true if we should report on unprotected accesses during the procedure *) (* return true if we should report on unprotected accesses during the procedure *)
let should_report_on_proc (_, tenv, proc_name, proc_desc) = let should_report_on_proc (_, tenv, proc_name, proc_desc) =

@ -116,12 +116,11 @@ let guarded_by = "GuardedBy"
let thread_safe = "ThreadSafe" let thread_safe = "ThreadSafe"
let not_thread_safe = "NotThreadSafe" let not_thread_safe = "NotThreadSafe"
let ui_thread = "UiThread" let ui_thread = "UiThread"
let thread_confined = "ThreadConfined"
let ia_is_not_thread_safe ia = let ia_is_not_thread_safe ia =
ia_ends_with ia not_thread_safe ia_ends_with ia not_thread_safe
(* we don't want it to just end with ThreadSafe, because this falls
foul of the @NotThreadSafe annotation *)
let ia_is_thread_safe ia = let ia_is_thread_safe ia =
ia_ends_with ia thread_safe ia_ends_with ia thread_safe
@ -218,6 +217,9 @@ let ia_is_guarded_by ia =
let ia_is_ui_thread ia = let ia_is_ui_thread ia =
ia_ends_with ia ui_thread ia_ends_with ia ui_thread
let ia_is_thread_confined ia =
ia_ends_with ia thread_confined
type annotation = type annotation =
| Nullable | Nullable
| Present | Present

@ -97,6 +97,7 @@ val ia_is_guarded_by : Annot.Item.t -> bool
val ia_is_not_thread_safe : Annot.Item.t -> bool val ia_is_not_thread_safe : Annot.Item.t -> bool
val ia_is_thread_safe : Annot.Item.t -> bool val ia_is_thread_safe : Annot.Item.t -> bool
val ia_is_ui_thread : Annot.Item.t -> bool val ia_is_ui_thread : Annot.Item.t -> bool
val ia_is_thread_confined : Annot.Item.t -> bool
val ia_iter : (Annot.t -> unit) -> Annot.Item.t -> unit val ia_iter : (Annot.t -> unit) -> Annot.Item.t -> unit

@ -11,6 +11,8 @@ package codetoanalyze.java.checkers;
import android.support.annotation.UiThread; 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 */ /** tests for classes and method annotations that are meaningful w.r.t thread-safety */
@ThreadSafe @ThreadSafe
@ -35,4 +37,19 @@ class Annotations {
this.f = new Object(); this.f = new Object();
} }
Confined con;
public void confinedCaller(){
con.foo();
}
@ThreadConfined
class Confined {
Integer x;
void foo(){
x = 22;
}
}
} }

Loading…
Cancel
Save