curl-fuzzer C++ reference
Loading...
Searching...
No Matches
allocation_fault_injection.h
1/*
2 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3 *
4 * SPDX-License-Identifier: curl
5 */
6
7#ifndef PROTO_FUZZER_ALLOCATION_FAULT_INJECTION_H_
8#define PROTO_FUZZER_ALLOCATION_FAULT_INJECTION_H_
9
10#include <cstddef>
11#include <cstdint>
12
13namespace proto_fuzzer::allocation_fault_injection {
14
15namespace internal {
16
18inline thread_local std::size_t failure_suppression_depth = 0;
20inline thread_local std::uint64_t failure_suppression_generation = 0;
21
24inline std::uint64_t BeginFailureSuppression() noexcept {
25 ++failure_suppression_depth;
26 return failure_suppression_generation;
27}
28
31inline void EndFailureSuppression(std::uint64_t generation) noexcept {
32 if (generation == failure_suppression_generation && failure_suppression_depth != 0) {
33 --failure_suppression_depth;
34 }
35}
36
38inline bool FailuresSuppressed() noexcept { return failure_suppression_depth != 0; }
39
41inline void ResetFailureSuppression() noexcept {
42 failure_suppression_depth = 0;
43 ++failure_suppression_generation;
44}
45
46} // namespace internal
47
57class ScopedFailureSuppression final {
58 public:
59 ScopedFailureSuppression() noexcept : generation_(internal::BeginFailureSuppression()) {}
60 ~ScopedFailureSuppression() { internal::EndFailureSuppression(generation_); }
61
62 ScopedFailureSuppression(const ScopedFailureSuppression&) = delete;
63 ScopedFailureSuppression& operator=(const ScopedFailureSuppression&) = delete;
64 ScopedFailureSuppression(ScopedFailureSuppression&&) = delete;
65 ScopedFailureSuppression& operator=(ScopedFailureSuppression&&) = delete;
66
67 private:
69 std::uint64_t generation_;
70};
71
73struct Statistics {
75 std::uint64_t allocation_attempts = 0;
77 std::uint64_t injected_failures = 0;
79 std::size_t configured_failures = 0;
81 bool armed = false;
82};
83
84bool InitializeCurl() noexcept;
85bool ReinitializeCurl() noexcept;
86bool CurlInitializationSucceeded() noexcept;
87bool ClaimCurlInitializationReference() noexcept;
88void Configure(const std::uint32_t* ordinals, std::size_t count) noexcept;
89void Arm() noexcept;
90void ArmNextAllocationFailure() noexcept;
91void Disarm() noexcept;
92void Reset() noexcept;
93Statistics GetStatistics() noexcept;
94
95// These are the process-wide callbacks passed to curl_global_init_mem. They
96// remain visible so the focused unit test can verify the allocator contracts
97// without depending on a particular libcurl call's private allocation trace.
98namespace internal {
99
100void* Malloc(std::size_t size) noexcept;
101void Free(void* pointer) noexcept;
102void* Realloc(void* pointer, std::size_t size) noexcept;
103char* Strdup(const char* string) noexcept;
104void* Calloc(std::size_t count, std::size_t size) noexcept;
105
106} // namespace internal
107} // namespace proto_fuzzer::allocation_fault_injection
108
109#endif // PROTO_FUZZER_ALLOCATION_FAULT_INJECTION_H_
Snapshot of the calling thread's current allocation-failure plan.
std::uint64_t allocation_attempts
Positive-size allocation calls observed since the last Arm().
std::uint64_t injected_failures
Selected calls rejected since the last Arm().
bool armed
Whether allocation calls on this thread are currently observed.
std::size_t configured_failures
Distinct ordinals retained in the bounded plan.