Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init each memory region in init script with zero and fix un-freed buffer #138

Merged
merged 6 commits into from
Jun 26, 2024
35 changes: 34 additions & 1 deletion include/etiss/SimpleMemSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "etiss/System.h"
#include "etiss/make_unique.h"
#include <fstream>
#include <random>

#include <cstring>
#include <iostream>
Expand Down Expand Up @@ -88,8 +89,15 @@ class MemSegment
const etiss::uint64 size_;
access_t mode_;

/// @brief Constructor of Memory Segment
/// @param start_addr Start address of segment
/// @param size Size in bytes
/// @param mode Access Mode (R/W/X)
/// @param name Segment name
/// @param mem Pre-allocated Memory (not overwritten with initString)
/// @param initString String for initialization with imple_mem_system.memseg_initelement_ attr/ If not specified random value allocation
MemSegment(etiss::uint64 start_addr, etiss::uint64 size, access_t mode, const std::string name,
etiss::uint8 *mem = nullptr)
etiss::uint8 *mem = nullptr, std::string initString = 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string default argument should be = "" or removed since it now will be managed by etiss::initialize

: name_(name), start_addr_(start_addr), end_addr_(start_addr + size - 1), size_(size), mode_(mode)
{
if (mem)
Expand All @@ -99,10 +107,35 @@ class MemSegment
else
{
mem_ = new etiss::uint8[size];
memInit(initString);
self_allocated_ = true;
}
}

// Can be overwritten afterwards with load_elf
void memInit(std::string initString = 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string default argument should be = "" or removed since it now will be managed by etiss::initialize

{
static std::default_random_engine generator{ static_cast<uint64_t>(0) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer having the seed (optionally) configurable via INI settings. Otherwise it would be quite hard to reproduce bugs,…

std::uniform_int_distribution<int> random_char_{ 0, 255 };

if (initString == "")
{
for (etiss::uint64 i = 0; i < size_; ++i)
{
mem_[i] = random_char_(generator);
}
}
else
{
const char* data = initString.c_str();
for (etiss::uint64 i = 0; i < size_; ++i)
{
mem_[i] = data[i%strlen(data)];
}
}
}


virtual ~MemSegment(void)
{
if (self_allocated_ == true)
Expand Down
22 changes: 19 additions & 3 deletions src/SimpleMemSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ void SimpleMemSystem::load_segments() {
}
std::stringstream().swap(ss);

ss << "simple_mem_system.memseg_initelement_" << std::setw(2) << std::setfill('0') << i;
std::string initString = etiss::cfg().get<std::string>(ss.str(), "");
std::stringstream().swap(ss);

ss << "simple_mem_system.memseg_image_" << std::setw(2) << std::setfill('0') << i;
std::string image = etiss::cfg().get<std::string>(ss.str(), "");
std::stringstream().swap(ss);
Expand Down Expand Up @@ -131,8 +135,9 @@ void SimpleMemSystem::load_segments() {
etiss::uint8 *buf = nullptr;
size_t fsize = 0;

if (image != "")
{
std::stringstream mem_msg;

if (image != "") {
std::ifstream ifs(image, std::ifstream::binary | std::ifstream::ate);
if (!ifs) {
std::stringstream msg;
Expand All @@ -145,10 +150,21 @@ void SimpleMemSystem::load_segments() {
buf = new etiss::uint8[fsize];

ifs.read((char*)buf, fsize);

mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " bytes from input_image !";
etiss::log(etiss::INFO, mem_msg.str());
} else if (initString != "") {
mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with value: " << initString;
etiss::log(etiss::INFO, mem_msg.str());
} else {
mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " random values !";
etiss::log(etiss::INFO, mem_msg.str());
}

auto mseg = std::make_unique<MemSegment>(origin, length, static_cast<MemSegment::access_t>(access), sname.str(), nullptr);

auto mseg = std::make_unique<MemSegment>(origin, length, static_cast<MemSegment::access_t>(access), sname.str(), buf, initString);
add_memsegment(mseg, buf, fsize);
delete[] buf;
}
}
}
Expand Down