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