Adding wrapper class WKBackForwardListItemHolder
[chromium-blink-merge.git] / crypto / openssl_util.cc
blobd89857f3851d71f9f36eac07091201c510ef69a4
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/openssl_util.h"
7 #include <openssl/err.h>
8 #include <openssl/ssl.h>
9 #include <openssl/cpu.h>
11 #include "base/logging.h"
12 #include "base/memory/singleton.h"
13 #include "base/strings/string_piece.h"
14 #include "build/build_config.h"
16 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
17 #include <cpu-features.h>
18 #include "base/cpu.h"
19 #endif
21 namespace crypto {
23 namespace {
25 // Singleton for initializing and cleaning up the OpenSSL library.
26 class OpenSSLInitSingleton {
27 public:
28 static OpenSSLInitSingleton* GetInstance() {
29 // We allow the SSL environment to leak for multiple reasons:
30 // - it is used from a non-joinable worker thread that is not stopped on
31 // shutdown, hence may still be using OpenSSL library after the AtExit
32 // runner has completed.
33 // - There are other OpenSSL related singletons (e.g. the client socket
34 // context) who's cleanup depends on the global environment here, but
35 // we can't control the order the AtExit handlers will run in so
36 // allowing the global environment to leak at least ensures it is
37 // available for those other singletons to reliably cleanup.
38 return Singleton<OpenSSLInitSingleton,
39 LeakySingletonTraits<OpenSSLInitSingleton> >::get();
41 private:
42 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>;
43 OpenSSLInitSingleton() {
44 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
45 const bool has_neon =
46 (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
47 // CRYPTO_set_NEON_capable is called before |SSL_library_init| because this
48 // stops BoringSSL from probing for NEON support via SIGILL in the case
49 // that getauxval isn't present.
50 CRYPTO_set_NEON_capable(has_neon);
51 // See https://code.google.com/p/chromium/issues/detail?id=341598
52 base::CPU cpu;
53 CRYPTO_set_NEON_functional(!cpu.has_broken_neon());
54 #endif
56 SSL_library_init();
59 ~OpenSSLInitSingleton() {}
61 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton);
64 // Callback routine for OpenSSL to print error messages. |str| is a
65 // NULL-terminated string of length |len| containing diagnostic information
66 // such as the library, function and reason for the error, the file and line
67 // where the error originated, plus potentially any context-specific
68 // information about the error. |context| contains a pointer to user-supplied
69 // data, which is currently unused.
70 // If this callback returns a value <= 0, OpenSSL will stop processing the
71 // error queue and return, otherwise it will continue calling this function
72 // until all errors have been removed from the queue.
73 int OpenSSLErrorCallback(const char* str, size_t len, void* context) {
74 DVLOG(1) << "\t" << base::StringPiece(str, len);
75 return 1;
78 } // namespace
80 void EnsureOpenSSLInit() {
81 (void)OpenSSLInitSingleton::GetInstance();
84 void ClearOpenSSLERRStack(const tracked_objects::Location& location) {
85 if (logging::DEBUG_MODE && VLOG_IS_ON(1)) {
86 uint32_t error_num = ERR_peek_error();
87 if (error_num == 0)
88 return;
90 std::string message;
91 location.Write(true, true, &message);
92 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message;
93 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
94 } else {
95 ERR_clear_error();
99 } // namespace crypto