Skip to content

Commit f1990fe

Browse files
[libc] add fuzz target for strtointeger functions
The string to integer conversion functions are well suited to differential fuzzing, and this patch adds a target to enable just that. It also fixes a bug in the fuzzing comparison logic and changes atoi slightly to match the behavior described in the C standard. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D140178
1 parent 4c13af2 commit f1990fe

File tree

5 files changed

+145
-2
lines changed

5 files changed

+145
-2
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ function(add_libc_fuzzer target_name)
319319
"LIBC_FUZZER"
320320
"" # No optional arguments
321321
"" # Single value arguments
322-
"SRCS;HDRS;DEPENDS" # Multi-value arguments
322+
"SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi-value arguments
323323
${ARGN}
324324
)
325325
if(NOT LIBC_FUZZER_SRCS)
@@ -374,6 +374,11 @@ function(add_libc_fuzzer target_name)
374374
${fq_deps_list}
375375
)
376376
add_dependencies(libc-fuzzer ${fq_target_name})
377+
378+
target_compile_options(${fq_target_name}
379+
PRIVATE
380+
${LIBC_FUZZER_COMPILE_OPTIONS})
381+
377382
endfunction(add_libc_fuzzer)
378383

379384
# Rule to add an integration test. An integration test is like a unit test

libc/fuzzing/math/Compare.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ValuesEqual(T x1, T x2) {
2828
template <typename T>
2929
__llvm_libc::cpp::enable_if_t<__llvm_libc::cpp::is_integral_v<T>, bool>
3030
ValuesEqual(T x1, T x2) {
31-
return x1 == x1;
31+
return x1 == x2;
3232
}
3333

3434
#endif // LLVM_LIBC_FUZZING_MATH_COMPARE_H

libc/fuzzing/stdlib/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,36 @@ add_libc_fuzzer(
1616
libc.src.stdlib.atof
1717
)
1818

19+
add_libc_fuzzer(
20+
strtointeger_differential_fuzz
21+
SRCS
22+
strtointeger_differential_fuzz.cpp
23+
HDRS
24+
StringParserOutputDiff.h
25+
DEPENDS
26+
libc.src.stdlib.atoi
27+
libc.src.stdlib.atol
28+
libc.src.stdlib.atoll
29+
libc.src.stdlib.strtol
30+
libc.src.stdlib.strtoll
31+
libc.src.stdlib.strtoul
32+
libc.src.stdlib.strtoull
33+
)
34+
35+
add_libc_fuzzer(
36+
strtointeger_differential_fuzz_cleaner
37+
SRCS
38+
strtointeger_differential_fuzz.cpp
39+
HDRS
40+
StringParserOutputDiff.h
41+
DEPENDS
42+
libc.src.stdlib.atoi
43+
libc.src.stdlib.atol
44+
libc.src.stdlib.atoll
45+
libc.src.stdlib.strtol
46+
libc.src.stdlib.strtoll
47+
libc.src.stdlib.strtoul
48+
libc.src.stdlib.strtoull
49+
COMPILE_OPTIONS
50+
-DLLVM_LIBC_FUZZ_ATOI_CLEANER_INPUT
51+
)

libc/fuzzing/stdlib/StringParserOutputDiff.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,28 @@ void StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,
3232
__builtin_trap();
3333
}
3434

35+
template <typename T>
36+
using StringToNumberFunc = T (*)(const char *, char **, int);
37+
38+
template <typename T>
39+
void 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+
3559
#endif // LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===-- strtointeger_differential_fuzz.cpp --------------------------------===//
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+
/// Fuzzing test for llvm-libc atof implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
#include "src/stdlib/atoi.h"
13+
#include "src/stdlib/atol.h"
14+
#include "src/stdlib/atoll.h"
15+
#include "src/stdlib/strtol.h"
16+
#include "src/stdlib/strtoll.h"
17+
#include "src/stdlib/strtoul.h"
18+
#include "src/stdlib/strtoull.h"
19+
#include <stddef.h>
20+
#include <stdint.h>
21+
#include <stdlib.h>
22+
23+
#include "fuzzing/stdlib/StringParserOutputDiff.h"
24+
25+
// This list contains (almost) all character that can possibly be accepted by a
26+
// string to integer conversion. Those are: space, tab, +/- signs, any digit,
27+
// and any letter. Technically there are some space characters accepted by
28+
// isspace that aren't in this list, but since space characters are just skipped
29+
// over anyways I'm not really worried.
30+
[[maybe_unused]] constexpr char VALID_CHARS[] = {
31+
' ', '\t', '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
32+
'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G',
33+
'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N',
34+
'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U',
35+
'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z'};
36+
37+
// This takes the randomized bytes in data and interprets the first byte as the
38+
// base for the string to integer conversion and the rest of them as a string to
39+
// be passed to the string to integer conversion.
40+
// If the CLEANER_INPUT flag is set, the string is modified so that it's only
41+
// made of characters that the string to integer functions could accept. This is
42+
// because every other character is effectively identical, and will be treated
43+
// as the end of the integer. For the fully randomized string this gives a
44+
// greater than 50% chance for each character to end the string, making the odds
45+
// of getting long numbers very low.
46+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
47+
uint8_t *container = new uint8_t[size + 1];
48+
if (!container)
49+
__builtin_trap();
50+
size_t i;
51+
52+
for (i = 0; i < size; ++i) {
53+
#ifdef LLVM_LIBC_FUZZ_ATOI_CLEANER_INPUT
54+
container[i] = VALID_CHARS[data[i] % sizeof(VALID_CHARS)];
55+
#else
56+
container[i] = data[i];
57+
#endif
58+
}
59+
container[size] = '\0'; // Add null terminator to container.
60+
// the first character is interpreted as the base, so it should be fully
61+
// random even when the input is cleaned.
62+
container[0] = data[0];
63+
64+
StringParserOutputDiff<int>(&__llvm_libc::atoi, &::atoi, container, size);
65+
StringParserOutputDiff<long>(&__llvm_libc::atol, &::atol, container, size);
66+
StringParserOutputDiff<long long>(&__llvm_libc::atoll, &::atoll, container,
67+
size);
68+
69+
StringToNumberOutputDiff<long>(&__llvm_libc::strtol, &::strtol, container,
70+
size);
71+
StringToNumberOutputDiff<long long>(&__llvm_libc::strtoll, &::strtoll,
72+
container, size);
73+
74+
StringToNumberOutputDiff<unsigned long>(&__llvm_libc::strtoul, &::strtoul,
75+
container, size);
76+
StringToNumberOutputDiff<unsigned long long>(&__llvm_libc::strtoull,
77+
&::strtoull, container, size);
78+
79+
delete[] container;
80+
return 0;
81+
}

0 commit comments

Comments
 (0)