Skip to content

Commit

Permalink
Fix externalization in command line macros, IBT tests
Browse files Browse the repository at this point in the history
This commit fixes a weird case where the SymbolExternalizer attempts
to rewrite a macro that was defined in the command line.

This also fixes the first IBT testcase, as well as add new ones that
are currently failing so we mark them as XFAIL.

linux tests are now in testsuite/linux.

Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Jun 5, 2024
1 parent 459bb1d commit e4c5381
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 23 deletions.
15 changes: 14 additions & 1 deletion libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ void TextModifications::Commit(void)

/* Insert into the list of FileIDs. */
const FileEntry *fentry = SM.getFileEntryForID(begin_id);

/* There are some cases where the fentry is known to return NULL. Check if
those are the cases we already acknownledged. */
if (fentry == nullptr) {
PresumedLoc ploc = SM.getPresumedLoc(a.ToChange.getBegin());
if (ploc.getFilename() == StringRef("<command line>")) {
/* Locations comming from the command line can be ignored. */
continue;
}

/* Crash with assertion. */
assert(fentry && "FileEntry is NULL on a non-acknowledged case");
}
/* Insert the FileEntry if we don't have one. */
if (FileEntryMap.find(fentry) == FileEntryMap.end()) {
/* Insert it. */
Expand Down Expand Up @@ -927,7 +940,7 @@ void SymbolExternalizer::Externalize_Symbols(std::vector<std::string> const &to_
FileID fi = sm.getMainFileID();
SourceLocation sl = sm.getLocForStartOfFile(fi);
SourceRange sr(sl, sl);
Insert_Text(sr, "#include <linux/livepatch.h>", 1000);
Insert_Text(sr, "#include <linux/livepatch.h>\n", 1000);
}

for (const std::string &to_externalize : to_externalize_array) {
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions testsuite/linux/ibt-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f -DCE_SYMVERS_PATH=../testsuite/linux/Modules.symvers -DCE_RENAME_SYMBOLS -nostdinc -I../testsuite/linux -DKBUILD_MODNAME=crc32c -D__USE_IBT__ -D__KERNEL__ -DCE_KEEP_INCLUDES" } */

typedef unsigned int u32;

u32 crc32c(u32 crc, const void *address, unsigned int length);

int f(void)
{
u32 lcrc = 0;
void *addr = 0;
unsigned int len = 0;

(void)crc32c(lcrc, addr, len);
return 0;
}

/* { dg-final { scan-tree-dump "u32 klpe_crc32c|u32 \(klpe_crc32c\)" } } */
/* { dg-final { scan-tree-dump "KLP_RELOC_SYMBOL\(crc32c, libcrc32c, crc32c\)" } } */
/* { dg-final { scan-tree-dump-not "\(\*klpe_crc32c\)" } } */
23 changes: 23 additions & 0 deletions testsuite/linux/ibt-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f -DCE_SYMVERS_PATH=../testsuite/linux/Modules.symvers -DCE_RENAME_SYMBOLS -nostdinc -I../testsuite/linux -DKBUILD_MODNAME=crc32c -D__USE_IBT__ -D__KERNEL__ -DCE_KEEP_INCLUDES" } */

/* Check why the include is not being output. */
/* { dg-xfail }*/

typedef unsigned int u32;

u32 crc32c(u32 crc, const void *address, unsigned int length);

int f(void)
{
u32 lcrc = 0;
void *addr = 0;
unsigned int len = 0;

(void)crc32c(lcrc, addr, len);
return 0;
}

/* { dg-final { scan-tree-dump "#include <linux/livepatch.h>" } } */
/* { dg-final { scan-tree-dump "u32 klpe_crc32c|u32 \(klpe_crc32c\)" } } */
/* { dg-final { scan-tree-dump "KLP_RELOC_SYMBOL\(crc32c, libcrc32c, crc32c\)" } } */
/* { dg-final { scan-tree-dump-not "\(\*klpe_crc32c\)" } } */
22 changes: 22 additions & 0 deletions testsuite/linux/ibt-3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f -DCE_SYMVERS_PATH=../testsuite/linux/Modules.symvers -DCE_RENAME_SYMBOLS -nostdinc -I../testsuite/linux -DKBUILD_MODNAME=crc32c -D__USE_IBT__ -D__KERNEL__ -DCE_KEEP_INCLUDES" } */
/* { dg-xfail } */

/* Check why parenthesis are being output in the redeclaration. */

typedef unsigned int u32;

u32 crc32c(u32 crc, const void *address, unsigned int length);

int f(void)
{
u32 lcrc = 0;
void *addr = 0;
unsigned int len = 0;

(void)crc32c(lcrc, addr, len);
return 0;
}

/* { dg-final { scan-tree-dump "u32 klpe_crc32c" } } */
/* { dg-final { scan-tree-dump "KLP_RELOC_SYMBOL\(crc32c, libcrc32c, crc32c\)" } } */
/* { dg-final { scan-tree-dump-not "\(\*klpe_crc32c\)" } } */
File renamed without changes.
Empty file added testsuite/linux/meson.build
Empty file.
2 changes: 1 addition & 1 deletion testsuite/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Author: Giuliano Belinassi

runtest = find_program('lib/runtest.py')
ordinary_test_dirs = [ 'small/', 'includes/', 'ccp/' ]
ordinary_test_dirs = [ 'small/', 'includes/', 'ccp/', 'linux' ]

returncode_to_bool = [ true, false ]

Expand Down
21 changes: 0 additions & 21 deletions testsuite/small/ibt-1.c

This file was deleted.

0 comments on commit e4c5381

Please sign in to comment.