Introduce mem_size() and New() and start using it.

This commit is contained in:
Markus F.X.J. Oberhumer
2016-09-20 15:24:07 +02:00
parent 8d433f2580
commit 44248f19b5
8 changed files with 102 additions and 65 deletions
+1 -2
View File
@@ -297,8 +297,7 @@ inline const T& UPX_MIN(const T& a, const T& b) { if (a < b) return a; return b;
// An Array allocates memory on the heap, but automatically // An Array allocates memory on the heap, but automatically
// gets destructed when leaving scope or on exceptions. // gets destructed when leaving scope or on exceptions.
#define Array(type, var, size) \ #define Array(type, var, size) \
assert((int)(size) > 0); \ MemBuffer var ## _membuf(mem_size(sizeof(type), size)); \
MemBuffer var ## _membuf((size)*(sizeof(type))); \
type * const var = ((type *) var ## _membuf.getVoidPtr()) type * const var = ((type *) var ## _membuf.getVoidPtr())
#define ByteArray(var, size) Array(unsigned char, var, size) #define ByteArray(var, size) Array(unsigned char, var, size)
+45 -14
View File
@@ -30,6 +30,43 @@
#include "mem.h" #include "mem.h"
/*************************************************************************
//
**************************************************************************/
// DO NOT CHANGE
#define MAX_SIZE (768 * 1024 * 1024)
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * MAX_SIZE * 9 / 8 + 16*1024*1024 < INT_MAX)
size_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
{
assert(element_size > 0);
if (element_size > MAX_SIZE) throwCantPack("mem_size 1; take care");
if (n > MAX_SIZE) throwCantPack("mem_size 2; take care");
if (extra > MAX_SIZE) throwCantPack("mem_size 3; take care");
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
if (bytes > MAX_SIZE) throwCantPack("mem_size 4; take care");
return ACC_ICONV(size_t, bytes);
}
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n)
{
(void) mem_size(element_size, n); // check
return ACC_ICONV(size_t, n); // return n
}
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
{
assert(element_size > 0);
if (element_size > MAX_SIZE) return false;
if (n > MAX_SIZE) return false;
if (extra > MAX_SIZE) return false;
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
if (bytes > MAX_SIZE) return false;
return true;
}
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
@@ -103,23 +140,19 @@ void MemBuffer::dealloc()
unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra) unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra)
{ {
assert((int)uncompressed_size > 0); size_t bytes = mem_size(1, uncompressed_size, extra);
assert((int)extra >= 0); bytes += uncompressed_size/8 + 256;
unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra; return ACC_ICONV(unsigned, bytes);
return size;
} }
unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra) unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra)
{ {
assert((int)uncompressed_size > 0); size_t bytes = mem_size(1, uncompressed_size, extra);
assert((int)extra >= 0);
unsigned size = uncompressed_size + extra;
// size += 512; // 512 safety bytes
// INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors // INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors
#if (ACC_ARCH_I386) #if (ACC_ARCH_I386)
size += 3; bytes += 3;
#endif #endif
return size; return ACC_ICONV(unsigned, bytes);
} }
@@ -187,10 +220,8 @@ void MemBuffer::alloc(unsigned size)
assert(b == NULL); assert(b == NULL);
assert(b_size == 0); assert(b_size == 0);
// //
assert((int)size > 0); size_t bytes = mem_size(1, size, use_mcheck ? 32 : 0);
unsigned total = use_mcheck ? size + 32 : size; unsigned char *p = (unsigned char *) malloc(bytes);
assert((int)total > 0);
unsigned char *p = (unsigned char *) malloc(total);
if (!p) if (!p)
throwOutOfMemoryException(); throwOutOfMemoryException();
b_size = size; b_size = size;
+7
View File
@@ -34,6 +34,13 @@
// //
**************************************************************************/ **************************************************************************/
size_t mem_size (upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0);
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n);
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0);
#define New(type,n) new type [ mem_size_get_n(sizeof(type),n) ]
class MemBuffer class MemBuffer
{ {
public: public:
+19 -19
View File
@@ -230,14 +230,14 @@ PackLinuxElf32::PackLinuxElf32help1(InputFile *f)
if (f && Elf32_Ehdr::ET_DYN!=e_type) { if (f && Elf32_Ehdr::ET_DYN!=e_type) {
unsigned const len = sz_phdrs + e_phoff; unsigned const len = sz_phdrs + e_phoff;
file_image = new char[len]; file_image = New(char, len);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, len); f->readx(file_image, len);
phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
} }
if (f && Elf32_Ehdr::ET_DYN==e_type) { if (f && Elf32_Ehdr::ET_DYN==e_type) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, file_size); f->readx(file_image, file_size);
phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
@@ -599,14 +599,14 @@ PackLinuxElf64::PackLinuxElf64help1(InputFile *f)
if (f && Elf64_Ehdr::ET_DYN!=e_type) { if (f && Elf64_Ehdr::ET_DYN!=e_type) {
unsigned const len = sz_phdrs + e_phoff; unsigned const len = sz_phdrs + e_phoff;
file_image = new char[len]; file_image = New(char, len);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, len); f->readx(file_image, len);
phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !!
} }
if (f && Elf64_Ehdr::ET_DYN==e_type) { if (f && Elf64_Ehdr::ET_DYN==e_type) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, file_size); f->readx(file_image, file_size);
phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !!
@@ -909,7 +909,7 @@ PackLinuxElf32::buildLinuxLoader(
unsigned char const *const uncLoader = fold_hdrlen + fold; unsigned char const *const uncLoader = fold_hdrlen + fold;
h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc)); h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc));
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr]; unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr);
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr, int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
@@ -917,7 +917,7 @@ PackLinuxElf32::buildLinuxLoader(
#if 0 //{ debugging only #if 0 //{ debugging only
if (M_IS_LZMA(ph.method)) { if (M_IS_LZMA(ph.method)) {
ucl_uint tmp_len = h.sz_unc; // LZMA uses this as EOF ucl_uint tmp_len = h.sz_unc; // LZMA uses this as EOF
unsigned char *tmp = new unsigned char[tmp_len]; unsigned char *tmp = New(unsigned char, tmp_len);
memset(tmp, 0, tmp_len); memset(tmp, 0, tmp_len);
r = upx_decompress(sizeof(h) + cprLoader, h.sz_cpr, tmp, &tmp_len, h.b_method, NULL); r = upx_decompress(sizeof(h) + cprLoader, h.sz_cpr, tmp, &tmp_len, h.b_method, NULL);
if (r == UPX_E_OUT_OF_MEMORY) if (r == UPX_E_OUT_OF_MEMORY)
@@ -974,7 +974,7 @@ PackLinuxElf64::buildLinuxLoader(
unsigned char const *const uncLoader = fold_hdrlen + fold; unsigned char const *const uncLoader = fold_hdrlen + fold;
h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc)); h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc));
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr]; unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr);
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr, int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
@@ -1660,7 +1660,7 @@ PackLinuxElf64ppcle::canPack()
if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) { if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size); fi->readx(file_image, file_size);
memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr)); memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr));
@@ -1835,7 +1835,7 @@ PackLinuxElf64amd::canPack()
if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) { if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size); fi->readx(file_image, file_size);
memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr)); memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr));
@@ -2300,7 +2300,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
} }
} }
if (note_size) { if (note_size) {
note_body = new unsigned char[note_size]; note_body = New(unsigned char, note_size);
note_size = 0; note_size = 0;
} }
phdr = phdri; phdr = phdri;
@@ -2340,7 +2340,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Shdr const *tmp = shdri; Elf32_Shdr const *tmp = shdri;
if (! shdri) { if (! shdri) {
shdr = new Elf32_Shdr[e_shnum]; shdr = New(Elf32_Shdr, e_shnum);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(ehdri.e_shoff,SEEK_SET); fi->seek(ehdri.e_shoff,SEEK_SET);
@@ -2353,7 +2353,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
//set the shstrtab //set the shstrtab
sec_strndx = &shdr[ehdri.e_shstrndx]; sec_strndx = &shdr[ehdri.e_shstrndx];
char *strtab = new char[(unsigned) sec_strndx->sh_size]; char *strtab = New(char, sec_strndx->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(sec_strndx->sh_offset,SEEK_SET); fi->seek(sec_strndx->sh_offset,SEEK_SET);
fi->readx(strtab,sec_strndx->sh_size); fi->readx(strtab,sec_strndx->sh_size);
@@ -2362,7 +2362,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id"); Elf32_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id");
if (buildid) { if (buildid) {
unsigned char *data = new unsigned char[(unsigned) buildid->sh_size]; unsigned char *data = New(unsigned char, buildid->sh_size);
memset(data,0,buildid->sh_size); memset(data,0,buildid->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(buildid->sh_offset,SEEK_SET); fi->seek(buildid->sh_offset,SEEK_SET);
@@ -2493,7 +2493,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
} }
} }
if (note_size) { if (note_size) {
note_body = new unsigned char[note_size]; note_body = New(unsigned char, note_size);
note_size = 0; note_size = 0;
} }
phdr = phdri; phdr = phdri;
@@ -2538,7 +2538,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Shdr *shdr = NULL; Elf64_Shdr *shdr = NULL;
if (! shdri) { if (! shdri) {
shdr = new Elf64_Shdr[e_shnum]; shdr = New(Elf64_Shdr, e_shnum);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(ehdri.e_shoff,SEEK_SET); fi->seek(ehdri.e_shoff,SEEK_SET);
@@ -2551,7 +2551,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
//set the shstrtab //set the shstrtab
sec_strndx = &shdri[ehdri.e_shstrndx]; sec_strndx = &shdri[ehdri.e_shstrndx];
char *strtab = new char[(unsigned) sec_strndx->sh_size]; char *strtab = New(char, sec_strndx->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(sec_strndx->sh_offset,SEEK_SET); fi->seek(sec_strndx->sh_offset,SEEK_SET);
fi->readx(strtab,sec_strndx->sh_size); fi->readx(strtab,sec_strndx->sh_size);
@@ -2560,7 +2560,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id"); Elf64_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id");
if (buildid) { if (buildid) {
unsigned char *data = new unsigned char[(unsigned) buildid->sh_size]; unsigned char *data = New(unsigned char, buildid->sh_size);
memset(data,0,buildid->sh_size); memset(data,0,buildid->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(buildid->sh_offset,SEEK_SET); fi->seek(buildid->sh_offset,SEEK_SET);
@@ -3253,7 +3253,7 @@ void PackLinuxElf64::unpack(OutputFile *fo)
unsigned orig_file_size = get_te32(&hbuf.p_filesize); unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize); blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size if (file_size > (off_t)orig_file_size || blocksize > orig_file_size
|| blocksize > 1024*1024*1024) || !mem_size_valid(1, blocksize, OVERHEAD))
throwCantUnpack("p_info corrupted"); throwCantUnpack("p_info corrupted");
ibuf.alloc(blocksize + OVERHEAD); ibuf.alloc(blocksize + OVERHEAD);
@@ -3782,7 +3782,7 @@ void PackLinuxElf32::unpack(OutputFile *fo)
unsigned orig_file_size = get_te32(&hbuf.p_filesize); unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize); blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size if (file_size > (off_t)orig_file_size || blocksize > orig_file_size
|| blocksize > 1024*1024*1024) || !mem_size_valid(1, blocksize, OVERHEAD))
throwCantUnpack("p_info corrupted"); throwCantUnpack("p_info corrupted");
ibuf.alloc(blocksize + OVERHEAD); ibuf.alloc(blocksize + OVERHEAD);
+1 -1
View File
@@ -98,7 +98,7 @@ PackVmlinuxBase<T>::compare_Phdr(void const *aa, void const *bb)
if (xa > xb) return 1; if (xa > xb) return 1;
if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr
if (a->p_paddr > b->p_paddr) return 1; if (a->p_paddr > b->p_paddr) return 1;
return 0; return 0;
} }
template <class T> template <class T>
+7 -7
View File
@@ -207,7 +207,7 @@ void PackWcle::encodeObjectTable()
{ {
unsigned ic,jc; unsigned ic,jc;
oobject_table = new le_object_table_entry_t[soobject_table = 2]; oobject_table = New(le_object_table_entry_t, soobject_table = 2);
memset(oobject_table,0,soobject_table * sizeof(*oobject_table)); memset(oobject_table,0,soobject_table * sizeof(*oobject_table));
// object #1: // object #1:
@@ -242,7 +242,7 @@ void PackWcle::encodeObjectTable()
void PackWcle::encodePageMap() void PackWcle::encodePageMap()
{ {
opm_entries = new le_pagemap_entry_t[sopm_entries = opages]; opm_entries = New(le_pagemap_entry_t, sopm_entries = opages);
for (unsigned ic = 0; ic < sopm_entries; ic++) for (unsigned ic = 0; ic < sopm_entries; ic++)
{ {
opm_entries[ic].l = (unsigned char) (ic+1); opm_entries[ic].l = (unsigned char) (ic+1);
@@ -256,7 +256,7 @@ void PackWcle::encodePageMap()
void PackWcle::encodeFixupPageTable() void PackWcle::encodeFixupPageTable()
{ {
unsigned ic; unsigned ic;
ofpage_table = new unsigned[sofpage_table = 1 + opages]; ofpage_table = New(unsigned, sofpage_table = 1 + opages);
for (ofpage_table[0] = ic = 0; ic < opages; ic++) for (ofpage_table[0] = ic = 0; ic < opages; ic++)
set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA); set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA);
} }
@@ -264,7 +264,7 @@ void PackWcle::encodeFixupPageTable()
void PackWcle::encodeFixups() void PackWcle::encodeFixups()
{ {
ofixups = new upx_byte[sofixups = 1*7 + FIXUP_EXTRA]; ofixups = New(upx_byte, sofixups = 1*7 + FIXUP_EXTRA);
memset(ofixups,0,sofixups); memset(ofixups,0,sofixups);
ofixups[0] = 7; ofixups[0] = 7;
set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1)); set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1));
@@ -608,7 +608,7 @@ void PackWcle::decodeFixups()
selfrel_fixups++; selfrel_fixups++;
unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9; unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9;
ofixups = new upx_byte[fixupn*9+1000+selectlen*5]; ofixups = New(upx_byte, fixupn*9+1000+selectlen*5);
upx_bytep fp = ofixups; upx_bytep fp = ofixups;
for (ic = 1, jc = 0; ic <= opages; ic++) for (ic = 1, jc = 0; ic <= opages; ic++)
@@ -682,7 +682,7 @@ void PackWcle::decodeFixups()
void PackWcle::decodeFixupPageTable() void PackWcle::decodeFixupPageTable()
{ {
ofpage_table = new unsigned[sofpage_table = 1 + opages]; ofpage_table = New(unsigned, sofpage_table = 1 + opages);
set_le32(ofpage_table,0); set_le32(ofpage_table,0);
// the rest of ofpage_table is filled by decodeFixups() // the rest of ofpage_table is filled by decodeFixups()
} }
@@ -691,7 +691,7 @@ void PackWcle::decodeFixupPageTable()
void PackWcle::decodeObjectTable() void PackWcle::decodeObjectTable()
{ {
soobject_table = oimage[ph.u_len - 1]; soobject_table = oimage[ph.u_len - 1];
oobject_table = new le_object_table_entry_t[soobject_table]; oobject_table = New(le_object_table_entry_t, soobject_table);
unsigned jc, ic = soobject_table * sizeof(*oobject_table); unsigned jc, ic = soobject_table * sizeof(*oobject_table);
const unsigned extradata = ph.version == 10 ? 17 : 13; const unsigned extradata = ph.version == 10 ? 17 : 13;
+21 -21
View File
@@ -310,7 +310,7 @@ PeFile::Reloc::Reloc(upx_byte *s,unsigned si) :
PeFile::Reloc::Reloc(unsigned rnum) : PeFile::Reloc::Reloc(unsigned rnum) :
start(NULL), size(0), rel(NULL), rel1(NULL) start(NULL), size(0), rel(NULL), rel1(NULL)
{ {
start = new upx_byte[rnum * 4 + 8192]; start = new upx_byte[mem_size(4, rnum, 8192)];
counts[0] = 0; counts[0] = 0;
} }
@@ -393,7 +393,7 @@ void PeFile32::processRelocs() // pass1
LE32 *fix[4]; LE32 *fix[4];
for (; ic; ic--) for (; ic; ic--)
fix[ic] = new LE32 [counts[ic]]; fix[ic] = New(LE32, counts[ic]);
unsigned xcounts[4]; unsigned xcounts[4];
memset(xcounts, 0, sizeof(xcounts)); memset(xcounts, 0, sizeof(xcounts));
@@ -430,7 +430,7 @@ void PeFile32::processRelocs() // pass1
} }
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3], sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3],
orelocs, ibuf + rvamin,1, &big_relocs), orelocs, ibuf + rvamin,1, &big_relocs),
orelocs); orelocs);
@@ -488,7 +488,7 @@ void PeFile64::processRelocs() // pass1
LE32 *fix[16]; LE32 *fix[16];
for (ic = 15; ic; ic--) for (ic = 15; ic; ic--)
fix[ic] = new LE32 [counts[ic]]; fix[ic] = New(LE32, counts[ic]);
unsigned xcounts[16]; unsigned xcounts[16];
memset(xcounts, 0, sizeof(xcounts)); memset(xcounts, 0, sizeof(xcounts));
@@ -528,7 +528,7 @@ void PeFile64::processRelocs() // pass1
} }
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10], sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10],
orelocs, ibuf + rvamin,1, &big_relocs), orelocs, ibuf + rvamin,1, &big_relocs),
orelocs); orelocs);
@@ -617,7 +617,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64
unsigned l = strlen(dll); unsigned l = strlen(dll);
assert(l > 0); assert(l > 0);
char *name = new char[3 * l + 2]; char *name = New(char, 3 * l + 2);
assert(name); assert(name);
name[0] = first_char; name[0] = first_char;
char *n = name + 1 + 2 * l; char *n = name + 1 + 2 * l;
@@ -632,7 +632,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64
{ {
unsigned len = 1 + 2 * strlen(dll) + 1 + 2 * strlen(proc) + 1 + 1; unsigned len = 1 + 2 * strlen(dll) + 1 + 2 * strlen(proc) + 1 + 1;
tstr dlln(name_for_dll(dll, first_char)); tstr dlln(name_for_dll(dll, first_char));
char *procn = new char[len]; char *procn = New(char, len);
upx_snprintf(procn, len - 1, "%s%c", (const char*) dlln, separator); upx_snprintf(procn, len - 1, "%s%c", (const char*) dlln, separator);
encode_name(proc, procn + strlen(procn)); encode_name(proc, procn + strlen(procn));
return procn; return procn;
@@ -761,7 +761,7 @@ public:
int osize = 4 + 2 * nsections; // upper limit for alignments int osize = 4 + 2 * nsections; // upper limit for alignments
for (unsigned ic = 0; ic < nsections; ic++) for (unsigned ic = 0; ic < nsections; ic++)
osize += sections[ic]->size; osize += sections[ic]->size;
output = new upx_byte[osize]; output = New(upx_byte, osize);
outputlen = 0; outputlen = 0;
// sort the sections by name before adding them all // sort the sections by name before adding them all
@@ -936,7 +936,7 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1
soimport++; // separator soimport++; // separator
} }
} }
oimport = new upx_byte[soimport]; oimport = New(upx_byte, soimport);
memset(oimport,0,soimport); memset(oimport,0,soimport);
qsort(idlls,dllnum,sizeof (udll*),udll::compare); qsort(idlls,dllnum,sizeof (udll*),udll::compare);
@@ -1112,13 +1112,13 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize)
iv.add(edir.name,len); iv.add(edir.name,len);
len = 4 * edir.functions; len = 4 * edir.functions;
functionptrs = new char[len + 1]; functionptrs = New(char, len + 1);
memcpy(functionptrs,base + edir.addrtable,len); memcpy(functionptrs,base + edir.addrtable,len);
size += len; size += len;
iv.add(edir.addrtable,len); iv.add(edir.addrtable,len);
unsigned ic; unsigned ic;
names = new char* [edir.names + edir.functions + 1]; names = New(char *, edir.names + edir.functions + 1);
for (ic = 0; ic < edir.names; ic++) for (ic = 0; ic < edir.names; ic++)
{ {
char *n = base + get_le32(base + edir.nameptrtable + ic * 4); char *n = base + get_le32(base + edir.nameptrtable + ic * 4);
@@ -1145,7 +1145,7 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize)
names[ic + edir.names] = NULL; names[ic + edir.names] = NULL;
len = 2 * edir.names; len = 2 * edir.names;
ordinals = new char[len + 1]; ordinals = New(char, len + 1);
memcpy(ordinals,base + edir.ordinaltable,len); memcpy(ordinals,base + edir.ordinaltable,len);
size += len; size += len;
iv.add(edir.ordinaltable,len); iv.add(edir.ordinaltable,len);
@@ -1207,7 +1207,7 @@ void PeFile::processExports(Export *xport) // pass1
} }
xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT)); xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT));
soexport = ALIGN_UP(xport->getsize(), 4u); soexport = ALIGN_UP(xport->getsize(), 4u);
oexport = new upx_byte[soexport]; oexport = New(upx_byte, soexport);
memset(oexport, 0, soexport); memset(oexport, 0, soexport);
} }
@@ -1327,7 +1327,7 @@ void PeFile::processTls1(Interval *iv,
sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size; sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size;
// the PE loader wants this stuff uncompressed // the PE loader wants this stuff uncompressed
otls = new upx_byte[sotls]; otls = New(upx_byte, sotls);
memset(otls,0,sotls); memset(otls,0,sotls);
memcpy(otls,ibuf + IDADDR(PEDIR_TLS),sizeof(tls)); memcpy(otls,ibuf + IDADDR(PEDIR_TLS),sizeof(tls));
// WARNING: this can acces data in BSS // WARNING: this can acces data in BSS
@@ -1419,7 +1419,7 @@ void PeFile::processLoadConf(Interval *iv) // pass 1
// printf("loadconf reloc detected: %x\n", pos); // printf("loadconf reloc detected: %x\n", pos);
} }
oloadconf = new upx_byte[soloadconf]; oloadconf = New(upx_byte, soloadconf);
memcpy(oloadconf, loadconf, soloadconf); memcpy(oloadconf, loadconf, soloadconf);
} }
@@ -1643,7 +1643,7 @@ PeFile::Resource::upx_rnode *PeFile::Resource::convert(const void *rnode,
ibufcheck(p, 2); ibufcheck(p, 2);
const unsigned len = 2 + 2 * get_le16(p); const unsigned len = 2 + 2 * get_le16(p);
ibufcheck(p, len); ibufcheck(p, len);
child->name = new upx_byte[len]; child->name = New(upx_byte, len);
memcpy(child->name,p,len); // copy unicode string memcpy(child->name,p,len); // copy unicode string
ssize += len; // size of unicode strings ssize += len; // size of unicode strings
} }
@@ -1698,7 +1698,7 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos,
upx_byte *PeFile::Resource::build() upx_byte *PeFile::Resource::build()
{ {
newstart = new upx_byte [dirsize()]; newstart = New(upx_byte, dirsize());
unsigned bpos = 0,spos = dsize; unsigned bpos = 0,spos = dsize;
build(root,bpos,spos,0); build(root,bpos,spos,0);
@@ -1864,7 +1864,7 @@ void PeFile::processResources(Resource *res)
for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size()) for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size())
; ;
oresources = new upx_byte[soresources]; oresources = New(upx_byte, soresources);
upx_byte *ores = oresources + res->dirsize(); upx_byte *ores = oresources + res->dirsize();
char *keep_icons = NULL; // icon ids in the first icon group char *keep_icons = NULL; // icon ids in the first icon group
@@ -1874,7 +1874,7 @@ void PeFile::processResources(Resource *res)
if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0) if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0)
{ {
iconsin1stdir = get_le16(ibuf + res->offs() + 4); iconsin1stdir = get_le16(ibuf + res->offs() + 4);
keep_icons = new char[1 + iconsin1stdir * 9]; keep_icons = New(char, 1 + iconsin1stdir * 9);
*keep_icons = 0; *keep_icons = 0;
for (unsigned ic = 0; ic < iconsin1stdir; ic++) for (unsigned ic = 0; ic < iconsin1stdir; ic++)
upx_snprintf(keep_icons + strlen(keep_icons), 9, "3/%u,", upx_snprintf(keep_icons + strlen(keep_icons), 9, "3/%u,",
@@ -2030,7 +2030,7 @@ unsigned PeFile::stripDebug(unsigned overlaystart)
void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih) void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih)
{ {
isection = new pe_section_t[objs]; isection = New(pe_section_t, objs);
fi->seek(pe_offset+sizeof_ih,SEEK_SET); fi->seek(pe_offset+sizeof_ih,SEEK_SET);
fi->readx(isection,sizeof(pe_section_t)*objs); fi->readx(isection,sizeof(pe_section_t)*objs);
rvamin = isection[0].vaddr; rvamin = isection[0].vaddr;
@@ -2920,7 +2920,7 @@ int PeFile::canUnpack0(unsigned max_sections, LE16 &ih_objects,
return false; return false;
unsigned objs = ih_objects; unsigned objs = ih_objects;
isection = new pe_section_t[objs]; isection = New(pe_section_t, objs);
fi->seek(pe_offset + ihsize, SEEK_SET); fi->seek(pe_offset + ihsize, SEEK_SET);
fi->readx(isection,sizeof(pe_section_t)*objs); fi->readx(isection,sizeof(pe_section_t)*objs);
if (ih_objects < 3) if (ih_objects < 3)
+1 -1
View File
@@ -83,7 +83,7 @@ void do_one_file(const char *iname, char *oname)
throwIOException("empty file -- skipped"); throwIOException("empty file -- skipped");
if (st.st_size < 512) if (st.st_size < 512)
throwIOException("file is too small -- skipped"); throwIOException("file is too small -- skipped");
if (st.st_size >= 1024*1024*1024) if (!mem_size_valid(1, st.st_size))
throwIOException("file is too large -- skipped"); throwIOException("file is too large -- skipped");
if ((st.st_mode & S_IWUSR) == 0) if ((st.st_mode & S_IWUSR) == 0)
{ {