xref: /aosp_15_r20/system/unwinding/libunwindstack/tests/MemoryMteTest.cpp (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
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 "ForkTest.h"
27 #include "MemoryLocal.h"
28 #include "MemoryRemote.h"
29 #include "PidUtils.h"
30 #include "TestUtils.h"
31 
32 namespace unwindstack {
33 
34 using MemoryMteTest = ForkTest;
35 
CreateTagMapping()36 static uintptr_t CreateTagMapping() {
37 #if defined(__aarch64__)
38   uintptr_t mapping =
39       reinterpret_cast<uintptr_t>(mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE | PROT_MTE,
40                                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
41   if (reinterpret_cast<void*>(mapping) == MAP_FAILED) {
42     return 0;
43   }
44   __asm__ __volatile__(".arch_extension mte; stg %0, [%0]"
45                        :
46                        : "r"(mapping + (1ULL << 56))
47                        : "memory");
48   return mapping;
49 #else
50   return 0;
51 #endif
52 }
53 
TEST_F(MemoryMteTest,remote_read_tag)54 TEST_F(MemoryMteTest, remote_read_tag) {
55 #if !defined(__aarch64__)
56   GTEST_SKIP() << "Requires aarch64";
57 #else
58   if (!mte_supported()) {
59     GTEST_SKIP() << "Requires MTE";
60   }
61 #endif
62 
63   uintptr_t mapping = CreateTagMapping();
64   ASSERT_NE(0U, mapping);
65 
66   ASSERT_NO_FATAL_FAILURE(Fork());
67 
68   MemoryRemote remote(pid_);
69 
70   EXPECT_EQ(1, remote.ReadTag(mapping));
71   EXPECT_EQ(0, remote.ReadTag(mapping + 16));
72 }
73 
TEST_F(MemoryMteTest,local_read_tag)74 TEST_F(MemoryMteTest, local_read_tag) {
75 #if !defined(__aarch64__)
76   GTEST_SKIP() << "Requires aarch64";
77 #else
78   if (!mte_supported()) {
79     GTEST_SKIP() << "Requires MTE";
80   }
81 #endif
82 
83   uintptr_t mapping = CreateTagMapping();
84   ASSERT_NE(0U, mapping);
85 
86   MemoryLocal local;
87 
88   EXPECT_EQ(1, local.ReadTag(mapping));
89   EXPECT_EQ(0, local.ReadTag(mapping + 16));
90 }
91 
92 }  // namespace unwindstack
93