summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2023-08-30 16:38:42 +0200
committerMoonchild <moonchild@palemoon.org>2023-08-30 16:38:42 +0200
commit226f97e2f44b1bb2a9ad602fd7f5ef2c5a0dd5ac (patch)
tree5cde655a1d9ecbaec1c2da9e2ffae94ea6fe1705
parent7fdc866b5a259b209337b9550c902b1bb1e7aa65 (diff)
downloaduxp-226f97e2f44b1bb2a9ad602fd7f5ef2c5a0dd5ac.tar.gz
[network] Add locking around access to WebSocketChannel::mPMCECompressor
-rw-r--r--netwerk/protocol/websocket/WebSocketChannel.cpp80
-rw-r--r--netwerk/protocol/websocket/WebSocketChannel.h2
2 files changed, 49 insertions, 33 deletions
diff --git a/netwerk/protocol/websocket/WebSocketChannel.cpp b/netwerk/protocol/websocket/WebSocketChannel.cpp
index 058dc46417..4b087a8bf2 100644
--- a/netwerk/protocol/websocket/WebSocketChannel.cpp
+++ b/netwerk/protocol/websocket/WebSocketChannel.cpp
@@ -1191,6 +1191,7 @@ WebSocketChannel::WebSocketChannel() :
mBufferSize(kIncomingBufferInitialSize),
mCurrentOut(nullptr),
mCurrentOutSent(0),
+ mCompressorMutex("WebSocketChannel::mCompressorMutex"),
mDynamicOutputSize(0),
mDynamicOutput(nullptr),
mPrivateBrowsing(false),
@@ -1627,6 +1628,7 @@ WebSocketChannel::ProcessInput(uint8_t *buffer, uint32_t count)
if (rsvBits) {
// PMCE sets RSV1 bit in the first fragment when the non-control frame
// is deflated
+ MutexAutoLock lock(mCompressorMutex);
if (mPMCECompressor && rsvBits == kRsv1Bit && mFragmentAccumulator == 0 &&
!(opcode & kControlFrameMask)) {
mPMCECompressor->SetMessageDeflated();
@@ -1700,25 +1702,28 @@ WebSocketChannel::ProcessInput(uint8_t *buffer, uint32_t count)
LOG(("WebSocketChannel:: ignoring read frame code %d after completion\n",
opcode));
} else if (opcode == nsIWebSocketFrame::OPCODE_TEXT) {
- bool isDeflated = mPMCECompressor && mPMCECompressor->IsMessageDeflated();
- LOG(("WebSocketChannel:: %stext frame received\n",
- isDeflated ? "deflated " : ""));
-
if (mListenerMT) {
nsCString utf8Data;
- if (isDeflated) {
- rv = mPMCECompressor->Inflate(payload, payloadLength, utf8Data);
- if (NS_FAILED(rv)) {
- return rv;
- }
- LOG(("WebSocketChannel:: message successfully inflated "
- "[origLength=%d, newLength=%d]\n", payloadLength,
- utf8Data.Length()));
- } else {
- if (!utf8Data.Assign((const char *)payload, payloadLength,
- mozilla::fallible)) {
- return NS_ERROR_OUT_OF_MEMORY;
+ { // lockscope
+ MutexAutoLock lock(mCompressorMutex);
+ bool isDeflated = mPMCECompressor && mPMCECompressor->IsMessageDeflated();
+ LOG(("WebSocketChannel:: %stext frame received\n",
+ isDeflated ? "deflated " : ""));
+
+ if (isDeflated) {
+ rv = mPMCECompressor->Inflate(payload, payloadLength, utf8Data);
+ if (NS_FAILED(rv)) {
+ return rv;
+ }
+ LOG(("WebSocketChannel:: message successfully inflated "
+ "[origLength=%d, newLength=%d]\n", payloadLength,
+ utf8Data.Length()));
+ } else {
+ if (!utf8Data.Assign((const char *)payload, payloadLength,
+ mozilla::fallible)) {
+ return NS_ERROR_OUT_OF_MEMORY;
+ }
}
}
@@ -1835,25 +1840,28 @@ WebSocketChannel::ProcessInput(uint8_t *buffer, uint32_t count)
mService->FrameReceived(mSerial, mInnerWindowID, frame.forget());
}
} else if (opcode == nsIWebSocketFrame::OPCODE_BINARY) {
- bool isDeflated = mPMCECompressor && mPMCECompressor->IsMessageDeflated();
- LOG(("WebSocketChannel:: %sbinary frame received\n",
- isDeflated ? "deflated " : ""));
-
if (mListenerMT) {
nsCString binaryData;
- if (isDeflated) {
- rv = mPMCECompressor->Inflate(payload, payloadLength, binaryData);
- if (NS_FAILED(rv)) {
- return rv;
- }
- LOG(("WebSocketChannel:: message successfully inflated "
- "[origLength=%d, newLength=%d]\n", payloadLength,
- binaryData.Length()));
- } else {
- if (!binaryData.Assign((const char *)payload, payloadLength,
- mozilla::fallible)) {
- return NS_ERROR_OUT_OF_MEMORY;
+ { //lockscope
+ MutexAutoLock lock(mCompressorMutex);
+ bool isDeflated = mPMCECompressor && mPMCECompressor->IsMessageDeflated();
+ LOG(("WebSocketChannel:: %sbinary frame received\n",
+ isDeflated ? "deflated " : ""));
+
+ if (isDeflated) {
+ rv = mPMCECompressor->Inflate(payload, payloadLength, binaryData);
+ if (NS_FAILED(rv)) {
+ return rv;
+ }
+ LOG(("WebSocketChannel:: message successfully inflated "
+ "[origLength=%d, newLength=%d]\n", payloadLength,
+ binaryData.Length()));
+ } else {
+ if (!binaryData.Assign((const char *)payload, payloadLength,
+ mozilla::fallible)) {
+ return NS_ERROR_OUT_OF_MEMORY;
+ }
}
}
@@ -2156,6 +2164,7 @@ WebSocketChannel::PrimeNewOutgoingMessage()
}
// deflate the payload if PMCE is negotiated
+ MutexAutoLock lock(mCompressorMutex);
if (mPMCECompressor &&
(msgType == kMsgTypeString || msgType == kMsgTypeBinaryString)) {
if (mCurrentOut->DeflatePayload(mPMCECompressor)) {
@@ -2447,7 +2456,10 @@ WebSocketChannel::StopSession(nsresult reason)
mCancelable = nullptr;
}
- mPMCECompressor = nullptr;
+ {
+ MutexAutoLock lock(mCompressorMutex);
+ mPMCECompressor = nullptr;
+ }
if (!mCalledOnStop) {
mCalledOnStop = 1;
@@ -2702,6 +2714,7 @@ WebSocketChannel::HandleExtensions()
serverMaxWindowBits = 15;
}
+ MutexAutoLock lock(mCompressorMutex);
mPMCECompressor = new PMCECompression(clientNoContextTakeover,
clientMaxWindowBits,
serverMaxWindowBits);
@@ -3678,6 +3691,7 @@ WebSocketChannel::OnTransportAvailable(nsISocketTransport *aTransport,
serverMaxWindowBits = 15;
}
+ MutexAutoLock lock(mCompressorMutex);
mPMCECompressor = new PMCECompression(serverNoContextTakeover,
serverMaxWindowBits,
clientMaxWindowBits);
diff --git a/netwerk/protocol/websocket/WebSocketChannel.h b/netwerk/protocol/websocket/WebSocketChannel.h
index 879027b2ad..691de4102c 100644
--- a/netwerk/protocol/websocket/WebSocketChannel.h
+++ b/netwerk/protocol/websocket/WebSocketChannel.h
@@ -18,6 +18,7 @@
#include "nsIChannelEventSink.h"
#include "nsIHttpChannelInternal.h"
#include "nsIStringStream.h"
+#include "mozilla/Mutex.h"
#include "BaseWebSocketChannel.h"
#include "nsCOMPtr.h"
@@ -288,6 +289,7 @@ private:
uint32_t mHdrOutToSend;
uint8_t *mHdrOut;
uint8_t mOutHeader[kCopyBreak + 16];
+ Mutex mCompressorMutex;
nsAutoPtr<PMCECompression> mPMCECompressor;
uint32_t mDynamicOutputSize;
uint8_t *mDynamicOutput;