Skip to content

Commit a4e2c20

Browse files
author
Alex Menkov
committed
8343344: Windows attach logic failed to handle a failed open on a pipe
Reviewed-by: kevinw, cjplummer
1 parent 63eb485 commit a4e2c20

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/hotspot/os/windows/attachListener_windows.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class PipeChannel : public AttachOperation::RequestReader, public AttachOperatio
8484
0, // default attributes
8585
nullptr); // no template file
8686
if (_hPipe == INVALID_HANDLE_VALUE) {
87-
log_error(attach)("could not open (%d) pipe %s", GetLastError(), pipe);
87+
log_error(attach)("could not open %s (%d) pipe %s",
88+
(write_only ? "write-only" : "read-write"), GetLastError(), pipe);
8889
return false;
8990
}
9091
return true;
@@ -106,7 +107,11 @@ class PipeChannel : public AttachOperation::RequestReader, public AttachOperatio
106107
(DWORD)size,
107108
&nread,
108109
nullptr); // not overlapped
109-
return fSuccess ? (int)nread : -1;
110+
if (!fSuccess) {
111+
log_error(attach)("pipe read error (%d)", GetLastError());
112+
return -1;
113+
}
114+
return (int)nread;
110115
}
111116

112117
// ReplyWriter
@@ -118,7 +123,11 @@ class PipeChannel : public AttachOperation::RequestReader, public AttachOperatio
118123
(DWORD)size,
119124
&written,
120125
nullptr); // not overlapped
121-
return fSuccess ? (int)written : -1;
126+
if (!fSuccess) {
127+
log_error(attach)("pipe write error (%d)", GetLastError());
128+
return -1;
129+
}
130+
return (int)written;
122131
}
123132

124133
void flush() override {
@@ -138,12 +147,12 @@ class Win32AttachOperation: public AttachOperation {
138147

139148
public:
140149
// for v1 pipe must be write-only
141-
void open_pipe(const char* pipe_name, bool write_only) {
142-
_pipe.open(pipe_name, write_only);
150+
bool open_pipe(const char* pipe_name, bool write_only) {
151+
return _pipe.open(pipe_name, write_only);
143152
}
144153

145154
bool read_request() {
146-
return AttachOperation::read_request(&_pipe);
155+
return AttachOperation::read_request(&_pipe);
147156
}
148157

149158
public:
@@ -390,13 +399,17 @@ Win32AttachOperation* Win32AttachListener::dequeue() {
390399
for (int i = 0; i < AttachOperation::arg_count_max; i++) {
391400
op->append_arg(request->arg(i));
392401
}
393-
op->open_pipe(request->pipe(), true/*write-only*/);
402+
if (!op->open_pipe(request->pipe(), true/*write-only*/)) {
403+
// error is already logged
404+
delete op;
405+
op = nullptr;
406+
}
394407
break;
395408
case ATTACH_API_V2:
396409
op = new Win32AttachOperation();
397-
op->open_pipe(request->pipe(), false/*write-only*/);
398-
if (!op->read_request()) {
399-
log_error(attach)("AttachListener::dequeue, reading request ERROR");
410+
if (!op->open_pipe(request->pipe(), false/*write-only*/)
411+
|| !op->read_request()) {
412+
// error is already logged
400413
delete op;
401414
op = nullptr;
402415
}

src/hotspot/share/services/attachListener.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,16 @@ bool AttachOperation::read_request(RequestReader* reader) {
628628
buffer_size = (name_length_max + 1) + arg_count_max * (arg_length_max + 1);
629629
min_str_count = 1 /*name*/ + arg_count_max;
630630
break;
631-
case ATTACH_API_V2: // <ver>0<size>0<cmd>0<arg>0<arg>0<arg>0
631+
case ATTACH_API_V2: // <ver>0<size>0<cmd>0(<arg>0)* (any number of arguments)
632632
if (AttachListener::get_supported_version() < 2) {
633-
log_error(attach)("Failed to read request: v2 is unsupported ot disabled");
633+
log_error(attach)("Failed to read request: v2 is unsupported or disabled");
634634
return false;
635635
}
636636

637637
// read size of the data
638638
buffer_size = reader->read_uint();
639639
if (buffer_size < 0) {
640+
log_error(attach)("Failed to read request: negative request size (%d)", buffer_size);
640641
return false;
641642
}
642643
log_debug(attach)("v2 request, data size = %d", buffer_size);
@@ -646,7 +647,7 @@ bool AttachOperation::read_request(RequestReader* reader) {
646647
log_error(attach)("Failed to read request: too big");
647648
return false;
648649
}
649-
// Must contain exact 'buffer_size' bytes.
650+
// Must contain exactly 'buffer_size' bytes.
650651
min_read_size = buffer_size;
651652
break;
652653
default:

0 commit comments

Comments
 (0)