aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/bugprone/SignalHandlerCheck.cpp
blob: a0b2afdb131032c9b8c52f431b9f8e26b83b3e0c (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//===--- SignalHandlerCheck.cpp - clang-tidy ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "SignalHandlerCheck.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/STLExtras.h"

// This is the minimal set of safe functions.
// https://wiki.sei.cmu.edu/confluence/display/c/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
constexpr llvm::StringLiteral MinimalConformingFunctions[] = {
    "signal", "abort", "_Exit", "quick_exit"};

// The POSIX-defined set of safe functions.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03
// 'quick_exit' is added to the set additionally because it looks like the
// mentioned POSIX specification was not updated after 'quick_exit' appeared
// in the C11 standard.
// Also, we want to keep the "minimal set" a subset of the "POSIX set".
// The list is repeated in bugprone-signal-handler.rst and should be kept up to date.
constexpr llvm::StringLiteral POSIXConformingFunctions[] = {
    "_Exit",
    "_exit",
    "abort",
    "accept",
    "access",
    "aio_error",
    "aio_return",
    "aio_suspend",
    "alarm",
    "bind",
    "cfgetispeed",
    "cfgetospeed",
    "cfsetispeed",
    "cfsetospeed",
    "chdir",
    "chmod",
    "chown",
    "clock_gettime",
    "close",
    "connect",
    "creat",
    "dup",
    "dup2",
    "execl",
    "execle",
    "execv",
    "execve",
    "faccessat",
    "fchdir",
    "fchmod",
    "fchmodat",
    "fchown",
    "fchownat",
    "fcntl",
    "fdatasync",
    "fexecve",
    "ffs",
    "fork",
    "fstat",
    "fstatat",
    "fsync",
    "ftruncate",
    "futimens",
    "getegid",
    "geteuid",
    "getgid",
    "getgroups",
    "getpeername",
    "getpgrp",
    "getpid",
    "getppid",
    "getsockname",
    "getsockopt",
    "getuid",
    "htonl",
    "htons",
    "kill",
    "link",
    "linkat",
    "listen",
    "longjmp",
    "lseek",
    "lstat",
    "memccpy",
    "memchr",
    "memcmp",
    "memcpy",
    "memmove",
    "memset",
    "mkdir",
    "mkdirat",
    "mkfifo",
    "mkfifoat",
    "mknod",
    "mknodat",
    "ntohl",
    "ntohs",
    "open",
    "openat",
    "pause",
    "pipe",
    "poll",
    "posix_trace_event",
    "pselect",
    "pthread_kill",
    "pthread_self",
    "pthread_sigmask",
    "quick_exit",
    "raise",
    "read",
    "readlink",
    "readlinkat",
    "recv",
    "recvfrom",
    "recvmsg",
    "rename",
    "renameat",
    "rmdir",
    "select",
    "sem_post",
    "send",
    "sendmsg",
    "sendto",
    "setgid",
    "setpgid",
    "setsid",
    "setsockopt",
    "setuid",
    "shutdown",
    "sigaction",
    "sigaddset",
    "sigdelset",
    "sigemptyset",
    "sigfillset",
    "sigismember",
    "siglongjmp",
    "signal",
    "sigpause",
    "sigpending",
    "sigprocmask",
    "sigqueue",
    "sigset",
    "sigsuspend",
    "sleep",
    "sockatmark",
    "socket",
    "socketpair",
    "stat",
    "stpcpy",
    "stpncpy",
    "strcat",
    "strchr",
    "strcmp",
    "strcpy",
    "strcspn",
    "strlen",
    "strncat",
    "strncmp",
    "strncpy",
    "strnlen",
    "strpbrk",
    "strrchr",
    "strspn",
    "strstr",
    "strtok_r",
    "symlink",
    "symlinkat",
    "tcdrain",
    "tcflow",
    "tcflush",
    "tcgetattr",
    "tcgetpgrp",
    "tcsendbreak",
    "tcsetattr",
    "tcsetpgrp",
    "time",
    "timer_getoverrun",
    "timer_gettime",
    "timer_settime",
    "times",
    "umask",
    "uname",
    "unlink",
    "unlinkat",
    "utime",
    "utimensat",
    "utimes",
    "wait",
    "waitpid",
    "wcpcpy",
    "wcpncpy",
    "wcscat",
    "wcschr",
    "wcscmp",
    "wcscpy",
    "wcscspn",
    "wcslen",
    "wcsncat",
    "wcsncmp",
    "wcsncpy",
    "wcsnlen",
    "wcspbrk",
    "wcsrchr",
    "wcsspn",
    "wcsstr",
    "wcstok",
    "wmemchr",
    "wmemcmp",
    "wmemcpy",
    "wmemmove",
    "wmemset",
    "write"};

using namespace clang::ast_matchers;

namespace clang::tidy {

template <>
struct OptionEnumMapping<
    bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind> {
  static llvm::ArrayRef<std::pair<
      bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind, StringRef>>
  getEnumMapping() {
    static constexpr std::pair<
        bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind, StringRef>
        Mapping[] = {
            {bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind::Minimal,
             "minimal"},
            {bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind::POSIX,
             "POSIX"},
        };
    return ArrayRef(Mapping);
  }
};

namespace bugprone {

namespace {

/// Returns if a function is declared inside a system header.
/// These functions are considered to be "standard" (system-provided) library
/// functions.
bool isStandardFunction(const FunctionDecl *FD) {
  // Find a possible redeclaration in system header.
  // FIXME: Looking at the canonical declaration is not the most exact way
  // to do this.

  // Most common case will be inclusion directly from a header.
  // This works fine by using canonical declaration.
  // a.c
  // #include <sysheader.h>

  // Next most common case will be extern declaration.
  // Can't catch this with either approach.
  // b.c
  // extern void sysfunc(void);

  // Canonical declaration is the first found declaration, so this works.
  // c.c
  // #include <sysheader.h>
  // extern void sysfunc(void); // redecl won't matter

  // This does not work with canonical declaration.
  // Probably this is not a frequently used case but may happen (the first
  // declaration can be in a non-system header for example).
  // d.c
  // extern void sysfunc(void); // Canonical declaration, not in system header.
  // #include <sysheader.h>

  return FD->getASTContext().getSourceManager().isInSystemHeader(
      FD->getCanonicalDecl()->getLocation());
}

/// Check if a statement is "C++-only".
/// This includes all statements that have a class name with "CXX" prefix
/// and every other statement that is declared in file ExprCXX.h.
bool isCXXOnlyStmt(const Stmt *S) {
  StringRef Name = S->getStmtClassName();
  if (Name.startswith("CXX"))
    return true;
  // Check for all other class names in ExprCXX.h that have no 'CXX' prefix.
  return isa<ArrayTypeTraitExpr, BuiltinBitCastExpr, CUDAKernelCallExpr,
             CoawaitExpr, CoreturnStmt, CoroutineBodyStmt, CoroutineSuspendExpr,
             CoyieldExpr, DependentCoawaitExpr, DependentScopeDeclRefExpr,
             ExprWithCleanups, ExpressionTraitExpr, FunctionParmPackExpr,
             LambdaExpr, MSDependentExistsStmt, MSPropertyRefExpr,
             MSPropertySubscriptExpr, MaterializeTemporaryExpr, OverloadExpr,
             PackExpansionExpr, SizeOfPackExpr, SubstNonTypeTemplateParmExpr,
             SubstNonTypeTemplateParmPackExpr, TypeTraitExpr,
             UserDefinedLiteral>(S);
}

/// Given a call graph node of a \p Caller function and a \p Callee that is
/// called from \p Caller, get a \c CallExpr of the corresponding function call.
/// It is unspecified which call is found if multiple calls exist, but the order
/// should be deterministic (depend only on the AST).
Expr *findCallExpr(const CallGraphNode *Caller, const CallGraphNode *Callee) {
  auto FoundCallee = llvm::find_if(
      Caller->callees(), [Callee](const CallGraphNode::CallRecord &Call) {
        return Call.Callee == Callee;
      });
  assert(FoundCallee != Caller->end() &&
         "Callee should be called from the caller function here.");
  return FoundCallee->CallExpr;
}

SourceRange getSourceRangeOfStmt(const Stmt *S, ASTContext &Ctx) {
  ParentMapContext &PM = Ctx.getParentMapContext();
  DynTypedNode P = DynTypedNode::create(*S);
  while (P.getSourceRange().isInvalid()) {
    DynTypedNodeList PL = PM.getParents(P);
    if (PL.size() != 1)
      return {};
    P = PL[0];
  }
  return P.getSourceRange();
}

} // namespace

AST_MATCHER(FunctionDecl, isStandardFunction) {
  return isStandardFunction(&Node);
}

SignalHandlerCheck::SignalHandlerCheck(StringRef Name,
                                       ClangTidyContext *Context)
    : ClangTidyCheck(Name, Context),
      AsyncSafeFunctionSet(Options.get("AsyncSafeFunctionSet",
                                       AsyncSafeFunctionSetKind::POSIX)) {
  if (AsyncSafeFunctionSet == AsyncSafeFunctionSetKind::Minimal) {
    for (StringRef v : MinimalConformingFunctions)
      ConformingFunctions.insert(v);
  } else {
    for (StringRef v : POSIXConformingFunctions)
      ConformingFunctions.insert(v);
  }
}

void SignalHandlerCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  Options.store(Opts, "AsyncSafeFunctionSet", AsyncSafeFunctionSet);
}

bool SignalHandlerCheck::isLanguageVersionSupported(
    const LangOptions &LangOpts) const {
  return !LangOpts.CPlusPlus17;
}

void SignalHandlerCheck::registerMatchers(MatchFinder *Finder) {
  auto SignalFunction = functionDecl(hasAnyName("::signal", "::std::signal"),
                                     parameterCountIs(2), isStandardFunction());
  auto HandlerExpr =
      declRefExpr(hasDeclaration(functionDecl().bind("handler_decl")),
                  unless(isExpandedFromMacro("SIG_IGN")),
                  unless(isExpandedFromMacro("SIG_DFL")))
          .bind("handler_expr");
  auto HandlerLambda = cxxMemberCallExpr(
      on(expr(ignoringParenImpCasts(lambdaExpr().bind("handler_lambda")))));
  Finder->addMatcher(callExpr(callee(SignalFunction),
                              hasArgument(1, anyOf(HandlerExpr, HandlerLambda)))
                         .bind("register_call"),
                     this);
}

void SignalHandlerCheck::check(const MatchFinder::MatchResult &Result) {
  if (const auto *HandlerLambda =
          Result.Nodes.getNodeAs<LambdaExpr>("handler_lambda")) {
    diag(HandlerLambda->getBeginLoc(),
         "lambda function is not allowed as signal handler (until C++17)")
        << HandlerLambda->getSourceRange();
    return;
  }

  const auto *HandlerDecl =
      Result.Nodes.getNodeAs<FunctionDecl>("handler_decl");
  const auto *HandlerExpr = Result.Nodes.getNodeAs<DeclRefExpr>("handler_expr");
  assert(Result.Nodes.getNodeAs<CallExpr>("register_call") && HandlerDecl &&
         HandlerExpr && "All of these should exist in a match here.");

  if (CG.size() <= 1) {
    // Call graph must be populated with the entire TU at the beginning.
    // (It is possible to add a single function but the functions called from it
    // are not analysed in this case.)
    CG.addToCallGraph(const_cast<TranslationUnitDecl *>(
        HandlerDecl->getTranslationUnitDecl()));
    assert(CG.size() > 1 &&
           "There should be at least one function added to call graph.");
  }

  if (!HandlerDecl->hasBody()) {
    // Check the handler function.
    // The warning is placed to the signal handler registration.
    // No need to display a call chain and no need for more checks.
    (void)checkFunction(HandlerDecl, HandlerExpr, {});
    return;
  }

  // FIXME: Update CallGraph::getNode to use canonical decl?
  CallGraphNode *HandlerNode = CG.getNode(HandlerDecl->getCanonicalDecl());
  assert(HandlerNode &&
         "Handler with body should be present in the call graph.");
  // Start from signal handler and visit every function call.
  auto Itr = llvm::df_begin(HandlerNode), ItrE = llvm::df_end(HandlerNode);
  while (Itr != ItrE) {
    const auto *CallF = dyn_cast<FunctionDecl>((*Itr)->getDecl());
    unsigned int PathL = Itr.getPathLength();
    if (CallF) {
      // A signal handler or a function transitively reachable from the signal
      // handler was found to be unsafe.
      // Generate notes for the whole call chain (including the signal handler
      // registration).
      const Expr *CallOrRef = (PathL > 1)
                                  ? findCallExpr(Itr.getPath(PathL - 2), *Itr)
                                  : HandlerExpr;
      auto ChainReporter = [this, &Itr, HandlerExpr](bool SkipPathEnd) {
        reportHandlerChain(Itr, HandlerExpr, SkipPathEnd);
      };
      // If problems were found in a function (`CallF`), skip the analysis of
      // functions that are called from it.
      if (checkFunction(CallF, CallOrRef, ChainReporter))
        Itr.skipChildren();
      else
        ++Itr;
    } else {
      ++Itr;
    }
  }
}

bool SignalHandlerCheck::checkFunction(
    const FunctionDecl *FD, const Expr *CallOrRef,
    std::function<void(bool)> ChainReporter) {
  bool FunctionIsCalled = isa<CallExpr>(CallOrRef);

  if (isStandardFunction(FD)) {
    if (!isStandardFunctionAsyncSafe(FD)) {
      diag(CallOrRef->getBeginLoc(), "standard function %0 may not be "
                                     "asynchronous-safe; "
                                     "%select{using it as|calling it from}1 "
                                     "a signal handler may be dangerous")
          << FD << FunctionIsCalled << CallOrRef->getSourceRange();
      if (ChainReporter)
        ChainReporter(/*SkipPathEnd=*/true);
      return true;
    }
    return false;
  }

  if (!FD->hasBody()) {
    diag(CallOrRef->getBeginLoc(), "cannot verify that external function %0 is "
                                   "asynchronous-safe; "
                                   "%select{using it as|calling it from}1 "
                                   "a signal handler may be dangerous")
        << FD << FunctionIsCalled << CallOrRef->getSourceRange();
    if (ChainReporter)
      ChainReporter(/*SkipPathEnd=*/true);
    return true;
  }

  if (getLangOpts().CPlusPlus)
    return checkFunctionCPP14(FD, CallOrRef, ChainReporter);

  return false;
}

bool SignalHandlerCheck::checkFunctionCPP14(
    const FunctionDecl *FD, const Expr *CallOrRef,
    std::function<void(bool)> ChainReporter) {
  if (!FD->isExternC()) {
    diag(CallOrRef->getBeginLoc(),
         "functions without C linkage are not allowed as signal "
         "handler (until C++17)");
    if (ChainReporter)
      ChainReporter(/*SkipPathEnd=*/true);
    return true;
  }

  const FunctionDecl *FBody;
  const Stmt *BodyS = FD->getBody(FBody);
  if (!BodyS)
    return false;

  bool StmtProblemsFound = false;
  ASTContext &Ctx = FBody->getASTContext();
  auto Matches =
      match(decl(forEachDescendant(stmt().bind("stmt"))), *FBody, Ctx);
  for (const auto &Match : Matches) {
    const auto *FoundS = Match.getNodeAs<Stmt>("stmt");
    if (isCXXOnlyStmt(FoundS)) {
      SourceRange R = getSourceRangeOfStmt(FoundS, Ctx);
      if (R.isInvalid())
        continue;
      diag(R.getBegin(),
           "C++-only construct is not allowed in signal handler (until C++17)")
          << R;
      diag(R.getBegin(), "internally, the statement is parsed as a '%0'",
           DiagnosticIDs::Remark)
          << FoundS->getStmtClassName();
      if (ChainReporter)
        ChainReporter(/*SkipPathEnd=*/false);
      StmtProblemsFound = true;
    }
  }

  return StmtProblemsFound;
}

bool SignalHandlerCheck::isStandardFunctionAsyncSafe(
    const FunctionDecl *FD) const {
  assert(isStandardFunction(FD));

  const IdentifierInfo *II = FD->getIdentifier();
  // Unnamed functions are not explicitly allowed.
  // C++ std operators may be unsafe and not within the
  // "common subset of C and C++".
  if (!II)
    return false;

  if (!FD->isInStdNamespace() && !FD->isGlobal())
    return false;

  if (ConformingFunctions.count(II->getName()))
    return true;

  return false;
}

void SignalHandlerCheck::reportHandlerChain(
    const llvm::df_iterator<clang::CallGraphNode *> &Itr,
    const DeclRefExpr *HandlerRef, bool SkipPathEnd) {
  int CallLevel = Itr.getPathLength() - 2;
  assert(CallLevel >= -1 && "Empty iterator?");

  const CallGraphNode *Caller = Itr.getPath(CallLevel + 1), *Callee = nullptr;
  while (CallLevel >= 0) {
    Callee = Caller;
    Caller = Itr.getPath(CallLevel);
    const Expr *CE = findCallExpr(Caller, Callee);
    if (SkipPathEnd)
      SkipPathEnd = false;
    else
      diag(CE->getBeginLoc(), "function %0 called here from %1",
           DiagnosticIDs::Note)
          << cast<FunctionDecl>(Callee->getDecl())
          << cast<FunctionDecl>(Caller->getDecl());
    --CallLevel;
  }

  if (!SkipPathEnd)
    diag(HandlerRef->getBeginLoc(),
         "function %0 registered here as signal handler", DiagnosticIDs::Note)
        << cast<FunctionDecl>(Caller->getDecl())
        << HandlerRef->getSourceRange();
}

} // namespace bugprone
} // namespace clang::tidy