1 // Copyright 2016 The Crashpad Authors. 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 "util/win/initial_client_data.h"
16 
17 #include <vector>
18 
19 #include "base/logging.h"
20 #include "base/strings/stringprintf.h"
21 #include "util/stdlib/string_number_conversion.h"
22 #include "util/string/split_string.h"
23 #include "util/win/handle.h"
24 
25 namespace crashpad {
26 
27 namespace {
28 
HandleFromString(const std::string & str,HANDLE * handle)29 bool HandleFromString(const std::string& str, HANDLE* handle) {
30   unsigned int handle_uint;
31   if (!StringToNumber(str, &handle_uint) ||
32       (*handle = IntToHandle(handle_uint)) == INVALID_HANDLE_VALUE) {
33     LOG(ERROR) << "could not convert '" << str << "' to HANDLE";
34     return false;
35   }
36   return true;
37 }
38 
AddressFromString(const std::string & str,WinVMAddress * address)39 bool AddressFromString(const std::string& str, WinVMAddress* address) {
40   if (!StringToNumber(str, address)) {
41     LOG(ERROR) << "could not convert '" << str << "' to WinVMAddress";
42     return false;
43   }
44   return true;
45 }
46 
47 }  // namespace
48 
InitialClientData()49 InitialClientData::InitialClientData()
50     : crash_exception_information_(0),
51       non_crash_exception_information_(0),
52       debug_critical_section_address_(0),
53       request_crash_dump_(nullptr),
54       request_non_crash_dump_(nullptr),
55       non_crash_dump_completed_(nullptr),
56       first_pipe_instance_(INVALID_HANDLE_VALUE),
57       client_process_(nullptr),
58       is_valid_(false) {}
59 
InitialClientData(HANDLE request_crash_dump,HANDLE request_non_crash_dump,HANDLE non_crash_dump_completed,HANDLE first_pipe_instance,HANDLE client_process,WinVMAddress crash_exception_information,WinVMAddress non_crash_exception_information,WinVMAddress debug_critical_section_address)60 InitialClientData::InitialClientData(
61     HANDLE request_crash_dump,
62     HANDLE request_non_crash_dump,
63     HANDLE non_crash_dump_completed,
64     HANDLE first_pipe_instance,
65     HANDLE client_process,
66     WinVMAddress crash_exception_information,
67     WinVMAddress non_crash_exception_information,
68     WinVMAddress debug_critical_section_address)
69     : crash_exception_information_(crash_exception_information),
70       non_crash_exception_information_(non_crash_exception_information),
71       debug_critical_section_address_(debug_critical_section_address),
72       request_crash_dump_(request_crash_dump),
73       request_non_crash_dump_(request_non_crash_dump),
74       non_crash_dump_completed_(non_crash_dump_completed),
75       first_pipe_instance_(first_pipe_instance),
76       client_process_(client_process),
77       is_valid_(true) {}
78 
InitializeFromString(const std::string & str)79 bool InitialClientData::InitializeFromString(const std::string& str) {
80   std::vector<std::string> parts(SplitString(str, ','));
81   if (parts.size() != 8) {
82     LOG(ERROR) << "expected 8 comma separated arguments";
83     return false;
84   }
85 
86   if (!HandleFromString(parts[0], &request_crash_dump_) ||
87       !HandleFromString(parts[1], &request_non_crash_dump_) ||
88       !HandleFromString(parts[2], &non_crash_dump_completed_) ||
89       !HandleFromString(parts[3], &first_pipe_instance_) ||
90       !HandleFromString(parts[4], &client_process_) ||
91       !AddressFromString(parts[5], &crash_exception_information_) ||
92       !AddressFromString(parts[6], &non_crash_exception_information_) ||
93       !AddressFromString(parts[7], &debug_critical_section_address_)) {
94     return false;
95   }
96 
97   is_valid_ = true;
98   return true;
99 }
100 
StringRepresentation() const101 std::string InitialClientData::StringRepresentation() const {
102   return base::StringPrintf("0x%x,0x%x,0x%x,0x%x,0x%x,0x%I64x,0x%I64x,0x%I64x",
103                             HandleToInt(request_crash_dump_),
104                             HandleToInt(request_non_crash_dump_),
105                             HandleToInt(non_crash_dump_completed_),
106                             HandleToInt(first_pipe_instance_),
107                             HandleToInt(client_process_),
108                             crash_exception_information_,
109                             non_crash_exception_information_,
110                             debug_critical_section_address_);
111 }
112 
113 }  // namespace crashpad
114