LibC+Kernel: Prevent string functions from calling themselves

Most of the string.h and wchar.h functions are implemented quite naively
at the moment, and GCC's pattern recognition pass might realize what we
are trying to do, and transform them into libcalls. This is usually a
useful optimization, but not when we're implementing the functions
themselves :^)

Relevant discussion from the GCC Bugzilla:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102725

This prevents the infamous recursive `strlen`.

A more proper fix would be writing these functions in assembly. That
would likely give a small performance boost as well ;)
This commit is contained in:
Daniel Bertalan 2022-05-07 18:11:00 +02:00 committed by Andreas Kling
parent f157ad8a35
commit fd3e3d5e28
Notes: sideshowbarker 2024-07-17 17:38:29 +09:00
3 changed files with 16 additions and 0 deletions

View File

@ -486,6 +486,11 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(TARGET_STRING "")
# Prevent naively implemented string functions (like strlen) from being "optimized" into a call to themselves.
set_source_files_properties(MiniStdlib.cpp
PROPERTIES COMPILE_FLAGS "-fno-tree-loop-distribution -fno-tree-loop-distribute-patterns")
add_link_options(LINKER:-z,pack-relative-relocs)
else() # Assume Clang
add_compile_options(-Waddress-of-packed-member)

View File

@ -36,6 +36,12 @@ set_source_files_properties (../Libraries/LibC/ssp.cpp PROPERTIES COMPILE_FLAGS
# Prevent GCC from removing null checks by marking the `FILE*` argument non-null
set_source_files_properties(../Libraries/LibC/stdio.cpp PROPERTIES COMPILE_FLAGS "-fno-builtin-fputc -fno-builtin-fputs -fno-builtin-fwrite")
# Prevent naively implemented string functions (like strlen) from being "optimized" into a call to themselves.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_source_files_properties(../Libraries/LibC/string.cpp ../Libraries/LibC/wchar.cpp
PROPERTIES COMPILE_FLAGS "-fno-tree-loop-distribution -fno-tree-loop-distribute-patterns")
endif()
add_executable(Loader.so ${SOURCES})
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

View File

@ -142,6 +142,11 @@ set_source_files_properties(stdio.cpp PROPERTIES COMPILE_FLAGS "-fno-builtin-fpu
# Add in the `posix_memalign` symbol to avoid breaking existing binaries.
set_source_files_properties(stdlib.cpp PROPERTIES COMPILE_FLAGS "-DSERENITY_LIBC_SHOW_POSIX_MEMALIGN")
# Prevent naively implemented string functions (like strlen) from being "optimized" into a call to themselves.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_source_files_properties(string.cpp wchar.cpp PROPERTIES COMPILE_FLAGS "-fno-tree-loop-distribution -fno-tree-loop-distribute-patterns")
endif()
add_library(LibCStaticWithoutDeps STATIC ${SOURCES})
target_link_libraries(LibCStaticWithoutDeps PUBLIC ssp LibTimeZone PRIVATE NoCoverage)
add_dependencies(LibCStaticWithoutDeps LibM LibSystem LibUBSanitizer)