| 1 |
// Copyright 2021 gRPC authors. |
| 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 |
|
| 15 |
#include <stdint.h> |
| 16 |
#include <sys/types.h> |
| 17 |
|
| 18 |
#include <functional> |
| 19 |
#include <limits> |
| 20 |
#include <map> |
| 21 |
#include <memory> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include "absl/status/status.h" |
| 25 |
#include "absl/strings/str_cat.h" |
| 26 |
#include "absl/types/optional.h" |
| 27 |
|
| 28 |
#include <grpc/event_engine/memory_allocator.h> |
| 29 |
#include <grpc/event_engine/memory_request.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
|
| 32 |
#include "src/core/lib/debug/trace.h" |
| 33 |
#include "src/core/lib/gpr/useful.h" |
| 34 |
#include "src/core/lib/gprpp/debug_location.h" |
| 35 |
#include "src/core/lib/iomgr/closure.h" |
| 36 |
#include "src/core/lib/iomgr/error.h" |
| 37 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 38 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 39 |
#include "src/libfuzzer/libfuzzer_macro.h" |
| 40 |
#include "test/core/resource_quota/call_checker.h" |
| 41 |
#include "test/core/resource_quota/memory_quota_fuzzer.pb.h" |
| 42 |
|
| 43 |
bool squelch = true; |
| 44 |
bool leak_check = true; |
| 45 |
|
| 46 |
namespace grpc_core { |
| 47 |
namespace testing { |
| 48 |
namespace { |
| 49 |
ReclamationPass MapReclamationPass(memory_quota_fuzzer::Reclaimer::Pass pass) { |
| 50 |
switch (pass) { |
| 51 |
case memory_quota_fuzzer::Reclaimer::BENIGN: |
| 52 |
return ReclamationPass::kBenign; |
| 53 |
case memory_quota_fuzzer::Reclaimer::IDLE: |
| 54 |
return ReclamationPass::kIdle; |
| 55 |
case memory_quota_fuzzer::Reclaimer::DESTRUCTIVE: |
| 56 |
return ReclamationPass::kDestructive; |
| 57 |
default: |
| 58 |
return ReclamationPass::kBenign; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
class Fuzzer { |
| 63 |
public: |
| 64 |
void Run(const memory_quota_fuzzer::Msg& msg) { |
| 65 |
ExecCtx exec_ctx; |
| 66 |
RunMsg(msg); |
| 67 |
do { |
| 68 |
memory_quotas_.clear(); |
| 69 |
memory_allocators_.clear(); |
| 70 |
allocations_.clear(); |
| 71 |
exec_ctx.Flush(); |
| 72 |
} while (!memory_quotas_.empty() || !memory_allocators_.empty() || |
| 73 |
!allocations_.empty()); |
| 74 |
} |
| 75 |
|
| 76 |
private: |
| 77 |
void RunMsg(const memory_quota_fuzzer::Msg& msg) { |
| 78 |
for (int i = 0; i < msg.actions_size(); ++i) { |
| 79 |
const auto& action = msg.actions(i); |
| 80 |
switch (action.action_type_case()) { |
| 81 |
case memory_quota_fuzzer::Action::kFlushExecCtx: |
| 82 |
ExecCtx::Get()->Flush(); |
| 83 |
break; |
| 84 |
case memory_quota_fuzzer::Action::kCreateQuota: |
| 85 |
memory_quotas_.emplace(action.quota(), |
| 86 |
MemoryQuota(absl::StrCat("quota-step-", i))); |
| 87 |
break; |
| 88 |
case memory_quota_fuzzer::Action::kDeleteQuota: |
| 89 |
memory_quotas_.erase(action.quota()); |
| 90 |
break; |
| 91 |
case memory_quota_fuzzer::Action::kCreateAllocator: |
| 92 |
WithQuota(action.quota(), [this, action, i](MemoryQuota* q) { |
| 93 |
memory_allocators_.emplace( |
| 94 |
action.allocator(), |
| 95 |
q->CreateMemoryOwner(absl::StrCat("allocator-step-", i))); |
| 96 |
}); |
| 97 |
break; |
| 98 |
case memory_quota_fuzzer::Action::kDeleteAllocator: |
| 99 |
memory_allocators_.erase(action.allocator()); |
| 100 |
break; |
| 101 |
case memory_quota_fuzzer::Action::kSetQuotaSize: |
| 102 |
WithQuota(action.quota(), [action](MemoryQuota* q) { |
| 103 |
q->SetSize(Clamp(action.set_quota_size(), uint64_t{0}, |
| 104 |
uint64_t{std::numeric_limits<ssize_t>::max()})); |
| 105 |
}); |
| 106 |
break; |
| 107 |
case memory_quota_fuzzer::Action::kCreateAllocation: { |
| 108 |
auto min = action.create_allocation().min(); |
| 109 |
auto max = action.create_allocation().max(); |
| 110 |
if (min > max) break; |
| 111 |
if (max > MemoryRequest::max_allowed_size()) break; |
| 112 |
MemoryRequest req(min, max); |
| 113 |
WithAllocator( |
| 114 |
action.allocator(), [this, action, req](MemoryOwner* a) { |
| 115 |
auto alloc = a->MakeReservation(req); |
| 116 |
allocations_.emplace(action.allocation(), std::move(alloc)); |
| 117 |
}); |
| 118 |
} break; |
| 119 |
case memory_quota_fuzzer::Action::kDeleteAllocation: |
| 120 |
allocations_.erase(action.allocation()); |
| 121 |
break; |
| 122 |
case memory_quota_fuzzer::Action::kPostReclaimer: { |
| 123 |
std::function<void(absl::optional<ReclamationSweep>)> reclaimer; |
| 124 |
auto cfg = action.post_reclaimer(); |
| 125 |
if (cfg.synchronous()) { |
| 126 |
reclaimer = [this, cfg](absl::optional<ReclamationSweep>) { |
| 127 |
RunMsg(cfg.msg()); |
| 128 |
}; |
| 129 |
} else { |
| 130 |
reclaimer = [cfg, this](absl::optional<ReclamationSweep> sweep) { |
| 131 |
struct Args { |
| 132 |
absl::optional<ReclamationSweep> sweep; |
| 133 |
memory_quota_fuzzer::Msg msg; |
| 134 |
Fuzzer* fuzzer; |
| 135 |
}; |
| 136 |
auto* args = new Args{std::move(sweep), cfg.msg(), this}; |
| 137 |
auto* closure = GRPC_CLOSURE_CREATE( |
| 138 |
[](void* arg, grpc_error_handle) { |
| 139 |
auto* args = static_cast<Args*>(arg); |
| 140 |
args->fuzzer->RunMsg(args->msg); |
| 141 |
delete args; |
| 142 |
}, |
| 143 |
args, nullptr); |
| 144 |
ExecCtx::Get()->Run(DEBUG_LOCATION, closure, absl::OkStatus()); |
| 145 |
}; |
| 146 |
auto pass = MapReclamationPass(cfg.pass()); |
| 147 |
WithAllocator( |
| 148 |
action.allocator(), [pass, reclaimer](MemoryOwner* a) { |
| 149 |
// ensure called exactly once |
| 150 |
auto call_checker = CallChecker::MakeOptional(); |
| 151 |
a->PostReclaimer(pass, |
| 152 |
[reclaimer, call_checker]( |
| 153 |
absl::optional<ReclamationSweep> sweep) { |
| 154 |
call_checker->Called(); |
| 155 |
reclaimer(std::move(sweep)); |
| 156 |
}); |
| 157 |
}); |
| 158 |
} |
| 159 |
} break; |
| 160 |
case memory_quota_fuzzer::Action::ACTION_TYPE_NOT_SET: |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
template <typename F> |
| 167 |
void WithQuota(int quota, F f) { |
| 168 |
auto it = memory_quotas_.find(quota); |
| 169 |
if (it == memory_quotas_.end()) return; |
| 170 |
f(&it->second); |
| 171 |
} |
| 172 |
|
| 173 |
template <typename F> |
| 174 |
void WithAllocator(int allocator, F f) { |
| 175 |
auto it = memory_allocators_.find(allocator); |
| 176 |
if (it == memory_allocators_.end()) return; |
| 177 |
f(&it->second); |
| 178 |
} |
| 179 |
|
| 180 |
std::map<int, MemoryQuota> memory_quotas_; |
| 181 |
std::map<int, MemoryOwner> memory_allocators_; |
| 182 |
std::map<int, MemoryAllocator::Reservation> allocations_; |
| 183 |
}; |
| 184 |
|
| 185 |
} // namespace |
| 186 |
} // namespace testing |
| 187 |
} // namespace grpc_core |
| 188 |
|
| 189 |
static void dont_log(gpr_log_func_args* /*args*/) {} |
| 190 |
|
| 191 |
DEFINE_PROTO_FUZZER(const memory_quota_fuzzer::Msg& msg) { |
| 192 |
if (squelch) gpr_set_log_function(dont_log); |
| 193 |
gpr_log_verbosity_init(); |
| 194 |
grpc_tracer_init(); |
| 195 |
grpc_core::testing::Fuzzer().Run(msg); |
| 196 |
} |