summaryrefslogtreecommitdiffstats
path: root/tools/dexfuzz/src/dexfuzz/program/mutators/ConstantValueChanger.java
blob: 22f04e8b414a6e4e2f84f655b0b0b31fb63c8254 (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
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 dexfuzz.program.mutators;

import dexfuzz.Log;
import dexfuzz.MutationStats;
import dexfuzz.program.MInsn;
import dexfuzz.program.MutatableCode;
import dexfuzz.program.Mutation;
import dexfuzz.rawdex.formats.ContainsConst;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ConstantValueChanger extends CodeMutator {
  /**
   * Every CodeMutator has an AssociatedMutation, representing the
   * mutation that this CodeMutator can perform, to allow separate
   * generateMutation() and applyMutation() phases, allowing serialization.
   */
  public static class AssociatedMutation extends Mutation {
    public int constInsnIdx;
    public long newConstant;

    @Override
    public String getString() {
      StringBuilder builder = new StringBuilder();
      builder.append(constInsnIdx).append(" ");
      builder.append(newConstant);
      return builder.toString();
    }

    @Override
    public void parseString(String[] elements) {
      constInsnIdx = Integer.parseInt(elements[2]);
      newConstant = Long.parseLong(elements[3]);
    }
  }

  // The following two methods are here for the benefit of MutationSerializer,
  // so it can create a CodeMutator and get the correct associated Mutation, as it
  // reads in mutations from a dump of mutations.
  @Override
  public Mutation getNewMutation() {
    return new AssociatedMutation();
  }

  public ConstantValueChanger() { }

  public ConstantValueChanger(Random rng, MutationStats stats, List<Mutation> mutations) {
    super(rng, stats, mutations);
    likelihood = 70;
  }

  // A cache that should only exist between generateMutation() and applyMutation(),
  // or be created at the start of applyMutation(), if we're reading in mutations from
  // a file.
  private List<MInsn> constInsns = null;

  private void generateCachedConstInsns(MutatableCode mutatableCode) {
    if (constInsns != null) {
      return;
    }

    constInsns = new ArrayList<MInsn>();
    for (MInsn mInsn : mutatableCode.getInstructions()) {
      if (mInsn.insn.info.format instanceof ContainsConst) {
        constInsns.add(mInsn);
      }
    }
  }

  @Override
  protected boolean canMutate(MutatableCode mutatableCode) {
    for (MInsn mInsn : mutatableCode.getInstructions()) {
      if (mInsn.insn.info.format instanceof ContainsConst) {
        return true;
      }
    }

    Log.debug("Method contains no const instructions.");
    return false;
  }

  @Override
  protected Mutation generateMutation(MutatableCode mutatableCode) {
    generateCachedConstInsns(mutatableCode);

    // Pick a random const instruction.
    int constInsnIdx = rng.nextInt(constInsns.size());
    MInsn constInsn = constInsns.get(constInsnIdx);

    // Get the constant.
    long oldConstant = ((ContainsConst)constInsn.insn.info.format).getConst(constInsn.insn);

    long newConstant = oldConstant;

    // Make a new constant.
    while (newConstant == oldConstant) {
      newConstant = rng.nextLong()
          % ((ContainsConst)constInsn.insn.info.format).getConstRange();
    }

    AssociatedMutation mutation = new AssociatedMutation();
    mutation.setup(this.getClass(), mutatableCode);
    mutation.constInsnIdx = constInsnIdx;
    mutation.newConstant = newConstant;
    return mutation;
  }

  @Override
  protected void applyMutation(Mutation uncastMutation) {
    // Cast the Mutation to our AssociatedMutation, so we can access its fields.
    AssociatedMutation mutation = (AssociatedMutation) uncastMutation;
    MutatableCode mutatableCode = mutation.mutatableCode;

    generateCachedConstInsns(mutatableCode);

    MInsn constInsn = constInsns.get(mutation.constInsnIdx);

    long oldConstant = ((ContainsConst)constInsn.insn.info.format).getConst(constInsn.insn);

    Log.info("Changed constant value #" + oldConstant + " to #" + mutation.newConstant
        + " in " + constInsn);

    stats.incrementStat("Changed constant value");

    // Set the new constant.
    ((ContainsConst)constInsn.insn.info.format).setConst(constInsn.insn, mutation.newConstant);

    // Clear cache.
    constInsns = null;
  }
}