summaryrefslogtreecommitdiff
path: root/runtime/base/safe_copy_test.cc
diff options
context:
space:
mode:
author Josh Gao <jmgao@google.com> 2017-04-17 18:31:26 -0700
committer Josh Gao <jmgao@google.com> 2017-04-18 14:04:20 -0700
commit682282eaac86202652e17752ef32f14c89e2ccda (patch)
treec779c7a48e7fa41b4aad0cb1f7bb4953fde241cc /runtime/base/safe_copy_test.cc
parentc36a970fcd4878eafd03d41eeff0a4cbdd400326 (diff)
base: add SafeCopy.
Add a function that uses process_vm_readv on Linux to safely dereference pointers without the risk of segfault. Bug: http://b/30836730 Test: safe_copy_test on host Change-Id: I10bafcffe2172e17b00f65455d1dd6a08aa631d7
Diffstat (limited to 'runtime/base/safe_copy_test.cc')
-rw-r--r--runtime/base/safe_copy_test.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/runtime/base/safe_copy_test.cc b/runtime/base/safe_copy_test.cc
new file mode 100644
index 0000000000..d5b8cdb05d
--- /dev/null
+++ b/runtime/base/safe_copy_test.cc
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "safe_copy.h"
+
+#include "common_runtime_test.h"
+
+#include <sys/mman.h>
+#include <sys/user.h>
+
+namespace art {
+
+#if defined(__linux__)
+
+TEST(SafeCopyTest, smoke) {
+ // Map two pages, and mark the second one as PROT_NONE.
+ void* map = mmap(nullptr, PAGE_SIZE * 2, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(MAP_FAILED, map);
+ char* page1 = static_cast<char*>(map);
+ ASSERT_EQ(0, mprotect(page1 + PAGE_SIZE, PAGE_SIZE, PROT_NONE));
+
+ page1[0] = 'a';
+ page1[PAGE_SIZE - 1] = 'z';
+
+ char buf[PAGE_SIZE];
+
+ // Completely valid read.
+ memset(buf, 0xCC, sizeof(buf));
+ EXPECT_EQ(static_cast<ssize_t>(PAGE_SIZE), SafeCopy(buf, page1, PAGE_SIZE));
+ EXPECT_EQ(0, memcmp(buf, page1, PAGE_SIZE));
+
+ // Reading off of the end.
+ memset(buf, 0xCC, sizeof(buf));
+ EXPECT_EQ(static_cast<ssize_t>(PAGE_SIZE - 1), SafeCopy(buf, page1 + 1, PAGE_SIZE));
+ EXPECT_EQ(0, memcmp(buf, page1 + 1, PAGE_SIZE - 1));
+
+ // Completely invalid.
+ EXPECT_EQ(0, SafeCopy(buf, page1 + PAGE_SIZE, PAGE_SIZE));
+ ASSERT_EQ(0, munmap(map, PAGE_SIZE * 2));
+}
+
+#endif // defined(__linux__)
+
+} // namespace art