summaryrefslogtreecommitdiff
path: root/libunwindstack/tests/UnwindTest.cpp
blob: 1996992128613d094884e9cffbf50737a504432f (plain)
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <gtest/gtest.h>

#include <atomic>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#include <vector>

#include <android-base/stringprintf.h>
#include <android-base/threads.h>

#include <unwindstack/Maps.h>
#include <unwindstack/Regs.h>
#include <unwindstack/RegsGetLocal.h>
#include <unwindstack/Unwinder.h>

#include "MemoryRemote.h"
#include "TestUtils.h"

namespace unwindstack {

enum TestTypeEnum : uint8_t {
  TEST_TYPE_LOCAL_UNWINDER = 0,
  TEST_TYPE_LOCAL_UNWINDER_FROM_PID,
  TEST_TYPE_LOCAL_WAIT_FOR_FINISH,
  TEST_TYPE_REMOTE,
  TEST_TYPE_REMOTE_WITH_INVALID_CALL,
};

static volatile bool g_ready_for_remote;
static volatile bool g_signal_ready_for_remote;
// In order to avoid the compiler not emitting the unwind entries for
// the InnerFunction code that loops waiting for g_finish, always make
// g_finish a volatile instead of an atomic. This issue was only ever
// observerd on the arm architecture.
static volatile bool g_finish;
static std::atomic_uintptr_t g_ucontext;
static std::atomic_int g_waiters;

static void ResetGlobals() {
  g_ready_for_remote = false;
  g_signal_ready_for_remote = false;
  g_finish = false;
  g_ucontext = 0;
  g_waiters = 0;
}

static std::vector<const char*> kFunctionOrder{"OuterFunction", "MiddleFunction", "InnerFunction"};

static std::vector<const char*> kFunctionSignalOrder{"OuterFunction",        "MiddleFunction",
                                                     "InnerFunction",        "SignalOuterFunction",
                                                     "SignalMiddleFunction", "SignalInnerFunction"};

static void SignalHandler(int, siginfo_t*, void* sigcontext) {
  g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
  while (!g_finish) {
  }
}

extern "C" void SignalInnerFunction() {
  g_signal_ready_for_remote = true;
  // Avoid any function calls because not every instruction will be
  // unwindable.
  // This method of looping is only used when testing a remote unwind.
  while (true) {
  }
}

extern "C" void SignalMiddleFunction() {
  SignalInnerFunction();
}

extern "C" void SignalOuterFunction() {
  SignalMiddleFunction();
}

static void SignalCallerHandler(int, siginfo_t*, void*) {
  SignalOuterFunction();
}

static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder* unwinder) {
  std::string unwind;
  for (size_t i = 0; i < unwinder->NumFrames(); i++) {
    unwind += unwinder->FormatFrame(i) + '\n';
  }

  return std::string(
             "Unwind completed without finding all frames\n"
             "  Unwinder error: ") +
         unwinder->LastErrorCodeString() + "\n" +
         "  Looking for function: " + function_names.front() + "\n" + "Unwind data:\n" + unwind;
}

static void VerifyUnwindFrames(Unwinder* unwinder,
                               std::vector<const char*> expected_function_names) {
  for (auto& frame : unwinder->frames()) {
    if (frame.function_name == expected_function_names.back()) {
      expected_function_names.pop_back();
      if (expected_function_names.empty()) {
        break;
      }
    }
  }

  ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder);

  // Verify that the load bias of every map with a MapInfo is has been initialized.
  for (auto& frame : unwinder->frames()) {
    if (frame.map_info == nullptr) {
      continue;
    }
    ASSERT_NE(UINT64_MAX, frame.map_info->GetLoadBias()) << "Frame " << frame.num << " failed";
  }
}

static void VerifyUnwind(Unwinder* unwinder, std::vector<const char*> expected_function_names) {
  unwinder->Unwind();

  VerifyUnwindFrames(unwinder, expected_function_names);
}

static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
                         std::vector<const char*> expected_function_names) {
  auto process_memory(Memory::CreateProcessMemory(pid));

  Unwinder unwinder(512, maps, regs, process_memory);
  VerifyUnwind(&unwinder, expected_function_names);
}

// This test assumes that this code is compiled with optimizations turned
// off. If this doesn't happen, then all of the calls will be optimized
// away.
extern "C" void InnerFunction(TestTypeEnum test_type) {
  // Use a switch statement to force the compiler to create unwinding information
  // for each case.
  switch (test_type) {
    case TEST_TYPE_LOCAL_WAIT_FOR_FINISH: {
      g_waiters++;
      while (!g_finish) {
      }
      break;
    }

    case TEST_TYPE_REMOTE:
    case TEST_TYPE_REMOTE_WITH_INVALID_CALL: {
      g_ready_for_remote = true;
      if (test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
        void (*crash_func)() = nullptr;
        crash_func();
      }
      while (true) {
      }
      break;
    }

    default: {
      std::unique_ptr<Unwinder> unwinder;
      std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
      RegsGetLocal(regs.get());
      std::unique_ptr<Maps> maps;

      if (test_type == TEST_TYPE_LOCAL_UNWINDER) {
        maps.reset(new LocalMaps());
        ASSERT_TRUE(maps->Parse());
        auto process_memory(Memory::CreateProcessMemory(getpid()));
        unwinder.reset(new Unwinder(512, maps.get(), regs.get(), process_memory));
      } else {
        UnwinderFromPid* unwinder_from_pid = new UnwinderFromPid(512, getpid());
        unwinder_from_pid->SetRegs(regs.get());
        unwinder.reset(unwinder_from_pid);
      }
      VerifyUnwind(unwinder.get(), kFunctionOrder);
      break;
    }
  }
}

extern "C" void MiddleFunction(TestTypeEnum test_type) {
  InnerFunction(test_type);
}

extern "C" void OuterFunction(TestTypeEnum test_type) {
  MiddleFunction(test_type);
}

class UnwindTest : public ::testing::Test {
 public:
  void SetUp() override { ResetGlobals(); }
};

TEST_F(UnwindTest, local) {
  OuterFunction(TEST_TYPE_LOCAL_UNWINDER);
}

TEST_F(UnwindTest, local_use_from_pid) {
  OuterFunction(TEST_TYPE_LOCAL_UNWINDER_FROM_PID);
}

static void LocalUnwind(void* data) {
  TestTypeEnum* test_type = reinterpret_cast<TestTypeEnum*>(data);
  OuterFunction(*test_type);
}

TEST_F(UnwindTest, local_check_for_leak) {
  TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER;
  TestCheckForLeaks(LocalUnwind, &test_type);
}

TEST_F(UnwindTest, local_use_from_pid_check_for_leak) {
  TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER_FROM_PID;
  TestCheckForLeaks(LocalUnwind, &test_type);
}

void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
  *completed = false;
  // Need to sleep before attempting first ptrace. Without this, on the
  // host it becomes impossible to attach and ptrace sets errno to EPERM.
  usleep(1000);
  for (size_t i = 0; i < 4000; i++) {
    ASSERT_TRUE(TestAttach(pid));

    MemoryRemote memory(pid);
    // Read the remote value to see if we are ready.
    bool value;
    if (memory.ReadFully(addr, &value, sizeof(value)) && value) {
      *completed = true;
    }
    if (!*completed || !leave_attached) {
      ASSERT_TRUE(TestDetach(pid));
    }
    if (*completed) {
      break;
    }
    usleep(5000);
  }
}

TEST_F(UnwindTest, remote) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    OuterFunction(TEST_TYPE_REMOTE);
    exit(0);
  }
  ASSERT_NE(-1, pid);
  TestScopedPidReaper reap(pid);

  bool completed;
  WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
  ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";

  RemoteMaps maps(pid);
  ASSERT_TRUE(maps.Parse());
  std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
  ASSERT_TRUE(regs.get() != nullptr);

  VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);

  ASSERT_TRUE(TestDetach(pid));
}

TEST_F(UnwindTest, unwind_from_pid_remote) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    OuterFunction(TEST_TYPE_REMOTE);
    exit(0);
  }
  ASSERT_NE(-1, pid);
  TestScopedPidReaper reap(pid);

  bool completed;
  WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
  ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";

  std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
  ASSERT_TRUE(regs.get() != nullptr);

  UnwinderFromPid unwinder(512, pid);
  unwinder.SetRegs(regs.get());

  VerifyUnwind(&unwinder, kFunctionOrder);

  ASSERT_TRUE(TestDetach(pid));
}

static void RemoteCheckForLeaks(void (*unwind_func)(void*)) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    OuterFunction(TEST_TYPE_REMOTE);
    exit(0);
  }
  ASSERT_NE(-1, pid);
  TestScopedPidReaper reap(pid);

  bool completed;
  WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
  ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";

  TestCheckForLeaks(unwind_func, &pid);

  ASSERT_TRUE(TestDetach(pid));
}

static void RemoteUnwind(void* data) {
  pid_t* pid = reinterpret_cast<pid_t*>(data);

  RemoteMaps maps(*pid);
  ASSERT_TRUE(maps.Parse());
  std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid));
  ASSERT_TRUE(regs.get() != nullptr);

  VerifyUnwind(*pid, &maps, regs.get(), kFunctionOrder);
}

TEST_F(UnwindTest, remote_check_for_leaks) {
  RemoteCheckForLeaks(RemoteUnwind);
}

static void RemoteUnwindFromPid(void* data) {
  pid_t* pid = reinterpret_cast<pid_t*>(data);

  std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid));
  ASSERT_TRUE(regs.get() != nullptr);

  UnwinderFromPid unwinder(512, *pid);
  unwinder.SetRegs(regs.get());

  VerifyUnwind(&unwinder, kFunctionOrder);
}

TEST_F(UnwindTest, remote_unwind_for_pid_check_for_leaks) {
  RemoteCheckForLeaks(RemoteUnwindFromPid);
}

TEST_F(UnwindTest, from_context) {
  std::atomic_int tid(0);
  std::thread thread([&]() {
    tid = syscall(__NR_gettid);
    OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
  });

  struct sigaction act, oldact;
  memset(&act, 0, sizeof(act));
  act.sa_sigaction = SignalHandler;
  act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
  ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
  // Wait for the tid to get set.
  for (size_t i = 0; i < 100; i++) {
    if (tid.load() != 0) {
      break;
    }
    usleep(1000);
  }
  ASSERT_NE(0, tid.load());
  ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);

  // Wait for context data.
  void* ucontext;
  for (size_t i = 0; i < 2000; i++) {
    ucontext = reinterpret_cast<void*>(g_ucontext.load());
    if (ucontext != nullptr) {
      break;
    }
    usleep(1000);
  }
  ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";

  LocalMaps maps;
  ASSERT_TRUE(maps.Parse());
  std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));

  VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);

  ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));

  g_finish = true;
  thread.join();
}

static void RemoteThroughSignal(int signal, unsigned int sa_flags) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    struct sigaction act, oldact;
    memset(&act, 0, sizeof(act));
    act.sa_sigaction = SignalCallerHandler;
    act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
    ASSERT_EQ(0, sigaction(signal, &act, &oldact));

    OuterFunction(signal != SIGSEGV ? TEST_TYPE_REMOTE : TEST_TYPE_REMOTE_WITH_INVALID_CALL);
    exit(0);
  }
  ASSERT_NE(-1, pid);
  TestScopedPidReaper reap(pid);

  bool completed;
  if (signal != SIGSEGV) {
    WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
    ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
    ASSERT_EQ(0, kill(pid, SIGUSR1));
  }
  WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
  ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";

  RemoteMaps maps(pid);
  ASSERT_TRUE(maps.Parse());
  std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
  ASSERT_TRUE(regs.get() != nullptr);

  VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);

  ASSERT_TRUE(TestDetach(pid));
}

TEST_F(UnwindTest, remote_through_signal) {
  RemoteThroughSignal(SIGUSR1, 0);
}

TEST_F(UnwindTest, remote_through_signal_sa_siginfo) {
  RemoteThroughSignal(SIGUSR1, SA_SIGINFO);
}

TEST_F(UnwindTest, remote_through_signal_with_invalid_func) {
  RemoteThroughSignal(SIGSEGV, 0);
}

TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) {
  RemoteThroughSignal(SIGSEGV, SA_SIGINFO);
}

// Verify that using the same map while unwinding multiple threads at the
// same time doesn't cause problems.
TEST_F(UnwindTest, multiple_threads_unwind_same_map) {
  static constexpr size_t kNumConcurrentThreads = 100;

  LocalMaps maps;
  ASSERT_TRUE(maps.Parse());
  auto process_memory(Memory::CreateProcessMemory(getpid()));

  std::vector<std::thread*> threads;

  std::atomic_bool wait;
  wait = true;
  size_t frames[kNumConcurrentThreads];
  for (size_t i = 0; i < kNumConcurrentThreads; i++) {
    std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() {
      while (wait)
        ;
      std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
      RegsGetLocal(regs.get());

      Unwinder unwinder(512, &maps, regs.get(), process_memory);
      unwinder.Unwind();
      frames[i] = unwinder.NumFrames();
      ASSERT_LE(3U, frames[i]) << "Failed for thread " << i;
    });
    threads.push_back(thread);
  }
  wait = false;
  for (auto thread : threads) {
    thread->join();
    delete thread;
  }
}

TEST_F(UnwindTest, thread_unwind) {
  ResetGlobals();

  std::atomic_int tid(0);
  std::thread thread([&tid]() {
    tid = android::base::GetThreadId();
    OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
  });

  while (tid.load() == 0)
    ;

  ThreadUnwinder unwinder(512);
  ASSERT_TRUE(unwinder.Init());
  unwinder.UnwindWithSignal(SIGRTMIN, tid);
  VerifyUnwindFrames(&unwinder, kFunctionOrder);

  g_finish = true;
  thread.join();
}

TEST_F(UnwindTest, thread_unwind_with_external_maps) {
  ResetGlobals();

  std::atomic_int tid(0);
  std::thread thread([&tid]() {
    tid = android::base::GetThreadId();
    OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
  });

  while (tid.load() == 0)
    ;

  LocalMaps maps;
  ASSERT_TRUE(maps.Parse());

  ThreadUnwinder unwinder(512, &maps);
  ASSERT_EQ(&maps, unwinder.GetMaps());
  ASSERT_TRUE(unwinder.Init());
  ASSERT_EQ(&maps, unwinder.GetMaps());
  unwinder.UnwindWithSignal(SIGRTMIN, tid);
  VerifyUnwindFrames(&unwinder, kFunctionOrder);
  ASSERT_EQ(&maps, unwinder.GetMaps());

  g_finish = true;
  thread.join();
}

TEST_F(UnwindTest, thread_unwind_cur_pid) {
  ThreadUnwinder unwinder(512);
  ASSERT_TRUE(unwinder.Init());
  unwinder.UnwindWithSignal(SIGRTMIN, getpid());
  EXPECT_EQ(0U, unwinder.NumFrames());
  EXPECT_EQ(ERROR_UNSUPPORTED, unwinder.LastErrorCode());
}

static std::thread* CreateUnwindThread(std::atomic_int& tid, ThreadUnwinder& unwinder,
                                       std::atomic_bool& start_unwinding,
                                       std::atomic_int& unwinders) {
  return new std::thread([&tid, &unwinder, &start_unwinding, &unwinders]() {
    while (!start_unwinding.load())
      ;

    ThreadUnwinder thread_unwinder(512, &unwinder);
    // Allow the unwind to timeout since this will be doing multiple
    // unwinds at once.
    for (size_t i = 0; i < 3; i++) {
      thread_unwinder.UnwindWithSignal(SIGRTMIN, tid);
      if (thread_unwinder.LastErrorCode() != ERROR_THREAD_TIMEOUT) {
        break;
      }
    }
    VerifyUnwindFrames(&thread_unwinder, kFunctionOrder);
    ++unwinders;
  });
}

TEST_F(UnwindTest, thread_unwind_same_thread_from_threads) {
  static constexpr size_t kNumThreads = 300;
  ResetGlobals();

  std::atomic_int tid(0);
  std::thread thread([&tid]() {
    tid = android::base::GetThreadId();
    OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
  });

  while (g_waiters.load() != 1)
    ;

  ThreadUnwinder unwinder(512);
  ASSERT_TRUE(unwinder.Init());

  std::atomic_bool start_unwinding(false);
  std::vector<std::thread*> threads;
  std::atomic_int unwinders(0);
  for (size_t i = 0; i < kNumThreads; i++) {
    threads.push_back(CreateUnwindThread(tid, unwinder, start_unwinding, unwinders));
  }

  start_unwinding = true;
  while (unwinders.load() != kNumThreads)
    ;

  for (auto* thread : threads) {
    thread->join();
    delete thread;
  }

  g_finish = true;
  thread.join();
}

TEST_F(UnwindTest, thread_unwind_multiple_thread_from_threads) {
  static constexpr size_t kNumThreads = 100;
  ResetGlobals();

  std::atomic_int tids[kNumThreads] = {};
  std::vector<std::thread*> threads;
  for (size_t i = 0; i < kNumThreads; i++) {
    std::thread* thread = new std::thread([&tids, i]() {
      tids[i] = android::base::GetThreadId();
      OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
    });
    threads.push_back(thread);
  }

  while (g_waiters.load() != kNumThreads)
    ;

  ThreadUnwinder unwinder(512);
  ASSERT_TRUE(unwinder.Init());

  std::atomic_bool start_unwinding(false);
  std::vector<std::thread*> unwinder_threads;
  std::atomic_int unwinders(0);
  for (size_t i = 0; i < kNumThreads; i++) {
    unwinder_threads.push_back(CreateUnwindThread(tids[i], unwinder, start_unwinding, unwinders));
  }

  start_unwinding = true;
  while (unwinders.load() != kNumThreads)
    ;

  for (auto* thread : unwinder_threads) {
    thread->join();
    delete thread;
  }

  g_finish = true;

  for (auto* thread : threads) {
    thread->join();
    delete thread;
  }
}

TEST_F(UnwindTest, thread_unwind_multiple_thread_from_threads_updatable_maps) {
  static constexpr size_t kNumThreads = 100;
  ResetGlobals();

  // Do this before the threads are started so that the maps needed to
  // unwind are not created yet, and this verifies the dynamic nature
  // of the LocalUpdatableMaps object.
  LocalUpdatableMaps maps;
  ASSERT_TRUE(maps.Parse());

  std::atomic_int tids[kNumThreads] = {};
  std::vector<std::thread*> threads;
  for (size_t i = 0; i < kNumThreads; i++) {
    std::thread* thread = new std::thread([&tids, i]() {
      tids[i] = android::base::GetThreadId();
      OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
    });
    threads.push_back(thread);
  }

  while (g_waiters.load() != kNumThreads)
    ;

  ThreadUnwinder unwinder(512, &maps);
  ASSERT_TRUE(unwinder.Init());

  std::atomic_bool start_unwinding(false);
  std::vector<std::thread*> unwinder_threads;
  std::atomic_int unwinders(0);
  for (size_t i = 0; i < kNumThreads; i++) {
    unwinder_threads.push_back(CreateUnwindThread(tids[i], unwinder, start_unwinding, unwinders));
  }

  start_unwinding = true;
  while (unwinders.load() != kNumThreads)
    ;

  for (auto* thread : unwinder_threads) {
    thread->join();
    delete thread;
  }

  g_finish = true;

  for (auto* thread : threads) {
    thread->join();
    delete thread;
  }
}

}  // namespace unwindstack