aboutsummaryrefslogtreecommitdiff
path: root/agent/src/test/java/com/code_intelligence/jazzer/instrumentor/TraceDataFlowInstrumentationTarget.java
blob: d8e28881e72d38b4a9c3bec44ba1f4c5160478d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2021 Code Intelligence GmbH
//
// 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.code_intelligence.jazzer.instrumentor;

import java.nio.ByteBuffer;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.Vector;

public class TraceDataFlowInstrumentationTarget implements DynamicTestContract {
  volatile long long1 = 1;
  volatile long long2 = 1;
  volatile long long3 = 2;
  volatile long long4 = 3;

  volatile int int1 = 4;
  volatile int int2 = 4;
  volatile int int3 = 6;
  volatile int int4 = 5;

  volatile int switchValue = 1200;

  @SuppressWarnings("ReturnValueIgnored")
  @Override
  public Map<String, Boolean> selfCheck() {
    Map<String, Boolean> results = new HashMap<>();

    results.put("longCompareEq", long1 == long2);
    results.put("longCompareNe", long3 != long4);

    results.put("intCompareEq", int1 == int2);
    results.put("intCompareNe", int3 != int4);
    results.put("intCompareLt", int4 < int3);
    results.put("intCompareLe", int4 <= int3);
    results.put("intCompareGt", int3 > int4);
    results.put("intCompareGe", int3 >= int4);

    // Not instrumented since all case values are non-negative and < 256.
    switch (switchValue) {
      case 119:
      case 120:
      case 121:
        results.put("tableSwitchUninstrumented", false);
        break;
      default:
        results.put("tableSwitchUninstrumented", true);
    }

    // Not instrumented since all case values are non-negative and < 256.
    switch (switchValue) {
      case 1:
      case 200:
        results.put("lookupSwitchUninstrumented", false);
        break;
      default:
        results.put("lookupSwitchUninstrumented", true);
    }

    results.put("emptySwitchUninstrumented", false);
    switch (switchValue) {
      default:
        results.put("emptySwitchUninstrumented", true);
    }

    switch (switchValue) {
      case 1000:
      case 1001:
        // case 1002: The tableswitch instruction will contain a gap case for 1002.
      case 1003:
        results.put("tableSwitch", false);
        break;
      default:
        results.put("tableSwitch", true);
    }

    switch (-switchValue) {
      case -1200:
        results.put("lookupSwitch", true);
        break;
      case -1:
      case -10:
      case -1000:
      case 200:
      default:
        results.put("lookupSwitch", false);
    }

    results.put("intDiv", (int3 / 2) == 3);

    results.put("longDiv", (long4 / 2) == 1);

    String[] referenceArray = {"foo", "foo", "bar"};
    boolean[] boolArray = {false, false, true};
    byte[] byteArray = {0, 0, 2};
    char[] charArray = {0, 0, 0, 3};
    double[] doubleArray = {0, 0, 0, 0, 4};
    float[] floatArray = {0, 0, 0, 0, 0, 5};
    int[] intArray = {0, 0, 0, 0, 0, 0, 6};
    long[] longArray = {0, 0, 0, 0, 0, 0, 0, 7};
    short[] shortArray = {0, 0, 0, 0, 0, 0, 0, 0, 8};

    results.put("referenceArrayGep", referenceArray[2].equals("bar"));
    results.put("boolArrayGep", boolArray[2]);
    results.put("byteArrayGep", byteArray[2] == 2);
    results.put("charArrayGep", charArray[3] == 3);
    results.put("doubleArrayGep", doubleArray[4] == 4);
    results.put("floatArrayGep", floatArray[5] == 5);
    results.put("intArrayGep", intArray[6] == 6);
    results.put("longArrayGep", longArray[7] == 7);
    results.put("shortArrayGep", shortArray[8] == 8);

    ByteBuffer buffer = ByteBuffer.allocate(100);
    buffer.get(2);
    buffer.getChar(3);
    buffer.getDouble(4);
    buffer.getFloat(5);
    buffer.getInt(6);
    buffer.getLong(7);
    buffer.getShort(8);

    "foobarbazbat".charAt(9);
    "foobarbazbat".codePointAt(10);
    new StringBuilder("foobarbazbat").charAt(11);

    (new Vector<>(Collections.nCopies(20, "foo"))).get(12);
    (new ArrayList<>(Collections.nCopies(20, "foo"))).get(13);
    Stack<String> stack = new Stack<>();
    for (int i = 0; i < 20; i++) stack.push("foo");
    stack.get(14);
    stack.get(15);
    ((AbstractList<String>) stack).get(16);
    ((List<String>) stack).get(17);

    return results;
  }
}