Bug 1558738. Stop using [array] in nsIUDPSocket. r=valentin
authorBoris Zbarsky <bzbarsky@mit.edu>
Wed, 12 Jun 2019 07:00:01 +0000
changeset 478417 fc1d9d669c4464fad834b5e751343184938602ef
parent 478416 c804bd8858942b814d821993cf4ce82b9089cf1a
child 478422 b30ec4c927b8eda32442c4eaf1f0c7ca5d28e48e
push id87703
push userbzbarsky@mozilla.com
push dateWed, 12 Jun 2019 12:10:25 +0000
treeherderautoland@fc1d9d669c44 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1558738
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1558738. Stop using [array] in nsIUDPSocket. r=valentin Differential Revision: https://phabricator.services.mozilla.com/D34672
dom/network/UDPSocketParent.cpp
netwerk/base/nsIUDPSocket.idl
netwerk/base/nsUDPSocket.cpp
netwerk/dns/mdns/libmdns/fallback/MulticastDNS.jsm
netwerk/test/TestUDPSocket.cpp
netwerk/test/unit/test_udpsocket.js
toolkit/modules/secondscreen/SimpleServiceDiscovery.jsm
--- a/dom/network/UDPSocketParent.cpp
+++ b/dom/network/UDPSocketParent.cpp
@@ -368,24 +368,22 @@ mozilla::ipc::IPCResult UDPSocketParent:
 
 void UDPSocketParent::Send(const InfallibleTArray<uint8_t>& aData,
                            const UDPSocketAddr& aAddr) {
   nsresult rv;
   uint32_t count;
   switch (aAddr.type()) {
     case UDPSocketAddr::TUDPAddressInfo: {
       const UDPAddressInfo& addrInfo(aAddr.get_UDPAddressInfo());
-      rv = mSocket->Send(addrInfo.addr(), addrInfo.port(), aData.Elements(),
-                         aData.Length(), &count);
+      rv = mSocket->Send(addrInfo.addr(), addrInfo.port(), aData, &count);
       break;
     }
     case UDPSocketAddr::TNetAddr: {
       const NetAddr& addr(aAddr.get_NetAddr());
-      rv = mSocket->SendWithAddress(&addr, aData.Elements(), aData.Length(),
-                                    &count);
+      rv = mSocket->SendWithAddress(&addr, aData, &count);
       break;
     }
     default:
       MOZ_ASSERT(false, "Invalid address type!");
       return;
   }
 
   if (NS_WARN_IF(NS_FAILED(rv)) || count == 0) {
--- a/netwerk/base/nsIUDPSocket.idl
+++ b/netwerk/base/nsIUDPSocket.idl
@@ -142,50 +142,44 @@ interface nsIUDPSocket : nsISupports
      * send
      *
      * Send out the datagram to specified remote host and port.
      * DNS lookup will be triggered.
      *
      * @param host The remote host name.
      * @param port The remote port.
      * @param data The buffer containing the data to be written.
-     * @param dataLength The maximum number of bytes to be written.
-     * @return number of bytes written. (0 or dataLength)
+     * @return number of bytes written. (0 or length of data)
      */
     unsigned long send(in AUTF8String host, in unsigned short port,
-                       [const, array, size_is(dataLength)]in uint8_t data,
-                       in unsigned long dataLength);
+                       in Array<uint8_t> data);
 
     /**
      * sendWithAddr
      *
      * Send out the datagram to specified remote host and port.
      *
      * @param addr The remote host address.
      * @param data The buffer containing the data to be written.
-     * @param dataLength The maximum number of bytes to be written.
-     * @return number of bytes written. (0 or dataLength)
+     * @return number of bytes written. (0 or length of data)
      */
     unsigned long sendWithAddr(in nsINetAddr addr,
-                               [const, array, size_is(dataLength)]in uint8_t data,
-                               in unsigned long dataLength);
+                               in Array<uint8_t> data);
 
     /**
      * sendWithAddress
      *
      * Send out the datagram to specified remote address and port.
      *
      * @param addr The remote host address.
      * @param data The buffer containing the data to be written.
-     * @param dataLength The maximum number of bytes to be written.
-     * @return number of bytes written. (0 or dataLength)
+     * @return number of bytes written. (0 or length of data)
      */
     [noscript] unsigned long sendWithAddress([const] in NetAddrPtr addr,
-                                             [const, array, size_is(dataLength)]in uint8_t data,
-                                             in unsigned long dataLength);
+                                             in Array<uint8_t> data);
 
     /**
      * sendBinaryStream
      *
      * Send out the datagram to specified remote address and port.
      *
      * @param host The remote host name.
      * @param port The remote port.
--- a/netwerk/base/nsUDPSocket.cpp
+++ b/netwerk/base/nsUDPSocket.cpp
@@ -977,18 +977,17 @@ PendingSend::OnLookupComplete(nsICancela
   if (NS_FAILED(status)) {
     NS_WARNING("Failed to send UDP packet due to DNS lookup failure");
     return NS_OK;
   }
 
   NetAddr addr;
   if (NS_SUCCEEDED(rec->GetNextAddr(mPort, &addr))) {
     uint32_t count;
-    nsresult rv = mSocket->SendWithAddress(&addr, mData.Elements(),
-                                           mData.Length(), &count);
+    nsresult rv = mSocket->SendWithAddress(&addr, mData, &count);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PendingSend::OnLookupByTypeComplete(nsICancelable* aRequest,
@@ -1055,17 +1054,17 @@ class SendRequestRunnable : public Runna
   RefPtr<nsUDPSocket> mSocket;
   const NetAddr mAddr;
   FallibleTArray<uint8_t> mData;
 };
 
 NS_IMETHODIMP
 SendRequestRunnable::Run() {
   uint32_t count;
-  mSocket->SendWithAddress(&mAddr, mData.Elements(), mData.Length(), &count);
+  mSocket->SendWithAddress(&mAddr, mData, &count);
   return NS_OK;
 }
 
 }  // namespace
 
 NS_IMETHODIMP
 nsUDPSocket::AsyncListen(nsIUDPSocketListener* aListener) {
   // ensuring mFD implies ensuring mLock
@@ -1081,92 +1080,89 @@ nsUDPSocket::AsyncListen(nsIUDPSocketLis
       // PBackground usage from media/mtransport
       mListener = new SocketListenerProxyBackground(aListener);
     }
   }
   return PostEvent(this, &nsUDPSocket::OnMsgAttach);
 }
 
 NS_IMETHODIMP
-nsUDPSocket::Send(const nsACString& aHost, uint16_t aPort, const uint8_t* aData,
-                  uint32_t aDataLength, uint32_t* _retval) {
+nsUDPSocket::Send(const nsACString& aHost, uint16_t aPort,
+                  const nsTArray<uint8_t>& aData, uint32_t* _retval) {
   NS_ENSURE_ARG_POINTER(_retval);
-  if (!((aData && aDataLength > 0) || (!aData && !aDataLength))) {
-    return NS_ERROR_INVALID_ARG;
-  }
 
   *_retval = 0;
 
   FallibleTArray<uint8_t> fallibleArray;
-  if (!fallibleArray.InsertElementsAt(0, aData, aDataLength, fallible)) {
+  if (!fallibleArray.InsertElementsAt(0, aData, fallible)) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   nsCOMPtr<nsIDNSListener> listener =
       new PendingSend(this, aPort, fallibleArray);
 
   nsresult rv = ResolveHost(aHost, mOriginAttributes, listener);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  *_retval = aDataLength;
+  *_retval = aData.Length();
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsUDPSocket::SendWithAddr(nsINetAddr* aAddr, const uint8_t* aData,
-                          uint32_t aDataLength, uint32_t* _retval) {
+nsUDPSocket::SendWithAddr(nsINetAddr* aAddr, const nsTArray<uint8_t>& aData,
+                          uint32_t* _retval) {
   NS_ENSURE_ARG(aAddr);
-  NS_ENSURE_ARG(aData);
   NS_ENSURE_ARG_POINTER(_retval);
 
   NetAddr netAddr;
   aAddr->GetNetAddr(&netAddr);
-  return SendWithAddress(&netAddr, aData, aDataLength, _retval);
+  return SendWithAddress(&netAddr, aData, _retval);
 }
 
 NS_IMETHODIMP
-nsUDPSocket::SendWithAddress(const NetAddr* aAddr, const uint8_t* aData,
-                             uint32_t aDataLength, uint32_t* _retval) {
+nsUDPSocket::SendWithAddress(const NetAddr* aAddr,
+                             const nsTArray<uint8_t>& aData,
+                             uint32_t* _retval) {
   NS_ENSURE_ARG(aAddr);
-  NS_ENSURE_ARG(aData);
   NS_ENSURE_ARG_POINTER(_retval);
 
   *_retval = 0;
 
   PRNetAddr prAddr;
   NetAddrToPRNetAddr(aAddr, &prAddr);
 
   bool onSTSThread = false;
   mSts->IsOnCurrentThread(&onSTSThread);
 
   if (onSTSThread) {
     MutexAutoLock lock(mLock);
     if (!mFD) {
       // socket is not initialized or has been closed
       return NS_ERROR_FAILURE;
     }
-    int32_t count = PR_SendTo(mFD, aData, sizeof(uint8_t) * aDataLength, 0,
-                              &prAddr, PR_INTERVAL_NO_WAIT);
+    int32_t count =
+        PR_SendTo(mFD, aData.Elements(), sizeof(uint8_t) * aData.Length(), 0,
+                  &prAddr, PR_INTERVAL_NO_WAIT);
     if (count < 0) {
       PRErrorCode code = PR_GetError();
       return ErrorAccordingToNSPR(code);
     }
     this->AddOutputBytes(count);
     *_retval = count;
   } else {
     FallibleTArray<uint8_t> fallibleArray;
-    if (!fallibleArray.InsertElementsAt(0, aData, aDataLength, fallible)) {
+    if (!fallibleArray.InsertElementsAt(0, aData, fallible)) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
 
     nsresult rv = mSts->Dispatch(
         new SendRequestRunnable(this, *aAddr, std::move(fallibleArray)),
         NS_DISPATCH_NORMAL);
     NS_ENSURE_SUCCESS(rv, rv);
-    *_retval = aDataLength;
+    *_retval = aData.Length();
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsUDPSocket::SendBinaryStream(const nsACString& aHost, uint16_t aPort,
                               nsIInputStream* aStream) {
   NS_ENSURE_ARG(aStream);
--- a/netwerk/dns/mdns/libmdns/fallback/MulticastDNS.jsm
+++ b/netwerk/dns/mdns/libmdns/fallback/MulticastDNS.jsm
@@ -311,17 +311,17 @@ class MulticastDNS {
     }
 
     return this._getSockets().then((sockets) => {
       sockets.forEach((socket, address) => {
         if (svc.address == "0.0.0.0" || address == svc.address) {
           let packet = this._makeServicePacket(svc, [address]);
           let data = packet.serialize();
           try {
-            socket.send(target.address, target.port, data, data.length);
+            socket.send(target.address, target.port, data);
           } catch (err) {
             DEBUG && debug("Failed to send packet to "
                             + target.address + ":" + target.port);
           }
         }
       });
     });
   }
@@ -397,17 +397,17 @@ class MulticastDNS {
 
     // Initialize the broadcast receiver socket in case it
     // hasn't already been started so we can listen for
     // multicast queries/announcements on all interfaces.
     this._getBroadcastReceiverSocket();
 
     this._getQuerySocket().then((querySocket) => {
       DEBUG && debug('sending query on query socket ("' + name + '")');
-      querySocket.send(MDNS_MULTICAST_GROUP, MDNS_PORT, data, data.length);
+      querySocket.send(MDNS_MULTICAST_GROUP, MDNS_PORT, data);
     });
 
     // Automatically announce previously-discovered
     // services that match and haven't expired yet.
     setTimeout(() => {
       DEBUG && debug('announcing previously discovered services ("' + name + '")');
       let { serviceType } = _parseServiceDomainName(name);
 
--- a/netwerk/test/TestUDPSocket.cpp
+++ b/netwerk/test/TestUDPSocket.cpp
@@ -96,19 +96,20 @@ UDPClientListener::OnPacketReceived(nsIU
   nsCString ip;
   nsCOMPtr<nsINetAddr> fromAddr;
   message->GetFromAddr(getter_AddRefs(fromAddr));
   fromAddr->GetPort(&port);
   fromAddr->GetAddress(ip);
 
   if (TEST_SEND_API == phase && CheckMessageContent(message, REQUEST)) {
     uint32_t count;
-    const uint32_t data = RESPONSE;
-    mResult = socket->SendWithAddr(fromAddr, (const uint8_t*)&data,
-                                   sizeof(uint32_t), &count);
+    nsTArray<uint8_t> data;
+    const uint32_t dataBuffer = RESPONSE;
+    data.AppendElements((const uint8_t*)&dataBuffer, sizeof(uint32_t));
+    mResult = socket->SendWithAddr(fromAddr, data, &count);
     if (mResult == NS_OK && count == sizeof(uint32_t)) {
       SUCCEED();
     } else {
       ADD_FAILURE();
     }
     return NS_OK;
   } else if (TEST_OUTPUT_STREAM != phase ||
              !CheckMessageContent(message, RESPONSE)) {
@@ -256,21 +257,22 @@ TEST(TestUDPSocket, TestUDPSocketMain)
 
   // Bind clinet on arbitrary port
   RefPtr<UDPClientListener> clientListener = new UDPClientListener(waiter);
   client->Init(0, false, systemPrincipal, true, 0);
   client->AsyncListen(clientListener);
 
   // Write data to server
   uint32_t count;
-  const uint32_t data = REQUEST;
+  nsTArray<uint8_t> data;
+  const uint32_t dataBuffer = REQUEST;
+  data.AppendElements((const uint8_t*)&dataBuffer, sizeof(uint32_t));
 
   phase = TEST_OUTPUT_STREAM;
-  rv = client->Send(NS_LITERAL_CSTRING("127.0.0.1"), serverPort,
-                    (uint8_t*)&data, sizeof(uint32_t), &count);
+  rv = client->Send(NS_LITERAL_CSTRING("127.0.0.1"), serverPort, data, &count);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   EXPECT_EQ(count, sizeof(uint32_t));
 
   // Wait for server
   waiter->Wait(1);
   ASSERT_TRUE(NS_SUCCEEDED(serverListener->mResult));
 
   // Read response from server
@@ -279,18 +281,17 @@ TEST(TestUDPSocket, TestUDPSocketMain)
   mozilla::net::NetAddr clientAddr;
   rv = client->GetAddress(&clientAddr);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   // The client address is 0.0.0.0, but Windows won't receive packets there, so
   // use 127.0.0.1 explicitly
   clientAddr.inet.ip = PR_htonl(127 << 24 | 1);
 
   phase = TEST_SEND_API;
-  rv = server->SendWithAddress(&clientAddr, (uint8_t*)&data, sizeof(uint32_t),
-                               &count);
+  rv = server->SendWithAddress(&clientAddr, data, &count);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   EXPECT_EQ(count, sizeof(uint32_t));
 
   // Wait for server
   waiter->Wait(1);
   ASSERT_TRUE(NS_SUCCEEDED(serverListener->mResult));
 
   // Read response from server
@@ -309,18 +310,17 @@ TEST(TestUDPSocket, TestUDPSocketMain)
   multicastAddr.inet.ip = PR_htonl(224 << 24 | 255);
   multicastAddr.inet.port = PR_htons(serverPort);
   rv = server->JoinMulticastAddr(multicastAddr, nullptr);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   // Send multicast ping
   timerCb->mResult = NS_OK;
   timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
-  rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
-                               sizeof(uint32_t), &count);
+  rv = client->SendWithAddress(&multicastAddr, data, &count);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   EXPECT_EQ(count, sizeof(uint32_t));
 
   // Wait for server to receive successfully
   waiter->Wait(1);
   ASSERT_TRUE(NS_SUCCEEDED(serverListener->mResult));
   ASSERT_TRUE(NS_SUCCEEDED(timerCb->mResult));
   timer->Cancel();
@@ -328,18 +328,17 @@ TEST(TestUDPSocket, TestUDPSocketMain)
   // Disable multicast loopback
   printf("Disable multicast loopback\n");
   client->SetMulticastLoopback(false);
   server->SetMulticastLoopback(false);
 
   // Send multicast ping
   timerCb->mResult = NS_OK;
   timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
-  rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
-                               sizeof(uint32_t), &count);
+  rv = client->SendWithAddress(&multicastAddr, data, &count);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   EXPECT_EQ(count, sizeof(uint32_t));
 
   // Wait for server to fail to receive
   waiter->Wait(1);
   ASSERT_FALSE(NS_SUCCEEDED(timerCb->mResult));
   timer->Cancel();
 
@@ -351,18 +350,17 @@ TEST(TestUDPSocket, TestUDPSocketMain)
   mozilla::net::NetAddr loopbackAddr;
   loopbackAddr.inet.family = AF_INET;
   loopbackAddr.inet.ip = PR_htonl(INADDR_LOOPBACK);
   client->SetMulticastInterfaceAddr(loopbackAddr);
 
   // Send multicast ping
   timerCb->mResult = NS_OK;
   timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
-  rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
-                               sizeof(uint32_t), &count);
+  rv = client->SendWithAddress(&multicastAddr, data, &count);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   EXPECT_EQ(count, sizeof(uint32_t));
 
   // Wait for server to fail to receive
   waiter->Wait(1);
   ASSERT_FALSE(NS_SUCCEEDED(timerCb->mResult));
   timer->Cancel();
 
@@ -374,18 +372,17 @@ TEST(TestUDPSocket, TestUDPSocketMain)
 
   // Leave multicast group
   rv = server->LeaveMulticastAddr(multicastAddr, nullptr);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   // Send multicast ping
   timerCb->mResult = NS_OK;
   timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
-  rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
-                               sizeof(uint32_t), &count);
+  rv = client->SendWithAddress(&multicastAddr, data, &count);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
   EXPECT_EQ(count, sizeof(uint32_t));
 
   // Wait for server to fail to receive
   waiter->Wait(1);
   ASSERT_FALSE(NS_SUCCEEDED(timerCb->mResult));
   timer->Cancel();
 
--- a/netwerk/test/unit/test_udpsocket.js
+++ b/netwerk/test/unit/test_udpsocket.js
@@ -25,17 +25,17 @@ add_test(function test_udp_message_raw_d
     },
     onStopListening: function(aSocket, aStatus){}
   });
 
   let rawData = new Uint8Array(HELLO_WORLD.length);
   for (let i = 0; i < HELLO_WORLD.length; i++) {
     rawData[i] = HELLO_WORLD.charCodeAt(i);
   }
-  let written = socket.send("127.0.0.1", socket.port, rawData, rawData.length);
+  let written = socket.send("127.0.0.1", socket.port, rawData);
   Assert.equal(written, HELLO_WORLD.length);
 });
 
 add_test(function test_udp_send_stream() {
   info("test for nsIUDPSocket.sendBinaryStream");
 
   let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance(Ci.nsIUDPSocket);
 
@@ -71,16 +71,16 @@ add_test(function test_udp_message_zero_
       Assert.equal(recv_data, aMessage.data);
       socket.close();
       run_next_test();
     },
     onStopListening: function(aSocket, aStatus){}
   });
 
   let rawData = new Uint8Array(EMPTY_MESSAGE.length);
-  let written = socket.send("127.0.0.1", socket.port, rawData, rawData.length);
+  let written = socket.send("127.0.0.1", socket.port, rawData);
   Assert.equal(written, EMPTY_MESSAGE.length);
 });
 
 function run_test(){
   run_next_test();
 }
 
--- a/toolkit/modules/secondscreen/SimpleServiceDiscovery.jsm
+++ b/toolkit/modules/secondscreen/SimpleServiceDiscovery.jsm
@@ -169,17 +169,17 @@ var SimpleServiceDiscovery = {
     let timeout = SSDP_DISCOVER_DELAY;
     for (let attempts = 0; attempts < SSDP_DISCOVER_ATTEMPTS; attempts++) {
       for (let [/* key */, device] of this._devices) {
         let target = device.target;
         setTimeout(function() {
           let msgData = data.replace("%SEARCH_TARGET%", target);
           try {
             let msgRaw = converter.convertToByteArray(msgData);
-            socket.send(SSDP_ADDRESS, SSDP_PORT, msgRaw, msgRaw.length);
+            socket.send(SSDP_ADDRESS, SSDP_PORT, msgRaw);
           } catch (e) {
             log("failed to convert to byte array: " + e);
           }
         }, timeout);
         timeout += SSDP_TRANSMISSION_INTERVAL;
       }
     }
   },