Skip to content

[libc] Added missing operator delete generated by gcc/clang #67457

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 1 commit into from
Jul 17, 2024

Conversation

mikhailramalho
Copy link
Member

This patch adds two operator delete functions that are being generated by clang 15 on rv32 (operator delete(void *mem, std::align_val_t)) and by gcc 13 on intel 64 (operator delete(void *mem, unsigned long)).

@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2023

@llvm/pr-subscribers-libc

Changes

This patch adds two operator delete functions that are being generated by clang 15 on rv32 (operator delete(void *mem, std::align_val_t)) and by gcc 13 on intel 64 (operator delete(void *mem, unsigned long)).


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

1 Files Affected:

  • (modified) libc/test/UnitTest/HermeticTestUtils.cpp (+19)
diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index 73d54b9eeb5ecc9..d4628091f30fac2 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -109,3 +109,22 @@ void operator delete(void *) {
   // we just trap here to catch any such accidental usages.
   __builtin_trap();
 }
+
+// Defining members in the std namespace is not preferred. But, we do it here
+// so that we can use it to define the operator new which takes std::align_val_t
+// argument.
+namespace std {
+enum class align_val_t : size_t {};
+} // namespace std
+
+void operator delete(void *mem, std::align_val_t) noexcept {
+  // The libc runtime should not use the global delete operator. Hence,
+  // we just trap here to catch any such accidental usages.
+  __builtin_trap();
+}
+
+void operator delete(void *mem, unsigned long) noexcept {
+  // The libc runtime should not use the global delete operator. Hence,
+  // we just trap here to catch any such accidental usages.
+  __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.

Wouldn't we also need this in the IntegrationTests?

@mikhailramalho
Copy link
Member Author

Wouldn't we also need this in the IntegrationTests?

It's not an issue in the integration tests currently

@jhuber6
Copy link
Contributor

jhuber6 commented Sep 26, 2023

Wouldn't we also need this in the IntegrationTests?

It's not an issue in the integration tests currently

Do you happen to know why this appears here and not there? I remember there was a recent patch that made the version with the alignment the default somewhere. Maybe we don't have any tests in integration that call delete?

@mikhailramalho
Copy link
Member Author

mikhailramalho commented Sep 28, 2023

Do you happen to know why this appears here and not there?

I'm still investigating, but it seems that the missing references are coming from the test classes, like StrtoTest, LlvmLibcStrtoimaxTest, etc.:

/usr/bin/ld: projects/libc/test/src/inttypes/CMakeFiles/libc.test.src.inttypes.strtoimax_test.__hermetic__.__build__.dir/strtoimax_test.cpp.o: in function `__llvm_libc_18_0_0_git::testing::Test::~Test()':
/home/mgadelha/tools/llvm-project/libc/test/UnitTest/LibcTest.h:112: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: projects/libc/test/src/inttypes/CMakeFiles/libc.test.src.inttypes.strtoimax_test.__hermetic__.__build__.dir/strtoimax_test.cpp.o: in function `StrtoTest<long>::~StrtoTest()':
/home/mgadelha/tools/llvm-project/libc/test/src/stdlib/StrtolTest.h:29: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: projects/libc/test/src/inttypes/CMakeFiles/libc.test.src.inttypes.strtoimax_test.__hermetic__.__build__.dir/strtoimax_test.cpp.o: in function `LlvmLibcStrtoimaxTest_AutomaticBaseSelection::~LlvmLibcStrtoimaxTest_AutomaticBaseSelection()':
/home/mgadelha/tools/llvm-project/libc/test/src/inttypes/strtoimax_test.cpp:15: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: projects/libc/test/src/inttypes/CMakeFiles/libc.test.src.inttypes.strtoimax_test.__hermetic__.__build__.dir/strtoimax_test.cpp.o: in function `LlvmLibcStrtoimaxTest_MessyBaseSixteenDecode::~LlvmLibcStrtoimaxTest_MessyBaseSixteenDecode()':
/home/mgadelha/tools/llvm-project/libc/test/src/inttypes/strtoimax_test.cpp:15: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: projects/libc/test/src/inttypes/CMakeFiles/libc.test.src.inttypes.strtoimax_test.__hermetic__.__build__.dir/strtoimax_test.cpp.o: in function `LlvmLibcStrtoimaxTest_CleanBaseSixteenDecode::~LlvmLibcStrtoimaxTest_CleanBaseSixteenDecode()':
/home/mgadelha/tools/llvm-project/libc/test/src/inttypes/strtoimax_test.cpp:15: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: projects/libc/test/src/inttypes/CMakeFiles/libc.test.src.inttypes.strtoimax_test.__hermetic__.__build__.dir/strtoimax_test.cpp.o:/home/mgadelha/tools/llvm-project/libc/test/src/inttypes/strtoimax_test.cpp:15: more undefined references to `operator delete(void*, unsigned long)' follow

I remember there was a recent patch that made the version with the alignment the default somewhere. Maybe we don't have any tests in integration that call delete?

I think @sivachandra mentioned on discord that changes were not required in the end. I can't find any commit that mentions the operator delete

@mikhailramalho
Copy link
Member Author

These operator delete(void*, unsigned long) calls are only being generated for test classes with arrays in their run() methods, e.g.:

  • bsearch_test.cpp:
TEST(LlvmLibcBsearchTest, IntegerArray) {
  constexpr int ARRAY[25] = {10,   23,   33,    35,   55,   70,   71,
                             100,  110,  123,   133,  135,  155,  170,
                             171,  1100, 1110,  1123, 1133, 1135, 1155,
                             1170, 1171, 11100, 12310};
  • qsort_r_test.cpp:
TEST(LlvmLibcQsortRTest, ReverseSortedArray) {
  int array[25] = {25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
                   12, 11, 10, 9,  8,  7,  6,  5,  4,  3,  2,  1};
  • bitset_test.cpp:
TEST(LlvmLibcBitsetTest, SettingBitXDoesNotResetBitY) {
  LIBC_NAMESPACE::cpp::bitset<128> bitset;

@mikhailramalho
Copy link
Member Author

I also just noticed a new warning by GCC 13:

libc/test/UnitTest/HermeticTestUtils.cpp:107:6: warning: the program should also define ‘void operator delete(void*, long unsigned int)’ [-Wsized-deallocation]
  107 | void operator delete(void *) {
      |      ^~~~~~~~

This patch adds two operator delete functions that are being generated
by clang 15 on rv32 (operator delete(void *mem, std::align_val_t)) and
by gcc 13 on intel 64 (operator delete(void *mem, unsigned long)).
@mikhailramalho mikhailramalho force-pushed the libc-operator-delete branch from 2d0e2f7 to 6dd3fd7 Compare July 5, 2024 12:35
@mikhailramalho mikhailramalho requested a review from lntue July 5, 2024 12:36
@lntue lntue requested a review from jhuber6 July 5, 2024 13:53
@mikhailramalho
Copy link
Member Author

ping

We need this patch to enable fullbuild for rv32

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.

You likely need to add these to the integration test.cpp as well.

@mikhailramalho
Copy link
Member Author

You likely need to add these to the integration test.cpp as well.

integration tests build even without this patch

@jhuber6
Copy link
Contributor

jhuber6 commented Jul 11, 2024

You likely need to add these to the integration test.cpp as well.

integration tests build even without this patch

Probably because they don't hit the same code paths, but I feel like we should do it just in case.

@mikhailramalho
Copy link
Member Author

mikhailramalho commented Jul 11, 2024 via email

@mikhailramalho
Copy link
Member Author

You likely need to add these to the integration test.cpp as well.

integration tests build even without this patch

Probably because they don't hit the same code paths, but I feel like we should do it just in case.

Do yo mean

// See https://llvm.org/LICENSE.txt for license information.
?

I still prefer not to add code unless it's needed. Will you block the PR if I don't add the code there?

@jhuber6
Copy link
Contributor

jhuber6 commented Jul 15, 2024

You likely need to add these to the integration test.cpp as well.

integration tests build even without this patch

Probably because they don't hit the same code paths, but I feel like we should do it just in case.

Do yo mean

// See https://llvm.org/LICENSE.txt for license information.

?

I still prefer not to add code unless it's needed. Will you block the PR if I don't add the code there?

Honestly, these files are practically the same so we could probably just merge them into a single object library or something.

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.

The integration test stuff doesn't have any of these operators, so I guess it's moot. That being said, this is pretty much just a wrapper to work in a hermetic environment given LIBC_NAMESPACE qualified identifiers, we could merge them in the future.

@mikhailramalho mikhailramalho merged commit 73799b4 into llvm:main Jul 17, 2024
6 checks passed
@mikhailramalho mikhailramalho deleted the libc-operator-delete branch July 17, 2024 15:20
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary: This patch adds two operators delete that are being generated by clang 15 on rv32 (operator delete(void *mem, std::align_val_t)) and by gcc 13 on intel 64 (operator delete(void *mem, unsigned long)).

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251538
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.

3 participants