Unverified Commit 4d89b54e authored by Quan (Andy) Gan's avatar Quan (Andy) Gan Committed by GitHub
Browse files

[Bug] Fix munmap() using wrong parameter (#2519)



* fix munmap() using wrong parameter

* rename variables
Co-authored-by: default avatarMinjie Wang <wmjlyjemaine@gmail.com>
parent a6ec8480
...@@ -25,14 +25,14 @@ class SharedMemory { ...@@ -25,14 +25,14 @@ class SharedMemory {
* If shared memory is created in the object, it'll be owned by the object * If shared memory is created in the object, it'll be owned by the object
* and will be responsible for deleting it when the object is destroyed. * and will be responsible for deleting it when the object is destroyed.
*/ */
bool own; bool own_;
/* \brief the file descripter of the shared memory. */ /* \brief the file descripter of the shared memory. */
int fd; int fd_;
/* \brief the address of the shared memory. */ /* \brief the address of the shared memory. */
void *ptr; void *ptr_;
/* \brief the size of the shared memory. */ /* \brief the size of the shared memory. */
size_t size; size_t size_;
/* /*
* \brief the name of the object. * \brief the name of the object.
...@@ -60,16 +60,16 @@ class SharedMemory { ...@@ -60,16 +60,16 @@ class SharedMemory {
/* /*
* \brief create shared memory. * \brief create shared memory.
* It creates the file and shared memory. * It creates the file and shared memory.
* \param size the size of the shared memory. * \param sz the size of the shared memory.
* \return the address of the shared memory * \return the address of the shared memory
*/ */
void *CreateNew(size_t size); void *CreateNew(size_t sz);
/* /*
* \brief allocate shared memory that has been created. * \brief allocate shared memory that has been created.
* \param size the size of the shared memory. * \param sz the size of the shared memory.
* \return the address of the shared memory * \return the address of the shared memory
*/ */
void *Open(size_t size); void *Open(size_t sz);
/* /*
* \brief check if the shared memory exist. * \brief check if the shared memory exist.
......
...@@ -41,10 +41,10 @@ class SharedMemoryResource: public Resource { ...@@ -41,10 +41,10 @@ class SharedMemoryResource: public Resource {
SharedMemory::SharedMemory(const std::string &name) { SharedMemory::SharedMemory(const std::string &name) {
#ifndef _WIN32 #ifndef _WIN32
this->name = name; this->name = name;
this->own = false; this->own_ = false;
this->fd = -1; this->fd_ = -1;
this->ptr = nullptr; this->ptr_ = nullptr;
this->size = 0; this->size_ = 0;
#else #else
LOG(FATAL) << "Shared memory is not supported on Windows."; LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif // _WIN32 #endif // _WIN32
...@@ -52,11 +52,11 @@ SharedMemory::SharedMemory(const std::string &name) { ...@@ -52,11 +52,11 @@ SharedMemory::SharedMemory(const std::string &name) {
SharedMemory::~SharedMemory() { SharedMemory::~SharedMemory() {
#ifndef _WIN32 #ifndef _WIN32
munmap(ptr, size); CHECK(munmap(ptr_, size_) != -1) << strerror(errno);
close(fd); close(fd_);
if (own) { if (own_) {
LOG(INFO) << "remove " << name << " for shared memory"; LOG(INFO) << "remove " << name << " for shared memory";
shm_unlink(name.c_str()); CHECK(shm_unlink(name.c_str()) != -1) << strerror(errno);
// The resource has been deleted. We don't need to keep track of it any more. // The resource has been deleted. We don't need to keep track of it any more.
DeleteResource(name); DeleteResource(name);
} }
...@@ -65,38 +65,40 @@ SharedMemory::~SharedMemory() { ...@@ -65,38 +65,40 @@ SharedMemory::~SharedMemory() {
#endif // _WIN32 #endif // _WIN32
} }
void *SharedMemory::CreateNew(size_t size) { void *SharedMemory::CreateNew(size_t sz) {
#ifndef _WIN32 #ifndef _WIN32
this->own = true; this->own_ = true;
// We need to create a shared-memory file. // We need to create a shared-memory file.
// TODO(zhengda) we need to report error if the shared-memory file exists. // TODO(zhengda) we need to report error if the shared-memory file exists.
int flag = O_RDWR|O_CREAT; int flag = O_RDWR|O_CREAT;
fd = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR); fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
CHECK_NE(fd, -1) << "fail to open " << name << ": " << strerror(errno); CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
// Shared memory cannot be deleted if the process exits abnormally. // Shared memory cannot be deleted if the process exits abnormally.
AddResource(name, std::shared_ptr<Resource>(new SharedMemoryResource(name))); AddResource(name, std::shared_ptr<Resource>(new SharedMemoryResource(name)));
auto res = ftruncate(fd, size); auto res = ftruncate(fd_, sz);
CHECK_NE(res, -1) CHECK_NE(res, -1)
<< "Failed to truncate the file. " << strerror(errno); << "Failed to truncate the file. " << strerror(errno);
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
CHECK_NE(ptr, MAP_FAILED) CHECK_NE(ptr_, MAP_FAILED)
<< "Failed to map shared memory. mmap failed with error " << strerror(errno); << "Failed to map shared memory. mmap failed with error " << strerror(errno);
return ptr; this->size_ = sz;
return ptr_;
#else #else
LOG(FATAL) << "Shared memory is not supported on Windows."; LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif // _WIN32 #endif // _WIN32
} }
void *SharedMemory::Open(size_t size) { void *SharedMemory::Open(size_t sz) {
#ifndef _WIN32 #ifndef _WIN32
int flag = O_RDWR; int flag = O_RDWR;
fd = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR); fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
CHECK_NE(fd, -1) << "fail to open " << name << ": " << strerror(errno); CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
CHECK_NE(ptr, MAP_FAILED) CHECK_NE(ptr_, MAP_FAILED)
<< "Failed to map shared memory. mmap failed with error " << strerror(errno); << "Failed to map shared memory. mmap failed with error " << strerror(errno);
return ptr; this->size_ = sz;
return ptr_;
#else #else
LOG(FATAL) << "Shared memory is not supported on Windows."; LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif // _WIN32 #endif // _WIN32
...@@ -104,9 +106,9 @@ void *SharedMemory::Open(size_t size) { ...@@ -104,9 +106,9 @@ void *SharedMemory::Open(size_t size) {
bool SharedMemory::Exist(const std::string &name) { bool SharedMemory::Exist(const std::string &name) {
#ifndef _WIN32 #ifndef _WIN32
int fd = shm_open(name.c_str(), O_RDONLY, S_IRUSR | S_IWUSR); int fd_ = shm_open(name.c_str(), O_RDONLY, S_IRUSR | S_IWUSR);
if (fd >= 0) { if (fd_ >= 0) {
close(fd); close(fd_);
return true; return true;
} else { } else {
return false; return false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment