shared: cleanup dlopening libjansson depending on configure options

- assert that WITH_JANSSON and JANSSON_SONAME are defined consistently.
  This check ensures that we can check at compile time that nm_json_vt()
  will always fail (because JANSSON_SONAME) is undefined.
  That is interesting, because this means you can do a compile time
  for !WITH_JANSSON, and know if nm_json_vt() will *never* succeed.
  With that, we could let the linker know when the code is unused
  and remove all uses of nm_json_vt(), without using the traditional
  conditional compilation with "#if WITH_JANSSON". But of course, we
  currently don't do this micro optimization to remove defunct code.

- drop the "mode" helper variable and pass the flags directly to
  dlopen().
This commit is contained in:
Thomas Haller 2020-07-03 20:13:41 +02:00
parent 18692faa9f
commit ca7bb15591
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 38 additions and 12 deletions

View file

@ -970,6 +970,24 @@ test_dcb_bandwidth_sums (void)
/*****************************************************************************/
static void
test_nm_json (void)
{
g_assert (NM_IN_SET (WITH_JANSSON, 0, 1));
#if WITH_JANSSON
g_assert (nm_json_vt ());
#else
g_assert (!nm_json_vt ());
#endif
#if WITH_JANSSON != defined (JANSSON_SONAME)
#error "WITH_JANSON and JANSSON_SONAME are defined inconsistently."
#endif
}
/*****************************************************************************/
static void
_test_team_config_sync (const char *team_config,
int notify_peer_count,
@ -4087,6 +4105,7 @@ main (int argc, char **argv)
g_test_add_func ("/libnm/settings/bridge/vlans", test_bridge_vlans);
g_test_add_func ("/libnm/settings/bridge/verify", test_bridge_verify);
g_test_add_func ("/libnm/test_nm_json", test_nm_json);
g_test_add_func ("/libnm/settings/team/sync_runner_from_config_roundrobin",
test_runner_roundrobin_sync_from_config);
g_test_add_func ("/libnm/settings/team/sync_runner_from_config_broadcast",

View file

@ -147,24 +147,31 @@ static NMJsonVtInternal *
_nm_json_vt_internal_load (void)
{
NMJsonVtInternal *v;
void *handle = NULL;
int mode;
const char *soname;
void *handle;
v = g_new0 (NMJsonVtInternal, 1);
#ifndef JANSSON_SONAME
#define JANSSON_SONAME ""
#if WITH_JANSSON && defined (JANSSON_SONAME)
G_STATIC_ASSERT_EXPR (NM_STRLEN (JANSSON_SONAME) > 0);
nm_assert (strlen (JANSSON_SONAME) > 0);
soname = JANSSON_SONAME;
#elif !WITH_JANSSON && !defined (JANSSON_SONAME)
soname = NULL;
#else
#error "WITH_JANSON and JANSSON_SONAME are defined inconsistently."
#endif
mode = RTLD_LAZY | RTLD_LOCAL | RTLD_NODELETE | RTLD_DEEPBIND;
#if defined (ASAN_BUILD)
/* Address sanitizer is incompatible with RTLD_DEEPBIND. */
mode &= ~RTLD_DEEPBIND;
if (!soname)
return v;
handle = dlopen (soname, RTLD_LAZY
| RTLD_LOCAL
| RTLD_NODELETE
#if !defined (ASAN_BUILD)
| RTLD_DEEPBIND
#endif
if (strlen (JANSSON_SONAME) > 0)
handle = dlopen (JANSSON_SONAME, mode);
| 0);
if (!handle)
return v;