diff --git a/linker/linker_mapped_file_fragment.cpp b/linker/linker_mapped_file_fragment.cpp index 6a500ef1e..27c1c69af 100644 --- a/linker/linker_mapped_file_fragment.cpp +++ b/linker/linker_mapped_file_fragment.cpp @@ -16,32 +16,13 @@ #include "linker_mapped_file_fragment.h" #include "linker_debug.h" +#include "linker_utils.h" #include #include #include #include -constexpr off64_t kPageMask = ~static_cast(PAGE_SIZE-1); - -static off64_t page_start(off64_t offset) { - return offset & kPageMask; -} - -static bool safe_add(off64_t* out, off64_t a, size_t b) { - CHECK(a >= 0); - if (static_cast(INT64_MAX - a) < b) { - return false; - } - - *out = a + b; - return true; -} - -static size_t page_offset(off64_t offset) { - return static_cast(offset & (PAGE_SIZE-1)); -} - MappedFileFragment::MappedFileFragment() : map_start_(nullptr), map_size_(0), data_(nullptr), size_ (0) { } diff --git a/linker/linker_utils.cpp b/linker/linker_utils.cpp index f81b77b23..db43d38f8 100644 --- a/linker/linker_utils.cpp +++ b/linker/linker_utils.cpp @@ -105,3 +105,23 @@ bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* return true; } +constexpr off64_t kPageMask = ~static_cast(PAGE_SIZE-1); + +off64_t page_start(off64_t offset) { + return offset & kPageMask; +} + +bool safe_add(off64_t* out, off64_t a, size_t b) { + CHECK(a >= 0); + if (static_cast(INT64_MAX - a) < b) { + return false; + } + + *out = a + b; + return true; +} + +size_t page_offset(off64_t offset) { + return static_cast(offset & (PAGE_SIZE-1)); +} + diff --git a/linker/linker_utils.h b/linker/linker_utils.h index b998fb5df..65ffbdc5f 100644 --- a/linker/linker_utils.h +++ b/linker/linker_utils.h @@ -24,4 +24,8 @@ bool normalize_path(const char* path, std::string* normalized_path); bool file_is_in_dir(const std::string& file, const std::string& dir); bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* entry_path); +off64_t page_start(off64_t offset); +size_t page_offset(off64_t offset); +bool safe_add(off64_t* out, off64_t a, size_t b); + #endif diff --git a/linker/tests/linker_utils_test.cpp b/linker/tests/linker_utils_test.cpp index d9b290cd6..3be9b3f37 100644 --- a/linker/tests/linker_utils_test.cpp +++ b/linker/tests/linker_utils_test.cpp @@ -69,3 +69,24 @@ TEST(linker_utils, parse_zip_path_smoke) { ASSERT_EQ("", entry_path); } +TEST(linker_utils, page_start) { + ASSERT_EQ(0x0001000, page_start(0x0001000)); + ASSERT_EQ(0x3002000, page_start(0x300222f)); + ASSERT_EQ(0x6001000, page_start(0x6001fff)); +} + +TEST(linker_utils, page_offset) { + ASSERT_EQ(0x0U, page_offset(0x0001000)); + ASSERT_EQ(0x22fU, page_offset(0x300222f)); + ASSERT_EQ(0xfffU, page_offset(0x6001fff)); +} + +TEST(linker_utils, safe_add) { + int64_t val = 42; + ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U)); + ASSERT_EQ(42, val); + ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U)); + ASSERT_EQ(INT64_MAX, val); + ASSERT_TRUE(safe_add(&val, 2000, 42U)); + ASSERT_EQ(2042, val); +}