<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Andrew,<div><br></div><div>In addition to Jordy's excellent comments, here are my comments on your patch. Comments inline:</div><div><br></div><div><div>Index: test/Analysis/malloc.c</div><div>===================================================================</div><div>--- test/Analysis/malloc.c<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ test/Analysis/malloc.c<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -4,7 +4,13 @@</div><div> void free(void *);</div><div> void *realloc(void *ptr, size_t size);</div><div> void *calloc(size_t nmemb, size_t size);</div><div>+void __attribute((ownership_returns(malloc))) *my_malloc(size_t);</div><div>+void __attribute((ownership_takes(malloc, 1))) my_free(void *);</div><div>+void __attribute((ownership_returns(malloc, 1))) *my_malloc2(size_t);</div><div>+void __attribute((ownership_holds(malloc, 1))) my_hold(void *);</div><div>+void *my_malloc3(size_t);</div><div> </div><div><br></div><div>Good test cases, but also include test cases where the index isn't '1'.</div><div><br></div><div>Please also include test cases when multiple indices are annotated with a single attribute.</div><div><br></div><div>Also, what happens if the same index is specified more than once?</div><div><br></div><div><br></div><div>Index: include/clang/Basic/Attr.td</div><div>===================================================================</div><div>--- include/clang/Basic/Attr.td<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ include/clang/Basic/Attr.td<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -297,6 +297,21 @@</div><div> let Spellings = ["overloadable"];</div><div> }</div><div> </div><div>+def OwnershipReturns : Attr {</div><div>+ let Spellings = ["ownership_returns"];</div><div>+ let Args = [StringArgument<"Module">, IntArgument<"SizeIdx">];</div><div>+}</div><div>+</div><div>+def OwnershipTakes : Attr {</div><div>+ let Spellings = ["ownership_takes"];</div><div>+ let Args = [StringArgument<"Module">, IntArgument<"PtrIdx">];</div><div>+}</div><div>+</div><div>+def OwnershipHolds : Attr {</div><div>+ let Spellings = ["ownership_holds"];</div><div>+ let Args = [StringArgument<"Module">, IntArgument<"PtrIdx">];</div><div>+}</div><div>+</div><div> def Packed : Attr {</div><div> let Spellings = ["packed"];</div><div> }</div><div><br></div><div>In addition to this syntax, Jeffrey Yaskin also made the excellent point of whether or not we want to allow the ownership_takes and ownership_holds attributes directly to be affixed directly to parameters instead of using indices. Should we allow both models, and thus make the PtrIdx optional in those cases?</div><div><br></div><div>I'm also not certain what the SizeIdx entry is for. Is this to explicitly model the size of the returned memory? That's fine, but please also keep in mind how we would want to model out-parameters. Would we want a separate attribute?</div><div><br></div><div><br></div><div>Index: include/clang/Basic/DiagnosticSemaKinds.td</div><div>===================================================================</div><div>--- include/clang/Basic/DiagnosticSemaKinds.td<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ include/clang/Basic/DiagnosticSemaKinds.td<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -831,6 +831,12 @@</div><div> "attribute may only be applied to an Objective-C interface">;</div><div> def err_nonnull_pointers_only : Error<</div><div> "nonnull attribute only applies to pointer arguments">;</div><div>+def err_ownership_returns_integers_only : Error<</div><div>+ "ownership_returns attribute only applies to integer arguments">;</div><div>+def err_ownership_takes_pointers_only : Error<</div><div>+ "ownership_takes attribute only applies to pointer arguments">;</div><div>+def err_ownership_holds_pointers_only : Error<</div><div>+ "ownership_holds attribute only applies to pointer arguments">;</div><div> def err_format_strftime_third_parameter : Error<</div><div> "strftime format attribute requires 3rd parameter to be 0">;</div><div> def err_format_attribute_requires_variadic : Error<</div><div><br></div><div><br></div><div><br></div><div><br></div><div>Index: include/clang/AST/Attr.h</div><div>===================================================================</div><div>--- include/clang/AST/Attr.h<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ include/clang/AST/Attr.h<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -351,6 +351,128 @@</div><div> static bool classof(const NonNullAttr *A) { return true; }</div><div> };</div><div> </div><div>+class OwnershipReturnsAttr: public AttrWithString {</div><div>+ unsigned* ArgNums;</div><div>+ unsigned Size;</div><div>+public:</div><div>+ OwnershipReturnsAttr(ASTContext &C, unsigned* arg_nums, unsigned size,</div><div>+ llvm::StringRef module);</div><div>+</div><div>+ virtual void Destroy(ASTContext &C);</div><div>+</div><div>+ llvm::StringRef getModule() const {</div><div>+ return getString();</div><div>+ }</div><div>+ void setModule(ASTContext &C, llvm::StringRef module);</div><div>+ typedef const unsigned *iterator;</div><div>+ iterator begin() const {</div><div>+ return ArgNums;</div><div>+ }</div><div>+ iterator end() const {</div><div>+ return ArgNums + Size;</div><div>+ }</div><div>+ unsigned size() const {</div><div>+ return Size;</div><div>+ }</div><div>+</div><div>+ bool isOwnershipReturns(unsigned arg) const {</div><div>+ return ArgNums ? std::binary_search(ArgNums, ArgNums + Size, arg) : true;</div><div>+ }</div><div>+</div><div>+ bool isModule(const char *m) const {</div><div>+ return getModule().equals(m);</div><div>+ }</div><div>+</div><div>+ virtual Attr *clone(ASTContext &C) const;</div><div>+</div><div>+ static bool classof(const Attr *A) {</div><div>+ return A->getKind() == attr::OwnershipReturns;</div><div>+ }</div><div>+ static bool classof(const OwnershipReturnsAttr *A) {</div><div>+ return true;</div><div>+ }</div><div>+};</div><div>+</div><div>+class OwnershipTakesAttr: public AttrWithString {</div><div>+ unsigned* ArgNums;</div><div>+ unsigned Size;</div><div>+public:</div><div>+ OwnershipTakesAttr(ASTContext &C, unsigned* arg_nums, unsigned size,</div><div>+ llvm::StringRef module);</div><div>+</div><div>+ virtual void Destroy(ASTContext &C);</div><div>+</div><div>+ llvm::StringRef getModule() const {</div><div>+ return getString();</div><div>+ }</div><div>+ void setModule(ASTContext &C, llvm::StringRef module);</div><div>+ typedef const unsigned *iterator;</div><div>+ iterator begin() const {</div><div>+ return ArgNums;</div><div>+ }</div><div>+ iterator end() const {</div><div>+ return ArgNums + Size;</div><div>+ }</div><div>+ unsigned size() const {</div><div>+ return Size;</div><div>+ }</div><div>+</div><div>+ bool isOwnershipTakes(unsigned arg) const {</div><div>+ return ArgNums ? std::binary_search(ArgNums, ArgNums + Size, arg) : true;</div><div>+ }</div><div>+</div><div>+ bool isModule(const char *m) const {</div><div>+ return getModule().equals(m);</div><div>+ }</div><div>+</div><div>+ virtual Attr *clone(ASTContext &C) const;</div><div>+</div><div>+ static bool classof(const Attr *A) {</div><div>+ return A->getKind() == attr::OwnershipTakes;</div><div>+ }</div><div>+ static bool classof(const OwnershipTakesAttr *A) {</div><div>+ return true;</div><div>+ }</div><div>+};</div><div>+</div><div>+class OwnershipHoldsAttr: public AttrWithString {</div><div>+ unsigned* ArgNums;</div><div>+ unsigned Size;</div><div>+public:</div><div>+ OwnershipHoldsAttr(ASTContext &C, unsigned* arg_nums, unsigned size,</div><div>+ llvm::StringRef module);</div><div>+</div><div>+ virtual void Destroy(ASTContext &C);</div><div>+</div><div>+ llvm::StringRef getModule() const {</div><div>+ return getString();</div><div>+ }</div><div>+ void setModule(ASTContext &C, llvm::StringRef module);</div><div>+ typedef const unsigned *iterator;</div><div>+ iterator begin() const {</div><div>+ return ArgNums;</div><div>+ }</div><div>+ iterator end() const {</div><div>+ return ArgNums + Size;</div><div>+ }</div><div>+ unsigned size() const {</div><div>+ return Size;</div><div>+ }</div><div>+</div><div>+ bool isOwnershipHolds(unsigned arg) const {</div><div>+ return ArgNums ? std::binary_search(ArgNums, ArgNums + Size, arg) : true;</div><div>+ }</div><div>+</div><div>+ bool isModule(const char *m) const {</div><div>+ return getModule().equals(m);</div><div>+ }</div><div>+</div><div>+ virtual Attr *clone(ASTContext &C) const;</div><div>+</div><div>+ static bool classof(const Attr *A) { return A->getKind() == attr::OwnershipHolds; }</div><div>+ static bool classof(const OwnershipHoldsAttr *A) { return true; }</div><div>+};</div><div><br></div><div>Consider having these three attribute classes subclass a common base class, e.g. OwnershipAttr. There's a bunch of duplication here which is unneeded. For example, setModule, getModule, etc., have identical implementations.</div><div><br></div><div>There is also value in having them derive from a common superclass. From a design perspective, it clearly marks them as being related.</div><div><br></div><div><br></div><div>Index: include/clang/Parse/AttributeList.h</div><div>===================================================================</div><div>--- include/clang/Parse/AttributeList.h<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ include/clang/Parse/AttributeList.h<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -97,6 +97,9 @@</div><div> AT_ns_returns_retained, // Clang-specific.</div><div> AT_objc_gc,</div><div> AT_overloadable, // Clang-specific.</div><div>+ AT_ownership_returns, // Clang-specific.</div><div>+ AT_ownership_takes, // Clang-specific.</div><div>+ AT_ownership_holds, // Clang-specific.</div><div><br></div><div>Please keep these sorted w.r.t. each other.</div><div><br></div><div> AT_packed,</div><div> AT_pure,</div><div> AT_regparm,</div><div>Index: lib/Sema/SemaDeclAttr.cpp</div><div><br></div><div><br></div><div>===================================================================</div><div>--- lib/Sema/SemaDeclAttr.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ lib/Sema/SemaDeclAttr.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -350,6 +350,261 @@</div><div> d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));</div><div> }</div><div> </div><div>+static void HandleOwnershipReturnsAttr(Decl *d, const AttributeList &Attr,</div><div>+ Sema &S) {</div><div>+ // This attribute must be applied to a function declaration.</div><div>+ // The first argument to the attribute must be a string,</div><div>+ // the name of the resource, for example "malloc".</div><div>+ // The next argument is optional, and represents the size of allocation.</div><div>+ // If present, it must be an integer less than the number of function arguments,</div><div>+ // this is an argument index, the argument must be of integer type.</div><div>+ // If not present, the allocation is of unknown size.</div><div>+</div><div>+ if (!Attr.getParameterName()) {</div><div>+ S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)</div><div>+ << "ownership_returns" << 1;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {</div><div>+ S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)</div><div>+ << Attr.getName() << 0 /*function*/;</div><div>+ return;</div><div>+ }</div><div><br></div><div>These attributes only apply to functions, not blocks or Objective-C methods. We can possibly extend them to support methods (and possibly even blocks), but that's not in the current design. Your diagnostic even indicates that it only works on functions. Use 'isFunction' instead.</div><div><br></div><div><br></div><div>+</div><div>+ unsigned NumArgs = getFunctionOrMethodNumArgs(d);</div><div>+</div><div>+ llvm::StringRef Module = Attr.getParameterName()->getName();</div><div>+</div><div>+ // Normalize the argument, __foo__ becomes foo.</div><div>+ if (Module.startswith("__") && Module.endswith("__"))</div><div>+ Module = Module.substr(2, Module.size() - 4);</div><div>+</div><div>+ llvm::SmallVector<unsigned, 10> OwnershipReturnsArgs;</div><div><br></div><div>Note that the small vector here is 'unsigned', but that the arguments themselves can be signed. You should issue the appropriate diagnostic, and make sure the indices are non-negative.</div><div><br></div><div>+</div><div>+ if (Attr.getNumArgs() == 1) {</div><div>+ // Handle the index argument, if present.</div><div>+ Expr *IdxExpr = static_cast<Expr *> (Attr.getArg(0));</div><div>+ llvm::APSInt ArgNum(32);</div><div>+ if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()</div><div>+ || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {</div><div>+ S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)</div><div>+ << "ownership_returns" << IdxExpr->getSourceRange();</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ unsigned x = (unsigned) ArgNum.getZExtValue();</div><div>+</div><div>+ if (x < 1 || x > NumArgs) {</div><div>+ S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)</div><div>+ << "ownership_returns" << 1 << IdxExpr->getSourceRange();</div><div>+ return;</div><div>+ }</div><div>+ --x;</div><div>+ // Is the function argument an integer type?</div><div>+ QualType T = getFunctionOrMethodArgType(d, x);</div><div>+ if (!T->isUnsignedIntegerType()) {</div><div>+ // FIXME: Should also highlight argument in decl.</div><div>+ S.Diag(Attr.getLoc(), diag::err_ownership_returns_integers_only)</div><div>+ << "ownership_returns" << IdxExpr->getSourceRange();</div><div>+ return;</div><div>+ }</div><div>+ OwnershipReturnsArgs.push_back(x);</div><div>+ }</div><div><br></div><div>Looks good.</div><div><br></div><div>+</div><div>+ unsigned* start = OwnershipReturnsArgs.data();</div><div>+ unsigned size = OwnershipReturnsArgs.size();</div><div>+ std::sort(start, start + size);</div><div><br></div><div>Please use array_pod_sort. std::sort causes a bunch of stuff to get pulled in (or instantiated) that bloats the code.</div><div><br></div><div><br></div><div>+ d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size,</div><div>+ Module));</div><div>+}</div><div><br></div><div>Looks good.</div><div><br></div><div>+</div><div>+static void HandleOwnershipHoldsAttr(Decl *d, const AttributeList &AL,</div><div>+ Sema &S) {</div><div>+ // This attribute must be applied to a function declaration.</div><div>+ // The first argument to the attribute must be a string,</div><div>+ // the name of the resource, for example "malloc".</div><div>+ // The following arguments must be argument indexes, the arguments must be of pointer type.</div><div>+ // The difference between Holds and Takes is that a pointer may still be used</div><div>+ // after being held. free() should be __attribute((ownership_takes)), whereas a list</div><div>+ // append function may well be __attribute((ownership_holds)).</div><div>+</div><div>+ if (!AL.getParameterName()) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)</div><div>+ << "ownership_holds" << 1;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (AL.getNumArgs() < 1) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {</div><div>+ S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)</div><div>+ << AL.getName() << 0 /*function*/;</div><div>+ return;</div><div>+ }</div><div><br></div><div>Same comment as above. Use 'isFunction'.</div><div><br></div><div>+</div><div>+ unsigned NumArgs = getFunctionOrMethodNumArgs(d);</div><div>+</div><div>+ llvm::StringRef Module = AL.getParameterName()->getName();</div><div>+</div><div>+ // Normalize the argument, __foo__ becomes foo.</div><div>+ if (Module.startswith("__") && Module.endswith("__"))</div><div>+ Module = Module.substr(2, Module.size() - 4);</div><div>+</div><div>+ llvm::SmallVector<unsigned, 10> OwnershipHoldsArgs;</div><div>+</div><div>+ for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I</div><div>+ != E; ++I) {</div><div>+</div><div>+ Expr *IdxExpr = static_cast<Expr *> (*I);</div><div>+ llvm::APSInt ArgNum(32);</div><div>+ if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()</div><div>+ || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)</div><div>+ << "ownership_holds" << IdxExpr->getSourceRange();</div><div>+ continue;</div><div>+ }</div><div>+</div><div>+ unsigned x = (unsigned) ArgNum.getZExtValue();</div><div>+</div><div>+ if (x < 1 || x > NumArgs) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)</div><div>+ << "ownership_holds" << x << IdxExpr->getSourceRange();</div><div>+ continue;</div><div>+ }</div><div>+ --x;</div><div>+ // Is the function argument a pointer type?</div><div>+ QualType T = getFunctionOrMethodArgType(d, x);</div><div>+ if (!T->isAnyPointerType() && !T->isBlockPointerType()) {</div><div>+ // FIXME: Should also highlight argument in decl.</div><div>+ S.Diag(AL.getLoc(), diag::err_ownership_holds_pointers_only)</div><div>+ << IdxExpr->getSourceRange();</div><div>+ continue;</div><div>+ }</div><div>+ // Check we don't have a conflict with an ownership_takes attribute.</div><div>+ if (d->hasAttrs()) {</div><div>+ for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {</div><div>+ if (const OwnershipTakesAttr* Att = dyn_cast<OwnershipTakesAttr> (attr)) {</div><div>+ for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I) {</div><div>+ if (x == *I) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)</div><div>+ << "ownership_takes" << "ownership_holds";</div><div>+ }</div><div>+ }</div><div>+ }</div><div>+ }</div><div>+ }</div><div>+ OwnershipHoldsArgs.push_back(x);</div><div>+ }</div><div>+</div><div>+ if (OwnershipHoldsArgs.empty()) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ unsigned* start = &OwnershipHoldsArgs[0];</div><div>+ unsigned size = OwnershipHoldsArgs.size();</div><div>+ std::sort(start, start + size);</div><div>+ d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size,</div><div>+ Module));</div><div>+}</div><div><br></div><div>Most of this is copy-and-paste from HandleOwnershipReturnsAttr. This looks like it could easily be refactored, with few choice 'if' statements based on the attribute type differentiating behavior. Not only is this excessively verbose, this can lead to subtle bugs and inconsistencies. For example, see how HandleNSReturnsRetainedAttr() is used to process four different kinds of attributes. Please refactor the logic for these attributes into a common function.</div><div><br></div><div>+</div><div>+static void HandleOwnershipTakesAttr(Decl *d, const AttributeList &AL,</div><div>+ Sema &S) {</div><div>+ // This attribute must be applied to a function declaration.</div><div>+ // The first argument to the attribute must be a string,</div><div>+ // the name of the resource, for example "malloc".</div><div>+ // The following arguments must be argument indexes, the arguments must be of pointer type.</div><div>+ // The difference between Holds and Takes is that a pointer may still be used</div><div>+ // after being held. free() should be __attribute((ownership_takes)), whereas a list</div><div>+ // append function may well be __attribute((ownership_holds)).</div><div>+</div><div>+ if (!AL.getParameterName()) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)</div><div>+ << "ownership_takes" << 1;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (AL.getNumArgs() < 1) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {</div><div>+ S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)</div><div>+ << AL.getName() << 0 /*function*/;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ unsigned NumArgs = getFunctionOrMethodNumArgs(d);</div><div>+</div><div>+ llvm::StringRef Module = AL.getParameterName()->getName();</div><div>+</div><div>+ // Normalize the argument, __foo__ becomes foo.</div><div>+ if (Module.startswith("__") && Module.endswith("__"))</div><div>+ Module = Module.substr(2, Module.size() - 4);</div><div>+</div><div>+ llvm::SmallVector<unsigned, 10> OwnershipTakesArgs;</div><div>+</div><div>+ for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I</div><div>+ != E; ++I) {</div><div>+</div><div>+ Expr *IdxExpr = static_cast<Expr *> (*I);</div><div>+ llvm::APSInt ArgNum(32);</div><div>+ if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()</div><div>+ || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)</div><div>+ << "ownership_takes" << IdxExpr->getSourceRange();</div><div>+ continue;</div><div>+ }</div><div>+</div><div>+ unsigned x = (unsigned) ArgNum.getZExtValue();</div><div>+</div><div>+ if (x < 1 || x > NumArgs) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)</div><div>+ << "ownership_takes" << x << IdxExpr->getSourceRange();</div><div>+ continue;</div><div>+ }</div><div>+ --x;</div><div>+ // Is the function argument a pointer type?</div><div>+ QualType T = getFunctionOrMethodArgType(d, x);</div><div>+ if (!T->isAnyPointerType() && !T->isBlockPointerType()) {</div><div>+ // FIXME: Should also highlight argument in decl.</div><div>+ S.Diag(AL.getLoc(), diag::err_ownership_holds_pointers_only)</div><div>+ << IdxExpr->getSourceRange();</div><div>+ continue;</div><div>+ }</div><div>+ // Check we don't have a conflict with an ownership_holds attribute.</div><div>+ if (d->hasAttrs()) {</div><div>+ for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {</div><div>+ if (const OwnershipHoldsAttr* Att = dyn_cast<OwnershipHoldsAttr> (attr)) {</div><div>+ for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I) {</div><div>+ if (x == *I) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)</div><div>+ << "ownership_takes" << "ownership_holds";</div><div>+ }</div><div>+ }</div><div>+ }</div><div>+ }</div><div>+ }</div><div>+ OwnershipTakesArgs.push_back(x);</div><div>+ }</div><div>+</div><div>+ if (OwnershipTakesArgs.empty()) {</div><div>+ S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ unsigned* start = &OwnershipTakesArgs[0];</div><div>+ unsigned size = OwnershipTakesArgs.size();</div><div>+ std::sort(start, start + size);</div><div>+ d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size,</div><div>+ Module));</div><div>+}</div><div>+</div><div><br></div><div>Same comment as above. This is all copy-and-paste. We can do better.</div><div><br></div><div><br></div><div> static bool isStaticVarOrStaticFunciton(Decl *D) {</div><div> if (VarDecl *VD = dyn_cast<VarDecl>(D))</div><div> return VD->getStorageClass() == VarDecl::Static;</div><div>@@ -2002,6 +2257,12 @@</div><div> case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;</div><div> case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;</div><div> case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;</div><div>+ case AttributeList::AT_ownership_returns:</div><div>+ HandleOwnershipReturnsAttr (D, Attr, S); break;</div><div>+ case AttributeList::AT_ownership_takes:</div><div>+ HandleOwnershipTakesAttr (D, Attr, S); break;</div><div>+ case AttributeList::AT_ownership_holds:</div><div>+ HandleOwnershipHoldsAttr (D, Attr, S); break;</div><div><br></div><div>Instead of calling three functions, probably calling one or two is fine (and looking at the Attr kind).</div><div><br></div><div> case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;</div><div> case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;</div><div> case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;</div><div><br></div><div><br></div><div>Index: lib/AST/AttrImpl.cpp</div><div>===================================================================</div><div>--- lib/AST/AttrImpl.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ lib/AST/AttrImpl.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -66,6 +66,70 @@</div><div> Attr::Destroy(C);</div><div> }</div><div> </div><div>+OwnershipReturnsAttr::OwnershipReturnsAttr(ASTContext &C, unsigned* arg_nums,</div><div>+ unsigned size,</div><div>+ llvm::StringRef module) :</div><div>+ AttrWithString(attr::OwnershipReturns, C, module), ArgNums(0), Size(0) {</div><div>+ if (size == 0)</div><div>+ return;</div><div>+ assert(arg_nums);</div><div>+ ArgNums = new (C) unsigned[size];</div><div>+ Size = size;</div><div>+ memcpy(ArgNums, arg_nums, sizeof(*ArgNums) * size);</div><div>+}</div><div><br></div><div>Looks good.</div><div><br></div><div>+</div><div>+void OwnershipReturnsAttr::setModule(ASTContext &C, llvm::StringRef module) {</div><div>+ ReplaceString(C, module);</div><div>+}</div><div>+</div><div>+void OwnershipReturnsAttr::Destroy(ASTContext &C) {</div><div>+ if (ArgNums)</div><div>+ C.Deallocate(ArgNums);</div><div>+ Attr::Destroy(C);</div><div>+}</div><div><br></div><div>Looks good.</div><div><br></div><div>+</div><div>+OwnershipTakesAttr::OwnershipTakesAttr(ASTContext &C, unsigned* arg_nums,</div><div>+ unsigned size, llvm::StringRef module) :</div><div>+ AttrWithString(attr::OwnershipTakes, C, module), ArgNums(0), Size(0) {</div><div>+ if (size == 0)</div><div>+ return;</div><div>+ assert(arg_nums);</div><div>+ ArgNums = new (C) unsigned[size];</div><div>+ Size = size;</div><div>+ memcpy(ArgNums, arg_nums, sizeof(*ArgNums) * size);</div><div>+}</div><div><br></div><div>Notice how this is practically identical to OwnershipReturnsAttr? I think most of this can be merged into a common constructor of a base class.</div><div><br></div><div>+</div><div>+void OwnershipTakesAttr::setModule(ASTContext &C, llvm::StringRef module) {</div><div>+ ReplaceString(C, module);</div><div>+}</div><div>+</div><div>+void OwnershipTakesAttr::Destroy(ASTContext &C) {</div><div>+ if (ArgNums)</div><div>+ C.Deallocate(ArgNums);</div><div>+ Attr::Destroy(C);</div><div>+}</div><div>+</div><div>+OwnershipHoldsAttr::OwnershipHoldsAttr(ASTContext &C, unsigned* arg_nums,</div><div>+ unsigned size, llvm::StringRef module) :</div><div>+ AttrWithString(attr::OwnershipHolds, C, module), ArgNums(0), Size(0) {</div><div>+ if (size == 0)</div><div>+ return;</div><div>+ assert(arg_nums);</div><div>+ ArgNums = new (C) unsigned[size];</div><div>+ Size = size;</div><div>+ memcpy(ArgNums, arg_nums, sizeof(*ArgNums) * size);</div><div>+}</div><div><br></div><div>Same comment as with OwnershipReturnsAttr. This is basically copy-and-paste. The attributes are basically the same except one is "takes" instead of "holds". We shouldn't be duplicating so much code for that.</div><div><br></div><div>+</div><div>+void OwnershipHoldsAttr::setModule(ASTContext &C, llvm::StringRef module) {</div><div>+ ReplaceString(C, module);</div><div>+}</div><div>+</div><div>+void OwnershipHoldsAttr::Destroy(ASTContext &C) {</div><div>+ if (ArgNums)</div><div>+ C.Deallocate(ArgNums);</div><div>+ Attr::Destroy(C);</div><div>+}</div><div>+</div><div> #define DEF_SIMPLE_ATTR_CLONE(ATTR) \</div><div> Attr *ATTR##Attr::clone(ASTContext &C) const { \</div><div> return ::new (C) ATTR##Attr; \</div><div>@@ -165,6 +229,18 @@</div><div> return ::new (C) NonNullAttr(C, ArgNums, Size);</div><div> }</div><div> </div><div>+Attr *OwnershipReturnsAttr::clone(ASTContext &C) const {</div><div>+ return ::new (C) OwnershipReturnsAttr(C, ArgNums, Size, getModule());</div><div>+}</div><div>+</div><div>+Attr *OwnershipTakesAttr::clone(ASTContext &C) const {</div><div>+ return ::new (C) OwnershipTakesAttr(C, ArgNums, Size, getModule());</div><div>+}</div><div>+</div><div>+Attr *OwnershipHoldsAttr::clone(ASTContext &C) const {</div><div>+ return ::new (C) OwnershipHoldsAttr(C, ArgNums, Size, getModule());</div><div>+}</div><div>+</div><div> Attr *FormatAttr::clone(ASTContext &C) const {</div><div> return ::new (C) FormatAttr(C, getType(), formatIdx, firstArg);</div><div> }</div><div>Index: lib/Lex/PPMacroExpansion.cpp</div><div>===================================================================</div><div>--- lib/Lex/PPMacroExpansion.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ lib/Lex/PPMacroExpansion.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -505,6 +505,9 @@</div><div> .Case("cxx_static_assert", LangOpts.CPlusPlus0x)</div><div> .Case("objc_nonfragile_abi", LangOpts.ObjCNonFragileABI)</div><div> .Case("objc_weak_class", LangOpts.ObjCNonFragileABI)</div><div>+ .Case("ownership_returns", true)</div><div>+ .Case("ownership_takes", true)</div><div>+ .Case("ownership_holds", true)</div><div><br></div><div>Great!</div><div><br></div><div> //.Case("cxx_concepts", false)</div><div> //.Case("cxx_lambdas", false)</div><div> //.Case("cxx_nullptr", false)</div><div>Index: lib/Checker/MallocChecker.cpp</div><div>===================================================================</div><div>--- lib/Checker/MallocChecker.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ lib/Checker/MallocChecker.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -24,15 +24,17 @@</div><div> namespace {</div><div> </div><div> class RefState {</div><div>- enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped } K;</div><div>+ enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped, Relinquished } K;</div><div> const Stmt *S;</div><div> </div><div> public:</div><div> RefState(Kind k, const Stmt *s) : K(k), S(s) {}</div><div> </div><div> bool isAllocated() const { return K == AllocateUnchecked; }</div><div>+ bool isFailed() const { return K == AllocateFailed; }</div><div> bool isReleased() const { return K == Released; }</div><div> bool isEscaped() const { return K == Escaped; }</div><div>+ bool isRelinquished() const { return K == Relinquished; }</div><div> </div><div> bool operator==(const RefState &X) const {</div><div> return K == X.K && S == X.S;</div><div>@@ -46,6 +48,7 @@</div><div> }</div><div> static RefState getReleased(const Stmt *s) { return RefState(Released, s); }</div><div> static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }</div><div>+ static RefState getRelinquished(const Stmt *s) { return RefState(Relinquished, s); }</div><div> </div><div> void Profile(llvm::FoldingSetNodeID &ID) const {</div><div> ID.AddInteger(K);</div><div>@@ -59,12 +62,13 @@</div><div> BuiltinBug *BT_DoubleFree;</div><div> BuiltinBug *BT_Leak;</div><div> BuiltinBug *BT_UseFree;</div><div>+ BuiltinBug *BT_UseRelinquished;</div><div> BuiltinBug *BT_BadFree;</div><div> IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;</div><div> </div><div> public:</div><div> MallocChecker() </div><div>- : BT_DoubleFree(0), BT_Leak(0), BT_UseFree(0), BT_BadFree(0),</div><div>+ : BT_DoubleFree(0), BT_Leak(0), BT_UseFree(0), BT_UseRelinquished(0), BT_BadFree(0),</div><div> II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}</div><div> static void *getTag();</div><div> bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);</div><div>@@ -76,6 +80,7 @@</div><div> </div><div> private:</div><div> void MallocMem(CheckerContext &C, const CallExpr *CE);</div><div>+ void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE, const OwnershipReturnsAttr* Att);</div><div> const GRState *MallocMemAux(CheckerContext &C, const CallExpr *CE,</div><div> const Expr *SizeEx, SVal Init,</div><div> const GRState *state) {</div><div>@@ -86,8 +91,10 @@</div><div> const GRState *state);</div><div> </div><div> void FreeMem(CheckerContext &C, const CallExpr *CE);</div><div>+ void FreeMemTakesAttr(CheckerContext &C, const CallExpr *CE, const OwnershipTakesAttr* Att);</div><div>+ void FreeMemHoldsAttr(CheckerContext &C, const CallExpr *CE, const OwnershipHoldsAttr* Att);</div><div> const GRState *FreeMemAux(CheckerContext &C, const CallExpr *CE,</div><div>- const GRState *state);</div><div>+ const GRState *state, unsigned Num, bool Hold);</div><div> </div><div> void ReallocMem(CheckerContext &C, const CallExpr *CE);</div><div> void CallocMem(CheckerContext &C, const CallExpr *CE);</div><div>@@ -156,7 +163,26 @@</div><div> return true;</div><div> }</div><div> </div><div>- return false;</div><div>+ // Check all the attributes, if there are any.</div><div>+ // There can be multiple of these attributes.</div><div>+ bool rv = false;</div><div>+ if (FD->hasAttrs()) {</div><div>+ for (const Attr *attr = FD->getAttrs(); attr; attr = attr->getNext()) {</div><div>+ if (const OwnershipReturnsAttr* Att = dyn_cast<OwnershipReturnsAttr>(attr)) {</div><div>+ MallocMemReturnsAttr(C, CE, Att);</div><div>+ rv = true;</div><div>+ }</div><div>+ if (const OwnershipTakesAttr* Att = dyn_cast<OwnershipTakesAttr> (attr)) {</div><div>+ FreeMemTakesAttr(C, CE, Att);</div><div>+ rv = true;</div><div>+ }</div><div>+ if (const OwnershipHoldsAttr* Att = dyn_cast<OwnershipHoldsAttr> (attr)) {</div><div>+ FreeMemHoldsAttr(C, CE, Att);</div><div>+ rv = true;</div><div>+ }</div><div><br></div><div>Use 'else if' to avoid redundant checks here.</div><div><br></div><div>+ }</div><div>+ }</div><div>+ return rv;</div><div> }</div><div> </div><div> void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {</div><div>@@ -165,6 +191,23 @@</div><div> C.addTransition(state);</div><div> }</div><div> </div><div>+void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,</div><div>+ const OwnershipReturnsAttr* Att) {</div><div>+ if (!Att->isModule("malloc"))</div><div>+ return;</div><div>+</div><div>+ const unsigned *I = Att->begin(), *E = Att->end();</div><div>+ if (I != E) {</div><div>+ const GRState *state =</div><div>+ MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());</div><div>+ C.addTransition(state);</div><div>+ return;</div><div>+ }</div><div>+ const GRState *state = MallocMemAux(C, CE, UndefinedVal(), UndefinedVal(),</div><div>+ C.getState());</div><div>+ C.addTransition(state);</div><div>+}</div><div>+</div><div> const GRState *MallocChecker::MallocMemAux(CheckerContext &C, </div><div> const CallExpr *CE,</div><div> SVal Size, SVal Init,</div><div>@@ -188,15 +231,41 @@</div><div> }</div><div> </div><div> void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) {</div><div>- const GRState *state = FreeMemAux(C, CE, C.getState());</div><div>+ const GRState *state = FreeMemAux(C, CE, C.getState(), 0, false);</div><div> </div><div> if (state)</div><div> C.addTransition(state);</div><div> }</div><div> </div><div>+void MallocChecker::FreeMemTakesAttr(CheckerContext &C, const CallExpr *CE,</div><div>+ const OwnershipTakesAttr* Att) {</div><div>+ if (!Att->isModule("malloc"))</div><div>+ return;</div><div>+</div><div>+ for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I) {</div><div>+ const GRState *state =</div><div>+ FreeMemAux(C, CE, C.getState(), *I, false);</div><div>+ if (state)</div><div>+ C.addTransition(state);</div><div>+ }</div><div>+}</div><div>+</div><div>+void MallocChecker::FreeMemHoldsAttr(CheckerContext &C, const CallExpr *CE,</div><div>+ const OwnershipHoldsAttr* Att) {</div><div>+ if (!Att->isModule("malloc"))</div><div>+ return;</div><div>+</div><div>+ for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I) {</div><div>+ const GRState *state =</div><div>+ FreeMemAux(C, CE, C.getState(), *I, true);</div><div>+ if (state)</div><div>+ C.addTransition(state);</div><div>+ }</div><div>+}</div><div>+</div><div> const GRState *MallocChecker::FreeMemAux(CheckerContext &C, const CallExpr *CE,</div><div>- const GRState *state) {</div><div>- const Expr *ArgExpr = CE->getArg(0);</div><div>+ const GRState *state, unsigned Num, bool Hold) {</div><div>+ const Expr *ArgExpr = CE->getArg(Num);</div><div> SVal ArgVal = state->getSVal(ArgExpr);</div><div> </div><div> // If ptr is NULL, no operation is preformed.</div><div>@@ -272,7 +341,24 @@</div><div> return NULL;</div><div> }</div><div> </div><div>+ // Check free after relinquished ownership.</div><div>+ if (RS->isRelinquished()) {</div><div>+ ExplodedNode *N = C.GenerateSink();</div><div>+ if (N) {</div><div>+ if (!BT_UseRelinquished)</div><div>+ BT_UseRelinquished = new BuiltinBug("Double free",</div><div>+ "Try to free a memory block that has been passed elsewhere");</div><div>+ // FIXME: should find where it's freed last time.</div><div>+ BugReport *R = new BugReport(*BT_UseRelinquished,</div><div>+ BT_UseRelinquished->getDescription(), N);</div><div>+ C.EmitReport(R);</div><div>+ }</div><div>+ return NULL;</div><div>+ }</div><div>+</div><div> // Normal free.</div><div>+ if (Hold)</div><div>+ return state->set<RegionState>(Sym, RefState::getRelinquished(CE));</div><div> return state->set<RegionState>(Sym, RefState::getReleased(CE));</div><div> }</div><div> </div><div>@@ -437,13 +523,13 @@</div><div> ValMgr.makeIntValWithPtrWidth(0, false));</div><div> </div><div> if (const GRState *stateSizeZero = stateNotEqual->Assume(SizeZero, true)) {</div><div>- const GRState *stateFree = FreeMemAux(C, CE, stateSizeZero);</div><div>+ const GRState *stateFree = FreeMemAux(C, CE, stateSizeZero, 0, false);</div><div> if (stateFree)</div><div> C.addTransition(stateFree->BindExpr(CE, UndefinedVal(), true));</div><div> }</div><div> </div><div> if (const GRState *stateSizeNotZero=stateNotEqual->Assume(SizeZero,false)) {</div><div>- const GRState *stateFree = FreeMemAux(C, CE, stateSizeNotZero);</div><div>+ const GRState *stateFree = FreeMemAux(C, CE, stateSizeNotZero, 0, false);</div><div> if (stateFree) {</div><div> // FIXME: We should copy the content of the original buffer.</div><div> const GRState *stateRealloc = MallocMemAux(C, CE, CE->getArg(1), </div><div>@@ -536,7 +622,6 @@</div><div> // FIXME: check other cases.</div><div> if (RS->isAllocated())</div><div> state = state->set<RegionState>(Sym, RefState::getEscaped(S));</div><div>-</div><div> C.addTransition(state);</div><div> }</div><div> </div><div>Index: lib/Parse/AttributeList.cpp</div><div>===================================================================</div><div>--- lib/Parse/AttributeList.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 106614)</div><div>+++ lib/Parse/AttributeList.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div><div>@@ -118,6 +118,9 @@</div><div> .Case("ns_returns_retained", AT_ns_returns_retained)</div><div> .Case("cf_returns_not_retained", AT_cf_returns_not_retained)</div><div> .Case("cf_returns_retained", AT_cf_returns_retained)</div><div>+ .Case("ownership_returns", AT_ownership_returns)</div><div>+ .Case("ownership_holds", AT_ownership_holds)</div><div>+ .Case("ownership_takes", AT_ownership_takes)</div><div> .Case("reqd_work_group_size", AT_reqd_wg_size)</div><div> .Case("init_priority", AT_init_priority)</div><div> .Case("no_instrument_function", AT_no_instrument_function)</div></div><div><br></div><div>Looks good.</div><div><br><div><div>On Jun 28, 2010, at 2:15 PM, Andrew McGregor wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Oops, forgot to reply-all...<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Andrew McGregor</b> <span dir="ltr"><<a href="mailto:andrewmcgr@gmail.com">andrewmcgr@gmail.com</a>></span><br>
Date: Mon, Jun 28, 2010 at 12:04 PM<br>Subject: Re: Ownership attribute for malloc etc. checking<br>To: Jordy Rose <<a href="mailto:jediknil@belkadan.com">jediknil@belkadan.com</a>><br><br><br>Hi,<div><br></div><div>
Thanks for the review. Comments inline, new patch attached.</div><div><br></div><div>Andrew<br><br><div class="gmail_quote"><div class="im">On Sun, Jun 27, 2010 at 9:33 AM, Jordy Rose <span dir="ltr"><<a href="mailto:jediknil@belkadan.com" target="_blank">jediknil@belkadan.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Looking good! Some remaining comments below.<br>
<br>
I'm also a reasonably new committer, so someone else should probably look<br>
over this as well (particularly the Attr hunks, which I'm not very familiar<br>
with).<br>
<br>
Jordy<br>
<br>
<br>
General:<br>
- It would be nice to make sure one argument isn't both ownership_takes<br>
and ownership_holds.<br></blockquote><div><br></div></div><div>True, will do.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- LLVM's convention is to use spaces rather than tabs.<br>
- There's a hunk where the only thing that changed is indentation; it'd be<br>
nice to take that out.<br></blockquote><div><br></div></div><div>Oops, neither of these were intentional.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- Two more useful test would be free(p); my_free(p) and free(p);<br>
my_hold(p). Both of which should warn, of course.<br></blockquote><div><br></div></div><div>Instead of crashing, yes. Well spotted.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">
(MallocChecker::EvalCallExpr)<br>
+ for (const Attr *attr = FD->getAttrs(); attr; attr = attr->getNext())<br>
{<br>
+ if (const OwnershipReturnsAttr* Att =<br>
dyn_cast<OwnershipReturnsAttr>(attr)) {<br>
+ MallocMemReturnsAttr(C, CE, Att);<br>
+ rv = true;<br>
+ }<br>
+ if (const OwnershipTakesAttr* Att = dyn_cast<OwnershipTakesAttr><br>
(attr)) {<br>
+ FreeMemTakesAttr(C, CE, Att);<br>
+ rv = true;<br>
+ }<br>
+ if (const OwnershipHoldsAttr* Att = dyn_cast<OwnershipHoldsAttr><br>
(attr)) {<br>
+ FreeMemHoldsAttr(C, CE, Att);<br>
+ rv = true;<br>
+ }<br>
+ }<br>
<br>
Here's the dispatch from EvalCallExpr(). I think even though it's<br>
technically a little less efficient, you should just simplify this and use<br>
FD->getAttr<OwnershipTakesAttr>(), etc. It's a little simpler, and it lets<br>
you order the attributes.<br></blockquote><div><br></div></div><div>However, I did it this way because I wanted to allow two or more attributes of the same kind (for the benefit of macros).</div><div><br></div><div>In other words, __attribute((ownership_holds, 1)) __attribute((ownership_holds, 2)) should be the same as __attribute((ownership_holds, 1, 2)).</div>
<div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Why does that matter? Because an ownership_takes() or ownership_holds()<br>
function might have a return value! If it's also ownership_returns(), then<br>
you're fine, but otherwise you need to set a symbolic return value. (You<br>
can see how this is done in MallocMemAux(), or in StreamChecker.cpp with<br>
fopen.) There should probably be a test for this as well.</blockquote><div><br></div></div><div>It isn't necessarily the case that an ownership_takes() function that returns a pointer generated that pointer from an ownership_takes argument, or allocated anything, so what would you set the region to, in the absence of other information? They default to Unknown, yes?</div>
<div class="im">
<div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">
Actually, because these attributes don't affect the return value, they<br>
should probably go in a PreVisitCallExpr() callback, rather than<br>
EvalCallExpr(). But for now it's probably okay, as long as you handle that<br>
return value.<br>
<br>
<br>
(MallocChecker::MallocMemReturnsAttr)<br>
+ for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I,<br>
++count) {<br>
+ const GRState *state =<br>
+ MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(),<br>
C.getState());<br>
+ C.addTransition(state);<br>
+ }<br>
+ if (count == 0) {<br>
+ const GRState *state = MallocMemAux(C, CE, UndefinedVal(),<br>
UndefinedVal(),<br>
+ C.getState());<br>
+ C.addTransition(state);<br>
+ }<br>
<br>
What does it mean to have multiple size fields? There can still only be<br>
one return value. Maybe you should only allow zero or one positions for<br>
ownership_returns (which would make this section simpler).<br></blockquote><div><br></div></div><div>The intention here was to implement something for calloc-like functions and matrix allocators; one argument is a special case, more than one the size of the region is the product of the arguments times sizeof(pointee of the return value), taking sizeof(void) as 1. If it isn't worth doing that, fair enough, but that's what I was thinking this could lead to.</div>
<div><br></div><div>However, the parser only allows the one argument at the moment, so this is redundant.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
(MallocChecker::PreVisitReturnStmt)<br>
+ if (RS->isReleased()) {<br>
+ ExplodedNode *N = C.GenerateSink();<br>
+ if (!BT_UseFree)<br>
+ BT_UseFree = new BuiltinBug("Use dynamically allocated memory<br>
after"<br>
+ " it is freed.");<br>
<br>
+ BugReport *R = new BugReport(*BT_UseFree,<br>
BT_UseFree->getDescription(),<br>
+ N);<br>
+ C.EmitReport(R);<br>
+ }<br>
<br>
This section checks whether the return value is a released address.<br>
Unfortunately this isn't a 100% unambiguous case; consider the following<br>
(contrived) example:<br>
<br>
struct Node *freeSmallest () {<br>
struct Node *min;<br>
// find the minimum node<br>
free(min);<br>
return min; // so the caller knows the ID of the deleted node<br>
}<br>
<br>
As long as the caller doesn't dereference the returned pointer, it could<br>
still be a useful value. 95% of the time, this check would be useful, but<br>
since a false positive is worse than a false negative for the analyzer, we<br>
should probably leave it out.<br>
</blockquote></div></div><br></div><div>Ok, I see. Out it is, then.</div><div><br></div><font color="#888888"><div>Andrew</div>
</font></div><br>
<span><clang-ownership-withtests-r1.patch></span></blockquote></div><br></div></body></html>