1 // Copyright 2015 The Chromium Authors. All rights reserved.
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/shared_memory.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 
TEST(NativeLibraryPrefetcherTest,TestPercentageOfResidentCode)25 TEST(NativeLibraryPrefetcherTest, TestPercentageOfResidentCode) {
26   size_t length = 4 * kPageSize;
27   base::SharedMemory shared_mem;
28   ASSERT_TRUE(shared_mem.CreateAndMapAnonymous(length));
29   void* address = shared_mem.memory();
30   size_t start = reinterpret_cast<size_t>(address);
31   size_t end = start + length;
32 
33   // Remove everything.
34   ASSERT_EQ(0, madvise(address, length, MADV_DONTNEED));
35   EXPECT_EQ(0, NativeLibraryPrefetcher::PercentageOfResidentCode(start, end));
36 
37   // Get everything back.
38   ASSERT_EQ(0, mlock(address, length));
39   EXPECT_EQ(100, NativeLibraryPrefetcher::PercentageOfResidentCode(start, end));
40   munlock(address, length);
41 }
42 #endif  // !defined(ADDRESS_SANITIZER)
43 
44 }  // namespace android
45 }  // namespace base
46 #endif  // BUILDFLAG(SUPPORTS_CODE_ORDERING)
47