llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[analyzer] macro expansion causes crash for certain macro expressions

Open vabridgers opened this issue 5 years ago • 4 comments

Bugzilla Link 48588
Version trunk
OS All
CC @steakhal,@devincoughlin,@vabridgers

Extended Description

We came across another macro expansion issue lately, and I think I have this debugged.

The simple reproducer is below. The crash is causes is edited for brevity, and pasted in below. I believe this was introduced by the refactoring change be0d79f32930fe780dc89ba96dac0ba163f7ec50.

In summary, I think the "TStream.injectRange(PrevParamMap.at(VariadicParamII));" needs to be guarded by at least a check for the key present in the map PrevParamMap. I prototyped a change, LITS pass and the crash is avoided.

Could someone check my analysis? I'll cross post a Phabricator review for this issue soon.

clang -cc1 -std=c++14 -analyzer-checker=core macro-crash.cpp -analyzer-config expand-macros=true

const char *traceid(const char *);
int trace(int, const char *, int, ...);
#define TRACE_CALL(tracelevel, ...)  { __VA_ARGS__; }                                                    

#define TRACE(tracelevel, str, ...) \
  TRACE_CALL((tracelevel), trace((0), traceid("formatstr " str), 0, tracelevel, __VA_ARGS__))

#define TRACE_WRAPPER TRACE

void funcXXX(
    void* Context_p)
{
  int localvar;
  TRACE_WRAPPER(
          localvar,
          "localvar=%u ",
          0);
}

The crash ...

    terminate called after throwing an instance of 'std::out_of_range'
      what():  map::at
    
      #​0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
          <base>/llvm/lib/Support/Unix/Signals.inc:563:22
     ...
    
     #&#8203;11 std::__throw_out_of_range(char const*)
          <base>/libstdc++-v3/src/c++11/functexcept.cc:82:5
     #&#8203;12 std::map<clang::IdentifierInfo const*,
            llvm::SmallVector<clang::Token, 2u>, std::less<clang::IdentifierInfo
            const*>, std::allocator<std::pair<clang::IdentifierInfo const* const,
            llvm::SmallVector<clang::Token, 2u> > > >::at(clang::IdentifierInfo
            const* const&) const
          <base>/gcc/9.3.0/include/c++/9.3.0/bits/stl_map.h:549:10
     #&#8203;13 getMacroExpansionInfo((anonymous namespace)::MacroParamMap const&,
            clang::SourceLocation, clang::Preprocessor const&)
          <base>/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp:1242:66
     ...

vabridgers avatar Dec 24 '20 01:12 vabridgers

The related patch stack has landed. Can I close this ticket now Vince?

steakhal avatar Mar 01 '21 16:03 steakhal

Balazs has a change in progress for this issue (among others) here -> https://reviews.llvm.org/D93222. Will keep this one open for now until that lands, and we can prove that change addresses this case.

vabridgers avatar Feb 03 '21 14:02 vabridgers

assigned to @vabridgers

vabridgers avatar Dec 24 '20 01:12 vabridgers

Bisected: the tested witness changes behavior starting with https://github.com/llvm/llvm-project/commit/170c67d5b8cc58dd8a4bd0ea7c5ca02290fac39c.

This comparison uses expand-macros=true with plist analyzer output and checks the reported macro invocation.

Godbolt: https://godbolt.org/z/d84nKrcT4

  • Clang 12.0.1: Crashes with std::out_of_range in map::at
  • Clang 13.0.0: Completes analysis and reports 4th function call argument is an uninitialized value
  • Clang (trunk): Completes analysis and reports 4th function call argument is an uninitialized value

This is only an observed change in behavior. I have not checked whether this commit addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.

Prepared with assistance from Codex.

hongtaihu avatar Nov 02 '26 07:11 hongtaihu