diff --git a/libc/fuzzing/math/Compare.h b/libc/fuzzing/math/Compare.h --- a/libc/fuzzing/math/Compare.h +++ b/libc/fuzzing/math/Compare.h @@ -28,7 +28,7 @@ template __llvm_libc::cpp::enable_if_t<__llvm_libc::cpp::is_integral_v, bool> ValuesEqual(T x1, T x2) { - return x1 == x1; + return x1 == x2; } #endif // LLVM_LIBC_FUZZING_MATH_COMPARE_H diff --git a/libc/fuzzing/stdlib/CMakeLists.txt b/libc/fuzzing/stdlib/CMakeLists.txt --- a/libc/fuzzing/stdlib/CMakeLists.txt +++ b/libc/fuzzing/stdlib/CMakeLists.txt @@ -16,3 +16,18 @@ libc.src.stdlib.atof ) +add_libc_fuzzer( + strtointeger_differential_fuzz + SRCS + strtointeger_differential_fuzz.cpp + HDRS + StringParserOutputDiff.h + DEPENDS + libc.src.stdlib.atoi + libc.src.stdlib.atol + libc.src.stdlib.atoll + libc.src.stdlib.strtol + libc.src.stdlib.strtoll + libc.src.stdlib.strtoul + libc.src.stdlib.strtoull +) diff --git a/libc/fuzzing/stdlib/StringParserOutputDiff.h b/libc/fuzzing/stdlib/StringParserOutputDiff.h --- a/libc/fuzzing/stdlib/StringParserOutputDiff.h +++ b/libc/fuzzing/stdlib/StringParserOutputDiff.h @@ -32,4 +32,28 @@ __builtin_trap(); } +template +using StringToNumberFunc = T (*)(const char *, char **, int); + +template +void StringToNumberOutputDiff(StringToNumberFunc func1, + StringToNumberFunc func2, const uint8_t *data, + size_t size) { + if (size < sizeof(T)) + return; + + const char *x = reinterpret_cast(data + 1); + int base = data[0] % 36; + base = base + ((base == 0) ? 0 : 1); + + char *outPtr1 = nullptr; + char *outPtr2 = nullptr; + + T result1 = func1(x, &outPtr1, base); + T result2 = func2(x, &outPtr2, base); + + if (!(ValuesEqual(result1, result2) && (*outPtr1 == *outPtr2))) + __builtin_trap(); +} + #endif // LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H diff --git a/libc/fuzzing/stdlib/strtointeger_differential_fuzz.cpp b/libc/fuzzing/stdlib/strtointeger_differential_fuzz.cpp new file mode 100644 --- /dev/null +++ b/libc/fuzzing/stdlib/strtointeger_differential_fuzz.cpp @@ -0,0 +1,77 @@ +//===-- strtointeger_differential_fuzz.cpp --------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Fuzzing test for llvm-libc atof implementation. +/// +//===----------------------------------------------------------------------===// +#include "src/stdlib/atoi.h" +#include "src/stdlib/atol.h" +#include "src/stdlib/atoll.h" +#include "src/stdlib/strtol.h" +#include "src/stdlib/strtoll.h" +#include "src/stdlib/strtoul.h" +#include "src/stdlib/strtoull.h" +#include +#include +#include + +#include "fuzzing/stdlib/StringParserOutputDiff.h" + +// This list contains (almost) all character that can possibly be accepted by a +// string to integer conversion. Those are: space, tab, any digit, and any +// letter. Technically there are some space characters accepted by isspace that +// aren't in this list, but since space characters are just skipped over anyways +// I'm not really worried. +const char valid_chars[] = { + ' ', '\t', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', + 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', + 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', + 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', + 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z'}; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + uint8_t *container = new uint8_t[size + 1]; + // Cleaner is a cleaner input for string to integer functions. It is much more + // likely to actually be a value with some sort of conversion. + uint8_t *cleaner = new uint8_t[size + 1]; + if (!container || !cleaner) + __builtin_trap(); + size_t i; + + for (i = 0; i < size; ++i) { + container[i] = data[i]; + cleaner[i] = valid_chars[data[i] % sizeof(valid_chars)]; + } + container[size] = '\0'; // Add null terminator to container. + cleaner[size] = '\0'; + cleaner[0] = container[0]; // the first character is interpreted as the base, + // so it hsould be fully random. + + // Check container with one pass, but most of the time it will just return 0. + StringToNumberOutputDiff(&__llvm_libc::strtol, &::strtol, container, + size); + + StringParserOutputDiff(&__llvm_libc::atoi, &::atoi, cleaner, size); + StringParserOutputDiff(&__llvm_libc::atol, &::atol, cleaner, size); + StringParserOutputDiff(&__llvm_libc::atoll, &::atoll, cleaner, + size); + + StringToNumberOutputDiff(&__llvm_libc::strtol, &::strtol, cleaner, + size); + StringToNumberOutputDiff(&__llvm_libc::strtoll, &::strtoll, + cleaner, size); + + StringToNumberOutputDiff(&__llvm_libc::strtoul, &::strtoul, + cleaner, size); + StringToNumberOutputDiff(&__llvm_libc::strtoull, + &::strtoull, cleaner, size); + + delete[] container; + delete[] cleaner; + return 0; +} diff --git a/libc/src/stdlib/atoi.cpp b/libc/src/stdlib/atoi.cpp --- a/libc/src/stdlib/atoi.cpp +++ b/libc/src/stdlib/atoi.cpp @@ -13,11 +13,11 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(int, atoi, (const char *str)) { - auto result = internal::strtointeger(str, 10); + auto result = internal::strtointeger(str, 10); if (result.has_error()) errno = result.error; - return result; + return static_cast(result); } } // namespace __llvm_libc