|
| 1 | +/** |
| 2 | + * @name Safe publication |
| 3 | + * @kind problem |
| 4 | + * @problem.severity error |
| 5 | + * @id java/safe-publication |
| 6 | + */ |
| 7 | + |
| 8 | +import java |
| 9 | +import semmle.code.java.ConflictingAccess |
| 10 | + |
| 11 | +/** |
| 12 | + * Holds if `v` should be the default value for the field `f`. |
| 13 | + * That is, `v` is an initial (or constructor) assignment of `f`. |
| 14 | + */ |
| 15 | +predicate shouldBeDefaultValueFor(Field f, Expr v) { |
| 16 | + v = f.getAnAssignedValue() and |
| 17 | + ( |
| 18 | + v = f.getInitializer() |
| 19 | + or |
| 20 | + v.getEnclosingCallable() = f.getDeclaringType().getAConstructor() |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Gets the default value for the field `f`. |
| 26 | + * See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html |
| 27 | + * for the default values of the primitive types. |
| 28 | + * The default value for non-primitive types is null. |
| 29 | + */ |
| 30 | +bindingset[result] |
| 31 | +Expr getDefaultValue(Field f) { |
| 32 | + f.getType().hasName("byte") and result.(IntegerLiteral).getIntValue() = 0 |
| 33 | + or |
| 34 | + f.getType().hasName("short") and result.(IntegerLiteral).getIntValue() = 0 |
| 35 | + or |
| 36 | + f.getType().hasName("int") and result.(IntegerLiteral).getIntValue() = 0 |
| 37 | + or |
| 38 | + f.getType().hasName("long") and |
| 39 | + ( |
| 40 | + result.(LongLiteral).getValue() = "0" or |
| 41 | + result.(IntegerLiteral).getValue() = "0" |
| 42 | + ) |
| 43 | + or |
| 44 | + f.getType().hasName("float") and result.(FloatLiteral).getValue() = "0.0" |
| 45 | + or |
| 46 | + f.getType().hasName("double") and result.(DoubleLiteral).getValue() = "0.0" |
| 47 | + or |
| 48 | + f.getType().hasName("char") and result.(CharacterLiteral).getCodePointValue() = 0 |
| 49 | + or |
| 50 | + f.getType().hasName("boolean") and result.(BooleanLiteral).getBooleanValue() = false |
| 51 | + or |
| 52 | + not f.getType().getName() in [ |
| 53 | + "byte", "short", "int", "long", "float", "double", "char", "boolean" |
| 54 | + ] and |
| 55 | + result instanceof NullLiteral |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Holds if all constructor or initial assignments (if any) are to the default value. |
| 60 | + * That is, assignments by the declaration: |
| 61 | + * int x = 0; OK |
| 62 | + * int x = 3; not OK |
| 63 | + * or inside a constructor: |
| 64 | + * public c(a) { |
| 65 | + * x = 0; OK |
| 66 | + * x = 3; not OK |
| 67 | + * x = a; not OK |
| 68 | + * } |
| 69 | + */ |
| 70 | +predicate isAssignedDefaultValue(Field f) { |
| 71 | + forall(Expr v | shouldBeDefaultValueFor(f, v) | v = getDefaultValue(f)) |
| 72 | +} |
| 73 | + |
| 74 | +predicate isSafelyPublished(Field f) { |
| 75 | + f.isFinal() or // TODO: Consider non-primitive types |
| 76 | + f.isStatic() or |
| 77 | + f.isVolatile() or |
| 78 | + isThreadSafeType(f.getType()) or |
| 79 | + isThreadSafeType(f.getInitializer().getType()) or |
| 80 | + isAssignedDefaultValue(f) |
| 81 | +} |
| 82 | + |
| 83 | +from Field f, ClassAnnotatedAsThreadSafe c |
| 84 | +where |
| 85 | + f = c.getAField() and |
| 86 | + not isSafelyPublished(f) |
| 87 | +select f, "The class $@ is marked as thread-safe, but this field is not safely published.", c, |
| 88 | + c.getName() |
0 commit comments