This is an archive of the discontinued LLVM Phabricator instance.

[ModuleUtils][KCFI] Set patchable-function-prefix for synthesized functions
ClosedPublic

Authored by samitolvanen on Jan 6 2023, 4:45 PM.

Details

Summary

When -fpatchable-function-entry is used to emit prefix nops
before functions, KCFI assumes all indirectly called functions
have the same number of prefix nops, because the nops are emitted
between the KCFI type hash and the function entry. However, as
patchable-function-prefix is a function attribute set by Clang,
functions later synthesized by LLVM don't inherit this attribute
and end up not having prefix nops. One of these functions
is asan.module_ctor, which the Linux kernel ends up calling
indirectly when KASAN is enabled.

In order to avoid tripping KCFI, save the expected prefix offset
to a module flag, and use it when we're setting KCFI type for the
relevant synthesized functions.

Link: https://github.com/ClangBuiltLinux/linux/issues/1742

Diff Detail

Event Timeline

samitolvanen created this revision.Jan 6 2023, 4:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 6 2023, 4:45 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
samitolvanen requested review of this revision.Jan 6 2023, 4:45 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 6 2023, 4:45 PM
MaskRay accepted this revision.Jan 6 2023, 5:13 PM
MaskRay added inline comments.
llvm/lib/Transforms/Utils/ModuleUtils.cpp
169

if (unsigned Offset = MD->getZExtValue())

llvm/test/Instrumentation/AddressSanitizer/kcfi-offset.ll
2

test/Transforms/KCFI/kcfi-patchable-function-prefix.ll or a new file is perhaps a better place for this test. It's mainly about kcfi's requirement and less about asan.

This revision is now accepted and ready to land.Jan 6 2023, 5:13 PM
samitolvanen marked an inline comment as done.

Addressed feedback.

llvm/test/Instrumentation/AddressSanitizer/kcfi-offset.ll
2

That also occurred to me, but the Transforms/KCFI directory only contains tests for the KCFI pass, which doesn't support prefix nops, and this test is about asan.module_ctor with KCFI, so I thought keeping the test with AddressSanitizer tests would be more appropriate.