File: LocateModuleCallbackTest.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (898 lines) | stat: -rw-r--r-- 37,982 bytes parent folder | download | duplicates (9)
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//===-- LocateModuleCallbackTest.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
#include "Plugins/Platform/Android/PlatformAndroid.h"
#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Target/Target.h"
#include "gmock/gmock.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::platform_android;
using namespace lldb_private::platform_linux;
using namespace lldb_private::breakpad;
using namespace testing;

namespace {

constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
constexpr llvm::StringLiteral k_platform_dir("remote-android");
constexpr llvm::StringLiteral k_cache_dir(".cache");
constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
constexpr llvm::StringLiteral
    k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
constexpr llvm::StringLiteral k_function_symbol("boom");
constexpr llvm::StringLiteral k_hidden_function_symbol("boom_hidden");
const size_t k_module_size = 3784;

ModuleSpec GetTestModuleSpec();

class MockProcess : public Process {
public:
  MockProcess(TargetSP target_sp, ListenerSP listener_sp)
      : Process(target_sp, listener_sp) {}

  llvm::StringRef GetPluginName() override { return k_process_plugin; };

  bool CanDebug(TargetSP target, bool plugin_specified_by_name) override {
    return true;
  }

  Status DoDestroy() override { return Status(); }

  void RefreshStateAfterStop() override {}

  bool DoUpdateThreadList(ThreadList &old_thread_list,
                          ThreadList &new_thread_list) override {
    return false;
  }

  size_t DoReadMemory(addr_t vm_addr, void *buf, size_t size,
                      Status &error) override {
    return 0;
  }

  bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,
                     ModuleSpec &module_spec) override {
    module_spec = GetTestModuleSpec();
    return true;
  }
};

FileSpec GetTestDir() {
  const auto *info = UnitTest::GetInstance()->current_test_info();
  FileSpec test_dir = HostInfo::GetProcessTempDir();
  test_dir.AppendPathComponent(std::string(info->test_case_name()) + "-" +
                               info->name());
  std::error_code ec = llvm::sys::fs::create_directory(test_dir.GetPath());
  EXPECT_FALSE(ec);
  return test_dir;
}

FileSpec GetRemotePath() {
  FileSpec fs("/", FileSpec::Style::posix);
  fs.AppendPathComponent("bin");
  fs.AppendPathComponent(k_module_file);
  return fs;
}

FileSpec GetUuidView(FileSpec spec) {
  spec.AppendPathComponent(k_platform_dir);
  spec.AppendPathComponent(k_cache_dir);
  spec.AppendPathComponent(k_module_uuid);
  spec.AppendPathComponent(k_module_file);
  return spec;
}

void BuildEmptyCacheDir(const FileSpec &test_dir) {
  FileSpec cache_dir(test_dir);
  cache_dir.AppendPathComponent(k_platform_dir);
  cache_dir.AppendPathComponent(k_cache_dir);
  std::error_code ec = llvm::sys::fs::create_directories(cache_dir.GetPath());
  EXPECT_FALSE(ec);
}

FileSpec BuildCacheDir(const FileSpec &test_dir) {
  FileSpec uuid_view = GetUuidView(test_dir);
  std::error_code ec =
      llvm::sys::fs::create_directories(uuid_view.GetDirectory().GetCString());
  EXPECT_FALSE(ec);
  ec = llvm::sys::fs::copy_file(GetInputFilePath(k_module_file),
                                uuid_view.GetPath().c_str());
  EXPECT_FALSE(ec);
  return uuid_view;
}

FileSpec GetSymFileSpec(const FileSpec &uuid_view) {
  return FileSpec(uuid_view.GetPath() + ".sym");
}

FileSpec BuildCacheDirWithSymbol(const FileSpec &test_dir) {
  FileSpec uuid_view = BuildCacheDir(test_dir);
  std::error_code ec =
      llvm::sys::fs::copy_file(GetInputFilePath(k_symbol_file),
                               GetSymFileSpec(uuid_view).GetPath().c_str());
  EXPECT_FALSE(ec);
  return uuid_view;
}

FileSpec BuildCacheDirWithBreakpadSymbol(const FileSpec &test_dir) {
  FileSpec uuid_view = BuildCacheDir(test_dir);
  std::error_code ec =
      llvm::sys::fs::copy_file(GetInputFilePath(k_breakpad_symbol_file),
                               GetSymFileSpec(uuid_view).GetPath().c_str());
  EXPECT_FALSE(ec);
  return uuid_view;
}

ModuleSpec GetTestModuleSpec() {
  ModuleSpec module_spec(GetRemotePath(), ArchSpec(k_arch));
  module_spec.GetUUID().SetFromStringRef(k_module_uuid);
  module_spec.SetObjectSize(k_module_size);
  return module_spec;
}

void CheckModule(const ModuleSP &module_sp) {
  ASSERT_TRUE(module_sp);
  ASSERT_EQ(module_sp->GetUUID().GetAsString(), k_module_uuid);
  ASSERT_EQ(module_sp->GetObjectOffset(), 0U);
  ASSERT_EQ(module_sp->GetPlatformFileSpec(), GetRemotePath());
}

SymbolContextList FindFunctions(const ModuleSP &module_sp,
                                const llvm::StringRef &name) {
  SymbolContextList sc_list;
  ModuleFunctionSearchOptions function_options;
  function_options.include_symbols = true;
  function_options.include_inlines = true;
  FunctionNameType type = static_cast<FunctionNameType>(eSymbolTypeCode);
  module_sp->FindFunctions(ConstString(name), CompilerDeclContext(), type,
                           function_options, sc_list);
  return sc_list;
}

void CheckStrippedSymbol(const ModuleSP &module_sp) {
  SymbolContextList sc_list = FindFunctions(module_sp, k_function_symbol);
  EXPECT_EQ(1U, sc_list.GetSize());

  sc_list = FindFunctions(module_sp, k_hidden_function_symbol);
  EXPECT_EQ(0U, sc_list.GetSize());
}

void CheckUnstrippedSymbol(const ModuleSP &module_sp) {
  SymbolContextList sc_list = FindFunctions(module_sp, k_function_symbol);
  EXPECT_EQ(1U, sc_list.GetSize());

  sc_list = FindFunctions(module_sp, k_hidden_function_symbol);
  EXPECT_EQ(1U, sc_list.GetSize());
}

ProcessSP MockProcessCreateInstance(TargetSP target_sp, ListenerSP listener_sp,
                                    const FileSpec *crash_file_path,
                                    bool can_connect) {
  return std::make_shared<MockProcess>(target_sp, listener_sp);
}

class LocateModuleCallbackTest : public testing::Test {
  SubsystemRAII<FileSystem, HostInfo, ObjectFileBreakpad, ObjectFileELF,
                PlatformAndroid, PlatformLinux, SymbolFileBreakpad,
                SymbolFileSymtab>
      subsystems;

public:
  void SetUp() override {
    m_test_dir = GetTestDir();

    // Set module cache directory for PlatformAndroid.
    PlatformAndroid::GetGlobalPlatformProperties().SetModuleCacheDirectory(
        m_test_dir);

    // Create Debugger.
    ArchSpec host_arch("i386-pc-linux");
    Platform::SetHostPlatform(
        platform_linux::PlatformLinux::CreateInstance(true, &host_arch));
    m_debugger_sp = Debugger::CreateInstance();
    EXPECT_TRUE(m_debugger_sp);

    // Create PlatformAndroid.
    ArchSpec arch(k_arch);
    m_platform_sp = PlatformAndroid::CreateInstance(true, &arch);
    EXPECT_TRUE(m_platform_sp);

    // Create Target.
    m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
                                                eLoadDependentsNo,
                                                m_platform_sp, m_target_sp);
    EXPECT_TRUE(m_target_sp);

    // Create MockProcess.
    PluginManager::RegisterPlugin(k_process_plugin, "",
                                  MockProcessCreateInstance);
    m_process_sp =
        m_target_sp->CreateProcess(Listener::MakeListener("test-listener"),
                                   k_process_plugin, /*crash_file=*/nullptr,
                                   /*can_connect=*/true);
    EXPECT_TRUE(m_process_sp);

    m_module_spec = GetTestModuleSpec();
    m_module_spec_without_uuid = ModuleSpec(GetRemotePath(), ArchSpec(k_arch));
  }

  void TearDown() override {
    if (m_module_sp)
      ModuleList::RemoveSharedModule(m_module_sp);
  }

  void CheckNoCallback() {
    EXPECT_FALSE(m_platform_sp->GetLocateModuleCallback());
    EXPECT_EQ(m_callback_call_count, 0);
  }

  void CheckCallbackArgs(const ModuleSpec &module_spec,
                         FileSpec &module_file_spec, FileSpec &symbol_file_spec,
                         const ModuleSpec &expected_module_spec,
                         int expected_callback_call_count) {
    EXPECT_TRUE(expected_module_spec.Matches(module_spec,
                                             /*exact_arch_match=*/true));
    EXPECT_FALSE(module_file_spec);
    EXPECT_FALSE(symbol_file_spec);

    EXPECT_EQ(++m_callback_call_count, expected_callback_call_count);
  }

  void CheckCallbackArgsWithUUID(const ModuleSpec &module_spec,
                                 FileSpec &module_file_spec,
                                 FileSpec &symbol_file_spec,
                                 int expected_callback_call_count) {
    CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
                      m_module_spec, expected_callback_call_count);
    EXPECT_TRUE(module_spec.GetUUID().IsValid());
  }

  void CheckCallbackArgsWithoutUUID(const ModuleSpec &module_spec,
                                    FileSpec &module_file_spec,
                                    FileSpec &symbol_file_spec,
                                    int expected_callback_call_count) {
    CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
                      m_module_spec_without_uuid, expected_callback_call_count);
    EXPECT_FALSE(module_spec.GetUUID().IsValid());
  }

protected:
  FileSpec m_test_dir;
  DebuggerSP m_debugger_sp;
  PlatformSP m_platform_sp;
  TargetSP m_target_sp;
  ProcessSP m_process_sp;
  ModuleSpec m_module_spec;
  ModuleSpec m_module_spec_without_uuid;
  ModuleSP m_module_sp;
  int m_callback_call_count = 0;
};

} // namespace

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModule) {
  // The module file is cached, and the locate module callback is not set.
  // GetOrCreateModule should succeed to return the module from the cache.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  CheckNoCallback();

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckStrippedSymbol(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModuleAndSymbol) {
  // The module and symbol files are cached, and the locate module callback is
  // not set. GetOrCreateModule should succeed to return the module from the
  // cache with the symbol.
  FileSpec uuid_view = BuildCacheDirWithSymbol(m_test_dir);

  CheckNoCallback();

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
  CheckUnstrippedSymbol(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleWithCachedModuleAndBreakpadSymbol) {
  // The module file and breakpad symbol file are cached, and the locate module
  // callback is not set. GetOrCreateModule should succeed to return the module
  // from the cache with the symbol.
  FileSpec uuid_view = BuildCacheDirWithBreakpadSymbol(m_test_dir);

  CheckNoCallback();

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
  CheckUnstrippedSymbol(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleFailure) {
  // The cache dir is empty, and the locate module callback is not set.
  // GetOrCreateModule should fail because PlatformAndroid tries to download the
  // module and fails.
  BuildEmptyCacheDir(m_test_dir);

  CheckNoCallback();

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_FALSE(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackFailureNoCache) {
  // The cache dir is empty, also the locate module callback fails for some
  // reason. GetOrCreateModule should fail because PlatformAndroid tries to
  // download the module and fails.
  BuildEmptyCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        return Status("The locate module callback failed");
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  ASSERT_FALSE(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackFailureCached) {
  // The module file is cached, so GetOrCreateModule should succeed to return
  // the module from the cache even though the locate module callback fails for
  // some reason.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        return Status("The locate module callback failed");
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckStrippedSymbol(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackNoFiles) {
  // The module file is cached, so GetOrCreateModule should succeed to return
  // the module from the cache even though the locate module callback returns
  // no files.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        // The locate module callback succeeds but it does not set
        // module_file_spec nor symbol_file_spec.
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckStrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackNonExistentModule) {
  // The module file is cached, so GetOrCreateModule should succeed to return
  // the module from the cache even though the locate module callback returns
  // non-existent module file.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        module_file_spec.SetPath("/this path does not exist");
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckStrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackNonExistentSymbol) {
  // The module file is cached, so GetOrCreateModule should succeed to return
  // the module from the cache even though the locate module callback returns
  // non-existent symbol file.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        // The locate module callback returns a right module file.
        module_file_spec.SetPath(GetInputFilePath(k_module_file));
        // But it returns non-existent symbols file.
        symbol_file_spec.SetPath("/this path does not exist");
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_TRUE(m_module_sp->GetSymbolFileFileSpec().GetPath().empty());
  CheckStrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackSuccessWithModule) {
  // The locate module callback returns a module file, GetOrCreateModule should
  // succeed to return the module from the Inputs directory.
  BuildEmptyCacheDir(m_test_dir);

  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec &module_spec,
                                                FileSpec &module_file_spec,
                                                FileSpec &symbol_file_spec) {
    CheckCallbackArgsWithUUID(module_spec, module_file_spec, symbol_file_spec,
                              1);
    module_file_spec.SetPath(GetInputFilePath(k_module_file));
    return Status();
  });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_module_file)));
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckStrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithSymbolAsModule) {
  // The locate module callback returns the symbol file as a module file. It
  // should work since the sections and UUID of the symbol file are the exact
  // same with the module file, GetOrCreateModule should succeed to return the
  // module with the symbol file from Inputs directory.
  BuildEmptyCacheDir(m_test_dir);

  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec &module_spec,
                                                FileSpec &module_file_spec,
                                                FileSpec &symbol_file_spec) {
    CheckCallbackArgsWithUUID(module_spec, module_file_spec, symbol_file_spec,
                              1);
    module_file_spec.SetPath(GetInputFilePath(k_symbol_file));
    return Status();
  });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_symbol_file)));
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithSymbolAsModuleAndSymbol) {
  // The locate module callback returns a symbol file as both a module file and
  // a symbol file. It should work since the sections and UUID of the symbol
  // file are the exact same with the module file, GetOrCreateModule should
  // succeed to return the module with the symbol file from Inputs directory.
  BuildEmptyCacheDir(m_test_dir);

  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec &module_spec,
                                                FileSpec &module_file_spec,
                                                FileSpec &symbol_file_spec) {
    CheckCallbackArgsWithUUID(module_spec, module_file_spec, symbol_file_spec,
                              1);
    module_file_spec.SetPath(GetInputFilePath(k_symbol_file));
    symbol_file_spec.SetPath(GetInputFilePath(k_symbol_file));
    return Status();
  });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_symbol_file)));
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithModuleAndSymbol) {
  // The locate module callback returns a module file and a symbol file,
  // GetOrCreateModule should succeed to return the module from Inputs
  // directory, along with the symbol file.
  BuildEmptyCacheDir(m_test_dir);

  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec &module_spec,
                                                FileSpec &module_file_spec,
                                                FileSpec &symbol_file_spec) {
    CheckCallbackArgsWithUUID(module_spec, module_file_spec, symbol_file_spec,
                              1);
    module_file_spec.SetPath(GetInputFilePath(k_module_file));
    symbol_file_spec.SetPath(GetInputFilePath(k_symbol_file));
    return Status();
  });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_module_file)));
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithModuleAndBreakpadSymbol) {
  // The locate module callback returns a module file and a breakpad symbol
  // file, GetOrCreateModule should succeed to return the module with the symbol
  // file from Inputs directory.
  BuildEmptyCacheDir(m_test_dir);

  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec &module_spec,
                                                FileSpec &module_file_spec,
                                                FileSpec &symbol_file_spec) {
    CheckCallbackArgsWithUUID(module_spec, module_file_spec, symbol_file_spec,
                              1);
    module_file_spec.SetPath(GetInputFilePath(k_module_file));
    symbol_file_spec.SetPath(GetInputFilePath(k_breakpad_symbol_file));
    return Status();
  });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_module_file)));
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_breakpad_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithOnlySymbol) {
  // The get callback returns only a symbol file, and the module is cached,
  // GetOrCreateModule should succeed to return the module from the cache
  // along with the symbol file from the Inputs directory.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        symbol_file_spec.SetPath(GetInputFilePath(k_symbol_file));
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithOnlyBreakpadSymbol) {
  // The get callback returns only a breakpad symbol file, and the module is
  // cached, GetOrCreateModule should succeed to return the module from the
  // cache along with the symbol file from the Inputs directory.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        symbol_file_spec.SetPath(GetInputFilePath(k_breakpad_symbol_file));
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_breakpad_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithMultipleSymbols) {
  // The get callback returns only a symbol file. The first call returns
  // a breakpad symbol file and the second call returns a symbol file.
  // Also the module is cached, so GetOrCreateModule should succeed to return
  // the module from the cache along with the breakpad symbol file from the
  // Inputs directory because GetOrCreateModule will use the first symbol file
  // from the callback.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        symbol_file_spec.SetPath(GetInputFilePath(
            callback_call_count == 1 ? k_breakpad_symbol_file : k_symbol_file));
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_breakpad_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleNoCacheWithCallbackOnlySymbol) {
  // The get callback returns only a symbol file, but the module is not
  // cached, GetOrCreateModule should fail because of the missing module.
  BuildEmptyCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        symbol_file_spec.SetPath(GetInputFilePath(k_symbol_file));
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  ASSERT_FALSE(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleNoCacheWithCallbackOnlyBreakpadSymbol) {
  // The get callback returns only a breakpad symbol file, but the module is not
  // cached, GetOrCreateModule should fail because of the missing module.
  BuildEmptyCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                  symbol_file_spec, ++callback_call_count);
        symbol_file_spec.SetPath(GetInputFilePath(k_breakpad_symbol_file));
        return Status();
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  ASSERT_FALSE(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithModuleByPlatformUUID) {
  // This is a simulation for Android remote platform debugging.
  // The locate module callback first call fails because module_spec does not
  // have UUID. Then, the callback second call returns a module file because the
  // platform resolved the module_spec UUID from the target process.
  // GetOrCreateModule should succeed to return the module from the Inputs
  // directory.
  BuildEmptyCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        callback_call_count++;
        if (callback_call_count == 1) {
          // The module_spec does not have UUID on the first call.
          CheckCallbackArgsWithoutUUID(module_spec, module_file_spec,
                                       symbol_file_spec, callback_call_count);
          return Status("Ignored empty UUID");
        } else {
          // The module_spec has UUID on the second call.
          CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                    symbol_file_spec, callback_call_count);
          module_file_spec.SetPath(GetInputFilePath(k_module_file));
          return Status();
        }
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec_without_uuid,
                                               /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_module_file)));
  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
  CheckStrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithSymbolByPlatformUUID) {
  // Same as GetOrCreateModuleCallbackSuccessWithModuleByPlatformUUID,
  // but with a symbol file. GetOrCreateModule should succeed to return the
  // module file and the symbol file from the Inputs directory.
  BuildEmptyCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        callback_call_count++;
        if (callback_call_count == 1) {
          // The module_spec does not have UUID on the first call.
          CheckCallbackArgsWithoutUUID(module_spec, module_file_spec,
                                       symbol_file_spec, callback_call_count);
          return Status("Ignored empty UUID");
        } else {
          // The module_spec has UUID on the second call.
          CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                    symbol_file_spec, callback_call_count);
          module_file_spec.SetPath(GetInputFilePath(k_module_file));
          symbol_file_spec.SetPath(GetInputFilePath(k_symbol_file));
          return Status();
        }
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec_without_uuid,
                                               /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_module_file)));
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithBreakpadSymbolByPlatformUUID) {
  // Same as GetOrCreateModuleCallbackSuccessWithModuleByPlatformUUID,
  // but with a breakpad symbol file. GetOrCreateModule should succeed to return
  // the module file and the symbol file from the Inputs directory.
  BuildEmptyCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        callback_call_count++;
        if (callback_call_count == 1) {
          // The module_spec does not have UUID on the first call.
          CheckCallbackArgsWithoutUUID(module_spec, module_file_spec,
                                       symbol_file_spec, callback_call_count);
          return Status("Ignored empty UUID");
        } else {
          // The module_spec has UUID on the second call.
          CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                    symbol_file_spec, callback_call_count);
          module_file_spec.SetPath(GetInputFilePath(k_module_file));
          symbol_file_spec.SetPath(GetInputFilePath(k_breakpad_symbol_file));
          return Status();
        }
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec_without_uuid,
                                               /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(),
            FileSpec(GetInputFilePath(k_module_file)));
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_breakpad_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}

TEST_F(LocateModuleCallbackTest,
       GetOrCreateModuleCallbackSuccessWithOnlyBreakpadSymbolByPlatformUUID) {
  // This is a simulation for Android remote platform debugging.
  // The locate module callback first call fails because module_spec does not
  // have UUID. Then, the callback second call returns a breakpad symbol file
  // for the UUID from the target process. GetOrCreateModule should succeed to
  // return the module from the cache along with the symbol file from the Inputs
  // directory.
  FileSpec uuid_view = BuildCacheDir(m_test_dir);

  int callback_call_count = 0;
  m_platform_sp->SetLocateModuleCallback(
      [this, &callback_call_count](const ModuleSpec &module_spec,
                                   FileSpec &module_file_spec,
                                   FileSpec &symbol_file_spec) {
        callback_call_count++;
        if (callback_call_count == 1) {
          // The module_spec does not have UUID on the first call.
          CheckCallbackArgsWithoutUUID(module_spec, module_file_spec,
                                       symbol_file_spec, callback_call_count);
          return Status("Ignored empty UUID");
        } else {
          // The module_spec has UUID on the second call.
          CheckCallbackArgsWithUUID(module_spec, module_file_spec,
                                    symbol_file_spec, callback_call_count);
          symbol_file_spec.SetPath(GetInputFilePath(k_breakpad_symbol_file));
          return Status();
        }
      });

  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec_without_uuid,
                                               /*notify=*/false);
  ASSERT_EQ(callback_call_count, 2);
  CheckModule(m_module_sp);
  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(),
            FileSpec(GetInputFilePath(k_breakpad_symbol_file)));
  CheckUnstrippedSymbol(m_module_sp);
  ModuleList::RemoveSharedModule(m_module_sp);
}