]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
test: make LD_PRELOAD tests work with ASAN
authorCaleb Sander <csander@purestorage.com>
Sat, 14 Oct 2023 01:13:56 +0000 (19:13 -0600)
committerDaniel Wagner <wagi@monom.org>
Thu, 2 Nov 2023 13:31:26 +0000 (14:31 +0100)
Several tests mock libc functions by setting LD_PRELOAD
to a shared library containing mock implementations of the functions.
Currently this prevents the tests from running with ASAN.
ASAN complains that libasan doesn't come first in LD_PRELOAD and aborts.
But in practice this doesn't seem to be an issue.
Sample ASAN issues added to libnvme, the test cases, and the mocks
are all correctly reported.
So suppress this warning by setting the environment variable
ASAN_OPTIONS=verify_asan_link_order=0.
In case the user does want to inject a specific libasan,
change the tests' use of LD_PRELOAD to append the mock shared library
rather than overwriting LD_PRELOAD entirely.

Signed-off-by: Caleb Sander <csander@purestorage.com>
test/ioctl/meson.build
test/meson.build

index edbe6b35a61c283674664dc0a2a169121966e645..b329d270b16e5949e080f4053290e918bd72572e 100644 (file)
@@ -3,6 +3,16 @@ mock_ioctl = library(
     ['mock.c', 'util.c'],
 )
 
+# Add mock-ioctl to the LD_PRELOAD path so it overrides libc.
+# Append to LD_PRELOAD so existing libraries, e.g. libasan, are kept.
+# If libasan isn't specified in the LD_PRELOAD path, ASAN warns about mock-ioctl
+# being loaded first because its memory allocations might not get intercepted.
+# But it appears this isn't a problem; ASAN errors in mock-ioctl are reported.
+# This is likely because the executable still links with libasan before libc.
+mock_ioctl_env = environment()
+mock_ioctl_env.append('LD_PRELOAD', mock_ioctl.full_path())
+mock_ioctl_env.set('ASAN_OPTIONS', 'verify_asan_link_order=0')
+
 discovery = executable(
     'test-discovery',
     'discovery.c',
@@ -11,7 +21,7 @@ discovery = executable(
     link_with: mock_ioctl,
 )
 
-test('discovery', discovery, env: ['LD_PRELOAD=' + mock_ioctl.full_path()])
+test('discovery', discovery, env: mock_ioctl_env)
 
 features = executable(
     'test-features',
@@ -20,7 +30,7 @@ features = executable(
     link_with: mock_ioctl,
 )
 
-test('features', features, env: ['LD_PRELOAD=' + mock_ioctl.full_path()])
+test('features', features, env: mock_ioctl_env)
 
 identify = executable(
     'test-identify',
@@ -29,4 +39,4 @@ identify = executable(
     link_with: mock_ioctl,
 )
 
-test('identify', identify, env: ['LD_PRELOAD=' + mock_ioctl.full_path()])
+test('identify', identify, env: mock_ioctl_env)
index 0c4587945805c6706aed2c29d5aa15b40cd6f6fa..71be57c08c73455ca4658169daee021b6e216564 100644 (file)
@@ -72,6 +72,11 @@ if conf.get('HAVE_NETDB')
         ['mock-ifaddrs.c', ],
     )
 
+    # See comment in test/ioctl/meson.build explaining how LD_PRELOAD is used
+    mock_ifaddrs_env = environment()
+    mock_ifaddrs_env.append('LD_PRELOAD', mock_ifaddrs.full_path())
+    mock_ifaddrs_env.set('ASAN_OPTIONS', 'verify_asan_link_order=0')
+
     tree = executable(
         'tree',
         ['tree.c'],
@@ -80,7 +85,7 @@ if conf.get('HAVE_NETDB')
         link_with: mock_ifaddrs,
     )
 
-    test('tree', tree, env: ['LD_PRELOAD=' + mock_ifaddrs.full_path()])
+    test('tree', tree, env: mock_ifaddrs_env)
 
     test_util = executable(
         'test-util',