xref: /aosp_15_r20/external/cronet/base/android/library_loader/library_prefetcher_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/android/library_loader/library_prefetcher.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <sys/mman.h>
10 #include "base/android/library_loader/anchor_functions_buildflags.h"
11 #include "base/memory/writable_shared_memory_region.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 #if BUILDFLAG(SUPPORTS_CODE_ORDERING)
16 namespace base {
17 namespace android {
18 
19 // Fails with ASAN, crbug.com/570423.
20 #if !defined(ADDRESS_SANITIZER)
21 namespace {
22 const size_t kPageSize = 4096;
23 }  // namespace
24 
25 // https://crbug.com/1056021 - flaky on Nexus 5.
TEST(NativeLibraryPrefetcherTest,DISABLED_TestPercentageOfResidentCode)26 TEST(NativeLibraryPrefetcherTest, DISABLED_TestPercentageOfResidentCode) {
27   size_t length = 4 * kPageSize;
28   auto shared_region = base::WritableSharedMemoryRegion::Create(length);
29   ASSERT_TRUE(shared_region.IsValid());
30   auto mapping = shared_region.Map();
31   ASSERT_TRUE(mapping.IsValid());
32   void* address = mapping.memory();
33   size_t start = reinterpret_cast<size_t>(address);
34   size_t end = start + length;
35 
36   // Remove everything.
37   ASSERT_EQ(0, madvise(address, length, MADV_DONTNEED));
38   EXPECT_EQ(0, NativeLibraryPrefetcher::PercentageOfResidentCode(start, end));
39 
40   // Get everything back.
41   ASSERT_EQ(0, mlock(address, length));
42   EXPECT_EQ(100, NativeLibraryPrefetcher::PercentageOfResidentCode(start, end));
43   munlock(address, length);
44 }
45 #endif  // !defined(ADDRESS_SANITIZER)
46 
47 }  // namespace android
48 }  // namespace base
49 #endif  // BUILDFLAG(SUPPORTS_CODE_ORDERING)
50