summaryrefslogtreecommitdiffstats
path: root/tools/dexfuzz/src/dexfuzz/program/MutationSerializer.java
blob: 7f795176daf0b96d3a30ad65683a7617d4e9a019 (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
/*
 * 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;

import dexfuzz.Log;
import dexfuzz.program.mutators.CodeMutator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

/**
 * Responsible for serializing mutations, allowing replay of mutations, and searching
 * for a minimal set of mutations.
 */
public class MutationSerializer {
  public static String getMutationString(Mutation mutation) {
    StringBuilder builder = new StringBuilder();
    builder.append(mutation.mutatorClass.getCanonicalName()).append(" ");
    builder.append(mutation.mutatableCodeIdx).append(" ");
    builder.append(mutation.getString());
    return builder.toString();
  }

  public static void writeMutation(BufferedWriter writer, Mutation mutation) throws IOException {
    // Write out the common fields.
    writer.write(mutation.mutatorClass.getCanonicalName() + " "
        + mutation.mutatableCodeIdx + " ");

    // Use the mutation's own function to write out the rest of the fields.
    writer.write(mutation.getString() + "\n");
  }

  @SuppressWarnings("unchecked")
  public static Mutation readMutation(BufferedReader reader) throws IOException {
    String line = reader.readLine();
    String[] fields = null;
    if (line != null) {
      fields = line.split(" ");
    } else {
      Log.errorAndQuit("Could not read line during mutation loading.");
    }

    // Read the mutator's class name
    String mutatorClassName = fields[0];

    // Get the class for that mutator
    Class<? extends CodeMutator> mutatorClass = null;
    try {
      mutatorClass = (Class<? extends CodeMutator>) Class.forName(mutatorClassName);
    } catch (ClassNotFoundException e) {
      Log.errorAndQuit("Cannot find a mutator class called: " + mutatorClassName);
    }

    Mutation mutation = null;
    try {
      mutation = mutatorClass.newInstance().getNewMutation();
    } catch (InstantiationException e) {
      Log.errorAndQuit("Unable to instantiate " + mutatorClassName
          + " using default constructor.");
    } catch (IllegalAccessException e) {
      Log.errorAndQuit("Unable to access methods in " + mutatorClassName + ".");
    }

    if (mutation == null) {
      Log.errorAndQuit("Unable to get Mutation for Mutator: " + mutatorClassName);
    }

    // Populate the common fields of the mutation.
    mutation.mutatorClass = mutatorClass;
    // The Program must set this later, using the mutatable_code_idx
    //   into its list of MutatableCodes.
    mutation.mutatableCode = null;
    mutation.mutatableCodeIdx = Integer.parseInt(fields[1]);

    // Use the mutation's own method to read the rest of the fields.
    mutation.parseString(fields);

    return mutation;
  }
}