Skip to content

Commit

Permalink
Require C++17
Browse files Browse the repository at this point in the history
Update-Note: BoringSSL now requires a C++17 toolchain. This aligns with
Google's Foundational C++ Support Policy as it has now been 10 years
since C++14 was published. See
https://opensource.google/documentation/policies/cplusplus-support

Bug: 42290600
Change-Id: I54b914ccdde2b61a76a4ebfbe67c1fb28f7ba6ac
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/74467
Auto-Submit: David Benjamin <[email protected]>
Reviewed-by: Adam Langley <[email protected]>
Commit-Queue: Adam Langley <[email protected]>
  • Loading branch information
davidben authored and Boringssl LUCI CQ committed Dec 17, 2024
1 parent 59fc518 commit 9ff8491
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .bcr/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tasks:
bazel: ${{ bazel }}
build_targets: *build_targets
build_flags: &macos_workaround
- '--cxxopt=-std=c++14'
- '--cxxopt=-std=c++17'
- '--sandbox_block_path=/usr/local'
bcr_test_module:
module_path: util/bazel-example
Expand Down
2 changes: 1 addition & 1 deletion BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ most recent stable version of each tool.
by CMake, it may be configured explicitly by setting
`CMAKE_ASM_NASM_COMPILER`.

* Compilers for C11 and C++14, or later, are required. On Windows, MSVC from
* Compilers for C11 and C++17, or later, are required. On Windows, MSVC from
Visual Studio 2022 or later with Windows 10 SDK 2104 or later are
supported, but using the latest versions is recommended. Recent versions of
GCC (6.1+) and Clang should work on non-Windows platforms, and maybe on
Expand Down
10 changes: 1 addition & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(EMSCRIPTEN 1)
endif()

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
Expand Down Expand Up @@ -689,13 +689,9 @@ add_executable(pki_test ${PKI_TEST_SOURCES})
target_link_libraries(pki_test test_support_lib boringssl_gtest pki crypto)
add_dependencies(all_tests pki_test)

# The PKI library requires C++17, but we haven't required this everywhere yet.
# TODO(crbug.com/42290600): Remove the C++17 bits.
set_target_properties(
pki pki_test
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
COMPILE_FLAGS "${PKI_CXX_FLAGS}")

add_executable(bssl ${BSSL_SOURCES})
Expand All @@ -718,10 +714,6 @@ if(FUZZ)
set_target_properties(
Fuzzer PROPERTIES
COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0"
# libFuzzer requires C++17, but we haven't required this everywhere yet.
# TODO(crbug.com/42290600): Remove this.
CXX_STANDARD 17
CXX_STANDARD_REQUIRED TRUE
)
endif()

Expand Down
147 changes: 53 additions & 94 deletions crypto/fipsmodule/mldsa/mldsa.cc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -51,126 +51,85 @@ constexpr uint32_t kInverseDegreeMontgomery = 41978;
// Constants that vary depending on ML-DSA size.
//
// These are implemented as templates which take the K parameter to distinguish
// the ML-DSA sizes. (At the time of writing, `if constexpr` was not available.)
//
// TODO(crbug.com/42290600): Switch this to `if constexpr` when C++17 is
// available.
// the ML-DSA sizes.

template <int K>
constexpr size_t public_key_bytes();

template <>
constexpr size_t public_key_bytes<6>() {
return BCM_MLDSA65_PUBLIC_KEY_BYTES;
}

template <>
constexpr size_t public_key_bytes<8>() {
return BCM_MLDSA87_PUBLIC_KEY_BYTES;
constexpr size_t public_key_bytes() {
if constexpr (K == 6) {
return BCM_MLDSA65_PUBLIC_KEY_BYTES;
} else if constexpr (K == 8) {
return BCM_MLDSA87_PUBLIC_KEY_BYTES;
}
}

template <int K>
constexpr size_t signature_bytes();

template <>
constexpr size_t signature_bytes<6>() {
return BCM_MLDSA65_SIGNATURE_BYTES;
}

template <>
constexpr size_t signature_bytes<8>() {
return BCM_MLDSA87_SIGNATURE_BYTES;
constexpr size_t signature_bytes() {
if constexpr (K == 6) {
return BCM_MLDSA65_SIGNATURE_BYTES;
} else if constexpr (K == 8) {
return BCM_MLDSA87_SIGNATURE_BYTES;
}
}

template <int K>
constexpr int tau();

template <>
constexpr int tau<6>() {
return 49;
}

template <>
constexpr int tau<8>() {
return 60;
constexpr int tau() {
if constexpr (K == 6) {
return 49;
} else if constexpr (K == 8) {
return 60;
}
}

template <int K>
constexpr int lambda_bytes();

template <>
constexpr int lambda_bytes<6>() {
return 192 / 8;
}

template <>
constexpr int lambda_bytes<8>() {
return 256 / 8;
constexpr int lambda_bytes() {
if constexpr (K == 6) {
return 192 / 8;
} else if constexpr (K == 8) {
return 256 / 8;
}
}

template <int K>
constexpr int gamma1();

template <>
constexpr int gamma1<6>() {
return 1 << 19;
}

template <>
constexpr int gamma1<8>() {
return 1 << 19;
constexpr int gamma1() {
if constexpr (K == 6 || K == 8) {
return 1 << 19;
}
}

template <int K>
constexpr int beta();

template <>
constexpr int beta<6>() {
return 196;
}

template <>
constexpr int beta<8>() {
return 120;
constexpr int beta() {
if constexpr (K == 6) {
return 196;
} else if constexpr (K == 8) {
return 120;
}
}

template <int K>
constexpr int omega();

template <>
constexpr int omega<6>() {
return 55;
}

template <>
constexpr int omega<8>() {
return 75;
constexpr int omega() {
if constexpr (K == 6) {
return 55;
} else if constexpr (K == 8) {
return 75;
}
}

template <int K>
constexpr int eta();

template <>
constexpr int eta<6>() {
return 4;
}

template <>
constexpr int eta<8>() {
return 2;
constexpr int eta() {
if constexpr (K == 6) {
return 4;
} else if constexpr (K == 8) {
return 2;
}
}

template <int K>
constexpr int plus_minus_eta_bitlen();

template <>
constexpr int plus_minus_eta_bitlen<6>() {
return 4;
}

template <>
constexpr int plus_minus_eta_bitlen<8>() {
return 3;
constexpr int plus_minus_eta_bitlen() {
if constexpr (K == 6) {
return 4;
} else if constexpr (K == 8) {
return 3;
}
}

// Fundamental types.
Expand Down
28 changes: 3 additions & 25 deletions crypto/rand_extra/urandom_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <gtest/gtest.h>
#include <stdlib.h>

#include <optional>

#include <openssl/bytestring.h>
#include <openssl/ctrdrbg.h>
#include <openssl/rand.h>
Expand Down Expand Up @@ -305,30 +307,6 @@ static bool regs_break_syscall(int child_pid, const struct regs *orig_regs) {

#endif

// SyscallResult is like std::optional<int>.
// TODO: use std::optional when we can use C++17.
class SyscallResult {
public:
SyscallResult &operator=(int value) {
has_value_ = true;
value_ = value;
return *this;
}

int value() const {
if (!has_value_) {
abort();
}
return value_;
}

bool has_value() const { return has_value_; }

private:
bool has_value_ = false;
int value_ = 0;
};

// memcpy_to_remote copies |n| bytes from |in_src| in the local address space,
// to |dest| in the address space of |child_pid|.
static void memcpy_to_remote(int child_pid, uint64_t dest, const void *in_src,
Expand Down Expand Up @@ -471,7 +449,7 @@ static void GetTrace(std::vector<Event> *out_trace, unsigned flags,
// force_result is unset to indicate that the system call should run
// normally. Otherwise it's, e.g. -EINVAL, to indicate that the system call
// should not run and that the given value should be injected on return.
SyscallResult force_result;
std::optional<int> force_result;

switch (regs.syscall) {
case __NR_getrandom:
Expand Down
6 changes: 2 additions & 4 deletions crypto/test/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ bool TemporaryFile::Init(bssl::Span<const uint8_t> content) {
path_ = path;
#else
std::string path = temp_dir + "bssl_tmp_file.XXXXXX";
// TODO(davidben): Use |path.data()| when we require C++17.
int fd = mkstemp(&path[0]);
int fd = mkstemp(path.data());
if (fd < 0) {
perror("Could not create temporary file");
return false;
Expand Down Expand Up @@ -208,8 +207,7 @@ bool TemporaryDirectory::Init() {
}
#else
path += "bssl_tmp_dir.XXXXXX";
// TODO(davidben): Use |path.data()| when we require C++17.
if (mkdtemp(&path[0]) == nullptr) {
if (mkdtemp(path.data()) == nullptr) {
perror("Could not make temporary directory");
return false;
}
Expand Down
17 changes: 3 additions & 14 deletions include/openssl/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ extern "C++" {
#include <stdlib.h>

#include <algorithm>
#include <type_traits>

#if __cplusplus >= 201703L
#include <string_view>
#endif
#include <type_traits>

#if defined(__has_include)
#if __has_include(<version>)
Expand Down Expand Up @@ -74,12 +71,10 @@ class SpanBase {

// Heuristically test whether C is a container type that can be converted into
// a Span<T> by checking for data() and size() member functions.
//
// TODO(davidben): Require C++17 support for std::is_convertible_v, etc.
template <typename C, typename T>
using EnableIfContainer = std::enable_if_t<
std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
std::is_integral<decltype(std::declval<C>().size())>::value>;
std::is_convertible_v<decltype(std::declval<C>().data()), T *> &&
std::is_integral_v<decltype(std::declval<C>().size())>>;

} // namespace internal

Expand Down Expand Up @@ -210,7 +205,6 @@ class Span : private internal::SpanBase<const T> {
template <typename T>
const size_t Span<T>::npos;

#if __cplusplus >= 201703L
template <typename T>
Span(T *, size_t) -> Span<T>;
template <typename T, size_t size>
Expand All @@ -220,10 +214,7 @@ template <
typename T = std::remove_pointer_t<decltype(std::declval<C>().data())>,
typename = internal::EnableIfContainer<C, T>>
Span(C &) -> Span<T>;
#endif

// C++17 callers can instead rely on CTAD and the deduction guides defined
// above.
template <typename T>
constexpr Span<T> MakeSpan(T *ptr, size_t size) {
return Span<T>(ptr, size);
Expand Down Expand Up @@ -255,15 +246,13 @@ constexpr Span<const T> MakeConstSpan(T (&array)[size]) {
return array;
}

#if __cplusplus >= 201703L
inline Span<const uint8_t> StringAsBytes(std::string_view s) {
return MakeConstSpan(reinterpret_cast<const uint8_t *>(s.data()), s.size());
}

inline std::string_view BytesAsStringView(bssl::Span<const uint8_t> b) {
return std::string_view(reinterpret_cast<const char *>(b.data()), b.size());
}
#endif

BSSL_NAMESPACE_END

Expand Down
Loading

0 comments on commit 9ff8491

Please sign in to comment.