blob: 9ca55258b3bb20535acd4996ec27e7867710c78d [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]cd924d62012-02-23 17:52:202// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tom Sepez8726d30e2025-01-29 02:11:085#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
7#pragma allow_unsafe_libc_calls
8#endif
9
[email protected]cd924d62012-02-23 17:52:2010#include "base/memory/aligned_memory.h"
dcheng093de9b2016-04-04 21:25:5111
danakjb4705072024-11-06 20:54:0912#include <stdint.h>
Tom Sepeze29ac692023-10-30 23:38:2313#include <string.h>
14
dcheng093de9b2016-04-04 21:25:5115#include <memory>
16
avi9beac252015-12-24 08:44:4717#include "build/build_config.h"
[email protected]cd924d62012-02-23 17:52:2018#include "testing/gtest/include/gtest/gtest.h"
19
brettw16289b3e2017-06-13 21:58:4020namespace base {
[email protected]cd924d62012-02-23 17:52:2021
danakjb4705072024-11-06 20:54:0922TEST(AlignedMemoryTest, AlignedUninit) {
23 {
24 base::AlignedHeapArray<char> h = AlignedUninit<char>(8, 32);
25 EXPECT_EQ(h.size(), 8u);
26 EXPECT_TRUE(IsAligned(h.data(), 32));
27 }
28 {
29 base::AlignedHeapArray<int16_t> h = AlignedUninit<int16_t>(8, 32);
30 EXPECT_EQ(h.size(), 8u);
31 EXPECT_TRUE(IsAligned(h.data(), 32));
32 }
33}
34
35TEST(AlignedMemoryTest, AlignedUninitCharArray) {
36 auto [h, s] = AlignedUninitCharArray<int16_t>(8, 32);
37 static_assert(std::same_as<base::AlignedHeapArray<char>, decltype(h)>);
38 static_assert(std::same_as<base::span<int16_t>, decltype(s)>);
39 EXPECT_EQ(h.size(), 8u * sizeof(int16_t));
40 EXPECT_TRUE(IsAligned(h.data(), 32));
41 EXPECT_EQ(s.size(), 8u);
42 EXPECT_TRUE(IsAligned(s.data(), 32));
43}
44
[email protected]9f01b022012-07-26 02:22:3945TEST(AlignedMemoryTest, DynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4046 void* p = AlignedAlloc(8, 8);
Tom Sepeze29ac692023-10-30 23:38:2347 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4148 EXPECT_TRUE(IsAligned(p, 8));
Tom Sepeze29ac692023-10-30 23:38:2349 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4050 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3951
brettw16289b3e2017-06-13 21:58:4052 p = AlignedAlloc(8, 16);
Tom Sepeze29ac692023-10-30 23:38:2353 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4154 EXPECT_TRUE(IsAligned(p, 16));
Tom Sepeze29ac692023-10-30 23:38:2355 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4056 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3957
brettw16289b3e2017-06-13 21:58:4058 p = AlignedAlloc(8, 256);
Tom Sepeze29ac692023-10-30 23:38:2359 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4160 EXPECT_TRUE(IsAligned(p, 256));
Tom Sepeze29ac692023-10-30 23:38:2361 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4062 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3963
brettw16289b3e2017-06-13 21:58:4064 p = AlignedAlloc(8, 4096);
Tom Sepeze29ac692023-10-30 23:38:2365 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4166 EXPECT_TRUE(IsAligned(p, 4096));
Tom Sepeze29ac692023-10-30 23:38:2367 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4068 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3969}
70
71TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4072 std::unique_ptr<float, AlignedFreeDeleter> p(
73 static_cast<float*>(AlignedAlloc(8, 8)));
[email protected]9f01b022012-07-26 02:22:3974 EXPECT_TRUE(p.get());
Brian Geffonae8ef4dd2020-04-06 19:48:4175 EXPECT_TRUE(IsAligned(p.get(), 8));
Lei Zhangdf4345c72020-06-15 22:07:0076
77 // Make sure IsAligned() can check const pointers as well.
78 const float* const_p = p.get();
79 EXPECT_TRUE(IsAligned(const_p, 8));
Brian Geffonae8ef4dd2020-04-06 19:48:4180}
81
82TEST(AlignedMemoryTest, IsAligned) {
83 // Check alignment around powers of two.
84 for (int i = 0; i < 64; ++i) {
85 const uint64_t n = static_cast<uint64_t>(1) << i;
86
87 // Walk back down all lower powers of two checking alignment.
88 for (int j = i - 1; j >= 0; --j) {
89 // n is aligned on all powers of two less than or equal to 2^i.
90 EXPECT_TRUE(IsAligned(n, n >> j))
91 << "Expected " << n << " to be " << (n >> j) << " aligned";
92
93 // Also, n - 1 should not be aligned on ANY lower power of two except 1
94 // (but since we're starting from i - 1 we don't test that case here.
95 EXPECT_FALSE(IsAligned(n - 1, n >> j))
96 << "Expected " << (n - 1) << " to NOT be " << (n >> j) << " aligned";
97 }
98 }
99
100 // And a few hard coded smoke tests for completeness:
101 EXPECT_TRUE(IsAligned(4, 2));
102 EXPECT_TRUE(IsAligned(8, 4));
103 EXPECT_TRUE(IsAligned(8, 2));
104 EXPECT_TRUE(IsAligned(0x1000, 4 << 10));
105 EXPECT_TRUE(IsAligned(0x2000, 8 << 10));
106 EXPECT_TRUE(IsAligned(1, 1));
107 EXPECT_TRUE(IsAligned(7, 1));
108 EXPECT_TRUE(IsAligned(reinterpret_cast<void*>(0x1000), 4 << 10));
109 EXPECT_TRUE(IsAligned(reinterpret_cast<int*>(0x1000), 4 << 10));
110
111 EXPECT_FALSE(IsAligned(3, 2));
112 EXPECT_FALSE(IsAligned(7, 4));
113 EXPECT_FALSE(IsAligned(7, 2));
114 EXPECT_FALSE(IsAligned(0x1001, 4 << 10));
115 EXPECT_FALSE(IsAligned(0x999, 8 << 10));
116 EXPECT_FALSE(IsAligned(7, 8));
[email protected]9f01b022012-07-26 02:22:39