• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PidUtils.h"
29 #include "TestUtils.h"
30 
31 namespace unwindstack {
32 
CreateTagMapping()33 static uintptr_t CreateTagMapping() {
34 #if defined(__aarch64__)
35   uintptr_t mapping =
36       reinterpret_cast<uintptr_t>(mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE | PROT_MTE,
37                                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
38   if (reinterpret_cast<void*>(mapping) == MAP_FAILED) {
39     return 0;
40   }
41   __asm__ __volatile__(".arch_extension mte; stg %0, [%0]"
42                        :
43                        : "r"(mapping + (1ULL << 56))
44                        : "memory");
45   return mapping;
46 #else
47   return 0;
48 #endif
49 }
50 
TEST(MemoryMteTest,remote_read_tag)51 TEST(MemoryMteTest, remote_read_tag) {
52 #if !defined(__aarch64__)
53   GTEST_SKIP() << "Requires aarch64";
54 #else
55   if (!mte_supported()) {
56     GTEST_SKIP() << "Requires MTE";
57   }
58 #endif
59 
60   uintptr_t mapping = CreateTagMapping();
61   ASSERT_NE(0U, mapping);
62 
63   pid_t pid;
64   if ((pid = fork()) == 0) {
65     while (true)
66       ;
67     exit(1);
68   }
69   ASSERT_LT(0, pid);
70   TestScopedPidReaper reap(pid);
71 
72   ASSERT_TRUE(Attach(pid));
73 
74   MemoryRemote remote(pid);
75 
76   EXPECT_EQ(1, remote.ReadTag(mapping));
77   EXPECT_EQ(0, remote.ReadTag(mapping + 16));
78 
79   ASSERT_TRUE(Detach(pid));
80 }
81 
TEST(MemoryMteTest,local_read_tag)82 TEST(MemoryMteTest, local_read_tag) {
83 #if !defined(__aarch64__)
84   GTEST_SKIP() << "Requires aarch64";
85 #else
86   if (!mte_supported()) {
87     GTEST_SKIP() << "Requires MTE";
88   }
89 #endif
90 
91   uintptr_t mapping = CreateTagMapping();
92   ASSERT_NE(0U, mapping);
93 
94   MemoryLocal local;
95 
96   EXPECT_EQ(1, local.ReadTag(mapping));
97   EXPECT_EQ(0, local.ReadTag(mapping + 16));
98 }
99 
100 }  // namespace unwindstack
101