15540765bScgyurgyik //===-- strcmp_fuzz.cpp ---------------------------------------------------===//
25540765bScgyurgyik //
35540765bScgyurgyik // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45540765bScgyurgyik // See https://llvm.org/LICENSE.txt for license information.
55540765bScgyurgyik // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65540765bScgyurgyik //
75540765bScgyurgyik //===----------------------------------------------------------------------===//
85540765bScgyurgyik ///
95540765bScgyurgyik /// Fuzzing test for llvm-libc strcmp implementation.
105540765bScgyurgyik ///
115540765bScgyurgyik //===----------------------------------------------------------------------===//
125540765bScgyurgyik #include "src/string/strcmp.h"
13f1c67192SSimon Pilgrim #include <stddef.h>
145540765bScgyurgyik #include <stdint.h>
155540765bScgyurgyik 
16*b07feef8Scgyurgyik // The general structure is to take the value of the first byte, set size1 to
17*b07feef8Scgyurgyik // that value, and add the null terminator. size2 will then contain the rest of
18*b07feef8Scgyurgyik // the bytes in data.
19*b07feef8Scgyurgyik // For example, with inputs (data={2, 6, 4, 8, 0}, size=5):
20*b07feef8Scgyurgyik //         size1: data[0] = 2
21*b07feef8Scgyurgyik //         data1: {2, 6} + '\0' = {2, 6, '\0'}
22*b07feef8Scgyurgyik //         size2: size - size1 = 3
23*b07feef8Scgyurgyik //         data2: {4, 8, '\0'}
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)24*b07feef8Scgyurgyik extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
25*b07feef8Scgyurgyik   // Verify the size is at least 1 and the data is null terminated.
26*b07feef8Scgyurgyik   if (!size || data[size - 1] != '\0')
275540765bScgyurgyik     return 0;
28*b07feef8Scgyurgyik 
29*b07feef8Scgyurgyik   const size_t size1 = (data[0] <= size ? data[0] : size);
30*b07feef8Scgyurgyik   const size_t size2 = size - size1;
31*b07feef8Scgyurgyik 
32*b07feef8Scgyurgyik   // The first size will always be at least 1 since
33*b07feef8Scgyurgyik   // we need to append the null terminator. The second size
34*b07feef8Scgyurgyik   // needs to be checked since it must also contain the null
35*b07feef8Scgyurgyik   // terminator.
36*b07feef8Scgyurgyik   if (!size2)
375540765bScgyurgyik     return 0;
385540765bScgyurgyik 
39*b07feef8Scgyurgyik   // Copy the data into new containers.
40*b07feef8Scgyurgyik   // Add one to data1 for null terminator.
41*b07feef8Scgyurgyik   uint8_t *data1 = new uint8_t[size1 + 1];
42*b07feef8Scgyurgyik   uint8_t *data2 = new uint8_t[size2];
43*b07feef8Scgyurgyik   if (!data1 || !data2)
44*b07feef8Scgyurgyik     __builtin_trap();
45*b07feef8Scgyurgyik 
46*b07feef8Scgyurgyik   size_t i;
47*b07feef8Scgyurgyik   for (i = 0; i < size1; ++i)
48*b07feef8Scgyurgyik     data1[i] = data[i];
49*b07feef8Scgyurgyik   data1[size1] = '\0'; // Add null terminator to data1.
50*b07feef8Scgyurgyik 
51*b07feef8Scgyurgyik   for (size_t j = 0; j < size2; ++j)
52*b07feef8Scgyurgyik     data2[j] = data[i++];
53*b07feef8Scgyurgyik 
545540765bScgyurgyik   const char *s1 = reinterpret_cast<const char *>(data1);
555540765bScgyurgyik   const char *s2 = reinterpret_cast<const char *>(data2);
56*b07feef8Scgyurgyik   size_t k = 0;
57*b07feef8Scgyurgyik   // Iterate until a null terminator is hit or the character comparison is
58*b07feef8Scgyurgyik   // different.
59*b07feef8Scgyurgyik   while (s1[k] && s2[k] && s1[k] == s2[k])
60*b07feef8Scgyurgyik     ++k;
615540765bScgyurgyik 
62*b07feef8Scgyurgyik   const unsigned char ch1 = static_cast<unsigned char>(s1[k]);
63*b07feef8Scgyurgyik   const unsigned char ch2 = static_cast<unsigned char>(s2[k]);
645540765bScgyurgyik   // The expected result should be the difference between the first non-equal
655540765bScgyurgyik   // characters of s1 and s2. If all characters are equal, the expected result
665540765bScgyurgyik   // should be '\0' - '\0' = 0.
67*b07feef8Scgyurgyik   if (__llvm_libc::strcmp(s1, s2) != ch1 - ch2)
685540765bScgyurgyik     __builtin_trap();
695540765bScgyurgyik 
705540765bScgyurgyik   // Verify reversed operands. This should be the negated value of the previous
715540765bScgyurgyik   // result, except of course if the previous result was zero.
72*b07feef8Scgyurgyik   if (__llvm_libc::strcmp(s2, s1) != ch2 - ch1)
735540765bScgyurgyik     __builtin_trap();
745540765bScgyurgyik 
75*b07feef8Scgyurgyik   delete[] data1;
76*b07feef8Scgyurgyik   delete[] data2;
775540765bScgyurgyik   return 0;
785540765bScgyurgyik }
79