This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Fix null pointer deref in CastValueChecker
ClosedPublic

Authored by vabridgers on Jun 6 2022, 5:39 AM.

Details

Summary

A crash was seen in CastValueChecker due to a null pointer dereference.

The fix uses QualType::getAsString to avoid the null dereference
when a CXXRecordDecl cannot be obtained. A small reproducer is added,
and cast value notes LITs are updated for the new debug messages.

Diff Detail

Event Timeline

vabridgers created this revision.Jun 6 2022, 5:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2022, 5:39 AM
vabridgers requested review of this revision.Jun 6 2022, 5:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2022, 5:39 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
vabridgers planned changes to this revision.Jun 6 2022, 11:40 AM

I know this will need a reproducer, and I'm working on that. That work is still in progress.

vabridgers updated this revision to Diff 434628.Jun 6 2022, 3:35 PM

add test case

I was able to find and add a covering test case. I understand the fix may not be "correct", but it does avoid the crash demonstrated by the test case.

vabridgers edited the summary of this revision. (Show Details)Jun 6 2022, 5:28 PM
vabridgers updated this revision to Diff 434746.Jun 7 2022, 2:37 AM

Use QualType::getAsString() per suggestion from martong

vabridgers edited the summary of this revision. (Show Details)Jun 7 2022, 2:39 AM

Likely related to https://github.com/llvm/llvm-project/issues/55715. Mention this in the summary as Fixes #55715.

clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp
168

So this was null, right? Which caused the crash.

clang/test/Analysis/cast-value-notes.cpp
306

It's good to know which line exactly caused the crash. Put this note right there.

309–310
311

This gotta be the getAs<T>. Please try to reconstruct the 'feel' of it; like return a T* instead of void etc.

clang/test/Analysis/cast-value-state-dump.cpp
26

IDK, I see the motivation, but we don't need the full name of these in most cases.
I find it more disturbing than helpful. I would instead stick to the shorter form.

martong added inline comments.Jun 7 2022, 4:11 AM
clang/test/Analysis/cast-value-state-dump.cpp
26

I see your point, but I this way, the checker provides more precise type info, which is good in my opinion.

Vince, please update the summary - it looks really weird. Along with that the content of it might not be much useful, as we have a test case to demonstrate the crash; you can probably remove those dumps etc.
Otherwise LGTM.

clang/test/Analysis/cast-value-state-dump.cpp
26

Okay. It's not that important. This is a domain-specific check only for llvm codebases, so I'm not too picky about it.

vabridgers added inline comments.Jun 7 2022, 5:43 AM
clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp
168

Yes, the call to "CastToTy->getPointeeCXXRecordDecl()" returned nullptr, which was then used to dereference getNameAsString(), then boom :)

clang/test/Analysis/cast-value-notes.cpp
306

Will address, thank you

311

I'll attempt a further simplification. This was the product of a very long and tedious manual and creduce reduction process from a 12M preprocessed file :/

steakhal added inline comments.Jun 7 2022, 6:10 AM
clang/test/Analysis/cast-value-notes.cpp
311

What I'm proposing is concretization.

Something like this:

template <typename> struct PointerUnion {
  template <typename T> T* getAs() {
    (void)isa<int>(*this);
    return nullptr;
}
vabridgers updated this revision to Diff 434821.Jun 7 2022, 8:18 AM

address @steakhal comments

vabridgers marked 8 inline comments as done.Jun 7 2022, 8:19 AM

I think all comments have been addressed, please let me know if I missed some detail :) Best!

vabridgers edited the summary of this revision. (Show Details)Jun 7 2022, 8:20 AM
steakhal accepted this revision.Jun 7 2022, 9:33 AM
This revision is now accepted and ready to land.Jun 7 2022, 9:33 AM

Submitted a backport request to land this in clang-14.0.5, since many llvm devs might be affected by this crash due to clangd and clang-tidy integration.
https://github.com/llvm/llvm-project/issues/55937