|
| 1 | +// Validates that handle_operation does not overwrite past the requested length |
| 2 | +#include <array> |
| 3 | +#include <systemc> |
| 4 | +#include <tlm> |
| 5 | +#include <gtest/gtest.h> |
| 6 | + |
| 7 | +#include "components/scc/memory.h" |
| 8 | +#include "scc/cci_broker.h" |
| 9 | + |
| 10 | +TEST(MemoryBoundaryTest, ReadAcrossPageDoesNotOverwriteBuffer) { |
| 11 | + static scc::cci_broker global_broker("global_broker"); |
| 12 | + static auto broker_handle = cci::cci_register_broker(&global_broker); |
| 13 | + constexpr uint64_t kPageSize = 1ull << 24; // matches util::sparse_array default page size |
| 14 | + scc::memory<(kPageSize + 2u)> mem{"mem"}; |
| 15 | + |
| 16 | + std::array<uint8_t, 2> write_data{{0xAAu, 0xBBu}}; |
| 17 | + tlm::tlm_generic_payload write; |
| 18 | + sc_core::sc_time delay = sc_core::SC_ZERO_TIME; |
| 19 | + write.set_command(tlm::TLM_WRITE_COMMAND); |
| 20 | + write.set_address(kPageSize - 1); // straddles page boundary |
| 21 | + write.set_data_length(write_data.size()); |
| 22 | + write.set_streaming_width(write_data.size()); |
| 23 | + write.set_data_ptr(write_data.data()); |
| 24 | + mem.handle_operation(write, delay); |
| 25 | + |
| 26 | + std::array<uint8_t, 4> read_buf{{0xEEu, 0x00u, 0x00u, 0xEEu}}; |
| 27 | + tlm::tlm_generic_payload read; |
| 28 | + read.set_command(tlm::TLM_READ_COMMAND); |
| 29 | + read.set_address(kPageSize - 1); |
| 30 | + read.set_data_length(write_data.size()); |
| 31 | + read.set_streaming_width(write_data.size()); |
| 32 | + read.set_data_ptr(read_buf.data() + 1); // leave guard bytes at both ends |
| 33 | + mem.handle_operation(read, delay); |
| 34 | + |
| 35 | + EXPECT_EQ(read_buf[0], 0xEEu); // leading guard untouched |
| 36 | + EXPECT_EQ(read_buf[1], 0xAAu); // first byte read correctly |
| 37 | + EXPECT_EQ(read_buf[2], 0xBBu); // second byte read correctly |
| 38 | + EXPECT_EQ(read_buf[3], 0xEEu); // trailing guard must remain |
| 39 | +} |
0 commit comments