• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 PLUMgrid, Inc.
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 <map>
18 #include <memory>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include <clang/AST/RecursiveASTVisitor.h>
24 #include <clang/Frontend/FrontendAction.h>
25 #include <clang/Rewrite/Core/Rewriter.h>
26 
27 #include "table_storage.h"
28 
29 namespace clang {
30 class ASTConsumer;
31 class ASTContext;
32 class CompilerInstance;
33 }
34 
35 namespace llvm {
36 class raw_ostream;
37 class StringRef;
38 }
39 
40 namespace ebpf {
41 
42 class BFrontendAction;
43 class FuncSource;
44 
45 // Traces maps with external pointers as values.
46 class MapVisitor : public clang::RecursiveASTVisitor<MapVisitor> {
47  public:
48   explicit MapVisitor(std::set<clang::Decl *> &m);
49   bool VisitCallExpr(clang::CallExpr *Call);
set_ptreg(std::tuple<clang::Decl *,int> & pt)50   void set_ptreg(std::tuple<clang::Decl *, int> &pt) { ptregs_.insert(pt); }
51  private:
52   std::set<clang::Decl *> &m_;
53   std::set<std::tuple<clang::Decl *, int>> ptregs_;
54 };
55 
56 // Type visitor and rewriter for B programs.
57 // It will look for B-specific features and rewrite them into a valid
58 // C program. As part of the processing, open the necessary BPF tables
59 // and store the open handles in a map of table-to-fd's.
60 class BTypeVisitor : public clang::RecursiveASTVisitor<BTypeVisitor> {
61  public:
62   explicit BTypeVisitor(clang::ASTContext &C, BFrontendAction &fe);
63   bool TraverseCallExpr(clang::CallExpr *Call);
64   bool VisitFunctionDecl(clang::FunctionDecl *D);
65   bool VisitCallExpr(clang::CallExpr *Call);
66   bool VisitVarDecl(clang::VarDecl *Decl);
67   bool VisitBinaryOperator(clang::BinaryOperator *E);
68   bool VisitImplicitCastExpr(clang::ImplicitCastExpr *E);
69 
70  private:
71   clang::SourceRange expansionRange(clang::SourceRange range);
72   bool checkFormatSpecifiers(const std::string& fmt, clang::SourceLocation loc);
73   void genParamDirectAssign(clang::FunctionDecl *D, std::string& preamble,
74                             const char **calling_conv_regs);
75   void genParamIndirectAssign(clang::FunctionDecl *D, std::string& preamble,
76                               const char **calling_conv_regs);
77   void rewriteFuncParam(clang::FunctionDecl *D);
78   template <unsigned N>
79   clang::DiagnosticBuilder error(clang::SourceLocation loc, const char (&fmt)[N]);
80   template <unsigned N>
81   clang::DiagnosticBuilder warning(clang::SourceLocation loc, const char (&fmt)[N]);
82 
83   clang::ASTContext &C;
84   clang::DiagnosticsEngine &diag_;
85   BFrontendAction &fe_;
86   clang::Rewriter &rewriter_;  /// modifications to the source go into this class
87   llvm::raw_ostream &out_;  /// for debugging
88   std::vector<clang::ParmVarDecl *> fn_args_;
89   std::set<clang::Expr *> visited_;
90   std::string current_fn_;
91 };
92 
93 // Do a depth-first search to rewrite all pointers that need to be probed
94 class ProbeVisitor : public clang::RecursiveASTVisitor<ProbeVisitor> {
95  public:
96   explicit ProbeVisitor(clang::ASTContext &C, clang::Rewriter &rewriter,
97                         std::set<clang::Decl *> &m, bool track_helpers);
98   bool VisitVarDecl(clang::VarDecl *Decl);
99   bool TraverseStmt(clang::Stmt *S);
100   bool VisitCallExpr(clang::CallExpr *Call);
101   bool VisitReturnStmt(clang::ReturnStmt *R);
102   bool VisitBinaryOperator(clang::BinaryOperator *E);
103   bool VisitUnaryOperator(clang::UnaryOperator *E);
104   bool VisitMemberExpr(clang::MemberExpr *E);
105   bool VisitArraySubscriptExpr(clang::ArraySubscriptExpr *E);
set_ptreg(std::tuple<clang::Decl *,int> & pt)106   void set_ptreg(std::tuple<clang::Decl *, int> &pt) { ptregs_.insert(pt); }
set_ctx(clang::Decl * D)107   void set_ctx(clang::Decl *D) { ctx_ = D; }
get_ptregs()108   std::set<std::tuple<clang::Decl *, int>> get_ptregs() { return ptregs_; }
109  private:
110   bool assignsExtPtr(clang::Expr *E, int *nbAddrOf);
111   bool isMemberDereference(clang::Expr *E);
112   bool IsContextMemberExpr(clang::Expr *E);
113   clang::SourceRange expansionRange(clang::SourceRange range);
114   clang::SourceLocation expansionLoc(clang::SourceLocation loc);
115   template <unsigned N>
116   clang::DiagnosticBuilder error(clang::SourceLocation loc, const char (&fmt)[N]);
117 
118   clang::ASTContext &C;
119   clang::Rewriter &rewriter_;
120   std::set<clang::Decl *> fn_visited_;
121   std::set<clang::Expr *> memb_visited_;
122   std::set<const clang::Stmt *> whitelist_;
123   std::set<std::tuple<clang::Decl *, int>> ptregs_;
124   std::set<clang::Decl *> &m_;
125   clang::Decl *ctx_;
126   bool track_helpers_;
127   std::list<int> ptregs_returned_;
128   const clang::Stmt *addrof_stmt_;
129   bool is_addrof_;
130 };
131 
132 // A helper class to the frontend action, walks the decls
133 class BTypeConsumer : public clang::ASTConsumer {
134  public:
135   explicit BTypeConsumer(clang::ASTContext &C, BFrontendAction &fe,
136                          clang::Rewriter &rewriter, std::set<clang::Decl *> &m);
137   void HandleTranslationUnit(clang::ASTContext &Context) override;
138  private:
139   BFrontendAction &fe_;
140   MapVisitor map_visitor_;
141   BTypeVisitor btype_visitor_;
142   ProbeVisitor probe_visitor1_;
143   ProbeVisitor probe_visitor2_;
144 };
145 
146 // Create a B program in 2 phases (everything else is normal C frontend):
147 // 1. Catch the map declarations and open the fd's
148 // 2. Capture the IR
149 class BFrontendAction : public clang::ASTFrontendAction {
150  public:
151   // Initialize with the output stream where the new source file contents
152   // should be written.
153   BFrontendAction(llvm::raw_ostream &os, unsigned flags, TableStorage &ts,
154                   const std::string &id, const std::string &main_path,
155                   FuncSource &func_src, std::string &mod_src,
156                   const std::string &maps_ns);
157 
158   // Called by clang when the AST has been completed, here the output stream
159   // will be flushed.
160   void EndSourceFileAction() override;
161 
162   std::unique_ptr<clang::ASTConsumer>
163       CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) override;
164 
rewriter()165   clang::Rewriter &rewriter() const { return *rewriter_; }
table_storage()166   TableStorage &table_storage() const { return ts_; }
id()167   std::string id() const { return id_; }
maps_ns()168   std::string maps_ns() const { return maps_ns_; }
169   bool is_rewritable_ext_func(clang::FunctionDecl *D);
170   void DoMiscWorkAround();
171 
172  private:
173   llvm::raw_ostream &os_;
174   unsigned flags_;
175   TableStorage &ts_;
176   std::string id_;
177   std::string maps_ns_;
178   std::unique_ptr<clang::Rewriter> rewriter_;
179   friend class BTypeVisitor;
180   std::map<std::string, clang::SourceRange> func_range_;
181   const std::string &main_path_;
182   FuncSource &func_src_;
183   std::string &mod_src_;
184   std::set<clang::Decl *> m_;
185 };
186 
187 }  // namespace visitor
188