1e14a7ff7Scgyurgyik //===-- strstr_fuzz.cpp ---------------------------------------------------===//
2e14a7ff7Scgyurgyik //
3e14a7ff7Scgyurgyik // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e14a7ff7Scgyurgyik // See https://llvm.org/LICENSE.txt for license information.
5e14a7ff7Scgyurgyik // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e14a7ff7Scgyurgyik //
7e14a7ff7Scgyurgyik //===----------------------------------------------------------------------===//
8e14a7ff7Scgyurgyik ///
9e14a7ff7Scgyurgyik /// Fuzzing test for llvm-libc strstr implementation.
10e14a7ff7Scgyurgyik ///
11e14a7ff7Scgyurgyik //===----------------------------------------------------------------------===//
12e14a7ff7Scgyurgyik
13e14a7ff7Scgyurgyik #include "src/string/strlen.h"
14e14a7ff7Scgyurgyik #include "src/string/strstr.h"
15e14a7ff7Scgyurgyik #include <stddef.h>
16e14a7ff7Scgyurgyik #include <stdint.h>
17e14a7ff7Scgyurgyik
18e14a7ff7Scgyurgyik // Simple loop to compare two strings up to a size n.
simple_memcmp(const char * left,const char * right,size_t n)19e14a7ff7Scgyurgyik static int simple_memcmp(const char *left, const char *right, size_t n) {
20e14a7ff7Scgyurgyik for (; n && *left == *right; ++left, ++right, --n)
21e14a7ff7Scgyurgyik ;
22e14a7ff7Scgyurgyik return n ? *left - *right : 0;
23e14a7ff7Scgyurgyik }
24e14a7ff7Scgyurgyik
25e14a7ff7Scgyurgyik // The general structure is to take the value of the first byte, set size1 to
26e14a7ff7Scgyurgyik // that value, and add the null terminator. size2 will then contain the rest of
27e14a7ff7Scgyurgyik // the bytes in data.
28e14a7ff7Scgyurgyik // For example, with inputs (data={2, 6, 4, 8, 0}, size=5):
29e14a7ff7Scgyurgyik // size1: data[0] = 2
30e14a7ff7Scgyurgyik // data1: {2, 6} + '\0' = {2, 6, '\0'}
31e14a7ff7Scgyurgyik // size2: size - size1 = 3
32e14a7ff7Scgyurgyik // data2: {4, 8, '\0'}
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)33e14a7ff7Scgyurgyik extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34e14a7ff7Scgyurgyik // Verify the size is at least 1 and the data is null terminated.
35e14a7ff7Scgyurgyik if (!size || data[size - 1] != '\0')
36e14a7ff7Scgyurgyik return 0;
37e14a7ff7Scgyurgyik const size_t size1 = (data[0] <= size ? data[0] : size);
38e14a7ff7Scgyurgyik // The first size will always be at least 1 since
39e14a7ff7Scgyurgyik // we need to append the null terminator. The second size
40e14a7ff7Scgyurgyik // needs to be checked since it must also contain the null
41e14a7ff7Scgyurgyik // terminator.
42e14a7ff7Scgyurgyik if (size - size1 == 0)
43e14a7ff7Scgyurgyik return 0;
44e14a7ff7Scgyurgyik
45e14a7ff7Scgyurgyik // Copy the data into a new container.
46e14a7ff7Scgyurgyik uint8_t *container = new uint8_t[size1 + 1];
47e14a7ff7Scgyurgyik if (!container)
48e14a7ff7Scgyurgyik __builtin_trap();
49e14a7ff7Scgyurgyik
50e14a7ff7Scgyurgyik size_t i;
51e14a7ff7Scgyurgyik for (i = 0; i < size1; ++i)
52e14a7ff7Scgyurgyik container[i] = data[i];
53e14a7ff7Scgyurgyik container[size1] = '\0'; // Add null terminator to container.
54e14a7ff7Scgyurgyik
55e14a7ff7Scgyurgyik const char *needle = reinterpret_cast<const char *>(container);
56e14a7ff7Scgyurgyik const char *haystack = reinterpret_cast<const char *>(data + i);
57*b6bc9d72SGuillaume Chatelet const char *result = LIBC_NAMESPACE::strstr(haystack, needle);
58e14a7ff7Scgyurgyik
59e14a7ff7Scgyurgyik // A null terminator may exist earlier in each, so this needs to be recorded.
60*b6bc9d72SGuillaume Chatelet const size_t haystack_size = LIBC_NAMESPACE::strlen(haystack);
61*b6bc9d72SGuillaume Chatelet const size_t needle_size = LIBC_NAMESPACE::strlen(needle);
62e14a7ff7Scgyurgyik
63e14a7ff7Scgyurgyik if (result) {
64e14a7ff7Scgyurgyik // The needle is in the haystack.
65e14a7ff7Scgyurgyik // 1. Verify that the result matches the needle.
66e14a7ff7Scgyurgyik if (simple_memcmp(needle, result, needle_size) != 0)
67e14a7ff7Scgyurgyik __builtin_trap();
68e14a7ff7Scgyurgyik
69e14a7ff7Scgyurgyik const char *haystack_ptr = haystack;
70e14a7ff7Scgyurgyik // 2. Verify that the result is the first occurrence of the needle.
71e14a7ff7Scgyurgyik for (; haystack_ptr != result; ++haystack_ptr) {
72e14a7ff7Scgyurgyik if (simple_memcmp(needle, haystack_ptr, needle_size) == 0)
73e14a7ff7Scgyurgyik __builtin_trap(); // There was an earlier occurrence of the needle.
74e14a7ff7Scgyurgyik }
75e14a7ff7Scgyurgyik } else {
76e14a7ff7Scgyurgyik // No result was found. Verify that the needle doesn't exist within the
77e14a7ff7Scgyurgyik // haystack.
78e14a7ff7Scgyurgyik for (size_t i = 0; i + needle_size < haystack_size; ++i) {
79e14a7ff7Scgyurgyik if (simple_memcmp(needle, haystack + i, needle_size) == 0)
80e14a7ff7Scgyurgyik __builtin_trap(); // There was an earlier occurrence of the needle.
81e14a7ff7Scgyurgyik }
82e14a7ff7Scgyurgyik }
83e14a7ff7Scgyurgyik delete[] container;
84e14a7ff7Scgyurgyik return 0;
85e14a7ff7Scgyurgyik }
86