xref: /llvm-project/libc/fuzzing/stdlib/strtointeger_fuzz.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
13a66446aSMichael Jones //===-- strtointeger_fuzz.cpp ---------------------------------------------===//
23a66446aSMichael Jones //
33a66446aSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43a66446aSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
53a66446aSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63a66446aSMichael Jones //
73a66446aSMichael Jones //===----------------------------------------------------------------------===//
83a66446aSMichael Jones ///
93a66446aSMichael Jones /// Fuzzing test for llvm-libc atof implementation.
103a66446aSMichael Jones ///
113a66446aSMichael Jones //===----------------------------------------------------------------------===//
123a66446aSMichael Jones #include "src/stdlib/atoi.h"
133a66446aSMichael Jones #include "src/stdlib/atol.h"
143a66446aSMichael Jones #include "src/stdlib/atoll.h"
153a66446aSMichael Jones #include "src/stdlib/strtol.h"
163a66446aSMichael Jones #include "src/stdlib/strtoll.h"
173a66446aSMichael Jones #include "src/stdlib/strtoul.h"
183a66446aSMichael Jones #include "src/stdlib/strtoull.h"
193a66446aSMichael Jones #include <stddef.h>
203a66446aSMichael Jones #include <stdint.h>
213a66446aSMichael Jones 
223a66446aSMichael Jones // This takes the randomized bytes in data and interprets the first byte as the
233a66446aSMichael Jones // base for the string to integer conversion and the rest of them as a string to
243a66446aSMichael Jones // be passed to the string to integer conversion.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)253a66446aSMichael Jones extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
263a66446aSMichael Jones   size_t container_size = 0;
273a66446aSMichael Jones   if (size == 0) {
283a66446aSMichael Jones     container_size = 1;
293a66446aSMichael Jones   } else {
303a66446aSMichael Jones     container_size = size;
313a66446aSMichael Jones   }
323a66446aSMichael Jones   uint8_t *container = new uint8_t[container_size];
333a66446aSMichael Jones   if (!container)
343a66446aSMichael Jones     __builtin_trap();
353a66446aSMichael Jones 
363a66446aSMichael Jones   int base = 0;
373a66446aSMichael Jones   if (size > 0) {
383a66446aSMichael Jones     base = data[0] % 36;
393a66446aSMichael Jones     base = base + ((base == 0) ? 0 : 1);
403a66446aSMichael Jones   }
413a66446aSMichael Jones   for (size_t i = 1; i < size; ++i) {
423a66446aSMichael Jones     container[i - 1] = data[i];
433a66446aSMichael Jones   }
443a66446aSMichael Jones 
453a66446aSMichael Jones   container[container_size - 1] = '\0'; // Add null terminator to container.
463a66446aSMichael Jones 
473a66446aSMichael Jones   const char *str_ptr = reinterpret_cast<const char *>(container);
483a66446aSMichael Jones 
493a66446aSMichael Jones   char *out_ptr = nullptr;
503a66446aSMichael Jones 
51*b6bc9d72SGuillaume Chatelet   auto volatile atoi_output = LIBC_NAMESPACE::atoi(str_ptr);
52*b6bc9d72SGuillaume Chatelet   auto volatile atol_output = LIBC_NAMESPACE::atol(str_ptr);
53*b6bc9d72SGuillaume Chatelet   auto volatile atoll_output = LIBC_NAMESPACE::atoll(str_ptr);
54*b6bc9d72SGuillaume Chatelet   auto volatile strtol_output = LIBC_NAMESPACE::strtol(str_ptr, &out_ptr, base);
553a66446aSMichael Jones   if (str_ptr + container_size - 1 < out_ptr)
563a66446aSMichael Jones     __builtin_trap();
57*b6bc9d72SGuillaume Chatelet   auto volatile strtoll_output =
58*b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::strtoll(str_ptr, &out_ptr, base);
593a66446aSMichael Jones   if (str_ptr + container_size - 1 < out_ptr)
603a66446aSMichael Jones     __builtin_trap();
61*b6bc9d72SGuillaume Chatelet   auto volatile strtoul_output =
62*b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::strtoul(str_ptr, &out_ptr, base);
633a66446aSMichael Jones   if (str_ptr + container_size - 1 < out_ptr)
643a66446aSMichael Jones     __builtin_trap();
653a66446aSMichael Jones   auto volatile strtoull_output =
66*b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::strtoull(str_ptr, &out_ptr, base);
673a66446aSMichael Jones   if (str_ptr + container_size - 1 < out_ptr)
683a66446aSMichael Jones     __builtin_trap();
693a66446aSMichael Jones 
7062e7bdd2SMichael Jones   // If atoi is non-zero and the base is at least 10
7162e7bdd2SMichael Jones   if (atoi_output != 0 && base >= 10) {
7262e7bdd2SMichael Jones     // Then all of the other functions should output non-zero values as well.
7362e7bdd2SMichael Jones     // This is a trivial check meant to silence the "unused variable" warnings.
7462e7bdd2SMichael Jones     if (atol_output == 0 || atoll_output == 0 || strtol_output == 0 ||
7562e7bdd2SMichael Jones         strtoll_output == 0 || strtoul_output == 0 || strtoull_output == 0) {
7662e7bdd2SMichael Jones       __builtin_trap();
7762e7bdd2SMichael Jones     }
7862e7bdd2SMichael Jones   }
7962e7bdd2SMichael Jones 
803a66446aSMichael Jones   delete[] container;
813a66446aSMichael Jones   return 0;
823a66446aSMichael Jones }
83