/src/cmp_tool/test/fuzz/fuzz_helpers.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under both the BSD-style license (found in the |
6 | | * LICENSE.BSD-3.Zstandard file in the 3rdparty_licenses directory) and the GPLv2 |
7 | | * (found in the LICENSE.GPL-2 file in the 3rdparty_licenses directory). |
8 | | * You may select, at your option, one of the above-listed licenses. |
9 | | */ |
10 | | |
11 | | /* |
12 | | * Modifications made by |
13 | | * @author Dominik Loidolt (dominik.loidolt@univie.ac.at) |
14 | | * @date 2025 |
15 | | * |
16 | | * - add FUZZ_buf_to_file |
17 | | * - add FUZZ_delete_file |
18 | | * |
19 | | * Modifications are also licensed under the same license for consistency |
20 | | */ |
21 | | |
22 | | /** |
23 | | * Helper functions for fuzzing. |
24 | | */ |
25 | | |
26 | | #include <stddef.h> |
27 | | #include <stdlib.h> |
28 | | #include <unistd.h> |
29 | | #include <string.h> |
30 | | #include <errno.h> |
31 | | |
32 | | #include "fuzz_helpers.h" |
33 | | #include "fuzz_data_producer.h" |
34 | | |
35 | | |
36 | | void *FUZZ_malloc(size_t size) |
37 | 3.31k | { |
38 | 3.31k | if (size > 0) { |
39 | 2.58k | void *const mem = malloc(size); |
40 | | |
41 | 2.58k | FUZZ_ASSERT(mem); |
42 | 2.58k | return mem; |
43 | 2.58k | } |
44 | 737 | return NULL; |
45 | 3.31k | } |
46 | | |
47 | | |
48 | | int FUZZ_delete_file(const char *path_name) |
49 | 0 | { |
50 | 0 | int const ret = unlink(path_name); |
51 | |
|
52 | 0 | FUZZ_ASSERT(ret != -1); |
53 | |
|
54 | 0 | free((void *)path_name); |
55 | |
|
56 | 0 | return ret; |
57 | 0 | } |
58 | | |
59 | | |
60 | | char *FUZZ_buf_to_file(const uint8_t *buf, size_t size) |
61 | 0 | { |
62 | 0 | int fd, ret_close; |
63 | 0 | size_t pos = 0; |
64 | |
|
65 | 0 | char *path_name = strdup(FUZZ_TMP_DIR "/fuzz-XXXXXX"); |
66 | |
|
67 | 0 | FUZZ_ASSERT(path_name != NULL); |
68 | |
|
69 | 0 | fd = mkstemp(path_name); |
70 | 0 | FUZZ_ASSERT_MSG(fd != 1, path_name); |
71 | | |
72 | 0 | while (pos < size) { |
73 | 0 | ssize_t bytes_written = write(fd, &buf[pos], size - pos); |
74 | |
|
75 | 0 | if (bytes_written == -1 && errno == EINTR) |
76 | 0 | continue; |
77 | | |
78 | 0 | FUZZ_ASSERT(bytes_written != -1); |
79 | |
|
80 | 0 | pos += (size_t)bytes_written; |
81 | 0 | } |
82 | |
|
83 | 0 | ret_close = close(fd); |
84 | 0 | FUZZ_ASSERT(ret_close != 1); |
85 | |
|
86 | 0 | return path_name; |
87 | 0 | } |