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

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Jan 21, 2024

-nostdlib++ is a clang-only flag. Replacing it with -nostdlib when building with gcc.

@llvmbot
Copy link
Member

llvmbot commented Jan 21, 2024

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

-nostdlib++ is a clang-only flag. Replacing it with -nostdlib when building with gcc.


Full diff: https://github.com/llvm/llvm-project/pull/78906.diff

2 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+6-2)
  • (modified) libc/test/UnitTest/HermeticTestUtils.cpp (+4)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 24f543f6e4c132..211188da801524 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -564,8 +564,10 @@ function(add_integration_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  else()
+  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
+  else()
+    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
   endif()
   target_link_libraries(
     ${fq_build_target_name}
@@ -741,8 +743,10 @@ 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(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
+  else()
+    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
   endif()
   target_link_libraries(
     ${fq_build_target_name}
diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index 68e31f478d79ab..349c182ff2379f 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -104,6 +104,8 @@ void *__dso_handle = nullptr;
 
 } // extern "C"
 
+void *operator new(unsigned long size, void *ptr) { return ptr; }
+
 void *operator new(size_t size) { return malloc(size); }
 
 void *operator new[](size_t size) { return malloc(size); }
@@ -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(); }

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Does this work with -nostdlib on clang? If so it's probably better to be consistent.

@SchrodingerZhu
Copy link
Contributor

// Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls

Is this still true with COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC?

@nickdesaulniers
Copy link
Member

gcc-13 added support for -nostdlib++. Perhaps it's worth using check_cxx_compiler_flag to check for -nostdlib++ support, then fall back to -nostdlib if not?

Copy link
Contributor

@SchrodingerZhu SchrodingerZhu left a comment

Choose a reason for hiding this comment

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

The change itself looks good to me. Just curious that if COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC guarantees the compatibility.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

overall LGTM

@@ -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."

@lntue lntue merged commit 029bfd6 into llvm:main Jan 23, 2024
@lntue lntue deleted the gcc branch January 23, 2024 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants