warfactoryracine/include/warfactory/gsl_contracts.hpp
StillHammer 5bdd072117 Complete 10-engine architecture with CMake build system
 All 10 engines now build successfully:
- Designer, Economy, Event, Factory, Intelligence
- Logistic, MacroEntity, Map, Operation, War

🚀 Features implemented:
- FAST_BUILD vs FULL_BUILD presets for efficient development
- Comprehensive defensive programming (sanitizers, contracts)
- 16 C++ libraries integrated via FetchContent
- GCC-compatible configuration with stack protection
- Unified CMake system across all engines

🛠️ Build commands:
- Fast: cmake -DFAST_BUILD=ON .. && make claude-workflow-fast
- Full: cmake .. && make (all sanitizers + validation)
- Single engine: make economy-engine

🔧 Development workflow optimized for daily iteration.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-19 14:19:25 +08:00

192 lines
6.5 KiB
C++

#pragma once
// Guidelines Support Library integration pour Warfactory
// Contracts automatiques et safety guarantees
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
#include <gsl/gsl>
#else
// Fallback lightweight implementation
namespace gsl {
template<typename T>
class not_null {
T ptr_;
public:
constexpr not_null(T ptr) : ptr_(ptr) {
if (ptr == nullptr) {
std::terminate(); // Fail-fast
}
}
constexpr T get() const { return ptr_; }
constexpr operator T() const { return ptr_; }
constexpr T operator->() const { return ptr_; }
constexpr auto operator*() const { return *ptr_; }
};
template<typename T>
class span {
T* data_;
size_t size_;
public:
constexpr span(T* data, size_t size) : data_(data), size_(size) {}
constexpr T* data() const { return data_; }
constexpr size_t size() const { return size_; }
constexpr T& operator[](size_t idx) const {
if (idx >= size_) std::terminate();
return data_[idx];
}
};
}
#endif
#include <warfactory/contracts.hpp>
namespace warfactory::gsl_integration {
// =============================================================================
// ENHANCED CONTRACTS WITH GSL
// =============================================================================
// Wrapper pour fonctions qui ne peuvent pas retourner null
template<typename T>
using non_null_result = gsl::not_null<T>;
// Safe array access avec bounds checking
template<typename T>
using safe_array = gsl::span<T>;
// =============================================================================
// EXAMPLES FOR WARFACTORY ENGINES
// =============================================================================
class SafeMarketData {
private:
std::vector<double> prices_;
public:
// Function qui garantit un pointeur non-null
non_null_result<const double*> get_price_ptr(size_t index) const {
WARFACTORY_REQUIRE(index < prices_.size());
const double* ptr = &prices_[index];
WARFACTORY_ENSURE_NOT_NULL(ptr);
return gsl::not_null<const double*>(ptr); // Compile-time guarantee
}
// Safe array access avec GSL span
safe_array<const double> get_prices_view() const {
if (prices_.empty()) {
WARFACTORY_FAIL_FAST_IF(true); // Empty view serait dangereux
}
return gsl::span<const double>(prices_.data(), prices_.size());
}
// GSL contracts combinés avec nos contracts Warfactory
void add_price_safe(gsl::not_null<const double*> price_ptr, double volume) {
WARFACTORY_REQUIRE(std::isfinite(*price_ptr));
WARFACTORY_REQUIRE(std::isfinite(volume));
WARFACTORY_REQUIRE(volume > 0.0);
// GSL garantit que price_ptr n'est pas null
prices_.push_back(*price_ptr);
WARFACTORY_ENSURE(!prices_.empty());
WARFACTORY_PROPERTY_FINITE_VALUES(prices_.back());
}
};
// =============================================================================
// FACTORY PRODUCTION WITH GSL SAFETY
// =============================================================================
class SafeProduction {
public:
struct ProductionStep {
gsl::span<const double> inputs;
gsl::span<double> outputs;
// GSL contract: inputs et outputs même taille
ProductionStep(gsl::span<const double> in, gsl::span<double> out)
: inputs(in), outputs(out) {
if (inputs.size() != outputs.size()) {
WARFACTORY_FAIL_FAST_IF(true);
}
}
};
void process_production_step(const ProductionStep& step) {
WARFACTORY_REQUIRE(step.inputs.size() > 0);
WARFACTORY_REQUIRE(step.outputs.size() > 0);
double total_input = 0.0;
double total_output = 0.0;
// GSL span garantit les bounds checks
for (size_t i = 0; i < step.inputs.size(); ++i) {
const double input = step.inputs[i]; // Bounds check automatique
double& output = step.outputs[i]; // Bounds check automatique
WARFACTORY_PROPERTY_FINITE_VALUES(input);
WARFACTORY_REQUIRE(input >= 0.0);
// Simulation de transformation
output = input * 0.9; // 10% loss
total_input += input;
total_output += output;
WARFACTORY_PROPERTY_FINITE_VALUES(output);
}
// Conservation de masse avec tolérance
WARFACTORY_PROPERTY_CONSERVATION_OF_MASS(total_input, total_output * 1.111); // +10% tolerance
}
};
// =============================================================================
// FUZZING TARGET FUNCTIONS
// =============================================================================
// Function spécialement conçue pour fuzzing avec GSL safety
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size < sizeof(double) * 2) {
return 0; // Input trop petit
}
try {
// Convert raw bytes to doubles safely
gsl::span<const uint8_t> input_data(data, size);
if (input_data.size() >= sizeof(double) * 2) {
// Extract two doubles from fuzzer input
double price, volume;
std::memcpy(&price, input_data.data(), sizeof(double));
std::memcpy(&volume, input_data.data() + sizeof(double), sizeof(double));
// Test our market with fuzzer input
SafeMarketData market;
// GSL + Contracts will catch invalid inputs
if (std::isfinite(price) && std::isfinite(volume) && volume > 0.0) {
market.add_price_safe(gsl::not_null<const double*>(&price), volume);
auto view = market.get_prices_view();
if (view.size() > 0) {
auto price_ptr = market.get_price_ptr(0);
WARFACTORY_PROPERTY_FINITE_VALUES(*price_ptr);
}
}
}
return 0; // Success
} catch (const warfactory::contracts::ContractViolation&) {
return 0; // Expected contract violation
} catch (...) {
return 1; // Unexpected error - fuzzer should explore this
}
}
} // namespace warfactory::gsl_integration