Skip to content

Add lifetimebound to ArrayRef and StringRef to detect dangling issues #113533

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

Closed
hokein opened this issue Oct 24, 2024 · 3 comments
Closed

Add lifetimebound to ArrayRef and StringRef to detect dangling issues #113533

hokein opened this issue Oct 24, 2024 · 3 comments
Labels
clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)

Comments

@hokein
Copy link
Collaborator

hokein commented Oct 24, 2024

Adding the lifetimebound annotation to the ArrayRef's array constructor can enable us to detect the following use-after-free issues:

ArrayRef<int> test() {
   int array[10];
   return array; // bug, return a stack local address
}

cc @Xazax-hun, @usx95

@hokein hokein added the clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) label Oct 24, 2024
@Xazax-hun
Copy link
Collaborator

Probably people run into that less frequently, but we could annotate all containers in ADT including small vector and such. They have a number of APIs handing out pointers and references that have the same lifetime as the container itself.

hokein added a commit that referenced this issue Oct 28, 2024
This enables clang to detect more dangling issues.

```
ArrayRef<int> func() {
   constexpr int array[] = {...}; // oops, missing the static
   return array; // return a dangling reference, bomb.
}
```

See #113533.
hokein added a commit that referenced this issue Oct 28, 2024
Adding the lifetimebound annotation to the ArrayRef's array constructor
can enable us to detect the following use-after-free issues:

```
llvm::StringRef TestZoneName() {
   char test[] = "foo"; // oops, missing static
   return test; // use-after-free.
}
```

See #113533
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this issue Nov 4, 2024
…13547)

This enables clang to detect more dangling issues.

```
ArrayRef<int> func() {
   constexpr int array[] = {...}; // oops, missing the static
   return array; // return a dangling reference, bomb.
}
```

See llvm#113533.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this issue Nov 4, 2024
…113878)

Adding the lifetimebound annotation to the ArrayRef's array constructor
can enable us to detect the following use-after-free issues:

```
llvm::StringRef TestZoneName() {
   char test[] = "foo"; // oops, missing static
   return test; // use-after-free.
}
```

See llvm#113533
@hokein
Copy link
Collaborator Author

hokein commented Nov 6, 2024

Probably people run into that less frequently, but we could annotate all containers in ADT including small vector and such. They have a number of APIs handing out pointers and references that have the same lifetime as the container itself.

Thanks for the idea, annotating all containers in ADT could certainly help.

My intention of this issue primarily targets the ArrayRef and StringRef constructors, which have already discovered some bugs in our internal codebase.

@hokein
Copy link
Collaborator Author

hokein commented Nov 6, 2024

Closing this issue now, as annotations for both ArrayRef and StringRef have been added.

@hokein hokein closed this as completed Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)
Projects
None yet
Development

No branches or pull requests

2 participants