Merge "Move some utility functions to linker_utils"
am: fb3219fbd1 * commit 'fb3219fbd1dbb0a369d52cbd0200330fd8852bc1': Move some utility functions to linker_utils
This commit is contained in:
commit
8bd9e9ec69
@ -16,32 +16,13 @@
|
||||
|
||||
#include "linker_mapped_file_fragment.h"
|
||||
#include "linker_debug.h"
|
||||
#include "linker_utils.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
constexpr off64_t kPageMask = ~static_cast<off64_t>(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<uint64_t>(INT64_MAX - a) < b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = a + b;
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t page_offset(off64_t offset) {
|
||||
return static_cast<size_t>(offset & (PAGE_SIZE-1));
|
||||
}
|
||||
|
||||
MappedFileFragment::MappedFileFragment() : map_start_(nullptr), map_size_(0),
|
||||
data_(nullptr), size_ (0)
|
||||
{ }
|
||||
|
@ -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<off64_t>(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<uint64_t>(INT64_MAX - a) < b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = a + b;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t page_offset(off64_t offset) {
|
||||
return static_cast<size_t>(offset & (PAGE_SIZE-1));
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user