Skip to content
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

[analyzer] "non-static stackless VarRegion" crash #124975

Closed
wjristow opened this issue Jan 29, 2025 · 4 comments · Fixed by #125044
Closed

[analyzer] "non-static stackless VarRegion" crash #124975

wjristow opened this issue Jan 29, 2025 · 4 comments · Fixed by #125044
Labels
clang:static analyzer confirmed Verified by a second party crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@wjristow
Copy link
Collaborator

The following code crashes the Clang Static Analyzer when built with assertions:

// =============================================================
enum PrimaryColors {
  Color_Red     = 0x01,
  Color_Green   = 0x02,
  Color_Blue    = 0x04,
  Color_None    = 0x00
};
extern void ReportMissing(PrimaryColors color);

struct ColorInfo {
  PrimaryColors getColor() const { return m_color; }
  PrimaryColors m_color;
};

void CheckColor(ColorInfo *pColorInfo, bool AllowSkip) {
  extern bool SkipCheck;
  if (SkipCheck || !pColorInfo) {
    if (AllowSkip)
      return;
  }
  if ((pColorInfo->getColor() & Color_Red) == 0)
    ReportMissing(Color_Red);
}
// -------------------------------------------------------------

Specifically, using Clang 19.1.0:

$ clang++ --analyze test.cpp
clang++: /root/llvm-project/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp:1290: bool isInitializationOfVar(const clang::ento::ExplodedNode*, const clang::ento::VarRegion*): Assertion `VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
  ...
$

Godbolt link.

FTR, with a non-assetions compiler, it gets a sensible warning from the analyzer:

$ clang++ --analyze test.cpp
test.cpp:21:8: warning: Called C++ object pointer is null [core.CallAndMessage]
   21 |   if ((pColorInfo->getColor() & Color_Red) == 0)
      |        ^~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
$

Looking through history, I see it passed with llvm 9.0, and crashes beginning with llvm 10.0.

@EugeneZelenko EugeneZelenko added clang:static analyzer confirmed Verified by a second party crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Jan 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2025

@llvm/issue-subscribers-clang-static-analyzer

Author: None (wjristow)

The following code crashes the Clang Static Analyzer when built with assertions:
// =============================================================
enum PrimaryColors {
  Color_Red     = 0x01,
  Color_Green   = 0x02,
  Color_Blue    = 0x04,
  Color_None    = 0x00
};
extern void ReportMissing(PrimaryColors color);

struct ColorInfo {
  PrimaryColors getColor() const { return m_color; }
  PrimaryColors m_color;
};

void CheckColor(ColorInfo *pColorInfo, bool AllowSkip) {
  extern bool SkipCheck;
  if (SkipCheck || !pColorInfo) {
    if (AllowSkip)
      return;
  }
  if ((pColorInfo->getColor() & Color_Red) == 0)
    ReportMissing(Color_Red);
}
// -------------------------------------------------------------

Specifically, using Clang 19.1.0:

$ clang++ --analyze test.cpp
clang++: /root/llvm-project/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp:1290: bool isInitializationOfVar(const clang::ento::ExplodedNode*, const clang::ento::VarRegion*): Assertion `VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
  ...
$

Godbolt link.

FTR, with a non-assetions compiler, it gets a sensible warning from the analyzer:

$ clang++ --analyze test.cpp
test.cpp:21:8: warning: Called C++ object pointer is null [core.CallAndMessage]
   21 |   if ((pColorInfo->getColor() & Color_Red) == 0)
      |        ^~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
$

Looking through history, I see it passed with llvm 9.0, and crashes beginning with llvm 10.0.

@EugeneZelenko
Copy link
Contributor

Crashes in main: https://godbolt.org/z/6W57nqK8q

@wjristow wjristow pinned this issue Jan 29, 2025
@wjristow wjristow unpinned this issue Jan 29, 2025
@steakhal
Copy link
Contributor

The case reduces into this: https://godbolt.org/z/GPceG9Pxq

void top(int *p) {
  if (p) {}

  extern bool SkipCheck;
  if (SkipCheck)
    return;

  *p = 4;
}

@wjristow
Copy link
Collaborator Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer confirmed Verified by a second party crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants