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 // SAFETY: There's no public way to get a span of the full mapped memory size.
33 // The `mapped_size()` is larger then `size()` but is the actual size of the
34 // shared memory backing.
35 span<uint8_t> memory =
36 UNSAFE_BUFFERS(base::span(mapping.data(), mapping.mapped_size()));
37 auto start = reinterpret_cast<uintptr_t>(&*memory.begin());
38 auto end = reinterpret_cast<uintptr_t>(&*memory.end());
39
40 // Remove everything.
41 ASSERT_EQ(0, madvise(memory.data(), memory.size(), MADV_DONTNEED));
42 EXPECT_EQ(0, NativeLibraryPrefetcher::PercentageOfResidentCode(start, end));
43
44 // Get everything back.
45 ASSERT_EQ(0, mlock(memory.data(), memory.size()));
46 EXPECT_EQ(100, NativeLibraryPrefetcher::PercentageOfResidentCode(start, end));
47 munlock(memory.data(), memory.size());
48 }
49 #endif // !defined(ADDRESS_SANITIZER)
50
51 } // namespace android
52 } // namespace base
53 #endif // BUILDFLAG(SUPPORTS_CODE_ORDERING)
54