1 // Copyright 2021 Code Intelligence GmbH
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 package com.code_intelligence.jazzer.sanitizers
16
17 import com.code_intelligence.jazzer.api.Jazzer
18 import java.io.InputStream
19
20 /**
21 * jaz.Zer is a honeypot class: All of its methods report a finding when called.
22 */
23 const val HONEYPOT_CLASS_NAME = "jaz.Zer"
24 const val HONEYPOT_LIBRARY_NAME = "jazzer_honeypot"
25
toBytesnull26 internal fun Short.toBytes(): ByteArray {
27 return byteArrayOf(
28 ((toInt() shr 8) and 0xFF).toByte(),
29 (toInt() and 0xFF).toByte(),
30 )
31 }
32
33 // Runtime is only O(size * needle.size), only use for small arrays.
indexOfnull34 internal fun ByteArray.indexOf(needle: ByteArray): Int {
35 outer@ for (i in 0 until size - needle.size + 1) {
36 for (j in needle.indices) {
37 if (this[i + j] != needle[j]) {
38 continue@outer
39 }
40 }
41 return i
42 }
43 return -1
44 }
45
guideMarkableInputStreamTowardsEqualitynull46 internal fun guideMarkableInputStreamTowardsEquality(stream: InputStream, target: ByteArray, id: Int) {
47 fun readBytes(stream: InputStream, size: Int): ByteArray {
48 val current = ByteArray(size)
49 var n = 0
50 while (n < size) {
51 val count = stream.read(current, n, size - n)
52 if (count < 0) break
53 n += count
54 }
55 return current
56 }
57
58 check(stream.markSupported())
59 stream.mark(target.size)
60 val current = readBytes(stream, target.size)
61 stream.reset()
62 Jazzer.guideTowardsEquality(current, target, id)
63 }
64