Skip to content

Commit 4b02f62

Browse files
authored
Use GCC visibility attributes on Unix (#1114)
* To allow compiling with -fvisibility=hidden flag, GCC visibility attributes (with value "default") should be set. * Enforce exporting unsafe_wait symbols on MacOS Signed-off-by: Vladislav Shchapov <[email protected]>
1 parent c881143 commit 4b02f62

File tree

13 files changed

+101
-67
lines changed

13 files changed

+101
-67
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ cmake_minimum_required(VERSION 3.5)
1616

1717
# Enable CMake policies
1818

19+
if (POLICY CMP0063)
20+
# The NEW behavior for this policy is to honor the visibility properties for all target types.
21+
cmake_policy(SET CMP0063 NEW)
22+
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
23+
endif()
24+
1925
if (POLICY CMP0068)
2026
# RPATH settings do not affect install_name on macOS since CMake 3.9
2127
cmake_policy(SET CMP0068 NEW)
@@ -84,6 +90,11 @@ endif()
8490

8591
set(CMAKE_CXX_EXTENSIONS OFF) # use -std=c++... instead of -std=gnu++...
8692
# ---------------------------------------------------------------------------------------------------------
93+
# Setup symbol visibility properties.
94+
95+
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)
96+
set(CMAKE_CXX_VISIBILITY_PRESET "hidden")
97+
# ---------------------------------------------------------------------------------------------------------
8798

8899
# Detect architecture (bitness).
89100
if (CMAKE_SIZEOF_VOID_P EQUAL 4)

include/oneapi/tbb/detail/_exception.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2005-2021 Intel Corporation
2+
Copyright (c) 2005-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -66,10 +66,16 @@ class TBB_EXPORT missing_wait : public std::exception {
6666
};
6767

6868
//! Exception for impossible finalization of task_sheduler_handle
69+
#if __APPLE__
70+
#pragma GCC visibility push(default)
71+
#endif
6972
class TBB_EXPORT unsafe_wait : public std::runtime_error {
7073
public:
7174
unsafe_wait(const char* msg) : std::runtime_error(msg) {}
7275
};
76+
#if __APPLE__
77+
#pragma GCC visibility pop
78+
#endif
7379

7480
//! Gathers all throw operators in one place.
7581
/** Its purpose is to minimize code bloat that can be caused by throw operators

include/oneapi/tbb/detail/_export.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2005-2021 Intel Corporation
2+
Copyright (c) 2005-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,27 +18,35 @@
1818
#define __TBB_detail__export_H
1919

2020
#if defined(__MINGW32__)
21-
#define _EXPORT __declspec(dllexport)
22-
#elif defined(_WIN32) || defined(__unix__) || defined(__APPLE__) // Use .def files for these
23-
#define _EXPORT
21+
#define __TBB_EXPORT __declspec(dllexport)
22+
#elif defined(_WIN32) // Use .def files for these
23+
#define __TBB_EXPORT
24+
#elif defined(__unix__) || defined(__APPLE__) // Use .def files for these
25+
#define __TBB_EXPORT __attribute__ ((visibility ("default")))
2426
#else
2527
#error "Unknown platform/compiler"
2628
#endif
2729

2830
#if __TBB_BUILD
29-
#define TBB_EXPORT _EXPORT
31+
#define TBB_EXPORT __TBB_EXPORT
3032
#else
3133
#define TBB_EXPORT
3234
#endif
3335

3436
#if __TBBMALLOC_BUILD
35-
#define TBBMALLOC_EXPORT _EXPORT
37+
#define TBBMALLOC_EXPORT __TBB_EXPORT
3638
#else
3739
#define TBBMALLOC_EXPORT
3840
#endif
3941

42+
#if __TBBMALLOCPROXY_BUILD
43+
#define TBBMALLOCPROXY_EXPORT __TBB_EXPORT
44+
#else
45+
#define TBBMALLOCPROXY_EXPORT
46+
#endif
47+
4048
#if __TBBBIND_BUILD
41-
#define TBBBIND_EXPORT _EXPORT
49+
#define TBBBIND_EXPORT __TBB_EXPORT
4250
#else
4351
#define TBBBIND_EXPORT
4452
#endif

src/tbb/queuing_rw_mutex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2005-2022 Intel Corporation
2+
Copyright (c) 2005-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -608,7 +608,7 @@ bool __TBB_EXPORTED_FUNC downgrade_to_reader(d1::queuing_rw_mutex::scoped_lock&
608608
return queuing_rw_mutex_impl::downgrade_to_reader(s);
609609
}
610610

611-
void __TBB_EXPORTED_FUNC construct(d1::queuing_rw_mutex& m) {
611+
TBB_EXPORT void __TBB_EXPORTED_FUNC construct(d1::queuing_rw_mutex& m) {
612612
queuing_rw_mutex_impl::construct(m);
613613
}
614614

src/tbbbind/tbb_bind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ TBBBIND_EXPORT int __TBB_internal_get_default_concurrency(int numa_id, int core_
528528
return system_topology::instance().get_default_concurrency(numa_id, core_type_id, max_threads_per_core);
529529
}
530530

531-
void __TBB_internal_destroy_system_topology() {
531+
TBBBIND_EXPORT void __TBB_internal_destroy_system_topology() {
532532
return system_topology::destroy();
533533
}
534534

src/tbbmalloc/frontend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,7 @@ extern "C" void scalable_free(void *object)
29512951
}
29522952

29532953
#if MALLOC_ZONE_OVERLOAD_ENABLED
2954-
extern "C" void __TBB_malloc_free_definite_size(void *object, size_t size)
2954+
extern "C" TBBMALLOC_EXPORT void __TBB_malloc_free_definite_size(void *object, size_t size)
29552955
{
29562956
internalPoolFree(defaultMemPool, object, size);
29572957
}

src/tbbmalloc_proxy/proxy.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static inline void initPageSize()
133133
2) check that dlsym("malloc") found something different from our replacement malloc
134134
*/
135135

136-
extern "C" void *__TBB_malloc_proxy(size_t) __TBB_ALIAS_ATTR_COPY(malloc);
136+
extern "C" TBBMALLOCPROXY_EXPORT void *__TBB_malloc_proxy(size_t) __TBB_ALIAS_ATTR_COPY(malloc);
137137

138138
static void *orig_msize;
139139

@@ -184,23 +184,23 @@ inline void InitOrigPointers() {}
184184

185185
#endif // MALLOC_UNIXLIKE_OVERLOAD_ENABLED and MALLOC_ZONE_OVERLOAD_ENABLED
186186

187-
void *PREFIX(malloc)(ZONE_ARG size_t size) __THROW
187+
TBBMALLOCPROXY_EXPORT void *PREFIX(malloc)(ZONE_ARG size_t size) __THROW
188188
{
189189
return scalable_malloc(size);
190190
}
191191

192-
void *PREFIX(calloc)(ZONE_ARG size_t num, size_t size) __THROW
192+
TBBMALLOCPROXY_EXPORT void *PREFIX(calloc)(ZONE_ARG size_t num, size_t size) __THROW
193193
{
194194
return scalable_calloc(num, size);
195195
}
196196

197-
void PREFIX(free)(ZONE_ARG void *object) __THROW
197+
TBBMALLOCPROXY_EXPORT void PREFIX(free)(ZONE_ARG void *object) __THROW
198198
{
199199
InitOrigPointers();
200200
__TBB_malloc_safer_free(object, (void (*)(void*))orig_free);
201201
}
202202

203-
void *PREFIX(realloc)(ZONE_ARG void* ptr, size_t sz) __THROW
203+
TBBMALLOCPROXY_EXPORT void *PREFIX(realloc)(ZONE_ARG void* ptr, size_t sz) __THROW
204204
{
205205
InitOrigPointers();
206206
return __TBB_malloc_safer_realloc(ptr, sz, orig_realloc);
@@ -209,13 +209,13 @@ void *PREFIX(realloc)(ZONE_ARG void* ptr, size_t sz) __THROW
209209
/* The older *NIX interface for aligned allocations;
210210
it's formally substituted by posix_memalign and deprecated,
211211
so we do not expect it to cause cyclic dependency with C RTL. */
212-
void *PREFIX(memalign)(ZONE_ARG size_t alignment, size_t size) __THROW
212+
TBBMALLOCPROXY_EXPORT void *PREFIX(memalign)(ZONE_ARG size_t alignment, size_t size) __THROW
213213
{
214214
return scalable_aligned_malloc(size, alignment);
215215
}
216216

217217
/* valloc allocates memory aligned on a page boundary */
218-
void *PREFIX(valloc)(ZONE_ARG size_t size) __THROW
218+
TBBMALLOCPROXY_EXPORT void *PREFIX(valloc)(ZONE_ARG size_t size) __THROW
219219
{
220220
if (! memoryPageSize) initPageSize();
221221

@@ -229,23 +229,23 @@ void *PREFIX(valloc)(ZONE_ARG size_t size) __THROW
229229

230230
// match prototype from system headers
231231
#if __ANDROID__
232-
size_t malloc_usable_size(const void *ptr) __THROW
232+
TBBMALLOCPROXY_EXPORT size_t malloc_usable_size(const void *ptr) __THROW
233233
#else
234-
size_t malloc_usable_size(void *ptr) __THROW
234+
TBBMALLOCPROXY_EXPORT size_t malloc_usable_size(void *ptr) __THROW
235235
#endif
236236
{
237237
InitOrigPointers();
238238
return __TBB_malloc_safer_msize(const_cast<void*>(ptr), (size_t (*)(void*))orig_msize);
239239
}
240240

241-
int posix_memalign(void **memptr, size_t alignment, size_t size) __THROW
241+
TBBMALLOCPROXY_EXPORT int posix_memalign(void **memptr, size_t alignment, size_t size) __THROW
242242
{
243243
return scalable_posix_memalign(memptr, alignment, size);
244244
}
245245

246246
/* pvalloc allocates smallest set of complete pages which can hold
247247
the requested number of bytes. Result is aligned on page boundary. */
248-
void *pvalloc(size_t size) __THROW
248+
TBBMALLOCPROXY_EXPORT void *pvalloc(size_t size) __THROW
249249
{
250250
if (! memoryPageSize) initPageSize();
251251
// align size up to the page size,
@@ -255,13 +255,13 @@ void *pvalloc(size_t size) __THROW
255255
return scalable_aligned_malloc(size, memoryPageSize);
256256
}
257257

258-
int mallopt(int /*param*/, int /*value*/) __THROW
258+
TBBMALLOCPROXY_EXPORT int mallopt(int /*param*/, int /*value*/) __THROW
259259
{
260260
return 1;
261261
}
262262

263263
#if defined(__GLIBC__) || defined(__ANDROID__)
264-
struct mallinfo mallinfo() __THROW
264+
TBBMALLOCPROXY_EXPORT struct mallinfo mallinfo() __THROW
265265
{
266266
struct mallinfo m;
267267
memset(&m, 0, sizeof(struct mallinfo));
@@ -274,30 +274,30 @@ struct mallinfo mallinfo() __THROW
274274
// Android doesn't have malloc_usable_size, provide it to be compatible
275275
// with Linux, in addition overload dlmalloc_usable_size() that presented
276276
// under Android.
277-
size_t dlmalloc_usable_size(const void *ptr) __TBB_ALIAS_ATTR_COPY(malloc_usable_size);
277+
TBBMALLOCPROXY_EXPORT size_t dlmalloc_usable_size(const void *ptr) __TBB_ALIAS_ATTR_COPY(malloc_usable_size);
278278
#else // __ANDROID__
279279
// TODO: consider using __typeof__ to guarantee the correct declaration types
280280
// C11 function, supported starting GLIBC 2.16
281-
void *aligned_alloc(size_t alignment, size_t size) __TBB_ALIAS_ATTR_COPY(memalign);
281+
TBBMALLOCPROXY_EXPORT void *aligned_alloc(size_t alignment, size_t size) __TBB_ALIAS_ATTR_COPY(memalign);
282282
// Those non-standard functions are exported by GLIBC, and might be used
283283
// in conjunction with standard malloc/free, so we must overload them.
284284
// Bionic doesn't have them. Not removing from the linker scripts,
285285
// as absent entry points are ignored by the linker.
286286

287-
void *__libc_malloc(size_t size) __TBB_ALIAS_ATTR_COPY(malloc);
288-
void *__libc_calloc(size_t num, size_t size) __TBB_ALIAS_ATTR_COPY(calloc);
289-
void *__libc_memalign(size_t alignment, size_t size) __TBB_ALIAS_ATTR_COPY(memalign);
290-
void *__libc_pvalloc(size_t size) __TBB_ALIAS_ATTR_COPY(pvalloc);
291-
void *__libc_valloc(size_t size) __TBB_ALIAS_ATTR_COPY(valloc);
287+
TBBMALLOCPROXY_EXPORT void *__libc_malloc(size_t size) __TBB_ALIAS_ATTR_COPY(malloc);
288+
TBBMALLOCPROXY_EXPORT void *__libc_calloc(size_t num, size_t size) __TBB_ALIAS_ATTR_COPY(calloc);
289+
TBBMALLOCPROXY_EXPORT void *__libc_memalign(size_t alignment, size_t size) __TBB_ALIAS_ATTR_COPY(memalign);
290+
TBBMALLOCPROXY_EXPORT void *__libc_pvalloc(size_t size) __TBB_ALIAS_ATTR_COPY(pvalloc);
291+
TBBMALLOCPROXY_EXPORT void *__libc_valloc(size_t size) __TBB_ALIAS_ATTR_COPY(valloc);
292292

293293
// call original __libc_* to support naive replacement of free via __libc_free etc
294-
void __libc_free(void *ptr)
294+
TBBMALLOCPROXY_EXPORT void __libc_free(void *ptr)
295295
{
296296
InitOrigPointers();
297297
__TBB_malloc_safer_free(ptr, (void (*)(void*))orig_libc_free);
298298
}
299299

300-
void *__libc_realloc(void *ptr, size_t size)
300+
TBBMALLOCPROXY_EXPORT void *__libc_realloc(void *ptr, size_t size)
301301
{
302302
InitOrigPointers();
303303
return __TBB_malloc_safer_realloc(ptr, size, orig_libc_realloc);
@@ -308,31 +308,31 @@ void *__libc_realloc(void *ptr, size_t size)
308308

309309
/*** replacements for global operators new and delete ***/
310310

311-
void* operator new(size_t sz) {
311+
TBBMALLOCPROXY_EXPORT void* operator new(size_t sz) {
312312
return InternalOperatorNew(sz);
313313
}
314-
void* operator new[](size_t sz) {
314+
TBBMALLOCPROXY_EXPORT void* operator new[](size_t sz) {
315315
return InternalOperatorNew(sz);
316316
}
317-
void operator delete(void* ptr) noexcept {
317+
TBBMALLOCPROXY_EXPORT void operator delete(void* ptr) noexcept {
318318
InitOrigPointers();
319319
__TBB_malloc_safer_free(ptr, (void (*)(void*))orig_free);
320320
}
321-
void operator delete[](void* ptr) noexcept {
321+
TBBMALLOCPROXY_EXPORT void operator delete[](void* ptr) noexcept {
322322
InitOrigPointers();
323323
__TBB_malloc_safer_free(ptr, (void (*)(void*))orig_free);
324324
}
325-
void* operator new(size_t sz, const std::nothrow_t&) noexcept {
325+
TBBMALLOCPROXY_EXPORT void* operator new(size_t sz, const std::nothrow_t&) noexcept {
326326
return scalable_malloc(sz);
327327
}
328-
void* operator new[](std::size_t sz, const std::nothrow_t&) noexcept {
328+
TBBMALLOCPROXY_EXPORT void* operator new[](std::size_t sz, const std::nothrow_t&) noexcept {
329329
return scalable_malloc(sz);
330330
}
331-
void operator delete(void* ptr, const std::nothrow_t&) noexcept {
331+
TBBMALLOCPROXY_EXPORT void operator delete(void* ptr, const std::nothrow_t&) noexcept {
332332
InitOrigPointers();
333333
__TBB_malloc_safer_free(ptr, (void (*)(void*))orig_free);
334334
}
335-
void operator delete[](void* ptr, const std::nothrow_t&) noexcept {
335+
TBBMALLOCPROXY_EXPORT void operator delete[](void* ptr, const std::nothrow_t&) noexcept {
336336
InitOrigPointers();
337337
__TBB_malloc_safer_free(ptr, (void (*)(void*))orig_free);
338338
}

src/tbbmalloc_proxy/proxy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2005-2021 Intel Corporation
2+
Copyright (c) 2005-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ extern "C" {
3737
TBBMALLOC_EXPORT size_t __TBB_malloc_safer_aligned_msize( void *ptr, size_t, size_t, size_t (*orig_msize_crt80d)(void*,size_t,size_t));
3838

3939
#if MALLOC_ZONE_OVERLOAD_ENABLED
40-
void __TBB_malloc_free_definite_size(void *object, size_t size);
40+
TBBMALLOC_EXPORT void __TBB_malloc_free_definite_size(void *object, size_t size);
4141
#endif
4242
} // extern "C"
4343

test/tbb/test_dynamic_link.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2005-2021 Intel Corporation
2+
Copyright (c) 2005-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@ enum FOO_TYPE {
3131
#if _WIN32 || _WIN64
3232
#define TEST_EXPORT
3333
#else
34-
#define TEST_EXPORT extern "C"
34+
#define TEST_EXPORT extern "C" __TBB_EXPORT
3535
#endif /* _WIN32 || _WIN64 */
3636

3737
// foo "implementations".

test/tbb/test_tbb_header.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2005-2023 Intel Corporation
2+
Copyright (c) 2005-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -165,6 +165,7 @@ static void TestExceptionClassesExports () {
165165
TestExceptionClassExports( std::out_of_range("test"), tbb::detail::exception_id::invalid_key );
166166
TestExceptionClassExports( tbb::user_abort(), tbb::detail::exception_id::user_abort );
167167
TestExceptionClassExports( std::runtime_error("test"), tbb::detail::exception_id::bad_tagged_msg_cast );
168+
TestExceptionClassExports( tbb::unsafe_wait("test"), tbb::detail::exception_id::unsafe_wait );
168169
}
169170

170171
#if __TBB_CPF_BUILD

0 commit comments

Comments
 (0)