Files
Rudolf Polzer f7543e6dbb Namespace crypto/internal.h's internal symbols.
Down from 65 to 3 unintended exported symbols.

The remaining 3 are weak symbols to override the malloc implementation
and might be acceptable (and if not, they probably will be prefixed like
public symbols instead):

extern "C" function OPENSSL_memory_alloc;  // crypto/mem.cc:103 (4077-4093) size
extern "C" function OPENSSL_memory_free;  // crypto/mem.cc:104 (4139-4155) ptr
extern "C" function OPENSSL_memory_get_size;  // crypto/mem.cc:105 (4196-4212) ptr

This has been confirmed acceptable, and is likely required by existing
code bases linking to BoringSSL.

Bug: 42220000
Change-Id: I5c7be4e12ef0aa09691e0bc938bfc6c76a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/86669
Reviewed-by: Xiangfei Ding <xfding@google.com>
Commit-Queue: Rudolf Polzer <rpolzer@google.com>
2026-01-19 02:29:43 -08:00

86 lines
2.3 KiB
C++

// Copyright 2015 The BoringSSL Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal.h"
#include <gtest/gtest.h>
#if defined(OPENSSL_THREADS)
#include <thread>
#endif
BSSL_NAMESPACE_BEGIN
namespace {
TEST(RefCountTest, Basic) {
CRYPTO_refcount_t count = 0;
CRYPTO_refcount_inc(&count);
EXPECT_EQ(1u, count.load());
EXPECT_TRUE(CRYPTO_refcount_dec_and_test_zero(&count));
EXPECT_EQ(0u, count.load());
count = CRYPTO_REFCOUNT_MAX;
CRYPTO_refcount_inc(&count);
EXPECT_EQ(CRYPTO_REFCOUNT_MAX, count.load())
<< "Count did not saturate correctly when incrementing.";
EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
EXPECT_EQ(CRYPTO_REFCOUNT_MAX, count.load())
<< "Count did not saturate correctly when decrementing.";
count = 2;
EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
EXPECT_EQ(1u, count.load());
}
#if defined(OPENSSL_THREADS)
// This test is primarily intended to run under ThreadSanitizer.
TEST(RefCountTest, Threads) {
CRYPTO_refcount_t count = 0;
// Race two increments.
{
std::thread thread([&] { CRYPTO_refcount_inc(&count); });
CRYPTO_refcount_inc(&count);
thread.join();
EXPECT_EQ(2u, count.load());
}
// Race an increment with a decrement.
{
std::thread thread([&] { CRYPTO_refcount_inc(&count); });
EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
thread.join();
EXPECT_EQ(2u, count.load());
}
// Race two decrements.
{
bool thread_saw_zero;
std::thread thread(
[&] { thread_saw_zero = CRYPTO_refcount_dec_and_test_zero(&count); });
bool saw_zero = CRYPTO_refcount_dec_and_test_zero(&count);
thread.join();
EXPECT_EQ(0u, count.load());
// Exactly one thread should see zero.
EXPECT_NE(saw_zero, thread_saw_zero);
}
}
#endif
} // namespace
BSSL_NAMESPACE_END