Skip to content

Commit 2729b13

Browse files
committed
[libNewDelete] Over-align the allocation.
By leveraging std::max_align_t, insure that the return addressed (offset-ed compared to the address allocated by TStorage::ObjectAlloc) is still well aligned. In https://root-forum.cern.ch/t/libnew-so-and-tcanvas/51039/8, executing in rootn.exe: TMapFile *mf=TMapFile::Create("test.map","recreate") TH1 *h=new TH1F("test","test",100,-5,5) h->Fill (0) h->Draw() led to a segmentation fault in during the execution of: => 0x00007ffff2a62a12 <+162>: movaps %xmm0,0x10(%r12) with an address that was ending in '8'. When allocating the TMethodCall, the address returned was aligned on a 16 bit boundary but the memory counting over-allocating was shifting the address by 'only' 8 bits, leading to a mis-aligned access by this vector memcpy.
1 parent 588e13c commit 2729b13

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

core/newdelete/src/NewDelete.cxx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,17 @@ static TReAllocInit gReallocInit;
8989

9090
#ifdef MEM_DEBUG
9191
# define MEM_MAGIC ((unsigned char)0xAB)
92+
# define RealStart(p) ((char*)(p) - sizeof(std::max_align_t))
9293
#ifdef R__B64
93-
# define storage_size(p) ((size_t)(((size_t*)p)[-1]))
94-
# define RealStart(p) ((char*)(p) - sizeof(size_t))
94+
# define storage_size(p) (*(size_t*)RealStart(p))
9595
# define StoreSize(p, sz) (*((size_t*)(p)) = (sz))
96-
# define ExtStart(p) ((char*)(p) + sizeof(size_t))
97-
# define RealSize(sz) ((sz) + sizeof(size_t) + sizeof(char))
98-
# define StoreMagic(p, sz) *((unsigned char*)(p)+sz+sizeof(size_t)) = MEM_MAGIC
9996
#else
100-
# define storage_size(p) ((size_t)(((int*)p)[-2]))
101-
# define RealStart(p) ((char*)(p) - 2*sizeof(int))
10297
# define StoreSize(p, sz) (*((int*)(p)) = (sz))
103-
# define ExtStart(p) ((char*)(p) + 2*sizeof(int))
104-
# define RealSize(sz) ((sz) + 2*sizeof(int) + sizeof(char))
105-
# define StoreMagic(p, sz) *((unsigned char*)(p)+sz+2*sizeof(int)) = MEM_MAGIC
98+
# define storage_size(p) ((size_t)*(int*)RealStart(p))
10699
#endif
100+
# define ExtStart(p) ((char*)(p) + sizeof(std::max_align_t))
101+
# define RealSize(sz) ((sz) + sizeof(std::max_align_t) + sizeof(char))
102+
# define StoreMagic(p, sz) *((unsigned char*)(p)+sz+sizeof(std::max_align_t)) = MEM_MAGIC
107103
# define MemClear(p, start, len) \
108104
if ((len) > 0) memset(&((char*)(p))[(start)], 0, (len))
109105
# define TestMagic(p, sz) (*((unsigned char*)(p)+sz) != MEM_MAGIC)

0 commit comments

Comments
 (0)