Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: net/base/cert_verifier.cc

Issue 5347001: Fix shutdown crash in CertVerifier by using a MessageLoopProxy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address wtc's comments. Whitelist another MessageLoop::current() use. Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/cert_verifier.h" 5 #include "net/base/cert_verifier.h"
6 6
7 #if defined(USE_NSS) 7 #if defined(USE_NSS)
8 #include <private/pprthred.h> // PR_DetatchThread 8 #include <private/pprthred.h> // PR_DetatchThread
9 #endif 9 #endif
10 10
11 #include "base/message_loop.h" 11 #include "base/message_loop_proxy.h"
12 #include "base/scoped_ptr.h"
12 #include "base/worker_pool.h" 13 #include "base/worker_pool.h"
13 #include "net/base/cert_verify_result.h" 14 #include "net/base/cert_verify_result.h"
14 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
15 #include "net/base/x509_certificate.h" 16 #include "net/base/x509_certificate.h"
16 17
17 namespace net { 18 namespace net {
18 19
19 class CertVerifier::Request : 20 class CertVerifier::Request :
20 public base::RefCountedThreadSafe<CertVerifier::Request> { 21 public base::RefCountedThreadSafe<CertVerifier::Request> {
21 public: 22 public:
22 Request(CertVerifier* verifier, 23 Request(CertVerifier* verifier,
23 X509Certificate* cert, 24 X509Certificate* cert,
24 const std::string& hostname, 25 const std::string& hostname,
25 int flags, 26 int flags,
26 CertVerifyResult* verify_result, 27 CertVerifyResult* verify_result,
27 CompletionCallback* callback) 28 CompletionCallback* callback)
28 : cert_(cert), 29 : cert_(cert),
29 hostname_(hostname), 30 hostname_(hostname),
30 flags_(flags), 31 flags_(flags),
31 verifier_(verifier), 32 verifier_(verifier),
32 verify_result_(verify_result), 33 verify_result_(verify_result),
33 callback_(callback), 34 callback_(callback),
34 origin_loop_(MessageLoop::current()), 35 origin_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()),
35 error_(OK) { 36 error_(OK) {
36 } 37 }
37 38
38 void DoVerify() { 39 void DoVerify() {
39 // Running on the worker thread 40 // Running on the worker thread
40 error_ = cert_->Verify(hostname_, flags_, &result_); 41 error_ = cert_->Verify(hostname_, flags_, &result_);
41 #if defined(USE_NSS) 42 #if defined(USE_NSS)
42 // Detach the thread from NSPR. 43 // Detach the thread from NSPR.
43 // Calling NSS functions attaches the thread to NSPR, which stores 44 // Calling NSS functions attaches the thread to NSPR, which stores
44 // the NSPR thread ID in thread-specific data. 45 // the NSPR thread ID in thread-specific data.
45 // The threads in our thread pool terminate after we have called 46 // The threads in our thread pool terminate after we have called
46 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets 47 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets
47 // segfaults on shutdown when the threads' thread-specific data 48 // segfaults on shutdown when the threads' thread-specific data
48 // destructors run. 49 // destructors run.
49 PR_DetachThread(); 50 PR_DetachThread();
50 #endif 51 #endif
51 52
52 Task* reply = NewRunnableMethod(this, &Request::DoCallback); 53 scoped_ptr<Task> reply(NewRunnableMethod(this, &Request::DoCallback));
53 54
54 // The origin loop could go away while we are trying to post to it, so we 55 // The origin loop could go away while we are trying to post to it, so we
55 // need to call its PostTask method inside a lock. See ~CertVerifier. 56 // need to call its PostTask method inside a lock. See ~CertVerifier.
56 { 57 AutoLock locked(origin_loop_proxy_lock_);
57 AutoLock locked(origin_loop_lock_); 58 if (origin_loop_proxy_) {
58 if (origin_loop_) { 59 bool posted = origin_loop_proxy_->PostTask(FROM_HERE, reply.release());
59 origin_loop_->PostTask(FROM_HERE, reply); 60 // TODO(willchan): Fix leaks and then change this to a DCHECK.
60 reply = NULL; 61 LOG_IF(ERROR, !posted) << "Leaked CertVerifier!";
61 }
62 } 62 }
63
64 // Does nothing if it got posted.
65 delete reply;
66 } 63 }
67 64
68 void DoCallback() { 65 void DoCallback() {
69 // Running on the origin thread. 66 // Running on the origin thread.
70 67
71 // We may have been cancelled! 68 // We may have been cancelled!
72 if (!verifier_) 69 if (!verifier_)
73 return; 70 return;
74 71
75 *verify_result_ = result_; 72 *verify_result_ = result_;
76 73
77 // Drop the verifier's reference to us. Do this before running the 74 // Drop the verifier's reference to us. Do this before running the
78 // callback since the callback might result in the verifier being 75 // callback since the callback might result in the verifier being
79 // destroyed. 76 // destroyed.
80 verifier_->request_ = NULL; 77 verifier_->request_ = NULL;
81 78
82 callback_->Run(error_); 79 callback_->Run(error_);
83 } 80 }
84 81
85 void Cancel() { 82 void Cancel() {
86 verifier_ = NULL; 83 verifier_ = NULL;
87 84
88 AutoLock locked(origin_loop_lock_); 85 AutoLock locked(origin_loop_proxy_lock_);
89 origin_loop_ = NULL; 86 origin_loop_proxy_ = NULL;
90 } 87 }
91 88
92 private: 89 private:
93 friend class base::RefCountedThreadSafe<CertVerifier::Request>; 90 friend class base::RefCountedThreadSafe<CertVerifier::Request>;
94 91
95 ~Request() {} 92 ~Request() {}
96 93
97 // Set on the origin thread, read on the worker thread. 94 // Set on the origin thread, read on the worker thread.
98 scoped_refptr<X509Certificate> cert_; 95 scoped_refptr<X509Certificate> cert_;
99 std::string hostname_; 96 std::string hostname_;
100 // bitwise OR'd of X509Certificate::VerifyFlags. 97 // bitwise OR'd of X509Certificate::VerifyFlags.
101 int flags_; 98 int flags_;
102 99
103 // Only used on the origin thread (where Verify was called). 100 // Only used on the origin thread (where Verify was called).
104 CertVerifier* verifier_; 101 CertVerifier* verifier_;
105 CertVerifyResult* verify_result_; 102 CertVerifyResult* verify_result_;
106 CompletionCallback* callback_; 103 CompletionCallback* callback_;
107 104
108 // Used to post ourselves onto the origin thread. 105 // Used to post ourselves onto the origin thread.
109 Lock origin_loop_lock_; 106 Lock origin_loop_proxy_lock_;
110 MessageLoop* origin_loop_; 107 // Use a MessageLoopProxy in case the owner of the CertVerifier is leaked, so
108 // this code won't crash: http://crbug.com/42275. If this is leaked, then it
109 // doesn't get Cancel()'d, so |origin_loop_proxy_| doesn't get NULL'd out. If
110 // the MessageLoop goes away, then if we had used a MessageLoop, this would
111 // crash.
112 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_;
111 113
112 // Assigned on the worker thread, read on the origin thread. 114 // Assigned on the worker thread, read on the origin thread.
113 int error_; 115 int error_;
114 CertVerifyResult result_; 116 CertVerifyResult result_;
115 }; 117 };
116 118
117 //----------------------------------------------------------------------------- 119 //-----------------------------------------------------------------------------
118 120
119 CertVerifier::CertVerifier() { 121 CertVerifier::CertVerifier() {
120 } 122 }
(...skipping 25 matching lines...) Expand all
146 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { 148 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) {
147 NOTREACHED(); 149 NOTREACHED();
148 request_ = NULL; 150 request_ = NULL;
149 return ERR_FAILED; 151 return ERR_FAILED;
150 } 152 }
151 153
152 return ERR_IO_PENDING; 154 return ERR_IO_PENDING;
153 } 155 }
154 156
155 } // namespace net 157 } // namespace net
OLDNEW
« base/message_loop_proxy_impl.cc ('K') | « base/message_loop_proxy_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698