[LLVMdev] [cfe-dev] RFC: Adding attribute(nonnull) to things in libc++
dblaikie at gmail.com
Mon Aug 3 20:07:09 PDT 2015
On Mon, Aug 3, 2015 at 7:20 PM, Chandler Carruth <chandlerc at google.com>
> I just want to revive this thread.
> I'm not really endorsing this particular use of non-null in API design (I
> agree with the committee that we should permit 'nullptr, 0' arguments).
> However, I think that we should add these attributes for memcpy and
> friends. Why? Because glibc has already shipped with these attributes which
> means that portable code will never be able to pass a null pointer here
> without running the risk of miscompile. Even if the standard were to
> retract the dubious wording, we would have a very large number of glibc
> installations in the world with the attributes and a large number of
> compilers that will miscompile code by optimizing based on them. We should,
> IMO, annotate it everywhere so that warnings and tools like UBSan can find
> all of these bugs waiting to happen.
I rather like optimizing on these things - but if you don't & you just want
to provide defense for those users writing portable code without punishing
them on our own compiler, what about using Doug's new nullability
attributes that are documented to not impact optimizatiosn/codegen?
> To get an idea of how widespread this is on at least the Linux platform,
> they went into glibc over a decade ago:
> I just spent a pile of time cleaning up LLVM and Clang. It would be great
> to get more help keeping us in this clean state.
> On Wed, Jun 3, 2015 at 6:37 AM Marshall Clow <mclow.lists at gmail.com>
>> On Wed, Jun 3, 2015 at 12:21 AM, Nick Lewycky <nlewycky at google.com>
>>> On 1 June 2015 at 07:20, Marshall Clow <mclow.lists at gmail.com> wrote:
>>>> This weekend, I got an email from Nuno Lopes informing me that UBSAN
>>>> now paid attention to attribute(nonnull), and he was having some problems
>>>> with it going off when using libc++.
>>> FYI, I also looked into turning this on, but with libstdc++, and found
>>> that they annotated basic_string<T>::assign(pointer, len) with attribute
>>> nonnull. That's a problem, because it's valid to call
>>> basic_string<T>::assign(nullptr, 0), but the reasoning why it's valid makes
>>> me want to ask the committee whether this is what they intended.
>>> The language std text claims that the pointer must point to an array of
>>> 'n' (second argument) length, but earlier in the text it also states
>>> that in the library, whenever it says "array" it means any pointer upon
>>> which address computations and accesses to objects (that would be valid if
>>> the pointer did point to the first element of such an array). Thus, nullptr
>>> is valid if 'n' is zero.
>>> This was changed in DR2235:
>>> The text and discussion of DR2235 sound like they intend to make the
>>> behaviour of assign match that of the constructor that takes the same
>>> arguments. What they actually did was change the constructor to match the
>>> behaviour of assign, and it doesn't look like removing the requirement of a
>>> nonnull pointer was considered and intended.
>>> At this point I made a note that somebody should ask the committee when
>>> they get the chance, and never got back around to it.
>> I think that the reference in the discussion to [res.on.arguments] (the
>> relevant section there is paragraph 1.2) make it clear that the change was
>> deliberate - to allow (null, 0) as a set of parameters.
>> I do agree that this is different behavior than memcpy, memmove, etc.
>> That should make Joerg happy :-)
>> -- Marshall
>> LLVM Developers mailing list
>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the llvm-dev