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

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: Add DCHECK 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.h"
wtc 2010/11/23 19:17:13 Nit: you should be able to remove #include "base/m
willchan no longer on Chromium 2010/11/23 20:32:42 Done.
12 #include "base/message_loop_proxy.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_(base::MessageLoopProxy::CreateForCurrentThread()),
wtc 2010/11/23 19:17:13 It would be nice to update the comment in message_
willchan no longer on Chromium 2010/11/23 20:32:42 Done.
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_lock_);
57 AutoLock locked(origin_loop_lock_); 58 if (origin_loop_) {
58 if (origin_loop_) { 59 bool posted = origin_loop_->PostTask(FROM_HERE, reply.release());
59 origin_loop_->PostTask(FROM_HERE, reply); 60 // Try to catch leaked CertVerifiers on shutdown with this DCHECK.
60 reply = NULL; 61 DCHECK(posted);
wtc 2010/11/23 19:17:13 I suggest using an LOG(ERROR) or LOG(WARNING) mess
willchan no longer on Chromium 2010/11/23 20:32:42 Done.
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_;
(...skipping 24 matching lines...) Expand all
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_lock_;
110 MessageLoop* origin_loop_; 107 // Use a MessageLoopProxy in case the owner of the CertVerifier is leaked, so
wtc 2010/11/23 19:17:13 This comment should elaborate on how a leaked owne
willchan no longer on Chromium 2010/11/23 20:32:42 Done.
108 // this code won't crash: http://crbug.com/42275.
109 scoped_refptr<base::MessageLoopProxy> origin_loop_;
wtc 2010/11/23 19:17:13 Should we rename this member origin_loop_proxy_?
willchan no longer on Chromium 2010/11/23 20:32:42 Done.
111 110
112 // Assigned on the worker thread, read on the origin thread. 111 // Assigned on the worker thread, read on the origin thread.
113 int error_; 112 int error_;
114 CertVerifyResult result_; 113 CertVerifyResult result_;
115 }; 114 };
116 115
117 //----------------------------------------------------------------------------- 116 //-----------------------------------------------------------------------------
118 117
119 CertVerifier::CertVerifier() { 118 CertVerifier::CertVerifier() {
120 } 119 }
(...skipping 25 matching lines...) Expand all
146 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { 145 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) {
147 NOTREACHED(); 146 NOTREACHED();
148 request_ = NULL; 147 request_ = NULL;
149 return ERR_FAILED; 148 return ERR_FAILED;
150 } 149 }
151 150
152 return ERR_IO_PENDING; 151 return ERR_IO_PENDING;
153 } 152 }
154 153
155 } // namespace net 154 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698