Coverage Report

Created: 2026-06-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/thread_pthread.cc
Line
Count
Source
1
// Copyright 2015 The BoringSSL Authors
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
//     https://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
// Ensure we can't call OPENSSL_malloc circularly.
16
#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
17
#include "internal.h"
18
19
#if defined(OPENSSL_PTHREADS)
20
21
#include <assert.h>
22
#include <pthread.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
27
BSSL_NAMESPACE_BEGIN
28
29
391k
void StaticMutex::LockRead() { BSSL_CHECK(pthread_rwlock_rdlock(&lock_) == 0); }
30
31
391k
void StaticMutex::UnlockRead() {
32
391k
  BSSL_CHECK(pthread_rwlock_unlock(&lock_) == 0);
33
391k
}
34
35
160k
void StaticMutex::LockWrite() {
36
160k
  BSSL_CHECK(pthread_rwlock_wrlock(&lock_) == 0);
37
160k
}
38
39
160k
void StaticMutex::UnlockWrite() {
40
160k
  BSSL_CHECK(pthread_rwlock_unlock(&lock_) == 0);
41
160k
}
42
43
346k
Mutex::~Mutex() { pthread_rwlock_destroy(&lock_); }
44
45
38.0M
void CRYPTO_once(CRYPTO_once_t *once, void (*init)()) {
46
38.0M
  BSSL_CHECK(pthread_once(once, init) == 0);
47
38.0M
}
48
49
static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
50
static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
51
52
// thread_local_destructor is called when a thread exits. It releases thread
53
// local data for that thread only.
54
0
static void thread_local_destructor(void *arg) {
55
0
  if (arg == nullptr) {
56
0
    return;
57
0
  }
58
59
0
  thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
60
0
  if (pthread_mutex_lock(&g_destructors_lock) != 0) {
61
0
    return;
62
0
  }
63
0
  OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
64
0
  pthread_mutex_unlock(&g_destructors_lock);
65
66
0
  unsigned i;
67
0
  void **pointers = reinterpret_cast<void **>(arg);
68
0
  for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
69
0
    if (destructors[i] != nullptr) {
70
0
      destructors[i](pointers[i]);
71
0
    }
72
0
  }
73
74
0
  free(pointers);
75
0
}
76
77
static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
78
static pthread_key_t g_thread_local_key;
79
static int g_thread_local_key_created = 0;
80
81
28
static void thread_local_init() {
82
28
  g_thread_local_key_created =
83
28
      pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
84
28
}
85
86
1.82M
void *CRYPTO_get_thread_local(thread_local_data_t index) {
87
1.82M
  CRYPTO_once(&g_thread_local_init_once, thread_local_init);
88
1.82M
  if (!g_thread_local_key_created) {
89
0
    return nullptr;
90
0
  }
91
92
1.82M
  void **pointers =
93
1.82M
      reinterpret_cast<void **>(pthread_getspecific(g_thread_local_key));
94
1.82M
  if (pointers == nullptr) {
95
28
    return nullptr;
96
28
  }
97
1.82M
  return pointers[index];
98
1.82M
}
99
100
int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
101
36
                            thread_local_destructor_t destructor) {
102
36
  CRYPTO_once(&g_thread_local_init_once, thread_local_init);
103
36
  if (!g_thread_local_key_created) {
104
0
    destructor(value);
105
0
    return 0;
106
0
  }
107
108
36
  void **pointers =
109
36
      reinterpret_cast<void **>(pthread_getspecific(g_thread_local_key));
110
36
  if (pointers == nullptr) {
111
28
    pointers = reinterpret_cast<void **>(
112
28
        malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS));
113
28
    if (pointers == nullptr) {
114
0
      destructor(value);
115
0
      return 0;
116
0
    }
117
28
    OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
118
28
    if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
119
0
      free(pointers);
120
0
      destructor(value);
121
0
      return 0;
122
0
    }
123
28
  }
124
125
36
  if (pthread_mutex_lock(&g_destructors_lock) != 0) {
126
0
    destructor(value);
127
0
    return 0;
128
0
  }
129
36
  g_destructors[index] = destructor;
130
36
  pthread_mutex_unlock(&g_destructors_lock);
131
132
36
  pointers[index] = value;
133
36
  return 1;
134
36
}
135
136
BSSL_NAMESPACE_END
137
138
#endif  // OPENSSL_PTHREADS