1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/mman.h>
18 #include <sys/types.h>
19
20 #include <gtest/gtest.h>
21
22 #ifdef __ANDROID__
23 #include <bionic/mte.h>
24 #endif
25
26 #include "MemoryLocal.h"
27 #include "MemoryRemote.h"
28 #include "TestUtils.h"
29
30 namespace unwindstack {
31
CreateTagMapping()32 static uintptr_t CreateTagMapping() {
33 #if defined(__aarch64__)
34 uintptr_t mapping =
35 reinterpret_cast<uintptr_t>(mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE | PROT_MTE,
36 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
37 if (reinterpret_cast<void*>(mapping) == MAP_FAILED) {
38 return 0;
39 }
40 __asm__ __volatile__(".arch_extension mte; stg %0, [%0]"
41 :
42 : "r"(mapping + (1ULL << 56))
43 : "memory");
44 return mapping;
45 #else
46 return 0;
47 #endif
48 }
49
TEST(MemoryMteTest,remote_read_tag)50 TEST(MemoryMteTest, remote_read_tag) {
51 #if !defined(__aarch64__)
52 GTEST_SKIP() << "Requires aarch64";
53 #else
54 if (!mte_supported()) {
55 GTEST_SKIP() << "Requires MTE";
56 }
57 #endif
58
59 uintptr_t mapping = CreateTagMapping();
60 ASSERT_NE(0U, mapping);
61
62 pid_t pid;
63 if ((pid = fork()) == 0) {
64 while (true)
65 ;
66 exit(1);
67 }
68 ASSERT_LT(0, pid);
69 TestScopedPidReaper reap(pid);
70
71 ASSERT_TRUE(TestAttach(pid));
72
73 MemoryRemote remote(pid);
74
75 EXPECT_EQ(1, remote.ReadTag(mapping));
76 EXPECT_EQ(0, remote.ReadTag(mapping + 16));
77
78 ASSERT_TRUE(TestDetach(pid));
79 }
80
TEST(MemoryMteTest,local_read_tag)81 TEST(MemoryMteTest, local_read_tag) {
82 #if !defined(__aarch64__)
83 GTEST_SKIP() << "Requires aarch64";
84 #else
85 if (!mte_supported()) {
86 GTEST_SKIP() << "Requires MTE";
87 }
88 #endif
89
90 uintptr_t mapping = CreateTagMapping();
91 ASSERT_NE(0U, mapping);
92
93 MemoryLocal local;
94
95 EXPECT_EQ(1, local.ReadTag(mapping));
96 EXPECT_EQ(0, local.ReadTag(mapping + 16));
97 }
98
99 } // namespace unwindstack
100