@@ -48,45 +48,43 @@ struct MsanMapUnmapCallback {
4848 }
4949 }
5050};
51- } // namespace
5251
53- namespace __msan {
5452// Note: to ensure that the allocator is compatible with the application memory
5553// layout (especially with high-entropy ASLR), kSpaceBeg and kSpaceSize must be
5654// duplicated as MappingDesc::ALLOCATOR in msan.h.
5755#if defined(__mips64)
58- static const uptr kMaxAllowedMallocSize = 2UL << 30 ;
56+ const uptr kMaxAllowedMallocSize = 2UL << 30 ;
5957
6058struct AP32 {
6159 static const uptr kSpaceBeg = 0 ;
6260 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
6361 static const uptr kMetadataSize = sizeof (Metadata);
64- typedef __sanitizer::CompactSizeClassMap SizeClassMap ;
62+ using SizeClassMap = __sanitizer::CompactSizeClassMap;
6563 static const uptr kRegionSizeLog = 20 ;
6664 using AddressSpaceView = LocalAddressSpaceView;
67- typedef MsanMapUnmapCallback MapUnmapCallback;
65+ using MapUnmapCallback = MsanMapUnmapCallback ;
6866 static const uptr kFlags = 0 ;
6967};
70- typedef SizeClassAllocator32<AP32> PrimaryAllocator ;
68+ using PrimaryAllocator = SizeClassAllocator32<AP32>;
7169#elif defined(__x86_64__)
7270#if SANITIZER_NETBSD || SANITIZER_LINUX
73- static const uptr kAllocatorSpace = 0x700000000000ULL ;
71+ const uptr kAllocatorSpace = 0x700000000000ULL ;
7472#else
75- static const uptr kAllocatorSpace = 0x600000000000ULL ;
73+ const uptr kAllocatorSpace = 0x600000000000ULL ;
7674#endif
77- static const uptr kMaxAllowedMallocSize = 1ULL << 40 ;
75+ const uptr kMaxAllowedMallocSize = 1ULL << 40 ;
7876
7977struct AP64 { // Allocator64 parameters. Deliberately using a short name.
8078 static const uptr kSpaceBeg = kAllocatorSpace ;
8179 static const uptr kSpaceSize = 0x40000000000 ; // 4T.
8280 static const uptr kMetadataSize = sizeof (Metadata);
83- typedef DefaultSizeClassMap SizeClassMap;
84- typedef MsanMapUnmapCallback MapUnmapCallback;
81+ using SizeClassMap = DefaultSizeClassMap ;
82+ using MapUnmapCallback = MsanMapUnmapCallback ;
8583 static const uptr kFlags = 0 ;
8684 using AddressSpaceView = LocalAddressSpaceView;
8785};
8886
89- typedef SizeClassAllocator64<AP64> PrimaryAllocator ;
87+ using PrimaryAllocator = SizeClassAllocator64<AP64>;
9088
9189#elif defined(__loongarch_lp64)
9290const uptr kAllocatorSpace = 0x700000000000ULL ;
@@ -96,58 +94,58 @@ struct AP64 { // Allocator64 parameters. Deliberately using a short name.
9694 static const uptr kSpaceBeg = kAllocatorSpace ;
9795 static const uptr kSpaceSize = 0x40000000000 ; // 4T.
9896 static const uptr kMetadataSize = sizeof (Metadata);
99- typedef DefaultSizeClassMap SizeClassMap;
100- typedef MsanMapUnmapCallback MapUnmapCallback;
97+ using SizeClassMap = DefaultSizeClassMap ;
98+ using MapUnmapCallback = MsanMapUnmapCallback ;
10199 static const uptr kFlags = 0 ;
102100 using AddressSpaceView = LocalAddressSpaceView;
103101};
104102
105- typedef SizeClassAllocator64<AP64> PrimaryAllocator ;
103+ using PrimaryAllocator = SizeClassAllocator64<AP64>;
106104
107105#elif defined(__powerpc64__)
108- static const uptr kMaxAllowedMallocSize = 2UL << 30 ; // 2G
106+ const uptr kMaxAllowedMallocSize = 2UL << 30 ; // 2G
109107
110108struct AP64 { // Allocator64 parameters. Deliberately using a short name.
111109 static const uptr kSpaceBeg = 0x300000000000 ;
112110 static const uptr kSpaceSize = 0x020000000000 ; // 2T.
113111 static const uptr kMetadataSize = sizeof (Metadata);
114- typedef DefaultSizeClassMap SizeClassMap;
115- typedef MsanMapUnmapCallback MapUnmapCallback;
112+ using SizeClassMap = DefaultSizeClassMap ;
113+ using MapUnmapCallback = MsanMapUnmapCallback ;
116114 static const uptr kFlags = 0 ;
117115 using AddressSpaceView = LocalAddressSpaceView;
118116};
119117
120- typedef SizeClassAllocator64<AP64> PrimaryAllocator ;
118+ using PrimaryAllocator = SizeClassAllocator64<AP64>;
121119#elif defined(__s390x__)
122- static const uptr kMaxAllowedMallocSize = 2UL << 30 ; // 2G
120+ const uptr kMaxAllowedMallocSize = 2UL << 30 ; // 2G
123121
124122struct AP64 { // Allocator64 parameters. Deliberately using a short name.
125123 static const uptr kSpaceBeg = 0x440000000000 ;
126124 static const uptr kSpaceSize = 0x020000000000 ; // 2T.
127125 static const uptr kMetadataSize = sizeof (Metadata);
128- typedef DefaultSizeClassMap SizeClassMap;
129- typedef MsanMapUnmapCallback MapUnmapCallback;
126+ using SizeClassMap = DefaultSizeClassMap ;
127+ using MapUnmapCallback = MsanMapUnmapCallback ;
130128 static const uptr kFlags = 0 ;
131129 using AddressSpaceView = LocalAddressSpaceView;
132130};
133131
134- typedef SizeClassAllocator64<AP64> PrimaryAllocator ;
132+ using PrimaryAllocator = SizeClassAllocator64<AP64>;
135133#elif defined(__aarch64__)
136- static const uptr kMaxAllowedMallocSize = 8UL << 30 ;
134+ const uptr kMaxAllowedMallocSize = 8UL << 30 ;
137135
138136struct AP64 {
139137 static const uptr kSpaceBeg = 0xE00000000000ULL ;
140138 static const uptr kSpaceSize = 0x40000000000 ; // 4T.
141139 static const uptr kMetadataSize = sizeof (Metadata);
142- typedef DefaultSizeClassMap SizeClassMap;
143- typedef MsanMapUnmapCallback MapUnmapCallback;
140+ using SizeClassMap = DefaultSizeClassMap ;
141+ using MapUnmapCallback = MsanMapUnmapCallback ;
144142 static const uptr kFlags = 0 ;
145143 using AddressSpaceView = LocalAddressSpaceView;
146144};
147- typedef SizeClassAllocator64<AP64> PrimaryAllocator ;
145+ using PrimaryAllocator = SizeClassAllocator64<AP64>;
148146#endif
149- typedef CombinedAllocator<PrimaryAllocator> Allocator ;
150- typedef Allocator::AllocatorCache AllocatorCache;
147+ using Allocator = CombinedAllocator<PrimaryAllocator>;
148+ using AllocatorCache = Allocator::AllocatorCache;
151149} // namespace __msan
152150
153151static Allocator allocator;
@@ -186,7 +184,7 @@ void MsanThreadLocalMallocStorage::CommitBack() {
186184}
187185
188186static void *MsanAllocate (BufferedStackTrace *stack, uptr size, uptr alignment,
189- bool zeroise ) {
187+ bool zero ) {
190188 if (UNLIKELY (size > max_malloc_size)) {
191189 if (AllocatorMayReturnNull ()) {
192190 Report (" WARNING: MemorySanitizer failed to allocate 0x%zx bytes\n " , size);
@@ -218,10 +216,9 @@ static void *MsanAllocate(BufferedStackTrace *stack, uptr size, uptr alignment,
218216 GET_FATAL_STACK_TRACE_IF_EMPTY (stack);
219217 ReportOutOfMemory (size, stack);
220218 }
221- Metadata *meta =
222- reinterpret_cast <Metadata *>(allocator.GetMetaData (allocated));
219+ auto *meta = reinterpret_cast <Metadata *>(allocator.GetMetaData (allocated));
223220 meta->requested_size = size;
224- if (zeroise ) {
221+ if (zero ) {
225222 if (allocator.FromPrimary (allocated))
226223 __msan_clear_and_unpoison (allocated, size);
227224 else
@@ -258,8 +255,7 @@ void __msan::MsanDeallocate(BufferedStackTrace *stack, void *p) {
258255 __msan_set_origin (p, size, o.raw_id ());
259256 }
260257 }
261- MsanThread *t = GetCurrentThread ();
262- if (t) {
258+ if (MsanThread *t = GetCurrentThread ()) {
263259 AllocatorCache *cache = GetAllocatorCache (&t->malloc_storage ());
264260 allocator.Deallocate (cache, p);
265261 } else {
@@ -286,7 +282,7 @@ static void *MsanReallocate(BufferedStackTrace *stack, void *old_p,
286282 return old_p;
287283 }
288284 uptr memcpy_size = Min (new_size, old_size);
289- void *new_p = MsanAllocate (stack, new_size, alignment, false /* zeroise */ );
285+ void *new_p = MsanAllocate (stack, new_size, alignment, false );
290286 if (new_p) {
291287 CopyMemory (new_p, old_p, memcpy_size, stack);
292288 MsanDeallocate (stack, old_p);
@@ -310,27 +306,27 @@ static const void *AllocationBegin(const void *p) {
310306 void *beg = allocator.GetBlockBegin (p);
311307 if (!beg)
312308 return nullptr ;
313- Metadata *b = ( Metadata *) allocator.GetMetaData (beg);
309+ auto *b = reinterpret_cast < Metadata *>( allocator.GetMetaData (beg) );
314310 if (!b)
315311 return nullptr ;
316312 if (b->requested_size == 0 )
317313 return nullptr ;
318314
319- return (const void *)beg;
320- }
321-
322- static uptr AllocationSize (const void *p) {
323- if (!p) return 0 ;
324- const void *beg = allocator.GetBlockBegin (p);
325- if (beg != p) return 0 ;
326- Metadata *b = (Metadata *)allocator.GetMetaData (p);
327- return b->requested_size ;
315+ return beg;
328316}
329317
330318static uptr AllocationSizeFast (const void *p) {
331319 return reinterpret_cast <Metadata *>(allocator.GetMetaData (p))->requested_size ;
332320}
333321
322+ static uptr AllocationSize (const void *p) {
323+ if (!p)
324+ return 0 ;
325+ if (allocator.GetBlockBegin (p) != p)
326+ return 0 ;
327+ return AllocationSizeFast (p);
328+ }
329+
334330void *__msan::msan_malloc (uptr size, BufferedStackTrace *stack) {
335331 return SetErrnoOnNull (MsanAllocate (stack, size, sizeof (u64 ), false ));
336332}
0 commit comments