#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/profiler/stack_buffer.h"
#include "base/memory/aligned_memory.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "base/bits.h"
#include "base/memory/page_size.h"
#endif
namespace base {
TEST(StackBufferTest, BufferAllocated) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST(StackBufferTest, MarkBufferContentsAsUnneeded) {
const unsigned int kBufferSize = 32 * GetPageSize();
StackBuffer stack_buffer(kBufferSize);
volatile uintptr_t* buffer = stack_buffer.buffer();
ASSERT_NE(nullptr, buffer);
for (unsigned int i = 0; i < (kBufferSize / sizeof(uintptr_t)); i++) {
buffer[i] = i;
EXPECT_EQ(buffer[i], i);
}
constexpr size_t kUndiscardedElements = 100;
stack_buffer.MarkUpperBufferContentsAsUnneeded(kUndiscardedElements *
sizeof(buffer[0]));
for (size_t i = 0; i < kUndiscardedElements; i++) {
EXPECT_EQ(buffer[i], i);
}
const size_t kExpectedDiscardStartPoint =
bits::AlignUp(kUndiscardedElements * sizeof(buffer[0]), GetPageSize()) /
sizeof(buffer[0]);
for (size_t i = kExpectedDiscardStartPoint;
i < kBufferSize / sizeof(buffer[0]); i++) {
EXPECT_EQ(buffer[i], 0U);
}
for (unsigned int i = 0; i < (kBufferSize / sizeof(buffer[0])); i++) {
buffer[i] = i + 7;
EXPECT_EQ(buffer[i], i + 7);
}
}
#endif
}