Skip to content

[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

Merged
merged 5 commits into from
Nov 19, 2020
Merged

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Nov 18, 2020

Found via a static-analysis tool:

  1. Inside leaveStruct()

RecordDecl *RD = Ty->getAsRecordDecl(); ---> Pointer 'RD' may be NULL
if (!RD->hasAttr()) ----> will be dereferenced here

  1. Inside visitRecord() function, Pointer 'RecordTy->getAsRecordDecl()' returned from
    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]

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]>
@smanna12 smanna12 marked this pull request as ready for review November 18, 2020 09:12
@smanna12 smanna12 requested a review from erichkeane November 18, 2020 09:25
Comment on lines 1217 to 1218
if (RecordTy->getAsRecordDecl() != nullptr &&
RecordTy->getAsRecordDecl()->hasAttr<SYCLRequiresDecompositionAttr>()) {
Copy link
Contributor

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.

Suggested change
if (RecordTy->getAsRecordDecl() != nullptr &&
RecordTy->getAsRecordDecl()->hasAttr<SYCLRequiresDecompositionAttr>()) {
RecordDecl *RD = RecordTy->getAsRecordDecl();
if (RD && RD->hasAttr<SYCLRequiresDecompositionAttr>()) {

@@ -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) {
Copy link
Contributor

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

Suggested change
if (PropDecl != nullptr && PropDecl->getTemplateArgs().size() != 1) {
if (PropDecl && PropDecl->getTemplateArgs().size() != 1) {

@@ -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>())
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Suggested change
if (RD != nullptr && !RD->hasAttr<SYCLRequiresDecompositionAttr>())
if (RD && !RD->hasAttr<SYCLRequiresDecompositionAttr>())

@@ -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>())
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Suggested change
if (RD != nullptr && !RD->hasAttr<SYCLRequiresDecompositionAttr>())
if (RD && !RD->hasAttr<SYCLRequiresDecompositionAttr>())

Signed-off-by: Soumi Manna <[email protected]>
@smanna12 smanna12 requested a review from Fznamznon November 18, 2020 11:48
Copy link
Contributor

@erichkeane erichkeane left a 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.

@@ -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>()) {
Copy link
Contributor

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.

@@ -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) {
Copy link
Contributor

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?

@@ -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>())
Copy link
Contributor

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.

@@ -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>())
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@@ -2703,6 +2704,8 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler {
QualType FieldTy) final {
const auto *AccTy =
cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl());
if (!AccTy)
Copy link
Contributor

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.

@@ -2717,6 +2720,8 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler {
bool handleSyclAccessorType(FieldDecl *FD, QualType FieldTy) final {
const auto *AccTy =
cast<ClassTemplateSpecializationDecl>(FieldTy->getAsRecordDecl());
if (!AccTy)
Copy link
Contributor

Choose a reason for hiding this comment

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

assert here as well.

Copy link
Contributor

@erichkeane erichkeane left a 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]>
@smanna12 smanna12 requested a review from erichkeane November 18, 2020 15:02
Copy link
Contributor

@erichkeane erichkeane left a 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.

@@ -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.");
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -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.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -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.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@erichkeane erichkeane left a 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.

@smanna12
Copy link
Contributor Author

Thank you everyone for the reviews.

@bader bader merged commit e1d8db7 into intel:sycl Nov 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants