-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[sanitizer] Add MemCpyAccessible #112794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sanitizer] Add MemCpyAccessible #112794
Changes from 8 commits
9de3ba0
14c0f3d
6acf4c4
6233ca1
b481c38
199f7b3
cb37ecb
fdf1a27
7e8937d
9310397
cb1e995
d920ef3
a178445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,6 +219,32 @@ static void StopStackDepotBackgroundThread() { | |
| static void StopStackDepotBackgroundThread() {} | ||
| #endif | ||
|
|
||
| void MemCpyAccessible(void *dest, const void *src, uptr n) { | ||
| if (TryMemCpy(dest, src, n)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not fully sure about this optimization. In the case where there are holes this makes it more expensive. Does the caller generally know that?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure as well. On some examples fast pass 4x faster than page by page. |
||
| return; | ||
|
|
||
| const uptr page_size = GetPageSize(); | ||
| uptr b = reinterpret_cast<uptr>(src); | ||
| uptr b_up = RoundUpTo(b, page_size); | ||
|
|
||
| uptr e = reinterpret_cast<uptr>(src) + n; | ||
| uptr e_down = RoundDownTo(e, page_size); | ||
|
|
||
| auto copy_or_zero = [dest, src](uptr b, uptr e) { | ||
vitalybuka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uptr d = reinterpret_cast<uptr>(dest) + (b - reinterpret_cast<uptr>(src)); | ||
| if (!TryMemCpy(reinterpret_cast<void *>(d), reinterpret_cast<void *>(b), | ||
| e - b)) | ||
| internal_memset(reinterpret_cast<void *>(d), 0, e - b); | ||
| }; | ||
|
|
||
| copy_or_zero(b, b_up); | ||
|
|
||
| for (uptr p = b_up; p < e_down; p += page_size) | ||
| copy_or_zero(p, p + page_size); | ||
|
|
||
| copy_or_zero(e_down, e); | ||
| } | ||
|
|
||
| } // namespace __sanitizer | ||
|
|
||
| SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_sandbox_on_notify, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,14 @@ | |
| #include "sanitizer_common/sanitizer_platform.h" | ||
| #if SANITIZER_POSIX | ||
|
|
||
| #include "sanitizer_common/sanitizer_common.h" | ||
| #include "gtest/gtest.h" | ||
| # include <pthread.h> | ||
| # include <sys/mman.h> | ||
|
|
||
| #include <pthread.h> | ||
| #include <sys/mman.h> | ||
| # include <algorithm> | ||
| # include <numeric> | ||
|
|
||
| # include "gtest/gtest.h" | ||
| # include "sanitizer_common/sanitizer_common.h" | ||
|
|
||
| namespace __sanitizer { | ||
|
|
||
|
|
@@ -86,6 +89,81 @@ TEST(SanitizerCommon, IsAccessibleMemoryRangeLarge) { | |
| buffer.size())); | ||
| } | ||
|
|
||
| TEST(SanitizerCommon, TryMemCpy) { | ||
| std::vector<char> src(10000000); | ||
| std::iota(src.begin(), src.end(), 123); | ||
| std::vector<char> dst; | ||
|
|
||
| // Don't use ::testing::ElementsAreArray or similar, as the huge output on an | ||
| // error is not helpful. | ||
|
|
||
| dst.assign(1, 0); | ||
| EXPECT_TRUE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), src.begin())); | ||
|
|
||
| dst.assign(100, 0); | ||
| EXPECT_TRUE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), src.begin())); | ||
|
|
||
| dst.assign(534, 0); | ||
| EXPECT_TRUE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), src.begin())); | ||
|
|
||
| dst.assign(GetPageSize(), 0); | ||
| EXPECT_TRUE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), src.begin())); | ||
|
|
||
| dst.assign(src.size(), 0); | ||
| EXPECT_TRUE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), src.begin())); | ||
|
|
||
| dst.assign(src.size() - 1, 0); | ||
| EXPECT_TRUE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), src.begin())); | ||
| } | ||
|
|
||
| TEST(SanitizerCommon, TryMemCpyNull) { | ||
| std::vector<char> dst(100); | ||
| EXPECT_FALSE(TryMemCpy(dst.data(), nullptr, dst.size())); | ||
| } | ||
|
|
||
| TEST(SanitizerCommon, MemCpyAccessible) { | ||
| const int page_num = 1000; | ||
| const int page_size = GetPageSize(); | ||
| InternalMmapVector<char> src(page_num * page_size); | ||
| std::iota(src.begin(), src.end(), 123); | ||
| std::vector<char> dst; | ||
| std::vector<char> exp = {src.begin(), src.end()}; | ||
|
|
||
| // Protect some pages. | ||
| for (int i = 7; i < page_num; i *= 2) { | ||
| mprotect(src.data() + i * page_size, page_size, PROT_NONE); | ||
| std::fill(exp.data() + i * page_size, exp.data() + (i + 1) * page_size, 0); | ||
| } | ||
|
|
||
| dst.assign(src.size(), 0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit '\0' |
||
| EXPECT_FALSE(TryMemCpy(dst.data(), src.data(), dst.size())); | ||
|
|
||
| // Full page aligned range with mprotect pages. | ||
| dst.assign(src.size(), 0); | ||
| MemCpyAccessible(dst.data(), src.data(), dst.size()); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), exp.begin())); | ||
|
|
||
| // Misaligned range with mprotect pages. | ||
| size_t offb = 3; | ||
| size_t offe = 7; | ||
| dst.assign(src.size() - offb - offe, 0); | ||
| MemCpyAccessible(dst.data(), src.data() + offb, dst.size()); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), exp.begin() + offb)); | ||
|
|
||
| // Misaligned range with ends in mprotect pages. | ||
| offb = 3 + 7 * page_size; | ||
| offe = 7 + 14 * page_size; | ||
| dst.assign(src.size() - offb - offe, 0); | ||
| MemCpyAccessible(dst.data(), src.data() + offb, dst.size()); | ||
| EXPECT_TRUE(std::equal(dst.begin(), dst.end(), exp.begin() + offb)); | ||
| } | ||
|
|
||
| } // namespace __sanitizer | ||
|
|
||
| #endif // SANITIZER_POSIX | ||
Uh oh!
There was an error while loading. Please reload this page.