diff options
author | David Carlier <[email protected]> | 2019-08-28 09:38:11 +0000 |
---|---|---|
committer | David Carlier <[email protected]> | 2019-08-28 09:43:27 +0000 |
commit | 426fc99fb55317a87641b625ac41ed21d8b429eb (patch) | |
tree | 7ca1bff777b5df2641ff04f17c616c611fe805ad /src | |
parent | fd99cf6c8e879e6471588dfc3d062551438611e3 (diff) |
locked pages, making the tag id possibly configurable.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/utils/os_utils.cpp | 37 | ||||
-rw-r--r-- | src/lib/utils/os_utils.h | 2 |
2 files changed, 31 insertions, 8 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index daa5bb73f..85da33382 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -55,6 +55,10 @@ extern "C" char **environ; #endif +#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) + #include <mach/vm_statistics.h> +#endif + namespace Botan { // Not defined in OS namespace for historical reasons @@ -401,6 +405,30 @@ size_t OS::read_env_variable_sz(const std::string& name, size_t def) return def; } +int OS::get_locked_fd() + { +#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) +// On Darwin, tagging anonymous pages allows vmmap to track these. +// Allowed from 240 to 255 for userland applications, taken an hardcoded +// value for now even though it can possibly intersect. + static constexpr int default_locked_fd = VM_MAKE_TAG(255); + int locked_fd = default_locked_fd; + + if (const char *locked_env = read_env_variable("BOTAN_LOCKED_FD")) + { + long locked_fdl = std::strtol(locked_env, nullptr, 10); + if (locked_fdl < 100 || locked_fdl > 255) + { + locked_fdl = default_locked_fd; + } + locked_fd = static_cast<int>(locked_fdl); + } + return VM_MAKE_TAG(locked_fd); +#else + return -1; +#endif + } + std::vector<void*> OS::allocate_locked_pages(size_t count) { std::vector<void*> result; @@ -430,14 +458,7 @@ std::vector<void*> OS::allocate_locked_pages(size_t count) #define PROT_MAX(p) 0 #endif const int pflags = PROT_READ | PROT_WRITE; -#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) -// On Darwin, tagging anonymous pages allows vmmap to track these. -// Allowed from 240 to 255 for userland applications, taken an hardcoded -// value for now even though it can possibly intersect. - const int locked_fd = (255<<24); -#else - const int locked_fd = -1; -#endif + const int locked_fd = get_locked_fd(); ptr = ::mmap(nullptr, 2*page_size, pflags | PROT_MAX(pflags), diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h index 5b8c49f99..9604777fe 100644 --- a/src/lib/utils/os_utils.h +++ b/src/lib/utils/os_utils.h @@ -111,6 +111,8 @@ const char* read_env_variable(const std::string& var_name); */ size_t read_env_variable_sz(const std::string& var_name, size_t def_value = 0); +int get_locked_fd(); + /** * Request count pages of RAM which are locked into memory using mlock, * VirtualLock, or some similar OS specific API. Free it with free_locked_pages. |