1 : // Copyright 2015 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 : #include "syzygy/refinery/analyzers/exception_analyzer.h"
16 :
17 : #include <dbghelp.h>
18 : #include <memory>
19 :
20 : #include "syzygy/refinery/analyzers/analyzer_util.h"
21 : #include "syzygy/refinery/process_state/process_state_util.h"
22 : #include "syzygy/refinery/process_state/refinery.pb.h"
23 :
24 m : namespace refinery {
25 :
26 : // static
27 m : const char ExceptionAnalyzer::kExceptionAnalyzerName[] = "ExceptionAnalyzer";
28 :
29 m : Analyzer::AnalysisResult ExceptionAnalyzer::Analyze(
30 m : const minidump::Minidump& minidump,
31 m : const ProcessAnalysis& process_analysis) {
32 m : DCHECK(process_analysis.process_state() != nullptr);
33 :
34 : // Retrieve the unique exception stream.
35 m : minidump::Minidump::Stream exception_stream =
36 m : minidump.FindNextStream(nullptr, ExceptionStream);
37 m : if (!exception_stream.IsValid()) {
38 : // Minidump has no exception data.
39 m : return ANALYSIS_COMPLETE;
40 m : }
41 m : minidump::Minidump::Stream offending_stream =
42 m : minidump.FindNextStream(&exception_stream, ExceptionStream);
43 m : if (offending_stream.IsValid())
44 m : return ANALYSIS_ERROR;
45 :
46 m : MINIDUMP_EXCEPTION_STREAM minidump_exception_stream = {};
47 m : if (!exception_stream.ReadAndAdvanceElement(&minidump_exception_stream))
48 m : return ANALYSIS_ERROR;
49 m : const MINIDUMP_EXCEPTION& exception_record =
50 m : minidump_exception_stream.ExceptionRecord;
51 :
52 : // TODO(manzagop): Consider chained exceptions
53 : // (exception_record.ExceptionRecord).
54 :
55 : // Populate the exception information.
56 m : Exception exception;
57 m : exception.set_thread_id(minidump_exception_stream.ThreadId);
58 m : exception.set_exception_code(exception_record.ExceptionCode);
59 m : exception.set_exception_flags(exception_record.ExceptionFlags);
60 m : exception.set_exception_record(exception_record.ExceptionRecord);
61 m : exception.set_exception_address(exception_record.ExceptionAddress);
62 m : for (int i = 0; i < exception_record.NumberParameters; ++i) {
63 m : exception.add_exception_information(
64 m : exception_record.ExceptionInformation[i]);
65 m : }
66 :
67 m : minidump::Minidump::Stream thread_context =
68 m : minidump.GetStreamFor(minidump_exception_stream.ThreadContext);
69 m : if (!thread_context.IsValid())
70 m : return ANALYSIS_ERROR;
71 : // TODO(siggi): This ought to probe for the architecture somehow.
72 m : CONTEXT ctx = {};
73 m : if (!thread_context.ReadAndAdvanceElement(&ctx))
74 m : return ANALYSIS_ERROR;
75 m : ParseContext(ctx, exception.mutable_register_info());
76 :
77 : // Add the exception information to the process state.
78 m : if (!process_analysis.process_state()->SetException(exception))
79 m : return ANALYSIS_ERROR;
80 :
81 m : return ANALYSIS_COMPLETE;
82 m : }
83 :
84 m : } // namespace refinery
|