Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions concore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Concore{
string inpath = "./in";
string outpath = "./out";

static constexpr size_t SHM_SIZE = 4096;

int shmId_create = -1;
int shmId_get = -1;

Expand Down Expand Up @@ -259,10 +261,24 @@ class Concore{
*/
void createSharedMemory(key_t key)
{
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);

if (shmId_create == -1) {
std::cerr << "Failed to create shared memory segment." << std::endl;
return;
}

// Verify the segment is large enough (shmget won't resize an existing segment)
struct shmid_ds shm_info;
if (shmctl(shmId_create, IPC_STAT, &shm_info) == 0 && shm_info.shm_segsz < SHM_SIZE) {
std::cerr << "Shared memory segment too small (" << shm_info.shm_segsz
<< " bytes, need " << SHM_SIZE << "). Removing and recreating." << std::endl;
shmctl(shmId_create, IPC_RMID, nullptr);
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
if (shmId_create == -1) {
std::cerr << "Failed to recreate shared memory segment." << std::endl;
return;
}
}

// Attach the shared memory segment to the process's address space
Expand All @@ -284,7 +300,7 @@ class Concore{
const int MAX_RETRY = 100;
while (retry < MAX_RETRY) {
// Get the shared memory segment created by Writer
shmId_get = shmget(key, 256, 0666);
shmId_get = shmget(key, SHM_SIZE, 0666);
// Check if shared memory exists
if (shmId_get != -1) {
break; // Break the loop if shared memory exists
Expand Down Expand Up @@ -490,7 +506,7 @@ class Concore{
try {
if (shmId_get != -1) {
if (sharedData_get && sharedData_get[0] != '\0') {
std::string message(sharedData_get, strnlen(sharedData_get, 256));
std::string message(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
ins = message;
}
else
Expand All @@ -515,7 +531,7 @@ class Concore{
this_thread::sleep_for(timespan);
try{
if(shmId_get != -1) {
std::string message(sharedData_get, strnlen(sharedData_get, 256));
std::string message(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
ins = message;
retrycount++;
}
Expand Down Expand Up @@ -658,13 +674,21 @@ class Concore{
try {
std::ostringstream outfile;
if(shmId_create != -1){
if (sharedData_create == nullptr)
throw 506;
val.insert(val.begin(),simtime+delta);
outfile<<'[';
for(int i=0;i<val.size()-1;i++)
outfile<<val[i]<<',';
outfile<<val[val.size()-1]<<']';
std::string result = outfile.str();
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
if (result.size() >= SHM_SIZE) {
std::cerr << "ERROR: write_SM payload (" << result.size()
<< " bytes) exceeds " << SHM_SIZE - 1
<< "-byte shared memory limit. Data truncated!" << std::endl;
}
std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1);
sharedData_create[SHM_SIZE - 1] = '\0';
// simtime must not be mutated here (issue #385).
}
else{
Expand All @@ -689,7 +713,15 @@ class Concore{
this_thread::sleep_for(timespan);
try {
if(shmId_create != -1){
std::strncpy(sharedData_create, val.c_str(), 256 - 1);
if (sharedData_create == nullptr)
throw 506;
if (val.size() >= SHM_SIZE) {
std::cerr << "ERROR: write_SM payload (" << val.size()
<< " bytes) exceeds " << SHM_SIZE - 1
<< "-byte shared memory limit. Data truncated!" << std::endl;
}
std::strncpy(sharedData_create, val.c_str(), SHM_SIZE - 1);
sharedData_create[SHM_SIZE - 1] = '\0';
}
else throw 505;
}
Expand Down
34 changes: 29 additions & 5 deletions concoredocker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

class Concore {
private:
static constexpr size_t SHM_SIZE = 4096;

int shmId_create = -1;
int shmId_get = -1;
char* sharedData_create = nullptr;
Expand Down Expand Up @@ -233,11 +235,25 @@ class Concore {

#ifdef __linux__
void createSharedMemory(key_t key) {
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
if (shmId_create == -1) {
std::cerr << "Failed to create shared memory segment.\n";
return;
}

// Verify the segment is large enough (shmget won't resize an existing segment)
struct shmid_ds shm_info;
if (shmctl(shmId_create, IPC_STAT, &shm_info) == 0 && shm_info.shm_segsz < SHM_SIZE) {
std::cerr << "Shared memory segment too small (" << shm_info.shm_segsz
<< " bytes, need " << SHM_SIZE << "). Removing and recreating.\n";
shmctl(shmId_create, IPC_RMID, nullptr);
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
if (shmId_create == -1) {
std::cerr << "Failed to recreate shared memory segment.\n";
return;
}
}

sharedData_create = static_cast<char*>(shmat(shmId_create, NULL, 0));
if (sharedData_create == reinterpret_cast<char*>(-1)) {
std::cerr << "Failed to attach shared memory segment.\n";
Expand All @@ -249,7 +265,7 @@ class Concore {
int retry = 0;
const int MAX_RETRY = 100;
while (retry < MAX_RETRY) {
shmId_get = shmget(key, 256, 0666);
shmId_get = shmget(key, SHM_SIZE, 0666);
if (shmId_get != -1)
break;
std::cout << "Shared memory does not exist. Make sure the writer process is running.\n";
Expand Down Expand Up @@ -345,7 +361,7 @@ class Concore {
std::string ins;
try {
if (shmId_get != -1 && sharedData_get && sharedData_get[0] != '\0')
ins = std::string(sharedData_get, strnlen(sharedData_get, 256));
ins = std::string(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
else
throw 505;
} catch (...) {
Expand All @@ -359,7 +375,7 @@ class Concore {
std::this_thread::sleep_for(std::chrono::seconds(delay));
try {
if (shmId_get != -1 && sharedData_get) {
ins = std::string(sharedData_get, strnlen(sharedData_get, 256));
ins = std::string(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
retrycount++;
} else {
retrycount++;
Expand Down Expand Up @@ -419,14 +435,22 @@ class Concore {
try {
if (shmId_create == -1)
throw 505;
if (sharedData_create == nullptr)
throw 506;
val.insert(val.begin(), simtime + delta);
std::ostringstream outfile;
outfile << '[';
for (size_t i = 0; i < val.size() - 1; i++)
outfile << val[i] << ',';
outfile << val[val.size() - 1] << ']';
std::string result = outfile.str();
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
if (result.size() >= SHM_SIZE) {
std::cerr << "ERROR: write_SM payload (" << result.size()
<< " bytes) exceeds " << SHM_SIZE - 1
<< "-byte shared memory limit. Data truncated!" << std::endl;
}
std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1);
sharedData_create[SHM_SIZE - 1] = '\0';
} catch (...) {
std::cerr << "skipping +" << outpath << port << "/" << name << "\n";
}
Expand Down
Loading