|
8 | 8 |
|
9 | 9 | /// \file
|
10 | 10 | /// Remove Instance-of Operators
|
| 11 | +/// |
| 12 | +/// Java has an operator called `instanceof`, which is used to check if an instance of a class is of a particular type. For example given that `A` and `B` are the identifiers of classes, which directly extend Object. If the following example java code is executed - |
| 13 | +/// ```java |
| 14 | +/// Object a = new A(); |
| 15 | +/// boolean x = a instanceof A; // true |
| 16 | +/// boolean y = a instanceof B; // false |
| 17 | +/// ``` |
| 18 | +/// Then, `x` will be set to `true` and `y` will be set to `false`. |
| 19 | +/// |
| 20 | +/// `instanceof` also works with inheritance. Given classes `C` and `D`, where `C` extends from `Object` and `D` extends `C`. If the following example java code is executed - |
| 21 | +/// ```java |
| 22 | +/// Object c = new C(); |
| 23 | +/// Object d = new D(); |
| 24 | +/// boolean b1 = c instanceof C; // true |
| 25 | +/// boolean b2 = c instanceof D; // false |
| 26 | +/// boolean b3 = d instanceof C; // true |
| 27 | +/// boolean b4 = d instanceof D; // true |
| 28 | +/// ``` |
| 29 | +/// Then, `b1` will be `true`, `b2` will be `false`, `b3` will be `true` and `b4` will be `true`. |
| 30 | +/// |
| 31 | +/// `instanceof` has its own instruction type in the java bytecode. We need to rewrite it in terms of other, more fundamental operations in order to analyse it. This operation is referred to as "lowering" or in this specific case - `remove_instanceof`. |
| 32 | +/// |
| 33 | +/// For a simple example where class `A` extends `Object` and no classes extend `A`, the expression `e instanceof A` needs to be lowered to an expression equivalent to `e == null ? false : e->@class_identifier == "A"`. The null case needs to be taken into account first, as we can't lookup the value of a `@class_identifier` field in a null pointer. Note that according to the Java specification, `null` is not an instance of any type. |
| 34 | +/// |
| 35 | +/// For an example with inheritance where class `D` extends `C` and `C` extends `Object`, then `e instanceof C` needs to be lowered to an expression equivalent to `e == null ? false : (e->@class_identifier == "C" || e->@class_identifier == "D")`. To lower `e instanceof C` correctly, we need to take into account each of the classes extends from `C`. Working out what classes extend a given class type is performed using an instance of the `class_hierarchyt` class. |
| 36 | +/// |
| 37 | +/// In terms of the goto program, the `instanceof` operator is identified in the expression tree, based on the `exprt.id()` being `ID_java_instance_of`. The left hand side of the operator must be a pointer and the right hand side must be a type. |
| 38 | +/// |
| 39 | +/// The lowered form has a refinement to help with null pointer analysis later on. Instead of dereferencing the pointer to get @class_identifier in each comparision, it is dereferenced a single time and the result is reused. This is easier to analyse for null pointers, because there is only a single dereference to consider. So the constructed program takes the form - |
| 40 | +/// ```cpp |
| 41 | +/// if(expr == null) |
| 42 | +/// instanceof_result = false; |
| 43 | +/// else |
| 44 | +/// string class_identifier_tmpX = expr->@class_identifier; |
| 45 | +/// instanceof_result = class_identifier_tmpX == "C" || class_identifier_tmpX == "D"; |
| 46 | +/// ``` |
| 47 | +/// Where the `X` in `class_identifier_tmp` is a number which makes the identifier unique. |
11 | 48 |
|
12 | 49 | #ifndef CPROVER_JAVA_BYTECODE_REMOVE_INSTANCEOF_H
|
13 | 50 | #define CPROVER_JAVA_BYTECODE_REMOVE_INSTANCEOF_H
|
|
0 commit comments