OSLog.cpp
7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// TODO: header template
#include "clang/AST/OSLog.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/FormatString.h"
#include "clang/Basic/Builtins.h"
#include "llvm/ADT/SmallBitVector.h"
using namespace clang;
using clang::analyze_os_log::OSLogBufferItem;
using clang::analyze_os_log::OSLogBufferLayout;
namespace {
class OSLogFormatStringHandler
: public analyze_format_string::FormatStringHandler {
private:
struct ArgData {
const Expr *E = nullptr;
Optional<OSLogBufferItem::Kind> Kind;
Optional<unsigned> Size;
Optional<const Expr *> Count;
Optional<const Expr *> Precision;
Optional<const Expr *> FieldWidth;
unsigned char Flags = 0;
StringRef MaskType;
};
SmallVector<ArgData, 4> ArgsData;
ArrayRef<const Expr *> Args;
OSLogBufferItem::Kind
getKind(analyze_format_string::ConversionSpecifier::Kind K) {
switch (K) {
case clang::analyze_format_string::ConversionSpecifier::sArg: // "%s"
return OSLogBufferItem::StringKind;
case clang::analyze_format_string::ConversionSpecifier::SArg: // "%S"
return OSLogBufferItem::WideStringKind;
case clang::analyze_format_string::ConversionSpecifier::PArg: { // "%P"
return OSLogBufferItem::PointerKind;
case clang::analyze_format_string::ConversionSpecifier::ObjCObjArg: // "%@"
return OSLogBufferItem::ObjCObjKind;
case clang::analyze_format_string::ConversionSpecifier::PrintErrno: // "%m"
return OSLogBufferItem::ErrnoKind;
default:
return OSLogBufferItem::ScalarKind;
}
}
}
public:
OSLogFormatStringHandler(ArrayRef<const Expr *> Args) : Args(Args) {
ArgsData.reserve(Args.size());
}
bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
const char *StartSpecifier,
unsigned SpecifierLen) override {
if (!FS.consumesDataArgument() &&
FS.getConversionSpecifier().getKind() !=
clang::analyze_format_string::ConversionSpecifier::PrintErrno)
return true;
ArgsData.emplace_back();
unsigned ArgIndex = FS.getArgIndex();
if (ArgIndex < Args.size())
ArgsData.back().E = Args[ArgIndex];
// First get the Kind
ArgsData.back().Kind = getKind(FS.getConversionSpecifier().getKind());
if (ArgsData.back().Kind != OSLogBufferItem::ErrnoKind &&
!ArgsData.back().E) {
// missing argument
ArgsData.pop_back();
return false;
}
switch (FS.getConversionSpecifier().getKind()) {
case clang::analyze_format_string::ConversionSpecifier::sArg: // "%s"
case clang::analyze_format_string::ConversionSpecifier::SArg: { // "%S"
auto &precision = FS.getPrecision();
switch (precision.getHowSpecified()) {
case clang::analyze_format_string::OptionalAmount::NotSpecified: // "%s"
break;
case clang::analyze_format_string::OptionalAmount::Constant: // "%.16s"
ArgsData.back().Size = precision.getConstantAmount();
break;
case clang::analyze_format_string::OptionalAmount::Arg: // "%.*s"
ArgsData.back().Count = Args[precision.getArgIndex()];
break;
case clang::analyze_format_string::OptionalAmount::Invalid:
return false;
}
break;
}
case clang::analyze_format_string::ConversionSpecifier::PArg: { // "%P"
auto &precision = FS.getPrecision();
switch (precision.getHowSpecified()) {
case clang::analyze_format_string::OptionalAmount::NotSpecified: // "%P"
return false; // length must be supplied with pointer format specifier
case clang::analyze_format_string::OptionalAmount::Constant: // "%.16P"
ArgsData.back().Size = precision.getConstantAmount();
break;
case clang::analyze_format_string::OptionalAmount::Arg: // "%.*P"
ArgsData.back().Count = Args[precision.getArgIndex()];
break;
case clang::analyze_format_string::OptionalAmount::Invalid:
return false;
}
break;
}
default:
if (FS.getPrecision().hasDataArgument()) {
ArgsData.back().Precision = Args[FS.getPrecision().getArgIndex()];
}
break;
}
if (FS.getFieldWidth().hasDataArgument()) {
ArgsData.back().FieldWidth = Args[FS.getFieldWidth().getArgIndex()];
}
if (FS.isSensitive())
ArgsData.back().Flags |= OSLogBufferItem::IsSensitive;
else if (FS.isPrivate())
ArgsData.back().Flags |= OSLogBufferItem::IsPrivate;
else if (FS.isPublic())
ArgsData.back().Flags |= OSLogBufferItem::IsPublic;
ArgsData.back().MaskType = FS.getMaskType();
return true;
}
void computeLayout(ASTContext &Ctx, OSLogBufferLayout &Layout) const {
Layout.Items.clear();
for (auto &Data : ArgsData) {
if (!Data.MaskType.empty()) {
CharUnits Size = CharUnits::fromQuantity(8);
Layout.Items.emplace_back(OSLogBufferItem::MaskKind, nullptr,
Size, 0, Data.MaskType);
}
if (Data.FieldWidth) {
CharUnits Size = Ctx.getTypeSizeInChars((*Data.FieldWidth)->getType());
Layout.Items.emplace_back(OSLogBufferItem::ScalarKind, *Data.FieldWidth,
Size, 0);
}
if (Data.Precision) {
CharUnits Size = Ctx.getTypeSizeInChars((*Data.Precision)->getType());
Layout.Items.emplace_back(OSLogBufferItem::ScalarKind, *Data.Precision,
Size, 0);
}
if (Data.Count) {
// "%.*P" has an extra "count" that we insert before the argument.
CharUnits Size = Ctx.getTypeSizeInChars((*Data.Count)->getType());
Layout.Items.emplace_back(OSLogBufferItem::CountKind, *Data.Count, Size,
0);
}
if (Data.Size)
Layout.Items.emplace_back(Ctx, CharUnits::fromQuantity(*Data.Size),
Data.Flags);
if (Data.Kind) {
CharUnits Size;
if (*Data.Kind == OSLogBufferItem::ErrnoKind)
Size = CharUnits::Zero();
else
Size = Ctx.getTypeSizeInChars(Data.E->getType());
Layout.Items.emplace_back(*Data.Kind, Data.E, Size, Data.Flags);
} else {
auto Size = Ctx.getTypeSizeInChars(Data.E->getType());
Layout.Items.emplace_back(OSLogBufferItem::ScalarKind, Data.E, Size,
Data.Flags);
}
}
}
};
} // end anonymous namespace
bool clang::analyze_os_log::computeOSLogBufferLayout(
ASTContext &Ctx, const CallExpr *E, OSLogBufferLayout &Layout) {
ArrayRef<const Expr *> Args(E->getArgs(), E->getArgs() + E->getNumArgs());
const Expr *StringArg;
ArrayRef<const Expr *> VarArgs;
switch (E->getBuiltinCallee()) {
case Builtin::BI__builtin_os_log_format_buffer_size:
assert(E->getNumArgs() >= 1 &&
"__builtin_os_log_format_buffer_size takes at least 1 argument");
StringArg = E->getArg(0);
VarArgs = Args.slice(1);
break;
case Builtin::BI__builtin_os_log_format:
assert(E->getNumArgs() >= 2 &&
"__builtin_os_log_format takes at least 2 arguments");
StringArg = E->getArg(1);
VarArgs = Args.slice(2);
break;
default:
llvm_unreachable("non-os_log builtin passed to computeOSLogBufferLayout");
}
const StringLiteral *Lit = cast<StringLiteral>(StringArg->IgnoreParenCasts());
assert(Lit && (Lit->isAscii() || Lit->isUTF8()));
StringRef Data = Lit->getString();
OSLogFormatStringHandler H(VarArgs);
ParsePrintfString(H, Data.begin(), Data.end(), Ctx.getLangOpts(),
Ctx.getTargetInfo(), /*isFreeBSDKPrintf*/ false);
H.computeLayout(Ctx, Layout);
return true;
}