Bug 1353767 - BufferList methods that can OOM should all be MOZ_MUST_USE. r=billm
authordOn'tReallycAre <vadfgsvrtascas@yahoo.com.tw>
Tue, 26 Mar 2019 13:24:00 +0200
changeset 466270 3ba34ca57b5b5c73a07a026a6103cbc7394c2207
parent 466269 9b8e1b1b70f326cc8170b3a26c4321e360632a2d
child 466271 a36496b378c79fea11d9d27377936ab8ffaefdb0
push id35763
push usercsabou@mozilla.com
push dateWed, 27 Mar 2019 04:45:26 +0000
treeherdermozilla-central@7f816aa10a20 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1353767
milestone68.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 1353767 - BufferList methods that can OOM should all be MOZ_MUST_USE. r=billm The Pickle methods can use MOZ_ALWAYS_TRUE because the BufferList is infallible, so the WriteBytes calls will never fail.
ipc/chromium/src/base/pickle.cc
mfbt/BufferList.h
mfbt/tests/TestBufferList.cpp
--- a/ipc/chromium/src/base/pickle.cc
+++ b/ipc/chromium/src/base/pickle.cc
@@ -486,17 +486,17 @@ void Pickle::BeginWrite(uint32_t length,
 
   if (padding) {
     MOZ_RELEASE_ASSERT(padding <= 8);
     static const char padding_data[8] = {
         kBytePaddingMarker, kBytePaddingMarker, kBytePaddingMarker,
         kBytePaddingMarker, kBytePaddingMarker, kBytePaddingMarker,
         kBytePaddingMarker, kBytePaddingMarker,
     };
-    buffers_.WriteBytes(padding_data, padding);
+    MOZ_ALWAYS_TRUE(buffers_.WriteBytes(padding_data, padding));
   }
 
   DCHECK((header_size_ + header_->payload_size + padding) % alignment == 0);
 
   header_->payload_size = new_size;
 }
 
 void Pickle::EndWrite(uint32_t length) {
@@ -506,17 +506,17 @@ void Pickle::EndWrite(uint32_t length) {
   if (padding) {
     MOZ_RELEASE_ASSERT(padding <= 4);
     static const char padding_data[4] = {
         kBytePaddingMarker,
         kBytePaddingMarker,
         kBytePaddingMarker,
         kBytePaddingMarker,
     };
-    buffers_.WriteBytes(padding_data, padding);
+    MOZ_ALWAYS_TRUE(buffers_.WriteBytes(padding_data, padding));
   }
 }
 
 bool Pickle::WriteBool(bool value) {
 #ifdef FUZZING
   mozilla::ipc::Faulty::instance().FuzzBool(&value);
 #endif
   return WriteInt(value ? 1 : 0);
@@ -622,17 +622,17 @@ bool Pickle::WriteBytesZeroCopy(void* da
 
 bool Pickle::WriteBytes(const void* data, uint32_t data_len,
                         uint32_t alignment) {
   DCHECK(alignment == 4 || alignment == 8);
   DCHECK(intptr_t(header_) % alignment == 0);
 
   BeginWrite(data_len, alignment);
 
-  buffers_.WriteBytes(reinterpret_cast<const char*>(data), data_len);
+  MOZ_ALWAYS_TRUE(buffers_.WriteBytes(reinterpret_cast<const char*>(data), data_len));
 
   EndWrite(data_len);
   return true;
 }
 
 bool Pickle::WriteString(const std::string& value) {
 #ifdef FUZZING
   std::string v(value);
@@ -662,17 +662,17 @@ bool Pickle::WriteWString(const std::wst
 #endif
 }
 
 bool Pickle::WriteData(const char* data, uint32_t length) {
   return WriteInt(length) && WriteBytes(data, length);
 }
 
 void Pickle::InputBytes(const char* data, uint32_t length) {
-  buffers_.WriteBytes(data, length);
+  MOZ_ALWAYS_TRUE(buffers_.WriteBytes(data, length));
 }
 
 int32_t* Pickle::GetInt32PtrForTest(uint32_t offset) {
   size_t pos = buffers_.Size() - offset;
   BufferList::IterImpl iter(buffers_);
   MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(buffers_, pos));
   return reinterpret_cast<int32_t*>(iter.Data());
 }
--- a/mfbt/BufferList.h
+++ b/mfbt/BufferList.h
@@ -302,17 +302,17 @@ class BufferList : private AllocPolicy {
     return mSegments[0].mData;
   }
   const char* Start() const { return mSegments[0].mData; }
 
   IterImpl Iter() const { return IterImpl(*this); }
 
   // Copies aSize bytes from aData into the BufferList. The storage for these
   // bytes may be split across multiple buffers. Size() is increased by aSize.
-  inline bool WriteBytes(const char* aData, size_t aSize);
+  inline MOZ_MUST_USE bool WriteBytes(const char* aData, size_t aSize);
 
   // Allocates a buffer of at most |aMaxBytes| bytes and, if successful, returns
   // that buffer, and places its size in |aSize|. If unsuccessful, returns null
   // and leaves |aSize| undefined.
   inline char* AllocateBytes(size_t aMaxSize, size_t* aSize);
 
   // Copies possibly non-contiguous byte range starting at aIter into
   // aData. aIter is advanced by aSize bytes. Returns false if it runs out of
@@ -394,17 +394,17 @@ class BufferList : private AllocPolicy {
 
   bool mOwning;
   Vector<Segment, 1, AllocPolicy> mSegments;
   size_t mSize;
   size_t mStandardCapacity;
 };
 
 template <typename AllocPolicy>
-bool BufferList<AllocPolicy>::WriteBytes(const char* aData, size_t aSize) {
+MOZ_MUST_USE bool BufferList<AllocPolicy>::WriteBytes(const char* aData, size_t aSize) {
   MOZ_RELEASE_ASSERT(mOwning);
   MOZ_RELEASE_ASSERT(mStandardCapacity);
 
   size_t copied = 0;
   while (copied < aSize) {
     size_t toCopy;
     char* data = AllocateBytes(aSize - copied, &toCopy);
     if (!data) {
--- a/mfbt/tests/TestBufferList.cpp
+++ b/mfbt/tests/TestBufferList.cpp
@@ -75,17 +75,17 @@ int main(void) {
   MOZ_RELEASE_ASSERT(iter.Done());
 
   // Writing to the buffer.
 
   const size_t kSmallWrite = 16;
 
   char toWrite[kSmallWrite];
   memset(toWrite, 0x0a, kSmallWrite);
-  bl.WriteBytes(toWrite, kSmallWrite);
+  MOZ_ALWAYS_TRUE(bl.WriteBytes(toWrite, kSmallWrite));
 
   MOZ_RELEASE_ASSERT(bl.Size() == kInitialSize + kSmallWrite);
 
   iter = bl.Iter();
   iter.Advance(bl, kInitialSize);
   MOZ_RELEASE_ASSERT(!iter.Done());
   MOZ_RELEASE_ASSERT(iter.RemainingInSegment() ==
                      kInitialCapacity - kInitialSize);
@@ -135,17 +135,17 @@ int main(void) {
                      kInitialCapacity - kInitialSize);
 
   const size_t kBigWrite = 1024;
 
   char* toWriteBig = static_cast<char*>(malloc(kBigWrite));
   for (unsigned i = 0; i < kBigWrite; i++) {
     toWriteBig[i] = i % 37;
   }
-  bl.WriteBytes(toWriteBig, kBigWrite);
+  MOZ_ALWAYS_TRUE(bl.WriteBytes(toWriteBig, kBigWrite));
 
   char* toReadBig = static_cast<char*>(malloc(kBigWrite));
   iter = bl.Iter();
   MOZ_RELEASE_ASSERT(
       iter.AdvanceAcrossSegments(bl, kInitialSize + kSmallWrite));
   bl.ReadBytes(iter, toReadBig, kBigWrite);
   MOZ_RELEASE_ASSERT(memcmp(toReadBig, toWriteBig, kBigWrite) == 0);
   MOZ_RELEASE_ASSERT(iter.Done());
@@ -184,19 +184,19 @@ int main(void) {
   MOZ_RELEASE_ASSERT(bl.Size() == 0);
   MOZ_RELEASE_ASSERT(bl.Iter().Done());
 
   // Move assignment.
 
   const size_t kSmallCapacity = 8;
 
   BufferList bl2(0, kSmallCapacity, kSmallCapacity);
-  bl2.WriteBytes(toWrite, kSmallWrite);
-  bl2.WriteBytes(toWrite, kSmallWrite);
-  bl2.WriteBytes(toWrite, kSmallWrite);
+  MOZ_ALWAYS_TRUE(bl2.WriteBytes(toWrite, kSmallWrite));
+  MOZ_ALWAYS_TRUE(bl2.WriteBytes(toWrite, kSmallWrite));
+  MOZ_ALWAYS_TRUE(bl2.WriteBytes(toWrite, kSmallWrite));
 
   bl = std::move(bl2);
   MOZ_RELEASE_ASSERT(bl2.Size() == 0);
   MOZ_RELEASE_ASSERT(bl2.Iter().Done());
 
   iter = bl.Iter();
   MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl, kSmallWrite * 3));
   MOZ_RELEASE_ASSERT(iter.Done());
@@ -257,48 +257,48 @@ int main(void) {
   BufferList bl3 = bl.Extract(iter, kExtractOverSize, &success);
   MOZ_RELEASE_ASSERT(!success);
 
   iter = bl2.Iter();
   MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl2, kExtractSize));
   MOZ_RELEASE_ASSERT(iter.Done());
 
   BufferList bl4(8, 8, 8);
-  bl4.WriteBytes("abcd1234", 8);
+  MOZ_ALWAYS_TRUE(bl4.WriteBytes("abcd1234", 8));
   iter = bl4.Iter();
   iter.Advance(bl4, 8);
 
   BufferList bl5 = bl4.Extract(iter, kExtractSize, &success);
   MOZ_RELEASE_ASSERT(!success);
 
   BufferList bl6(0, 0, 16);
-  bl6.WriteBytes("abcdefgh12345678", 16);
-  bl6.WriteBytes("ijklmnop87654321", 16);
+  MOZ_ALWAYS_TRUE(bl6.WriteBytes("abcdefgh12345678", 16));
+  MOZ_ALWAYS_TRUE(bl6.WriteBytes("ijklmnop87654321", 16));
   iter = bl6.Iter();
   iter.Advance(bl6, 8);
   BufferList bl7 = bl6.Extract(iter, 16, &success);
   MOZ_RELEASE_ASSERT(success);
   char data[16];
   MOZ_RELEASE_ASSERT(bl6.ReadBytes(iter, data, 8));
   MOZ_RELEASE_ASSERT(memcmp(data, "87654321", 8) == 0);
   iter = bl7.Iter();
   MOZ_RELEASE_ASSERT(bl7.ReadBytes(iter, data, 16));
   MOZ_RELEASE_ASSERT(memcmp(data, "12345678ijklmnop", 16) == 0);
 
   BufferList bl8(0, 0, 16);
-  bl8.WriteBytes("abcdefgh12345678", 16);
+  MOZ_ALWAYS_TRUE(bl8.WriteBytes("abcdefgh12345678", 16));
   iter = bl8.Iter();
   BufferList bl9 = bl8.Extract(iter, 8, &success);
   MOZ_RELEASE_ASSERT(success);
   MOZ_RELEASE_ASSERT(bl9.Size() == 8);
   MOZ_RELEASE_ASSERT(!iter.Done());
 
   BufferList bl10(0, 0, 8);
-  bl10.WriteBytes("abcdefgh", 8);
-  bl10.WriteBytes("12345678", 8);
+  MOZ_ALWAYS_TRUE(bl10.WriteBytes("abcdefgh", 8));
+  MOZ_ALWAYS_TRUE(bl10.WriteBytes("12345678", 8));
   iter = bl10.Iter();
   BufferList bl11 = bl10.Extract(iter, 16, &success);
   MOZ_RELEASE_ASSERT(success);
   MOZ_RELEASE_ASSERT(bl11.Size() == 16);
   MOZ_RELEASE_ASSERT(iter.Done());
   iter = bl11.Iter();
   MOZ_RELEASE_ASSERT(bl11.ReadBytes(iter, data, 16));
   MOZ_RELEASE_ASSERT(memcmp(data, "abcdefgh12345678", 16) == 0);