Skip to content

Commit 7be99e4

Browse files
authored
Merge pull request swiftlang#37084 from kateinoigakukun/katei/share-fatalerror-concurrency
Rename duplicated `swift::fatalError` in `swiftRuntime` and `swift_Concurrency`. Both `swiftRuntime` and `swift_Concurrency` had `swift::fatalError` implementation, but it causes symbol conflict with the `-static-stdlib` flag. This patch removes one of the implementations in `swift_Concurrency` to avoid conflicts. Also added a test case to ensure that linking the Concurrency module with `-static-stdlib` works. This issue was found by SwiftWasm test cases.
2 parents 9c02fd1 + f2fe95e commit 7be99e4

File tree

15 files changed

+141
-17
lines changed

15 files changed

+141
-17
lines changed

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
4242
CheckedContinuation.swift
4343
GlobalExecutor.cpp
4444
Errors.swift
45+
Error.cpp
4546
Executor.swift
4647
AsyncCompactMapSequence.swift
4748
AsyncDropFirstSequence.swift

stdlib/public/Concurrency/Error.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===--- Error.cpp - Error handling support code --------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "Error.h"
14+
15+
// swift::fatalError is not exported from libswiftCore and not shared, so define another
16+
// internal function instead.
17+
SWIFT_NORETURN void swift::swift_Concurrency_fatalError(uint32_t flags, const char *format, ...) {
18+
abort();
19+
}

stdlib/public/Concurrency/Error.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===--- Error.h - Swift Concurrency error helpers --------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Error handling support.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_CONCURRENCY_ERRORS_H
18+
#define SWIFT_CONCURRENCY_ERRORS_H
19+
20+
#include "../SwiftShims/Visibility.h"
21+
#include <cstdint>
22+
#include <stdlib.h>
23+
24+
namespace swift {
25+
26+
SWIFT_NORETURN void swift_Concurrency_fatalError(uint32_t flags, const char *format, ...);
27+
28+
} // namespace swift
29+
30+
#endif

stdlib/public/Concurrency/GlobalExecutor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "swift/Runtime/Concurrency.h"
5858
#include "swift/Runtime/EnvironmentVariables.h"
5959
#include "TaskPrivate.h"
60+
#include "Error.h"
6061

6162
#include <dispatch/dispatch.h>
6263

@@ -267,7 +268,7 @@ static std::atomic<dispatch_queue_t> globalQueueCache[globalQueueCacheCount];
267268
static dispatch_queue_t getGlobalQueue(JobPriority priority) {
268269
size_t numericPriority = static_cast<size_t>(priority);
269270
if (numericPriority >= globalQueueCacheCount)
270-
fatalError(0, "invalid job priority %#zx");
271+
swift_Concurrency_fatalError(0, "invalid job priority %#zx");
271272

272273
auto *ptr = &globalQueueCache[numericPriority];
273274
auto queue = ptr->load(std::memory_order_relaxed);

stdlib/public/Concurrency/Mutex.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "Error.h"
14+
15+
#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError
16+
1317
// Include the runtime's mutex support code.
1418
// FIXME: figure out some reasonable way to share this stuff
1519

1620
#include "../runtime/MutexPThread.cpp"
1721
#include "../runtime/MutexWin32.cpp"
18-
19-
SWIFT_NORETURN void swift::fatalError(uint32_t flags, const char *format, ...) {
20-
abort();
21-
}

stdlib/public/Concurrency/Task.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "TaskPrivate.h"
2626
#include "AsyncCall.h"
2727
#include "Debug.h"
28+
#include "Error.h"
2829

2930
#include <dispatch/dispatch.h>
3031

@@ -690,7 +691,7 @@ static void swift_task_future_waitImpl(OpaqueValue *result,
690691
return waitingTask->runInFullyEstablishedContext();
691692

692693
case FutureFragment::Status::Error:
693-
fatalError(0, "future reported an error, but wait cannot throw");
694+
swift_Concurrency_fatalError(0, "future reported an error, but wait cannot throw");
694695
}
695696
}
696697

stdlib/public/Concurrency/TaskAlloc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "swift/Runtime/Concurrency.h"
2121
#include "swift/ABI/Task.h"
2222
#include "TaskPrivate.h"
23+
#include "Error.h"
24+
25+
#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError
2326
#include "../runtime/StackAllocator.h"
2427
#include <stdlib.h>
2528

stdlib/public/runtime/MutexPThread.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
#endif
2121

2222
#if defined(_POSIX_THREADS) && !defined(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
23+
24+
// Notes: swift::fatalError is not shared between libswiftCore and libswift_Concurrency
25+
// and libswift_Concurrency uses swift_Concurrency_fatalError instead.
26+
#ifndef SWIFT_FATAL_ERROR
27+
#define SWIFT_FATAL_ERROR swift::fatalError
28+
#endif
29+
2330
#include "swift/Runtime/Mutex.h"
2431

2532
#include "swift/Runtime/Debug.h"
@@ -32,8 +39,8 @@ using namespace swift;
3239
do { \
3340
int errorcode = PThreadFunction; \
3441
if (errorcode != 0) { \
35-
fatalError(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \
36-
#PThreadFunction, errorName(errorcode), errorcode); \
42+
SWIFT_FATAL_ERROR(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \
43+
#PThreadFunction, errorName(errorcode), errorcode); \
3744
} \
3845
} while (false)
3946

@@ -44,8 +51,8 @@ using namespace swift;
4451
return true; \
4552
if (returnFalseOnEBUSY && errorcode == EBUSY) \
4653
return false; \
47-
fatalError(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \
48-
#PThreadFunction, errorName(errorcode), errorcode); \
54+
SWIFT_FATAL_ERROR(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \
55+
#PThreadFunction, errorName(errorcode), errorcode); \
4956
} while (false)
5057

5158
static const char *errorName(int errorcode) {

stdlib/public/runtime/MutexWin32.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#if defined(_WIN32)
19+
20+
// Notes: swift::fatalError is not shared between libswiftCore and libswift_Concurrency
21+
// and libswift_Concurrency uses swift_Concurrency_fatalError instead.
22+
#ifndef SWIFT_FATAL_ERROR
23+
#define SWIFT_FATAL_ERROR swift::fatalError
24+
#endif
25+
1926
#include "swift/Runtime/Mutex.h"
2027
#include "swift/Runtime/Debug.h"
2128

@@ -26,9 +33,9 @@ void ConditionPlatformHelper::wait(CONDITION_VARIABLE &condition,
2633
BOOL result = SleepConditionVariableSRW(&condition, &mutex, INFINITE, 0);
2734
if (!result) {
2835
DWORD errorcode = GetLastError();
29-
fatalError(/* flags = */ 0,
30-
"'SleepConditionVariableSRW()' failed with error code %d\n",
31-
errorcode);
36+
SWIFT_FATAL_ERROR(/* flags = */ 0,
37+
"'SleepConditionVariableSRW()' failed with error code %d\n",
38+
errorcode);
3239
}
3340
}
3441
#endif

stdlib/public/runtime/StackAllocator.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#include "llvm/Support/Alignment.h"
1919
#include <cstddef>
2020

21+
// Notes: swift::fatalError is not shared between libswiftCore and libswift_Concurrency
22+
// and libswift_Concurrency uses swift_Concurrency_fatalError instead.
23+
#ifndef SWIFT_FATAL_ERROR
24+
#define SWIFT_FATAL_ERROR swift::fatalError
25+
#endif
26+
2127
namespace swift {
2228

2329
/// A bump-pointer allocator that obeys a stack discipline.
@@ -151,7 +157,7 @@ class StackAllocator {
151157
if (guardAllocations) {
152158
auto *endOfAllocation = (uintptr_t *)getAddr(currentOffset);
153159
if (endOfAllocation[-1] != magicEndOfAllocation)
154-
fatalError(0, "Buffer overflow in StackAllocator");
160+
SWIFT_FATAL_ERROR(0, "Buffer overflow in StackAllocator");
155161
for (auto *p = (uintptr_t *)allocation; p < endOfAllocation; ++p)
156162
*p = magicUninitialized;
157163
}
@@ -256,7 +262,7 @@ class StackAllocator {
256262

257263
~StackAllocator() {
258264
if (lastAllocation)
259-
fatalError(0, "not all allocations are deallocated");
265+
SWIFT_FATAL_ERROR(0, "not all allocations are deallocated");
260266
(void)freeAllSlabs(firstSlabIsPreallocated ? firstSlab->next : firstSlab);
261267
assert(getNumAllocatedSlabs() == 0);
262268
}
@@ -277,7 +283,7 @@ class StackAllocator {
277283
/// Deallocate memory \p ptr.
278284
void dealloc(void *ptr) {
279285
if (!lastAllocation || lastAllocation->getAllocatedMemory() != ptr)
280-
fatalError(0, "freed pointer was not the last allocation");
286+
SWIFT_FATAL_ERROR(0, "freed pointer was not the last allocation");
281287

282288
Allocation *prev = lastAllocation->previous;
283289
lastAllocation->slab->deallocate(lastAllocation);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Statically link a program with concurrency module
2+
// REQUIRES: static_stdlib
3+
// REQUIRES: concurrency
4+
// REQUIRES: libdispatch_static
5+
6+
// RUN: %empty-directory(%t)
7+
// RUN: echo 'public func asyncFunc() async { print("Hello") }' > %t/asyncModule.swift
8+
9+
// RUN: %target-swiftc_driver -emit-library -emit-module -module-name asyncModule -module-link-name asyncModule %t/asyncModule.swift -static -static-stdlib -o %t/libasyncModule.a
10+
// TODO: "-ldispatch -lBlocksRuntime" should be told by asyncModule.swiftmodule transitively
11+
// RUN: %target-swiftc_driver -parse-as-library -static -static-stdlib -module-name main %s %import-static-libdispatch -I%t -L%t -ldispatch -lBlocksRuntime -o %t/main
12+
13+
// RUN: %t/main | %FileCheck %s
14+
// CHECK: Hello
15+
16+
// RUN: if [ %target-os == "linux-gnu" ]; \
17+
// RUN: then \
18+
// RUN: ldd %t/main | %FileCheck %s --check-prefix=LDD; \
19+
// RUN: fi
20+
21+
// LDD-NOT: libswiftCore.so
22+
// LDD-NOT: libswift_Concurrency.so
23+
24+
import asyncModule
25+
26+
@main
27+
struct Main {
28+
static func main() async {
29+
await asyncFunc()
30+
}
31+
}

test/lit.cfg

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,17 +1281,31 @@ elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-
12811281

12821282
libdispatch_artifact_dir = config.libdispatch_build_path
12831283
libdispatch_swift_module_dir = make_path(libdispatch_artifact_dir, 'src', 'swift', 'swift')
1284+
libdispatch_source_dir = make_path(config.swift_src_root, os.pardir, 'swift-corelibs-libdispatch')
12841285
libdispatch_artifacts = [
12851286
make_path(libdispatch_artifact_dir, 'libdispatch.so'),
12861287
make_path(libdispatch_artifact_dir, 'libswiftDispatch.so'),
12871288
make_path(libdispatch_swift_module_dir, 'Dispatch.swiftmodule')]
12881289
if (all(os.path.exists(p) for p in libdispatch_artifacts)):
12891290
config.available_features.add('libdispatch')
12901291
config.libdispatch_artifact_dir = libdispatch_artifact_dir
1291-
libdispatch_source_dir = make_path(config.swift_src_root, os.pardir, 'swift-corelibs-libdispatch')
12921292
config.import_libdispatch = ('-I %s -I %s -L %s'
12931293
% (libdispatch_source_dir, libdispatch_swift_module_dir, libdispatch_artifact_dir))
12941294

1295+
libdispatch_static_artifact_dir = config.libdispatch_static_build_path
1296+
libdispatch_swift_static_module_dir = make_path(libdispatch_static_artifact_dir, 'src', 'swift', 'swift')
1297+
libdispatch_static_artifacts = [
1298+
make_path(libdispatch_static_artifact_dir, 'src', 'libdispatch.a'),
1299+
make_path(libdispatch_static_artifact_dir, 'src', 'swift', 'libswiftDispatch.a'),
1300+
make_path(libdispatch_swift_static_module_dir, 'Dispatch.swiftmodule')]
1301+
if (all(os.path.exists(p) for p in libdispatch_static_artifacts)):
1302+
config.available_features.add('libdispatch_static')
1303+
config.import_libdispatch_static = ('-I %s -I %s -L %s -L %s -L %s'
1304+
% (libdispatch_source_dir, libdispatch_swift_static_module_dir,
1305+
make_path(libdispatch_static_artifact_dir, 'src'),
1306+
make_path(libdispatch_static_artifact_dir, 'src', 'BlocksRuntime'),
1307+
make_path(libdispatch_static_artifact_dir, 'src', 'swift')))
1308+
12951309
config.target_build_swift = (
12961310
'%s -target %s -toolchain-stdlib-rpath %s %s %s %s %s'
12971311
% (config.swiftc, config.variant_triple, resource_dir_opt, mcp_opt,
@@ -2110,6 +2124,7 @@ run_filecheck = '%s %s --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --use-fi
21102124
config.substitutions.append(('%FileCheck', run_filecheck))
21112125
config.substitutions.append(('%raw-FileCheck', shell_quote(config.filecheck)))
21122126
config.substitutions.append(('%import-libdispatch', getattr(config, 'import_libdispatch', '')))
2127+
config.substitutions.append(('%import-static-libdispatch', getattr(config, 'import_libdispatch_static', '')))
21132128

21142129
if config.lldb_build_root != "":
21152130
lldb_python_path = get_lldb_python_path(config.lldb_build_root)

test/lit.site.cfg.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ config.swift_test_results_dir = \
3434
config.coverage_mode = "@SWIFT_ANALYZE_CODE_COVERAGE@"
3535
config.lldb_build_root = "@LLDB_BUILD_DIR@"
3636
config.libdispatch_build_path = "@SWIFT_PATH_TO_LIBDISPATCH_BUILD@"
37+
config.libdispatch_static_build_path = "@SWIFT_PATH_TO_LIBDISPATCH_STATIC_BUILD@"
3738
config.swift_driver_test_options = "@SWIFT_DRIVER_TEST_OPTIONS@"
3839
config.swift_frontend_test_options = "@SWIFT_FRONTEND_TEST_OPTIONS@"
3940

utils/build-script-impl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,7 @@ for host in "${ALL_HOSTS[@]}"; do
19661966
-DSWIFT_PATH_TO_CMARK_BUILD:PATH="$(build_directory ${host} cmark)"
19671967
-DSWIFT_PATH_TO_LIBDISPATCH_SOURCE:PATH="${LIBDISPATCH_SOURCE_DIR}"
19681968
-DSWIFT_PATH_TO_LIBDISPATCH_BUILD:PATH="$(build_directory ${host} libdispatch)"
1969+
-DSWIFT_PATH_TO_LIBDISPATCH_STATIC_BUILD:PATH="$(build_directory ${host} libdispatch_static)"
19691970
)
19701971

19711972
if [[ ! "${SKIP_BUILD_LIBICU}" ]] ; then

validation-test/lit.site.cfg.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ else:
5656
config.coverage_mode = "@SWIFT_ANALYZE_CODE_COVERAGE@"
5757
config.lldb_build_root = "@LLDB_BUILD_DIR@"
5858
config.libdispatch_build_path = "@SWIFT_PATH_TO_LIBDISPATCH_BUILD@"
59+
config.libdispatch_static_build_path = "@SWIFT_PATH_TO_LIBDISPATCH_STATIC_BUILD@"
5960

6061
if "@SWIFT_ASAN_BUILD@" == "TRUE":
6162
config.available_features.add("asan")

0 commit comments

Comments
 (0)