util: Fixes memory leak in __getProgramName
This is happened when multi-threading access to util_get_process_name memory leak point: Direct leak of 4097 byte(s) in 1 object(s) allocated from: #0 0x7f42888c0e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 #1 0x7f4288859d18 in __interceptor_realpath ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:3608 #2 0x55a9c272e03d in __getProgramName ../src/util/u_process.c:75 #3 0x55a9c272e03d in util_get_process_name ../src/util/u_process.c:197 #4 0x55a9c2746da7 in util_queue_init ../src/util/u_queue.c:416 #5 0x55a9c272c233 in queue_init ../src/util/perf/u_trace.c:403 #6 0x55a9c272c233 in u_trace_context_init ../src/util/perf/u_trace.c:453 #7 0x55a9c262eb54 in test_thread ../src/util/tests/perf/u_trace_test.cpp:14 #8 0x55a9c275228b in impl_thrd_routine ../src/c11/impl/threads_posix.c:67 Signed-off-by: Yonggang Luo <luoyonggang@gmail.com> Acked-by: Marek Olšák <marek.olsak@amd.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18764>
This commit is contained in:
@@ -32,8 +32,6 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#undef GET_PROGRAM_NAME
|
|
||||||
|
|
||||||
#if DETECT_OS_WINDOWS
|
#if DETECT_OS_WINDOWS
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
@@ -49,21 +47,17 @@
|
|||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "util/u_call_once.h"
|
||||||
|
|
||||||
|
#undef GET_PROGRAM_NAME_NOT_AVAILABLE
|
||||||
|
|
||||||
#if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
|
#if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
|
||||||
|
static char *
|
||||||
static char *path = NULL;
|
|
||||||
|
|
||||||
static void __freeProgramPath()
|
|
||||||
{
|
|
||||||
free(path);
|
|
||||||
path = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *
|
|
||||||
__getProgramName()
|
__getProgramName()
|
||||||
{
|
{
|
||||||
char * arg = strrchr(program_invocation_name, '/');
|
char * arg = strrchr(program_invocation_name, '/');
|
||||||
if (arg) {
|
if (arg) {
|
||||||
|
char *program_name = NULL;
|
||||||
/* If the / character was found this is likely a linux path or
|
/* If the / character was found this is likely a linux path or
|
||||||
* an invocation path for a 64-bit wine program.
|
* an invocation path for a 64-bit wine program.
|
||||||
*
|
*
|
||||||
@@ -71,20 +65,22 @@ __getProgramName()
|
|||||||
* Strip these arguments out by using the realpath only if it was
|
* Strip these arguments out by using the realpath only if it was
|
||||||
* a prefix of the invocation name.
|
* a prefix of the invocation name.
|
||||||
*/
|
*/
|
||||||
if (!path) {
|
char *path = realpath("/proc/self/exe", NULL);
|
||||||
path = realpath("/proc/self/exe", NULL);
|
|
||||||
atexit(__freeProgramPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path && strncmp(path, program_invocation_name, strlen(path)) == 0) {
|
if (path && strncmp(path, program_invocation_name, strlen(path)) == 0) {
|
||||||
/* This shouldn't be null because path is a a prefix,
|
/* This shouldn't be null because path is a a prefix,
|
||||||
* but check it anyway since path is static. */
|
* but check it anyway since path is static. */
|
||||||
char * name = strrchr(path, '/');
|
char * name = strrchr(path, '/');
|
||||||
if (name)
|
if (name)
|
||||||
return name + 1;
|
program_name = strdup(name + 1);
|
||||||
}
|
}
|
||||||
|
if (path) {
|
||||||
return arg+1;
|
free(path);
|
||||||
|
}
|
||||||
|
if (!program_name) {
|
||||||
|
program_name = strdup(arg+1);
|
||||||
|
}
|
||||||
|
return program_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If there was no '/' at all we likely have a windows like path from
|
/* If there was no '/' at all we likely have a windows like path from
|
||||||
@@ -92,51 +88,59 @@ __getProgramName()
|
|||||||
*/
|
*/
|
||||||
arg = strrchr(program_invocation_name, '\\');
|
arg = strrchr(program_invocation_name, '\\');
|
||||||
if (arg)
|
if (arg)
|
||||||
return arg+1;
|
return strdup(arg+1);
|
||||||
|
|
||||||
return program_invocation_name;
|
return strdup(program_invocation_name);
|
||||||
}
|
}
|
||||||
# define GET_PROGRAM_NAME() __getProgramName()
|
|
||||||
#elif defined(HAVE_PROGRAM_INVOCATION_NAME)
|
#elif defined(HAVE_PROGRAM_INVOCATION_NAME)
|
||||||
# define GET_PROGRAM_NAME() program_invocation_short_name
|
static char *
|
||||||
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(ANDROID)
|
__getProgramName()
|
||||||
# define GET_PROGRAM_NAME() getprogname()
|
{
|
||||||
#elif defined(__NetBSD__)
|
return strdup(program_invocation_short_name);
|
||||||
|
}
|
||||||
|
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(ANDROID) || defined(__NetBSD__)
|
||||||
|
#if defined(__NetBSD__)
|
||||||
# include <sys/param.h>
|
# include <sys/param.h>
|
||||||
# if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
|
|
||||||
# define GET_PROGRAM_NAME() getprogname()
|
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 106000100)
|
||||||
|
#define GET_PROGRAM_NAME_NOT_AVAILABLE
|
||||||
|
#else /* !(defined(__NetBSD_Version__) && (__NetBSD_Version__ < 106000100)) */
|
||||||
|
static char *
|
||||||
|
__getProgramName()
|
||||||
|
{
|
||||||
|
const char *program_name = getprogname();
|
||||||
|
if (program_name) {
|
||||||
|
return strdup(program_name);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
#endif /* defined(__NetBSD_Version__) && (__NetBSD_Version__ < 106000100) */
|
||||||
#elif defined(__sun)
|
#elif defined(__sun)
|
||||||
/* Solaris has getexecname() which returns the full path - return just
|
/* Solaris has getexecname() which returns the full path - return just
|
||||||
the basename to match BSD getprogname() */
|
the basename to match BSD getprogname() */
|
||||||
# include <libgen.h>
|
# include <libgen.h>
|
||||||
|
|
||||||
static const char *
|
static char *
|
||||||
__getProgramName()
|
__getProgramName()
|
||||||
{
|
{
|
||||||
static const char *progname;
|
char *progname = NULL;
|
||||||
|
|
||||||
if (progname == NULL) {
|
|
||||||
const char *e = getexecname();
|
const char *e = getexecname();
|
||||||
if (e != NULL) {
|
if (e != NULL) {
|
||||||
/* Have to make a copy since getexecname can return a readonly
|
/* Have to make a copy since getexecname can return a readonly
|
||||||
string, but basename expects to be able to modify its arg. */
|
string, but basename expects to be able to modify its arg. */
|
||||||
char *n = strdup(e);
|
char *n = strdup(e);
|
||||||
if (n != NULL) {
|
if (n != NULL) {
|
||||||
progname = basename(n);
|
progname = strdup(basename(n));
|
||||||
}
|
free(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return progname;
|
return progname;
|
||||||
}
|
}
|
||||||
|
|
||||||
# define GET_PROGRAM_NAME() __getProgramName()
|
|
||||||
#elif DETECT_OS_WINDOWS
|
#elif DETECT_OS_WINDOWS
|
||||||
static const char *
|
static char *
|
||||||
__getProgramName()
|
__getProgramName()
|
||||||
{
|
{
|
||||||
static const char *progname;
|
char *progname;
|
||||||
if (progname == NULL) {
|
|
||||||
static char buf[MAX_PATH];
|
static char buf[MAX_PATH];
|
||||||
GetModuleFileNameA(NULL, buf, sizeof(buf));
|
GetModuleFileNameA(NULL, buf, sizeof(buf));
|
||||||
progname = strrchr(buf, '\\');
|
progname = strrchr(buf, '\\');
|
||||||
@@ -144,57 +148,77 @@ __getProgramName()
|
|||||||
progname++;
|
progname++;
|
||||||
else
|
else
|
||||||
progname = buf;
|
progname = buf;
|
||||||
|
return strdup(progname);
|
||||||
}
|
}
|
||||||
return progname;
|
|
||||||
}
|
|
||||||
# define GET_PROGRAM_NAME() __getProgramName()
|
|
||||||
#elif defined(__HAIKU__)
|
#elif defined(__HAIKU__)
|
||||||
# include <libgen.h>
|
# include <libgen.h>
|
||||||
extern char **__libc_argv;
|
extern char **__libc_argv;
|
||||||
extern int __libc_argc;
|
extern int __libc_argc;
|
||||||
|
|
||||||
static const char *
|
static char *
|
||||||
__getProgramName()
|
__getProgramName()
|
||||||
{
|
{
|
||||||
static const char *progname;
|
char *progname = NULL;
|
||||||
|
|
||||||
if (progname == NULL) {
|
|
||||||
char *n = strdup(__libc_argv[0]);
|
char *n = strdup(__libc_argv[0]);
|
||||||
if (n != NULL) {
|
if (n != NULL) {
|
||||||
progname = basename(n);
|
progname = strdup(basename(n));
|
||||||
}
|
free(n);
|
||||||
}
|
}
|
||||||
return progname;
|
return progname;
|
||||||
}
|
}
|
||||||
# define GET_PROGRAM_NAME() __getProgramName()
|
#else
|
||||||
|
#define GET_PROGRAM_NAME_NOT_AVAILABLE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(GET_PROGRAM_NAME)
|
#if defined(GET_PROGRAM_NAME_NOT_AVAILABLE)
|
||||||
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__UCLIBC__) || defined(ANDROID)
|
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__UCLIBC__) || defined(ANDROID)
|
||||||
/* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
|
/* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
|
||||||
* Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
|
* Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
|
||||||
* used as a last resort, if there is no documented facility available. */
|
* used as a last resort, if there is no documented facility available. */
|
||||||
static const char *
|
static char *
|
||||||
__getProgramName()
|
__getProgramName()
|
||||||
{
|
{
|
||||||
extern const char *__progname;
|
extern const char *__progname;
|
||||||
char * arg = strrchr(__progname, '/');
|
char * arg = strrchr(__progname, '/');
|
||||||
if (arg)
|
if (arg)
|
||||||
return arg+1;
|
return strdup(arg+1);
|
||||||
else
|
else
|
||||||
return __progname;
|
return strdup(__progname);
|
||||||
}
|
}
|
||||||
# define GET_PROGRAM_NAME() __getProgramName()
|
|
||||||
#else
|
#else
|
||||||
# define GET_PROGRAM_NAME() ""
|
|
||||||
#pragma message ( "Warning: Per application configuration won't work with your OS version." )
|
#pragma message ( "Warning: Per application configuration won't work with your OS version." )
|
||||||
|
static char *
|
||||||
|
__getProgramName()
|
||||||
|
{
|
||||||
|
return strdup("");
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif /* GET_PROGRAM_NAME_NOT_AVAILABLE */
|
||||||
|
|
||||||
|
static char *program_name;
|
||||||
|
|
||||||
|
static void
|
||||||
|
free_program_name(void)
|
||||||
|
{
|
||||||
|
free(program_name);
|
||||||
|
program_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
util_get_process_name_callback(void)
|
||||||
|
{
|
||||||
|
program_name = __getProgramName();
|
||||||
|
if (program_name) {
|
||||||
|
atexit(free_program_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
util_get_process_name(void)
|
util_get_process_name(void)
|
||||||
{
|
{
|
||||||
return GET_PROGRAM_NAME();
|
static util_once_flag once_state = UTIL_ONCE_FLAG_INIT;
|
||||||
|
util_call_once(&once_state, util_get_process_name_callback);
|
||||||
|
return program_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
|
Reference in New Issue
Block a user