Skip to content

Commit de96199

Browse files
authored
[WebKit checkers] Add an annotation for pointer conversion. (#141277)
This PR adds the WebKit checker support for [[clang::annotate_type("webkit.pointerconversion")]]. When this attribute is set on the return value of a function, the function is treated as safe to call anywhere and the return value's pointer origin is the argument.`
1 parent e5fa38b commit de96199

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ bool isPtrConversion(const FunctionDecl *F) {
468468
FunctionName == "checked_objc_cast")
469469
return true;
470470

471+
auto ReturnType = F->getReturnType();
472+
if (auto *Type = ReturnType.getTypePtrOrNull()) {
473+
if (auto *AttrType = dyn_cast<AttributedType>(Type)) {
474+
if (auto *Attr = AttrType->getAttr()) {
475+
if (auto *AnnotateType = dyn_cast<AnnotateTypeAttr>(Attr)) {
476+
if (AnnotateType->getAnnotation() == "webkit.pointerconversion")
477+
return true;
478+
}
479+
}
480+
}
481+
}
482+
471483
return false;
472484
}
473485

clang/test/Analysis/Checkers/WebKit/call-args-safe-functions.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
2-
// expected-no-diagnostics
2+
3+
#include "mock-types.h"
34

45
class Base {
56
public:
6-
inline void ref();
7-
inline void deref();
7+
void ref();
8+
void deref();
9+
void doWork();
810
};
911

1012
class Derived : public Base {
@@ -21,6 +23,7 @@ class SubDerived final : public Derived {
2123
class OtherObject {
2224
public:
2325
Derived* obj();
26+
Base* base();
2427
};
2528

2629
class String {
@@ -44,6 +47,12 @@ inline Target* uncheckedDowncast(Source* source)
4447
return static_cast<Target*>(source);
4548
}
4649

50+
template<typename Target, typename Source>
51+
Target* [[clang::annotate_type("webkit.pointerconversion")]] newCastFunction(Source*);
52+
53+
template<typename Target, typename Source>
54+
Target* [[clang::annotate_type("unrelated-annotation")]] badCastFunction(Source*);
55+
4756
template<typename... Types>
4857
String toString(const Types&... values);
4958

@@ -52,5 +61,17 @@ void foo(OtherObject* other)
5261
dynamicDowncast<SubDerived>(other->obj());
5362
checkedDowncast<SubDerived>(other->obj());
5463
uncheckedDowncast<SubDerived>(other->obj());
64+
newCastFunction<SubDerived>(other->obj());
65+
badCastFunction<SubDerived>(other->obj());
66+
// expected-warning@-1{{Call argument is uncounted and unsafe}}
5567
toString(other->obj());
5668
}
69+
70+
struct SomeStruct {
71+
Derived* [[clang::annotate_type("webkit.pointerconversion")]] ptrConversion(Base*);
72+
73+
void foo(OtherObject& otherObj) {
74+
RefPtr ptr = otherObj.base();
75+
ptrConversion(ptr.get())->doWork();
76+
}
77+
};

0 commit comments

Comments
 (0)