Skip to content

Clang does not optimize out _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES from libc++ #66953

@davidben

Description

@davidben

libc++ contains assertions of the following form:

static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
if (!__libcpp_is_constant_evaluated())
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(
__s2 < __s1 || __s2 >= __s1 + __n, "char_traits::copy overlapped range");
std::copy_n(__s2, __n, __s1);
return __s1;
}

These are enabled with _LIBCPP_ENABLE_SAFE_MODE and are, among other places, used when constructing std::strings from string literals:

__init(__s, traits_type::length(__s));

traits_type::copy(std::__to_address(__p), __s, __sz);

Looking at the disassembly of some code in Chromium, this does not seem to get optimized out, even when all we're doing is constructing a std::string from a string literal. The compiler should know that string literals cannot alias with much of anything.

I put together this minimal program in godbolt and it seems, even with -O3, Clang (and GCC) do not optimize these checks out. That suggests Clang (and GCC) just don't know how to optimize this pattern? Separately, I'm not sure if the check is correct. See the comment about the "fixed" versions.
https://godbolt.org/z/oef93T9xb

Metadata

Metadata

Assignees

Labels

hardeningIssues related to the hardening effortlibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.llvm:optimizations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions