From cf308d96dcce19653a4a23ad8bc2826886176a69 Mon Sep 17 00:00:00 2001 From: jokap11 Date: Tue, 8 Aug 2023 11:40:34 +0200 Subject: [PATCH 1/6] Init each memory region in init script with zero and fix un-freed buffer --- src/SimpleMemSystem.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/SimpleMemSystem.cpp b/src/SimpleMemSystem.cpp index 5d1cd6c112..bf66085cbb 100644 --- a/src/SimpleMemSystem.cpp +++ b/src/SimpleMemSystem.cpp @@ -145,10 +145,17 @@ void SimpleMemSystem::load_segments() { buf = new etiss::uint8[fsize]; ifs.read((char*)buf, fsize); + }else{ + std::stringstream msg; + msg << "This memory segment " << i << " is initialized with 0x" << std::hex << length << " zeros !"; + etiss::log(etiss::INFO, msg.str()); + fsize = length; + buf = new etiss::uint8[fsize](); } auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), nullptr); add_memsegment(mseg, buf, fsize); + delete[] buf; } } } From 9c00cbb3484d1747d72c42dbc835f32362781ac4 Mon Sep 17 00:00:00 2001 From: jokap11 Date: Wed, 29 May 2024 14:13:43 +0200 Subject: [PATCH 2/6] Added new memseg_initelement_ to initialize memsegs in SimpleMemSys (Default init=random) --- include/etiss/SimpleMemSystem.h | 30 +++++++++++++++++++++++++++++- src/SimpleMemSystem.cpp | 22 ++++++++++++++++------ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/include/etiss/SimpleMemSystem.h b/include/etiss/SimpleMemSystem.h index 099b4061aa..1180f060c9 100644 --- a/include/etiss/SimpleMemSystem.h +++ b/include/etiss/SimpleMemSystem.h @@ -53,6 +53,7 @@ #include "etiss/System.h" #include "etiss/make_unique.h" #include +#include #include #include @@ -89,7 +90,7 @@ class MemSegment access_t mode_; MemSegment(etiss::uint64 start_addr, etiss::uint64 size, access_t mode, const std::string name, - etiss::uint8 *mem = nullptr) + etiss::uint8 *mem = nullptr, uint8_t initVal = 0) : name_(name), start_addr_(start_addr), end_addr_(start_addr + size - 1), size_(size), mode_(mode) { if (mem) @@ -99,10 +100,37 @@ class MemSegment else { mem_ = new etiss::uint8[size]; + memInit(initVal); self_allocated_ = true; } } + // Can be overwritten afterwards with load_elf + void memInit(uint8_t initVal = 0) + { + static std::default_random_engine generator{ static_cast(0) }; + std::uniform_int_distribution random_char_{ 0, 255 }; + + if (initVal == 0){ + for (etiss::uint64 i = 0; i < size_; ++i) + { + mem_[i] = random_char_(generator); + } + } + else + { + memset(mem_, initVal, size_); + } + // std::stringstream mem_msg; + // mem_msg << "This memory segment " << " is initialized with 0x" << std::hex << size_ << " bytes! \n"; + // for (etiss::uint64 i = 0; i < size_; ++i) + // { + // mem_msg << static_cast( mem_[i]) << ":"; + // } + // etiss::log(etiss::INFO, mem_msg.str()); + } + + virtual ~MemSegment(void) { if (self_allocated_ == true) diff --git a/src/SimpleMemSystem.cpp b/src/SimpleMemSystem.cpp index bf66085cbb..3d356015c2 100644 --- a/src/SimpleMemSystem.cpp +++ b/src/SimpleMemSystem.cpp @@ -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; + uint8_t initVal = static_cast(etiss::cfg().get(ss.str(), 0)); + std::stringstream().swap(ss); + ss << "simple_mem_system.memseg_image_" << std::setw(2) << std::setfill('0') << i; std::string image = etiss::cfg().get(ss.str(), ""); std::stringstream().swap(ss); @@ -131,6 +135,8 @@ void SimpleMemSystem::load_segments() { etiss::uint8 *buf = nullptr; size_t fsize = 0; + std::stringstream mem_msg; + if (image != "") { std::ifstream ifs(image, std::ifstream::binary | std::ifstream::ate); @@ -145,15 +151,19 @@ 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 (initVal != 0){ + mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with value: " << static_cast(initVal); + etiss::log(etiss::INFO, mem_msg.str()); }else{ - std::stringstream msg; - msg << "This memory segment " << i << " is initialized with 0x" << std::hex << length << " zeros !"; - etiss::log(etiss::INFO, msg.str()); - fsize = length; - buf = new etiss::uint8[fsize](); + 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(origin, length, static_cast(access), sname.str(), nullptr); + + auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), buf, initVal); add_memsegment(mseg, buf, fsize); delete[] buf; } From 64138b4414eb6e17385091afdf7dbbe7e9104ff6 Mon Sep 17 00:00:00 2001 From: jokap11 Date: Wed, 12 Jun 2024 14:10:47 +0200 Subject: [PATCH 3/6] Changed initElement to type String | random init= default --- include/etiss/SimpleMemSystem.h | 29 +++++++++++++++++------------ src/SimpleMemSystem.cpp | 13 ++++++------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/include/etiss/SimpleMemSystem.h b/include/etiss/SimpleMemSystem.h index 1180f060c9..6aa8bdfac0 100644 --- a/include/etiss/SimpleMemSystem.h +++ b/include/etiss/SimpleMemSystem.h @@ -89,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, uint8_t initVal = 0) + etiss::uint8 *mem = nullptr, std::string initString = 0) : name_(name), start_addr_(start_addr), end_addr_(start_addr + size - 1), size_(size), mode_(mode) { if (mem) @@ -100,18 +107,19 @@ class MemSegment else { mem_ = new etiss::uint8[size]; - memInit(initVal); + memInit(initString); self_allocated_ = true; } } // Can be overwritten afterwards with load_elf - void memInit(uint8_t initVal = 0) + void memInit(std::string initString = 0) { static std::default_random_engine generator{ static_cast(0) }; std::uniform_int_distribution random_char_{ 0, 255 }; - if (initVal == 0){ + if (initString == "") + { for (etiss::uint64 i = 0; i < size_; ++i) { mem_[i] = random_char_(generator); @@ -119,15 +127,12 @@ class MemSegment } else { - memset(mem_, initVal, size_); + const char* data = initString.c_str(); + for (etiss::uint64 i = 0; i < size_; ++i) + { + mem_[i] = data[i%strlen(data)]; + } } - // std::stringstream mem_msg; - // mem_msg << "This memory segment " << " is initialized with 0x" << std::hex << size_ << " bytes! \n"; - // for (etiss::uint64 i = 0; i < size_; ++i) - // { - // mem_msg << static_cast( mem_[i]) << ":"; - // } - // etiss::log(etiss::INFO, mem_msg.str()); } diff --git a/src/SimpleMemSystem.cpp b/src/SimpleMemSystem.cpp index 3d356015c2..867ec8c73a 100644 --- a/src/SimpleMemSystem.cpp +++ b/src/SimpleMemSystem.cpp @@ -98,7 +98,7 @@ void SimpleMemSystem::load_segments() { std::stringstream().swap(ss); ss << "simple_mem_system.memseg_initelement_" << std::setw(2) << std::setfill('0') << i; - uint8_t initVal = static_cast(etiss::cfg().get(ss.str(), 0)); + std::string initString = etiss::cfg().get(ss.str(), ""); std::stringstream().swap(ss); ss << "simple_mem_system.memseg_image_" << std::setw(2) << std::setfill('0') << i; @@ -137,8 +137,7 @@ void SimpleMemSystem::load_segments() { std::stringstream mem_msg; - if (image != "") - { + if (image != "") { std::ifstream ifs(image, std::ifstream::binary | std::ifstream::ate); if (!ifs) { std::stringstream msg; @@ -154,16 +153,16 @@ void SimpleMemSystem::load_segments() { 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 (initVal != 0){ - mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with value: " << static_cast(initVal); + } 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{ + } 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(origin, length, static_cast(access), sname.str(), buf, initVal); + auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), buf, initString); add_memsegment(mseg, buf, fsize); delete[] buf; } From 03cc16fc912c2e00732396de83e1c03db8c3a27c Mon Sep 17 00:00:00 2001 From: jokap11 Date: Wed, 12 Jun 2024 14:28:10 +0200 Subject: [PATCH 4/6] Removed function default values for memInit and MemSegment constr --- include/etiss/SimpleMemSystem.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/etiss/SimpleMemSystem.h b/include/etiss/SimpleMemSystem.h index 6aa8bdfac0..e9b8d0d401 100644 --- a/include/etiss/SimpleMemSystem.h +++ b/include/etiss/SimpleMemSystem.h @@ -97,7 +97,7 @@ class MemSegment /// @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, std::string initString = 0) + etiss::uint8 *mem = nullptr, std::string initString) : name_(name), start_addr_(start_addr), end_addr_(start_addr + size - 1), size_(size), mode_(mode) { if (mem) @@ -113,7 +113,7 @@ class MemSegment } // Can be overwritten afterwards with load_elf - void memInit(std::string initString = 0) + void memInit(std::string initString) { static std::default_random_engine generator{ static_cast(0) }; std::uniform_int_distribution random_char_{ 0, 255 }; From 1a7de070855a4c9095b9780b976b2c31167f60b2 Mon Sep 17 00:00:00 2001 From: jokap11 Date: Thu, 13 Jun 2024 14:41:11 +0200 Subject: [PATCH 5/6] Implemented different InitVal types like string/hex_val/uninit --- include/etiss/SimpleMemSystem.h | 28 ++++++++++++++++++++++++---- src/SimpleMemSystem.cpp | 22 ++++++++++++++++------ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/include/etiss/SimpleMemSystem.h b/include/etiss/SimpleMemSystem.h index e9b8d0d401..cd3b706eca 100644 --- a/include/etiss/SimpleMemSystem.h +++ b/include/etiss/SimpleMemSystem.h @@ -97,7 +97,7 @@ class MemSegment /// @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, std::string initString) + etiss::uint8 *mem = nullptr, std::string initString = "", bool InitEleSet = false) : name_(name), start_addr_(start_addr), end_addr_(start_addr + size - 1), size_(size), mode_(mode) { if (mem) @@ -107,7 +107,8 @@ class MemSegment else { mem_ = new etiss::uint8[size]; - memInit(initString); + if (InitEleSet) + memInit(initString); self_allocated_ = true; } } @@ -118,11 +119,30 @@ class MemSegment static std::default_random_engine generator{ static_cast(0) }; std::uniform_int_distribution random_char_{ 0, 255 }; - if (initString == "") + if (initString.find("0x") == 0) { + initString.erase(initString.begin(),initString.begin()+2); + std::stringstream mem_msg; for (etiss::uint64 i = 0; i < size_; ++i) { - mem_[i] = random_char_(generator); + const char* dataPtr = initString.substr(i%initString.length(),1).c_str(); + char** endPtr = nullptr; + errno = 0; + uint8_t hexVal = static_cast(strtol(dataPtr, endPtr, 16)); + mem_[i] = hexVal; + + if (errno != 0){ + etiss::log(etiss::WARNING, "Hex Value MemSegment input is erronous (typo?)"); + throw "MemSegmentInit for hex value is errnounous"; + } + } + } + else if (initString.find("random") == 0 || initString.find("RANDOM") == 0) + { + const char* data = initString.c_str(); + for (etiss::uint64 i = 0; i < size_; ++i) + { + mem_[i] = data[i%strlen(data)]; } } else diff --git a/src/SimpleMemSystem.cpp b/src/SimpleMemSystem.cpp index 867ec8c73a..867a7e9b4f 100644 --- a/src/SimpleMemSystem.cpp +++ b/src/SimpleMemSystem.cpp @@ -99,6 +99,7 @@ void SimpleMemSystem::load_segments() { ss << "simple_mem_system.memseg_initelement_" << std::setw(2) << std::setfill('0') << i; std::string initString = etiss::cfg().get(ss.str(), ""); + bool initEleSet = etiss::cfg().isSet(ss.str()); std::stringstream().swap(ss); ss << "simple_mem_system.memseg_image_" << std::setw(2) << std::setfill('0') << i; @@ -153,16 +154,25 @@ void SimpleMemSystem::load_segments() { 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 if (initEleSet) { + if (initString.find("random") == 0 || initString.find("RANDOM")== 0) { + mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " random bytes !"; + etiss::log(etiss::INFO, mem_msg.str()); + } else if (initString.find("0x") == 0) { + mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with hex value: " << initString; + etiss::log(etiss::INFO, mem_msg.str()); + } + else + { + mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with the string: " << initString; + etiss::log(etiss::INFO, mem_msg.str()); + } } else { - mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " random values !"; + mem_msg << "The memory segment " << i << " is allocated uninitialized with length 0x" << std::hex << length << " !"; etiss::log(etiss::INFO, mem_msg.str()); } - - auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), buf, initString); + auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), buf, initString, initEleSet); add_memsegment(mseg, buf, fsize); delete[] buf; } From 6ea7bd2f2c4d231675b6493769bbe4cd53550fc9 Mon Sep 17 00:00:00 2001 From: jokap11 Date: Thu, 13 Jun 2024 23:45:00 +0200 Subject: [PATCH 6/6] Considered PR feedback (strtol->stoi)| randomRoot arg | alloc infos shift --- include/etiss/SimpleMemSystem.h | 69 ++++++++++++++++++++++++--------- src/SimpleMemSystem.cpp | 25 ++++-------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/include/etiss/SimpleMemSystem.h b/include/etiss/SimpleMemSystem.h index cd3b706eca..2241f95bd5 100644 --- a/include/etiss/SimpleMemSystem.h +++ b/include/etiss/SimpleMemSystem.h @@ -95,9 +95,11 @@ class MemSegment /// @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 + /// @param initString String for initialization with imple_mem_system.memseg_initelement_ value: hex_string with 0x... / string /random options + /// @param InitEleSet Should self allocated MemSegment be initialized? + /// @param randomRoot If initString==Random use this value as generator root MemSegment(etiss::uint64 start_addr, etiss::uint64 size, access_t mode, const std::string name, - etiss::uint8 *mem = nullptr, std::string initString = "", bool InitEleSet = false) + etiss::uint8 *mem = nullptr, std::string initString = "", bool InitEleSet = false, uint64_t randomRoot = 0) : name_(name), start_addr_(start_addr), end_addr_(start_addr + size - 1), size_(size), mode_(mode) { if (mem) @@ -108,45 +110,76 @@ class MemSegment { mem_ = new etiss::uint8[size]; if (InitEleSet) - memInit(initString); + { + memInit(initString, randomRoot); + } + else + { + std::stringstream memMsg; + memMsg << "The memory segment is allocated uninitialized with length 0x" << std::hex << size_ << " !"; + etiss::log(etiss::INFO, memMsg.str()); + } self_allocated_ = true; } } // Can be overwritten afterwards with load_elf - void memInit(std::string initString) + void memInit(std::string initString, uint64_t randomRoot = 0) { - static std::default_random_engine generator{ static_cast(0) }; - std::uniform_int_distribution random_char_{ 0, 255 }; + std::stringstream memMsg; if (initString.find("0x") == 0) { + memMsg << "The memory segment is initialized with 0x" << std::hex << size_ << " elements with hex value: " << initString; + etiss::log(etiss::INFO, memMsg.str()); + + // actual conversion from hex string to corresponding hex val initString.erase(initString.begin(),initString.begin()+2); - std::stringstream mem_msg; + const char* dataPtr; + size_t j{0}; for (etiss::uint64 i = 0; i < size_; ++i) { - const char* dataPtr = initString.substr(i%initString.length(),1).c_str(); - char** endPtr = nullptr; - errno = 0; - uint8_t hexVal = static_cast(strtol(dataPtr, endPtr, 16)); - mem_[i] = hexVal; - - if (errno != 0){ - etiss::log(etiss::WARNING, "Hex Value MemSegment input is erronous (typo?)"); - throw "MemSegmentInit for hex value is errnounous"; + if( j != (initString.length()-1)) + { + dataPtr = initString.substr(j, 2).c_str(); + } + else + { + dataPtr = initString.substr(j, 1).c_str(); + } + + j = (j+2 <= initString.length()-1) ? j+2 : 0; + + try + { + uint8_t hexVal = static_cast(std::stoi(dataPtr, 0 ,16)); + mem_[i] = hexVal; + } + catch (std::invalid_argument const& exp) + { + memMsg << "\n Hex Value MemSegment input is erronous (typo?) at " << exp.what(); + etiss::log(etiss::FATALERROR, memMsg.str()); } } } else if (initString.find("random") == 0 || initString.find("RANDOM") == 0) { - const char* data = initString.c_str(); + memMsg << "The memory segment is initialized with 0x" << std::hex << size_ << " random bytes and root: " << randomRoot; + etiss::log(etiss::INFO, memMsg.str()); + + static std::default_random_engine generator{randomRoot}; + std::uniform_int_distribution random_char_{ 0, 255 }; for (etiss::uint64 i = 0; i < size_; ++i) { - mem_[i] = data[i%strlen(data)]; + mem_[i] = random_char_(generator); } + } else { + memMsg << "The memory segment is initialized with 0x" << std::hex << size_ << " elements with the string: " << initString; + etiss::log(etiss::INFO, memMsg.str()); + const char* data = initString.c_str(); for (etiss::uint64 i = 0; i < size_; ++i) { diff --git a/src/SimpleMemSystem.cpp b/src/SimpleMemSystem.cpp index 867a7e9b4f..72e0890421 100644 --- a/src/SimpleMemSystem.cpp +++ b/src/SimpleMemSystem.cpp @@ -102,6 +102,10 @@ void SimpleMemSystem::load_segments() { bool initEleSet = etiss::cfg().isSet(ss.str()); std::stringstream().swap(ss); + ss << "simple_mem_system.memseg_initelement_random_root_" << std::setw(2) << std::setfill('0') << i; + uint64_t randomRoot = etiss::cfg().get(ss.str(), 0); + std::stringstream().swap(ss); + ss << "simple_mem_system.memseg_image_" << std::setw(2) << std::setfill('0') << i; std::string image = etiss::cfg().get(ss.str(), ""); std::stringstream().swap(ss); @@ -136,7 +140,6 @@ void SimpleMemSystem::load_segments() { etiss::uint8 *buf = nullptr; size_t fsize = 0; - std::stringstream mem_msg; if (image != "") { std::ifstream ifs(image, std::ifstream::binary | std::ifstream::ate); @@ -152,27 +155,13 @@ void SimpleMemSystem::load_segments() { ifs.read((char*)buf, fsize); + std::stringstream mem_msg; 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 (initEleSet) { - if (initString.find("random") == 0 || initString.find("RANDOM")== 0) { - mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " random bytes !"; - etiss::log(etiss::INFO, mem_msg.str()); - } else if (initString.find("0x") == 0) { - mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with hex value: " << initString; - etiss::log(etiss::INFO, mem_msg.str()); - } - else - { - mem_msg << "The memory segment " << i << " is initialized with 0x" << std::hex << length << " elements with the string: " << initString; - etiss::log(etiss::INFO, mem_msg.str()); - } - } else { - mem_msg << "The memory segment " << i << " is allocated uninitialized with length 0x" << std::hex << length << " !"; - etiss::log(etiss::INFO, mem_msg.str()); } - auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), buf, initString, initEleSet); + auto mseg = std::make_unique(origin, length, static_cast(access), sname.str(), + buf, initString, initEleSet, randomRoot); add_memsegment(mseg, buf, fsize); delete[] buf; }