From f5a3b8a848ef1c3f72d2fa960437ca9b66ead654 Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Thu, 10 Aug 2023 13:38:17 -0700 Subject: [PATCH 1/2] protoutil: Fix EncodedBuffer test Commit ag/10780537 ("Optimize memory usage in incidentd") broke the libprotoutil_test since it page aligns the chunk size but didn't update the tests. Fix the tests by page aligning TEST_CHUNK_SIZE; and add a test mapping. Test: atest -c libprotoutil_test Bug: 295228590 Change-Id: I85d828a10cc67334751c3c3a451a75c4dbe3eb76 Signed-off-by: Kalesh Singh --- libs/protoutil/Android.bp | 4 ++++ libs/protoutil/AndroidTest.xml | 26 --------------------- libs/protoutil/TEST_MAPPING | 12 ++++++++++ libs/protoutil/tests/EncodedBuffer_test.cpp | 11 +++++---- 4 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 libs/protoutil/AndroidTest.xml create mode 100644 libs/protoutil/TEST_MAPPING diff --git a/libs/protoutil/Android.bp b/libs/protoutil/Android.bp index 128be3c33ac56..28856c87f7c69 100644 --- a/libs/protoutil/Android.bp +++ b/libs/protoutil/Android.bp @@ -80,6 +80,10 @@ cc_test { "libgmock", ], + test_suites: [ + "general-tests", + ], + proto: { type: "full", }, diff --git a/libs/protoutil/AndroidTest.xml b/libs/protoutil/AndroidTest.xml deleted file mode 100644 index 46d418e1bb0a9..0000000000000 --- a/libs/protoutil/AndroidTest.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - diff --git a/libs/protoutil/TEST_MAPPING b/libs/protoutil/TEST_MAPPING new file mode 100644 index 0000000000000..b10dd9b067b6b --- /dev/null +++ b/libs/protoutil/TEST_MAPPING @@ -0,0 +1,12 @@ +{ + "presubmit": [ + { + "name": "libprotoutil_test" + } + ], + "hwasan-postsubmit": [ + { + "name": "libprotoutil_test" + } + ] +} diff --git a/libs/protoutil/tests/EncodedBuffer_test.cpp b/libs/protoutil/tests/EncodedBuffer_test.cpp index f895154c4983e..8a7becf68d693 100644 --- a/libs/protoutil/tests/EncodedBuffer_test.cpp +++ b/libs/protoutil/tests/EncodedBuffer_test.cpp @@ -18,7 +18,8 @@ using namespace android::util; using android::sp; -constexpr size_t TEST_CHUNK_SIZE = 16UL; +constexpr size_t __TEST_CHUNK_SIZE = 16UL; +constexpr size_t TEST_CHUNK_SIZE = (__TEST_CHUNK_SIZE + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1); constexpr size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2; constexpr size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE; @@ -34,13 +35,13 @@ TEST(EncodedBufferTest, WriteSimple) { expectPointer(buffer->wp(), 0); EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_SIZE); for (size_t i = 0; i < TEST_CHUNK_HALF_SIZE; i++) { - buffer->writeRawByte(50 + i); + buffer->writeRawByte(static_cast(50 + i)); } EXPECT_EQ(buffer->size(), TEST_CHUNK_HALF_SIZE); expectPointer(buffer->wp(), TEST_CHUNK_HALF_SIZE); EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_HALF_SIZE); for (size_t i = 0; i < TEST_CHUNK_SIZE; i++) { - buffer->writeRawByte(80 + i); + buffer->writeRawByte(static_cast(80 + i)); } EXPECT_EQ(buffer->size(), TEST_CHUNK_SIZE + TEST_CHUNK_HALF_SIZE); expectPointer(buffer->wp(), TEST_CHUNK_SIZE + TEST_CHUNK_HALF_SIZE); @@ -49,10 +50,10 @@ TEST(EncodedBufferTest, WriteSimple) { // verifies the buffer's data expectPointer(buffer->ep(), 0); for (size_t i = 0; i < TEST_CHUNK_HALF_SIZE; i++) { - EXPECT_EQ(buffer->readRawByte(), 50 + i); + EXPECT_EQ(buffer->readRawByte(), static_cast(50 + i)); } for (size_t i = 0; i < TEST_CHUNK_SIZE; i++) { - EXPECT_EQ(buffer->readRawByte(), 80 + i); + EXPECT_EQ(buffer->readRawByte(), static_cast(80 + i)); } // clears the buffer From c1d4f195bc63c7c591a540948f79d6156ab1c792 Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Thu, 10 Aug 2023 13:46:09 -0700 Subject: [PATCH 2/2] protoutil: EncodedBuffer: Remove usage of PAGE_SIZE 4096 bionic hard codes the PAGE_SIZE macro as 4096. This is going away as Android begins to support larger page sizes. Remove the usage of this assumption from protoutil source; use instead getpagesize() which provides the real pagesize. Test: atest -c libprotoutil_test Bug: 295228590 Change-Id: I6c3212600256d61a71430dfbbf7b0c9944fe4859 Signed-off-by: Kalesh Singh --- libs/protoutil/src/EncodedBuffer.cpp | 6 ++++-- libs/protoutil/tests/EncodedBuffer_test.cpp | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/libs/protoutil/src/EncodedBuffer.cpp b/libs/protoutil/src/EncodedBuffer.cpp index 96b54c63a836b..afb54a6c49b62 100644 --- a/libs/protoutil/src/EncodedBuffer.cpp +++ b/libs/protoutil/src/EncodedBuffer.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -25,7 +26,8 @@ namespace android { namespace util { -const size_t BUFFER_SIZE = 8 * 1024; // 8 KB +constexpr size_t BUFFER_SIZE = 8 * 1024; // 8 KB +const size_t kPageSize = getpagesize(); EncodedBuffer::Pointer::Pointer() : Pointer(BUFFER_SIZE) { @@ -92,7 +94,7 @@ EncodedBuffer::EncodedBuffer(size_t chunkSize) { // Align chunkSize to memory page size chunkSize = chunkSize == 0 ? BUFFER_SIZE : chunkSize; - mChunkSize = (chunkSize / PAGE_SIZE + ((chunkSize % PAGE_SIZE == 0) ? 0 : 1)) * PAGE_SIZE; + mChunkSize = (chunkSize + (kPageSize - 1)) & ~(kPageSize - 1); mWp = Pointer(mChunkSize); mEp = Pointer(mChunkSize); } diff --git a/libs/protoutil/tests/EncodedBuffer_test.cpp b/libs/protoutil/tests/EncodedBuffer_test.cpp index 8a7becf68d693..a09558544c268 100644 --- a/libs/protoutil/tests/EncodedBuffer_test.cpp +++ b/libs/protoutil/tests/EncodedBuffer_test.cpp @@ -15,13 +15,16 @@ #include #include +#include + using namespace android::util; using android::sp; constexpr size_t __TEST_CHUNK_SIZE = 16UL; -constexpr size_t TEST_CHUNK_SIZE = (__TEST_CHUNK_SIZE + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1); -constexpr size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2; -constexpr size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE; +const size_t kPageSize = getpagesize(); +const size_t TEST_CHUNK_SIZE = (__TEST_CHUNK_SIZE + (kPageSize - 1)) & ~(kPageSize - 1); +const size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2; +const size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE; static void expectPointer(EncodedBuffer::Pointer* p, size_t pos) { EXPECT_EQ(p->pos(), pos);