llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Codegen for type-aware allocation attempts to compute the size of an incomplete type when building in c++17 mode

Open ojhunt opened this issue 4 months ago • 8 comments

I've been trying to build a self-targeting fuzzer that does not involve LLMs by working with the various builtin clang AST tooling. Focused solely on operator new because that is something I found bugs in today. Currently it is in no way scalable :D

That aside it found a bug \o/

Prior to c++23 deleting an incomplete object was UB but only produced a warning. In c++23 it became a hard error, but the type-aware allocator logic assumes the c++23 behaviour. As a result building this with clang in c++17 mode this triggers an assertion during codegen:

class Foo;
typedef __SIZE_TYPE__ size_t;
namespace std {
  enum class align_val_t : size_t {};
  template <class T> struct type_identity {
    typedef T type;
  };
}
template <class T> void operator delete(std::type_identity<T>, void *, size_t, std::align_val_t);

void f(Foo *o) {
  delete o;
}

With libc++ this requires a manual declaration of std::type_identity as it availability gates on the c++ version, I think stdlibc++ does not apply this restriction.

Two possible fixes:

  1. Explicitly limit type-aware allocators to >= c++23
  2. Reject/don't attempt type aware delete of incomplete types

I think there are reasonable arguments in both directions.

ojhunt avatar Jul 29 '26 03:07 ojhunt

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Oliver Hunt (ojhunt)

I've been trying to build a self-targeting fuzzer that does not involve LLMs by working with the various builtin clang AST tooling. Focused solely on operator new because that is something I found bugs in today. Currently it is in no way scalable :D

That aside it found a bug \o/

Prior to c++23 deleting an incomplete object was UB but only produced a warning. In c++23 it became a hard error, but the type-aware allocator logic assumes the c++23 behaviour. As a result building this with clang in c++17 mode this triggers an assertion during codegen:

class Foo;
typedef __SIZE_TYPE__ size_t;
namespace std {
  enum class align_val_t : size_t {};
  template &lt;class T&gt; struct type_identity {
    typedef T type;
  };
}
template &lt;class T&gt; void operator delete(std::type_identity&lt;T&gt;, void *, size_t, std::align_val_t);

void f(Foo *o) {
  delete o;
}

With libc++ this requires a manual declaration of std::type_identity as it availability gates on the c++ version, I think stdlibc++ does not apply this restriction.

Two possible fixes:

  1. Explicitly limit type-aware allocators to >= c++23
  2. Reject/don't attempt type aware delete of incomplete types

I think there are reasonable arguments in both directions.

Hey @ojhunt I'd like to work on this

ankit-cybertron avatar Jul 29 '26 04:07 ankit-cybertron

I've been trying to build a self-targeting fuzzer that does not involve LLMs by working with the various builtin clang AST tooling. Focused solely on operator new because that is something I found bugs in today. Currently it is in no way scalable :D

That aside it found a bug \o/

Prior to c++23 deleting an incomplete object was UB but only produced a warning. In c++23 it became a hard error, but the type-aware allocator logic assumes the c++23 behaviour. As a result building this with clang in c++17 mode this triggers an assertion during codegen:

class Foo; typedef SIZE_TYPE size_t; namespace std { enum class align_val_t : size_t {}; template struct type_identity { typedef T type; }; } template void operator delete(std::type_identity, void *, size_t, std::align_val_t);

void f(Foo *o) { delete o; }

With libc++ this requires a manual declaration of std::type_identity as it availability gates on the c++ version, I think stdlibc++ does not apply this restriction.

Two possible fixes:

1. Explicitly limit type-aware allocators to >= c++23

2. Reject/don't attempt type aware delete of incomplete types

I think there are reasonable arguments in both directions.

Hi, just to get a clarification (am not working on this as someone has already chimed in) what are the arguments for not proceeding with limiting the type aware allocators to cpp23? Seems like the most practical thing to do here as option 2 resorts to compiler trying to fix the code that should not compile in the first place (unless am mistaken)

nots1dd avatar Jul 29 '26 10:07 nots1dd

Hey @ojhunt,

I'd like to work on this issue as well. I know @ankit-cybertron is already working on it, but I'm new to LLVM and mainly want to use this as a learning opportunity.

My goal isn't to compete for the issue or have my PR accepted over anyone else's. I just want to work through the implementation, receive feedback on what I did right or wrong, and better understand the project so I can gain the confidence to contribute to more complex issues in the future.

I completely understand that reviewers' time is valuable, and I don't want to create unnecessary duplicate work. That said, would it be okay if I submitted my implementation for review purely for learning purposes, if someone has the time to look at it?

linisha15 avatar Jul 30 '26 06:07 linisha15

Hi, just to get a clarification (am not working on this as someone has already chimed in) what are the arguments for not proceeding with limiting the type aware allocators to cpp23? Seems like the most practical thing to do here as option 2 resorts to compiler trying to fix the code that should not compile in the first place (unless am mistaken)

I think you're right, I'm unsure why I thought the other way would even work let alone be right

ojhunt avatar Jul 31 '26 04:07 ojhunt

Hi @ankit-cybertron @linisha15, Are you guys still working on this? If not, I'd be happy to work on this issue

danielle060821 avatar Sep 12 '26 18:09 danielle060821

Hi @danielle060821, thanks for checking! Yes, I’m still working on this issue. I have an active PR open as #213455, so I’m waiting for further feedback from the maintainers.

ankit-cybertron avatar Sep 14 '26 04:09 ankit-cybertron