public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [Bug c++/97752] New: incorrect address to inherited object in constexpr context @ 2020-11-07 14:58 steven.vanderschoot@nextlevel-electronics.com 2020-11-07 15:50 ` [Bug c++/97752] " mpolacek at gcc dot gnu.org ` (2 more replies) 0 siblings, 3 replies; 4+ messages in thread From: steven.vanderschoot@nextlevel-electronics.com @ 2020-11-07 14:58 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97752 Bug ID: 97752 Summary: incorrect address to inherited object in constexpr context Product: gcc Version: 10.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: steven.vanderschoot@nextlevel-electronics.com Target Milestone: --- Created attachment 49519 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49519&action=edit C++ code to trigger the bug When working on a project I discovered a function where gcc, when in a constexpr context, seems to incorrectly compute the value of a pointer. Attached is a reduced example that triggers the bug. I defined a class Derived that inherits from two classes Base1 and Base2. A member function in Base1 will downcast its this pointer to Derived*, which is than casted to Base2*. When dereferencing the Base2* pointer in a constexpr context gcc reports: modification of ‘*(((Derived<Base1, Base2>*)(& derived.Derived<Base1, Base2>::<anonymous>)) + 18446744073709551612)’ is not a constant expression. The code attached to this bug report has a some template magic around the described behaviour. When I tried to remove the template magic I failed to get this bug to be triggered. Note this only fails when explicitly executing in a constexpr context. When the constexpr keyword on line 178 is removed the resulting binary seems correct: 0000000000000000 <test()>: 0: b8 4a 04 00 00 mov $0x44a,%eax 5: c3 retq Also the code seems to compile fine in a constexpr context using clang: https://cpp.godbolt.org/z/n3c3ea gcc version: 10.2.1 20201016 System: Fedora 33 x86_64 Command: g++ -v -std=c++17 -O3 -Wextra -pedantic test.cpp Response when running g++: ------------------------------------------------------ Using built-in specs. COLLECT_GCC=/usr/bin/g++ OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.2.1 20201016 (Red Hat 10.2.1-6) (GCC) COLLECT_GCC_OPTIONS='-v' '-std=c++17' '-O3' '-Wextra' '-Wpedantic' '-c' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/10/cc1plus -quiet -v -D_GNU_SOURCE ./test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase-strip test.o -O3 -Wextra -Wpedantic -std=c++17 -version -o /tmp/ccIaE47Z.s GNU C++17 (GCC) version 10.2.1 20201016 (Red Hat 10.2.1-6) (x86_64-redhat-linux) compiled by GNU C version 10.2.1 20201016 (Red Hat 10.2.1-6), GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version isl-0.16.1-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/10/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/10/../../../../x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux /usr/lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward /usr/lib/gcc/x86_64-redhat-linux/10/include /usr/local/include /usr/include End of search list. GNU C++17 (GCC) version 10.2.1 20201016 (Red Hat 10.2.1-6) (x86_64-redhat-linux) compiled by GNU C version 10.2.1 20201016 (Red Hat 10.2.1-6), GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version isl-0.16.1-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: bc4d6419b2bc73981424827b3550eaef ./test.cpp: In function ‘int test()’: ./test.cpp:178:27: in ‘constexpr’ expansion of ‘f.test()::<lambda()>()’ ./test.cpp:175:11: in ‘constexpr’ expansion of ‘(& obj)->Base1<Derived<Base1, Base2> >::test()’ ./test.cpp:26:16: in ‘constexpr’ expansion of ‘Base1<Derived<Base1, Base2> >::callCallbacks<Base2<NS>::test<int>::<lambda(auto:2)> >(callbacks, 987)’ ./test.cpp:42:16: in ‘constexpr’ expansion of ‘Base1<Derived<Base1, Base2> >::callCallbacks<Base2<NS>::test<int>::<lambda(auto:2)>, 0>(callbacks, payload, (std::make_index_sequence<1>{}, std::make_index_sequence<1>()))’ ./test.cpp:37:26: in ‘constexpr’ expansion of ‘(& std::get<0, Base2<NS>::test<int>::<lambda(auto:2)> >(callbacks))->Base2<NS>::test<int>::<lambda(auto:2)>(payload)’ ./test.cpp:63:4: in ‘constexpr’ expansion of ‘(& other)->Base1<Derived<Base1, Base2> >::set((payload + 111))’ ./test.cpp:178:28: error: modification of ‘*(((Derived<Base1, Base2>*)(& derived.Derived<Base1, Base2>::<anonymous>)) + 18446744073709551612)’ is not a constant expression 178 | constexpr auto result = f(); | ^ ------------------------------------------------------ ^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/97752] incorrect address to inherited object in constexpr context 2020-11-07 14:58 [Bug c++/97752] New: incorrect address to inherited object in constexpr context steven.vanderschoot@nextlevel-electronics.com @ 2020-11-07 15:50 ` mpolacek at gcc dot gnu.org 2021-08-28 22:11 ` pinskia at gcc dot gnu.org 2021-11-06 23:40 ` ppalka at gcc dot gnu.org 2 siblings, 0 replies; 4+ messages in thread From: mpolacek at gcc dot gnu.org @ 2020-11-07 15:50 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97752 Marek Polacek <mpolacek at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mpolacek at gcc dot gnu.org Status|UNCONFIRMED |NEW Last reconfirmed| |2020-11-07 Ever confirmed|0 |1 --- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> --- Reduced: template <int a> struct b { static constexpr int c = a; }; template <typename, typename> struct g : b<false> {}; template <typename d> struct g<d, d> : b<true> {}; template <int, typename e, typename> struct j { typedef e af; }; template <typename e, typename k> struct j<false, e, k> { typedef k af; }; template <bool ag, typename e, typename k> using ah = typename j<ag, e, k>::af; template <typename d, typename aj> constexpr bool ak = g<d, aj>::c; template <typename> struct l; template <long, typename d> using ao = typename l<d>::af; template <unsigned long> struct m; struct n { typedef m<0> ar; }; template <typename d, d> struct o {}; template <typename, typename = n::ar> struct p; template <typename d, unsigned long... av> struct p<d, m<av...>> { typedef o<d, av...> ar; }; template <typename d, d> using aw = typename p<d>::ar; template <long... av> using ax = o<unsigned long, av...>; template <long at> using ay = aw<unsigned long, at>; template <typename az> struct ba { template <typename bb> constexpr ba(bb q) : bd(q) {} static constexpr az be(ba &q) { return q.bd; } az bd; }; template <unsigned long, typename...> struct bg; template <unsigned long av, typename az> struct bg<av, az> : ba<az> { template <typename bb> constexpr bg(bb q) : ba<az>(q) {} }; template <typename... bo> class r : public bg<0, bo...> { public: template <typename... bm> constexpr r(bm... q) : bg<0, bo...>(q...) {} }; template <typename az, typename... br> struct l<r<az, br...>> { typedef az af; }; template <unsigned long bs, typename az> constexpr az bt(bg<bs, az> &q) { return bg<bs, az>::be(q); } template <int bs, typename... bo> constexpr ao<bs, r<bo...>> bu(r<bo...> q) { return bt(q); } template <typename... bo> constexpr r<bo...> bv(bo... q) { return r<bo...>(q...); } template <typename... bx> struct by { template <typename bz> static constexpr bool ca = (... || ak<bx, bz>); }; class s; template <typename cd> class t { public: using bx = by<int>; constexpr void cf() { auto callbacks = cg().template ch<s>([](auto &ci) { return ci.cf(6); }); cj(callbacks, 7); } constexpr void ck(int q) { cl = q; } int cl; template <typename... bz, unsigned long... h> constexpr void cj(r<bz...> q, int p2, ax<h...>) { (bu<h>(q)(p2), ...); } template <typename... bz> constexpr void cj(r<bz...> q, int p2) { cj(q, p2, ay<sizeof...(bz)>{}); } constexpr auto &cg() { return *static_cast<cd *>(this); } }; template <template <typename> typename...> class Q; template <typename> class u { public: using bx = by<s>; template <typename bz> constexpr auto cf(bz) { return [this](auto) { auto &ci = cg(); ci.ck(1); }; } int i; constexpr auto &cg() { return *static_cast<Q<t, u> *>(this); } }; template <template <typename> typename... cp> class Q : public cp<Q<cp...>>... { struct L { template <typename cr> static typename cr::bx cs(int); template <typename cr> using by = decltype(cs<cr>(0)); }; template <typename...> struct v { template <typename cu> using cv = v<cu>; template <typename> using cw = v; }; template <typename bz, typename... cx> struct v<bz, cx...> { template <typename cr> using cw = ah<L::template by<bz>::template ca<cr>, typename v<>::template cv<bz>, typename v<cx...>::template cw<cr>>; }; template <typename> struct w; template <typename... cz> struct w<v<cz...>> { static constexpr auto &da(Q &q) { return (q.bu<cz>(), ...); } template <typename dc> static constexpr auto dd(Q &q, dc p2) { return bv(p2(q.bu<cz>())...); } }; public: template <typename cr> constexpr auto &bu() { return *static_cast<cr *>(this); } template <typename> constexpr auto &de() { return w<typename v<cp<Q>...>::template cw<int>>::da(*this); } template <typename cw, typename dc> constexpr auto ch(dc q) { return w<typename v<cp<Q>...>::template cw<cw>>::dd(*this, q); } }; void f() { auto f = [] { Q<t, u> derived{}; auto &df = derived.de<int>(); df.cf(); return df; }; constexpr auto dg = f(); } ^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/97752] incorrect address to inherited object in constexpr context 2020-11-07 14:58 [Bug c++/97752] New: incorrect address to inherited object in constexpr context steven.vanderschoot@nextlevel-electronics.com 2020-11-07 15:50 ` [Bug c++/97752] " mpolacek at gcc dot gnu.org @ 2021-08-28 22:11 ` pinskia at gcc dot gnu.org 2021-11-06 23:40 ` ppalka at gcc dot gnu.org 2 siblings, 0 replies; 4+ messages in thread From: pinskia at gcc dot gnu.org @ 2021-08-28 22:11 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97752 --- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> --- Seems fixed on the trunk. ^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/97752] incorrect address to inherited object in constexpr context 2020-11-07 14:58 [Bug c++/97752] New: incorrect address to inherited object in constexpr context steven.vanderschoot@nextlevel-electronics.com 2020-11-07 15:50 ` [Bug c++/97752] " mpolacek at gcc dot gnu.org 2021-08-28 22:11 ` pinskia at gcc dot gnu.org @ 2021-11-06 23:40 ` ppalka at gcc dot gnu.org 2 siblings, 0 replies; 4+ messages in thread From: ppalka at gcc dot gnu.org @ 2021-11-06 23:40 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97752 Patrick Palka <ppalka at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |ppalka at gcc dot gnu.org Status|NEW |RESOLVED Resolution|--- |DUPLICATE --- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> --- Looks like this is a dup of PR100209 which has recently been fixed for GCC 12. Thanks for the bug report. *** This bug has been marked as a duplicate of bug 100209 *** ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-11-06 23:40 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-11-07 14:58 [Bug c++/97752] New: incorrect address to inherited object in constexpr context steven.vanderschoot@nextlevel-electronics.com 2020-11-07 15:50 ` [Bug c++/97752] " mpolacek at gcc dot gnu.org 2021-08-28 22:11 ` pinskia at gcc dot gnu.org 2021-11-06 23:40 ` ppalka at gcc dot gnu.org
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).