It was observed that getty crashes on RISC-V 64-bit target, with the
busybox binary compiled by clang/LLVM 17 with -O2. Not only getty,
but also some other applets like syslogd/vi are broken too.

Commit 5156b245 ("Make const ptr assign as function call in clang")
introduced XZALLOC_CONST_PTR() to defeat the compiler optimization,
however it only fixed a small number of broken places when compiling
busybox with clang/LLVM. A large number of places remain broken.

This commit renames existing ASSIGN_CONST_PTR() to _ASSIGN_CONST_PTR()
and makes the new ASSIGN_CONST_PTR() an out-of-line function to prevent
clang from reading pointer before it is assigned. With that, previous
XZALLOC_CONST_PTR() is no longer necessary and gets removed.

Signed-off-by: Bin Meng <[email protected]>

---
This commit supersedes previous one [1] as the fix is generic instead
of only fixing getty codes.

[1] https://lists.busybox.net/pipermail/busybox/2025-May/091486.html

 coreutils/test.c   |  2 +-
 include/libbb.h    | 10 +++++-----
 libbb/const_hack.c |  4 ++--
 libbb/lineedit.c   |  2 +-
 shell/ash.c        |  6 +++---
 5 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/coreutils/test.c b/coreutils/test.c
index b63e33cc0..2cb396ef8 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -444,7 +444,7 @@ extern struct test_statics *BB_GLOBAL_CONST 
test_ptr_to_statics;
 #define leaving         (S.leaving      )
 
 #define INIT_S() do { \
-       XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \
+       ASSIGN_CONST_PTR(&test_ptr_to_statics, xzalloc(sizeof(S))); \
 } while (0)
 #define DEINIT_S() do { \
        free(test_ptr_to_statics); \
diff --git a/include/libbb.h b/include/libbb.h
index 558f3c627..6e44e0aec 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -2350,21 +2350,21 @@ static ALWAYS_INLINE void* not_const_pp(const void *p)
        );
        return pp;
 }
-# define ASSIGN_CONST_PTR(pptr, v) do { \
+# define _ASSIGN_CONST_PTR(pptr, v) do { \
        *(void**)not_const_pp(pptr) = (void*)(v); \
        barrier(); \
 } while (0)
-/* XZALLOC_CONST_PTR() is an out-of-line function to prevent
+/* ASSIGN_CONST_PTR() is an out-of-line function to prevent
  * clang from reading pointer before it is assigned.
  */
-void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC;
+void ASSIGN_CONST_PTR(const void *pptr, void *v) FAST_FUNC;
 #else
-# define ASSIGN_CONST_PTR(pptr, v) do { \
+# define _ASSIGN_CONST_PTR(pptr, v) do { \
        *(void**)(pptr) = (void*)(v); \
        /* At least gcc 3.4.6 on mipsel needs optimization barrier */ \
        barrier(); \
 } while (0)
-# define XZALLOC_CONST_PTR(pptr, size) ASSIGN_CONST_PTR(pptr, xzalloc(size))
+# define ASSIGN_CONST_PTR(pptr, v) _ASSIGN_CONST_PTR(pptr, v)
 #endif
 
 #define SET_PTR_TO_GLOBALS(x) ASSIGN_CONST_PTR(&ptr_to_globals, x)
diff --git a/libbb/const_hack.c b/libbb/const_hack.c
index 9575e6d67..1568ac54d 100644
--- a/libbb/const_hack.c
+++ b/libbb/const_hack.c
@@ -9,8 +9,8 @@
 #include "libbb.h"
 
 #if defined(__clang_major__) && __clang_major__ >= 9
-void FAST_FUNC XZALLOC_CONST_PTR(const void *pptr, size_t size)
+void FAST_FUNC ASSIGN_CONST_PTR(const void *pptr, void *v)
 {
-       ASSIGN_CONST_PTR(pptr, xzalloc(size));
+       _ASSIGN_CONST_PTR(pptr, v);
 }
 #endif
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 151208c1c..93fc88ea4 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -212,7 +212,7 @@ extern struct lineedit_statics *BB_GLOBAL_CONST 
lineedit_ptr_to_statics;
 #define delbuf           (S.delbuf          )
 
 #define INIT_S() do { \
-       XZALLOC_CONST_PTR(&lineedit_ptr_to_statics, sizeof(S)); \
+       ASSIGN_CONST_PTR(&lineedit_ptr_to_statics, xzalloc(sizeof(S))); \
 } while (0)
 
 static void deinit_S(void)
diff --git a/shell/ash.c b/shell/ash.c
index 9173b8608..d63de6f83 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -531,7 +531,7 @@ extern struct globals_misc *BB_GLOBAL_CONST 
ash_ptr_to_globals_misc;
 #define random_gen  (G_misc.random_gen )
 #define backgndpid  (G_misc.backgndpid )
 #define INIT_G_misc() do { \
-       XZALLOC_CONST_PTR(&ash_ptr_to_globals_misc, sizeof(G_misc)); \
+       ASSIGN_CONST_PTR(&ash_ptr_to_globals_misc, xzalloc(sizeof(G_misc))); \
        savestatus = -1; \
        curdir = nullstr; \
        physdir = nullstr; \
@@ -1612,7 +1612,7 @@ extern struct globals_memstack *BB_GLOBAL_CONST 
ash_ptr_to_globals_memstack;
 #define g_stacknleft (G_memstack.g_stacknleft)
 #define stackbase    (G_memstack.stackbase   )
 #define INIT_G_memstack() do { \
-       XZALLOC_CONST_PTR(&ash_ptr_to_globals_memstack, sizeof(G_memstack)); \
+       ASSIGN_CONST_PTR(&ash_ptr_to_globals_memstack, 
xzalloc(sizeof(G_memstack))); \
        g_stackp = &stackbase; \
        g_stacknxt = stackbase.space; \
        g_stacknleft = MINSIZE; \
@@ -2242,7 +2242,7 @@ extern struct globals_var *BB_GLOBAL_CONST 
ash_ptr_to_globals_var;
 #endif
 #define INIT_G_var() do { \
        unsigned i; \
-       XZALLOC_CONST_PTR(&ash_ptr_to_globals_var, sizeof(G_var)); \
+       ASSIGN_CONST_PTR(&ash_ptr_to_globals_var, xzalloc(sizeof(G_var))); \
        for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
                varinit[i].flags    = varinit_data[i].flags; \
                varinit[i].var_text = varinit_data[i].var_text; \
-- 
2.34.1

_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to