1
+ // Copyright 2024 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
1
15
#include " ./fuzztest/fuzztest_macros.h"
2
16
3
17
#include < cerrno>
12
26
#include < utility>
13
27
#include < vector>
14
28
15
- #include " absl/log/absl_check.h"
16
29
#include " absl/status/status.h"
30
+ #include " absl/status/statusor.h"
17
31
#include " absl/strings/escaping.h"
18
32
#include " absl/strings/str_cat.h"
19
33
#include " absl/strings/str_split.h"
20
34
#include " absl/strings/string_view.h"
21
- #include " ./fuzztest/internal/io.h"
22
35
#include " ./fuzztest/internal/logging.h"
36
+ #include " ./fuzztest/internal/runtime.h"
23
37
24
38
namespace fuzztest {
25
39
@@ -67,26 +81,7 @@ absl::StatusOr<std::string> ParseDictionaryEntry(absl::string_view entry) {
67
81
68
82
std::vector<std::tuple<std::string>> ReadFilesFromDirectory (
69
83
std::string_view dir) {
70
- std::vector<std::tuple<std::string>> out;
71
- const std::filesystem::path fs_dir (dir);
72
- if (!std::filesystem::is_directory (fs_dir)) return out;
73
- for (const auto & entry :
74
- std::filesystem::recursive_directory_iterator (fs_dir)) {
75
- if (std::filesystem::is_directory (entry)) continue ;
76
- std::ifstream stream (entry.path ());
77
- if (!stream.good ()) {
78
- // Using stderr instead of GetStderr() to avoid
79
- // initialization-order-fiasco when reading files at static init time with
80
- // `.WithSeeds(fuzztest::ReadFilesFromDirectory(...))`.
81
- absl::FPrintF (stderr, " [!] %s:%d: Error reading %s: (%d) %s\n " , __FILE__,
82
- __LINE__, entry.path ().string (), errno, strerror (errno));
83
- continue ;
84
- }
85
- std::stringstream buffer;
86
- buffer << stream.rdbuf ();
87
- out.emplace_back (std::move (buffer).str ());
88
- }
89
- return out;
84
+ return ReadFilesFromDirectory (dir, [](std::string_view) { return true ; });
90
85
}
91
86
92
87
std::vector<std::tuple<std::string>> ReadFilesFromDirectory (
@@ -113,10 +108,11 @@ std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
113
108
}
114
109
115
110
absl::StatusOr<std::vector<std::string>> ParseDictionary (
116
- absl ::string_view text) {
111
+ std ::string_view text) {
117
112
std::vector<std::string> parsed_entries;
118
113
int line_number = 0 ;
119
- for (absl::string_view line : absl::StrSplit (text, ' \n ' )) {
114
+ for (absl::string_view line :
115
+ absl::StrSplit (absl::string_view{text.data (), text.size ()}, ' \n ' )) {
120
116
++line_number;
121
117
122
118
if (line.empty () || line[0 ] == ' #' ) continue ;
@@ -154,17 +150,20 @@ std::vector<std::string> ReadDictionaryFromFile(
154
150
" Not a file: " , dictionary_file);
155
151
const std::filesystem::path fs_path (dictionary_file);
156
152
std::ifstream stream (fs_path);
157
- ABSL_CHECK (stream.good ()) << " Error reading " << fs_path. string () << " : ( "
158
- << errno << " ) " << strerror (errno);
153
+ FUZZTEST_INTERNAL_CHECK_PRECONDITION (stream.good (), " Error reading " ,
154
+ fs_path. string (), " : " , strerror (errno) );
159
155
std::stringstream buffer;
160
156
buffer << stream.rdbuf ();
161
- // https://llvm.org/docs/LibFuzzer.html#dictionaries
162
157
absl::StatusOr<std::vector<std::string>> parsed_entries =
163
158
ParseDictionary (buffer.str ());
164
- ABSL_CHECK (parsed_entries. status (). ok ())
165
- << " Could not parse dictionary file " << fs_path << " : "
166
- << parsed_entries.status ();
159
+ FUZZTEST_INTERNAL_CHECK_PRECONDITION (
160
+ parsed_entries. status (). ok (), " Could not parse dictionary file " ,
161
+ fs_path. string (), " : " , parsed_entries.status () );
167
162
return *parsed_entries;
168
163
}
169
164
165
+ void SkipTestsOrCurrentInput () {
166
+ internal::Runtime::instance ().SetSkippingRequested (true );
167
+ }
168
+
170
169
} // namespace fuzztest
0 commit comments