@@ -305,6 +305,64 @@ struct hwloc_component {
305305 void * data ;
306306};
307307
308+ /** \brief Make sure that plugins can lookup core symbols.
309+ *
310+ * This is a sanity check to avoid lazy-lookup failures when libhwloc
311+ * is loaded within a plugin, and later tries to load its own plugins.
312+ * This may fail (and abort the program) if libhwloc symbols are in a
313+ * private namespace.
314+ *
315+ * \return 0 on success.
316+ * \return -1 if the plugin cannot be successfully loaded. The caller
317+ * plugin init() callback should return a negative error code as well.
318+ *
319+ * Plugins should call this function in their init() callback to avoid
320+ * later crashes if lazy symbol resolution is used by the upper layer that
321+ * loaded hwloc (e.g. OpenCL implementations using dlopen with RTLD_LAZY).
322+ *
323+ * \note The build system must define HWLOC_INSIDE_PLUGIN if and only if
324+ * building the caller as a plugin.
325+ *
326+ * \note This function should remain inline so plugins can call it even
327+ * when they cannot find libhwloc symbols.
328+ */
329+ static __hwloc_inline int
330+ hwloc_plugin_check_namespace (const char * pluginname __hwloc_attribute_unused , const char * symbol __hwloc_attribute_unused )
331+ {
332+ #ifdef HWLOC_INSIDE_PLUGIN
333+ void * sym ;
334+ #ifdef HWLOC_HAVE_LTDL
335+ lt_dlhandle handle = lt_dlopen (NULL );
336+ #else
337+ void * handle = dlopen (NULL , RTLD_NOW |RTLD_LOCAL );
338+ #endif
339+ if (!handle )
340+ /* cannot check, assume things will work */
341+ return 0 ;
342+ #ifdef HWLOC_HAVE_LTDL
343+ sym = lt_dlsym (handle , symbol );
344+ lt_dlclose (handle );
345+ #else
346+ sym = dlsym (handle , symbol );
347+ dlclose (handle );
348+ #endif
349+ if (!sym ) {
350+ static int verboseenv_checked = 0 ;
351+ static int verboseenv_value = 0 ;
352+ if (!verboseenv_checked ) {
353+ const char * verboseenv = getenv ("HWLOC_PLUGINS_VERBOSE" );
354+ verboseenv_value = verboseenv ? atoi (verboseenv ) : 0 ;
355+ verboseenv_checked = 1 ;
356+ }
357+ if (verboseenv_value )
358+ fprintf (stderr , "Plugin `%s' disabling itself because it cannot find the `%s' core symbol.\n" ,
359+ pluginname , symbol );
360+ return -1 ;
361+ }
362+ #endif /* HWLOC_INSIDE_PLUGIN */
363+ return 0 ;
364+ }
365+
308366/** @} */
309367
310368
@@ -414,64 +472,6 @@ HWLOC_DECLSPEC int hwloc_obj_add_children_sets(hwloc_obj_t obj);
414472 */
415473HWLOC_DECLSPEC int hwloc_topology_reconnect (hwloc_topology_t topology , unsigned long flags __hwloc_attribute_unused );
416474
417- /** \brief Make sure that plugins can lookup core symbols.
418- *
419- * This is a sanity check to avoid lazy-lookup failures when libhwloc
420- * is loaded within a plugin, and later tries to load its own plugins.
421- * This may fail (and abort the program) if libhwloc symbols are in a
422- * private namespace.
423- *
424- * \return 0 on success.
425- * \return -1 if the plugin cannot be successfully loaded. The caller
426- * plugin init() callback should return a negative error code as well.
427- *
428- * Plugins should call this function in their init() callback to avoid
429- * later crashes if lazy symbol resolution is used by the upper layer that
430- * loaded hwloc (e.g. OpenCL implementations using dlopen with RTLD_LAZY).
431- *
432- * \note The build system must define HWLOC_INSIDE_PLUGIN if and only if
433- * building the caller as a plugin.
434- *
435- * \note This function should remain inline so plugins can call it even
436- * when they cannot find libhwloc symbols.
437- */
438- static __hwloc_inline int
439- hwloc_plugin_check_namespace (const char * pluginname __hwloc_attribute_unused , const char * symbol __hwloc_attribute_unused )
440- {
441- #ifdef HWLOC_INSIDE_PLUGIN
442- void * sym ;
443- #ifdef HWLOC_HAVE_LTDL
444- lt_dlhandle handle = lt_dlopen (NULL );
445- #else
446- void * handle = dlopen (NULL , RTLD_NOW |RTLD_LOCAL );
447- #endif
448- if (!handle )
449- /* cannot check, assume things will work */
450- return 0 ;
451- #ifdef HWLOC_HAVE_LTDL
452- sym = lt_dlsym (handle , symbol );
453- lt_dlclose (handle );
454- #else
455- sym = dlsym (handle , symbol );
456- dlclose (handle );
457- #endif
458- if (!sym ) {
459- static int verboseenv_checked = 0 ;
460- static int verboseenv_value = 0 ;
461- if (!verboseenv_checked ) {
462- const char * verboseenv = getenv ("HWLOC_PLUGINS_VERBOSE" );
463- verboseenv_value = verboseenv ? atoi (verboseenv ) : 0 ;
464- verboseenv_checked = 1 ;
465- }
466- if (verboseenv_value )
467- fprintf (stderr , "Plugin `%s' disabling itself because it cannot find the `%s' core symbol.\n" ,
468- pluginname , symbol );
469- return -1 ;
470- }
471- #endif /* HWLOC_INSIDE_PLUGIN */
472- return 0 ;
473- }
474-
475475/** @} */
476476
477477
0 commit comments