aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/com/code_intelligence/jazzer/autofuzz/FuzzTarget.java
blob: 9806bb3ce533079457c66ad565c3926d66638042 (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
154
155
// 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.autofuzz;

import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.utils.Utils;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class FuzzTarget {
  private static final long MAX_EXECUTIONS_WITHOUT_INVOCATION = 100;

  private static String methodReference;
  private static Method[] targetMethods;
  private static Map<Method, Class<?>[]> throwsDeclarations;
  private static long executionsSinceLastInvocation = 0;

  public static void fuzzerInitialize(String[] args) {
    if (args.length != 1 || !args[0].contains("::")) {
      System.err.println(
          "Expected the argument to --autofuzz to be a method reference (e.g. System.out::println)");
      System.exit(1);
    }
    methodReference = args[0];
    String[] parts = methodReference.split("::", 2);
    String className = parts[0];
    String methodNameAndOptionalDescriptor = parts[1];
    String methodName;
    String descriptor;
    int descriptorStart = methodNameAndOptionalDescriptor.indexOf('(');
    if (descriptorStart != -1) {
      methodName = methodNameAndOptionalDescriptor.substring(0, descriptorStart);
      // URL decode the descriptor to allow copy-pasting from javadoc links such as:
      // https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#valueOf(char%5B%5D)
      try {
        descriptor =
            URLDecoder.decode(methodNameAndOptionalDescriptor.substring(descriptorStart), "UTF-8");
      } catch (UnsupportedEncodingException e) {
        // UTF-8 is always supported.
        e.printStackTrace();
        System.exit(1);
        return;
      }
    } else {
      methodName = methodNameAndOptionalDescriptor;
      descriptor = null;
    }

    Class<?> targetClass;
    try {
      targetClass = Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
      System.err.printf(
          "Failed to find class %s for autofuzz, please ensure it is contained in the classpath "
              + "specified with --cp and specify the full package name%n",
          className);
      e.printStackTrace();
      System.exit(1);
      return;
    }

    targetMethods = Arrays.stream(targetClass.getMethods())
                        .filter(method
                            -> method.getName().equals(methodName)
                                && (descriptor == null
                                    || Utils.getReadableDescriptor(method).equals(descriptor)))
                        .toArray(Method[] ::new);
    if (targetMethods.length == 0) {
      if (descriptor == null) {
        System.err.printf("Failed to find accessible methods named %s in class %s for autofuzz.%n"
                + "Accessible methods:%n%s",
            methodName, className,
            Arrays.stream(targetClass.getMethods())
                .map(method
                    -> String.format(
                        "%s::%s", method.getDeclaringClass().getName(), method.getName()))
                .distinct()
                .collect(Collectors.joining(System.lineSeparator())));
      } else {
        System.err.printf("Failed to find accessible methods named %s in class %s for autofuzz.%n"
                + "Accessible methods with that name:%n%s",
            methodName, className,
            Arrays.stream(targetClass.getMethods())
                .filter(method -> method.getName().equals(methodName))
                .map(method
                    -> String.format("%s::%s%s", method.getDeclaringClass().getName(),
                        method.getName(), Utils.getReadableDescriptor(method)))
                .distinct()
                .collect(Collectors.joining(System.lineSeparator())));
      }
      System.exit(1);
    }
    throwsDeclarations =
        Arrays.stream(targetMethods)
            .collect(Collectors.toMap(method -> method, Method::getExceptionTypes));
  }

  public static void fuzzerTestOneInput(FuzzedDataProvider data) throws Throwable {
    Method targetMethod;
    if (targetMethods.length == 1) {
      targetMethod = targetMethods[0];
    } else {
      targetMethod = data.pickValue(targetMethods);
    }
    try {
      Meta.autofuzz(data, targetMethod);
      executionsSinceLastInvocation = 0;
    } catch (AutofuzzConstructionException e) {
      if (Meta.isDebug()) {
        e.printStackTrace();
      }
      // Ignore exceptions thrown while constructing the parameters for the target method. We can
      // only guess how to generate valid parameters and any exceptions thrown while doing so
      // are most likely on us. However, if this happens too often, Autofuzz got stuck and we should
      // let the user know.
      executionsSinceLastInvocation++;
      if (executionsSinceLastInvocation >= MAX_EXECUTIONS_WITHOUT_INVOCATION) {
        System.err.printf("Failed to generate valid arguments to '%s' in %d attempts; giving up%n",
            methodReference, executionsSinceLastInvocation);
        System.exit(1);
      }
    } catch (AutofuzzInvocationException e) {
      executionsSinceLastInvocation = 0;
      Throwable cause = e.getCause();
      Class<?> causeClass = cause.getClass();
      // Do not report exceptions declared to be thrown by the method under test.
      for (Class<?> declaredThrow : throwsDeclarations.get(targetMethod)) {
        if (declaredThrow.isAssignableFrom(causeClass)) {
          return;
        }
      }
      throw cause;
    } catch (Throwable t) {
      System.err.println("Unexpected exception encountered during autofuzz");
      t.printStackTrace();
      System.exit(1);
    }
  }
}