Skip to content

Commit 2832af0

Browse files
committed
test: Add unit test verifying c application can dlopen library
This test checks whether plugin library can be loaded with dlopen from c application . The reason is that plugin formerly didn't link against libstdc++ and relied on application to provide required linkage. This has been fixed by recent commit but test was not added. Signed-off-by: Michael Axtmann <[email protected]>
1 parent da12027 commit 2832af0

File tree

5 files changed

+117
-37
lines changed

5 files changed

+117
-37
lines changed

tests/unit/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ region_based_tuner
88
scheduler
99
histogram
1010
histogram_binner
11+
dlopen_c_test

tests/unit/Makefile.am

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ AM_CPPFLAGS += -isystem $(abs_top_srcdir)/3rd-party/uthash/include
1313
LDADD = $(top_builddir)/src/libinternal_net_plugin.la
1414
noinst_HEADERS = test-common.h
1515

16+
# Define C compiler for dlopen_c_test
17+
dlopen_c_test_CFLAGS = $(AM_CFLAGS)
18+
dlopen_c_test_LDADD = -ldl
19+
1620
noinst_PROGRAMS = \
1721
freelist \
1822
msgbuff \
@@ -21,7 +25,8 @@ noinst_PROGRAMS = \
2125
ep_addr_list \
2226
mr \
2327
histogram_binner \
24-
histogram
28+
histogram \
29+
dlopen_c_test
2530

2631
if WANT_PLATFORM_AWS
2732
noinst_PROGRAMS += aws_platform_mapper
@@ -47,6 +52,7 @@ mr_SOURCES = mr.cpp
4752
aws_platform_mapper_SOURCES = aws_platform_mapper.cpp
4853
histogram_binner_SOURCES = histogram_binner.cpp
4954
histogram_SOURCES = histogram.cpp
55+
dlopen_c_test_SOURCES = dlopen_c_test.c
5056

5157
TESTS = $(noinst_PROGRAMS)
5258
endif

tests/unit/dlopen_c_test.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2025 Amazon.com, Inc. or its affiliates. All rights reserved.
3+
*/
4+
5+
#include "config.h"
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <assert.h>
10+
#include <stdint.h>
11+
#include <dlfcn.h>
12+
#include <string.h>
13+
14+
#include "test-logger.h"
15+
16+
nccl_ofi_logger_t ofi_log_function = NULL;
17+
18+
/*
19+
* This is a simple C test that loads the shared object dynamically
20+
* for the AWS OFI NCCL plugin
21+
*/
22+
23+
int main(int argc, char *argv[])
24+
{
25+
void *handle = NULL;
26+
char *error = NULL;
27+
int ret = 0;
28+
29+
/* Set up logging */
30+
ofi_log_function = logger;
31+
32+
/* Try to load the appropriate shared object based on build configuration */
33+
#ifdef HAVE_NEURON
34+
const char *lib_path = "../../src/.libs/libnccom-net.so";
35+
NCCL_OFI_INFO(NCCL_INIT, "Testing Neuron build: attempting to load %s\n", lib_path);
36+
#else
37+
const char *lib_path = "../../src/.libs/libnccl_net_ofi.so";
38+
NCCL_OFI_INFO(NCCL_INIT, "Testing standard build: attempting to load %s\n", lib_path);
39+
#endif
40+
41+
/* Open the shared object file */
42+
handle = dlopen(lib_path, RTLD_LAZY);
43+
if (!handle) {
44+
NCCL_OFI_WARN("Error opening shared object: %s\n", dlerror());
45+
return 1;
46+
}
47+
48+
/* Log test progress */
49+
NCCL_OFI_INFO(NCCL_INIT, "Successfully loaded AWS OFI NCCL plugin shared object");
50+
51+
/* Perform some basic assertions to verify test framework */
52+
assert(handle != NULL);
53+
54+
/* Close the shared object */
55+
dlclose(handle);
56+
57+
NCCL_OFI_INFO(NCCL_INIT, "Test completed successfully!\n");
58+
59+
return 0;
60+
}

tests/unit/test-common.h

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,6 @@
99
#include <stdio.h>
1010

1111
#include "nccl_ofi.h"
12-
#include "nccl_ofi_log.h"
13-
14-
static inline void logger(ncclDebugLogLevel level, unsigned long flags, const char *filefunc,
15-
int line, const char *fmt, ...)
16-
{
17-
va_list vargs;
18-
19-
switch (level) {
20-
case NCCL_LOG_WARN:
21-
printf("WARN: Function: %s Line: %d: ", filefunc, line);
22-
break;
23-
case NCCL_LOG_INFO:
24-
printf("INFO: Function: %s Line: %d: ", filefunc, line);
25-
break;
26-
case NCCL_LOG_TRACE:
27-
#if OFI_NCCL_TRACE
28-
printf("TRACE: Function: %s Line: %d: ", filefunc, line);
29-
break;
30-
#else
31-
return;
32-
#endif
33-
case NCCL_LOG_NONE:
34-
case NCCL_LOG_VERSION:
35-
case NCCL_LOG_ABORT:
36-
default:
37-
break;
38-
};
39-
40-
#pragma GCC diagnostic push
41-
#pragma GCC diagnostic ignored "-Wformat=2"
42-
va_start(vargs, fmt);
43-
vprintf(fmt, vargs);
44-
printf("\n");
45-
va_end(vargs);
46-
#pragma GCC diagnostic pop
47-
}
12+
#include "test-logger.h"
4813

4914
#endif // End TEST_COMMON_H_

tests/unit/test-logger.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2018-2023 Amazon.com, Inc. or its affiliates. All rights reserved.
3+
*/
4+
5+
#ifndef TEST_LOGGER_H_
6+
#define TEST_LOGGER_H_
7+
8+
#include <stdarg.h>
9+
#include <stdio.h>
10+
11+
#include "nccl_ofi_log.h"
12+
13+
static inline void logger(ncclDebugLogLevel level, unsigned long flags, const char *filefunc,
14+
int line, const char *fmt, ...)
15+
{
16+
va_list vargs;
17+
18+
switch (level) {
19+
case NCCL_LOG_WARN:
20+
printf("WARN: Function: %s Line: %d: ", filefunc, line);
21+
break;
22+
case NCCL_LOG_INFO:
23+
printf("INFO: Function: %s Line: %d: ", filefunc, line);
24+
break;
25+
case NCCL_LOG_TRACE:
26+
#if OFI_NCCL_TRACE
27+
printf("TRACE: Function: %s Line: %d: ", filefunc, line);
28+
break;
29+
#else
30+
return;
31+
#endif
32+
case NCCL_LOG_NONE:
33+
case NCCL_LOG_VERSION:
34+
case NCCL_LOG_ABORT:
35+
default:
36+
break;
37+
};
38+
39+
#pragma GCC diagnostic push
40+
#pragma GCC diagnostic ignored "-Wformat=2"
41+
va_start(vargs, fmt);
42+
vprintf(fmt, vargs);
43+
printf("\n");
44+
va_end(vargs);
45+
#pragma GCC diagnostic pop
46+
}
47+
#endif // End TEST_LOGGER_H_
48+

0 commit comments

Comments
 (0)