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

@ -20,6 +20,7 @@ class OurThreadUtils{
class OurThreadUtil{ /*This is like AndroidThreadUtil*/
native static boolean isUiThread();
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_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.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.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`]

Loading…
Cancel
Save