Merge pull request #650 from 64/global-config

options/{internal,rtdl}: add GlobalConfig and RtdlConfig
This commit is contained in:
Geert Custers 2022-07-18 14:26:37 +02:00 committed by GitHub
commit 44c38c8d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 86 additions and 22 deletions

View file

@ -214,6 +214,7 @@ internal_sources = [
'options/internal/generic/ensure.cpp', 'options/internal/generic/ensure.cpp',
'options/internal/generic/essential.cpp', 'options/internal/generic/essential.cpp',
'options/internal/generic/frigg.cpp', 'options/internal/generic/frigg.cpp',
'options/internal/generic/global-config.cpp',
'options/internal/generic/inline-emitter.cpp', 'options/internal/generic/inline-emitter.cpp',
'options/internal/generic/locale.cpp', 'options/internal/generic/locale.cpp',
'options/internal/generic/sigset.cpp', 'options/internal/generic/sigset.cpp',

View file

@ -20,6 +20,7 @@
#include <mlibc/ansi-sysdeps.hpp> #include <mlibc/ansi-sysdeps.hpp>
#include <mlibc/strtofp.hpp> #include <mlibc/strtofp.hpp>
#include <mlibc/strtol.hpp> #include <mlibc/strtol.hpp>
#include <mlibc/global-config.hpp>
#ifdef __MLIBC_POSIX_OPTION #ifdef __MLIBC_POSIX_OPTION
#include <pthread.h> #include <pthread.h>
@ -437,19 +438,9 @@ size_t wcstombs(char *mb_string, const wchar_t *wc_string, size_t max_size) {
return wcsrtombs(mb_string, &wc_string, max_size, 0); return wcsrtombs(mb_string, &wc_string, max_size, 0);
} }
static bool debugMalloc() {
static bool debug = [] () -> bool {
// TODO: We should use a locking variant of getenv here to guard
// against concurrent modifications of the environment.
auto value = getenv("MLIBC_DEBUG_MALLOC");
return value && !strcmp(value, "1");
}(); // Immediately invoked.
return debug;
}
void free(void *ptr) { void free(void *ptr) {
// TODO: Print PID only if POSIX option is enabled. // TODO: Print PID only if POSIX option is enabled.
if(debugMalloc()) { if (mlibc::globalConfig().debugMalloc) {
mlibc::infoLogger() << "mlibc (PID ?): free() on " mlibc::infoLogger() << "mlibc (PID ?): free() on "
<< ptr << frg::endlog; << ptr << frg::endlog;
if((uintptr_t)ptr & 1) if((uintptr_t)ptr & 1)
@ -461,7 +452,7 @@ void free(void *ptr) {
void *malloc(size_t size) { void *malloc(size_t size) {
auto nptr = getAllocator().allocate(size); auto nptr = getAllocator().allocate(size);
// TODO: Print PID only if POSIX option is enabled. // TODO: Print PID only if POSIX option is enabled.
if(debugMalloc()) if (mlibc::globalConfig().debugMalloc)
mlibc::infoLogger() << "mlibc (PID ?): malloc() returns " mlibc::infoLogger() << "mlibc (PID ?): malloc() returns "
<< nptr << frg::endlog; << nptr << frg::endlog;
return nptr; return nptr;
@ -470,7 +461,7 @@ void *malloc(size_t size) {
void *realloc(void *ptr, size_t size) { void *realloc(void *ptr, size_t size) {
auto nptr = getAllocator().reallocate(ptr, size); auto nptr = getAllocator().reallocate(ptr, size);
// TODO: Print PID only if POSIX option is enabled. // TODO: Print PID only if POSIX option is enabled.
if(debugMalloc()) if (mlibc::globalConfig().debugMalloc)
mlibc::infoLogger() << "mlibc (PID ?): realloc() on " mlibc::infoLogger() << "mlibc (PID ?): realloc() on "
<< ptr << " returns " << nptr << frg::endlog; << ptr << " returns " << nptr << frg::endlog;
return nptr; return nptr;

View file

@ -0,0 +1,27 @@
#include <stdlib.h>
#include <string.h>
#include <mlibc/global-config.hpp>
namespace mlibc {
struct GlobalConfigGuard {
GlobalConfigGuard();
};
GlobalConfigGuard guard;
GlobalConfigGuard::GlobalConfigGuard() {
// Force the config to be created during initialization of libc.so.
mlibc::globalConfig();
}
static bool envEnabled(const char *env) {
auto value = getenv(env);
return value && *value && *value != '0';
}
GlobalConfig::GlobalConfig() {
debugMalloc = envEnabled("MLIBC_DEBUG_MALLOC");
}
}

View file

@ -0,0 +1,19 @@
#ifndef MLIBC_GLOBAL_CONFIG
#define MLIBC_GLOBAL_CONFIG
namespace mlibc {
struct GlobalConfig {
GlobalConfig();
bool debugMalloc;
};
inline const GlobalConfig &globalConfig() {
static GlobalConfig cached;
return cached;
}
}
#endif // MLIBC_GLOBAL_CONFIG

View file

@ -16,6 +16,7 @@
#include <mlibc/allocator.hpp> #include <mlibc/allocator.hpp>
#include <mlibc/debug.hpp> #include <mlibc/debug.hpp>
#include <mlibc/posix-sysdeps.hpp> #include <mlibc/posix-sysdeps.hpp>
#include <mlibc/rtdl-config.hpp>
namespace { namespace {
constexpr bool debugPathResolution = false; constexpr bool debugPathResolution = false;
@ -466,10 +467,8 @@ int getloadavg(double *, int) {
__builtin_unreachable(); __builtin_unreachable();
} }
extern "C" int __dlapi_secure_required(void);
char *secure_getenv(const char *name) { char *secure_getenv(const char *name) {
if (__dlapi_secure_required()) if (mlibc::rtdlConfig().secureRequired)
return NULL; return NULL;
else else
return getenv(name); return getenv(name);

View file

@ -7,6 +7,7 @@
#include <abi-bits/auxv.h> #include <abi-bits/auxv.h>
#include <mlibc/debug.hpp> #include <mlibc/debug.hpp>
#include <mlibc/rtdl-sysdeps.hpp> #include <mlibc/rtdl-sysdeps.hpp>
#include <mlibc/rtdl-config.hpp>
#include <mlibc/rtdl-abi.hpp> #include <mlibc/rtdl-abi.hpp>
#include <mlibc/stack_protector.hpp> #include <mlibc/stack_protector.hpp>
#include <internal-config.h> #include <internal-config.h>
@ -28,7 +29,8 @@ extern HIDDEN void *_GLOBAL_OFFSET_TABLE_[];
extern HIDDEN Elf64_Dyn _DYNAMIC[]; extern HIDDEN Elf64_Dyn _DYNAMIC[];
#endif #endif
bool secureRequired; mlibc::RtdlConfig rtdlConfig;
uintptr_t *entryStack; uintptr_t *entryStack;
frg::manual_box<ObjectRepository> initialRepository; frg::manual_box<ObjectRepository> initialRepository;
frg::manual_box<Scope> globalScope; frg::manual_box<Scope> globalScope;
@ -221,7 +223,7 @@ extern "C" void *interpreterMain(uintptr_t *entry_stack) {
case AT_ENTRY: entry_pointer = reinterpret_cast<void *>(*value); break; case AT_ENTRY: entry_pointer = reinterpret_cast<void *>(*value); break;
case AT_EXECFN: execfn = reinterpret_cast<const char *>(*value); break; case AT_EXECFN: execfn = reinterpret_cast<const char *>(*value); break;
case AT_RANDOM: stack_entropy = reinterpret_cast<void*>(*value); break; case AT_RANDOM: stack_entropy = reinterpret_cast<void*>(*value); break;
case AT_SECURE: secureRequired = reinterpret_cast<uintptr_t>(*value); break; case AT_SECURE: rtdlConfig.secureRequired = reinterpret_cast<uintptr_t>(*value); break;
} }
aux += 2; aux += 2;
@ -307,10 +309,6 @@ extern "C" [[ gnu::visibility("default") ]] uintptr_t *__dlapi_entrystack() {
return entryStack; return entryStack;
} }
extern "C" [[ gnu::visibility("default") ]] int __dlapi_secure_required() {
return secureRequired;
}
extern "C" [[ gnu::visibility("default") ]] extern "C" [[ gnu::visibility("default") ]]
const char *__dlapi_error() { const char *__dlapi_error() {
auto error = lastError; auto error = lastError;
@ -323,6 +321,11 @@ void *__dlapi_get_tls(struct __abi_tls_entry *entry) {
return reinterpret_cast<char *>(accessDtv(entry->object)) + entry->offset; return reinterpret_cast<char *>(accessDtv(entry->object)) + entry->offset;
} }
extern "C" [[ gnu::visibility("default") ]]
const mlibc::RtdlConfig &__dlapi_get_config() {
return rtdlConfig;
}
#ifdef __MLIBC_POSIX_OPTION #ifdef __MLIBC_POSIX_OPTION
extern "C" [[ gnu::visibility("default") ]] extern "C" [[ gnu::visibility("default") ]]

View file

@ -0,0 +1,24 @@
#ifndef MLIBC_RTDL_CONFIG
#define MLIBC_RTDL_CONFIG
namespace mlibc {
struct RtdlConfig {
bool secureRequired;
};
}
extern "C" const mlibc::RtdlConfig &__dlapi_get_config();
#ifndef MLIBC_BUILDING_RTDL
namespace mlibc {
inline const RtdlConfig &rtdlConfig() {
return __dlapi_get_config();
}
}
#endif
#endif // MLIBC_RTDL_CONFIG