1 //===- CheriSetBounds.h - Functions to log information on CSetBounds ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions perform various local transformations to the
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_CHERISETBOUNDS_H
16 #define LLVM_SUPPORT_CHERISETBOUNDS_H
17 
18 #include "llvm/ADT/FunctionExtras.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/Alignment.h"
24 
25 namespace llvm {
26 
27 class Value;
28 class Instruction;
29 
30 namespace cheri {
31 
32 class StatsOutputFile {
33   std::unique_ptr<llvm::raw_fd_ostream> Stream;
34   int FD;
StatsOutputFile(int FD,std::unique_ptr<llvm::raw_fd_ostream> S)35   explicit StatsOutputFile(int FD, std::unique_ptr<llvm::raw_fd_ostream> S)
36       : Stream(std::move(S)), FD(FD) {}
37 
38 public:
39   using ErrorCallback = llvm::unique_function<void(StringRef, const std::error_code &)>;
stream()40   llvm::raw_fd_ostream &stream() { return *Stream; }
41   uint64_t size();
42   ~StatsOutputFile();
43   static std::unique_ptr<StatsOutputFile>
44   open(StringRef File, ErrorCallback OnOpenError, ErrorCallback OnLockError);
45 };
46 
47 enum class SetBoundsPointerSource {
48   Unknown,
49   Heap,
50   Stack,
51   GlobalVar,
52   CodePointer,
53   SubObject,
54 };
55 
56 class CSetBoundsStatistics {
57 public:
58   struct Entry {
59     Optional<uint64_t> RequestedSize;
60     Optional<uint64_t> RequestedSizeMultipleOf;
61     Align KnownAlignment;
62     SetBoundsPointerSource PointerKind;
63     std::string SourceLocation;
64     std::string Pass;
65     std::string Details;
66   };
67   CSetBoundsStatistics();
68   ~CSetBoundsStatistics();
69 
70   void add(Align KnownAlignment, Optional<uint64_t> Length, StringRef Pass,
71            SetBoundsPointerSource Kind, const Twine &Details,
72            std::string SourceLoc, Optional<uint64_t> SizeMultipleOf = None);
73   void print(llvm::raw_ostream &OS, StringRef MainFile, bool PrintHeader);
74   void print(StatsOutputFile &S, StringRef MainFile);
75   static StringRef outputFile();
76 
77 private:
78   SmallVector<Entry, 32> Entries;
79 };
80 
81 enum StatsFormat {
82   StatsOff = 0,
83   StatsCSV,
84   StatsJSON,
85 };
86 
87 extern StatsFormat ShouldCollectCSetBoundsStats;
88 extern ManagedStatic<CSetBoundsStatistics> CSetBoundsStats;
89 
90 } // end namespace cheri
91 
92 } // end namespace llvm
93 
94 #endif // LLVM_SUPPORT_CHERISETBOUNDS_H
95