|
| 1 | +// CR-365 R2-A2 (extra to the brief): an admission refusal that the read-only |
| 2 | +// live-limit ticket cannot see must still precede the host cohort handle. |
| 3 | +// |
| 4 | +// cohort_for opens the table's backing file during admission. When that open is |
| 5 | +// refused (here: the scratch directory is a regular file) the new id must be |
| 6 | +// refused before require_host().cohort_open(), so no kernel cohort handle or |
| 7 | +// receipt is consumed. The witness is dynamic: it reads the kernel's next |
| 8 | +// handle before and after the refused call, and it first proves, through the |
| 9 | +// table's own emplace and without touching the kernel, that the injected fault |
| 10 | +// really refuses placement. Both facts are read from the real objects. |
| 11 | +#include <pineforge/source/pine_strategy_host.hpp> |
| 12 | + |
| 13 | +#include <cstddef> |
| 14 | +#include <cstdint> |
| 15 | +#include <cstdio> |
| 16 | +#include <stdexcept> |
| 17 | +#include <string> |
| 18 | +#include <system_error> |
| 19 | +#include <type_traits> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace { |
| 23 | +using Adapter = pineforge::source::PineExecutionAdapter; |
| 24 | + |
| 25 | +// The same explicit-instantiation access seam as test_id_adapter_storage.cpp: |
| 26 | +// no changed class definition or production friend. |
| 27 | +template<class Tag, auto Member> struct Access { |
| 28 | + friend auto access(Tag) { return Member; } |
| 29 | +}; |
| 30 | +struct Cohorts { friend auto access(Cohorts); }; |
| 31 | +struct CohortFor { friend auto access(CohortFor); }; |
| 32 | +template struct Access<Cohorts, &Adapter::cohorts_by_id_>; |
| 33 | +template struct Access<CohortFor, &Adapter::cohort_for>; |
| 34 | + |
| 35 | +class IoRefusalHost final : public pineforge::source::PineStrategyHost { |
| 36 | +public: |
| 37 | + IoRefusalHost() { |
| 38 | + pineforge::source::PineStrategyConfig config; |
| 39 | + config.initial_capital = 1e12; |
| 40 | + config.margin_long = config.margin_short = 0.0; |
| 41 | + config.pyramiding = 5000; |
| 42 | + configure_pine_strategy(config); |
| 43 | + set_syminfo_mintick(0.25); |
| 44 | + set_syminfo_timezone("UTC"); |
| 45 | + set_syminfo_session("24x7"); |
| 46 | + } |
| 47 | + |
| 48 | + void on_source_bar(const pineforge::Bar&) override { |
| 49 | + if (pine_bar_index() == 0) probe(); |
| 50 | + } |
| 51 | + |
| 52 | + bool probed = false; |
| 53 | + std::string verdict; |
| 54 | + |
| 55 | +private: |
| 56 | + void fail(const std::string& why) { |
| 57 | + if (verdict.empty()) verdict = why; |
| 58 | + } |
| 59 | + |
| 60 | + void probe() { |
| 61 | + probed = true; |
| 62 | + auto& table = adapter_.*access(Cohorts{}); |
| 63 | + // A regular file is not a directory: a scratch child of it cannot be created. |
| 64 | + table.set_directory("/dev/null/pineforge-cr365-r2-refused"); |
| 65 | + // Setup proof, independent of cohort_for and of the kernel: the table |
| 66 | + // itself refuses to place a row, and stays empty. |
| 67 | + using Facts = std::decay_t<decltype(table.find(std::string())->second)>; |
| 68 | + bool setup_refused = false; |
| 69 | + try { |
| 70 | + table.emplace("cr365-setup-probe", Facts{}); |
| 71 | + } catch (const std::system_error&) { |
| 72 | + setup_refused = true; |
| 73 | + } |
| 74 | + const auto logical = table.size(); |
| 75 | + const auto first = cohort_open(); |
| 76 | + const auto hash_before = broker_state_hash_projection(); |
| 77 | + bool refused = false; |
| 78 | + std::string what; |
| 79 | + try { |
| 80 | + (void)(adapter_.*access(CohortFor{}))("cr365-admission-io-refused"); |
| 81 | + } catch (const std::system_error& error) { |
| 82 | + refused = true; |
| 83 | + what = error.what(); |
| 84 | + } |
| 85 | + const auto hash_after = broker_state_hash_projection(); |
| 86 | + const auto second = cohort_open(); |
| 87 | + std::printf("R2-A2 setup_refused=%d logical=%llu first_handle=%llu second_handle=%llu " |
| 88 | + "refused=%d same_hash=%d text=\"%s\"\n", |
| 89 | + setup_refused ? 1 : 0, static_cast<unsigned long long>(logical), |
| 90 | + static_cast<unsigned long long>(first.value), |
| 91 | + static_cast<unsigned long long>(second.value), refused ? 1 : 0, |
| 92 | + hash_after == hash_before ? 1 : 0, what.c_str()); |
| 93 | + if (!setup_refused || logical != 0) |
| 94 | + return fail("SETUP: the injected scratch fault did not refuse a table placement"); |
| 95 | + if (!refused) |
| 96 | + return fail("BEHAVIOR: cohort_for accepted a new id although the table refuses placement"); |
| 97 | + if (table.size() != logical) |
| 98 | + return fail("BEHAVIOR: the refused cohort_for left a source row behind"); |
| 99 | + if (second.value != first.value + 1) |
| 100 | + return fail("BEHAVIOR: the refused cohort_for consumed a host cohort handle (next=" |
| 101 | + + std::to_string(second.value) + " expected=" |
| 102 | + + std::to_string(first.value + 1) + ")"); |
| 103 | + if (hash_after != hash_before) |
| 104 | + return fail("BEHAVIOR: the refused cohort_for changed the broker state"); |
| 105 | + } |
| 106 | +}; |
| 107 | +} // namespace |
| 108 | + |
| 109 | +int main() { |
| 110 | + try { |
| 111 | + std::vector<pineforge::Bar> bars(4); |
| 112 | + for (std::size_t i = 0; i < bars.size(); ++i) |
| 113 | + bars[i] = {100.0, 101.0, 99.0, 100.0, 1000.0, |
| 114 | + 1736121600000LL + static_cast<std::int64_t>(i) * 60000}; |
| 115 | + IoRefusalHost host; |
| 116 | + host.run(bars.data(), static_cast<int>(bars.size()), "1", "1"); |
| 117 | + if (!host.probed) throw std::runtime_error("SETUP: the probe bar was never reached"); |
| 118 | + if (!host.verdict.empty()) throw std::runtime_error(host.verdict); |
| 119 | + std::puts("cohort_for admission io refusal: OK"); |
| 120 | + return 0; |
| 121 | + } catch (const std::exception& error) { |
| 122 | + std::fprintf(stderr, "FAIL: %s\n", error.what()); |
| 123 | + return 1; |
| 124 | + } |
| 125 | +} |
0 commit comments