-
Notifications
You must be signed in to change notification settings - Fork 788
[SYCL][NFC] Fix static code analysis concerns #2789
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
Conversation
Found via a static-analysis tool: 1. Inside visitRecord() Pointer 'RecordTy->getAsRecordDecl()' returned from call to function 'getAsRecordDecl' may be NULL and will be dereferenced at line below: “if (RecordTy->getAsRecordDecl()->hasAttr<SYCLRequiresDecompositionAttr>()) {“ 2. Inside leaveStruct() RecordDecl *RD = Ty->getAsRecordDecl(); --->Pointer 'RD' may be NULL if (!RD->hasAttr<SYCLRequiresDecompositionAttr>()) ---> will be dereferenced here 3. Inside leaveStruct() RecordDecl *RD = Ty->getAsRecordDecl(); ---> Pointer 'RD' may be NULL if (!RD->hasAttr<SYCLRequiresDecompositionAttr>()) ----> will be dereferenced here 4. Inside handleSyclAccessorType() const auto *AccTy = cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl()); ---> Pointer 'AccTy' may be NULL assert(AccTy->getTemplateArgs().size() >= 2 && ----> will be dereferenced here "Incorrect template args for Accessor Type"); 5. Inside handleSyclAccessorType() const auto *AccTy = cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl()); ---> Pointer 'AccTy' may be NULL assert(AccTy->getTemplateArgs().size() >= 2 && will be dereferenced here "Incorrect template args for Accessor Type"); 6. Inside checkBufferLocationType() const auto *PropDecl = cast<ClassTemplateSpecializationDecl>(PropTy->getAsRecordDecl()); -->Pointer 'PropDecl' may be NULL if (PropDecl->getTemplateArgs().size() != 1) {---> will be dereferenced here This patch fixes null pointer dereference issues in SemaDecl.cpp file. Signed-off-by: Soumi Manna <[email protected]>
Signed-off-by: Soumi Manna <[email protected]>
clang/lib/Sema/SemaSYCL.cpp
Outdated
if (RecordTy->getAsRecordDecl() != nullptr && | ||
RecordTy->getAsRecordDecl()->hasAttr<SYCLRequiresDecompositionAttr>()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest calling getAsRecordDecl()
once.
if (RecordTy->getAsRecordDecl() != nullptr && | |
RecordTy->getAsRecordDecl()->hasAttr<SYCLRequiresDecompositionAttr>()) { | |
RecordDecl *RD = RecordTy->getAsRecordDecl(); | |
if (RD && RD->hasAttr<SYCLRequiresDecompositionAttr>()) { |
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1351,7 +1352,7 @@ class SyclKernelFieldChecker : public SyclKernelFieldHandler { | |||
void checkBufferLocationType(QualType PropTy, SourceLocation Loc) { | |||
const auto *PropDecl = | |||
cast<ClassTemplateSpecializationDecl>(PropTy->getAsRecordDecl()); | |||
if (PropDecl->getTemplateArgs().size() != 1) { | |||
if (PropDecl != nullptr && PropDecl->getTemplateArgs().size() != 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make it simpler
if (PropDecl != nullptr && PropDecl->getTemplateArgs().size() != 1) { | |
if (PropDecl && PropDecl->getTemplateArgs().size() != 1) { |
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1583,7 +1584,7 @@ class SyclKernelDecompMarker : public SyclKernelFieldHandler { | |||
bool leaveStruct(const CXXRecordDecl *, FieldDecl *, QualType Ty) final { | |||
if (CollectionStack.pop_back_val()) { | |||
RecordDecl *RD = Ty->getAsRecordDecl(); | |||
if (!RD->hasAttr<SYCLRequiresDecompositionAttr>()) | |||
if (RD != nullptr && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
if (RD != nullptr && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) | |
if (RD && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) |
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1601,7 +1602,7 @@ class SyclKernelDecompMarker : public SyclKernelFieldHandler { | |||
QualType Ty) final { | |||
if (CollectionStack.pop_back_val()) { | |||
RecordDecl *RD = Ty->getAsRecordDecl(); | |||
if (!RD->hasAttr<SYCLRequiresDecompositionAttr>()) | |||
if (RD != nullptr && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
if (RD != nullptr && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) | |
if (RD && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) |
Signed-off-by: Soumi Manna <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patch changes control flow in REALLY bad error conditions, it should not.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1214,7 +1214,8 @@ void KernelObjVisitor::visitRecord(const CXXRecordDecl *Owner, ParentTy &Parent, | |||
const CXXRecordDecl *Wrapper, | |||
QualType RecordTy, | |||
HandlerTys &... Handlers) { | |||
if (RecordTy->getAsRecordDecl()->hasAttr<SYCLRequiresDecompositionAttr>()) { | |||
RecordDecl *RD = RecordTy->getAsRecordDecl(); | |||
if (RD && RD->hasAttr<SYCLRequiresDecompositionAttr>()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an assert here, not a test. If we are here and RD is null, something very wrong has happened.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1351,7 +1352,7 @@ class SyclKernelFieldChecker : public SyclKernelFieldHandler { | |||
void checkBufferLocationType(QualType PropTy, SourceLocation Loc) { | |||
const auto *PropDecl = | |||
cast<ClassTemplateSpecializationDecl>(PropTy->getAsRecordDecl()); | |||
if (PropDecl->getTemplateArgs().size() != 1) { | |||
if (PropDecl && PropDecl->getTemplateArgs().size() != 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an assert as well?
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1583,7 +1584,7 @@ class SyclKernelDecompMarker : public SyclKernelFieldHandler { | |||
bool leaveStruct(const CXXRecordDecl *, FieldDecl *, QualType Ty) final { | |||
if (CollectionStack.pop_back_val()) { | |||
RecordDecl *RD = Ty->getAsRecordDecl(); | |||
if (!RD->hasAttr<SYCLRequiresDecompositionAttr>()) | |||
if (RD && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely should be an assert.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1601,7 +1602,7 @@ class SyclKernelDecompMarker : public SyclKernelFieldHandler { | |||
QualType Ty) final { | |||
if (CollectionStack.pop_back_val()) { | |||
RecordDecl *RD = Ty->getAsRecordDecl(); | |||
if (!RD->hasAttr<SYCLRequiresDecompositionAttr>()) | |||
if (RD && !RD->hasAttr<SYCLRequiresDecompositionAttr>()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -2703,6 +2704,8 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler { | |||
QualType FieldTy) final { | |||
const auto *AccTy = | |||
cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl()); | |||
if (!AccTy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is possible either, so this should assert.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -2717,6 +2720,8 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler { | |||
bool handleSyclAccessorType(FieldDecl *FD, QualType FieldTy) final { | |||
const auto *AccTy = | |||
cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl()); | |||
if (!AccTy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert here as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should have done this before, making sure i 'request changes'.
Signed-off-by: Soumi Manna <[email protected]>
Signed-off-by: Soumi Manna <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my 1st review was before caffeine and I missed that these were the results of casts.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -2720,8 +2723,7 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler { | |||
bool handleSyclAccessorType(FieldDecl *FD, QualType FieldTy) final { | |||
const auto *AccTy = | |||
cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl()); | |||
if (!AccTy) | |||
return false; | |||
assert(AccTy && "should not be null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So actually upon reread, I think this is a false-positive. the 'cast' above should assert on null or wrong type, so this shouldn't be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @erichkeane for pointing this. I also missed the fact that we are using cast here.
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -2704,8 +2708,7 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler { | |||
QualType FieldTy) final { | |||
const auto *AccTy = | |||
cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl()); | |||
if (!AccTy) | |||
return false; | |||
assert(AccTy && "should not be null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clang/lib/Sema/SemaSYCL.cpp
Outdated
@@ -1352,7 +1353,8 @@ class SyclKernelFieldChecker : public SyclKernelFieldHandler { | |||
void checkBufferLocationType(QualType PropTy, SourceLocation Loc) { | |||
const auto *PropDecl = | |||
cast<ClassTemplateSpecializationDecl>(PropTy->getAsRecordDecl()); | |||
if (PropDecl && PropDecl->getTemplateArgs().size() != 1) { | |||
assert(PropDecl && "should not be null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave it to @Fznamznon and @elizabethandrews and @premanandrao to bikeshed assert messages if they want, but functionally this looks right.
Thank you everyone for the reviews. |
Found via a static-analysis tool:
RecordDecl *RD = Ty->getAsRecordDecl(); ---> Pointer 'RD' may be NULL
if (!RD->hasAttr()) ----> will be dereferenced here
call to function 'getAsRecordDecl' at line
“if (RecordTy->getAsRecordDecl()->hasAttr()) {“
may be NULL and will be dereferenced at same line.
This patch fixes null pointer dereference issues in SemaSYCL.cpp file.
Signed-off-by: Soumi Manna [email protected]