Skip to content

[BoundsSafety] Do not check for bounds-attributed output arguments in dependent contexts #9733

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
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DependenceFlags.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/AST/Expr.h"
Expand Down Expand Up @@ -8332,7 +8333,7 @@ static bool checkDynamicCountPointerAsParameter(Sema &S, FunctionDecl *FDecl,
if (!ArgInfo.isCountInParamOrCountPointer() && !ArgInfo.isCountInRet() &&
!ParmInfo.isCountInParamOrCountPointer() && !ParmInfo.isCountInRet())
continue;
assert(!ActualArgExp->isValueDependent());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is it still necessary to remove this, despite checking !TheCall->getDependence() before calling checkDynamicCountPointerAsParameter? Or is this something else?

Copy link
Author

@ziqingluo-90 ziqingluo-90 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!TheCall->getDependence() implies no argument isValueDependent(). So I think that assertion becomes unreachable.

This is where I got the sense that Call->getDependence() includes dependence info of its arguments:
https://github.com/llvm/llvm-project/blob/2916352936097a35cdcaaf38a9097465adbf8cf5/clang/lib/AST/ComputeDependence.cpp#L642C1-L654C2


// Disable these checks for attribute-only mode because we don't want
// non-type-incompatibility errors in that mode.
if (!S.getLangOpts().isBoundsSafetyAttributeOnlyMode() &&
Expand Down Expand Up @@ -8903,7 +8904,10 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl,
if (getLangOpts().BoundsSafetyAttributes && FDecl) {
// FIXME: We need to support function pointers and blocks that don't have
// function decl.
if (!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))

// For C++, we don't want to check for any dependent constructs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe instead of casting enum to bool, we could be explicit TheCall->getDependence() == ExprDependence::None? We could also move the comment before if.

Copy link

@rapidsna rapidsna Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "For C++, we don't want to check for any dependent constructs."

if (TheCall->getDependence() == ExprDependence::None &&
!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))
return ExprError();
if (getLangOpts().BoundsSafety)
if (!checkDynamicRangePointerAsParameter(*this, FDecl, TheCall))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ class MyClass {
namespace value_dependent_assertion_violation {
// Running attribute-only mode on the C++ code below crashes
// previously.
void g(unsigned);
void g(int * __counted_by(n) * p, size_t n);

template<typename T>
struct S {
void f(T p) {
g(sizeof(p));
g(nullptr, sizeof(p));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we certain this still exercises the same path as caused the crash previously, or should we keep the old function and add another?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old one was testing for no type checking in a dependent context, if there is no bounds attribute. Now we are testing that the same path will not be reached under a stronger condition, i.e., no type checking in a dependent context.

So the new test is stronger and covers the old one after the change of this PR.

}
};

// expected-error@-4{{incompatible dynamic count pointer argument to parameter of type 'int * __counted_by(n)*' (aka 'int **')}}
template struct S<int>; // expected-note{{in instantiation of member function 'value_dependent_assertion_violation::S<int>::f' requested here}}
}