ASH: why ash_ptr_to_globals_misc is declared as const pointer

Mike Frysinger vapier at gentoo.org
Fri Jan 26 22:15:15 UTC 2018


On 26 Jan 2018 11:39, Yunlian Jiang wrote:
> I will use a similar issue in lineedit.c to explain the problem. (it is
> easier to reproduce).
> The problem is that
> clang thinks lineedit_ptr_to_statics is a constant pointer, so the pointer
> should be unchanged. As a result, inside a function, it
> loads the value once, and it can use it as many as it want without worrying
> anthing changes.
> In the macro
> 
>  #define INIT_S() do { \
>         (*(struct lineedit_statics**)&lineedit_ptr_to_statics) =
> xzalloc(sizeof(S)); \
>         barrier(); \
>         cmdedit_termw = 80; \
>         IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
>         IF_FEATURE_EDITING_VI(delptr = delbuf;) \
> } while (0)
> 
>  (*(struct lineedit_statics**)&lineedit_ptr_to_statics) =
> xzalloc(sizeof(S));
> actually changes value of the pointer (from 0x0 to the return value of
> xzalloc). But clang did not realize that, otherwise, it
> should omit a compiler time error, so clang thinks it has nothing to do
> with lineedit_ptr_to_statics
> 
> in the statement
> IF_FEATURE_EDITING_VI(delptr = delbuf;), clang still assumes the value of
> the pointer is '0x0', so the segfaults happens.
> 
> 
> I think compiler has the right to assume the pointer is unchanged because
> it is declared as const.

thanks, that analysis looks good.  we've been using barrier() to trick
compilers into doing the load after we init it (which i'm pretty sure
is the point of commit 574f2f43948bb21d6e4187936ba5a5afccba25f6 [1]).
the question is how can we trick clang into delaying the load :).
-mike

[1] https://git.busybox.net/busybox/commit/?id=574f2f43948bb21d6e4187936ba5a5afccba25f6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Digital signature
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20180126/8dbe247a/attachment.asc>


More information about the busybox mailing list