1 : // Copyright 2016 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_AGENT_ASAN_REPORTERS_KASKO_REPORTER_H_
16 : #define SYZYGY_AGENT_ASAN_REPORTERS_KASKO_REPORTER_H_
17 :
18 : #include <windows.h>
19 : #include <memory>
20 :
21 : #include "syzygy/agent/asan/reporter.h"
22 : #include "syzygy/agent/asan/reporters/exported_function.h"
23 :
24 : namespace agent {
25 : namespace asan {
26 : namespace reporters {
27 :
28 : // Implements Kasko crash reporting integration.
29 : class KaskoReporter : public ReporterInterface {
30 : public:
31 : // Exported functions that are used for Kasko crash reporting integration.
32 : using ReportCrashWithProtobuf = ExportedFunction<
33 : void __cdecl(EXCEPTION_POINTERS* info,
34 : const char* protobuf,
35 : size_t protobuf_length)>;
36 : using ReportCrashWithProtobufAndMemoryRanges = ExportedFunction<
37 : void __cdecl(EXCEPTION_POINTERS* info,
38 : const char* protobuf,
39 : size_t protobuf_length,
40 : const void* const* base_addresses,
41 : const size_t* lengths)>;
42 : using SetCrashKeyValueImpl = ExportedFunction<
43 : void __cdecl(const wchar_t* key, const wchar_t* value)>;
44 :
45 : // Expected Kasko crash reporter functions. This allows functions
46 : // to be injected for testing.
47 : struct KaskoFunctions {
48 : ReportCrashWithProtobuf report_crash_with_protobuf;
49 : ReportCrashWithProtobufAndMemoryRanges
50 : report_crash_with_protobuf_and_memory_ranges;
51 : SetCrashKeyValueImpl set_crash_key_value_impl;
52 : };
53 :
54 : // Factory for a KaskoReporter. This returns null if the running process
55 : // does not support Kasko crash reporting. Support is decided by examining
56 : // the exports of the running executable, and looking for Kasko's expected
57 : // exports.
58 : // @returns an allocated KaskoReporter
59 : static std::unique_ptr<KaskoReporter> Create();
60 :
61 : // Helper to determine if a given set of functions is valid.
62 : // @param kasko_functions The functions to evaluate.
63 : // @returns true on success, false otherwise.
64 : static bool AreValid(const KaskoFunctions& kasko_functions);
65 :
66 : // Constructor with specified functions.
67 E : explicit KaskoReporter(const KaskoFunctions& kasko_functions)
68 E : : kasko_functions_(kasko_functions) {
69 E : DCHECK(AreValid(kasko_functions_));
70 E : }
71 :
72 E : virtual ~KaskoReporter() {}
73 :
74 : // @name ReporterInterface implementation.
75 : // @{
76 : const char* GetName() const override;
77 : uint32_t GetFeatures() const override;
78 : bool SetCrashKey(base::StringPiece key, base::StringPiece value) override;
79 : bool SetMemoryRanges(const MemoryRanges& memory_ranges) override;
80 : bool SetCustomStream(uint32_t stream_type,
81 : const uint8_t* stream_data,
82 : size_t stream_length) override;
83 : void DumpAndCrash(EXCEPTION_POINTERS* exception_pointers) override;
84 : bool DumpWithoutCrash(const CONTEXT& context) override;
85 : // @}
86 :
87 : protected:
88 : friend class KaskoReporterTest;
89 :
90 : // Returns true if the instrumented application supports early crash keys.
91 : // Visible for testing.
92 : static bool SupportsEarlyCrashKeys();
93 :
94 : // The kasko functions to use.
95 : KaskoFunctions kasko_functions_;
96 :
97 : // Memory ranges set by SetMemoryRanges. These are unfolded into the format
98 : // expected by Kasko.
99 : std::vector<const void*> range_bases_;
100 : std::vector<size_t> range_lengths_;
101 :
102 : // Stores the serialized crash data protobuf to be added to the crash report.
103 : // Set by SetCustomStream, but if and only if called with |stream_type| ==
104 : // kCrashdataProtobufStreamType.
105 : std::string protobuf_;
106 :
107 : private:
108 : DISALLOW_COPY_AND_ASSIGN(KaskoReporter);
109 : };
110 :
111 : } // namespace reporters
112 : } // namespace asan
113 : } // namespace agent
114 :
115 : #endif // SYZYGY_AGENT_ASAN_REPORTERS_KASKO_REPORTER_H_
|