Skip to content

[lit] Use CMAKE_C_FLAGS and CMAKE_EXE_LINKER_FLAGS when compiling test plugin #70136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

drodriguez
Copy link
Contributor

In cases where the swift-plugin-server is built with specialized CMAKE_C_FLAGS and/or CMAKE_EXE_LINKER_FLAGS, if one wants to load a plugin in it, one needs to build with a compatible set of flags, or the plugin will not be able to be loaded.

Most of the time the tests can build against the / sysroot and be fine, but in the case of these test plugins, since they have to be loaded into the swift-plugin-server process, they need to compiled and linked in a compatible way. Using CMAKE_C_FLAGS and CMAKE_EXE_LINKER_FLAGS uses the same set of flags in both cases, and allow loading these test plugins during testing and avoid failures.

The change in lit.site.cfg.in embeds the values of CMAKE_C_FLAGS and CMAKE_EXE_LINKER_FLAGS from CMake into the Lit configuration. The usage of raw triple quoted strings allow single and double quoted arguments to survive the interpolation.

The change in lit.local.cfg creates two substitutions for them, and uses those substitutions in swift-build-cxx-plugin.

…t plugin

In cases where the swift-plugin-server is built with specialized `CMAKE_C_FLAGS`
and/or `CMAKE_EXE_LINKER_FLAGS`, if one wants to load a plugin in it,
one needs to build with a compatible set of flags, or the plugin will
not be able to be loaded.

Most of the time the tests can build against the `/` sysroot and be
fine, but in the case of these test plugins, since they have to be
loaded into the swift-plugin-server process, they need to compiled and
linked in a compatible way. Using `CMAKE_C_FLAGS` and
`CMAKE_EXE_LINKER_FLAGS` uses the same set of flags in both cases, and
allow loading these test plugins during testing and avoid failures.

The change in `lit.site.cfg.in` embeds the values of `CMAKE_C_FLAGS` and
`CMAKE_EXE_LINKER_FLAGS` from CMake into the Lit configuration. The
usage of raw triple quoted strings allow single and double quoted
arguments to survive the interpolation.

The change in `lit.local.cfg` creates two substitutions for them, and
uses those substitutions in `swift-build-cxx-plugin`.
@drodriguez drodriguez requested a review from compnerd November 30, 2023 22:33
@drodriguez
Copy link
Contributor Author

@swift-ci please test

@drodriguez
Copy link
Contributor Author

@compnerd I don't know if you want a similar change in the Windows branch. It seems to work for the Windows builds, so not sure if they need this extra flexibility.

@compnerd
Copy link
Member

Do you know specifically what flags are causing issues here?

@compnerd compnerd requested review from rintaro and bnbarham November 30, 2023 22:35
@drodriguez
Copy link
Contributor Author

Do you know specifically what flags are causing issues here?

In our case, we compile swift-plugin-server against an specific glibc version (2.34) of a different sysroot. When we compile these test plugins, instead of that specific glibc version, the compiler uses the one in /usr/lib (in our case it is 2.28) because no flag tells it otherwise. When the test executes, swift-plugin-server loads the the 2.34 version, and when the plugin is loaded, the symbols referenced by the test plugin (the ones that 2.28 provides) are nowhere to be found, so the plugin does not load and the test fails.

@compnerd
Copy link
Member

Ah, yes, that is a good point - we should match the host configuration for the tests. We get away with it on Windows because we don't have cross-compilation against different versions atm.

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍 Thanks @drodriguez

@@ -25,6 +25,8 @@ else:
0,
(
'%swift-build-cxx-plugin',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me think swift-build-cxx-plugin is the wrong name, since it's not C++ 😅.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want that change, I would prefer doing it in a different PR. It involves changing a lot of "callsites".

Copy link
Contributor

@bnbarham bnbarham Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fine, I just noticed because you added C_FLAGS and I was about to comment with "CXX_FLAGS?".

@drodriguez drodriguez merged commit b2e5d86 into swiftlang:main Dec 2, 2023
@drodriguez drodriguez deleted the test-plugin-compilation-weird-sysroots branch December 2, 2023 20:13
drodriguez added a commit to drodriguez/swift that referenced this pull request Dec 2, 2023
As pointed out in swiftlang#70136 (comment)
the substitution name does not make sense because we are compiling
C code, not C++.

This should not introduce any behavioural changes.
drodriguez added a commit to drodriguez/swift that referenced this pull request Dec 2, 2023
As pointed out in swiftlang#70136 (comment)
the substitution name does not make sense because we are compiling
C code, not C++.

This should not introduce any behavioural changes.
drodriguez added a commit that referenced this pull request Dec 4, 2023
…0188)

As pointed out in #70136 (comment)
the substitution name does not make sense because we are compiling
C code, not C++.

This should not introduce any behavioural changes.
@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

@drodriguez
Some plugin tests started failing in ASAN bot https://ci.swift.org/job/oss-swift-incremental-ASAN-RA-macos/4553/
Could this be the cause?

@drodriguez
Copy link
Contributor Author

drodriguez commented Dec 4, 2023

@drodriguez Some plugin tests started failing in ASAN bot https://ci.swift.org/job/oss-swift-incremental-ASAN-RA-macos/4553/ Could this be the cause?

Probably related. -fsanitize=address ends up in the CMAKE_C_FLAGS, it seems, which end up being used when compiling the plugins. I don't know if the plugins have address problems because the output is not very clear about what fails (some of the failures do not print a lot).

Do you prefer to revert and I will try to investigate ASan problems? It will take me a while to build an ASan build, I am afraid.

@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

FYI, tests are disabled in ASAN #70206
I started to building it as well, but if you have time, I'd appreciate it.

@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

The plugin failed with:

% /Users/rintaro/Repositories/swift-oss/build/Ninja-ReleaseAssert+asan/swift-macosx-arm64/test-macosx-arm64/Macros/Output/macro_swiftdeps.swift.tmp/mock-plugin
mock-plugin(80389,0x1dca2e5c0) malloc: nano zone abandoned due to inability to reserve vm space.
dyld[80389]: missing symbol called
zsh: abort      

@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               mock-plugin [80504]
Path:                  /Users/USER/*/mock-plugin
Identifier:            mock-plugin

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000

Termination Reason:    Namespace DYLD, Code 9 
missing symbol called

Application Specific Information:
missing symbol called


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   dyld                          	       0x185cde55c __abort_with_payload + 8
1   dyld                          	       0x185ceab10 abort_with_payload_wrapper_internal + 104
2   dyld                          	       0x185ceab44 abort_with_payload + 16
3   dyld                          	       0x185c71584 dyld4::halt(char const*, dyld4::StructuredError const*) + 304
4   dyld                          	       0x185ca9ba0 dyld4::APIs::_dyld_missing_symbol_abort() + 28
5   lib_swiftMockPlugin.dylib     	       0x104c58d14 asan.module_ctor + 16
6   dyld                          	       0x185c89a24 invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const::$_0::operator()() const + 168
7   dyld                          	       0x185ccf0f4 invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 172
8   dyld                          	       0x185cc2668 invocation function for block in dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const + 496
9   dyld                          	       0x185c692fc dyld3::MachOFile::forEachLoadCommand(Diagnostics&, void (load_command const*, bool&) block_pointer) const + 300
10  dyld                          	       0x185cc16a0 dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const + 192
11  dyld                          	       0x185cc4188 dyld3::MachOFile::forEachInitializerPointerSection(Diagnostics&, void (unsigned int, unsigned int, bool&) block_pointer) const + 160
12  dyld                          	       0x185ccede8 dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 432
13  dyld                          	       0x185c85b38 dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 524
14  dyld                          	       0x185c8bf70 dyld4::JustInTimeLoader::runInitializers(dyld4::RuntimeState&) const + 36
15  dyld                          	       0x185c85f24 dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 220
16  dyld                          	       0x185c85f00 dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 184
17  dyld                          	       0x185c89ab0 dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const::$_1::operator()() const + 112
18  dyld                          	       0x185c860f0 dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const + 380
19  dyld                          	       0x185cab4dc dyld4::APIs::runAllInitializersForMain() + 464
20  dyld                          	       0x185c6dfa0 dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*) + 3192
21  dyld                          	       0x185c6cedc start + 1844

@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

Apparently this is because lib_swiftMockPlugin.dylib is built with the builder machine's compiler. But mock-plugin is built with the just built clang.

@drodriguez
Copy link
Contributor Author

Thanks for all the clues. I am still in 1509/2444 when building the ASAN preset.

Maybe the trick is trying to match %swift-build-cxx-plugin to what it is used for lib_swiftMockPlugin.dylib.

If I understand https://github.com/apple/swift/blob/a7a0b329f21bf036cf65ebf073af8773b03eb934/cmake/modules/AddSwift.cmake#L149C4-L149C43 correctly, it might happen that add_swift_host_library will only add the sanitizer flags if a CMAKE_Swift_COMPILER is provided, which I am not sure if it is available in every case for lib_swiftMockPlugin.dylib.

I will keep waiting for the build to finish to be able to test theories on the actual compiled code.

@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

Yeah, I will open a PR to fix it. Basically, I will expose CMAKE_C_COMPILER via lit.site.cfg.in.

@rintaro
Copy link
Member

rintaro commented Dec 4, 2023

#70213

Catfish-Man pushed a commit to Catfish-Man/swift that referenced this pull request Jan 19, 2024
…iftlang#70188)

As pointed out in swiftlang#70136 (comment)
the substitution name does not make sense because we are compiling
C code, not C++.

This should not introduce any behavioural changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants