A question about nonnull value of auto_ptr
Hal Finkel
hfinkel at anl.gov
Tue Nov 4 18:40:33 PST 2014
----- Original Message -----
> From: "Jiangning Liu" <liujiangning1 at gmail.com>
> To: "llvm-commits at cs.uiuc.edu for LLVM" <llvm-commits at cs.uiuc.edu>, cfe-commits at cs.uiuc.edu
> Sent: Tuesday, November 4, 2014 8:23:13 PM
> Subject: A question about nonnull value of auto_ptr
>
> Hi,
>
>
> For the following small test case,
>
>
> #include <iostream>
> #include <memory>
>
>
> class C {
> public:
> std::auto_ptr<const C> pc;
>
>
> int f(void);
> };
>
>
> int C::f(void) {
> return (&*this->pc == 0);
> }
>
>
> clang/llvm -O2 optimizes (&*this->pc == 0) to be false statically,
> while gcc doesn't.
>
>
> So my question is if this optimization is really safe?
>
Yes, the program here is not well defined. Calling operator*() on an auto_ptr, according to the standard, "Requires: get() != 0
". (see D.10.1.2). Generally speaking, binding a reference to a null pointer is undefined behavior. Changing the program to this->pc.get() != 0 should fix the problem.
-Hal
>
>
> Front-end result shows the auto pointer is nonnull, i.e.
> dereferenceable(8), and this is why llvm back-end can optimize the
> comparison away, but the problem is can we really assume &*this->pc
> is not 0? Why?
>
>
>
> ; Function Attrs: nounwind uwtable
> define i32 @_ZN1C1fEv(%class.C* %this) #2 align 2 {
> entry:
> %this.addr = alloca %class.C*, align 8
> store %class.C* %this, %class.C** %this.addr, align 8
> %this1 = load %class.C** %this.addr
> %pc = getelementptr inbounds %class.C* %this1, i32 0, i32 0
> %call = call dereferenceable(8) %class.C*
> @_ZNKSt8auto_ptrIK1CEdeEv(%"class.std::auto_ptr"* %pc) #1
> %cmp = icmp eq %class.C* %call, null
> %conv = zext i1 %cmp to i32
> ret i32 %conv
> }
>
>
> BTW, this issue triggered a run-time crash for one of SPEC
> benchmarks.
>
>
> Thanks,
> -Jiangning
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
--
Hal Finkel
Assistant Computational Scientist
Leadership Computing Facility
Argonne National Laboratory
More information about the cfe-commits
mailing list