Skip to content

Fixed #8048: Crash with SIGBUS when there is no free space on the partition with memory-mapped files #8053

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

Merged
merged 2 commits into from
Mar 25, 2024
Merged
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
64 changes: 64 additions & 0 deletions src/common/isc_sync.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,56 @@ void SharedMemoryBase::unlinkFile(const TEXT* expanded_filename) noexcept

#ifdef UNIX

static inline void reportError(const char* func, CheckStatusWrapper* statusVector)
{
if (!statusVector)
system_call_failed::raise(func);
else
error(statusVector, func, errno);
}

bool allocFileSpace(int fd, off_t offset, FB_SIZE_T length, CheckStatusWrapper* statusVector)
{
#if defined(HAVE_LINUX_FALLOC_H) && defined(HAVE_FALLOCATE)
if (fallocate(fd, 0, offset, length) == 0)
return true;

if (errno != EOPNOTSUPP && errno != ENOSYS)
{
reportError("fallocate", statusVector);
return false;
}
// fallocate is not supported by this kernel or file system
// take the long way around
#endif
static const FB_SIZE_T buf128KSize = 131072;
HalfStaticArray<UCHAR, BUFFER_LARGE> buf;
const FB_SIZE_T bufSize = length < buf128KSize ? length : buf128KSize;

memset(buf.getBuffer(bufSize), 0, bufSize);
os_utils::lseek(fd, LSEEK_OFFSET_CAST offset, SEEK_SET);

while (length)
{
const FB_SIZE_T cnt = length < bufSize ? length : bufSize;
if (write(fd, buf.begin(), cnt) != (ssize_t) cnt)
{
reportError("write", statusVector);
return false;
}
length -= cnt;
}

if (fsync(fd))
{
reportError("fsync", statusVector);
return false;
}

return true;
}


void SharedMemoryBase::internalUnmap()
{
if (sh_mem_header)
Expand Down Expand Up @@ -1303,7 +1353,10 @@ SharedMemoryBase::SharedMemoryBase(const TEXT* filename, ULONG length, IpcObject
if (mainLock->setlock(&statusVector, FileLock::FLM_TRY_EXCLUSIVE))
{
if (trunc_flag)
{
FB_UNUSED(os_utils::ftruncate(mainLock->getFd(), length));
allocFileSpace(mainLock->getFd(), 0, length, NULL);
}

if (callback->initialize(this, true))
{
Expand Down Expand Up @@ -2436,8 +2489,19 @@ bool SharedMemoryBase::remapFile(CheckStatusWrapper* statusVector, ULONG new_len
}

if (flag)
{
FB_UNUSED(os_utils::ftruncate(mainLock->getFd(), new_length));

if (new_length > sh_mem_length_mapped)
{
if (!allocFileSpace(mainLock->getFd(), sh_mem_length_mapped,
new_length - sh_mem_length_mapped, statusVector))
{
return false;
}
}
}

MemoryHeader* const address = (MemoryHeader*) os_utils::mmap(0, new_length,
PROT_READ | PROT_WRITE, MAP_SHARED, mainLock->getFd(), 0);

Expand Down