You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
577 lines
12 KiB
577 lines
12 KiB
9 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
|
|
||
|
package codetoanalyze.java.infer;
|
||
|
|
||
8 years ago
|
import android.annotation.SuppressLint;
|
||
6 years ago
|
import com.google.common.annotations.VisibleForTesting;
|
||
|
import java.io.Closeable;
|
||
8 years ago
|
import java.util.concurrent.locks.Lock;
|
||
8 years ago
|
import java.util.concurrent.locks.ReadWriteLock;
|
||
8 years ago
|
import java.util.concurrent.locks.ReentrantLock;
|
||
8 years ago
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||
6 years ago
|
import javax.annotation.concurrent.GuardedBy;
|
||
9 years ago
|
|
||
|
public class GuardedByExample {
|
||
|
|
||
9 years ago
|
static class AutoCloseableReadWriteUpdateLock implements Closeable {
|
||
6 years ago
|
@Override
|
||
|
public void close() {}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
private Object mLock = new Object();
|
||
|
|
||
|
private Object mOtherLock = new Object();
|
||
|
|
||
9 years ago
|
private AutoCloseableReadWriteUpdateLock mReadWriteLock = new AutoCloseableReadWriteUpdateLock();
|
||
|
|
||
9 years ago
|
@GuardedBy("mLock")
|
||
9 years ago
|
private Object f = new Object();
|
||
9 years ago
|
|
||
|
@GuardedBy("this")
|
||
|
Object g = new Object();
|
||
|
|
||
|
Object mCopyOfG;
|
||
|
|
||
|
@GuardedBy("SomeLockThatDoesntExist")
|
||
|
Object h = new Object();
|
||
|
|
||
9 years ago
|
@GuardedBy("mReadWriteLock")
|
||
|
Object i = new Object();
|
||
|
|
||
9 years ago
|
private static Object sLock = new Object();
|
||
|
|
||
|
@GuardedBy("sLock")
|
||
|
static Object sFld;
|
||
|
|
||
9 years ago
|
@GuardedBy("GuardedByExample.class")
|
||
|
static Object sGuardedByClass;
|
||
|
|
||
9 years ago
|
static {
|
||
|
// don't warn on class initializer
|
||
|
sFld = new Object();
|
||
|
}
|
||
|
|
||
|
public GuardedByExample() {
|
||
|
// don't warn on reads or writes of Guarded fields in constructor
|
||
|
f.toString();
|
||
|
g = new Object();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void readFBad() {
|
||
|
this.f.toString();
|
||
|
}
|
||
|
|
||
8 years ago
|
@SuppressLint("InvalidAccessToGuardedField")
|
||
|
void readFBadButSuppressed() {
|
||
|
this.f.toString();
|
||
|
}
|
||
|
|
||
|
@SuppressLint("SomeOtherWarning")
|
||
|
void readFBadButSuppressedOther() {
|
||
|
this.f.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
void writeFBad() {
|
||
|
this.f = new Object();
|
||
|
}
|
||
|
|
||
9 years ago
|
void readFBadWrongLock() {
|
||
|
synchronized (mOtherLock) {
|
||
|
this.f.toString(); // f is supposed to be protected by mLock
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void writeFBadWrongLock() {
|
||
|
synchronized (mOtherLock) {
|
||
|
this.f = new Object(); // f is supposed to be protected by mLock
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void readFAfterBlockBad() {
|
||
|
synchronized (mLock) {
|
||
|
}
|
||
|
this.f.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
void writeFAfterBlockBad() {
|
||
|
synchronized (mLock) {
|
||
|
}
|
||
|
this.f = new Object();
|
||
|
}
|
||
|
|
||
9 years ago
|
@GuardedBy("mOtherLock")
|
||
|
void readFBadWrongAnnotation() {
|
||
|
this.f.toString();
|
||
|
}
|
||
|
|
||
|
@GuardedBy("mLock")
|
||
|
void readFOkMethodAnnotated() {
|
||
|
this.f.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
synchronized void synchronizedMethodReadOk() {
|
||
9 years ago
|
this.g.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
synchronized void synchronizedMethodWriteOk() {
|
||
|
this.g = new Object();
|
||
|
}
|
||
|
|
||
9 years ago
|
void readFOkSynchronized() {
|
||
|
synchronized (mLock) {
|
||
|
this.f.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void writeFOkSynchronized() {
|
||
|
synchronized (mLock) {
|
||
|
this.f = new Object();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
synchronized void synchronizedMethodReadBad() {
|
||
9 years ago
|
this.f.toString(); // f is supposed to be protected by mLock, not this
|
||
|
}
|
||
|
|
||
9 years ago
|
synchronized void synchronizedMethodWriteBad() {
|
||
|
this.f = new Object(); // f is supposed to be protected by mLock, not this
|
||
|
}
|
||
|
|
||
|
void reassignCopyOk() {
|
||
|
synchronized (this) {
|
||
6 years ago
|
mCopyOfG = g; // these are ok: access of g guarded by this
|
||
9 years ago
|
}
|
||
|
mCopyOfG = new Object(); // ok; this doesn't change the value of g
|
||
|
}
|
||
|
|
||
9 years ago
|
void readHBad() {
|
||
|
synchronized (mLock) { // h is not protected by mLock
|
||
|
this.h.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
synchronized void readHBadSynchronizedMethodShouldntHelp() {
|
||
|
this.h.toString(); // h is not protected by this
|
||
|
}
|
||
|
|
||
9 years ago
|
private void privateUnguardedAccess() {
|
||
|
// not protected, but safe if all call sites guard the access to f
|
||
|
this.g.toString();
|
||
|
}
|
||
|
|
||
|
public void guardedCallSite1() {
|
||
|
synchronized (this) {
|
||
|
privateUnguardedAccess(); // should not warn; lock is held
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public synchronized void guardedCallSite2() {
|
||
|
privateUnguardedAccess(); // should not warn; lock is held
|
||
|
}
|
||
|
|
||
|
private void wrapper() {
|
||
|
privateUnguardedAccess(); // should not warn, just propagate the proof obl
|
||
|
}
|
||
|
|
||
|
public void guardedCallSite3() {
|
||
|
synchronized (this) {
|
||
|
wrapper(); // should not warn
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void readWriteLockOk() {
|
||
|
try (AutoCloseableReadWriteUpdateLock lock = mReadWriteLock) {
|
||
|
this.i.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
static synchronized void staticSynchronizedOk() {
|
||
9 years ago
|
sGuardedByClass.toString();
|
||
|
}
|
||
|
|
||
8 years ago
|
static void synchronizeOnClassOk1() {
|
||
6 years ago
|
synchronized (GuardedByExample.class) {
|
||
9 years ago
|
sGuardedByClass.toString(); // should not warn here
|
||
8 years ago
|
sGuardedByClass = new Object(); // or here
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void synchronizedOnThisBad() {
|
||
|
sGuardedByClass.toString();
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
Object dontReportOnCompilerGenerated() {
|
||
|
return new Object() {
|
||
|
public void accessInAnonClassOk() {
|
||
|
synchronized (mLock) {
|
||
|
f.toString();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
Object readFromInnerClassOkOuter() {
|
||
|
return new Object() {
|
||
|
public String readFromInnerClassOk() {
|
||
|
synchronized (GuardedByExample.this) {
|
||
|
return g.toString();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
Object readFromInnerClassBad1Outer() {
|
||
|
return new Object() {
|
||
|
public String readFromInnerClassBad1() {
|
||
|
synchronized (this) {
|
||
|
return g.toString(); // g is guarded by the outer class this, not this$0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
Object readFromInnerClassBad2Outer() {
|
||
|
return new Object() {
|
||
|
public synchronized String readFromInnerClassBad2() {
|
||
|
return g.toString(); // g is guarded by the outer class this, not this$0
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
9 years ago
|
@VisibleForTesting
|
||
|
public void visibleForTestingOk1() {
|
||
|
f.toString(); // should push proof obl to caller
|
||
|
}
|
||
|
|
||
|
@VisibleForTesting
|
||
|
void visibleForTestingOk2() {
|
||
|
f.toString(); // should push proof obl to caller
|
||
|
}
|
||
|
|
||
8 years ago
|
synchronized Object returnPtG() {
|
||
|
return g;
|
||
|
}
|
||
|
|
||
|
// note: this test should raise an error under "by value" GuardedBy semantics, but not under
|
||
|
// "by reference" GuardedBy semantics
|
||
|
void readGFromCopyOk() {
|
||
|
synchronized (this) {
|
||
6 years ago
|
mCopyOfG = g; // these are ok: access of g guarded by this
|
||
8 years ago
|
g.toString();
|
||
|
}
|
||
6 years ago
|
mCopyOfG.toString(); // should be an error; unprotected access to pt(g)
|
||
8 years ago
|
}
|
||
|
|
||
|
// another "by reference" vs "by value" test. buggy in "by value", but safe in "by reference"
|
||
|
void usePtG() {
|
||
|
Object ptG = returnPtG();
|
||
|
ptG.toString();
|
||
|
}
|
||
|
|
||
|
Object byRefTrickyBad() {
|
||
|
Object local = null;
|
||
6 years ago
|
synchronized (this) {
|
||
8 years ago
|
local = g; // we have a local pointer... to pt(G)
|
||
|
}
|
||
|
g.toString(); // ...but unsafe access is through g!
|
||
|
return local;
|
||
|
}
|
||
|
|
||
|
void byRefTrickyOk() {
|
||
|
Object local = null;
|
||
6 years ago
|
synchronized (this) {
|
||
8 years ago
|
local = g; // we have a local pointer... to pt(G)
|
||
|
}
|
||
|
local.toString(); // ...but unsafe access is through g!
|
||
|
}
|
||
|
|
||
8 years ago
|
@GuardedBy("ui_thread")
|
||
|
Object uiThread1;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("ui-thread")
|
||
|
Object uiThread2;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("uithread")
|
||
|
Object uiThread3;
|
||
|
|
||
|
@GuardedBy("something that's clearly not an expression")
|
||
|
Object nonExpression;
|
||
|
|
||
|
// tests for not reporting false alarms on unrecognized GuardedBy strings
|
||
|
void accessUnrecognizedGuardedByFieldsOk() {
|
||
|
uiThread1 = new Object();
|
||
|
uiThread1.toString();
|
||
|
uiThread2 = new Object();
|
||
|
uiThread2.toString();
|
||
|
uiThread3 = new Object();
|
||
|
uiThread3.toString();
|
||
|
nonExpression = new Object();
|
||
|
nonExpression.toString();
|
||
|
}
|
||
|
|
||
8 years ago
|
// outer class this tests
|
||
|
@GuardedBy("GuardedByExample.this")
|
||
|
Object guardedByOuterThis;
|
||
|
|
||
|
synchronized void okOuterAccess() {
|
||
|
guardedByOuterThis = null;
|
||
|
}
|
||
|
|
||
|
// inner class this tests
|
||
|
private class Inner {
|
||
|
@GuardedBy("this")
|
||
|
Object guardedByInnerThis1;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("Inner.this")
|
||
|
Object guardedByInnerThis2;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("GuardedByExample$Inner.this")
|
||
|
Object guardedByInnerThis3;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("Inner.class")
|
||
|
Object guardedByInnerClass1;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("GuardedByExample.Inner.class")
|
||
|
Object guardedByInnerClass2;
|
||
6 years ago
|
|
||
8 years ago
|
@GuardedBy("GuardedByExample$Inner.class")
|
||
|
Object guardedByInnerClass3;
|
||
|
|
||
8 years ago
|
synchronized void okAccess1() {
|
||
|
guardedByInnerThis1 = null;
|
||
|
}
|
||
|
|
||
|
synchronized void okAccess2() {
|
||
|
guardedByInnerThis2 = null;
|
||
|
}
|
||
|
|
||
|
synchronized void okAccess3() {
|
||
|
guardedByInnerThis3 = null;
|
||
|
}
|
||
|
|
||
8 years ago
|
void okInnerClassGuard1() {
|
||
|
synchronized (Inner.class) {
|
||
|
guardedByInnerClass1 = new Object();
|
||
|
guardedByInnerClass2 = new Object();
|
||
|
guardedByInnerClass3 = new Object();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void okInnerClassGuard2() {
|
||
|
synchronized (GuardedByExample.Inner.class) {
|
||
|
guardedByInnerClass1 = new Object();
|
||
|
guardedByInnerClass2 = new Object();
|
||
|
guardedByInnerClass3 = new Object();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
// TODO: report on these cases
|
||
|
/*
|
||
|
public void unguardedCallSiteBad1() {
|
||
|
privateUnguardedAccess(); // should warn; lock is not held
|
||
|
}
|
||
|
|
||
|
protected void unguardedCallSiteBad2() {
|
||
|
privateUnguardedAccess(); // should warn; lock is not held
|
||
|
}
|
||
|
|
||
|
void unguardedCallSiteBad3() {
|
||
|
privateUnguardedAccess(); // should warn; lock is not held
|
||
|
}
|
||
|
*/
|
||
|
|
||
8 years ago
|
int n;
|
||
|
|
||
8 years ago
|
public void withloop2() {
|
||
8 years ago
|
synchronized (mLock) {
|
||
6 years ago
|
for (int i = 0; i <= n; i++) {
|
||
8 years ago
|
f = 42;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void withoutloop2() {
|
||
|
synchronized (mLock) {
|
||
|
f = 42;
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
@GuardedBy("self_reference")
|
||
|
Object self_reference;
|
||
8 years ago
|
|
||
6 years ago
|
void guardedBySelfReferenceOK() {
|
||
|
synchronized (self_reference) {
|
||
|
this.self_reference.toString();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// TODO: report on this case, or at least a version which writes
|
||
|
/*
|
||
6 years ago
|
void guardedBySelfReferenceBad() {
|
||
|
this.self_reference.toString();
|
||
|
}
|
||
|
*/
|
||
8 years ago
|
|
||
|
@GuardedBy("itself")
|
||
|
Object itself_fld;
|
||
|
|
||
6 years ago
|
void itselfOK() {
|
||
|
synchronized (itself_fld) {
|
||
|
this.itself_fld.toString();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// TODO: report on this case, or at least a version which writes
|
||
|
/*
|
||
6 years ago
|
void itselfBad() {
|
||
|
this.itself_fld.toString();
|
||
|
}
|
||
|
*/
|
||
8 years ago
|
|
||
|
ReadWriteLock mRWL;
|
||
|
|
||
|
@GuardedBy("mRWL")
|
||
|
Integer guardedbymRWL;
|
||
|
|
||
|
Integer someOtherInt;
|
||
|
|
||
|
void readLockOK() {
|
||
|
mRWL.readLock().lock();
|
||
|
someOtherInt = guardedbymRWL;
|
||
|
mRWL.readLock().unlock();
|
||
|
}
|
||
|
|
||
|
void writeLockOK() {
|
||
|
mRWL.writeLock().lock();
|
||
|
guardedbymRWL = 55;
|
||
|
mRWL.writeLock().unlock();
|
||
|
}
|
||
|
|
||
|
ReentrantReadWriteLock mRRWL;
|
||
|
|
||
|
@GuardedBy("mRRWL")
|
||
|
Integer guardedbymRRWL;
|
||
|
|
||
|
void reentrantReadLockOK() {
|
||
|
mRRWL.readLock().lock();
|
||
|
someOtherInt = guardedbymRRWL;
|
||
|
mRRWL.readLock().unlock();
|
||
|
}
|
||
|
|
||
|
void reentrantWriteLockOK() {
|
||
|
mRRWL.writeLock().lock();
|
||
|
guardedbymRRWL = 55;
|
||
|
mRRWL.writeLock().unlock();
|
||
|
}
|
||
|
|
||
|
// TODO: warn on misuse of read/write locks.
|
||
|
|
||
8 years ago
|
@GuardedBy("this")
|
||
|
Integer xForSub;
|
||
|
|
||
6 years ago
|
static class Sub extends GuardedByExample {
|
||
8 years ago
|
|
||
|
void goodSub1() {
|
||
6 years ago
|
synchronized (this) {
|
||
8 years ago
|
xForSub = 22;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
synchronized void goodSub2() {
|
||
|
xForSub = 22;
|
||
|
}
|
||
|
|
||
|
void badSub() {
|
||
|
xForSub = 22;
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
Lock normallock;
|
||
|
|
||
|
@GuardedBy("normallock")
|
||
|
Integer guardedbynl;
|
||
|
|
||
|
ReentrantLock reentrantlock;
|
||
|
|
||
|
@GuardedBy("reentrantlock")
|
||
|
Integer guardedbyrel;
|
||
|
|
||
|
void goodGuardedByNormalLock() {
|
||
|
normallock.lock();
|
||
|
guardedbynl = 22;
|
||
|
normallock.unlock();
|
||
|
}
|
||
|
|
||
|
void goodTryLockGuardedByNormalLock() {
|
||
|
if (normallock.tryLock()) {
|
||
|
guardedbynl = 22;
|
||
|
normallock.unlock();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void goodTryLockGuardedByReentrantLock() {
|
||
|
if (reentrantlock.tryLock()) {
|
||
|
guardedbyrel = 44;
|
||
|
reentrantlock.unlock();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
void badGuardedByNormalLock() {
|
||
8 years ago
|
guardedbynl = 22;
|
||
|
}
|
||
|
|
||
6 years ago
|
void badGuardedByReentrantLock() {
|
||
8 years ago
|
guardedbyrel = 44;
|
||
|
}
|
||
|
|
||
8 years ago
|
static class OtherClassWithLock {
|
||
|
ReentrantLock lock;
|
||
|
|
||
|
@GuardedBy("lock")
|
||
|
Object guardedByLock;
|
||
|
|
||
|
Object otherClassObject;
|
||
|
|
||
|
void guardedInSameClassOk() {
|
||
|
lock.lock();
|
||
|
guardedByLock = new Object();
|
||
|
lock.unlock();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@GuardedBy("OtherClassWithLock.lock")
|
||
|
Object guardedByLock1;
|
||
|
|
||
|
@GuardedBy("codetoanalyze.java.infer.GuardedByExample$OtherClassWithLock.lock")
|
||
|
Object guardedByLock2;
|
||
|
|
||
|
@GuardedBy("OtherClassWithLock.otherClassObject")
|
||
|
Object guardedByLock3;
|
||
|
|
||
|
OtherClassWithLock otherClass;
|
||
|
|
||
|
void guardedByTypeSyntaxOk1() {
|
||
|
otherClass.lock.lock();
|
||
|
guardedByLock1 = true;
|
||
|
guardedByLock2 = true;
|
||
|
otherClass.lock.unlock();
|
||
|
}
|
||
|
|
||
|
void guardedByTypeSyntaxOk2() {
|
||
|
synchronized (otherClass.otherClassObject) {
|
||
|
guardedByLock3 = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void guardedByTypeSyntaxBad() {
|
||
|
guardedByLock1 = true;
|
||
|
guardedByLock2 = true;
|
||
|
}
|
||
9 years ago
|
}
|