[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / base / android / library_loader / library_prefetcher.h
blob64e5e1ecb4b786f6e8c8ad159262c64c6e69ff17
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.
5 #ifndef BASE_ANDROID_LIBRARY_LOADER_LIBRARY_PREFETCHER_H_
6 #define BASE_ANDROID_LIBRARY_LOADER_LIBRARY_PREFETCHER_H_
8 #include <jni.h>
10 #include <stdint.h>
11 #include <string>
13 #include "base/debug/proc_maps_linux.h"
14 #include "base/gtest_prod_util.h"
16 namespace base {
17 namespace android {
19 // Forks and waits for a process prefetching the native library. This is done in
20 // a forked process for the following reasons:
21 // - Isolating the main process from mistakes in the parsing. If the parsing
22 // returns an incorrect address, only the forked process will crash.
23 // - Not inflating the memory used by the main process uselessly, which could
24 // increase its likelihood to be killed.
25 // The forked process has background priority and, since it is not declared to
26 // the Android runtime, can be killed at any time, which is not an issue here.
27 class BASE_EXPORT NativeLibraryPrefetcher {
28 public:
29 // Finds the ranges matching the native library, forks a low priority
30 // process pre-fetching these ranges and wait()s for it.
31 // Returns true for success.
32 static bool ForkAndPrefetchNativeLibrary();
34 private:
35 using AddressRange = std::pair<uintptr_t, uintptr_t>;
36 // Returns true if the region matches native code or data.
37 static bool IsGoodToPrefetch(const base::debug::MappedMemoryRegion& region);
38 // Filters the regions to keep only libchrome ranges if possible.
39 static void FilterLibchromeRangesOnlyIfPossible(
40 const std::vector<base::debug::MappedMemoryRegion>& regions,
41 std::vector<AddressRange>* ranges);
42 // Finds the ranges matching the native library in /proc/self/maps.
43 // Returns true for success.
44 static bool FindRanges(std::vector<AddressRange>* ranges);
46 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
47 TestIsGoodToPrefetchNoRange);
48 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
49 TestIsGoodToPrefetchUnreadableRange);
50 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
51 TestIsGoodToPrefetchSkipSharedRange);
52 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
53 TestIsGoodToPrefetchLibchromeRange);
54 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
55 TestIsGoodToPrefetchBaseApkRange);
56 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
57 TestFilterLibchromeRangesOnlyIfPossibleNoLibchrome);
58 FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
59 TestFilterLibchromeRangesOnlyIfPossibleHasLibchrome);
61 DISALLOW_IMPLICIT_CONSTRUCTORS(NativeLibraryPrefetcher);
64 } // namespace android
65 } // namespace base
67 #endif // BASE_ANDROID_LIBRARY_LOADER_LIBRARY_PREFETCHER_H_