[thread-safety] add @AssumeThreadSafe annotation to assume thread-safety of a method without checking it

Reviewed By: peterogithub

Differential Revision: D4433402

fbshipit-source-id: 742ad51
master
Sam Blackshear 8 years ago committed by Facebook Github Bot
parent f4b1af6f91
commit 618e9c9338

@ -0,0 +1,23 @@
/*
* 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;
/** assume that a method is thread-safe without actually checking it (as opposed to @ThreadSafe,
which does check). useful for suppressing warnings involving benign races */
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.CLASS)
public @interface AssumeThreadSafe {
}

@ -315,6 +315,9 @@ let runs_on_ui_thread proc_desc =
(fun annot -> Annotations.ia_is_ui_thread annot || Annotations.ia_is_on_event annot)
proc_desc
let is_assumed_thread_safe pdesc =
is_annotated Annotations.ia_is_assume_thread_safe pdesc
(* 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 *)
(* note: in the future, we will want to analyze the procedures in all of these cases in order to
@ -326,7 +329,8 @@ let should_analyze_proc pdesc tenv =
not (is_call_to_builder_class_method pn) &&
not (is_call_to_immutable_collection_method tenv pn) &&
not (runs_on_ui_thread pdesc) &&
not (is_thread_confined_method tenv pdesc)
not (is_thread_confined_method tenv pdesc) &&
not (is_assumed_thread_safe pdesc)
(* return true if we should report on unprotected accesses during the procedure *)
let should_report_on_proc (_, tenv, proc_name, proc_desc) =

@ -123,6 +123,7 @@ let guarded_by = "GuardedBy"
let thread_safe = "ThreadSafe"
let thread_safe_method = "ThreadSafeMethod"
let not_thread_safe = "NotThreadSafe"
let assume_thread_safe = "AssumeThreadSafe"
let ui_thread = "UiThread"
let any_thread = "AnyThread"
let for_ui_thread = "ForUiThread"
@ -138,6 +139,9 @@ let ia_is_thread_safe ia =
let ia_is_thread_safe_method ia =
ia_ends_with ia thread_safe_method
let ia_is_assume_thread_safe ia =
ia_ends_with ia assume_thread_safe
let ia_is_nullable ia =
ia_ends_with ia nullable

@ -102,6 +102,7 @@ val ia_is_guarded_by : 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_method : Annot.Item.t -> bool
val ia_is_assume_thread_safe : Annot.Item.t -> bool
val ia_is_ui_thread : Annot.Item.t -> bool
val ia_is_thread_confined : Annot.Item.t -> bool

@ -18,6 +18,7 @@ import javax.annotation.concurrent.ThreadSafe;
import android.support.annotation.UiThread;
import com.facebook.infer.annotation.AssumeThreadSafe;
import com.facebook.infer.annotation.ThreadConfined;
/** tests for classes and method annotations that are meaningful w.r.t thread-safety */
@ -88,4 +89,9 @@ class Annotations {
this.f = new Object();
}
@AssumeThreadSafe
public void assumeThreadSafeOk() {
this.f = new Object();
}
}

Loading…
Cancel
Save