• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 import com.code_intelligence.jazzer.api.FuzzedDataProvider;
18 import java.util.Base64;
19 
20 public class ExampleValueProfileFuzzer {
base64(byte[] input)21   private static String base64(byte[] input) {
22     return Base64.getEncoder().encodeToString(input);
23   }
24 
insecureEncrypt(long input)25   private static long insecureEncrypt(long input) {
26     long key = 0xefe4eb93215cb6b0L;
27     return input ^ key;
28   }
29 
fuzzerTestOneInput(FuzzedDataProvider data)30   public static void fuzzerTestOneInput(FuzzedDataProvider data) {
31     // Without -use_value_profile=1, the fuzzer gets stuck here as there is no direct correspondence
32     // between the input bytes and the compared string. With value profile, the fuzzer can guess the
33     // expected input byte by byte, which takes linear rather than exponential time.
34     if (base64(data.consumeBytes(6)).equals("SmF6emVy")) {
35       long[] plaintextBlocks = data.consumeLongs(2);
36       if (plaintextBlocks.length != 2)
37         return;
38       if (insecureEncrypt(plaintextBlocks[0]) == 0x9fc48ee64d3dc090L) {
39         // Without --fake_pcs (enabled by default with -use_value_profile=1), the fuzzer would get
40         // stuck here as the value profile information for long comparisons would not be able to
41         // distinguish between this comparison and the one above.
42         if (insecureEncrypt(plaintextBlocks[1]) == 0x888a82ff483ad9c2L) {
43           mustNeverBeCalled();
44         }
45       }
46     }
47   }
48 
mustNeverBeCalled()49   private static void mustNeverBeCalled() {
50     throw new IllegalStateException("mustNeverBeCalled has been called");
51   }
52 }
53