Skip to content

[libc] Replace -nostdlib++ flag when building with gcc and add placement new operator to HermeticTestUtils.cpp. #78906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/cmake/modules/CheckCompilerFeatures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")

# clang-8+, gcc-12+
check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)

# clang-6+, gcc-13+
check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
21 changes: 19 additions & 2 deletions libc/cmake/modules/LLVMLibCTestRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ function(add_libc_fuzzer target_name)

endfunction(add_libc_fuzzer)

# Get libgcc_s to be used in hermetic and integration tests.
if(NOT LIBC_CC_SUPPORTS_NOSTDLIBPP)
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc_s.so.1
OUTPUT_VARIABLE LIBGCC_S_LOCATION)
string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION)
endif()

# DEPRECATED: Use add_hermetic_test instead.
#
# Rule to add an integration test. An integration test is like a unit test
Expand Down Expand Up @@ -564,8 +571,13 @@ function(add_integration_test test_name)

if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
else()
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
else()
# Older version of gcc does not support `nostdlib++` flag. We use
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
endif()
target_link_libraries(
${fq_build_target_name}
Expand Down Expand Up @@ -741,8 +753,13 @@ function(add_libc_hermetic_test test_name)

if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
else()
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
else()
# Older version of gcc does not support `nostdlib++` flag. We use
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
endif()
target_link_libraries(
${fq_build_target_name}
Expand Down
4 changes: 4 additions & 0 deletions libc/test/UnitTest/HermeticTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void *__dso_handle = nullptr;

} // extern "C"

void *operator new(unsigned long size, void *ptr) { return ptr; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this okay or should it call realloc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is variation (9) in the list: https://en.cppreference.com/w/cpp/memory/new/operator_new
and it is said that: "The standard library implementation performs no action and returns ptr unmodified."


void *operator new(size_t size) { return malloc(size); }

void *operator new[](size_t size) { return malloc(size); }
Expand All @@ -113,3 +115,5 @@ void operator delete(void *) {
// we just trap here to catch any such accidental usages.
__builtin_trap();
}

void operator delete(void *ptr, size_t size) { __builtin_trap(); }