Skip to content
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

[UR] Fix cfi initialization #17472

Open
wants to merge 1 commit into
base: sycl
Choose a base branch
from

Conversation

RossBrunton
Copy link
Contributor

@RossBrunton RossBrunton commented Mar 14, 2025

There were a number of problems preventing CFI from being enabled, this hopefully fixes them.

@RossBrunton RossBrunton requested a review from a team as a code owner March 14, 2025 12:51
@RossBrunton RossBrunton requested a review from aarongreig March 14, 2025 12:52
@@ -154,7 +154,7 @@ function(add_ur_target_link_options name)
if(NOT MSVC)
if (NOT APPLE)
target_link_options(${name} PRIVATE
"LINKER:-z,relro,-z,now,-z,noexecstack ${CFI_FLAGS}"
"LINKER:-z,relro,-z,now,-z,noexecstack,${CFI_FLAGS}"
Copy link
Contributor

Choose a reason for hiding this comment

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

wait actually CFI_FLAGS is space separated so we're going to hit the same problem after the first flag, should we have a different comma separated one for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually these aren't link options at all, lld doesn't recognize them. Think we just need to remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I've looked into this further, I don't think the CFI stuff works at all, it should be:

set(CFI_FLAGS "-flto;-fsanitize=cfi;-fno-sanitize=cfi-icall;-fsanitize-ignorelist=${PROJECT_SOURCE_DIR}/sanitizer-ignorelist.txt")

Otherwise it gets parsed as one big argument even in target_compile_options(${name} PRIVATE ${CFI_FLAGS}).

I also don't think CFI_FLAGS should be included in the LINKER: section, since they aren't linker options. What was the motivation behind moving it here https://github.com/intel/llvm/pull/17452/files#diff-bb0766d43b4a66d03975ff909dbc08a14f03b104ac5acd72b2e670cecfe6e30fL154 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I was seeing similar quote-related problems here as with the compiler flags, where cmake was keeping the quotes in the final command because the link_options arg wasn't one big string. Figured the order didn't matter as long as they were still space separated as they (presumably) should have been before

Copy link
Contributor

Choose a reason for hiding this comment

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

fwiw I do get sensible looking compile commands with spaces rather than ;:

 -O3 -DNDEBUG -std=gnu++17 -fPIC -Wall -Wpedantic -Wempty-body -Wformat -Wformat-security -Wunused-parameter -fPIC -fstack-protector-strong -fvisibility=hidden -fcf-protection=full -fstack-clash-protection -fcolor-diagnostics -flto -fsanitize=cfi -fno-sanitize=cfi-icall -fsanitize-ignorelist=/path/to/sanitizer-ignorelist.txt -MD -MT 

Copy link
Contributor

Choose a reason for hiding this comment

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

I also don't think CFI_FLAGS should be included in the LINKER: section, since they aren't linker options. What was the motivation behind moving it here https://github.com/intel/llvm/pull/17452/files#diff-bb0766d43b4a66d03975ff909dbc08a14f03b104ac5acd72b2e670cecfe6e30fL154 ?

Those are all inside target_link_options() so will always be treated as linkger options, if they are not linker options they should be in a target_compile_options() instead.

fwiw I do get sensible looking compile commands with spaces rather than ;:

 -O3 -DNDEBUG -std=gnu++17 -fPIC -Wall -Wpedantic -Wempty-body -Wformat -Wformat-security -Wunused-parameter -fPIC -fstack-protector-strong -fvisibility=hidden -fcf-protection=full -fstack-clash-protection -fcolor-diagnostics -flto -fsanitize=cfi -fno-sanitize=cfi-icall -fsanitize-ignorelist=/path/to/sanitizer-ignorelist.txt -MD -MT 

If you don't surround a CMake list variable with quotes it will be treated as-if it's space separated when being parse in the context of the target_link_options().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By "linker options" I mean options passed to the linker itself via https://cmake.org/cmake/help/latest/command/target_link_options.html#handling-compiler-driver-differences .

The CFI flags should be passed to c++ directly, not via -Xlinker or so.

Copy link
Contributor

Choose a reason for hiding this comment

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

You don't pass flags to c++ with target_link_options(), is my point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is used to pass flags to c++ as part of the link step.

If they aren't passed, linking will fail because c++ isn't able to link the object files (since -flto doesn't use elf as its object file format).

@aarongreig aarongreig self-requested a review March 14, 2025 13:08
@RossBrunton RossBrunton changed the title [UR] Fix noexecstack [UR] Fix cfi initialization Mar 14, 2025
@@ -132,8 +132,6 @@ struct MsanShadowMemoryGPU : public MsanShadowMemory {
ur_mutex VirtualMemMapsMutex;

uptr LocalShadowOffset = 0;

uptr PrivateShadowOffset = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, otherwise building with clang fails because it is unused.

There were a number of problems preventing CFI from being enabled,
this hopefully fixes them.

As part of this, an unused variable compile error in `clang` was
fixed.
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.

3 participants