From a4e2c20849008d5b560f94b58fe70ef8e58c8d4c Mon Sep 17 00:00:00 2001 From: Alex Menkov Date: Tue, 12 Nov 2024 20:24:25 +0000 Subject: [PATCH] 8343344: Windows attach logic failed to handle a failed open on a pipe Reviewed-by: kevinw, cjplummer --- .../os/windows/attachListener_windows.cpp | 33 +++++++++++++------ src/hotspot/share/services/attachListener.cpp | 7 ++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/hotspot/os/windows/attachListener_windows.cpp b/src/hotspot/os/windows/attachListener_windows.cpp index bfa377d52cf..5a34639a1cf 100644 --- a/src/hotspot/os/windows/attachListener_windows.cpp +++ b/src/hotspot/os/windows/attachListener_windows.cpp @@ -84,7 +84,8 @@ public: 0, // default attributes nullptr); // no template file if (_hPipe == INVALID_HANDLE_VALUE) { - log_error(attach)("could not open (%d) pipe %s", GetLastError(), pipe); + log_error(attach)("could not open %s (%d) pipe %s", + (write_only ? "write-only" : "read-write"), GetLastError(), pipe); return false; } return true; @@ -106,7 +107,11 @@ public: (DWORD)size, &nread, nullptr); // not overlapped - return fSuccess ? (int)nread : -1; + if (!fSuccess) { + log_error(attach)("pipe read error (%d)", GetLastError()); + return -1; + } + return (int)nread; } // ReplyWriter @@ -118,7 +123,11 @@ public: (DWORD)size, &written, nullptr); // not overlapped - return fSuccess ? (int)written : -1; + if (!fSuccess) { + log_error(attach)("pipe write error (%d)", GetLastError()); + return -1; + } + return (int)written; } void flush() override { @@ -138,12 +147,12 @@ private: public: // for v1 pipe must be write-only - void open_pipe(const char* pipe_name, bool write_only) { - _pipe.open(pipe_name, write_only); + bool open_pipe(const char* pipe_name, bool write_only) { + return _pipe.open(pipe_name, write_only); } bool read_request() { - return AttachOperation::read_request(&_pipe); + return AttachOperation::read_request(&_pipe); } public: @@ -390,13 +399,17 @@ Win32AttachOperation* Win32AttachListener::dequeue() { for (int i = 0; i < AttachOperation::arg_count_max; i++) { op->append_arg(request->arg(i)); } - op->open_pipe(request->pipe(), true/*write-only*/); + if (!op->open_pipe(request->pipe(), true/*write-only*/)) { + // error is already logged + delete op; + op = nullptr; + } break; case ATTACH_API_V2: op = new Win32AttachOperation(); - op->open_pipe(request->pipe(), false/*write-only*/); - if (!op->read_request()) { - log_error(attach)("AttachListener::dequeue, reading request ERROR"); + if (!op->open_pipe(request->pipe(), false/*write-only*/) + || !op->read_request()) { + // error is already logged delete op; op = nullptr; } diff --git a/src/hotspot/share/services/attachListener.cpp b/src/hotspot/share/services/attachListener.cpp index 90f5930cce0..8cf5c31b0b3 100644 --- a/src/hotspot/share/services/attachListener.cpp +++ b/src/hotspot/share/services/attachListener.cpp @@ -628,15 +628,16 @@ bool AttachOperation::read_request(RequestReader* reader) { buffer_size = (name_length_max + 1) + arg_count_max * (arg_length_max + 1); min_str_count = 1 /*name*/ + arg_count_max; break; - case ATTACH_API_V2: // 000000 + case ATTACH_API_V2: // 000(0)* (any number of arguments) if (AttachListener::get_supported_version() < 2) { - log_error(attach)("Failed to read request: v2 is unsupported ot disabled"); + log_error(attach)("Failed to read request: v2 is unsupported or disabled"); return false; } // read size of the data buffer_size = reader->read_uint(); if (buffer_size < 0) { + log_error(attach)("Failed to read request: negative request size (%d)", buffer_size); return false; } log_debug(attach)("v2 request, data size = %d", buffer_size); @@ -646,7 +647,7 @@ bool AttachOperation::read_request(RequestReader* reader) { log_error(attach)("Failed to read request: too big"); return false; } - // Must contain exact 'buffer_size' bytes. + // Must contain exactly 'buffer_size' bytes. min_read_size = buffer_size; break; default: