File: CheckIPCVisitor.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,362 bytes parent folder | download | duplicates (9)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This check ensures that 32/64-bit unstable types are not used in IPC.
//
// A type (or typedef) is unstable if it changes size between 32/ 64-bit
// platforms. However, it's impossible to accurately identify unstable
// typedefs, because their definitions rely on the preprocessor. For
// example uintptr_t is either unsigned int or unsigned long.
//
// So we're not trying to be accurate, and just blocklisting some types
// that are known to be unstable:
// 1. Types: long / unsigned long (but not typedefs to)
// 2. Typedefs: intmax_t, uintmax_t, intptr_t, uintptr_t, wint_t,
//    size_t, rsize_t, ssize_t, ptrdiff_t, dev_t, off_t, clock_t,
//    time_t, suseconds_t (including typedefs to)
//
// Additionally, templates referencing blocklisted types (e.g. vector<long>)
// are also blocklisted.
//
// Blacklisted types are checked in:
// 1. IPC::WriteParam() calls
// 2. IPC::CheckedTuple<> specializations
//

#ifndef TOOLS_CLANG_PLUGINS_CHECKIPC_VISITOR_H_
#define TOOLS_CLANG_PLUGINS_CHECKIPC_VISITOR_H_

#include <vector>

#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/ADT/StringSet.h"

namespace chrome_checker {

class CheckIPCVisitor {
 public:
  explicit CheckIPCVisitor(clang::CompilerInstance& compiler);

  void set_context(clang::ASTContext* context) { context_ = context; }

  void BeginDecl(clang::Decl* decl);
  void EndDecl();
  void VisitTemplateSpecializationType(
      clang::TemplateSpecializationType* spec);
  void VisitCallExpr(clang::CallExpr* call_expr);

 private:
  // ValidateXXX functions return false if validation failed and diagnostic
  // was reported. They return true otherwise (not applicable / validation
  // succeeded).

  bool ValidateWriteParam(const clang::CallExpr* call_expr);
  bool ValidateWriteParamSignature(const clang::CallExpr* call_expr);
  bool ValidateWriteParamArgument(const clang::Expr* arg_expr);
  bool ValidateCheckedTuple(
      const clang::TemplateSpecializationType* spec);

  template <typename T>
  const T* GetParentDecl() const;

  bool IsBlacklistedType(clang::QualType type) const;
  bool IsBlacklistedTypedef(const clang::TypedefNameDecl* tdef) const;

  struct CheckDetails {
    clang::QualType entry_type;
    clang::QualType exit_type;
    llvm::SmallVector<const clang::TypedefType*, 5> typedefs;
  };

  bool CheckType(clang::QualType type, CheckDetails* details) const;
  bool CheckIntegerType(clang::QualType type, CheckDetails* details) const;
  bool CheckTemplateArgument(const clang::TemplateArgument& arg,
                             CheckDetails* details) const;

  void ReportCheckError(const CheckDetails& details,
                        clang::SourceLocation loc,
                        unsigned error);

  clang::CompilerInstance& compiler_;
  clang::ASTContext* context_;

  unsigned error_write_param_bad_type_;
  unsigned error_tuple_bad_type_;
  unsigned error_write_param_bad_signature_;
  unsigned note_see_here_;

  std::vector<const clang::Decl*> decl_stack_;

  llvm::StringSet<> blocklisted_typedefs_;
};

}  // namespace chrome_checker

#endif  // TOOLS_CLANG_PLUGINS_CHECKIPC_VISITOR_H_