Coverage Report

Created: 2026-04-02 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/pool/internal.h
Line
Count
Source
1
// Copyright 2016 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
#ifndef OPENSSL_HEADER_CRYPTO_POOL_INTERNAL_H
16
#define OPENSSL_HEADER_CRYPTO_POOL_INTERNAL_H
17
18
#include "../internal.h"
19
#include "../lhash/internal.h"
20
#include "../mem_internal.h"
21
22
23
DECLARE_OPAQUE_STRUCT(crypto_buffer_st, CryptoBuffer)
24
DECLARE_OPAQUE_STRUCT(crypto_buffer_pool_st, CryptoBufferPool)
25
26
BSSL_NAMESPACE_BEGIN
27
28
// A CryptoBufferPoolHandle is the portion of the pool that lasts as long as any
29
// live buffer or pool. This allows buffers to outlive the pool. (The pool is
30
// only needed as long as callers wish to create new buffers.)
31
class CryptoBufferPoolHandle : public RefCounted<CryptoBufferPoolHandle> {
32
 public:
33
  explicit CryptoBufferPoolHandle(CryptoBufferPool *pool)
34
0
      : RefCounted(CheckSubClass()), pool_(pool) {}
35
36
  // pool_ is protected by lock_.
37
  Mutex lock_;
38
  CryptoBufferPool *pool_ = nullptr;
39
40
 private:
41
  friend RefCounted;
42
0
  ~CryptoBufferPoolHandle() = default;
43
};
44
45
class CryptoBuffer : public crypto_buffer_st {
46
 public:
47
337k
  CryptoBuffer() = default;
48
  CryptoBuffer(const CryptoBuffer &) = delete;
49
  CryptoBuffer &operator=(const CryptoBuffer &) = delete;
50
51
282k
  Span<const uint8_t> span() const { return Span(data_, len_); }
52
53
  // Instead of subclassing RefCounted<T>, implement refcounting by hand.
54
  // CryptoBuffer's refcounting must synchronize with CryptoBufferPool.
55
  static constexpr bool kAllowRefCountedUniquePtr = true;
56
  void UpRefInternal();
57
  void DecRefInternal();
58
59
  UniquePtr<CryptoBufferPoolHandle> pool_handle_;
60
  uint8_t *data_ = nullptr;
61
  size_t len_ = 0;
62
  CRYPTO_refcount_t references_ = 1;
63
  bool data_is_static_ = false;
64
65
 private:
66
  ~CryptoBuffer();
67
};
68
69
DEFINE_LHASH_OF(CryptoBuffer)
70
71
class CryptoBufferPool : public crypto_buffer_pool_st {
72
 public:
73
  static constexpr bool kAllowUniquePtr = true;
74
  CryptoBufferPool();
75
  ~CryptoBufferPool();
76
77
  // Hash returns the hash of |data|.
78
  uint32_t Hash(Span<const uint8_t> data) const;
79
80
  // FindBufferLocked looks for a buffer with hash |hash| and contents |data|.
81
  // It returns it if found and nullptr otherwise. |handle_->lock_| must be
82
  // locked for reading or writing before calling this.
83
  CryptoBuffer *FindBufferLocked(uint32_t hash, Span<const uint8_t> data);
84
85
  UniquePtr<CryptoBufferPoolHandle> handle_;
86
  LHASH_OF(CryptoBuffer) *bufs_ = nullptr;
87
  uint64_t hash_key_[2];
88
};
89
90
BSSL_NAMESPACE_END
91
92
#endif  // OPENSSL_HEADER_CRYPTO_POOL_INTERNAL_H