Skip to content

Commit ed6adcd

Browse files
Fuzzbawlsfurszy
authored andcommitted
Add backwards compatible implementation of explicit_bzero
This makes `explicit_bzero` compatible with our minimum required glibc version.
1 parent 43474d5 commit ed6adcd

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ libsapling_a_SOURCES = \
538538
sapling/crypter_sapling.cpp \
539539
sapling/saplingscriptpubkeyman.cpp
540540

541+
if GLIBC_BACK_COMPAT
542+
libsapling_a_SOURCES += compat/glibc_compat.cpp
543+
AM_LDFLAGS += $(COMPAT_LDFLAGS)
544+
endif
545+
541546
# cli: shared between pivx-cli and pivx-qt
542547
libbitcoin_cli_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
543548
libbitcoin_cli_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/compat/glibc_compat.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <cstddef>
1111
#include <cstdint>
12+
#include <cstring>
13+
#include <pthread.h>
14+
#include <stdlib.h>
1215

1316
#if defined(HAVE_SYS_SELECT_H)
1417
#include <sys/select.h>
@@ -75,3 +78,31 @@ extern "C" float __wrap_log2f(float x)
7578
{
7679
return log2f_old(x);
7780
}
81+
82+
/* glibc-internal users use __explicit_bzero_chk, and explicit_bzero
83+
redirects to that. */
84+
#undef explicit_bzero
85+
/* Set LEN bytes of S to 0. The compiler will not delete a call to
86+
this function, even if S is dead after the call. */
87+
void explicit_bzero (void *s, size_t len)
88+
{
89+
memset (s, '\0', len);
90+
/* Compiler barrier. */
91+
asm volatile ("" ::: "memory");
92+
}
93+
94+
// Redefine explicit_bzero_chk
95+
void __explicit_bzero_chk (void *dst, size_t len, size_t dstlen)
96+
{
97+
/* Inline __memset_chk to avoid a PLT reference to __memset_chk. */
98+
if (__glibc_unlikely (dstlen < len))
99+
__chk_fail ();
100+
memset (dst, '\0', len);
101+
/* Compiler barrier. */
102+
asm volatile ("" ::: "memory");
103+
}
104+
/* libc-internal references use the hidden
105+
__explicit_bzero_chk_internal symbol. This is necessary if
106+
__explicit_bzero_chk is implemented as an IFUNC because some
107+
targets do not support hidden references to IFUNC symbols. */
108+
#define strong_alias (__explicit_bzero_chk, __explicit_bzero_chk_internal)

0 commit comments

Comments
 (0)