|
22 | 22 |
|
23 | 23 | package pascal.taie.analysis.pta.plugin.natives; |
24 | 24 |
|
| 25 | +import pascal.taie.analysis.pta.core.cs.context.Context; |
| 26 | +import pascal.taie.analysis.pta.core.cs.element.CSObj; |
| 27 | +import pascal.taie.analysis.pta.core.heap.Descriptor; |
| 28 | +import pascal.taie.analysis.pta.core.heap.Obj; |
25 | 29 | import pascal.taie.analysis.pta.core.solver.Solver; |
| 30 | +import pascal.taie.analysis.pta.plugin.util.AnalysisModelPlugin; |
26 | 31 | import pascal.taie.analysis.pta.plugin.util.IRModelPlugin; |
27 | 32 | import pascal.taie.analysis.pta.plugin.util.InvokeHandler; |
| 33 | +import pascal.taie.analysis.pta.pts.PointsToSet; |
28 | 34 | import pascal.taie.ir.exp.ArrayAccess; |
29 | 35 | import pascal.taie.ir.exp.CastExp; |
30 | 36 | import pascal.taie.ir.exp.Var; |
31 | 37 | import pascal.taie.ir.stmt.Cast; |
32 | | -import pascal.taie.ir.stmt.Copy; |
33 | 38 | import pascal.taie.ir.stmt.Invoke; |
34 | 39 | import pascal.taie.ir.stmt.LoadArray; |
35 | 40 | import pascal.taie.ir.stmt.Stmt; |
|
42 | 47 |
|
43 | 48 | import java.util.List; |
44 | 49 |
|
45 | | -public class ArrayModel extends IRModelPlugin { |
| 50 | +public class ArrayModel { |
46 | 51 |
|
47 | | - private final ClassType objType; |
| 52 | + private static final Descriptor COPY_OF_ARRAY_DESC = () -> "ArrayGeneratedByCopyOfModel"; |
48 | 53 |
|
49 | | - private final ArrayType objArrayType; |
| 54 | + public static class AnalysisModel extends AnalysisModelPlugin { |
50 | 55 |
|
51 | | - /** |
52 | | - * Counter for naming temporary variables. |
53 | | - */ |
54 | | - private int counter = 0; |
| 56 | + AnalysisModel(Solver solver) { |
| 57 | + super(solver); |
| 58 | + } |
55 | 59 |
|
56 | | - ArrayModel(Solver solver) { |
57 | | - super(solver); |
58 | | - objType = typeSystem.getClassType(ClassNames.OBJECT); |
59 | | - objArrayType = typeSystem.getArrayType(objType, 1); |
60 | | - } |
| 60 | + @Override |
| 61 | + public void onStart() { |
| 62 | + // Solver should ignore `Arrays.copyOf()` to avoid spurious flows merging from other |
| 63 | + // callsites, as in the `IRModelPlugin.onStart()`. |
| 64 | + handlers.keySet().forEach(solver::addIgnoredMethod); |
| 65 | + } |
61 | 66 |
|
62 | | - @InvokeHandler(signature = "<java.util.Arrays: java.lang.Object[] copyOf(java.lang.Object[],int)>") |
63 | | - public List<Stmt> arraysCopyOf(Invoke invoke) { |
64 | | - Var result = invoke.getResult(); |
65 | | - return result != null |
66 | | - ? List.of(new Copy(result, invoke.getInvokeExp().getArg(0))) |
67 | | - : List.of(); |
| 67 | + @InvokeHandler(signature = "<java.util.Arrays: java.lang.Object[] copyOf(java.lang.Object[],int)>", argIndexes = {0}) |
| 68 | + public void arraysCopyOf(Context context, Invoke invoke, PointsToSet from) { |
| 69 | + JMethod container = invoke.getContainer(); |
| 70 | + Var result = invoke.getResult(); |
| 71 | + if (result != null) { |
| 72 | + from.getObjects().forEach(csObj -> { |
| 73 | + // When the array object from the first argument is not functional, |
| 74 | + // create a new functional array |
| 75 | + if (!csObj.getObject().isFunctional()) { |
| 76 | + Type type = csObj.getObject().getType(); |
| 77 | + Obj newArray = heapModel.getMockObj(COPY_OF_ARRAY_DESC, invoke, type, container); |
| 78 | + CSObj csNewArray = csManager.getCSObj(context, newArray); |
| 79 | + solver.addVarPointsTo(context, result, csNewArray); |
| 80 | + } else { |
| 81 | + solver.addVarPointsTo(context, result, csObj); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
68 | 86 | } |
69 | 87 |
|
70 | | - @InvokeHandler(signature = "<java.lang.System: void arraycopy(java.lang.Object,int,java.lang.Object,int,int)>") |
71 | | - public List<Stmt> systemArraycopy(Invoke invoke) { |
72 | | - JMethod container = invoke.getContainer(); |
73 | | - Var src = getTempVar(container, "src", objArrayType); |
74 | | - Var dest = getTempVar(container, "dest", objArrayType); |
75 | | - Var temp = getTempVar(container, "temp", objType); |
76 | | - List<Var> args = invoke.getInvokeExp().getArgs(); |
77 | | - return List.of( |
78 | | - new Cast(src, new CastExp(args.get(0), objArrayType)), |
79 | | - new Cast(dest, new CastExp(args.get(2), objArrayType)), |
80 | | - new LoadArray(temp, new ArrayAccess(src, args.get(1))), |
81 | | - new StoreArray(new ArrayAccess(dest, args.get(3)), temp)); |
82 | | - } |
| 88 | + public static class IRModel extends IRModelPlugin { |
| 89 | + |
| 90 | + private final ClassType objType; |
| 91 | + |
| 92 | + private final ArrayType objArrayType; |
| 93 | + |
| 94 | + /** |
| 95 | + * Counter for naming temporary variables. |
| 96 | + */ |
| 97 | + private int counter = 0; |
| 98 | + |
| 99 | + IRModel(Solver solver) { |
| 100 | + super(solver); |
| 101 | + objType = typeSystem.getClassType(ClassNames.OBJECT); |
| 102 | + objArrayType = typeSystem.getArrayType(objType, 1); |
| 103 | + } |
| 104 | + |
| 105 | + @InvokeHandler(signature = "<java.lang.System: void arraycopy(java.lang.Object,int,java.lang.Object,int,int)>") |
| 106 | + public List<Stmt> systemArraycopy(Invoke invoke) { |
| 107 | + JMethod container = invoke.getContainer(); |
| 108 | + Var src = getTempVar(container, "src", objArrayType); |
| 109 | + Var dest = getTempVar(container, "dest", objArrayType); |
| 110 | + Var temp = getTempVar(container, "temp", objType); |
| 111 | + List<Var> args = invoke.getInvokeExp().getArgs(); |
| 112 | + return List.of( |
| 113 | + new Cast(src, new CastExp(args.get(0), objArrayType)), |
| 114 | + new Cast(dest, new CastExp(args.get(2), objArrayType)), |
| 115 | + new LoadArray(temp, new ArrayAccess(src, args.get(1))), |
| 116 | + new StoreArray(new ArrayAccess(dest, args.get(3)), temp)); |
| 117 | + } |
83 | 118 |
|
84 | | - private Var getTempVar(JMethod container, String name, Type type) { |
85 | | - String varName = "%native-arraycopy-" + name + counter++; |
86 | | - return new Var(container, varName, type, -1); |
| 119 | + private Var getTempVar(JMethod container, String name, Type type) { |
| 120 | + String varName = "%native-arraycopy-" + name + counter++; |
| 121 | + return new Var(container, varName, type, -1); |
| 122 | + } |
87 | 123 | } |
88 | 124 | } |
0 commit comments