summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYU Jincheng2021-10-10 02:19:51 +0800
committerDenys Vlasenko2021-10-09 22:30:45 +0200
commit5156b245536ce0f07165793f07c63fd9fa5dd3b7 (patch)
tree3b73b7ea8ed1830d9cc13cbce1da6918926553e2
parent04ad683bf99333c2a6c6fd6549faa67978ad9a98 (diff)
downloadbusybox-5156b245536ce0f07165793f07c63fd9fa5dd3b7.zip
busybox-5156b245536ce0f07165793f07c63fd9fa5dd3b7.tar.gz
Make const ptr assign as function call in clang
- This can act as memory barrier in clang to avoid read before assign of a const ptr Signed-off-by: LoveSy <shana@zju.edu.cn> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/test.c2
-rw-r--r--include/libbb.h21
-rw-r--r--libbb/Kbuild.src1
-rw-r--r--libbb/appletlib.c2
-rw-r--r--libbb/const_hack.c16
-rw-r--r--libbb/lineedit.c2
-rw-r--r--shell/ash.c6
7 files changed, 38 insertions, 12 deletions
diff --git a/coreutils/test.c b/coreutils/test.c
index fc95672..a914c74 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -446,7 +446,7 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics;
#define leaving (S.leaving )
#define INIT_S() do { \
- ASSIGN_CONST_PTR(test_ptr_to_statics, xzalloc(sizeof(S))); \
+ XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \
} while (0)
#define DEINIT_S() do { \
free(group_array); \
diff --git a/include/libbb.h b/include/libbb.h
index 296417d..a340f27 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -2280,6 +2280,7 @@ extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */
extern const int const_int_0;
//extern const int const_int_1;
+
/* This struct is deliberately not defined. */
/* See docs/keep_data_small.txt */
struct globals;
@@ -2304,23 +2305,31 @@ static ALWAYS_INLINE void* not_const_pp(const void *p)
);
return pp;
}
+# 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
+ * clang from reading pointer before it is assigned.
+ */
+void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC;
#else
-static ALWAYS_INLINE void* not_const_pp(const void *p) { return (void*)p; }
-#endif
-
-#define ASSIGN_CONST_PTR(p, v) do { \
- *(void**)not_const_pp(&p) = (void*)(v); \
+# 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))
+#endif
-#define SET_PTR_TO_GLOBALS(x) ASSIGN_CONST_PTR(ptr_to_globals, x)
+#define SET_PTR_TO_GLOBALS(x) ASSIGN_CONST_PTR(&ptr_to_globals, x)
#define FREE_PTR_TO_GLOBALS() do { \
if (ENABLE_FEATURE_CLEAN_UP) { \
free(ptr_to_globals); \
} \
} while (0)
+
/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
* use bb_default_login_shell and following defines.
* If you change LIBBB_DEFAULT_LOGIN_SHELL,
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index 6763008..2fa2398 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -24,6 +24,7 @@ lib-y += chomp.o
lib-y += compare_string_array.o
lib-y += concat_path_file.o
lib-y += concat_subpath_file.o
+lib-y += const_hack.o
lib-y += copy_file.o
lib-y += copyfd.o
lib-y += crc32.o
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index bf26c99..e8c3084 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -247,7 +247,7 @@ void lbb_prepare(const char *applet
IF_FEATURE_INDIVIDUAL(, char **argv))
{
#ifdef bb_cached_errno_ptr
- ASSIGN_CONST_PTR(bb_errno, get_perrno());
+ ASSIGN_CONST_PTR(&bb_errno, get_perrno());
#endif
applet_name = applet;
diff --git a/libbb/const_hack.c b/libbb/const_hack.c
new file mode 100644
index 0000000..9575e6d
--- /dev/null
+++ b/libbb/const_hack.c
@@ -0,0 +1,16 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Trick to assign a const ptr with barrier for clang
+ *
+ * Copyright (C) 2021 by YU Jincheng <shana@zju.edu.cn>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+#include "libbb.h"
+
+#if defined(__clang_major__) && __clang_major__ >= 9
+void FAST_FUNC XZALLOC_CONST_PTR(const void *pptr, size_t size)
+{
+ ASSIGN_CONST_PTR(pptr, xzalloc(size));
+}
+#endif
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 3c87abc..9960448 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -214,7 +214,7 @@ extern struct lineedit_statics *BB_GLOBAL_CONST lineedit_ptr_to_statics;
#define delbuf (S.delbuf )
#define INIT_S() do { \
- ASSIGN_CONST_PTR(lineedit_ptr_to_statics, xzalloc(sizeof(S))); \
+ XZALLOC_CONST_PTR(&lineedit_ptr_to_statics, sizeof(S)); \
} while (0)
static void deinit_S(void)
diff --git a/shell/ash.c b/shell/ash.c
index 1999751..2d3cc8a 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -504,7 +504,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 { \
- ASSIGN_CONST_PTR(ash_ptr_to_globals_misc, xzalloc(sizeof(G_misc))); \
+ XZALLOC_CONST_PTR(&ash_ptr_to_globals_misc, sizeof(G_misc)); \
savestatus = -1; \
curdir = nullstr; \
physdir = nullstr; \
@@ -1582,7 +1582,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 { \
- ASSIGN_CONST_PTR(ash_ptr_to_globals_memstack, xzalloc(sizeof(G_memstack))); \
+ XZALLOC_CONST_PTR(&ash_ptr_to_globals_memstack, sizeof(G_memstack)); \
g_stackp = &stackbase; \
g_stacknxt = stackbase.space; \
g_stacknleft = MINSIZE; \
@@ -2213,7 +2213,7 @@ extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var;
#endif
#define INIT_G_var() do { \
unsigned i; \
- ASSIGN_CONST_PTR(ash_ptr_to_globals_var, xzalloc(sizeof(G_var))); \
+ XZALLOC_CONST_PTR(&ash_ptr_to_globals_var, 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; \