Path: blob/master/runtime/compiler/net/CommunicationStream.cpp
6000 views
/*******************************************************************************1* Copyright (c) 2018, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122#include "control/CompilationRuntime.hpp"23#include "control/Options.hpp" // TR::Options::useCompressedPointers()24#include "env/CompilerEnv.hpp" // for TR::Compiler->target.is64Bit()25#include "net/CommunicationStream.hpp"262728namespace JITServer29{3031uint32_t CommunicationStream::CONFIGURATION_FLAGS = 0;3233#if defined(MESSAGE_SIZE_STATS)34TR_Stats JITServer::CommunicationStream::msgSizeStats[];35#endif /* defined(MESSAGE_SIZE_STATS) */3637void38CommunicationStream::initConfigurationFlags()39{40if (TR::Compiler->target.is64Bit() && TR::Options::useCompressedPointers())41{42CONFIGURATION_FLAGS |= JITServerCompressedRef;43}44CONFIGURATION_FLAGS |= JAVA_SPEC_VERSION & JITServerJavaVersionMask;45}4647bool CommunicationStream::useSSL()48{49TR::CompilationInfo *compInfo = TR::CompilationInfo::get();50return (compInfo->getJITServerSslKeys().size() ||51compInfo->getJITServerSslCerts().size() ||52compInfo->getJITServerSslRootCerts().size());53}5455void CommunicationStream::initSSL()56{57(*OSSL_load_error_strings)();58(*OSSL_library_init)();59// OpenSSL_add_ssl_algorithms() is a synonym for SSL_library_init() and is implemented as a macro60// It's redundant, should be able to remove it later61// OpenSSL_add_ssl_algorithms();62}6364void65CommunicationStream::readMessage2(Message &msg)66{67msg.clearForRead();6869// read message size70uint32_t serializedSize;71readBlocking(serializedSize);7273msg.expandBufferIfNeeded(serializedSize);74msg.setSerializedSize(serializedSize);7576// read the rest of the message77uint32_t messageSize = serializedSize - sizeof(uint32_t);78readBlocking(msg.getBufferStartForRead() + sizeof(uint32_t), messageSize);7980// rebuild the message81msg.deserialize();8283// collect message size84#if defined(MESSAGE_SIZE_STATS)85msgSizeStats[int(msg.type())].update(serializedSize);86#endif /* defined(MESSAGE_SIZE_STATS) */87}8889void90CommunicationStream::readMessage(Message &msg)91{92msg.clearForRead();9394// The message buffer storage and its capacity could be95// changed when the serialized size is set.96char *buffer = msg.getBufferStartForRead();97uint32_t bufferCapacity = msg.getBufferCapacity();9899int32_t bytesRead = readOnceBlocking(buffer, bufferCapacity);100101// bytesRead should be greater than 0 here, readOnceBlocking() throws102// an exception already if (bytesRead <= 0).103if (bytesRead < sizeof(uint32_t))104{105throw JITServer::StreamFailure("JITServer I/O error: fail to read the size of the message");106}107108// bytesRead >= sizeof(uint32_t)109uint32_t serializedSize = ((uint32_t *)buffer)[0];110if (bytesRead > serializedSize)111{112throw JITServer::StreamFailure("JITServer I/O error: read more than the message size");113}114115// serializedSize >= bytesRead116uint32_t bytesLeftToRead = serializedSize - bytesRead;117118if (bytesLeftToRead > 0)119{120if (serializedSize > bufferCapacity)121{122// bytesRead could be less than the buffer capacity.123msg.expandBuffer(serializedSize, bytesRead);124125// The buffer storage will change after the buffer is expanded.126buffer = msg.getBufferStartForRead();127}128129readBlocking(buffer + bytesRead, bytesLeftToRead);130}131132msg.setSerializedSize(serializedSize);133134// rebuild the message135msg.deserialize();136137#if defined(MESSAGE_SIZE_STATS)138msgSizeStats[int(msg.type())].update(serializedSize);139#endif /* defined(MESSAGE_SIZE_STATS) */140}141142void143CommunicationStream::writeMessage(Message &msg)144{145char *serialMsg = msg.serialize();146// write serialized message to the socket147writeBlocking(serialMsg, msg.serializedSize());148msg.clearForWrite();149}150}151152153