0

C++11 std::array rewrite for memory safety [4/19]

Split from:
https://chromium-review.googlesource.com/c/chromium/src/+/6004959/21

Generated patch
---------------
- Tool: ./tool/clang/spanify/rewrite-multiple-platform.sh
- Platform: Linux.
- Filter: This includes 2400/4222 patches. I included the std::array
      ones and excluded build errors.

Google announcement:
--------------------
https://groups.google.com/a/google.com/g/chrome-memory-safety/c/RMiO4gaVLQA/m/Yz-3NCObAgAJ

Benchmarks:
----------
See design doc and
https://chromium-review.googlesource.com/c/chromium/src/+/6004959/21

Description
-----------
The consensus during the memory safety summit was to begin rewriting
relevant C-style arrays to C++11 std::array. It can be done immediately,
offers better developer ergonomics, and fix large chunks of the
-Wunsafe-buffer-usage errors in Chrome.

To clarify, this effort is complementary to the longer plan work with
enabling -fsanitize=array-bounds, and we plan to leverage both,
especially for protecting 3p code.

[Attached] is a document detailing the rationale, benefits, and
considerations for potential compile-time and performance impacts.

[Attached]:https://docs.google.com/document/d/1z5aBDg26lHmNDjXRCysElWKx7E4PAJXqykI_k7ondJI/edit?tab=t.0#heading=h.cqgo7wvp0kzt

NO_IFTTT=No need to update base/debug/stack_trace.h

Bug: 378069401
Change-Id: I838a8c44e82ed27dc75e771acec31d84992860af
R: dcheng@chromium.org
AX-Relnotes: n/a.
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6038951
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Auto-Submit: Arthur Sonzogni <arthursonzogni@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
Owners-Override: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1394163}
This commit is contained in:
Arthur Sonzogni
2024-12-10 09:58:59 +00:00
committed by Chromium LUCI CQ
parent deef7d51df
commit b7c1fd51e1
20 changed files with 224 additions and 185 deletions

@ -11,6 +11,7 @@
#include <stddef.h>
#include <array>
#include <limits>
#include <utility>
@ -202,7 +203,7 @@ TEST_F(CommandStorageBackendTest, SimpleReadWriteEncrypted) {
}
TEST_F(CommandStorageBackendTest, RandomDataEncrypted) {
struct TestData data[] = {
auto data = std::to_array<TestData>({
{1, "a"},
{2, "ab"},
{3, "abc"},
@ -216,7 +217,7 @@ TEST_F(CommandStorageBackendTest, RandomDataEncrypted) {
{11, "abcdefghijk"},
{12, "abcdefghijkl"},
{13, "abcdefghijklm"},
};
});
const std::vector<uint8_t> key = CommandStorageManager::CreateCryptoKey();
for (size_t i = 0; i < std::size(data); ++i) {
@ -240,10 +241,10 @@ TEST_F(CommandStorageBackendTest, RandomDataEncrypted) {
}
TEST_F(CommandStorageBackendTest, BigDataEncrypted) {
struct TestData data[] = {
auto data = std::to_array<TestData>({
{1, "a"},
{2, "ab"},
};
});
const std::vector<uint8_t> key = CommandStorageManager::CreateCryptoKey();
scoped_refptr<CommandStorageBackend> backend = CreateBackend();
@ -422,7 +423,7 @@ TEST_F(CommandStorageBackendTest, SimpleReadWriteWithRestoreType) {
}
TEST_F(CommandStorageBackendTest, RandomDataWithRestoreType) {
struct TestData data[] = {
auto data = std::to_array<TestData>({
{1, "a"},
{2, "ab"},
{3, "abc"},
@ -436,7 +437,7 @@ TEST_F(CommandStorageBackendTest, RandomDataWithRestoreType) {
{11, "abcdefghijk"},
{12, "abcdefghijkl"},
{13, "abcdefghijklm"},
};
});
for (size_t i = 0; i < std::size(data); ++i) {
scoped_refptr<CommandStorageBackend> backend =
@ -459,10 +460,10 @@ TEST_F(CommandStorageBackendTest, RandomDataWithRestoreType) {
}
TEST_F(CommandStorageBackendTest, BigDataWithRestoreType) {
struct TestData data[] = {
auto data = std::to_array<TestData>({
{1, "a"},
{2, "ab"},
};
});
scoped_refptr<CommandStorageBackend> backend = CreateBackendWithRestoreType();
std::vector<std::unique_ptr<SessionCommand>> commands;