Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f686ae8

Browse files
committed
Add workaround to make ubsan and ps_status.c compatible
At least on linux, set_ps_display() breaks /proc/$pid/environ. The sanitizer's helper library uses /proc/$pid/environ to implement getenv(), as it wants to work independent of libc. When just using undefined and alignment sanitizers, the sanitizer library is only initialized when the first error occurs, by which time we've often already called set_ps_display(), preventing the sanitizer libraries from seeing the options. We can work around that by defining __ubsan_default_options, a weak symbol libsanitizer uses to get defaults from the application, and return getenv("UBSAN_OPTIONS"). But only if main already was reached, so that we don't end up relying on a not-yet-working getenv(). As it's just a function that won't get called when not running a sanitizer, it doesn't seem necessary to make compilation of the function conditional. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/20220323173537.ll7klrglnp4gn2um@alap3.anarazel.de
1 parent aeaaf52 commit f686ae8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/backend/main/main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444

4545
const char *progname;
46+
static bool reached_main = false;
4647

4748

4849
static void startup_hacks(const char *progname);
@@ -59,6 +60,8 @@ main(int argc, char *argv[])
5960
{
6061
bool do_check_root = true;
6162

63+
reached_main = true;
64+
6265
/*
6366
* If supported on the current platform, set up a handler to be called if
6467
* the backend/postmaster crashes with a fatal signal or exception.
@@ -420,3 +423,30 @@ check_root(const char *progname)
420423
}
421424
#endif /* WIN32 */
422425
}
426+
427+
/*
428+
* At least on linux, set_ps_display() breaks /proc/$pid/environ. The
429+
* sanitizer library uses /proc/$pid/environ to implement getenv() as it wants
430+
* to work independent of libc. When just using undefined and alignment
431+
* sanitizers, the sanitizer library is only initialized when the first error
432+
* occurs, by which time we've often already called set_ps_display(),
433+
* preventing the sanitizer libraries from seeing the options.
434+
*
435+
* We can work around that by defining __ubsan_default_options, a weak symbol
436+
* libsanitizer uses to get defaults from the application, and return
437+
* getenv("UBSAN_OPTIONS"). But only if main already was reached, so that we
438+
* don't end up relying on a not-yet-working getenv().
439+
*
440+
* As this function won't get called when not running a sanitizer, it doesn't
441+
* seem necessary to only compile it conditionally.
442+
*/
443+
const char *__ubsan_default_options(void);
444+
const char *
445+
__ubsan_default_options(void)
446+
{
447+
/* don't call libc before it's guaranteed to be initialized */
448+
if (!reached_main)
449+
return "";
450+
451+
return getenv("UBSAN_OPTIONS");
452+
}

0 commit comments

Comments
 (0)