From 328d2d9361afdf70c581328411e3e6b7804e3f86 Mon Sep 17 00:00:00 2001 From: Anna Zaks Date: Thu, 15 Dec 2016 22:55:18 +0000 Subject: [PATCH] [analyzer] Teach the analyzer that pointers can escape into __cxa_demangle This fixes a reported false positive in the malloc checker. Differential Revision: https://reviews.llvm.org/D27599 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289886 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Core/CallEvent.cpp | 5 +++++ test/Analysis/Inputs/system-header-simulator-cxx.h | 9 +++++++++ test/Analysis/malloc.cpp | 14 ++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lib/StaticAnalyzer/Core/CallEvent.cpp b/lib/StaticAnalyzer/Core/CallEvent.cpp index bd47e897c3..420e2a6b5c 100644 --- a/lib/StaticAnalyzer/Core/CallEvent.cpp +++ b/lib/StaticAnalyzer/Core/CallEvent.cpp @@ -382,6 +382,11 @@ bool AnyFunctionCall::argumentsMayEscape() const { if (II->isStr("funopen")) return true; + // - __cxa_demangle - can reallocate memory and can return the pointer to + // the input buffer. + if (II->isStr("__cxa_demangle")) + return true; + StringRef FName = II->getName(); // - CoreFoundation functions that end with "NoCopy" can free a passed-in diff --git a/test/Analysis/Inputs/system-header-simulator-cxx.h b/test/Analysis/Inputs/system-header-simulator-cxx.h index b32d200364..04f1000dbc 100644 --- a/test/Analysis/Inputs/system-header-simulator-cxx.h +++ b/test/Analysis/Inputs/system-header-simulator-cxx.h @@ -240,3 +240,12 @@ void* operator new (std::size_t size, void* ptr) throw() { return ptr; }; void* operator new[] (std::size_t size, void* ptr) throw() { return ptr; }; void operator delete (void* ptr, void*) throw() {}; void operator delete[] (void* ptr, void*) throw() {}; + +namespace __cxxabiv1 { +extern "C" { +extern char *__cxa_demangle(const char *mangled_name, + char *output_buffer, + size_t *length, + int *status); +}} +namespace abi = __cxxabiv1; diff --git a/test/Analysis/malloc.cpp b/test/Analysis/malloc.cpp index f24ccf58dc..a8a79cac86 100644 --- a/test/Analysis/malloc.cpp +++ b/test/Analysis/malloc.cpp @@ -1,6 +1,8 @@ // RUN: %clang_cc1 -w -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s // RUN: %clang_cc1 -triple i386-unknown-linux-gnu -w -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s +#include "Inputs/system-header-simulator-cxx.h" + typedef __typeof(sizeof(int)) size_t; void *malloc(size_t); void free(void *); @@ -125,3 +127,15 @@ namespace PR31226 { p->m(); // no-crash // no-warning } } + +// Allow __cxa_demangle to escape. +char* test_cxa_demangle(const char* sym) { + size_t funcnamesize = 256; + char* funcname = (char*)malloc(funcnamesize); + int status; + char* ret = abi::__cxa_demangle(sym, funcname, &funcnamesize, &status); + if (status == 0) { + funcname = ret; + } + return funcname; // no-warning +} -- 2.40.0