1*66d00febSPaula Toth //===-- strcpy_fuzz.cpp ---------------------------------------------------===//
2a4f45ee7SPaula Toth //
3a4f45ee7SPaula Toth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a4f45ee7SPaula Toth // See https://llvm.org/LICENSE.txt for license information.
5a4f45ee7SPaula Toth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a4f45ee7SPaula Toth //
7a4f45ee7SPaula Toth //===----------------------------------------------------------------------===//
8a4f45ee7SPaula Toth ///
9a4f45ee7SPaula Toth /// Fuzzing test for llvm-libc strcpy implementation.
10a4f45ee7SPaula Toth ///
11a4f45ee7SPaula Toth //===----------------------------------------------------------------------===//
12a4f45ee7SPaula Toth #include "src/string/strcpy.h"
13a4f45ee7SPaula Toth #include <stdint.h>
14a4f45ee7SPaula Toth 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)15a4f45ee7SPaula Toth extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
16a4f45ee7SPaula Toth   // Validate input
17a4f45ee7SPaula Toth   if (!size) return 0;
18a4f45ee7SPaula Toth   if (data[size - 1] != '\0') return 0;
19a4f45ee7SPaula Toth   const char *src = (const char *)data;
20a4f45ee7SPaula Toth 
21a4f45ee7SPaula Toth   char *dest = new char[size];
22a4f45ee7SPaula Toth   if (!dest) __builtin_trap();
23a4f45ee7SPaula Toth 
24a4f45ee7SPaula Toth   __llvm_libc::strcpy(dest, src);
25a4f45ee7SPaula Toth 
26a4f45ee7SPaula Toth   size_t i;
27a4f45ee7SPaula Toth   for (i = 0; src[i] != '\0'; i++) {
28a4f45ee7SPaula Toth     // Ensure correctness of strcpy
29a4f45ee7SPaula Toth     if (dest[i] != src[i]) __builtin_trap();
30a4f45ee7SPaula Toth   }
31a4f45ee7SPaula Toth   // Ensure strcpy null terminates dest
32a4f45ee7SPaula Toth   if (dest[i] != src[i]) __builtin_trap();
33a4f45ee7SPaula Toth 
34a4f45ee7SPaula Toth   delete[] dest;
35a4f45ee7SPaula Toth 
36a4f45ee7SPaula Toth   return 0;
37a4f45ee7SPaula Toth }
38a4f45ee7SPaula Toth 
39