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/ptrace.h>
18 #include <sys/uio.h>
19
20 #if defined(__BIONIC__)
21 #include <bionic/mte.h>
22 #else
23 #define mte_supported() false
24 #endif
25
26 #include "MemoryLocal.h"
27 #include "MemoryRemote.h"
28
29 namespace unwindstack {
30
ReadTag(uint64_t addr)31 long MemoryRemote::ReadTag(uint64_t addr) {
32 #if defined(PTRACE_PEEKMTETAGS) || defined(PT_PEEKMTETAGS)
33 char tag;
34 iovec iov = {&tag, 1};
35 if (ptrace(PTRACE_PEEKMTETAGS, pid_, reinterpret_cast<void*>(addr), &iov) != 0 ||
36 iov.iov_len != 1) {
37 return -1;
38 }
39 return tag;
40 #else
41 (void)addr;
42 return -1;
43 #endif
44 }
45
ReadTag(uint64_t addr)46 long MemoryLocal::ReadTag(uint64_t addr) {
47 #if defined(__aarch64__)
48 // Check that the memory is readable first. This is racy with the ldg but there's not much
49 // we can do about it.
50 char data;
51 if (!mte_supported() || !Read(addr, &data, 1)) {
52 return -1;
53 }
54
55 __asm__ __volatile__(".arch_extension mte; ldg %0, [%0]" : "+r"(addr) : : "memory");
56 return (addr >> 56) & 0xf;
57 #else
58 (void)addr;
59 return -1;
60 #endif
61 }
62
63 } // namespace unwindstack
64