[ModuleUtils][KCFI] Set patchable-function-prefix for synthesized functions
authorSami Tolvanen <samitolvanen@google.com>
Wed, 11 Jan 2023 23:06:20 +0000 (23:06 +0000)
committerSami Tolvanen <samitolvanen@google.com>
Wed, 11 Jan 2023 23:45:49 +0000 (23:45 +0000)
commitfd5e2627066075f3d15ef774ef368e08735a9ac9
tree094886bbd5b594d8c425da1524714f0515237c8f
parentdf87e62cbd4b5090b5349247d29457706d2ac4e8
[ModuleUtils][KCFI] Set patchable-function-prefix for synthesized functions

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
Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D141172
clang/lib/CodeGen/CodeGenModule.cpp
clang/test/CodeGen/kcfi.c
llvm/lib/Transforms/Utils/ModuleUtils.cpp
llvm/test/Instrumentation/AddressSanitizer/kcfi-offset.ll [new file with mode: 0644]