Skip to content

Commit

Permalink
8268165: AsyncLogging will crash if rotate() fails
Browse files Browse the repository at this point in the history
LogFileOutput::rotate() may leave _stream NULL because os::fopen() may return NULL due to space or inode limitations.  AsyncLogWriter::write() calls LogFileOutput::write_blocking() without null check.  Null check is added to prevent from crashing.

Reviewed-by: ysuenaga
  • Loading branch information
Xin Liu authored and YaSuenag committed Jun 8, 2021
1 parent fd91b2a commit 8105478
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/hotspot/share/logging/logFileOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,26 @@ bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
return true;
}

class RotationLocker : public StackObj {
Semaphore& _sem;

public:
RotationLocker(Semaphore& sem) : _sem(sem) {
sem.wait();
}

~RotationLocker() {
_sem.signal();
}
};

int LogFileOutput::write_blocking(const LogDecorations& decorations, const char* msg) {
_rotation_semaphore.wait();
RotationLocker lock(_rotation_semaphore);
if (_stream == NULL) {
// An error has occurred with this output, avoid writing to it.
return 0;
}

int written = LogFileStreamOutput::write(decorations, msg);
if (written > 0) {
_current_size += written;
Expand All @@ -295,7 +313,6 @@ int LogFileOutput::write_blocking(const LogDecorations& decorations, const char*
rotate();
}
}
_rotation_semaphore.signal();

return written;
}
Expand Down Expand Up @@ -327,7 +344,7 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
return 0;
}

_rotation_semaphore.wait();
RotationLocker lock(_rotation_semaphore);
int written = LogFileStreamOutput::write(msg_iterator);
if (written > 0) {
_current_size += written;
Expand All @@ -336,7 +353,6 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
rotate();
}
}
_rotation_semaphore.signal();

return written;
}
Expand All @@ -363,13 +379,12 @@ void LogFileOutput::force_rotate() {
// Rotation not possible
return;
}
_rotation_semaphore.wait();

RotationLocker lock(_rotation_semaphore);
rotate();
_rotation_semaphore.signal();
}

void LogFileOutput::rotate() {

if (fclose(_stream)) {
jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
_file_name, os::strerror(errno));
Expand Down

1 comment on commit 8105478

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.