xref: /llvm-project/libc/fuzzing/stdlib/StringParserOutputDiff.h (revision f1990feb35e835ab81d6351dd4b6ef3dccc4aca5)
187c01607SMichael Jones //===-- Template to diff single-input-single-output functions ---*- C++ -*-===//
287c01607SMichael Jones //
387c01607SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
487c01607SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
587c01607SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
687c01607SMichael Jones //
787c01607SMichael Jones //===----------------------------------------------------------------------===//
887c01607SMichael Jones 
987c01607SMichael Jones #ifndef LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
1087c01607SMichael Jones #define LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
1187c01607SMichael Jones 
1287c01607SMichael Jones #include "fuzzing/math/Compare.h"
1387c01607SMichael Jones 
1487c01607SMichael Jones #include <stddef.h>
1587c01607SMichael Jones #include <stdint.h>
1687c01607SMichael Jones 
1787c01607SMichael Jones template <typename T> using StringInputSingleOutputFunc = T (*)(const char *);
1887c01607SMichael Jones 
1987c01607SMichael Jones template <typename T>
StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,StringInputSingleOutputFunc<T> func2,const uint8_t * data,size_t size)2087c01607SMichael Jones void StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,
2187c01607SMichael Jones                             StringInputSingleOutputFunc<T> func2,
2287c01607SMichael Jones                             const uint8_t *data, size_t size) {
2387c01607SMichael Jones   if (size < sizeof(T))
2487c01607SMichael Jones     return;
2587c01607SMichael Jones 
2687c01607SMichael Jones   const char *x = reinterpret_cast<const char *>(data);
2787c01607SMichael Jones 
2887c01607SMichael Jones   T result1 = func1(x);
2987c01607SMichael Jones   T result2 = func2(x);
3087c01607SMichael Jones 
3187c01607SMichael Jones   if (!ValuesEqual(result1, result2))
3287c01607SMichael Jones     __builtin_trap();
3387c01607SMichael Jones }
3487c01607SMichael Jones 
35*f1990febSMichael Jones template <typename T>
36*f1990febSMichael Jones using StringToNumberFunc = T (*)(const char *, char **, int);
37*f1990febSMichael Jones 
38*f1990febSMichael Jones template <typename T>
StringToNumberOutputDiff(StringToNumberFunc<T> func1,StringToNumberFunc<T> func2,const uint8_t * data,size_t size)39*f1990febSMichael Jones void StringToNumberOutputDiff(StringToNumberFunc<T> func1,
40*f1990febSMichael Jones                               StringToNumberFunc<T> func2, const uint8_t *data,
41*f1990febSMichael Jones                               size_t size) {
42*f1990febSMichael Jones   if (size < sizeof(T))
43*f1990febSMichael Jones     return;
44*f1990febSMichael Jones 
45*f1990febSMichael Jones   const char *x = reinterpret_cast<const char *>(data + 1);
46*f1990febSMichael Jones   int base = data[0] % 36;
47*f1990febSMichael Jones   base = base + ((base == 0) ? 0 : 1);
48*f1990febSMichael Jones 
49*f1990febSMichael Jones   char *outPtr1 = nullptr;
50*f1990febSMichael Jones   char *outPtr2 = nullptr;
51*f1990febSMichael Jones 
52*f1990febSMichael Jones   T result1 = func1(x, &outPtr1, base);
53*f1990febSMichael Jones   T result2 = func2(x, &outPtr2, base);
54*f1990febSMichael Jones 
55*f1990febSMichael Jones   if (!(ValuesEqual(result1, result2) && (*outPtr1 == *outPtr2)))
56*f1990febSMichael Jones     __builtin_trap();
57*f1990febSMichael Jones }
58*f1990febSMichael Jones 
5987c01607SMichael Jones #endif // LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
60