[asan] asan_allocator2 fix two asserts that happen on full chrome: a) memalign called with 0 size and large alignment and b) malloc called after TSD has been destructed
llvm-svn: 170900
This commit is contained in:
		
							parent
							
								
									fbde69e266
								
							
						
					
					
						commit
						fe80f080ee
					
				| 
						 | 
					@ -82,7 +82,7 @@ static const uptr kMaxAllowedMallocSize =
 | 
				
			||||||
static const uptr kMaxThreadLocalQuarantine =
 | 
					static const uptr kMaxThreadLocalQuarantine =
 | 
				
			||||||
  FIRST_32_SECOND_64(1 << 18, 1 << 20);
 | 
					  FIRST_32_SECOND_64(1 << 18, 1 << 20);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const uptr kReturnOnZeroMalloc = 0x0123;  // Zero page is protected.
 | 
					static const uptr kReturnOnZeroMalloc = 2048;  // Zero page is protected.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int inited = 0;
 | 
					static int inited = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -282,8 +282,12 @@ static void *Allocate(uptr size, uptr alignment, StackTrace *stack,
 | 
				
			||||||
  Init();
 | 
					  Init();
 | 
				
			||||||
  CHECK(stack);
 | 
					  CHECK(stack);
 | 
				
			||||||
  if (alignment < 8) alignment = 8;
 | 
					  if (alignment < 8) alignment = 8;
 | 
				
			||||||
  if (size == 0)
 | 
					  if (size == 0) {
 | 
				
			||||||
 | 
					    if (alignment <= kReturnOnZeroMalloc)
 | 
				
			||||||
      return reinterpret_cast<void *>(kReturnOnZeroMalloc);
 | 
					      return reinterpret_cast<void *>(kReturnOnZeroMalloc);
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      return 0;  // 0 bytes with large alignment requested. Just return 0.
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  CHECK(IsPowerOfTwo(alignment));
 | 
					  CHECK(IsPowerOfTwo(alignment));
 | 
				
			||||||
  uptr rz_size = ComputeRZSize(size);
 | 
					  uptr rz_size = ComputeRZSize(size);
 | 
				
			||||||
  uptr rounded_size = RoundUpTo(size, rz_size);
 | 
					  uptr rounded_size = RoundUpTo(size, rz_size);
 | 
				
			||||||
| 
						 | 
					@ -298,10 +302,8 @@ static void *Allocate(uptr size, uptr alignment, StackTrace *stack,
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  AsanThread *t = asanThreadRegistry().GetCurrent();
 | 
					  AsanThread *t = asanThreadRegistry().GetCurrent();
 | 
				
			||||||
  // Printf("t = %p\n", t);
 | 
					  AllocatorCache *cache = t ? GetAllocatorCache(&t->malloc_storage()) : 0;
 | 
				
			||||||
  CHECK(t);  // FIXME
 | 
					  void *allocated = allocator.Allocate(cache, needed_size, 8, false);
 | 
				
			||||||
  void *allocated = allocator.Allocate(
 | 
					 | 
				
			||||||
      GetAllocatorCache(&t->malloc_storage()), needed_size, 8, false);
 | 
					 | 
				
			||||||
  uptr alloc_beg = reinterpret_cast<uptr>(allocated);
 | 
					  uptr alloc_beg = reinterpret_cast<uptr>(allocated);
 | 
				
			||||||
  uptr alloc_end = alloc_beg + needed_size;
 | 
					  uptr alloc_end = alloc_beg + needed_size;
 | 
				
			||||||
  uptr beg_plus_redzone = alloc_beg + rz_size;
 | 
					  uptr beg_plus_redzone = alloc_beg + rz_size;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -762,10 +762,14 @@ class CombinedAllocator {
 | 
				
			||||||
    if (alignment > 8)
 | 
					    if (alignment > 8)
 | 
				
			||||||
      size = RoundUpTo(size, alignment);
 | 
					      size = RoundUpTo(size, alignment);
 | 
				
			||||||
    void *res;
 | 
					    void *res;
 | 
				
			||||||
    if (primary_.CanAllocate(size, alignment))
 | 
					    if (primary_.CanAllocate(size, alignment)) {
 | 
				
			||||||
 | 
					      if (cache)  // Allocate from cache.
 | 
				
			||||||
        res = cache->Allocate(&primary_, primary_.ClassID(size));
 | 
					        res = cache->Allocate(&primary_, primary_.ClassID(size));
 | 
				
			||||||
    else
 | 
					      else  // No thread-local cache, allocate directly from primary allocator.
 | 
				
			||||||
 | 
					        res = primary_.Allocate(size, alignment);
 | 
				
			||||||
 | 
					    } else {  // Secondary allocator does not use cache.
 | 
				
			||||||
      res = secondary_.Allocate(size, alignment);
 | 
					      res = secondary_.Allocate(size, alignment);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (alignment > 8)
 | 
					    if (alignment > 8)
 | 
				
			||||||
      CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
 | 
					      CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
 | 
				
			||||||
    if (cleared && res)
 | 
					    if (cleared && res)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue