From f5d21cd145c0527427fc975a4c25941789bcd338 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Sun, 10 Sep 2023 18:25:49 -0700 Subject: [PATCH] [libc++] Allow std::string sizeof.compile test to pass The current layout is somewhat inefficient with an allocator: https://github.com/CTSRD-CHERI/llvm-project/issues/718 --- .../basic.string/sizeof.compile.pass.cpp | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp b/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp index 9024d11c598e..376826450e15 100644 --- a/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp +++ b/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp @@ -49,22 +49,29 @@ template using small_string = std::basic_string, small_iter_allocator >; #if __SIZE_WIDTH__ == 64 - -static_assert(sizeof(std::string) == 24, ""); -static_assert(sizeof(min_string) == 24, ""); -static_assert(sizeof(test_string) == 32, ""); +constexpr size_t expected_string_size = sizeof(void*) + 2 * sizeof(size_t); +#ifdef __CHERI_PURE_CAPABILITY__ +// The current layout is inefficient (https://github.com/CTSRD-CHERI/llvm-project/issues/718) +// until _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT becomes the default. +constexpr size_t expected_test_string_size = 64; +#else +constexpr size_t expected_test_string_size = 32; +#endif +static_assert(sizeof(std::string) == expected_string_size, ""); +static_assert(sizeof(min_string) == expected_string_size, ""); +static_assert(sizeof(test_string) == expected_test_string_size, ""); static_assert(sizeof(small_string) == 6, ""); # ifndef TEST_HAS_NO_WIDE_CHARACTERS # if __WCHAR_WIDTH__ == 32 -static_assert(sizeof(std::wstring) == 24, ""); -static_assert(sizeof(min_string) == 24, ""); -static_assert(sizeof(test_string) == 32, ""); +static_assert(sizeof(std::wstring) == expected_string_size, ""); +static_assert(sizeof(min_string) == expected_string_size, ""); +static_assert(sizeof(test_string) == expected_test_string_size, ""); static_assert(sizeof(small_string) == 12, ""); # elif __WCHAR_WIDTH__ == 16 -static_assert(sizeof(std::wstring) == 24, ""); -static_assert(sizeof(min_string) == 24, ""); -static_assert(sizeof(test_string) == 32, ""); +static_assert(sizeof(std::wstring) == expected_string_size, ""); +static_assert(sizeof(min_string) == expected_string_size, ""); +static_assert(sizeof(test_string) == expected_test_string_size, ""); static_assert(sizeof(small_string) == 6, ""); # else # error "Unexpected wchar_t width" @@ -72,19 +79,19 @@ static_assert(sizeof(small_string) == 6, ""); # endif # ifndef TEST_HAS_NO_CHAR8_T -static_assert(sizeof(std::u8string) == 24, ""); -static_assert(sizeof(min_string) == 24, ""); -static_assert(sizeof(test_string) == 32, ""); +static_assert(sizeof(std::u8string) == expected_string_size, ""); +static_assert(sizeof(min_string) == expected_string_size, ""); +static_assert(sizeof(test_string) == expected_test_string_size, ""); static_assert(sizeof(small_string) == 6, ""); # endif # ifndef TEST_HAS_NO_UNICODE_CHARS -static_assert(sizeof(std::u16string) == 24, ""); -static_assert(sizeof(std::u32string) == 24, ""); -static_assert(sizeof(min_string) == 24, ""); -static_assert(sizeof(min_string) == 24, ""); -static_assert(sizeof(test_string) == 32, ""); -static_assert(sizeof(test_string) == 32, ""); +static_assert(sizeof(std::u16string) == expected_string_size, ""); +static_assert(sizeof(std::u32string) == expected_string_size, ""); +static_assert(sizeof(min_string) == expected_string_size, ""); +static_assert(sizeof(min_string) == expected_string_size, ""); +static_assert(sizeof(test_string) == expected_test_string_size, ""); +static_assert(sizeof(test_string) == expected_test_string_size, ""); static_assert(sizeof(small_string) == 6, ""); static_assert(sizeof(small_string) == 12, ""); # endif