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

base / sequence_checker_unittest.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/sequence_checker.h"

#include <stddef.h>

#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/sequence_checker_impl.h"
#include "base/sequence_token.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/lock_subtle.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/threading/simple_thread.h"
#include "base/threading/thread_local.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base::internal {

namespace {

// Runs a callback on another thread.
class RunCallbackThread : public SimpleThread {
 public:
  explicit RunCallbackThread(OnceClosure callback)
      : SimpleThread("RunCallbackThread"), callback_(std::move(callback)) {
    Start();
    Join();
  }
  RunCallbackThread(const RunCallbackThread&) = delete;
  RunCallbackThread& operator=(const RunCallbackThread&) = delete;

 private:
  // SimpleThread:
  void Run() override { std::move(callback_).Run(); }

  OnceClosure callback_;
};

void ExpectCalledOnValidSequence(SequenceCheckerImpl* sequence_checker) {
  ASSERT_TRUE(sequence_checker);

  // This should bind |sequence_checker| to the current sequence if it wasn't
  // already bound to a sequence.
  EXPECT_TRUE(sequence_checker->CalledOnValidSequence());

  // Since |sequence_checker| is now bound to the current sequence, another call
  // to CalledOnValidSequence() should return true.
  EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
}

void ExpectNotCalledOnValidSequence(SequenceCheckerImpl* sequence_checker) {
  ASSERT_TRUE(sequence_checker);
  EXPECT_FALSE(sequence_checker->CalledOnValidSequence());
}

}  // namespace

TEST(SequenceCheckerTest, NoTaskScope) {
  SequenceCheckerImpl sequence_checker;
  EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, TaskScope) {
  TaskScope task_scope(SequenceToken::Create(),
                       /* is_thread_bound=*/false);
  SequenceCheckerImpl sequence_checker;
  EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, TaskScopeThreadBound) {
  TaskScope task_scope(SequenceToken::Create(),
                       /* is_thread_bound=*/true);
  SequenceCheckerImpl sequence_checker;
  EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, DifferentThreadNoTaskScope) {
  SequenceCheckerImpl sequence_checker;
  RunCallbackThread thread(
      BindOnce(&ExpectNotCalledOnValidSequence, Unretained(&sequence_checker)));
}

TEST(SequenceCheckerTest, DifferentThreadDifferentSequenceToken) {
  SequenceCheckerImpl sequence_checker;
  RunCallbackThread thread(BindLambdaForTesting([&] {
    TaskScope task_scope(SequenceToken::Create(),
                         /* is_thread_bound=*/false);
    ExpectNotCalledOnValidSequence(&sequence_checker);
  }));
}

TEST(SequenceCheckerTest, DifferentThreadDifferentSequenceTokenThreadBound) {
  SequenceCheckerImpl sequence_checker;
  RunCallbackThread thread(BindLambdaForTesting([&] {
    TaskScope task_scope(SequenceToken::Create(),
                         /* is_thread_bound=*/true);
    ExpectNotCalledOnValidSequence(&sequence_checker);
  }));
}

TEST(SequenceCheckerTest, DifferentThreadSameSequenceToken) {
  const SequenceToken token = SequenceToken::Create();
  TaskScope task_scope(token, /* is_thread_bound=*/false);
  SequenceCheckerImpl sequence_checker;
  RunCallbackThread thread(BindLambdaForTesting([&] {
    TaskScope task_scope(token, /* is_thread_bound=*/false);
    ExpectCalledOnValidSequence(&sequence_checker);
  }));
}

TEST(SequenceCheckerTest, DifferentThreadSameSequenceTokenThreadBound) {
  // Note: A callback running synchronously in `RunOrPostTask()` may have a
  // non-thread-bound `TaskScope` associated with the same `SequenceToken` as
  // another thread-bound `TaskScope`. This test recreates this case.
  const SequenceToken token = SequenceToken::Create();
  TaskScope task_scope(token, /* is_thread_bound=*/true);
  SequenceCheckerImpl sequence_checker;
  RunCallbackThread thread(BindLambdaForTesting([&] {
    TaskScope task_scope(token, /* is_thread_bound=*/false);
    ExpectCalledOnValidSequence(&sequence_checker);
  }));
}

TEST(SequenceCheckerTest, SameThreadDifferentSequenceToken) {
  std::unique_ptr<SequenceCheckerImpl> sequence_checker;

  {
    TaskScope task_scope(SequenceToken::Create(),
                         /* is_thread_bound=*/false);
    sequence_checker = std::make_unique<SequenceCheckerImpl>();
  }

  {
    // Different SequenceToken.
    TaskScope task_scope(SequenceToken::Create(),
                         /* is_thread_bound=*/false);
    EXPECT_FALSE(sequence_checker->CalledOnValidSequence());
  }

  // No explicit SequenceToken.
  EXPECT_FALSE(sequence_checker->CalledOnValidSequence());
}

TEST(SequenceCheckerTest, DetachFromSequence) {
  std::unique_ptr<SequenceCheckerImpl> sequence_checker;

  {
    TaskScope task_scope(SequenceToken::Create(),
                         /* is_thread_bound=*/false);
    sequence_checker = std::make_unique<SequenceCheckerImpl>();
  }

  sequence_checker->DetachFromSequence();

  {
    // Verify that CalledOnValidSequence() returns true when called with
    // a different sequence token after a call to DetachFromSequence().
    TaskScope task_scope(SequenceToken::Create(),
                         /* is_thread_bound=*/false);
    EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
  }
}

TEST(SequenceCheckerTest, DetachFromSequenceNoSequenceToken) {
  SequenceCheckerImpl sequence_checker;
  sequence_checker.DetachFromSequence();

  // Verify that CalledOnValidSequence() returns true when called on a
  // different thread after a call to DetachFromSequence().
  RunCallbackThread thread(
      BindOnce(&ExpectCalledOnValidSequence, Unretained(&sequence_checker)));

  EXPECT_FALSE(sequence_checker.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, Move) {
  SequenceCheckerImpl initial;
  EXPECT_TRUE(initial.CalledOnValidSequence());

  SequenceCheckerImpl move_constructed(std::move(initial));
  EXPECT_TRUE(move_constructed.CalledOnValidSequence());

  SequenceCheckerImpl move_assigned;
  move_assigned = std::move(move_constructed);

  // The two SequenceCheckerImpls moved from should be able to rebind to another
  // sequence.
  RunCallbackThread thread1(
      BindOnce(&ExpectCalledOnValidSequence, Unretained(&initial)));
  RunCallbackThread thread2(
      BindOnce(&ExpectCalledOnValidSequence, Unretained(&move_constructed)));

  // But the latest one shouldn't be able to run on another sequence.
  RunCallbackThread thread(
      BindOnce(&ExpectNotCalledOnValidSequence, Unretained(&move_assigned)));

  EXPECT_TRUE(move_assigned.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, MoveAssignIntoDetached) {
  SequenceCheckerImpl initial;

  SequenceCheckerImpl move_assigned;
  move_assigned.DetachFromSequence();
  move_assigned = std::move(initial);

  // |initial| is detached after move.
  RunCallbackThread thread1(
      BindOnce(&ExpectCalledOnValidSequence, Unretained(&initial)));

  // |move_assigned| should be associated with the main thread.
  RunCallbackThread thread2(
      BindOnce(&ExpectNotCalledOnValidSequence, Unretained(&move_assigned)));

  EXPECT_TRUE(move_assigned.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, MoveFromDetachedRebinds) {
  SequenceCheckerImpl initial;
  initial.DetachFromSequence();

  SequenceCheckerImpl moved_into(std::move(initial));

  // |initial| is still detached after move.
  RunCallbackThread thread1(
      BindOnce(&ExpectCalledOnValidSequence, Unretained(&initial)));

  // |moved_into| is bound to the current sequence as part of the move.
  RunCallbackThread thread2(
      BindOnce(&ExpectNotCalledOnValidSequence, Unretained(&moved_into)));
  EXPECT_TRUE(moved_into.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, MoveOffSequenceBanned) {
  GTEST_FLAG_SET(death_test_style, "threadsafe");

  SequenceCheckerImpl other_sequence;
  other_sequence.DetachFromSequence();
  RunCallbackThread thread(
      BindOnce(&ExpectCalledOnValidSequence, Unretained(&other_sequence)));

  EXPECT_DCHECK_DEATH(
      SequenceCheckerImpl main_sequence(std::move(other_sequence)));
}

TEST(SequenceCheckerMacroTest, Macros) {
  auto scope = std::make_unique<TaskScope>(SequenceToken::Create(),
                                           /* is_thread_bound=*/false);
  SEQUENCE_CHECKER(my_sequence_checker);

  {
    // Don't expect a DCHECK death when a SequenceChecker is used on the right
    // sequence.
    DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker);
  }
  scope.reset();

#if DCHECK_IS_ON()
  // Expect DCHECK death when used on a different sequence.
  EXPECT_DCHECK_DEATH(
      { DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker); });
#else
    // Happily no-ops on non-dcheck builds.
  DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker);
#endif

  DETACH_FROM_SEQUENCE(my_sequence_checker);

  // Don't expect a DCHECK death when a SequenceChecker is used for the first
  // time after having been detached.
  DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker);
}

// Owns a SequenceCheckerImpl, and asserts that CalledOnValidSequence() is valid
// in ~SequenceCheckerOwner.
class SequenceCheckerOwner {
 public:
  explicit SequenceCheckerOwner(SequenceCheckerImpl* other_checker)
      : other_checker_(other_checker) {}
  SequenceCheckerOwner(const SequenceCheckerOwner&) = delete;
  SequenceCheckerOwner& operator=(const SequenceCheckerOwner&) = delete;
  ~SequenceCheckerOwner() {
    // Check passes on TLS destruction.
    EXPECT_TRUE(checker_.CalledOnValidSequence());

    // Check also passes on TLS destruction after move assignment.
    *other_checker_ = std::move(checker_);
    EXPECT_TRUE(other_checker_->CalledOnValidSequence());
  }

 private:
  SequenceCheckerImpl checker_;
  raw_ptr<SequenceCheckerImpl> other_checker_;
};

// Verifies SequenceCheckerImpl::CalledOnValidSequence() returns true if called
// during thread destruction.
TEST(SequenceCheckerTest, FromThreadDestruction) {
  SequenceChecker::EnableStackLogging();

  SequenceCheckerImpl other_checker;
  ThreadLocalOwnedPointer<SequenceCheckerOwner> thread_local_owner;
  {
    test::TaskEnvironment task_environment;
    auto task_runner = ThreadPool::CreateSequencedTaskRunner({});
    task_runner->PostTask(
        FROM_HERE, BindLambdaForTesting([&] {
          thread_local_owner.Set(
              std::make_unique<SequenceCheckerOwner>(&other_checker));
        }));
    task_runner = nullptr;
    task_environment.RunUntilIdle();
  }
}

// Verifies sequence checking while holding the same locks from different
// sequences.
//
// Note: This is only supported in DCHECK builds.
#if DCHECK_IS_ON()
TEST(SequenceCheckerTest, LockBasic) {
  test::TaskEnvironment task_environment;
  WaitableEvent thread_pool_done;
  Lock lock;

  // Create sequence checker while holding lock.
  ReleasableAutoLock releasable_auto_lock(&lock,
                                          subtle::LockTracking::kEnabled);
  SequenceCheckerImpl sequence_checker;
  releasable_auto_lock.Release();

  ThreadPool::PostTask(BindLambdaForTesting([&] {
    // Check sequencing while holding the lock.
    {
      AutoLock auto_lock(lock, subtle::LockTracking::kEnabled);
      EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
    }

    thread_pool_done.Signal();
  }));

  thread_pool_done.Wait();

  // Check sequencing from the creation sequence, without holding the lock.
  // Sequencing is *not* valid because sequencing now depends on the lock.
  EXPECT_FALSE(sequence_checker.CalledOnValidSequence());

  // Check sequencing from the creation sequence while holding the lock.
  {
    AutoLock auto_lock(lock, subtle::LockTracking::kEnabled);
    EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
  }
}

TEST(SequenceCheckerTest, ManyLocks) {
  test::TaskEnvironment task_environment;
  WaitableEvent thread_pool_done;
  Lock lock_a;
  Lock lock_b;
  Lock lock_c;

  ReleasableAutoLock releasable_auto_lock_a(&lock_a,
                                            subtle::LockTracking::kEnabled);
  ReleasableAutoLock releasable_auto_lock_b(&lock_b,
                                            subtle::LockTracking::kEnabled);
  ReleasableAutoLock releasable_auto_lock_c(&lock_c,
                                            subtle::LockTracking::kEnabled);
  SequenceCheckerImpl sequence_checker;
  releasable_auto_lock_c.Release();
  releasable_auto_lock_b.Release();
  releasable_auto_lock_a.Release();

  ThreadPool::PostTask(BindLambdaForTesting([&] {
    {
      AutoLock auto_lock_a(lock_a, subtle::LockTracking::kEnabled);
      AutoLock auto_lock_b(lock_b, subtle::LockTracking::kEnabled);
      AutoLock auto_lock_c(lock_c, subtle::LockTracking::kEnabled);
      EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
    }

    {
      AutoLock auto_lock_a(lock_a, subtle::LockTracking::kEnabled);
      AutoLock auto_lock_b(lock_b, subtle::LockTracking::kEnabled);
      EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
    }

    {
      AutoLock auto_lock_c(lock_c, subtle::LockTracking::kEnabled);
      EXPECT_FALSE(sequence_checker.CalledOnValidSequence());
    }

    {
      AutoLock auto_lock_b(lock_b, subtle::LockTracking::kEnabled);
      EXPECT_TRUE(sequence_checker.CalledOnValidSequence());
    }

    {
      AutoLock auto_lock_b(lock_a, subtle::LockTracking::kEnabled);
      EXPECT_FALSE(sequence_checker.CalledOnValidSequence());
    }

    thread_pool_done.Signal();
  }));

  thread_pool_done.Wait();

  EXPECT_FALSE(sequence_checker.CalledOnValidSequence());
}

TEST(SequenceCheckerTest, LockAndSequence) {
  test::TaskEnvironment task_environment;
  WaitableEvent thread_pool_done;
  Lock lock;

  // Create sequence checker while holding lock.
  ReleasableAutoLock releasable_auto_lock(&lock,
                                          subtle::LockTracking::kEnabled);
  SequenceCheckerImpl sequence_checker;
  releasable_auto_lock.Release();

  // Check sequencing without holding the lock.
  EXPECT_TRUE(sequence_checker.CalledOnValidSequence());

  ThreadPool::PostTask(BindLambdaForTesting([&] {
    // Check sequencing while holding the lock. This is not valid because
    // `CalledOnValidSequence()` previously returned true while the lock wasn't
    // held.
    {
      AutoLock auto_lock(lock, subtle::LockTracking::kEnabled);
      EXPECT_FALSE(sequence_checker.CalledOnValidSequence());
    }

    thread_pool_done.Signal();
  }));

  thread_pool_done.Wait();
}

TEST(SequenceCheckerTest, LockDetachFromSequence) {
  test::TaskEnvironment task_environment;
  WaitableEvent thread_pool_done;
  Lock lock;

  // Create sequence checker and detach while holding lock.
  ReleasableAutoLock releasable_auto_lock(&lock,
                                          subtle::LockTracking::kEnabled);
  SequenceCheckerImpl sequence_checker;
  sequence_checker.DetachFromSequence();
  releasable_auto_lock.Release();

  // Re-bind without holding the lock.
  EXPECT_TRUE(sequence_checker.CalledOnValidSequence());

  ThreadPool::PostTask(BindLambdaForTesting([&] {
    // Check sequencing while holding the lock. This is not valid because the
    // sequence checker was detached and re-bound without the lock.
    {
      AutoLock auto_lock(lock, subtle::LockTracking::kEnabled);
      EXPECT_FALSE(sequence_checker.CalledOnValidSequence());
    }

    thread_pool_done.Signal();
  }));

  thread_pool_done.Wait();
}

TEST(SequenceCheckerTest, LockMoveConstruction) {
  test::TaskEnvironment task_environment;
  WaitableEvent thread_pool_done;
  Lock lock;

  // Create sequence checker and move-construct while holding a lock.
  ReleasableAutoLock releasable_auto_lock(&lock,
                                          subtle::LockTracking::kEnabled);
  SequenceCheckerImpl sequence_checker;
  SequenceCheckerImpl other_sequence_checker(std::move(sequence_checker));
  releasable_auto_lock.Release();

  ThreadPool::PostTask(BindLambdaForTesting([&] {
    // Check sequencing while holding the lock.
    {
      AutoLock auto_lock(lock, subtle::LockTracking::kEnabled);
      EXPECT_TRUE(other_sequence_checker.CalledOnValidSequence());
    }

    thread_pool_done.Signal();
  }));

  thread_pool_done.Wait();
}

TEST(SequenceCheckerTest, LockMoveAssignment) {
  test::TaskEnvironment task_environment;
  WaitableEvent thread_pool_done;
  Lock lock;

  SequenceCheckerImpl other_sequence_checker;

  // Create sequence checker and move-assign it to `other_sequence_checker`
  // while holding a lock.
  ReleasableAutoLock releasable_auto_lock(&lock,
                                          subtle::LockTracking::kEnabled);
  SequenceCheckerImpl sequence_checker;
  other_sequence_checker = std::move(sequence_checker);
  releasable_auto_lock.Release();

  ThreadPool::PostTask(BindLambdaForTesting([&] {
    // Check sequencing while holding the lock.
    {
      AutoLock auto_lock(lock, subtle::LockTracking::kEnabled);
      EXPECT_TRUE(other_sequence_checker.CalledOnValidSequence());
    }

    thread_pool_done.Signal();
  }));

  thread_pool_done.Wait();
}
#endif  // DCHECK_IS_ON()

}  // namespace base::internal