[PATCH] Fix const pointer assignment trick on clang 9+

余生与君 lovesykun at gmail.com
Sat Oct 9 18:21:26 UTC 2021


Done and tested:
http://lists.busybox.net/pipermail/busybox/2021-October/089292.html


On Sat, Oct 9, 2021 at 7:58 PM Denys Vlasenko <vda.linux at googlemail.com> wrote:
>
> On Fri, Oct 8, 2021 at 10:39 AM 余生与君 <lovesykun at gmail.com> wrote:
> > On Thu, Oct 7, 2021 at 9:48 PM Denys Vlasenko <vda.linux at googlemail.com> wrote:
> > > On Thu, Oct 7, 2021 at 3:25 PM 余生与君 <lovesykun at gmail.com> wrote:
> > > > > where p is a dummy, unused variable
> > > > No. p here shadows the global variable with the same name local
> > > > variable so the following context (scope) will use this local variable
> > > > instead of the global one.
> > >
> > > Aha...
> > > The problem here is that even though later uses of "p" in this block
> > > where we use ASSIGN_CONST_PTR() macro will use the local "p" pointer,
> > > when we exit the block, the following uses will refer to the global one.
> > > Nothing prevents them to still use incorrect value.
> > > The fix depends only on the hope that there won't be such uses.
> > > But they already exist:
> > >
> > > inetd.c:
> > >         INIT_G();
> > >         real_uid = getuid();
> > >
> > > ftpgetput.c:
> > >         INIT_G();
> > >         /* Set default values */
> > >         user = "anonymous";
> > >         password = "busybox";
> >
> > Yes, I noticed that, and that's why I removed do-while in the INIT_G
> > in this patch.
> >
> > > Can you try something more? E.g. (in current git):
> > >
> > > #define ASSIGN_CONST_PTR(p, v) do { \
> > >         *(void**)not_const_pp(&p) = (void*)(v); \
> > >         /* At least gcc 3.4.6 on mipsel needs optimization barrier */ \
> > >         barrier(); \
> > > +       sleep(0); \
> > > } while (0)
> >
> > Cool! Sleep(0) does the magic!
> >
> > ADRP            X21, #ash_ptr_to_globals_misc_ptr at PAGE
> > LDR             X21, [X21,#ash_ptr_to_globals_misc_ptr at PAGEOFF]
> > MOV             X8, X21
> > STR             X0, [X8]
> > MOV             W0, WZR
> > BL              sleep
> > LDR             X21, [X21]
> >
> >
> > And further investigation shows that a dummy function can also do this trick!
> >
> > hack.c:
> > void clang_barrier() {
> > }
> >
> > libbb.h:
> > void clang_barrier(); // invisible in this file
> > #define ASSIGN_CONST_PTR(p, v) do { \
> >         *(void**)not_const_pp(&p) = (void*)(v); \
> >         /* At least gcc 3.4.6 on mipsel needs optimization barrier */ \
> > -        barrier(); \
> > +       clang_barrier(); \
> > } while (0)
>
> Let's go with having a function.
>
> All ASSIGN_CONST_PTR's except one assign a malloced address.
>
> Let's have XZALLOC_CONST_PTR(&cptr, size), and let's
> make it a function, not macro, for clang. It will act
> a barrier function.


More information about the busybox mailing list