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

[LifetimeSafety] provide more details on lifetimebound violation

Open firewave opened this issue 4 months ago • 18 comments

#include <vector>

struct S
{
    S(const std::vector<int>& v); // missing annotation
};

S f2(const std::vector<int>& v [[clang::lifetimebound]])
{
    return {v}; // actual violation location
}

S f1(const std::vector<int>& v [[clang::lifetimebound]])
{
    return f2(v);
}
<source>:8:34: warning: could not verify that the return value can be lifetime bound to 'v' [-Wlifetime-safety-lifetimebound-violation]
    8 | S f2(const std::vector<int>& v [[clang::lifetimebound]])
      |                                  ^~~~~~~~~~~~~~~~~~~~

https://godbolt.org/z/Wex6xaW1n

firewave avatar Jul 15 '26 13:07 firewave

Encountered in https://github.com/cppcheck-opensource/simplecpp/pull/676.

In our case f2() has branched code which calls conditional overloaded constructors so it is not clear where the issue lies. We could slap the annotation on each one which is apparently missing it but it would be better if it could be done tool-driven i.e. hint at the actual code which is at fault.

firewave avatar Jul 15 '26 13:07 firewave

The nesting in the example above is unnecessary. The constructor called in f2() simply lacks the annotation.

The original code reports the violation on f1(). I will file a different ticket about this after I have a minimal reproducer.

firewave avatar Jul 15 '26 13:07 firewave

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

Author: Oliver Stöneberg (firewave)

```cpp #include <vector>

struct S { S(const std::vector<int>& v); // missing annotation };

S f2(const std::vector<int>& v [[clang::lifetimebound]]) { return {v}; // actual violation location }

S f1(const std::vector<int>& v [[clang::lifetimebound]]) { return f2(v); }


<source>:8:34: warning: could not verify that the return value can be lifetime bound to 'v' [-Wlifetime-safety-lifetimebound-violation] 8 | S f2(const std::vector<int>& v [[clang::lifetimebound]]) | ^~~~~~~~~~~~~~~~~~~~


https://godbolt.org/z/Wex6xaW1n
</details>

Hi mantainers , I am interested in doing this work . Please assign this to me

Udaytaneja avatar Jul 15 '26 14:07 Udaytaneja

@EugeneZelenko this is related to a compiler warning so the label was appropriate.

@Udaytaneja In the example I have given the additional details are obvious but in a more complex function it might not be. If v is not passed along at all it would also be a clear case. But what if it is passed into several functions/contexts - how would yo decide that some/one of them is lacking the annotation. I think we should wait for some feedback of the actual maintainers before diving into this functionality.

firewave avatar Jul 15 '26 18:07 firewave

@EugeneZelenko this is related to a compiler warning so the label was appropriate.

As far as I understand, -Wlifetime-safety family of warnings are implemented in clang/lib/Analysis/LifetimeSafety part, so they are separated from the rest of diagnostics.

EugeneZelenko avatar Jul 15 '26 18:07 EugeneZelenko

I also think we should discuss this before implementing any changes. One approach I can see is the following:

Maintain a mapping from each lifetimebound parameter in the function being analyzed to any unannotated callee parameters that may cause its loan propagation to be lost.

We would run the analysis as usual. When analyzing a function with a 'lifetimebound' parameter, we would keep track of calls through which that parameter’s loan is expected to propagate. If a call produces no origin-flow facts for the corresponding callee parameter, we would record that parameter pair in the map.

Then, when reporting a 'lifetimebound' violation, we could check whether the reported parameter appears in the map. If it does, we could emit notes on the corresponding callee parameters along the lines of:

potentially missing 'lifetimebound' annotation here

The main concern is that this could become quite noisy, especially when the value is passed to multiple functions.

I don't really see any other way we can approach this (the implementation details may differ, but the high level concept I see is the same), since we can't look Into what the called function is doing and the best approach (that I can see) is to take a guess.

@Xazax-hun @usx95 WDYT?

NeKon69 avatar Jul 16 '26 05:07 NeKon69

I think we could potentially do this the other way around too. Tracking the set of returned loans and where they are coming from.

And in case there is an expression that has the parameter's placeholder loan as an input, and a returned loan as an output, that is suspicious. But yeah, this is all just a heuristic.

Xazax-hun avatar Jul 16 '26 09:07 Xazax-hun

@firewave Thanks for the clarification. That makes sense—I agree it would be better to wait for feedback from the lifetime-safety maintainers before deciding on an implementation.

I'll hold off for now and follow the discussion. Once there's consensus on the expected behavior or diagnostic, I'd be happy to work on it if it's still available.

Udaytaneja avatar Jul 16 '26 10:07 Udaytaneja

I think we could potentially do this the other way around too. Tracking the set of returned loans and where they are coming from.

And in case there is an expression that has the parameter's placeholder loan as an input, and a returned loan as an output, that is suspicious. But yeah, this is all just a heuristic.

I see, that will allow us to separate every function call that takes in that parameter from the ones that actually contribute to the return value.

This looks like a pretty complex issue though, since the return value of call expressions does not carry any loans (due to missing lifetiembound annotation) reconstructing the flow will be cumbersome.

NeKon69 avatar Jul 17 '26 09:07 NeKon69

We can introduce a placeholder loan for these cases.

Xazax-hun avatar Jul 17 '26 09:07 Xazax-hun

We can introduce a placeholder loan for these cases.

Do you mean creating a new loan each time a function is called? Hm, yeah, that can simplify things. Though I'm wondering, is it's ok to introduce a new loan type that is there just for this one diagnostic?

NeKon69 avatar Jul 17 '26 10:07 NeKon69

Maybe take a step back and do not try to solve exactly what I was suggesting but rather just improve the current diagnostic and rather report things like:

  • annotated parameter is not used at all
  • annotated parameter is never passed through
  • annotated parameter is passed through to function with no annotation
  • etc.

The first two would make it clear that the annotation is unnecessary whereas the latter suggests that you need to review your code.

firewave avatar Jul 17 '26 12:07 firewave

An annotated parameter not used is also a parameter not used and we already have a warning for unused parameters. An annotated parameter passed to a function with no annotations in not necessarily a bug, since that function might not return anything that is lifetimebound to any of its arguments. But it is possible I am missing something.

Xazax-hun avatar Jul 17 '26 13:07 Xazax-hun

An annotated parameter not used is also a parameter not used and we already have a warning for unused parameters.

True, if those are enabled and in the past I encountered a lot of code bases which do not enable them. Also it would be a valid detail to report for the warning.

An annotated parameter passed to a function with no annotations in not necessarily a bug, since that function might not return anything that is lifetimebound to any of its arguments.

But that might be the source of the violation and thus would qualify as something to show as details.

firewave avatar Jul 17 '26 14:07 firewave

But that might be the source of the violation and thus would qualify as something to show as details.

I think the approach @Xazax-hun suggested is superior in this scenario. Since we'll be able to trace the actual path the parameter takes from the return statement. With just marking any function that takes in that parameter, we will get much more noisy diagnostics:

int* foo(int* x [[clang::lifetiembound]]) {
  log(x);         // will be flagged but does not need an annotation
  return view(x); // also will be flagged but actually needs an annotation
}

NeKon69 avatar Jul 17 '26 15:07 NeKon69

With just marking any function that takes in that parameter, we will get much more noisy diagnostics:

int* foo(int* x [[clang::lifetiembound]]) { log(x); // will be flagged but does not need an annotation return view(x); // also will be flagged but actually needs an annotation }

Ah, that's where the misunderstanding is. I wasn't suggesting to flag any function at all. Just have like a context summary as an incremental improvement to keep the complexity down:

  • if not used -> not used
  • if not passed through at all -> not passed through
  • else -> a function which is passed through to might lack the annotation

firewave avatar Jul 17 '26 15:07 firewave

Oh if you don't mean to mention any specific called functions, then yes, this can also work. Thought that's quite different from what you originally proposed in this issue.

NeKon69 avatar Jul 17 '26 18:07 NeKon69