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"
135540765bScgyurgyik #include <stdint.h>
145540765bScgyurgyik 
155540765bScgyurgyik extern "C" int LLVMFuzzerTestTwoInputs(const uint8_t *data1, size_t size1,
165540765bScgyurgyik                                        const uint8_t *data2, size_t size2) {
175540765bScgyurgyik   // Verify each data source contains at least one character.
185540765bScgyurgyik   if (!size1 || !size2)
195540765bScgyurgyik     return 0;
205540765bScgyurgyik   // Verify that the final character is the null terminator.
215540765bScgyurgyik   if (data1[size1 - 1] != '\0' || data2[size2 - 1] != '\0')
225540765bScgyurgyik     return 0;
235540765bScgyurgyik 
245540765bScgyurgyik   const char *s1 = reinterpret_cast<const char *>(data1);
255540765bScgyurgyik   const char *s2 = reinterpret_cast<const char *>(data2);
265540765bScgyurgyik 
27*4ffe2b24Scgyurgyik   const size_t minimum_size = size1 < size2 ? size1 : size2;
285540765bScgyurgyik 
295540765bScgyurgyik   // Iterate through until either the minimum size is hit,
305540765bScgyurgyik   // a character is the null terminator, or the first set
315540765bScgyurgyik   // of differed bytes between s1 and s2 are found.
325540765bScgyurgyik   // No bytes following a null byte should be compared.
335540765bScgyurgyik   size_t i;
34*4ffe2b24Scgyurgyik   for (i = 0; i < minimum_size; ++i) {
35*4ffe2b24Scgyurgyik     if (!s1[i] || s1[i] != s2[i])
36*4ffe2b24Scgyurgyik       break;
37*4ffe2b24Scgyurgyik   }
385540765bScgyurgyik 
395540765bScgyurgyik   int expected_result = s1[i] - s2[i];
405540765bScgyurgyik   int actual_result = __llvm_libc::strcmp(s1, s2);
415540765bScgyurgyik 
425540765bScgyurgyik   // The expected result should be the difference between the first non-equal
435540765bScgyurgyik   // characters of s1 and s2. If all characters are equal, the expected result
445540765bScgyurgyik   // should be '\0' - '\0' = 0.
455540765bScgyurgyik   if (expected_result != actual_result)
465540765bScgyurgyik     __builtin_trap();
475540765bScgyurgyik 
485540765bScgyurgyik   // Verify reversed operands. This should be the negated value of the previous
495540765bScgyurgyik   // result, except of course if the previous result was zero.
505540765bScgyurgyik   expected_result = s2[i] - s1[i];
515540765bScgyurgyik   actual_result = __llvm_libc::strcmp(s2, s1);
525540765bScgyurgyik   if (expected_result != actual_result)
535540765bScgyurgyik     __builtin_trap();
545540765bScgyurgyik 
555540765bScgyurgyik   return 0;
565540765bScgyurgyik }
57