1//===-- Template to diff single-input-single-output functions ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
10#define LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
11
12#include "fuzzing/math/Compare.h"
13
14#include <stddef.h>
15#include <stdint.h>
16
17template <typename T> using StringInputSingleOutputFunc = T (*)(const char *);
18
19template <typename T>
20void StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,
21 StringInputSingleOutputFunc<T> func2,
22 const uint8_t *data, size_t size) {
23 if (size < sizeof(T))
24 return;
25
26 const char *x = reinterpret_cast<const char *>(data);
27
28 T result1 = func1(x);
29 T result2 = func2(x);
30
31 if (!ValuesEqual(result1, result2))
32 __builtin_trap();
33}
34
35template <typename T>
36using StringToNumberFunc = T (*)(const char *, char **, int);
37
38template <typename T>
39void StringToNumberOutputDiff(StringToNumberFunc<T> func1,
40 StringToNumberFunc<T> func2, const uint8_t *data,
41 size_t size) {
42 if (size < sizeof(T))
43 return;
44
45 const char *x = reinterpret_cast<const char *>(data + 1);
46 int base = data[0] % 36;
47 base = base + ((base == 0) ? 0 : 1);
48
49 char *outPtr1 = nullptr;
50 char *outPtr2 = nullptr;
51
52 T result1 = func1(x, &outPtr1, base);
53 T result2 = func2(x, &outPtr2, base);
54
55 if (!(ValuesEqual(result1, result2) && (*outPtr1 == *outPtr2)))
56 __builtin_trap();
57}
58
59#endif // LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
60

source code of libc/fuzzing/stdlib/StringParserOutputDiff.h