|
| 1 | +#include "../OptiScaler/framegen/ProviderHandleRegistry.h" |
| 2 | +#include "../OptiScaler/framegen/ProviderHandleCreation.h" |
| 3 | +#include <atomic> |
| 4 | +#include <cstdlib> |
| 5 | +#include <iostream> |
| 6 | +#include <new> |
| 7 | +#include <stdexcept> |
| 8 | + |
| 9 | +// Fail individual ordinary allocations, or every allocation after the native |
| 10 | +// callback succeeds. No production allocation hooks or test toggles are added. |
| 11 | +static thread_local int allocationBudget = -1; |
| 12 | +void* operator new(std::size_t size) |
| 13 | +{ |
| 14 | + if (allocationBudget == 0) |
| 15 | + throw std::bad_alloc(); |
| 16 | + if (allocationBudget > 0) |
| 17 | + --allocationBudget; |
| 18 | + if (auto* p = std::malloc(size ? size : 1)) |
| 19 | + return p; |
| 20 | + throw std::bad_alloc(); |
| 21 | +} |
| 22 | +void operator delete(void* p) noexcept { std::free(p); } |
| 23 | +void operator delete(void* p, std::size_t) noexcept { std::free(p); } |
| 24 | + |
| 25 | +struct Handle |
| 26 | +{ |
| 27 | + unsigned id; |
| 28 | + int* nativeHandle = nullptr; |
| 29 | +}; |
| 30 | + |
| 31 | +int main() |
| 32 | +{ |
| 33 | + int checks = 0; |
| 34 | + auto check = [&](bool ok) |
| 35 | + { |
| 36 | + if (!ok) |
| 37 | + { |
| 38 | + std::cerr << "publication allocation check " << checks + 1 << " failed\n"; |
| 39 | + std::exit(2); |
| 40 | + } |
| 41 | + ++checks; |
| 42 | + }; |
| 43 | + int native = 9; |
| 44 | + bool observedPreflightFailure = false; |
| 45 | + bool observedSuccess = false; |
| 46 | + // Covers the entry, hash node and possible bucket allocations across STL |
| 47 | + // implementations. Every throw must occur before the native callback. |
| 48 | + for (int budget = 0; budget != 8; ++budget) |
| 49 | + { |
| 50 | + ProviderHandleRegistry<Handle> registry; |
| 51 | + Handle* out = reinterpret_cast<Handle*>(1); |
| 52 | + int calls = 0; |
| 53 | + allocationBudget = budget; |
| 54 | + bool threw = false; |
| 55 | + try |
| 56 | + { |
| 57 | + CreateProviderHandle(registry, &out, Handle { 1 }, 0, -2, -1, |
| 58 | + [&](Handle& h) |
| 59 | + { |
| 60 | + ++calls; |
| 61 | + h.nativeHandle = &native; |
| 62 | + return 0; |
| 63 | + }); |
| 64 | + } |
| 65 | + catch (const std::bad_alloc&) |
| 66 | + { |
| 67 | + threw = true; |
| 68 | + } |
| 69 | + allocationBudget = -1; |
| 70 | + if (threw) |
| 71 | + { |
| 72 | + observedPreflightFailure = true; |
| 73 | + check(calls == 0 && !out); |
| 74 | + check(registry.LiveKeys([](auto&) { return true; }).empty()); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + observedSuccess = true; |
| 79 | + check(calls == 1 && out); |
| 80 | + } |
| 81 | + } |
| 82 | + check(observedPreflightFailure && observedSuccess); |
| 83 | + |
| 84 | + ProviderHandleRegistry<Handle> registry; |
| 85 | + Handle* out = nullptr; |
| 86 | + const void* privateAddress = nullptr; |
| 87 | + auto create = [&](bool fail, bool throws) |
| 88 | + { |
| 89 | + return CreateProviderHandle( |
| 90 | + registry, &out, Handle { 2 }, 0, -2, -1, |
| 91 | + [&](Handle& h) |
| 92 | + { |
| 93 | + privateAddress = &h; |
| 94 | + check(!out); |
| 95 | + check(!registry.GetIdentity(&h, [](auto& v) { return v.id; })); |
| 96 | + check(registry.Read(&h, -3, [](auto&) { return 0; }) == -3); |
| 97 | + check(registry.Release(&h, -3, [](auto&) { return 0; }, [](int r) { return r == 0; }) == -3); |
| 98 | + check(registry.LiveKeys([](auto&) { return true; }).empty()); |
| 99 | + if (throws) |
| 100 | + throw std::runtime_error("native create"); |
| 101 | + if (fail) |
| 102 | + return -1; |
| 103 | + h.nativeHandle = &native; |
| 104 | + allocationBudget = 0; // No allocation may follow native success. |
| 105 | + return 0; |
| 106 | + }); |
| 107 | + }; |
| 108 | + check(create(true, false) == -1 && !out); |
| 109 | + check(!registry.GetIdentity(privateAddress, [](auto& v) { return v.id; })); |
| 110 | + bool threw = false; |
| 111 | + try |
| 112 | + { |
| 113 | + create(false, true); |
| 114 | + } |
| 115 | + catch (const std::runtime_error&) |
| 116 | + { |
| 117 | + threw = true; |
| 118 | + } |
| 119 | + check(threw && !out && registry.LiveKeys([](auto&) { return true; }).empty()); |
| 120 | + int result = -1; |
| 121 | + threw = false; |
| 122 | + try |
| 123 | + { |
| 124 | + result = create(false, false); |
| 125 | + } |
| 126 | + catch (const std::bad_alloc&) |
| 127 | + { |
| 128 | + threw = true; |
| 129 | + } |
| 130 | + allocationBudget = -1; |
| 131 | + check(!threw && result == 0 && out); |
| 132 | + check(registry.Read(out, -3, [](auto& h) { return *h.nativeHandle; }) == native); |
| 133 | + check(registry.LiveKeys([](auto&) { return true; }).size() == 1); |
| 134 | + check(registry.Release(out, -3, [](auto&) { return 0; }, [](int r) { return r == 0; }) == 0); |
| 135 | + std::cout << "PASS: " << checks << " publication allocation checks\n"; |
| 136 | +} |
0 commit comments