-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
1,503 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/safety/Safety.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone.safety; | ||
|
||
import org.checkerframework.errorprone.dataflow.analysis.AbstractValue; | ||
|
||
public enum Safety implements AbstractValue<Safety> { | ||
UNKNOWN() { | ||
@Override | ||
public Safety leastUpperBound(Safety other) { | ||
return other == SAFE ? this : other; | ||
} | ||
|
||
@Override | ||
public boolean allowsValueWith(Safety _valueSafety) { | ||
// No constraints when safety isn't specified | ||
return true; | ||
} | ||
}, | ||
DO_NOT_LOG() { | ||
@Override | ||
public Safety leastUpperBound(Safety _other) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean allowsValueWith(Safety _valueSafety) { | ||
// do-not-log on a parameter isn't meaningful for callers, only for the implementation | ||
return true; | ||
} | ||
}, | ||
UNSAFE() { | ||
@Override | ||
public Safety leastUpperBound(Safety other) { | ||
return other == DO_NOT_LOG ? other : this; | ||
} | ||
|
||
@Override | ||
public boolean allowsValueWith(Safety valueSafety) { | ||
// We allow safe data to be provided to an unsafe annotated parameter because that's safe, however | ||
// we should separately flag and prompt migration of such UnsafeArgs to SafeArg. | ||
return valueSafety != Safety.DO_NOT_LOG; | ||
} | ||
}, | ||
SAFE() { | ||
@Override | ||
public Safety leastUpperBound(Safety other) { | ||
return other; | ||
} | ||
|
||
@Override | ||
public boolean allowsValueWith(Safety valueSafety) { | ||
return valueSafety == Safety.UNKNOWN || valueSafety == Safety.SAFE; | ||
} | ||
}; | ||
|
||
public abstract boolean allowsValueWith(Safety valueSafety); | ||
|
||
public boolean allowsAll() { | ||
return this == UNKNOWN || this == DO_NOT_LOG; | ||
} | ||
|
||
/** | ||
* Merge Safety using {@link Safety#leastUpperBound(AbstractValue)} except that {@link Safety#UNKNOWN} assumes | ||
* no confidence, preferring the other type if data is available. | ||
* For example, casting from {@link Object} to a known-safe type should result in {@link Safety#SAFE}. | ||
*/ | ||
public static Safety mergeAssumingUnknownIsSame(Safety one, Safety two) { | ||
if (one == UNKNOWN) { | ||
return two; | ||
} | ||
if (two == UNKNOWN) { | ||
return one; | ||
} | ||
return one.leastUpperBound(two); | ||
} | ||
|
||
public static Safety mergeAssumingUnknownIsSame(Safety one, Safety two, Safety three) { | ||
Safety result = mergeAssumingUnknownIsSame(one, two); | ||
return mergeAssumingUnknownIsSame(result, three); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ine-error-prone/src/main/java/com/palantir/baseline/errorprone/safety/SafetyAnalysis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone.safety; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.dataflow.DataFlow; | ||
import com.palantir.baseline.errorprone.safety.SafetyPropagationTransfer.ClearVisitorState; | ||
import com.sun.source.util.TreePath; | ||
import com.sun.tools.javac.util.Context; | ||
|
||
public final class SafetyAnalysis { | ||
private static final Context.Key<SafetyPropagationTransfer> SAFETY_PROPAGATION = new Context.Key<>(); | ||
|
||
/** | ||
* Returns the safety of the item at the current path. | ||
* Callers may need to use {@link VisitorState#withPath(TreePath)} to provide a more specific path. | ||
*/ | ||
public static Safety of(VisitorState state) { | ||
SafetyPropagationTransfer propagation = instance(state.context); | ||
try (ClearVisitorState ignored = propagation.setVisitorState(state)) { | ||
return DataFlow.expressionDataflow(state.getPath(), state.context, propagation); | ||
} | ||
} | ||
|
||
private static SafetyPropagationTransfer instance(Context context) { | ||
SafetyPropagationTransfer instance = context.get(SAFETY_PROPAGATION); | ||
if (instance == null) { | ||
instance = new SafetyPropagationTransfer(); | ||
context.put(SAFETY_PROPAGATION, instance); | ||
} | ||
return instance; | ||
} | ||
|
||
private SafetyAnalysis() {} | ||
} |
Oops, something went wrong.