-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathcentipede_default_callbacks.cc
More file actions
123 lines (112 loc) · 4.58 KB
/
centipede_default_callbacks.cc
File metadata and controls
123 lines (112 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2022 The Centipede Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "./centipede/centipede_default_callbacks.h"
#include <cstddef>
#include <cstdlib>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/time/time.h"
#include "./centipede/centipede_callbacks.h"
#include "./centipede/environment.h"
#include "./centipede/mutation_input.h"
#include "./centipede/runner_result.h"
#include "./centipede/stop.h"
#include "./common/defs.h"
#include "./common/logging.h" // IWYU pragma: keep
namespace fuzztest::internal {
CentipedeDefaultCallbacks::CentipedeDefaultCallbacks(const Environment &env)
: CentipedeCallbacks(env) {
for (const auto &dictionary_path : env_.dictionary) {
LoadDictionary(dictionary_path);
}
if (env_.has_input_wildcards) {
FUZZTEST_LOG(INFO) << "Disabling custom mutator for standalone target";
custom_mutator_is_usable_ = false;
}
}
bool CentipedeDefaultCallbacks::Execute(std::string_view binary,
const std::vector<ByteArray>& inputs,
BatchResult& batch_result,
absl::Time deadline) {
return ExecuteCentipedeSancovBinaryWithShmem(binary, inputs, batch_result,
deadline) == 0;
}
size_t CentipedeDefaultCallbacks::GetSeeds(size_t num_seeds,
std::vector<ByteArray> &seeds) {
seeds.resize(num_seeds);
if (GetSeedsViaExternalBinary(env_.binary, num_seeds, seeds)) {
return num_seeds;
}
return CentipedeCallbacks::GetSeeds(num_seeds, seeds);
}
absl::StatusOr<std::string>
CentipedeDefaultCallbacks::GetSerializedTargetConfig() {
std::string serialized_target_config;
if (GetSerializedTargetConfigViaExternalBinary(env_.binary,
serialized_target_config)) {
return serialized_target_config;
}
return absl::InternalError(
"Failed to get serialized configuration from the target binary.");
}
std::vector<ByteArray> CentipedeDefaultCallbacks::Mutate(
const std::vector<MutationInputRef> &inputs, size_t num_mutants) {
if (num_mutants == 0) return {};
// Try to use the custom mutator if it hasn't been disabled.
if (custom_mutator_is_usable_.value_or(true)) {
MutationResult result =
MutateViaExternalBinary(env_.binary, inputs, num_mutants);
if (result.exit_code() == EXIT_SUCCESS) {
if (!custom_mutator_is_usable_.has_value()) {
custom_mutator_is_usable_ = result.has_custom_mutator();
if (*custom_mutator_is_usable_) {
FUZZTEST_LOG(INFO) << "Custom mutator detected; will use it.";
} else {
FUZZTEST_LOG(INFO)
<< "Custom mutator not detected; falling back to the "
"built-in mutator.";
}
}
if (*custom_mutator_is_usable_) {
// TODO(b/398261908): Exit with failure instead of crashing.
FUZZTEST_CHECK(result.has_custom_mutator())
<< "Test binary no longer has a custom mutator, even though it was "
"previously detected.";
if (!result.mutants().empty()) return std::move(result).mutants();
FUZZTEST_LOG_FIRST_N(WARNING, 5)
<< "Custom mutator returned no mutants; will "
"generate some using the built-in mutator.";
}
} else if (ShouldStop()) {
FUZZTEST_LOG(WARNING)
<< "Custom mutator failed, but ignored since the stop "
"condition it met. Possibly what triggered the stop "
"condition also interrupted the mutator.";
// Returning whatever mutants we got before the failure.
return std::move(result).mutants();
} else {
FUZZTEST_LOG(ERROR)
<< "Test binary failed when asked to mutate inputs - exiting.";
RequestEarlyStop(EXIT_FAILURE);
return {};
}
}
// Fall back to the internal mutator.
return CentipedeCallbacks::Mutate(inputs, num_mutants);
}
} // namespace fuzztest::internal