Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/compiler/net/CommunicationStream.cpp
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2018, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include "control/CompilationRuntime.hpp"
24
#include "control/Options.hpp" // TR::Options::useCompressedPointers()
25
#include "env/CompilerEnv.hpp" // for TR::Compiler->target.is64Bit()
26
#include "net/CommunicationStream.hpp"
27
28
29
namespace JITServer
30
{
31
32
uint32_t CommunicationStream::CONFIGURATION_FLAGS = 0;
33
34
#if defined(MESSAGE_SIZE_STATS)
35
TR_Stats JITServer::CommunicationStream::msgSizeStats[];
36
#endif /* defined(MESSAGE_SIZE_STATS) */
37
38
void
39
CommunicationStream::initConfigurationFlags()
40
{
41
if (TR::Compiler->target.is64Bit() && TR::Options::useCompressedPointers())
42
{
43
CONFIGURATION_FLAGS |= JITServerCompressedRef;
44
}
45
CONFIGURATION_FLAGS |= JAVA_SPEC_VERSION & JITServerJavaVersionMask;
46
}
47
48
bool CommunicationStream::useSSL()
49
{
50
TR::CompilationInfo *compInfo = TR::CompilationInfo::get();
51
return (compInfo->getJITServerSslKeys().size() ||
52
compInfo->getJITServerSslCerts().size() ||
53
compInfo->getJITServerSslRootCerts().size());
54
}
55
56
void CommunicationStream::initSSL()
57
{
58
(*OSSL_load_error_strings)();
59
(*OSSL_library_init)();
60
// OpenSSL_add_ssl_algorithms() is a synonym for SSL_library_init() and is implemented as a macro
61
// It's redundant, should be able to remove it later
62
// OpenSSL_add_ssl_algorithms();
63
}
64
65
void
66
CommunicationStream::readMessage2(Message &msg)
67
{
68
msg.clearForRead();
69
70
// read message size
71
uint32_t serializedSize;
72
readBlocking(serializedSize);
73
74
msg.expandBufferIfNeeded(serializedSize);
75
msg.setSerializedSize(serializedSize);
76
77
// read the rest of the message
78
uint32_t messageSize = serializedSize - sizeof(uint32_t);
79
readBlocking(msg.getBufferStartForRead() + sizeof(uint32_t), messageSize);
80
81
// rebuild the message
82
msg.deserialize();
83
84
// collect message size
85
#if defined(MESSAGE_SIZE_STATS)
86
msgSizeStats[int(msg.type())].update(serializedSize);
87
#endif /* defined(MESSAGE_SIZE_STATS) */
88
}
89
90
void
91
CommunicationStream::readMessage(Message &msg)
92
{
93
msg.clearForRead();
94
95
// The message buffer storage and its capacity could be
96
// changed when the serialized size is set.
97
char *buffer = msg.getBufferStartForRead();
98
uint32_t bufferCapacity = msg.getBufferCapacity();
99
100
int32_t bytesRead = readOnceBlocking(buffer, bufferCapacity);
101
102
// bytesRead should be greater than 0 here, readOnceBlocking() throws
103
// an exception already if (bytesRead <= 0).
104
if (bytesRead < sizeof(uint32_t))
105
{
106
throw JITServer::StreamFailure("JITServer I/O error: fail to read the size of the message");
107
}
108
109
// bytesRead >= sizeof(uint32_t)
110
uint32_t serializedSize = ((uint32_t *)buffer)[0];
111
if (bytesRead > serializedSize)
112
{
113
throw JITServer::StreamFailure("JITServer I/O error: read more than the message size");
114
}
115
116
// serializedSize >= bytesRead
117
uint32_t bytesLeftToRead = serializedSize - bytesRead;
118
119
if (bytesLeftToRead > 0)
120
{
121
if (serializedSize > bufferCapacity)
122
{
123
// bytesRead could be less than the buffer capacity.
124
msg.expandBuffer(serializedSize, bytesRead);
125
126
// The buffer storage will change after the buffer is expanded.
127
buffer = msg.getBufferStartForRead();
128
}
129
130
readBlocking(buffer + bytesRead, bytesLeftToRead);
131
}
132
133
msg.setSerializedSize(serializedSize);
134
135
// rebuild the message
136
msg.deserialize();
137
138
#if defined(MESSAGE_SIZE_STATS)
139
msgSizeStats[int(msg.type())].update(serializedSize);
140
#endif /* defined(MESSAGE_SIZE_STATS) */
141
}
142
143
void
144
CommunicationStream::writeMessage(Message &msg)
145
{
146
char *serialMsg = msg.serialize();
147
// write serialized message to the socket
148
writeBlocking(serialMsg, msg.serializedSize());
149
msg.clearForWrite();
150
}
151
}
152
153