[llvm-dev] GEP with a null pointer base

David Blaikie via llvm-dev llvm-dev at lists.llvm.org
Fri Aug 4 11:03:32 PDT 2017


On Fri, Aug 4, 2017 at 9:34 AM Peter Lawrence <peterl95124 at sbcglobal.net>
wrote:

> Daniel,
>             I think you do or at least did work at Apple,
> which is highly invested in static analyzers, in fact
> one of the reasons they chose to use LLVM is because
> of its suitability as a base for static analyzers. I am
> guessing that the standard software engineering practice
> at Apple is that software needs to be compiler warning free,
> static analyzer warning free, and dynamic sanitizer warning
> free. Given all that, then there really isn’t anything left for
> the compiler to delete when it comes to deleting “UB”.
>

That's, again, not exactly true - examples have been given on several of
these threads where a perfectly valid (& warning, static, and dynamic
analysis clean) program contains dead UB that can be deleted. That's partly
the point of building those tools (static, dynamic, etc), to make sure
these optimizations (deleting UB code as dead, for example) can be made
safely (& when a user stumbles over problems, they can use the tools to
double check/get a better message about where they went wrong).

As in the example I gave previously.

  bool is_good_for_thing(int*);
  void first_thing();
  void middle_thing(int);
  void last_thing();

  void do_all_the_things(int *i) {
    first_thing();
    if (is_good_for_thing(i))
      middle_thing(*i);
    last_thing();
  }

  void do_all_default_things() {
    do_all_the_things(nullptr);
  }

  // in another file:
  bool is_good_for_things(int* i) {
    if (!i)
      return false;
    /* lots of other tests of *i for 'goodness' */
  }

do_all_the_things does not have UB for any value of 'i' including null -
but the compiler can't prove that. But at the do_all_the_things caller,
once it's inlined and 'i' is seen to be null, the compiler can conclude
that 'is_good_for_thing' must return false if 'i' is null, otherwise there
would be UB, and delete the middle_thing call.

This code would be warning, static analysis (even global static analysis,
if such were tractable), and dynamic analysis clean - yet still there
remains (after inlining) dead UB code which the compiler can remove.


> So why the insistence that it should delete this “UB”
> rather than warn, why the insistence that there not be an
> option to get a warning rather than silently deleting, in
> case the compiler found some “UB” that the static analyzer
> isn’t yet capable of finding, and needs to be enhanced for.
>

Except it doesn't or can't (interprocedural or inter-file analysis - may be
impossible (separate compilation) or prohibitively expensive (too many
paths)) - that's why we've built the dynamic analysis tools. And yes, if
you find a place where LLVM deletes UB code that wouldn't've been diagnosed
by -fsanitize=undefined - sounds like a great opportunity to enhance it!
That would help users immensely, I'm sure.


>
> If Apple is still your employer, I think they would find it odd
> that you are arguing against their established software
> engineering principles.
>
>
> Peter Lawrence.
>
>
>
>
> On Aug 2, 2017, at 8:34 AM, Daniel Berlin <dberlin at dberlin.org> wrote:
>
>
>> Saying “The C++ language lets me assume that that won’t happen, &
>> optimize
>> on that basis” is an assumption that that’s what the user wants, but you
>> haven’t asked you’ve just assumed, and AFAICT it is an incorrect
>> assumption.
>>
> This is actually an incorrect assumption on your part.  Many users have
> been asked many many many times.  This wasn't just done.
> So please don't say "you haven't asked you've just assumed", because that
> is flat out wrong.
>
> " and AFAICT it is an incorrect assumption."
> This however, needs a citation to anything that is real data.
>
> What’s worse is that there isn’t any way to opt-out of this assumption
>> (-fsanitize=undefined isn’t opt’ing out, its opt’ing in to a lot extra
>> that I
>> might or might not want).
>>
>
> Correct, there is no way to opt-out except to use a different language (or
> at the very least, a different compiler, but i'm not aware of any that
> would meet your requirements in general).
> Sometimes, you don't build something for every use case, and the answer is
> "if that's your use case, that's awesome, but it's not a thing we serve".
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170804/67b997e3/attachment.html>


More information about the llvm-dev mailing list