File: leak_detector_impl_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (751 lines) | stat: -rw-r--r-- 28,890 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/leak_detector/leak_detector_impl.h"

#include <math.h>
#include <stddef.h>
#include <stdint.h>

#include <complex>
#include <memory>
#include <new>
#include <set>
#include <vector>

#include "base/macros.h"
#include "components/metrics/leak_detector/custom_allocator.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace metrics {
namespace leak_detector {

namespace {

// Makes working with complex numbers easier.
using Complex = std::complex<double>;

using InternalLeakReport = LeakDetectorImpl::LeakReport;
using AllocationBreakdown = LeakDetectorImpl::LeakReport::AllocationBreakdown;

// The mapping location in memory for a fictional executable.
const uintptr_t kMappingAddr = 0x800000;
const size_t kMappingSize = 0x200000;

// Some call stacks within the fictional executable.
// * - outside the mapping range, e.g. JIT code.
const uintptr_t kRawStack0[] = {
    0x800100, 0x900000, 0x880080, 0x810000,
};
const uintptr_t kRawStack1[] = {
    0x940000, 0x980000,
    0xdeadbeef,  // *
    0x9a0000,
};
const uintptr_t kRawStack2[] = {
    0x8f0d00, 0x803abc, 0x9100a0,
};
const uintptr_t kRawStack3[] = {
    0x90fcde,
    0x900df00d,  // *
    0x801000,   0x880088,
    0xdeadcafe,  // *
    0x9f0000,   0x8700a0, 0x96037c,
};
const uintptr_t kRawStack4[] = {
    0x8c0000, 0x85d00d, 0x921337,
    0x780000,  // *
};
const uintptr_t kRawStack5[] = {
    0x990000, 0x888888, 0x830ac0, 0x8e0000,
    0xc00000,  // *
};

// This struct makes it easier to pass call stack info to
// LeakDetectorImplTest::Alloc().
struct TestCallStack {
  const uintptr_t* stack;  // A reference to the original stack data.
  size_t depth;
};

const TestCallStack kStack0 = {kRawStack0, arraysize(kRawStack0)};
const TestCallStack kStack1 = {kRawStack1, arraysize(kRawStack1)};
const TestCallStack kStack2 = {kRawStack2, arraysize(kRawStack2)};
const TestCallStack kStack3 = {kRawStack3, arraysize(kRawStack3)};
const TestCallStack kStack4 = {kRawStack4, arraysize(kRawStack4)};
const TestCallStack kStack5 = {kRawStack5, arraysize(kRawStack5)};

// The interval between consecutive analyses (LeakDetectorImpl::TestForLeaks),
// in number of bytes allocated. e.g. if |kAllocedSizeAnalysisInterval| = 1024
// then call TestForLeaks() every 1024 bytes of allocation that occur.
const size_t kAllocedSizeAnalysisInterval = 8192;

// Suspicion thresholds used by LeakDetectorImpl for size and call stacks.
const uint32_t kSizeSuspicionThreshold = 4;
const uint32_t kCallStackSuspicionThreshold = 4;

// Because it takes N+1 analyses to reach a suspicion threshold of N (the
// suspicion score is only calculated based on deltas from the previous
// analysis), the actual number of analyses it takes to generate a report for
// the first time is:
const uint32_t kMinNumAnalysesToGenerateReport =
    kSizeSuspicionThreshold + 1 + kCallStackSuspicionThreshold + 1;

// Returns the offset within [kMappingAddr, kMappingAddr + kMappingSize) if
// |addr| falls in that range. Otherwise, returns |UINTPTR_MAX|.
uintptr_t GetOffsetInMapping(uintptr_t addr) {
  if (addr >= kMappingAddr && addr < kMappingAddr + kMappingSize)
    return addr - kMappingAddr;
  return UINTPTR_MAX;
}

// Copied from leak_detector_impl.cc. Converts a size to a size class index.
// Any size in the range [index * 4, index * 4 + 3] falls into that size class.
uint32_t SizeToIndex(size_t size) {
  return size / sizeof(uint32_t);
}

// Returns true if the |alloc_breakdown_history_| field of the two LeakReports
// |a| and |b| are the same.
bool CompareReportAllocHistory(const InternalLeakReport& a,
                               const InternalLeakReport& b) {
  auto alloc_breakdown_compare_func = [](AllocationBreakdown a,
                                         AllocationBreakdown b) -> bool {
    return std::equal(a.counts_by_size.begin(), a.counts_by_size.end(),
                      b.counts_by_size.begin()) &&
           a.count_for_call_stack == b.count_for_call_stack;
  };
  return std::equal(
      a.alloc_breakdown_history().begin(), a.alloc_breakdown_history().end(),
      b.alloc_breakdown_history().begin(), alloc_breakdown_compare_func);
}

}  // namespace

// This test suite will test the ability of LeakDetectorImpl to catch leaks in
// a program. Individual tests can run leaky code locally.
//
// The leaky code must call Alloc() and Free() for heap memory management. It
// should not call See comments on those
// functions for more details.
class LeakDetectorImplTest : public ::testing::Test {
 public:
  LeakDetectorImplTest()
      : total_num_allocs_(0),
        total_num_frees_(0),
        total_alloced_size_(0),
        next_analysis_total_alloced_size_(kAllocedSizeAnalysisInterval),
        num_reports_generated_(0) {}

  void SetUp() override {
    CustomAllocator::Initialize();

    detector_.reset(new LeakDetectorImpl(kMappingAddr, kMappingSize,
                                         kSizeSuspicionThreshold,
                                         kCallStackSuspicionThreshold));
  }

  void TearDown() override {
    // Free any memory that was leaked by test cases. Do not use Free() because
    // that will try to modify |alloced_ptrs_|.
    for (void* ptr : alloced_ptrs_)
      delete[] reinterpret_cast<char*>(ptr);
    alloced_ptrs_.clear();

    // Must destroy all objects that use CustomAllocator before shutting down.
    detector_.reset();
    stored_reports_.clear();

    EXPECT_TRUE(CustomAllocator::Shutdown());
  }

 protected:
  template <typename T>
  using InternalVector = LeakDetectorImpl::InternalVector<T>;

  // Alloc and free functions that allocate and free heap memory and
  // automatically pass alloc/free info to |detector_|. They emulate the
  // alloc/free hook functions that would call into LeakDetectorImpl in
  // real-life usage. They also keep track of individual allocations locally, so
  // any leaked memory could be cleaned up.
  //
  // |stack| is just a nominal call stack object to identify the call site. It
  // doesn't have to contain the stack trace of the actual call stack.
  void* Alloc(size_t size, const TestCallStack& stack) {
    void* ptr = new char[size];
    detector_->RecordAlloc(ptr, size, stack.depth,
                           reinterpret_cast<const void* const*>(stack.stack));

    EXPECT_TRUE(alloced_ptrs_.find(ptr) == alloced_ptrs_.end());
    alloced_ptrs_.insert(ptr);

    ++total_num_allocs_;
    total_alloced_size_ += size;
    if (total_alloced_size_ >= next_analysis_total_alloced_size_) {
      InternalVector<InternalLeakReport> reports;
      detector_->TestForLeaks(&reports, 1024);
      for (const InternalLeakReport& report : reports) {
        auto iter = stored_reports_.find(report);
        if (iter == stored_reports_.end()) {
          stored_reports_.insert(report);
        } else {
          // InternalLeakReports are uniquely identified by |alloc_size_bytes_|
          // and |call_stack_|. See InternalLeakReport::operator<().
          // If a report with the same size and call stack already exists,
          // overwrite it with the new report, which has a newer history.
          stored_reports_.erase(iter);
          stored_reports_.insert(report);
        }
      }
      num_reports_generated_ += reports.size();

      // Determine when the next leak analysis should occur.
      while (total_alloced_size_ >= next_analysis_total_alloced_size_)
        next_analysis_total_alloced_size_ += kAllocedSizeAnalysisInterval;
    }
    return ptr;
  }

  // See comment for Alloc().
  void Free(void* ptr) {
    auto find_ptr_iter = alloced_ptrs_.find(ptr);
    EXPECT_FALSE(find_ptr_iter == alloced_ptrs_.end());
    if (find_ptr_iter == alloced_ptrs_.end())
      return;
    alloced_ptrs_.erase(find_ptr_iter);
    ++total_num_frees_;

    detector_->RecordFree(ptr);

    delete[] reinterpret_cast<char*>(ptr);
  }

  // TEST CASE: Simple program that leaks memory regularly. Pass in
  // enable_leaks=true to trigger some memory leaks.
  void SimpleLeakyFunction(bool enable_leaks);

  // TEST CASE: Julia set fractal computation. Pass in enable_leaks=true to
  // trigger some memory leaks.
  void JuliaSet(bool enable_leaks);

  // Instance of the class being tested.
  std::unique_ptr<LeakDetectorImpl> detector_;

  // Number of pointers allocated and freed so far.
  size_t total_num_allocs_;
  size_t total_num_frees_;

  // Keeps count of total size allocated by Alloc().
  size_t total_alloced_size_;

  // The cumulative allocation size at which to trigger the TestForLeaks() call.
  size_t next_analysis_total_alloced_size_;

  // Stores all pointers to memory allocated by by Alloc() so we can manually
  // free the leaked pointers at the end. This also serves as redundant
  // bookkeepping: it stores all pointers that have been allocated but not yet
  // freed.
  std::set<void*> alloced_ptrs_;

  // Store leak reports here. Use a set so duplicate reports are not stored.
  std::set<InternalLeakReport> stored_reports_;

  // Keeps track of the actual number of reports (duplicate or not) that were
  // generated by |detector_|.
  size_t num_reports_generated_;

 private:
  DISALLOW_COPY_AND_ASSIGN(LeakDetectorImplTest);
};

void LeakDetectorImplTest::SimpleLeakyFunction(bool enable_leaks) {
  std::vector<uint32_t*> ptrs(7);

  const int kNumOuterIterations = 32;
  for (int j = 0; j < kNumOuterIterations; ++j) {
    // The inner loop allocates 256 bytes. Run it 32 times so that 8192 bytes
    // (|kAllocedSizeAnalysisInterval|) are allocated for each iteration of the
    // outer loop.
    const int kNumInnerIterations = 32;
    static_assert(kNumInnerIterations * 256 == kAllocedSizeAnalysisInterval,
                  "Inner loop iterations do not allocate the correct number of "
                  "bytes.");
    for (int i = 0; i < kNumInnerIterations; ++i) {
      size_t alloc_size_at_beginning = total_alloced_size_;

      ptrs[0] = new(Alloc(16, kStack0)) uint32_t;
      ptrs[1] = new(Alloc(32, kStack1)) uint32_t;
      ptrs[2] = new(Alloc(48, kStack2)) uint32_t;
      // Allocate two 32-byte blocks and record them as from the same call site.
      ptrs[3] = new(Alloc(32, kStack3)) uint32_t;
      ptrs[4] = new(Alloc(32, kStack3)) uint32_t;
      // Allocate two 48-byte blocks and record them as from the same call site.
      ptrs[5] = new(Alloc(48, kStack4)) uint32_t;
      ptrs[6] = new(Alloc(48, kStack4)) uint32_t;

      // Now free these pointers.
      Free(ptrs[0]);
      if (!enable_leaks)  // Leak with size=32, call_stack=kStack1.
        Free(ptrs[1]);
      if (!enable_leaks)  // Leak with size=48, call_stack=kStack2.
        Free(ptrs[2]);
      Free(ptrs[3]);
      Free(ptrs[4]);
      Free(ptrs[5]);
      Free(ptrs[6]);

      // Make sure that the above code actually allocates 256 bytes.
      EXPECT_EQ(alloc_size_at_beginning + 256, total_alloced_size_);
    }
  }
}

void LeakDetectorImplTest::JuliaSet(bool enable_leaks) {
  // The center region of the complex plane that is the basis for our Julia set
  // computations is a circle of radius kRadius.
  constexpr double kRadius = 2;

  // To track points in the complex plane, we will use a rectangular grid in the
  // range defined by [-kRadius, kRadius] along both axes.
  constexpr double kRangeMin = -kRadius;
  constexpr double kRangeMax = kRadius;

  // Divide each axis into intervals, each of which is associated with a point
  // on that axis at its center.
  constexpr double kIntervalInverse = 64;
  constexpr double kInterval = 1.0 / kIntervalInverse;
  constexpr int kNumPoints = (kRangeMax - kRangeMin) / kInterval + 1;

  // Contains some useful functions for converting between points on the complex
  // plane and in a gridlike data structure.
  struct ComplexPlane {
    static int GetXGridIndex(const Complex& value) {
      return (value.real() + kInterval / 2 - kRangeMin) / kInterval;
    }
    static int GetYGridIndex(const Complex& value) {
      return (value.imag() + kInterval / 2 - kRangeMin) / kInterval;
    }
    static int GetArrayIndex(const Complex& value) {
      return GetXGridIndex(value) + GetYGridIndex(value) * kNumPoints;
    }
    static Complex GetComplexForGridPoint(size_t x, size_t y) {
      return Complex(kRangeMin + x * kInterval, kRangeMin + y * kInterval);
    }
  };

  // Make sure the choice of interval doesn't result in any loss of precision.
  ASSERT_EQ(1.0, kInterval * kIntervalInverse);

  // Create a grid for part of the complex plane, with each axis within the
  // range [kRangeMin, kRangeMax].
  constexpr size_t width = kNumPoints;
  constexpr size_t height = kNumPoints;
  std::vector<Complex*> grid(width * height);

  // Initialize an object for each point within the inner circle |z| < kRadius.
  for (size_t i = 0; i < width; ++i) {
    for (size_t j = 0; j < height; ++j) {
      Complex point = ComplexPlane::GetComplexForGridPoint(i, j);
      // Do not store any values outside the inner circle.
      if (abs(point) <= kRadius) {
        grid[i + j * width] =
            new (Alloc(sizeof(Complex), kStack0)) Complex(point);
      }
    }
  }
  EXPECT_LE(alloced_ptrs_.size(), width * height);

  // Create a new grid for the result of the transformation.
  std::vector<Complex*> next_grid(width * height, nullptr);

  // Number of times to run the Julia set iteration. This is not the same as the
  // number of analyses performed by LeakDetectorImpl, which is determined by
  // the total number of bytes allocated divided by
  // |kAllocedSizeAnalysisInterval|.
  const int kNumIterations = 20;
  for (int n = 0; n < kNumIterations; ++n) {
    for (int i = 0; i < kNumPoints; ++i) {
      for (int j = 0; j < kNumPoints; ++j) {
        if (!grid[i + j * width])
          continue;

        // NOTE: The below code is NOT an efficient way to compute a Julia set.
        // This is only to test the leak detector with some nontrivial code.

        // A simple polynomial function for generating Julia sets is:
        //   f(z) = z^n + c

        // But in this algorithm, we need the inverse:
        //   fInv(z) = (z - c)^(1/n)

        // Here, let's use n=5 and c=0.544.
        const Complex c(0.544, 0);
        const Complex& z = *grid[i + j * width];

        // This is the principal root.
        Complex root = pow(z - c, 0.2);

        // Discard the result if it is too far out from the center of the plane.
        if (abs(root) > kRadius)
          continue;

        // The below code only allocates Complex objects of the same size. The
        // leak detector expects various sizes, so increase the allocation size
        // by a different amount at each call site.

        // Nth root produces N results.
        // Place all root results on |next_grid|.

        // First, place the principal root.
        if (!next_grid[ComplexPlane::GetArrayIndex(root)]) {
          next_grid[ComplexPlane::GetArrayIndex(root)] =
              new (Alloc(sizeof(Complex) + 24, kStack1)) Complex(root);
        }

        double magnitude = abs(root);
        double angle = arg(root);
        // To generate other roots, rotate the principal root by increments of
        // 1/N of a full circle.
        const double kAngleIncrement = M_PI * 2 / 5;

        // Second root.
        root = std::polar(magnitude, angle + kAngleIncrement);
        if (!next_grid[ComplexPlane::GetArrayIndex(root)]) {
          next_grid[ComplexPlane::GetArrayIndex(root)] =
              new (Alloc(sizeof(Complex) + 40, kStack2)) Complex(root);
        }

        // In some of the sections below, setting |enable_leaks| to true will
        // trigger a memory leak by overwriting the old Complex pointer value
        // without freeing it. Due to the nature of complex roots being confined
        // to equal sections of the complex plane, each new pointer will
        // displace an old pointer that was allocated from the same line of
        // code.

        // Third root.
        root = std::polar(magnitude, angle + kAngleIncrement * 2);
        // *** LEAK ***
        if (enable_leaks || !next_grid[ComplexPlane::GetArrayIndex(root)]) {
          next_grid[ComplexPlane::GetArrayIndex(root)] =
              new (Alloc(sizeof(Complex) + 40, kStack3)) Complex(root);
        }

        // Fourth root.
        root = std::polar(magnitude, angle + kAngleIncrement * 3);
        // *** LEAK ***
        if (enable_leaks || !next_grid[ComplexPlane::GetArrayIndex(root)]) {
          next_grid[ComplexPlane::GetArrayIndex(root)] =
              new (Alloc(sizeof(Complex) + 52, kStack4)) Complex(root);
        }

        // Fifth root.
        root = std::polar(magnitude, angle + kAngleIncrement * 4);
        if (!next_grid[ComplexPlane::GetArrayIndex(root)]) {
          next_grid[ComplexPlane::GetArrayIndex(root)] =
              new (Alloc(sizeof(Complex) + 52, kStack5)) Complex(root);
        }
      }
    }

    // Clear the previously allocated points.
    for (Complex*& point : grid) {
      if (point) {
        Free(point);
        point = nullptr;
      }
    }

    // Now swap the two grids for the next iteration.
    grid.swap(next_grid);
  }

  // Clear the previously allocated points.
  for (Complex*& point : grid) {
    if (point) {
      Free(point);
      point = nullptr;
    }
  }
}

TEST_F(LeakDetectorImplTest, CheckTestFramework) {
  EXPECT_EQ(0U, total_num_allocs_);
  EXPECT_EQ(0U, total_num_frees_);
  EXPECT_EQ(0U, alloced_ptrs_.size());

  // Allocate some memory.
  void* ptr0 = Alloc(12, kStack0);
  void* ptr1 = Alloc(16, kStack0);
  void* ptr2 = Alloc(24, kStack0);
  EXPECT_EQ(3U, total_num_allocs_);
  EXPECT_EQ(0U, total_num_frees_);
  EXPECT_EQ(3U, alloced_ptrs_.size());

  // Free one of the pointers.
  Free(ptr1);
  EXPECT_EQ(3U, total_num_allocs_);
  EXPECT_EQ(1U, total_num_frees_);
  EXPECT_EQ(2U, alloced_ptrs_.size());

  // Allocate some more memory.
  void* ptr3 = Alloc(72, kStack1);
  void* ptr4 = Alloc(104, kStack1);
  void* ptr5 = Alloc(96, kStack1);
  void* ptr6 = Alloc(24, kStack1);
  EXPECT_EQ(7U, total_num_allocs_);
  EXPECT_EQ(1U, total_num_frees_);
  EXPECT_EQ(6U, alloced_ptrs_.size());

  // Free more pointers.
  Free(ptr2);
  Free(ptr4);
  Free(ptr6);
  EXPECT_EQ(7U, total_num_allocs_);
  EXPECT_EQ(4U, total_num_frees_);
  EXPECT_EQ(3U, alloced_ptrs_.size());

  // Free remaining memory.
  Free(ptr0);
  Free(ptr3);
  Free(ptr5);
  EXPECT_EQ(7U, total_num_allocs_);
  EXPECT_EQ(7U, total_num_frees_);
  EXPECT_EQ(0U, alloced_ptrs_.size());
}

TEST_F(LeakDetectorImplTest, SimpleLeakyFunctionNoLeak) {
  SimpleLeakyFunction(false /* enable_leaks */);

  // SimpleLeakyFunction() should have run cleanly without leaking.
  EXPECT_EQ(total_num_allocs_, total_num_frees_);
  EXPECT_EQ(0U, alloced_ptrs_.size());
  EXPECT_EQ(0U, num_reports_generated_);
  EXPECT_EQ(0U, stored_reports_.size());
}

TEST_F(LeakDetectorImplTest, SimpleLeakyFunctionWithLeak) {
  SimpleLeakyFunction(true /* enable_leaks */);

  // SimpleLeakyFunction() should generated some leak reports.
  EXPECT_GT(total_num_allocs_, total_num_frees_);
  EXPECT_GT(alloced_ptrs_.size(), 0U);
  EXPECT_EQ(2U, num_reports_generated_);
  ASSERT_EQ(2U, stored_reports_.size());

  // The reports should be stored in order of size.

  // |report1| comes from the call site marked with kStack1, with size=32.
  const InternalLeakReport& report1 = *stored_reports_.begin();
  EXPECT_EQ(32U, report1.alloc_size_bytes());
  ASSERT_EQ(kStack1.depth, report1.call_stack().size());
  for (size_t i = 0; i < kStack1.depth; ++i) {
    EXPECT_EQ(GetOffsetInMapping(kStack1.stack[i]),
              report1.call_stack()[i]) << i;
  }

  // |report2| comes from the call site marked with kStack2, with size=48.
  const InternalLeakReport& report2 = *(++stored_reports_.begin());
  EXPECT_EQ(48U, report2.alloc_size_bytes());
  ASSERT_EQ(kStack2.depth, report2.call_stack().size());
  for (size_t i = 0; i < kStack2.depth; ++i) {
    EXPECT_EQ(GetOffsetInMapping(kStack2.stack[i]),
              report2.call_stack()[i]) << i;
  }

  // Check historical data recorded in the reports.
  // - Each inner loop iteration allocates a net of 1x 32 bytes and 1x 48 bytes.
  // - Each outer loop iteration allocates a net of 32x 32 bytes and 32x 48
  //   bytes.
  // - However, the leak analysis happens after the allocs but before the frees
  //   that come right after. So it should count the two extra allocs made at
  //   call sites |kStack3| and |kStack4|. The formula is |(i + 1) * 32 + 2|,
  //   where |i| is the iteration index.
  // - It takes |kMinNumAnalysesToGenerateReport| analyses for the first report
  //   to be generated. Subsequent analyises do not generate reports due to the
  //   cooldown mechanism.

  const auto& report1_history = report1.alloc_breakdown_history();
  EXPECT_EQ(kMinNumAnalysesToGenerateReport, report1_history.size());

  for (size_t i = 0; i < report1_history.size(); ++i) {
    const AllocationBreakdown& entry = report1_history[i];

    const InternalVector<uint32_t>& counts_by_size = entry.counts_by_size;
    ASSERT_GT(counts_by_size.size(), SizeToIndex(48));

    // Check the two leaky sizes, 32 and 48.
    uint32_t expected_leaky_count = (i + 1) * 32 + 2;
    EXPECT_EQ(expected_leaky_count, counts_by_size[SizeToIndex(32)]);
    EXPECT_EQ(expected_leaky_count, counts_by_size[SizeToIndex(48)]);

    // Not related to the leaks, but there should be a dangling 16-byte
    // allocation during each leak analysis, because it hasn't yet been freed.
    EXPECT_EQ(1U, counts_by_size[SizeToIndex(16)]);
  }

  // Check call site count over time.
  ASSERT_LT(kSizeSuspicionThreshold, report1_history.size());
  // Initially, there has been no call site tracking.
  for (size_t i = 0; i < kSizeSuspicionThreshold; ++i)
    EXPECT_EQ(0U, report1_history[i].count_for_call_stack);

  // Once |kSizeSuspicionThreshold| has been reached and call site tracking has
  // begun, the number of allocations for the suspected call site should
  // increase by 32 each frame. See comments above.
  uint32_t expected_call_stack_count = 0;
  for (size_t i = kSizeSuspicionThreshold; i < report1_history.size(); ++i) {
    EXPECT_EQ(expected_call_stack_count,
              report1_history[i].count_for_call_stack);
    expected_call_stack_count += 32;
  }

  // |report2| should have the same size history and call stack history as
  // |report1|.
  EXPECT_TRUE(CompareReportAllocHistory(report1, report2));
}

TEST_F(LeakDetectorImplTest, SimpleLeakyFunctionWithLeakThreeTimes) {
  // Run three iterations of the leaky function.
  SimpleLeakyFunction(true /* enable_leaks */);
  SimpleLeakyFunction(true /* enable_leaks */);
  SimpleLeakyFunction(true /* enable_leaks */);

  // SimpleLeakyFunction() should have generated three times as many leak
  // reports, because the number of iterations is the same as the cooldown of
  // LeakDetectorImpl. But the number of unique reports stored is still two.
  EXPECT_EQ(6U, num_reports_generated_);
  ASSERT_EQ(2U, stored_reports_.size());

  // The reports should be stored in order of size.

  // |report1| comes from the call site marked with kStack1, with size=32.
  const InternalLeakReport& report1 = *stored_reports_.begin();
  EXPECT_EQ(32U, report1.alloc_size_bytes());
  ASSERT_EQ(kStack1.depth, report1.call_stack().size());
  for (size_t i = 0; i < kStack1.depth; ++i) {
    EXPECT_EQ(GetOffsetInMapping(kStack1.stack[i]), report1.call_stack()[i])
        << i;
  }

  // |report2| comes from the call site marked with kStack2, with size=48.
  const InternalLeakReport& report2 = *(++stored_reports_.begin());
  EXPECT_EQ(48U, report2.alloc_size_bytes());
  ASSERT_EQ(kStack2.depth, report2.call_stack().size());
  for (size_t i = 0; i < kStack2.depth; ++i) {
    EXPECT_EQ(GetOffsetInMapping(kStack2.stack[i]), report2.call_stack()[i])
        << i;
  }

  const auto& report1_history = report1.alloc_breakdown_history();
  EXPECT_EQ(32U, report1_history.size());

  for (size_t i = 1; i < report1_history.size(); ++i) {
    const InternalVector<uint32_t>& counts_by_size =
        report1_history[i].counts_by_size;
    const InternalVector<uint32_t>& prev_counts_by_size =
        report1_history[i - 1].counts_by_size;
    ASSERT_GT(counts_by_size.size(), SizeToIndex(48));

    // Check the two leaky sizes, 32 and 48. At this point, the exact counts
    // could be computed but the computations are too complex for a unit test.
    // Instead, check that the counts increase by 32 from the previous count.
    // Same goes for checking call site counts later.
    EXPECT_GT(counts_by_size[SizeToIndex(32)], 0U);
    EXPECT_GT(counts_by_size[SizeToIndex(48)], 0U);
    EXPECT_EQ(prev_counts_by_size[SizeToIndex(32)] + 32,
              counts_by_size[SizeToIndex(32)]);
    EXPECT_EQ(prev_counts_by_size[SizeToIndex(48)] + 32,
              counts_by_size[SizeToIndex(48)]);

    // Not related to the leaks, but there should be a dangling 16-byte
    // allocation during each leak analysis, because it hasn't yet been freed.
    EXPECT_EQ(1U, counts_by_size[SizeToIndex(16)]);
  }

  // Check call site count over time.
  ASSERT_LT(kSizeSuspicionThreshold, report1_history.size());
  // Sufficient time has passed since the first report was generated. The entire
  // alloc history should contain call site counts.
  for (size_t i = 1; i < report1_history.size(); ++i) {
    EXPECT_GT(report1_history[i].count_for_call_stack, 0U);
    EXPECT_EQ(report1_history[i - 1].count_for_call_stack + 32,
              report1_history[i].count_for_call_stack);
  }

  // |report2| should have the same size history and call stack history as
  // |report1|.
  EXPECT_TRUE(CompareReportAllocHistory(report1, report2));
}

TEST_F(LeakDetectorImplTest, JuliaSetNoLeak) {
  JuliaSet(false /* enable_leaks */);

  // JuliaSet() should have run cleanly without leaking.
  EXPECT_EQ(total_num_allocs_, total_num_frees_);
  EXPECT_EQ(0U, alloced_ptrs_.size());
  EXPECT_EQ(0U, num_reports_generated_);
  ASSERT_EQ(0U, stored_reports_.size());
}

TEST_F(LeakDetectorImplTest, JuliaSetWithLeak) {
  JuliaSet(true /* enable_leaks */);

  // JuliaSet() should have leaked some memory from two call sites.
  EXPECT_GT(total_num_allocs_, total_num_frees_);
  EXPECT_GT(alloced_ptrs_.size(), 0U);
  EXPECT_GT(num_reports_generated_, 0U);

  // There should be one unique leak report generated for each leaky call site.
  ASSERT_EQ(2U, stored_reports_.size());

  // The reports should be stored in order of size.

  // |report1| comes from the call site in JuliaSet() corresponding to
  // |kStack3|.
  const InternalLeakReport& report1 = *stored_reports_.begin();
  EXPECT_EQ(sizeof(Complex) + 40, report1.alloc_size_bytes());
  ASSERT_EQ(kStack3.depth, report1.call_stack().size());
  for (size_t i = 0; i < kStack3.depth; ++i) {
    EXPECT_EQ(GetOffsetInMapping(kStack3.stack[i]),
              report1.call_stack()[i]) << i;
  }

  // |report2| comes from the call site in JuliaSet() corresponding to
  // |kStack4|.
  const InternalLeakReport& report2 = *(++stored_reports_.begin());
  EXPECT_EQ(sizeof(Complex) + 52, report2.alloc_size_bytes());
  ASSERT_EQ(kStack4.depth, report2.call_stack().size());
  for (size_t i = 0; i < kStack4.depth; ++i) {
    EXPECT_EQ(GetOffsetInMapping(kStack4.stack[i]),
              report2.call_stack()[i]) << i;
  }

  // Check |report1|'s historical data.
  const auto& report1_history = report1.alloc_breakdown_history();
  // Computing the exact number of leak analyses is not trivial, but we know it
  // must be at least |kSizeSuspicionThreshold + kCallStackSuspicionThreshold|
  // in order to have generated a report.
  EXPECT_GT(report1_history.size(),
            kSizeSuspicionThreshold + kCallStackSuspicionThreshold);

  // Make sure that the final allocation counts for the leaky sizes are larger
  // than that of the non-leaky size by at least an order of magnitude.
  const AllocationBreakdown& final_entry = *report1_history.rbegin();
  const InternalVector<uint32_t>& counts_by_size = final_entry.counts_by_size;
  uint32_t size_0_index = SizeToIndex(sizeof(Complex) + 24);
  uint32_t size_1_index = SizeToIndex(sizeof(Complex) + 40);
  uint32_t size_2_index = SizeToIndex(sizeof(Complex) + 52);
  ASSERT_LT(size_0_index, counts_by_size.size());
  ASSERT_LT(size_1_index, counts_by_size.size());
  ASSERT_LT(size_2_index, counts_by_size.size());

  EXPECT_GT(counts_by_size[size_1_index], counts_by_size[size_0_index] * 10);
  EXPECT_GT(counts_by_size[size_2_index], counts_by_size[size_0_index] * 10);

  // |report1| and |report2| do not necessarily have the same allocation history
  // due to the different rates at which they were generated.
}

}  // namespace leak_detector
}  // namespace metrics