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