Bug 1542958 - avoid malloc/delete[] mismatches in elfhack; r=glandium
authorNathan Froyd <froydnj@mozilla.com>
Mon, 15 Apr 2019 23:40:04 +0000
changeset 469667 04eccd9f23a1ac4f5805765aa69ed183713157a3
parent 469666 66ca3efa1ac2131e6c8aa3248fbf10e7bc24c2ff
child 469668 00f2ad0a202aad8fdb62c26350bbc24c7893957a
push id35879
push usernerli@mozilla.com
push dateTue, 16 Apr 2019 22:01:48 +0000
treeherdermozilla-central@12a60898fdc1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1542958
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 1542958 - avoid malloc/delete[] mismatches in elfhack; r=glandium We were allocating ElfSection's data with `new[]` and modifying it with `realloc` in some places, which causes allocator mismatches. Consistently manage the data with `malloc`, `realloc`, and `free` instead. Differential Revision: https://phabricator.services.mozilla.com/D27327
build/unix/elfhack/elf.cpp
build/unix/elfhack/elfhack.cpp
build/unix/elfhack/elfxx.h
--- a/build/unix/elfhack/elf.cpp
+++ b/build/unix/elfhack/elf.cpp
@@ -492,17 +492,20 @@ ElfSection::ElfSection(Elf_Shdr &s, std:
                                      : parent->getSection(shdr.sh_link)),
       next(nullptr),
       previous(nullptr),
       index(-1) {
   if ((file == nullptr) || (shdr.sh_type == SHT_NULL) ||
       (shdr.sh_type == SHT_NOBITS))
     data = nullptr;
   else {
-    data = new char[shdr.sh_size];
+    data = static_cast<char *>(malloc(shdr.sh_size));
+    if (!data) {
+      throw std::runtime_error("Could not malloc ElfSection data");
+    }
     int pos = file->tellg();
     file->seekg(shdr.sh_offset);
     file->read(data, shdr.sh_size);
     file->seekg(pos);
   }
   if (shdr.sh_name == 0)
     name = nullptr;
   else {
--- a/build/unix/elfhack/elfhack.cpp
+++ b/build/unix/elfhack/elfhack.cpp
@@ -174,17 +174,20 @@ class ElfRelHackCode_Section : public El
         addr = (addr | ((*c)->getAddrAlign() - 1)) + 1;
       (*c)->getShdr().sh_addr = addr;
       // We need to align this section depending on the greater
       // alignment required by code sections.
       if (shdr.sh_addralign < (*c)->getAddrAlign())
         shdr.sh_addralign = (*c)->getAddrAlign();
     }
     shdr.sh_size = code.back()->getAddr() + code.back()->getSize();
-    data = new char[shdr.sh_size];
+    data = static_cast<char *>(malloc(shdr.sh_size));
+    if (!data) {
+      throw std::runtime_error("Could not malloc ElfSection data");
+    }
     char *buf = data;
     for (c = code.begin(); c != code.end(); ++c) {
       memcpy(buf, (*c)->getData(), (*c)->getSize());
       buf += (*c)->getSize();
     }
     name = elfhack_text;
   }
 
--- a/build/unix/elfhack/elfxx.h
+++ b/build/unix/elfhack/elfxx.h
@@ -320,17 +320,17 @@ class ElfSection {
  public:
   typedef union {
     ElfSection *section;
     int index;
   } SectionInfo;
 
   ElfSection(Elf_Shdr &s, std::ifstream *file, Elf *parent);
 
-  virtual ~ElfSection() { delete[] data; }
+  virtual ~ElfSection() { free(data); }
 
   const char *getName() { return name; }
   unsigned int getType() { return shdr.sh_type; }
   unsigned int getFlags() { return shdr.sh_flags; }
   unsigned int getAddr();
   unsigned int getSize() { return shdr.sh_size; }
   unsigned int getAddrAlign() { return shdr.sh_addralign; }
   unsigned int getEntSize() { return shdr.sh_entsize; }