[thread-safety] support assertOnBackgroundThread and similar

Summary: Handling the utility functions for asserting that we're on background thread.

Reviewed By: jberdine

Differential Revision: D5863435

fbshipit-source-id: 3ad95b5
master
Sam Blackshear 7 years ago committed by Facebook Github Bot
parent 1e605bdd94
commit ca51c72f3a

@ -37,7 +37,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
type lock_model = Lock | Unlock | LockedIfTrue | NoEffect type lock_model = Lock | Unlock | LockedIfTrue | NoEffect
type thread_model = MainThread | MainThreadIfTrue | Unknown type thread_model = BackgroundThread | MainThread | MainThreadIfTrue | UnknownThread
type container_access_model = ContainerRead | ContainerWrite type container_access_model = ContainerRead | ContainerWrite
@ -119,20 +119,18 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
-> NoEffect -> NoEffect
let get_thread_model = function let get_thread_model = function
| Typ.Procname.Java java_pname | Typ.Procname.Java java_pname when is_thread_utils_type java_pname -> (
-> if is_thread_utils_type java_pname then match Typ.Procname.java_get_method java_pname with
match Typ.Procname.java_get_method java_pname with | "assertMainThread" | "assertOnUiThread" | "checkOnMainThread"
| "assertMainThread" | "checkOnMainThread" | "assertOnUiThread" -> MainThread
-> MainThread | "isMainThread" | "isUiThread"
| "isMainThread" | "isUiThread" -> MainThreadIfTrue
-> MainThreadIfTrue | "assertOnBackgroundThread" | "assertOnNonUiThread" | "checkOnNonUiThread"
| _ -> BackgroundThread
-> Unknown | _
else Unknown -> UnknownThread )
(* TODO: we can model this now *)
(* Note we are not modelling assertOnNonUiThread or assertOnBackgroundThread. These treated as Unknown *)
| _ | _
-> Unknown -> UnknownThread
let get_container_access = let get_container_access =
let is_cpp_container_read = let is_cpp_container_read =
@ -608,6 +606,8 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
let astate = {astate with accesses} in let astate = {astate with accesses} in
let astate = let astate =
match get_thread_model callee_pname with match get_thread_model callee_pname with
| BackgroundThread
-> {astate with threads= ThreadsDomain.Background}
| MainThread | MainThread
-> {astate with threads= ThreadsDomain.Main} -> {astate with threads= ThreadsDomain.Main}
| MainThreadIfTrue -> ( | MainThreadIfTrue -> (
@ -622,7 +622,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
-> L.(die InternalError) -> L.(die InternalError)
"Procedure %a specified as returning boolean, but returns nothing" "Procedure %a specified as returning boolean, but returns nothing"
Typ.Procname.pp callee_pname ) Typ.Procname.pp callee_pname )
| Unknown | UnknownThread
-> astate -> astate
in in
let astate_callee = let astate_callee =

@ -20,6 +20,7 @@ class OurThreadUtils{
class OurThreadUtil{ /*This is like AndroidThreadUtil*/ class OurThreadUtil{ /*This is like AndroidThreadUtil*/
native static boolean isUiThread(); native static boolean isUiThread();
static void assertOnUiThread(){} static void assertOnUiThread(){}
static void assertOnBackgroundThread(){}
} }
@ -186,4 +187,16 @@ class RaceWithMainThread{
} }
} }
Object mFld;
public void confusedAssertBad(boolean b) {
if (b) {
OurThreadUtil.assertOnBackgroundThread();
} else {
OurThreadUtil.assertOnUiThread();
}
// not sure if we're on UI or background, should report
mFld = null;
}
} }

@ -75,6 +75,7 @@ codetoanalyze/java/threadsafety/Ownership.java, void Ownership.writeToOwnedInCal
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.conditional_isMainThread_ElseBranch_Bad(), 7, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.ff`] codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.conditional_isMainThread_ElseBranch_Bad(), 7, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.ff`]
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.conditional_isMainThread_Negation_Bad(), 3, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.ff`] codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.conditional_isMainThread_Negation_Bad(), 3, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.ff`]
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.conditional_isUiThread_ElseBranch_Bad(), 7, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.ff`] codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.conditional_isUiThread_ElseBranch_Bad(), 7, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.ff`]
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.confusedAssertBad(boolean), 7, THREAD_SAFETY_VIOLATION, [access to `codetoanalyze.java.checkers.RaceWithMainThread.mFld`]
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.readProtectedUnthreadedBad(), 3, THREAD_SAFETY_VIOLATION, [<Beginning of read trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`,<Beginning of write trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`] codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.readProtectedUnthreadedBad(), 3, THREAD_SAFETY_VIOLATION, [<Beginning of read trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`,<Beginning of write trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`]
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.read_unprotected_unthreaded1_Bad(), 2, THREAD_SAFETY_VIOLATION, [<Beginning of read trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f1`,<Beginning of write trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f1`] codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.read_unprotected_unthreaded1_Bad(), 2, THREAD_SAFETY_VIOLATION, [<Beginning of read trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f1`,<Beginning of write trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f1`]
codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.read_unprotected_unthreaded_Bad(), 2, THREAD_SAFETY_VIOLATION, [<Beginning of read trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`,<Beginning of write trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`] codetoanalyze/java/threadsafety/RaceWithMainThread.java, void RaceWithMainThread.read_unprotected_unthreaded_Bad(), 2, THREAD_SAFETY_VIOLATION, [<Beginning of read trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`,<Beginning of write trace>,access to `codetoanalyze.java.checkers.RaceWithMainThread.f`]

Loading…
Cancel
Save