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

Feature request: annotation for maybe deleted this [clang:temporal-safety]

Open ricea opened this issue 5 months ago • 6 comments

In Chromium network code, it is common to have a method like HandleMessage() that may synchronously send a response message. Sending a response message may fail if there is no longer a route to the remote host, in which case cleanup code may have run and deleted the current object.

We generally handle this in one of two ways

  1. Use a weak pointer to keep track of whether this is still valid, or
  2. Since 1) is expensive, in simpler cases just have a comment saying // this may be deleted here after the call.

For the second case, it would be useful to have an annotation instead of a comment so that that the compiler can check that members are not accessed afterwards. For example:

  HandleMessage(message);
  [[this_maybe_deleted]];
  return;

If someone then added some code before return that relied on this, the compiler could issue a warning.

Another issue is that another method further up the stack may not be aware of the comment and may add code that touches this. To help prevent this, the annotation would also be useful as an attribute on function declarations, eg.

[[this_maybe_deleted]] void ReadThenHandleMessage();

Then code like

ReadThenHandleMessage();
RecordHistogram("Net.MessageCount", message_count_);

would also issue a warning.

Further enhancements could issue a warning if ReadThenHandleMessage() calls HandleMessage() which has the [[this_maybe_deleted]] annotation but ReadThenHandleMessage() itself doesn't have the annotation. Calling such a method from the destructor could also issue a warning.

Another possible enhancement would be to warn on incorrect use of the annotation. For example,

HandleMessage();
LOG(VERBOSE) << "Handled message with id " << last_id_;
[[this_maybe_deleted]];

is a bug which could be caught, assuming we can see inside the LOG macro to see that it will never delete this.

A common pattern in Chromium IPC code is to have an IPC handler which is owned by a factory object, and to call a method on the factory object to request deletion, something like:

factory_->RemoveHandler(this);
// this is deleted here.

In this case, this is always deleted, but it's probably not necessary to have a separate annotation for this case, as the warnings that need to be given are the same.

ricea avatar Jun 29 '26 12:06 ricea

@llvm/issue-subscribers-clang-temporal-safety

Author: Adam Rice (ricea)

In Chromium network code, it is common to have a method like `HandleMessage()` that may synchronously send a response message. Sending a response message may fail if there is no longer a route to the remote host, in which case cleanup code may have run and deleted the current object.

We generally handle this in one of two ways

  1. Use a weak pointer to keep track of whether this is still valid, or
  2. Since 1) is expensive, in simpler cases just have a comment saying // this may be deleted here after the call.

For the second case, it would be useful to have an annotation instead of a comment so that that the compiler can check that members are not accessed afterwards. For example:

  HandleMessage(message);
  [[this_maybe_deleted]];
  return;

If someone then added some code before return that relied on this, the compiler could issue a warning.

Another issue is that another method further up the stack may not be aware of the comment and may add code that touches this. To help prevent this, the annotation would also be useful as an attribute on function declarations, eg.

[[this_maybe_deleted]] void ReadThenHandleMessage();

Then code like

ReadThenHandleMessage();
RecordHistogram("Net.MessageCount", message_count_);

would also issue a warning.

Further enhancements could issue a warning if ReadThenHandleMessage() calls HandleMessage() which has the [[this_maybe_deleted]] annotation but ReadThenHandleMessage() itself doesn't have the annotation. Calling such a method from the destructor could also issue a warning.

Another possible enhancement would be to warn on incorrect use of the annotation. For example,

HandleMessage();
LOG(VERBOSE) &lt;&lt; "Handled message with id " &lt;&lt; last_id_;
[[this_maybe_deleted]];

is a bug which could be caught, assuming we can see inside the LOG macro to see that it will never delete this.

A common pattern in Chromium IPC code is to have an IPC handler which is owned by a factory object, and to call a method on the factory object to request deletion, something like:

factory_-&gt;RemoveHandler(this);
// this is deleted here.

In this case, this is always deleted, but it's probably not necessary to have a separate annotation for this case, as the warnings that need to be given are the same.

This sounds interesting as a concept! I was thinking about something similar like how we can come up with a new attribute we can place on parameters, but never thought of placing it on implicit this object. A problem with this is that this will likely introduce a bit of false positives. Consider this case:

int* delete_or_realloc(int* x [[clang::may_free]], bool flag) {
  if (flag)
    delete x;
  else
    new (x) int();
  return x;
}
void foo() {
  int* x = new int;
  delete_or_realloc(x, false);
  // Completely safe to use x here!
}

It all really depends on what the function is doing. (in the LifetimeSafety analysis at least, we can not see into the called functions) "may" is not a strong guarantee, and this will probably produce a lot of false positives/negatives, unless, of course, we make it more conservative in one way or another.

NeKon69 avatar Jun 29 '26 19:06 NeKon69

Also I don't think placing attributes like this in the middle of code is valid C++:

  HandleMessage(message);
  [[this_maybe_deleted]];
  return;

The attribute will most likely have to go on a function somewhere.

NeKon69 avatar Jun 29 '26 19:06 NeKon69

Also I don't think placing attributes like this in the middle of code is valid C++

It's valid. See for example https://en.cppreference.com/cpp/language/attributes/assume.

ricea avatar Jun 30 '26 08:06 ricea

Also I don't think placing attributes like this in the middle of code is valid C++

It's valid. See for example https://en.cppreference.com/cpp/language/attributes/assume.

Huh, interesting. Thanks for the correction. TIL!

NeKon69 avatar Jun 30 '26 08:06 NeKon69

A problem with this is that this will likely introduce a bit of false positives.

I noticed that clang splits out -Wlifetime-safety-suggestions separately from the other warnings, and I think that approach could work here. If you normally compile without that flag, then you can just leave out the annotation where you would otherwise get a false positive. But you can have a CI bot that compiles new code with suggestions enabled, so that they are still surfaced, but can be ignored if needed.

For your delete_or_realloc() example, I would just refactor it into delete_int() and realloc_int() functions, but obviously real-world cases are not so easy.

ricea avatar Jun 30 '26 08:06 ricea