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

Check generated code for IBT compatibilty #38

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libcextract/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,16 @@ class FunctionExternalizerPass : public Pass
ClangCompat_None, ctx->OFS);
PrettyPrint::Set_Source_Manager(&ctx->AST->getSourceManager());

if (ctx->Ibt && ctx->AST) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe also check here is Kernel was set? Maybe we should only set Ibt is Kernel was set in the first place...

/* Do a sanity check on IBT macros. Some kernel branches can't use it,
so do a check here for sanity reasons. */
Preprocessor &pp = ctx->AST->getPreprocessor();
if (pp.isMacroDefined("KLP_RELOC_SYMBOL") == false) {
throw std::runtime_error("KLP_RELOC_SYMBOL not defined, kernel may not "
"be compatible with IBT.");
}
}

const DiagnosticsEngine &de = ctx->AST->getDiagnostics();
return !de.hasErrorOccurred();
}
Expand Down
18 changes: 11 additions & 7 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,17 @@ bool SymbolExternalizer::_Externalize_Symbol(const std::string &to_externalize,
/* Create a string with the new variable type and name. */
std::string o;
llvm::raw_string_ostream outstr(o);

if (Ibt && !EmittedLinuxLivepatch) {
outstr << "#ifndef KLP_RELOC_SYMBOL_POS\n"
"# define KLP_RELOC_SYMBOL_POS(LP_OBJ_NAME, SYM_OBJ_NAME, SYM_NAME, SYM_POS) \\\n"
" asm(\"\\\".klp.sym.rela.\" #LP_OBJ_NAME \".\" #SYM_OBJ_NAME \".\" #SYM_NAME \",\" #SYM_POS \"\\\"\")\n"
"# define KLP_RELOC_SYMBOL(LP_OBJ_NAME, SYM_OBJ_NAME, SYM_NAME) \\\n"
" KLP_RELOC_SYMBOL_POS(LP_OBJ_NAME, SYM_OBJ_NAME, SYM_NAME, 0)\n"
"#endif\n\n";
EmittedLinuxLivepatch = true;
}

new_decl->print(outstr, AST->getLangOpts());

if (Ibt) {
Expand Down Expand Up @@ -961,13 +972,6 @@ void SymbolExternalizer::Externalize_Symbol(const std::string &to_externalize)

void SymbolExternalizer::Externalize_Symbols(std::vector<std::string> const &to_externalize_array)
{
if (Ibt) {
SourceManager &sm = AST->getSourceManager();
FileID fi = sm.getMainFileID();
SourceLocation sl = sm.getLocForStartOfFile(fi);
Insert_Text(sl, "#include <linux/livepatch.h>\n");
}

for (const std::string &to_externalize : to_externalize_array) {
Externalize_Symbol(to_externalize);
}
Expand Down
4 changes: 4 additions & 0 deletions libcextract/SymbolExternalizer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class SymbolExternalizer
TM(ast, dump),
IA(ia),
Ibt(ibt),
EmittedLinuxLivepatch(false),
PatchObject(patch_object)
{
}
Expand Down Expand Up @@ -295,6 +296,9 @@ class SymbolExternalizer
/** Defines the method that a private symbol will be searched. */
bool Ibt;

/** Flag if we already emit the #include<linux/livepatch.h> */
bool EmittedLinuxLivepatch;

/* Name of the object that will be patched. */
std::string PatchObject;
};
Loading