blob: 72a560cfee732b62f697b9d75f875a8136d2a1e0 [file] [log] [blame]
Peter Collingbourne39a17302024-03-07 17:50:10 -08001/*
2 * Copyright 2024, 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 "libdebuggerd/utility_host.h"
18
19#include <sys/prctl.h>
20
21#include <string>
22
23#include <android-base/stringprintf.h>
24
25using android::base::StringPrintf;
26
27#ifndef PR_MTE_TAG_SHIFT
28#define PR_MTE_TAG_SHIFT 3
29#endif
30
31#ifndef PR_MTE_TAG_MASK
32#define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
33#endif
34
35#ifndef PR_MTE_TCF_ASYNC
36#define PR_MTE_TCF_ASYNC (1UL << 2)
37#endif
38
39#ifndef PR_MTE_TCF_SYNC
40#define PR_MTE_TCF_SYNC (1UL << 1)
41#endif
42
43#ifndef PR_PAC_APIAKEY
44#define PR_PAC_APIAKEY (1UL << 0)
45#endif
46
47#ifndef PR_PAC_APIBKEY
48#define PR_PAC_APIBKEY (1UL << 1)
49#endif
50
51#ifndef PR_PAC_APDAKEY
52#define PR_PAC_APDAKEY (1UL << 2)
53#endif
54
55#ifndef PR_PAC_APDBKEY
56#define PR_PAC_APDBKEY (1UL << 3)
57#endif
58
59#ifndef PR_PAC_APGAKEY
60#define PR_PAC_APGAKEY (1UL << 4)
61#endif
62
63#ifndef PR_TAGGED_ADDR_ENABLE
64#define PR_TAGGED_ADDR_ENABLE (1UL << 0)
65#endif
66
67#define DESCRIBE_FLAG(flag) \
68 if (value & flag) { \
69 desc += ", "; \
70 desc += #flag; \
71 value &= ~flag; \
72 }
73
74static std::string describe_end(long value, std::string& desc) {
75 if (value) {
76 desc += StringPrintf(", unknown 0x%lx", value);
77 }
78 return desc.empty() ? "" : " (" + desc.substr(2) + ")";
79}
80
81std::string describe_tagged_addr_ctrl(long value) {
82 std::string desc;
83 DESCRIBE_FLAG(PR_TAGGED_ADDR_ENABLE);
84 DESCRIBE_FLAG(PR_MTE_TCF_SYNC);
85 DESCRIBE_FLAG(PR_MTE_TCF_ASYNC);
86 if (value & PR_MTE_TAG_MASK) {
87 desc += StringPrintf(", mask 0x%04lx", (value & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT);
88 value &= ~PR_MTE_TAG_MASK;
89 }
90 return describe_end(value, desc);
91}
92
93std::string describe_pac_enabled_keys(long value) {
94 std::string desc;
95 DESCRIBE_FLAG(PR_PAC_APIAKEY);
96 DESCRIBE_FLAG(PR_PAC_APIBKEY);
97 DESCRIBE_FLAG(PR_PAC_APDAKEY);
98 DESCRIBE_FLAG(PR_PAC_APDBKEY);
99 DESCRIBE_FLAG(PR_PAC_APGAKEY);
100 return describe_end(value, desc);
101}